Skip to content

Commit

Permalink
move some defaults to initConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
dcousens committed Feb 12, 2024
1 parent 117efcc commit 0776cfe
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 27 deletions.
8 changes: 4 additions & 4 deletions packages/core/src/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { printGeneratedTypes } from './lib/schema-type-printer'
import { ExitError } from './scripts/utils'
import { initialiseLists } from './lib/core/initialise-lists'
import { printPrismaSchema } from './lib/core/prisma-schema-printer'
import { initConfig } from './lib/config'
import { initConfig } from './system'

export function getFormattedGraphQLSchema (schema: string) {
return (
Expand Down Expand Up @@ -93,19 +93,19 @@ export function getSystemPaths (cwd: string, config: KeystoneConfig) {
: null

const builtTypesPath = config.types?.path
? path.join(cwd, config.types.path)
? path.join(cwd, config.types.path) // TODO: enforce initConfig before getSystemPaths
: path.join(cwd, 'node_modules/.keystone/types.ts')

const builtPrismaPath = config.db?.prismaSchemaPath
? path.join(cwd, config.db.prismaSchemaPath)
? path.join(cwd, config.db.prismaSchemaPath) // TODO: enforce initConfig before getSystemPaths
: path.join(cwd, 'schema.prisma')

const relativePrismaPath = prismaClientPath
? `./${posixify(path.relative(path.dirname(builtTypesPath), prismaClientPath))}`
: '@prisma/client'

const builtGraphqlPath = config.graphql?.schemaPath
? path.join(cwd, config.graphql.schemaPath)
? path.join(cwd, config.graphql.schemaPath) // TODO: enforce initConfig before getSystemPaths
: path.join(cwd, 'schema.graphql')

return {
Expand Down
8 changes: 6 additions & 2 deletions packages/core/src/context.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import type { BaseKeystoneTypeInfo, KeystoneConfig, KeystoneContext } from './types'
import { initConfig } from './lib/config'
import {
type BaseKeystoneTypeInfo,
type KeystoneConfig,
type KeystoneContext
} from './types'
import { initConfig } from './system'
import { createSystem } from './lib/createSystem'

export function getContext<TypeInfo extends BaseKeystoneTypeInfo> (
Expand Down
19 changes: 1 addition & 18 deletions packages/core/src/lib/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { KeystoneConfig } from '../types'
import { idFieldType } from './id-field'

function applyIdFieldDefaults (config: KeystoneConfig): KeystoneConfig['lists'] {
export function applyIdFieldDefaults (config: KeystoneConfig): KeystoneConfig['lists'] {
// some error checking
for (const [listKey, list] of Object.entries(config.lists)) {
if (list.fields.id) {
Expand Down Expand Up @@ -53,20 +53,3 @@ function applyIdFieldDefaults (config: KeystoneConfig): KeystoneConfig['lists']

return listsWithIds
}

export function initConfig (config: KeystoneConfig) {
if (!['postgresql', 'sqlite', 'mysql'].includes(config.db.provider)) {
throw new TypeError(
'Invalid db configuration. Please specify db.provider as either "sqlite", "postgresql" or "mysql"'
)
}

// WARNING: Typescript should prevent this, but empty string is useful for Prisma errors
config.db.url ??= 'postgres://'

// TODO: use zod or something if want to follow this path
return {
...config,
lists: applyIdFieldDefaults(config),
}
}
35 changes: 32 additions & 3 deletions packages/core/src/system.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,45 @@
import type express from 'express'
import next from 'next'
import type { KeystoneConfig, KeystoneContext } from './types'
import {
type KeystoneConfig,
type KeystoneContext
} from './types'
import { createAdminUIMiddlewareWithNextApp } from './lib/server/createAdminUIMiddleware'
import { applyIdFieldDefaults } from './lib/config'

/** @deprecated */
export { createSystem } from './lib/createSystem'

/** @deprecated */
export { createExpressServer } from './lib/server/createExpressServer'

/** @deprecated */
export { initConfig } from './lib/config'
/** @deprecated, TODO: remove in breaking change */
export function initConfig (config: KeystoneConfig) {
if (!['postgresql', 'sqlite', 'mysql'].includes(config.db.provider)) {
throw new TypeError(
'Invalid db configuration. Please specify db.provider as either "sqlite", "postgresql" or "mysql"'
)
}

// WARNING: Typescript should prevent this, but any string is useful for Prisma errors
config.db.url ??= 'postgres://'

return {
types: {
path: 'node_modules/.keystone/types.ts'
},
...config,
db: {
prismaSchemaPath: 'schema.prisma',
...config.db,
},
graphql: {
schemaPath: 'schema.graphql',
...config.graphql,
},
lists: applyIdFieldDefaults(config),
}
}

/** @deprecated */
export async function createAdminUIMiddleware (
Expand Down

0 comments on commit 0776cfe

Please sign in to comment.