From 7451d50502e2cbd9eab3c10c210feb2adf356975 Mon Sep 17 00:00:00 2001 From: Tim Leslie Date: Tue, 11 May 2021 16:52:23 +1000 Subject: [PATCH] Add getDBProvider --- .changeset/short-cougars-travel.md | 6 +++++ .../keystone/src/lib/createKeystone.ts | 23 +++---------------- .../keystone/src/lib/createSystem.ts | 18 +++++++++++++-- packages-next/types/src/core.ts | 2 ++ 4 files changed, 27 insertions(+), 22 deletions(-) create mode 100644 .changeset/short-cougars-travel.md diff --git a/.changeset/short-cougars-travel.md b/.changeset/short-cougars-travel.md new file mode 100644 index 00000000000..7445b086204 --- /dev/null +++ b/.changeset/short-cougars-travel.md @@ -0,0 +1,6 @@ +--- +'@keystone-next/keystone': patch +'@keystone-next/types': patch +--- + +Refactored code to parse `config.db`. No functional changes. diff --git a/packages-next/keystone/src/lib/createKeystone.ts b/packages-next/keystone/src/lib/createKeystone.ts index cd532498a81..a6e038bcd38 100644 --- a/packages-next/keystone/src/lib/createKeystone.ts +++ b/packages-next/keystone/src/lib/createKeystone.ts @@ -1,31 +1,14 @@ import { PrismaAdapter } from '@keystone-next/adapter-prisma-legacy'; -import type { KeystoneConfig, BaseKeystone } from '@keystone-next/types'; +import type { KeystoneConfig, BaseKeystone, Provider } from '@keystone-next/types'; import { Keystone } from './core/Keystone/index'; -export function createKeystone(config: KeystoneConfig, prismaClient?: any) { +export function createKeystone(config: KeystoneConfig, provider: Provider, prismaClient?: any) { // Note: For backwards compatibility we may want to expose // this as a public API so that users can start their transition process // by using this pattern for creating their Keystone object before using // it in their existing custom servers or original CLI systems. const { db, graphql, lists } = config; - let adapter: PrismaAdapter; - if (db.adapter === 'prisma_postgresql' || db.provider === 'postgresql') { - adapter = new PrismaAdapter({ - prismaClient, - ...db, - provider: 'postgresql', - }); - } else if (db.adapter === 'prisma_sqlite' || db.provider === 'sqlite') { - adapter = new PrismaAdapter({ - prismaClient, - ...db, - provider: 'sqlite', - }); - } else { - throw new Error( - 'Invalid db configuration. Please specify db.provider as either "sqlite" or "postgresql"' - ); - } + const adapter = new PrismaAdapter({ prismaClient, ...db, provider }); // @ts-ignore The @types/keystonejs__keystone package has the wrong type for KeystoneOptions const keystone: BaseKeystone = new Keystone({ adapter, diff --git a/packages-next/keystone/src/lib/createSystem.ts b/packages-next/keystone/src/lib/createSystem.ts index 3bb9736918d..def07241027 100644 --- a/packages-next/keystone/src/lib/createSystem.ts +++ b/packages-next/keystone/src/lib/createSystem.ts @@ -1,11 +1,25 @@ -import type { KeystoneConfig } from '@keystone-next/types'; +import type { KeystoneConfig, Provider } from '@keystone-next/types'; import { createGraphQLSchema } from './createGraphQLSchema'; import { makeCreateContext } from './context/createContext'; import { createKeystone } from './createKeystone'; +export function getDBProvider(db: KeystoneConfig['db']): Provider { + if (db.adapter === 'prisma_postgresql' || db.provider === 'postgresql') { + return 'postgresql'; + } else if (db.adapter === 'prisma_sqlite' || db.provider === 'sqlite') { + return 'sqlite'; + } else { + throw new Error( + 'Invalid db configuration. Please specify db.provider as either "sqlite" or "postgresql"' + ); + } +} + export function createSystem(config: KeystoneConfig, prismaClient?: any) { - const keystone = createKeystone(config, prismaClient); + const provider = getDBProvider(config.db); + + const keystone = createKeystone(config, provider, prismaClient); const graphQLSchema = createGraphQLSchema(config, keystone, 'public'); diff --git a/packages-next/types/src/core.ts b/packages-next/types/src/core.ts index 9b5191598de..b037a85ae90 100644 --- a/packages-next/types/src/core.ts +++ b/packages-next/types/src/core.ts @@ -5,6 +5,8 @@ import type { KeystoneContext, SessionContext } from './context'; /* TODO: Review these types */ type FieldDefaultValueArgs = { context: KeystoneContext; originalInput?: T }; +export type Provider = 'sqlite' | 'postgresql'; + export type FieldDefaultValue = | T | null