From 56e5fe10bc89877be7d7e3013e53012b4d82b648 Mon Sep 17 00:00:00 2001 From: Mitchell Hamilton Date: Wed, 10 Mar 2021 14:49:19 +1000 Subject: [PATCH] Change createKeystone and createSystem to accept a migration mode rather than script (#5087) * Change createKeystone to accept a migration mode rather than script * Update nice-crews-jog.md --- .changeset/new-bears-train.md | 5 +++++ .changeset/nice-crews-jog.md | 7 +++++++ packages-next/admin-ui/src/templates/api.ts | 2 +- packages-next/keystone/src/lib/createKeystone.ts | 13 +++---------- packages-next/keystone/src/lib/createSystem.ts | 6 +++--- packages-next/keystone/src/scripts/build/build.ts | 2 +- .../keystone/src/scripts/migrate/deploy.ts | 2 +- .../keystone/src/scripts/migrate/generate.ts | 2 +- packages-next/keystone/src/scripts/migrate/reset.ts | 2 +- packages-next/keystone/src/scripts/run/dev.ts | 8 ++++++-- packages-next/keystone/src/scripts/run/start.ts | 2 +- packages-next/types/src/core.ts | 2 ++ packages/test-utils/src/index.ts | 2 +- 13 files changed, 33 insertions(+), 22 deletions(-) create mode 100644 .changeset/new-bears-train.md create mode 100644 .changeset/nice-crews-jog.md diff --git a/.changeset/new-bears-train.md b/.changeset/new-bears-train.md new file mode 100644 index 00000000000..b68389dab0d --- /dev/null +++ b/.changeset/new-bears-train.md @@ -0,0 +1,5 @@ +--- +'@keystone-next/types': minor +--- + +Added `MigrationMode` type diff --git a/.changeset/nice-crews-jog.md b/.changeset/nice-crews-jog.md new file mode 100644 index 00000000000..ef9a4de884c --- /dev/null +++ b/.changeset/nice-crews-jog.md @@ -0,0 +1,7 @@ +--- +'@keystone-next/keystone': major +'@keystone-next/admin-ui': major +'@keystone-next/test-utils-legacy': major +--- + +Updated `createKeystone` and `createSystem` to accept a migration mode rather than script diff --git a/packages-next/admin-ui/src/templates/api.ts b/packages-next/admin-ui/src/templates/api.ts index 9f4009818bd..bebb96ba3e5 100644 --- a/packages-next/admin-ui/src/templates/api.ts +++ b/packages-next/admin-ui/src/templates/api.ts @@ -11,7 +11,7 @@ import { initConfig, createSystem, createApolloServerMicro } from '@keystone-nex import { PrismaClient } from '../../../../../prisma/generated-client'; const initializedKeystoneConfig = initConfig(keystoneConfig); -const { graphQLSchema, keystone, createContext } = createSystem(initializedKeystoneConfig, '.keystone', 'start', PrismaClient); +const { graphQLSchema, keystone, createContext } = createSystem(initializedKeystoneConfig, '.keystone', 'none', PrismaClient); const apolloServer = createApolloServerMicro({ graphQLSchema, createContext, diff --git a/packages-next/keystone/src/lib/createKeystone.ts b/packages-next/keystone/src/lib/createKeystone.ts index 1d1981fb20f..d10c084e240 100644 --- a/packages-next/keystone/src/lib/createKeystone.ts +++ b/packages-next/keystone/src/lib/createKeystone.ts @@ -7,12 +7,12 @@ import { MongooseAdapter } from '@keystone-next/adapter-mongoose-legacy'; import { KnexAdapter } from '@keystone-next/adapter-knex-legacy'; // @ts-ignore import { PrismaAdapter } from '@keystone-next/adapter-prisma-legacy'; -import type { KeystoneConfig, BaseKeystone } from '@keystone-next/types'; +import type { KeystoneConfig, BaseKeystone, MigrationMode } from '@keystone-next/types'; export function createKeystone( config: KeystoneConfig, dotKeystonePath: string, - script: string, + migrationMode: MigrationMode, prismaClient?: any ) { // Note: For backwards compatibility we may want to expose @@ -31,14 +31,7 @@ export function createKeystone( } else if (db.adapter === 'prisma_postgresql') { adapter = new PrismaAdapter({ getPrismaPath: () => path.join(dotKeystonePath, 'prisma'), - migrationMode: - script === 'prototype' - ? 'prototype' - : script === 'generate' - ? 'createOnly' - : script === 'dev' - ? 'dev' - : 'none', + migrationMode, prismaClient, ...db, }); diff --git a/packages-next/keystone/src/lib/createSystem.ts b/packages-next/keystone/src/lib/createSystem.ts index af9ea0e16bd..0530c22ed90 100644 --- a/packages-next/keystone/src/lib/createSystem.ts +++ b/packages-next/keystone/src/lib/createSystem.ts @@ -1,4 +1,4 @@ -import type { KeystoneConfig } from '@keystone-next/types'; +import type { KeystoneConfig, MigrationMode } from '@keystone-next/types'; import { createGraphQLSchema } from './createGraphQLSchema'; import { makeCreateContext } from './createContext'; @@ -7,10 +7,10 @@ import { createKeystone } from './createKeystone'; export function createSystem( config: KeystoneConfig, dotKeystonePath: string, - script: string, + migrationMode: MigrationMode, prismaClient?: any ) { - const keystone = createKeystone(config, dotKeystonePath, script, prismaClient); + const keystone = createKeystone(config, dotKeystonePath, migrationMode, prismaClient); const graphQLSchema = createGraphQLSchema(config, keystone); diff --git a/packages-next/keystone/src/scripts/build/build.ts b/packages-next/keystone/src/scripts/build/build.ts index f7d087f677d..aaed6076e06 100644 --- a/packages-next/keystone/src/scripts/build/build.ts +++ b/packages-next/keystone/src/scripts/build/build.ts @@ -88,7 +88,7 @@ export async function build({ dotKeystonePath, projectAdminPath }: StaticPaths) const config = initConfig(requireSource(CONFIG_PATH).default); - const { keystone, graphQLSchema } = createSystem(config, dotKeystonePath, 'build'); + const { keystone, graphQLSchema } = createSystem(config, dotKeystonePath, 'none'); console.log('✨ Generating graphQL schema'); await saveSchemaAndTypes(graphQLSchema, keystone, dotKeystonePath); diff --git a/packages-next/keystone/src/scripts/migrate/deploy.ts b/packages-next/keystone/src/scripts/migrate/deploy.ts index f5cacfe1736..f8f52b661c9 100644 --- a/packages-next/keystone/src/scripts/migrate/deploy.ts +++ b/packages-next/keystone/src/scripts/migrate/deploy.ts @@ -8,7 +8,7 @@ export const deploy = async ({ dotKeystonePath }: StaticPaths) => { console.log('🤞 Migrating Keystone'); const config = initConfig(requireSource(CONFIG_PATH).default); - const keystone = createKeystone(config, dotKeystonePath, 'deploy'); + const keystone = createKeystone(config, dotKeystonePath, 'none'); console.log('✨ Deploying migrations'); await keystone.adapter.deploy(keystone._consolidateRelationships()); diff --git a/packages-next/keystone/src/scripts/migrate/generate.ts b/packages-next/keystone/src/scripts/migrate/generate.ts index a67c7d80e02..2ba57d406ef 100644 --- a/packages-next/keystone/src/scripts/migrate/generate.ts +++ b/packages-next/keystone/src/scripts/migrate/generate.ts @@ -12,7 +12,7 @@ export const generate = async ({ dotKeystonePath }: StaticPaths) => { const { keystone, graphQLSchema, createContext } = createSystem( config, dotKeystonePath, - 'generate' + 'createOnly' ); console.log('✨ Generating graphQL schema'); diff --git a/packages-next/keystone/src/scripts/migrate/reset.ts b/packages-next/keystone/src/scripts/migrate/reset.ts index ab2eef89989..d40518ed9de 100644 --- a/packages-next/keystone/src/scripts/migrate/reset.ts +++ b/packages-next/keystone/src/scripts/migrate/reset.ts @@ -8,7 +8,7 @@ export const reset = async ({ dotKeystonePath }: StaticPaths) => { console.log('🤞 Migrating Keystone'); const config = initConfig(requireSource(CONFIG_PATH).default); - const keystone = createKeystone(config, dotKeystonePath, 'reset'); + const keystone = createKeystone(config, dotKeystonePath, 'none'); console.log('✨ Resetting database'); await keystone.adapter._prepareSchema(keystone._consolidateRelationships()); diff --git a/packages-next/keystone/src/scripts/run/dev.ts b/packages-next/keystone/src/scripts/run/dev.ts index 3092efa3bff..38062d9b74e 100644 --- a/packages-next/keystone/src/scripts/run/dev.ts +++ b/packages-next/keystone/src/scripts/run/dev.ts @@ -8,6 +8,7 @@ import { createExpressServer } from '../../lib/createExpressServer'; import { saveSchemaAndTypes } from '../../lib/saveSchemaAndTypes'; import { CONFIG_PATH } from '../utils'; import type { StaticPaths } from '..'; +import { MigrationMode } from '@keystone-next/types'; // TODO: Don't generate or start an Admin UI if it isn't configured!! const devLoadingHTMLFilepath = path.join( @@ -17,7 +18,10 @@ const devLoadingHTMLFilepath = path.join( 'dev-loading.html' ); -export const dev = async ({ dotKeystonePath, projectAdminPath }: StaticPaths, script = 'dev') => { +export const dev = async ( + { dotKeystonePath, projectAdminPath }: StaticPaths, + migrationMode: MigrationMode = 'dev' +) => { console.log('🤞 Starting Keystone'); const server = express(); @@ -28,7 +32,7 @@ export const dev = async ({ dotKeystonePath, projectAdminPath }: StaticPaths, sc const { keystone, graphQLSchema, createContext } = createSystem( config, dotKeystonePath, - script + migrationMode ); console.log('✨ Generating graphQL schema'); diff --git a/packages-next/keystone/src/scripts/run/start.ts b/packages-next/keystone/src/scripts/run/start.ts index 1daf02850c8..cc8ac9a432b 100644 --- a/packages-next/keystone/src/scripts/run/start.ts +++ b/packages-next/keystone/src/scripts/run/start.ts @@ -15,7 +15,7 @@ export const start = async ({ dotKeystonePath, projectAdminPath }: StaticPaths) throw new Error('keystone-next build must be run before running keystone-next start'); } const config = initConfig(require(apiFile).config); - const { keystone, graphQLSchema, createContext } = createSystem(config, dotKeystonePath, 'start'); + const { keystone, graphQLSchema, createContext } = createSystem(config, dotKeystonePath, 'none'); console.log('✨ Connecting to the database'); await keystone.connect({ context: createContext().sudo() }); diff --git a/packages-next/types/src/core.ts b/packages-next/types/src/core.ts index e173b95777b..14715df0477 100644 --- a/packages-next/types/src/core.ts +++ b/packages-next/types/src/core.ts @@ -65,3 +65,5 @@ export function getGqlNames({ relateToOneInputName: `${_itemQueryName}RelateToOneInput`, }; } + +export type MigrationMode = 'none' | 'createOnly' | 'dev' | 'prototype'; diff --git a/packages/test-utils/src/index.ts b/packages/test-utils/src/index.ts index 047a0a13dec..d0ccd46c07d 100644 --- a/packages/test-utils/src/index.ts +++ b/packages/test-utils/src/index.ts @@ -72,7 +72,7 @@ async function setupFromConfig({ const { keystone, createContext, graphQLSchema } = createSystem( config, path.resolve('.keystone'), - '' + 'prototype' ); const app = await createExpressServer(config, graphQLSchema, createContext, true, '', false);