Skip to content

Commit

Permalink
feat(core/db): allow to extend PrismaClient (#9114)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Cousens <[email protected]>
  • Loading branch information
iamandrewluca and dcousens authored Apr 25, 2024
1 parent 2613769 commit a9d1d22
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/extend-prisma-client.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-6/core': minor
---

Add `config.db.extendPrismaClient` to support extending the `PrismaClient`
5 changes: 4 additions & 1 deletion packages/core/src/lib/createSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,10 @@ export function createSystem (config_: KeystoneConfig) {
log: config.db.enableLogging
})

const prismaClient = injectNewDefaults(prePrismaClient, lists)
const prismaClient = config.db.extendPrismaClient(
injectNewDefaults(prePrismaClient, lists)
)

const context = createContext({
config,
lists,
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/lib/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ function defaultIsAccessAllowed ({ session, sessionStrategy }: KeystoneContext)
}

async function noop () {}
function identity<T> (x: T) { return x }

export function resolveDefaults <TypeInfo extends BaseKeystoneTypeInfo> (config: KeystoneConfig<TypeInfo>): __ResolvedKeystoneConfig<TypeInfo> {
if (!['postgresql', 'sqlite', 'mysql'].includes(config.db.provider)) {
Expand Down Expand Up @@ -108,7 +109,8 @@ export function resolveDefaults <TypeInfo extends BaseKeystoneTypeInfo> (config:
db: {
...config.db,
shadowDatabaseUrl: config.db?.shadowDatabaseUrl ?? '',
extendPrismaSchema: config.db?.extendPrismaSchema ?? ((schema: string) => schema),
extendPrismaSchema: config.db?.extendPrismaSchema ?? identity,
extendPrismaClient: config.db?.extendPrismaClient ?? identity,
onConnect: config.db.onConnect ?? noop,
prismaClientPath: config.db?.prismaClientPath ?? '@prisma/client',
prismaSchemaPath: config.db?.prismaSchemaPath ?? 'schema.prisma',
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/types/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export type KeystoneConfig<TypeInfo extends BaseKeystoneTypeInfo = BaseKeystoneT
prismaSchemaPath?: string

extendPrismaSchema?: (schema: string) => string
extendPrismaClient?: (client: any) => any
}

graphql?: {
Expand Down

0 comments on commit a9d1d22

Please sign in to comment.