Skip to content

Commit

Permalink
Add db.provider to replace db.adapter (#5368)
Browse files Browse the repository at this point in the history
* Add db.provider to replace db.adapter

* Update deprecation messages
  • Loading branch information
timleslie authored Apr 6, 2021
1 parent 26cb0c4 commit b400163
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 25 deletions.
8 changes: 8 additions & 0 deletions .changeset/hungry-ladybugs-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@keystone-next/website': minor
'@keystone-next/keystone': minor
'@keystone-next/types': minor
'@keystone-next/test-utils-legacy': patch
---

The config option `db.adapter` is now deprecated. It has been repaced with `db.provider` which can take the values `postgresql` or `sqlite`.
14 changes: 7 additions & 7 deletions docs-next/pages/apis/config.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ import type { DatabaseConfig } from '@keystone-next/types';
The `db` config option configures the database used to store data in your Keystone system.
It has a TypeScript type of `DatabaseConfig`.
Keystone supports the database types **PostgreSQL** and **SQLite**.
These database types are powered by their corresponding Keystone database adapters; `prisma_postgresql` and `prisma_sqlite`.
These database types are powered by their corresponding Prisma database providers; `postgresql` and `sqlite`.

All database adapters require the `url` argument, which defines the connection URL for your database.
They also all have an optional `onConnect` async function, which takes a [`KeystoneContext`](./context) object, and lets perform any actions you might need at startup, such as data seeding.

As well as these common options, each adapter supports a number of optional advanced configuration options.
As well as these common options, each provider supports a number of optional advanced configuration options.

### prisma_postgresql
### postgresql

Advanced configuration:

Expand All @@ -74,7 +74,7 @@ Advanced configuration:
```typescript
export default config({
db: {
adapter: 'prisma_postgresql',
provider: 'postgresql',
url: 'postgres://dbuser:dbpass@localhost:5432/keystone',
onConnect: async context => { /* ... */ },
// Optional advanced configuration
Expand All @@ -85,7 +85,7 @@ export default config({
});
```

### prisma_sqlite
### sqlite

Advanced configuration:

Expand All @@ -95,7 +95,7 @@ Advanced configuration:
```typescript
export default config({
db: {
adapter: 'prisma_sqlite',
provider: 'sqlite',
url: 'file:./keystone.db',
onConnect: async context => { /* ... */ },
// Optional advanced configuration
Expand All @@ -108,7 +108,7 @@ export default config({

#### Limitations

The `prisma_sqlite` is not intended to be used in production systems, and has certain limitations:
The `sqlite` provider is not intended to be used in production systems, and has certain limitations:

- `document`: The `document` field type is not supported.
- `decimal`: The `decimal` field type is not supported.
Expand Down
2 changes: 1 addition & 1 deletion packages-next/keystone/src/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ const makeVercelIncludeTheSQLiteDB = (
directoryOfFileToBeWritten: string,
config: KeystoneConfig
) => {
if (config.db.adapter === 'prisma_sqlite') {
if (config.db.adapter === 'prisma_sqlite' || config.db.provider === 'sqlite') {
const sqliteDbAbsolutePath = path.resolve(cwd, config.db.url.replace('file:', ''));

return `import path from 'path';
Expand Down
7 changes: 1 addition & 6 deletions packages-next/keystone/src/lib/applyIdFieldDefaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@ export function applyIdFieldDefaults(config: KeystoneConfig): KeystoneConfig['li
)} list. This is not allowed, use the idField option instead.`
);
}
let idField =
config.lists[key].idField ??
{
prisma_postgresql: autoIncrement({}),
prisma_sqlite: autoIncrement({}),
}[config.db.adapter];
let idField = config.lists[key].idField ?? autoIncrement({});
idField = {
...idField,
config: {
Expand Down
4 changes: 2 additions & 2 deletions packages-next/keystone/src/lib/createKeystone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ export function createKeystone(config: KeystoneConfig, prismaClient?: any) {
// it in their existing custom servers or original CLI systems.
const { db, graphql, lists } = config;
let adapter;
if (db.adapter === 'prisma_postgresql') {
if (db.adapter === 'prisma_postgresql' || db.provider === 'postgresql') {
adapter = new PrismaAdapter({
prismaClient,
...db,
provider: 'postgresql',
});
} else if (db.adapter === 'prisma_sqlite') {
} else if (db.adapter === 'prisma_sqlite' || db.provider === 'sqlite') {
adapter = new PrismaAdapter({
prismaClient,
...db,
Expand Down
32 changes: 26 additions & 6 deletions packages-next/types/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,36 @@ export type DatabaseCommon = {

export type DatabaseConfig = DatabaseCommon &
(
| {
adapter: 'prisma_postgresql';
| ((
| {
/** @deprecated The `adapter` option is deprecated. Please use `{ provider: 'postgresql' }` */
adapter: 'prisma_postgresql';
provider?: undefined;
}
| {
/** @deprecated The `adapter` option is deprecated. Please use `{ provider: 'postgresql' }` */
adapter?: undefined;
provider: 'postgresql';
}
) & {
useMigrations?: boolean;
enableLogging?: boolean;
}
| {
adapter: 'prisma_sqlite';
})
| ((
| {
/** @deprecated The `adapter` option is deprecated. Please use `{ provider: 'sqlite' }` */
adapter: 'prisma_sqlite';
provider?: undefined;
}
| {
/** @deprecated The `adapter` option is deprecated. Please use `{ provider: 'sqlite' }` */
adapter?: undefined;
provider: 'sqlite';
}
) & {
useMigrations?: boolean;
enableLogging?: boolean;
}
})
);

// config.ui
Expand Down
6 changes: 3 additions & 3 deletions packages/test-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ const hashPrismaSchema = memoizeOne(prismaSchema =>
const argGenerator = {
prisma_postgresql: () => ({
url: process.env.DATABASE_URL!,
provider: 'postgresql',
provider: 'postgresql' as const,
getDbSchemaName: () => null as any,
// Turn this on if you need verbose debug info
enableLogging: false,
}),
prisma_sqlite: () => ({
url: process.env.DATABASE_URL!,
provider: 'sqlite',
provider: 'sqlite' as const,
// Turn this on if you need verbose debug info
enableLogging: false,
}),
Expand All @@ -58,7 +58,7 @@ async function setupFromConfig({
const adapterArgs = await argGenerator[adapterName]();
const config = initConfig({
..._config,
db: { adapter: adapterName, ...adapterArgs },
db: adapterArgs,
ui: { isDisabled: true },
});

Expand Down

1 comment on commit b400163

@vercel
Copy link

@vercel vercel bot commented on b400163 Apr 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.