Skip to content

Commit

Permalink
Change createKeystone and createSystem to accept a migration mode rat…
Browse files Browse the repository at this point in the history
…her than script (#5087)

* Change createKeystone to accept a migration mode rather than script

* Update nice-crews-jog.md
  • Loading branch information
emmatown authored Mar 10, 2021
1 parent 17c86e0 commit 56e5fe1
Show file tree
Hide file tree
Showing 13 changed files with 33 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .changeset/new-bears-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-next/types': minor
---

Added `MigrationMode` type
7 changes: 7 additions & 0 deletions .changeset/nice-crews-jog.md
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion packages-next/admin-ui/src/templates/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
13 changes: 3 additions & 10 deletions packages-next/keystone/src/lib/createKeystone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
});
Expand Down
6 changes: 3 additions & 3 deletions packages-next/keystone/src/lib/createSystem.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion packages-next/keystone/src/scripts/build/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion packages-next/keystone/src/scripts/migrate/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion packages-next/keystone/src/scripts/migrate/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const generate = async ({ dotKeystonePath }: StaticPaths) => {
const { keystone, graphQLSchema, createContext } = createSystem(
config,
dotKeystonePath,
'generate'
'createOnly'
);

console.log('✨ Generating graphQL schema');
Expand Down
2 changes: 1 addition & 1 deletion packages-next/keystone/src/scripts/migrate/reset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
8 changes: 6 additions & 2 deletions packages-next/keystone/src/scripts/run/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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();
Expand All @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion packages-next/keystone/src/scripts/run/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() });
Expand Down
2 changes: 2 additions & 0 deletions packages-next/types/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,5 @@ export function getGqlNames({
relateToOneInputName: `${_itemQueryName}RelateToOneInput`,
};
}

export type MigrationMode = 'none' | 'createOnly' | 'dev' | 'prototype';
2 changes: 1 addition & 1 deletion packages/test-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

1 comment on commit 56e5fe1

@vercel
Copy link

@vercel vercel bot commented on 56e5fe1 Mar 10, 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.