Skip to content

Commit

Permalink
Add getDBProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie committed May 11, 2021
1 parent 6dcd1fe commit 7451d50
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 22 deletions.
6 changes: 6 additions & 0 deletions .changeset/short-cougars-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@keystone-next/keystone': patch
'@keystone-next/types': patch
---

Refactored code to parse `config.db`. No functional changes.
23 changes: 3 additions & 20 deletions packages-next/keystone/src/lib/createKeystone.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
18 changes: 16 additions & 2 deletions packages-next/keystone/src/lib/createSystem.ts
Original file line number Diff line number Diff line change
@@ -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');

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 @@ -5,6 +5,8 @@ import type { KeystoneContext, SessionContext } from './context';
/* TODO: Review these types */
type FieldDefaultValueArgs<T> = { context: KeystoneContext; originalInput?: T };

export type Provider = 'sqlite' | 'postgresql';

export type FieldDefaultValue<T> =
| T
| null
Expand Down

0 comments on commit 7451d50

Please sign in to comment.