Skip to content

Commit 7639410

Browse files
authored
Tidy up internal Prisma types (#9053)
1 parent 73c27ed commit 7639410

File tree

20 files changed

+178
-196
lines changed

20 files changed

+178
-196
lines changed

packages/auth/src/gql/getInitFirstItemSchema.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ export function getInitFirstItemSchema ({
4747
throw new Error('No session implementation available on context')
4848
}
4949

50-
const dbItemAPI = context.sudo().db[listKey]
50+
const sudoContext = context.sudo()
5151

5252
// should approximate hasInitFirstItemConditions
53-
const count = await dbItemAPI.count({})
53+
const count = await sudoContext.db[listKey].count()
5454
if (count !== 0) {
5555
throw new Error('Initial items can only be created when no items exist in that list')
5656
}
@@ -59,7 +59,7 @@ export function getInitFirstItemSchema ({
5959
// this is strictly speaking incorrect. the db API will do GraphQL coercion on a value which has already been coerced
6060
// (this is also mostly fine, the chance that people are using things where
6161
// the input value can't round-trip like the Upload scalar here is quite low)
62-
const item = await dbItemAPI.createOne({ data: { ...data, ...itemData } })
62+
const item = await sudoContext.db[listKey].createOne({ data: { ...data, ...itemData } })
6363
const sessionToken = (await context.sessionStrategy.start({
6464
data: { listKey, itemId: item.id.toString() },
6565
context,

packages/core/src/___internal-do-not-use-will-break-in-patch/artifacts.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,3 @@ export {
44
generatePrismaAndGraphQLSchemas,
55
getCommittedArtifacts,
66
} from '../artifacts'
7-
export type { PrismaModule } from '../artifacts'

packages/core/src/artifacts.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -217,14 +217,3 @@ async function generatePrismaClient (prismaSchemaPath: string, dataProxy: boolea
217217
})
218218
)
219219
}
220-
221-
export type PrismaModule = {
222-
PrismaClient: {
223-
new (args: unknown): any
224-
}
225-
Prisma: {
226-
DbNull: unknown
227-
JsonNull: unknown
228-
[key: string]: unknown
229-
}
230-
}

packages/core/src/context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ export function getContext<TypeInfo extends BaseKeystoneTypeInfo> (
1111
PrismaModule: unknown
1212
): KeystoneContext<TypeInfo> {
1313
const system = createSystem(initConfig(config))
14-
const { context } = system.getKeystone(PrismaModule as any)
14+
const { context } = system.getKeystone(PrismaModule)
1515
return context
1616
}

packages/core/src/lib/context/createContext.ts

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1-
import type { IncomingMessage, ServerResponse } from 'http'
2-
import { type ExecutionResult, graphql, type GraphQLSchema, print } from 'graphql'
3-
import type { KeystoneContext, KeystoneGraphQLAPI, KeystoneConfig } from '../../types'
4-
5-
import type { PrismaClient } from '../core/utils'
6-
import type { InitialisedList } from '../core/initialise-lists'
1+
import {
2+
type IncomingMessage,
3+
type ServerResponse
4+
} from 'http'
5+
import {
6+
type ExecutionResult,
7+
type GraphQLSchema,
8+
graphql,
9+
print
10+
} from 'graphql'
11+
import {
12+
type KeystoneConfig,
13+
type KeystoneContext,
14+
type KeystoneGraphQLAPI,
15+
} from '../../types'
16+
17+
import { type InitialisedList } from '../core/initialise-lists'
718
import { createImagesContext } from '../assets/createImagesContext'
819
import { createFilesContext } from '../assets/createFilesContext'
920
import { getDbFactory, getQueryFactory } from './api'
@@ -14,12 +25,17 @@ export function createContext ({
1425
graphQLSchema,
1526
graphQLSchemaSudo,
1627
prismaClient,
28+
prismaTypes
1729
}: {
1830
config: KeystoneConfig
1931
lists: Record<string, InitialisedList>
2032
graphQLSchema: GraphQLSchema
2133
graphQLSchemaSudo: GraphQLSchema
22-
prismaClient: PrismaClient
34+
prismaClient: unknown
35+
prismaTypes: {
36+
DbNull: unknown,
37+
JsonNull: unknown
38+
}
2339
}) {
2440
const dbFactories: Record<string, ReturnType<typeof getDbFactory>> = {}
2541
for (const [listKey, list] of Object.entries(lists)) {
@@ -110,12 +126,18 @@ export function createContext ({
110126
// TODO: deprecated, remove in breaking change
111127
gqlNames: (listKey: string) => lists[listKey].graphql.names,
112128

113-
// TODO: rename __internal ?
129+
// TODO: deprecated, remove in breaking change
114130
...(config.experimental?.contextInitialisedLists
115131
? {
116132
experimental: { initialisedLists: lists },
117133
}
118134
: {}),
135+
136+
__internal: {
137+
prisma: {
138+
...prismaTypes
139+
}
140+
}
119141
}
120142

121143
const _dbFactories = sudo ? dbFactoriesSudo : dbFactories

packages/core/src/lib/context/executeGraphQLFieldToRootVal.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ function getRootValGivenOutputType (originalType: OutputType, value: any): any {
145145
return value[rawField]
146146
}
147147

148-
export function executeGraphQLFieldToRootVal (field: GraphQLField<any, any>) {
148+
export function executeGraphQLFieldToRootVal (field: GraphQLField<any, unknown>) {
149149
const { argumentNodes, variableDefinitions } = getVariablesForGraphQLField(field)
150150
const document: DocumentNode = {
151151
kind: Kind.DOCUMENT,
@@ -174,7 +174,7 @@ export function executeGraphQLFieldToRootVal (field: GraphQLField<any, any>) {
174174

175175
const type = getTypeForField(field.type)
176176

177-
const fieldConfig: RequiredButStillAllowUndefined<GraphQLFieldConfig<any, any>> = {
177+
const fieldConfig: RequiredButStillAllowUndefined<GraphQLFieldConfig<unknown, unknown>> = {
178178
args: argsToArgsConfig(field.args),
179179
astNode: undefined,
180180
deprecationReason: field.deprecationReason,
@@ -195,7 +195,7 @@ export function executeGraphQLFieldToRootVal (field: GraphQLField<any, any>) {
195195
})
196196

197197
return async (
198-
args: Record<string, any>,
198+
args: Record<string, unknown>,
199199
context: KeystoneContext,
200200
rootValue: Record<string, string> = {}
201201
) => {

packages/core/src/lib/core/access-control.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@ import { type InitialisedList } from './initialise-lists'
2222
import { type InputFilter } from './where-inputs'
2323

2424
export function cannotForItem (operation: string, list: InitialisedList) {
25-
return (
26-
`You cannot ${operation} that ${list.listKey}` +
27-
(operation === 'create' ? '' : ' - it may not exist')
28-
)
25+
if (operation === 'create') return `You cannot ${operation} that ${list.listKey}`
26+
return `You cannot ${operation} that ${list.listKey} - it may not exist`
2927
}
3028

3129
export function cannotForItemFields (

packages/core/src/lib/core/initialise-lists.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ function getIsEnabled (listKey: string, listConfig: KeystoneConfig['lists'][stri
154154
create: notOmit,
155155
update: notOmit,
156156
delete: notOmit,
157-
filter: notOmit && defaultIsFilterable,
158-
orderBy: notOmit && defaultIsOrderable,
157+
filter: notOmit ? defaultIsFilterable : false,
158+
orderBy: notOmit ? defaultIsOrderable : false,
159159
}
160160
}
161161

packages/core/src/lib/core/mutations/access-control.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import type { InitialisedList } from '../initialise-lists'
55
import { cannotForItem, cannotForItemFields } from '../access-control'
66
import {
77
type InputFilter,
8+
type UniqueInputFilter,
89
resolveUniqueWhereInput,
910
resolveWhereInput,
10-
type UniqueInputFilter,
11-
type UniquePrismaFilter,
1211
} from '../where-inputs'
12+
import {
13+
type UniquePrismaFilter,
14+
} from '../../../types/prisma'
1315

1416
async function getFilteredItem (
1517
list: InitialisedList,

packages/core/src/lib/core/mutations/create-update.ts

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
1-
import type { KeystoneContext, BaseItem } from '../../../types'
2-
import type { ResolvedDBField } from '../resolve-relationships'
3-
import type { InitialisedList } from '../initialise-lists'
41
import {
2+
type BaseItem,
3+
type KeystoneContext
4+
} from '../../../types'
5+
import {
6+
type ResolvedDBField
7+
} from '../resolve-relationships'
8+
import {
9+
type InitialisedList
10+
} from '../initialise-lists'
11+
import {
12+
type IdType,
513
promiseAllRejectWithAllErrors,
614
getDBFieldKeyForFieldOnMultiField,
7-
type IdType,
8-
getPrismaNamespace,
915
} from '../utils'
10-
import { type InputFilter, resolveUniqueWhereInput, type UniqueInputFilter } from '../where-inputs'
16+
import {
17+
type InputFilter,
18+
type UniqueInputFilter,
19+
resolveUniqueWhereInput,
20+
} from '../where-inputs'
1121
import {
1222
accessDeniedError,
1323
extensionError,
@@ -82,8 +92,10 @@ export async function createOne (
8292
const operationAccess = await getOperationAccess(list, context, 'create')
8393
if (!operationAccess) throw accessDeniedError(cannotForItem('create', list))
8494

95+
// operation
8596
const { item, afterOperation } = await createSingle(createInput, list, context)
8697

98+
// after operation
8799
await afterOperation(item)
88100

89101
return item
@@ -100,8 +112,12 @@ export async function createMany (
100112
// throw for each attempt
101113
if (!operationAccess) throw accessDeniedError(cannotForItem('create', list))
102114

115+
// operation
103116
const { item, afterOperation } = await createSingle({ data }, list, context)
117+
118+
// after operation
104119
await afterOperation(item)
120+
105121
return item
106122
})
107123
}
@@ -137,13 +153,15 @@ async function updateSingle (
137153
item
138154
)
139155

156+
// operation
140157
const updatedItem = await context.prisma[list.listKey].update({
141158
where: { id: item.id },
142159
data,
143160
})
144161

145162
// after operation
146163
await afterOperation(updatedItem)
164+
147165
return updatedItem
148166
}
149167

@@ -390,8 +408,7 @@ function transformInnerDBField (
390408
value: unknown
391409
) {
392410
if (dbField.kind === 'scalar' && dbField.scalar === 'Json' && value === null) {
393-
const Prisma = getPrismaNamespace(context)
394-
return Prisma.DbNull
411+
return context.__internal.prisma.DbNull
395412
}
396413
return value
397414
}
@@ -402,18 +419,25 @@ function transformForPrismaClient (
402419
context: KeystoneContext
403420
) {
404421
return Object.fromEntries(
405-
Object.entries(data).flatMap(([fieldKey, value]) => {
406-
const { dbField } = fields[fieldKey]
407-
if (dbField.kind === 'multi') {
408-
return Object.entries(value).map(([innerFieldKey, fieldValue]) => {
409-
return [
410-
getDBFieldKeyForFieldOnMultiField(fieldKey, innerFieldKey),
411-
transformInnerDBField(dbField.fields[innerFieldKey], context, fieldValue),
412-
]
413-
})
414-
}
422+
[...function* () {
423+
for (const fieldKey in data) {
424+
const value = data[fieldKey]
425+
const { dbField } = fields[fieldKey]
426+
427+
if (dbField.kind === 'multi') {
428+
for (const innerFieldKey in value) {
429+
const innerFieldValue = value[innerFieldKey]
430+
yield [
431+
getDBFieldKeyForFieldOnMultiField(fieldKey, innerFieldKey),
432+
transformInnerDBField(dbField.fields[innerFieldKey], context, innerFieldValue),
433+
]
434+
}
435+
436+
continue
437+
}
415438

416-
return [[fieldKey, transformInnerDBField(dbField, context, value)]]
417-
})
439+
yield [fieldKey, transformInnerDBField(dbField, context, value)]
440+
}
441+
}()]
418442
)
419443
}

0 commit comments

Comments
 (0)