Skip to content

Commit

Permalink
Add support for Prisma to create-keystone-app
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie committed Oct 12, 2020
1 parent b6b5379 commit f814822
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 28 deletions.
28 changes: 2 additions & 26 deletions docs/guides/prisma.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,38 +41,14 @@ yarn create keystone-app my-app
```

- Call your project `my-app`
- Select `PostgreSQL` as your database type.
- Select `Prisma (Experimental)` as your database type.
- Provide the connection URL, including username and password, e.g. `postgres://keystone5:change_me_plz@localhost:5432/keystone`
- Select `Todo` application as your starter project.

This will create a fresh project for you, which uses the `knex` database adapter. We're going to take this project and modify it to use the Prisma adapter.

> **Note:** In upcoming releases the Prisma adapter will be available as an option in `create-keystone-app`.
Next, go into your new project directory and install the Prisma adapter package.
Your project is now ready to run! Run the following commands (make sure to use the connection string for your database!), and Keystone will start your project

```
cd my-app
yarn add @keystonejs/adapter-prisma
```

You can now open up `index.js` and edit it to use the `PrismaAdapter` rather than the `KnexAdapter`. Make the following changes:

```diff
-const { KnexAdapter: Adapter } = require('@keystonejs/adapter-knex');
+const { PrismaAdapter: Adapter } = require('@keystonejs/adapter-prisma');
```

and

```diff
-const adapterConfig = { knexOptions: { connection: 'postgres://keystone5:change_me_plz@localhost:5432/keystone } };
+const adapterConfig = { url: 'postgres://keystone5:change_me_plz@localhost:5432/keystone' };
```

Your project is now ready to run! Run the following command (make sure to use the connection string for your database!), and Keystone will start your project

```
DATABASE_URL=postgres://keystone5:change_me_plz@localhost:5432/keystone yarn dev
```

Expand Down
2 changes: 2 additions & 0 deletions packages/create-keystone-app/lib/generate-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ const generateCode = async () => {
const adapterConfig = {
MongoDB: `{ mongoUri: '${await getAdapterConfig()}' }`,
PostgreSQL: `{ knexOptions: { connection: '${await getAdapterConfig()}' } }`,
Prisma: '{}',
}[adapterChoice.key];

const adapterRequire = {
MongoDB: `const { MongooseAdapter: Adapter } = require('@keystonejs/adapter-mongoose');`,
PostgreSQL: `const { KnexAdapter: Adapter } = require('@keystonejs/adapter-knex');`,
Prisma: `const { PrismaAdapter: Adapter } = require('@keystonejs/adapter-prisma');`,
}[adapterChoice.key];

return `${adapterRequire}
Expand Down
8 changes: 8 additions & 0 deletions packages/create-keystone-app/lib/get-adapter-choice.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ const adapters = {
removeDependencies: ['@keystonejs/adapter-mongoose'],
defaultConfig: name => `postgres://localhost/${slugify(name, { separator: '_' })}`,
},
Prisma: {
name: 'Prisma (Experimental)',
file: 'adapter-prisma.js',
dependencies: ['@keystonejs/adapter-prisma'],
description: 'Connect to a PostgreSQL database with prisma (Experimental).',
removeDependencies: ['@keystonejs/adapter-mongoose'],
defaultConfig: name => `postgres://localhost/${slugify(name, { separator: '_' })}`,
},
};

const choices = Object.entries(adapters).map(([key, value]) => ({
Expand Down
4 changes: 3 additions & 1 deletion packages/create-keystone-app/lib/show-success-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ const path = require('path');
const terminalLink = require('terminal-link');
const { getProjectDirectory } = require('./util');
const { getAdapterChoice } = require('./get-adapter-choice');
const { getAdapterConfig } = require('./get-adapter-config');

const showSuccessMessage = async () => {
const projectDir = await getProjectDirectory();
const adapterChoice = await getAdapterChoice();
let knexMessage = '';
const adapterConfig = await getAdapterConfig();
if (adapterChoice.key === 'PostgreSQL') {
knexMessage = `
${c.bold(' Before you run Keystone you will need to initialise the tables in your database:')}
Expand All @@ -28,7 +30,7 @@ ${c.bold(' Before you run Keystone you will need to initialise the tables in yo
${c.bold('To launch your app, run:')}
- cd ${projectDir}
- yarn dev
- ${adapterChoice.key === 'Prisma' ? `DATABASE_URL=${adapterConfig} yarn dev` : 'yarn dev'}
${c.bold('Next steps:')}
Expand Down
8 changes: 7 additions & 1 deletion packages/create-keystone-app/lib/test-adapter-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,16 @@ const testAdapterConnection = async () => {
const adapterChoice = await getAdapterChoice();
const config = await getAdapterConfig();

const Adapter = { MongoDB: MongooseAdapter, PostgreSQL: KnexAdapter }[adapterChoice.key];
// We currently test the connection of the Prisma adapter via the KnexAdapter
// This is fine for now, what we're trying to verify here is that the connection
// string passed in is something we can actually connect to.
const Adapter = { MongoDB: MongooseAdapter, PostgreSQL: KnexAdapter, Prisma: KnexAdapter }[
adapterChoice.key
];
const adapterConfig = {
MongoDB: { mongoUri: config },
PostgreSQL: { knexOptions: { connection: config } },
Prisma: { knexOptions: { connection: config } },
}[adapterChoice.key];
const adapter = new Adapter(adapterConfig);
try {
Expand Down
1 change: 1 addition & 0 deletions packages/create-keystone-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"dependencies": {
"@keystonejs/adapter-knex": "^12.0.0",
"@keystonejs/adapter-mongoose": "^10.0.0",
"@keystonejs/adapter-prisma": "^1.0.3",
"@sindresorhus/slugify": "^0.11.0",
"arg": "^4.1.3",
"cfonts": "^2.8.6",
Expand Down

0 comments on commit f814822

Please sign in to comment.