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..505c8b55d52 100644 --- a/packages-next/keystone/src/lib/createKeystone.ts +++ b/packages-next/keystone/src/lib/createKeystone.ts @@ -1,31 +1,18 @@ import { PrismaAdapter } from '@keystone-next/adapter-prisma-legacy'; -import type { KeystoneConfig, BaseKeystone } from '@keystone-next/types'; +import type { KeystoneConfig, BaseKeystone, DatabaseProvider } from '@keystone-next/types'; import { Keystone } from './core/Keystone/index'; -export function createKeystone(config: KeystoneConfig, prismaClient?: any) { +export function createKeystone( + config: KeystoneConfig, + provider: DatabaseProvider, + 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..bb7b52ba30d 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, DatabaseProvider } from '@keystone-next/types'; import { createGraphQLSchema } from './createGraphQLSchema'; import { makeCreateContext } from './context/createContext'; import { createKeystone } from './createKeystone'; +export function getDBProvider(db: KeystoneConfig['db']): DatabaseProvider { + 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..5aac541618d 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 DatabaseProvider = 'sqlite' | 'postgresql'; + export type FieldDefaultValue = | T | null