-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
createSystem.ts
101 lines (91 loc) · 3.35 KB
/
createSystem.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { FieldData, KeystoneConfig, DatabaseProvider, getGqlNames } from '@keystone-next/types';
import { createAdminMeta } from '../admin-ui/system/createAdminMeta';
import { createGraphQLSchema } from './createGraphQLSchema';
import { makeCreateContext } from './context/createContext';
import { initialiseLists } from './core/types-for-lists';
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 if (db.provider === 'mongodb') {
return 'mongodb';
} else {
throw new Error(
'Invalid db configuration. Please specify db.provider as either "sqlite" or "postgresql"'
);
}
}
function getInternalGraphQLSchema(config: KeystoneConfig, provider: DatabaseProvider) {
const transformedConfig: KeystoneConfig = {
...config,
lists: Object.fromEntries(
Object.entries(config.lists).map(([listKey, list]) => {
return [
listKey,
{
...list,
access: true,
fields: Object.fromEntries(
Object.entries(list.fields).map(([fieldKey, field]) => {
return [
fieldKey,
(data: FieldData) => {
return { ...field(data), access: true };
},
];
})
),
},
];
})
),
};
const lists = initialiseLists(transformedConfig.lists, provider);
const adminMeta = createAdminMeta(transformedConfig, lists);
return createGraphQLSchema(transformedConfig, lists, adminMeta);
}
export function createSystem(config: KeystoneConfig) {
const provider = getDBProvider(config.db);
const lists = initialiseLists(config.lists, provider);
const adminMeta = createAdminMeta(config, lists);
const graphQLSchema = createGraphQLSchema(config, lists, adminMeta);
const internalGraphQLSchema = getInternalGraphQLSchema(config, provider);
return {
graphQLSchema,
adminMeta,
getKeystone: (PrismaClient: any) => {
const prismaClient = new PrismaClient({
log: config.db.enableLogging && ['query'],
datasources: { [provider]: { url: config.db.url } },
});
prismaClient.$on('beforeExit', async () => {
// Prisma is failing to properly clean up its child processes
// https://github.com/keystonejs/keystone/issues/5477
// We explicitly send a SIGINT signal to the prisma child process on exit
// to ensure that the process is cleaned up appropriately.
prismaClient._engine.child.kill('SIGINT');
});
const createContext = makeCreateContext({
graphQLSchema,
internalSchema: internalGraphQLSchema,
config,
prismaClient,
gqlNamesByList: Object.fromEntries(
Object.entries(lists).map(([listKey, list]) => [listKey, getGqlNames(list)])
),
});
return {
async connect() {
await prismaClient.$connect();
const context = createContext({ skipAccessControl: true, schemaName: 'internal' });
await config.db.onConnect?.(context);
},
async disconnect() {
await prismaClient.$disconnect();
},
createContext,
};
},
};
}