Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getDBProvider #5673

Merged
merged 2 commits into from
May 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
27 changes: 7 additions & 20 deletions packages-next/keystone/src/lib/createKeystone.ts
Original file line number Diff line number Diff line change
@@ -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,
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, 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');

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 DatabaseProvider = 'sqlite' | 'postgresql';

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