From 6bdf3209a2df833fdfd8e7dc5953ec08b785b72d Mon Sep 17 00:00:00 2001 From: Austin Burdine Date: Mon, 12 Aug 2024 02:19:13 -0400 Subject: [PATCH] Add support for providing prisma transaction options (#9280) --- .changeset/kind-grapes-return.md | 5 +++++ examples/custom-output-paths/my-types.ts | 1 + .../extend-graphql-schema-nexus/keystone-types.ts | 1 + packages/core/src/lib/context/createContext.ts | 4 ++-- packages/core/src/lib/typescript-schema-printer.ts | 1 + packages/core/src/types/context.ts | 11 +++++++++-- packages/core/src/types/type-info.ts | 1 + packages/core/src/types/type-tests.ts | 1 + tests/cli-tests/__snapshots__/artifacts.test.ts.snap | 3 ++- 9 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 .changeset/kind-grapes-return.md diff --git a/.changeset/kind-grapes-return.md b/.changeset/kind-grapes-return.md new file mode 100644 index 00000000000..7bef5edcc65 --- /dev/null +++ b/.changeset/kind-grapes-return.md @@ -0,0 +1,5 @@ +--- +"@keystone-6/core": minor +--- + +Adds context.transaction passthrough for prisma $transaction options diff --git a/examples/custom-output-paths/my-types.ts b/examples/custom-output-paths/my-types.ts index 6e20117423b..ec283d8f8fe 100644 --- a/examples/custom-output-paths/my-types.ts +++ b/examples/custom-output-paths/my-types.ts @@ -167,6 +167,7 @@ export type TypeInfo = { readonly Post: Lists.Post.TypeInfo } prisma: import('./node_modules/.myprisma/client').PrismaClient + prismaTypes: import('./node_modules/.myprisma/client').Prisma session: Session } diff --git a/examples/extend-graphql-schema-nexus/keystone-types.ts b/examples/extend-graphql-schema-nexus/keystone-types.ts index 24c47686ee5..5bbbe72c500 100644 --- a/examples/extend-graphql-schema-nexus/keystone-types.ts +++ b/examples/extend-graphql-schema-nexus/keystone-types.ts @@ -284,6 +284,7 @@ export type TypeInfo = { readonly Author: Lists.Author.TypeInfo } prisma: import('./node_modules/.myprisma/client').PrismaClient + prismaTypes: import('./node_modules/.myprisma/client').Prisma session: Session } diff --git a/packages/core/src/lib/context/createContext.ts b/packages/core/src/lib/context/createContext.ts index f83a9d28e85..138494c3e2d 100644 --- a/packages/core/src/lib/context/createContext.ts +++ b/packages/core/src/lib/context/createContext.ts @@ -97,7 +97,7 @@ export function createContext ({ sudo: () => construct({ prisma, session, sudo: true, req, res }), - transaction: async (f) => { + transaction: async (f, opts) => { return await prisma.$transaction(async (prisma_: any) => { const newContext = construct({ prisma: prisma_, @@ -108,7 +108,7 @@ export function createContext ({ }) return await f(newContext) - }) + }, opts) }, req, diff --git a/packages/core/src/lib/typescript-schema-printer.ts b/packages/core/src/lib/typescript-schema-printer.ts index 2e7fc8d892a..4a7f37f9e67 100644 --- a/packages/core/src/lib/typescript-schema-printer.ts +++ b/packages/core/src/lib/typescript-schema-printer.ts @@ -201,6 +201,7 @@ export function printGeneratedTypes ( })(), ` }`, ` prisma: import('${prismaClientPath}').PrismaClient`, + ` prismaTypes: import('${prismaClientPath}').Prisma`, ` session: Session`, `}`, ``, diff --git a/packages/core/src/types/context.ts b/packages/core/src/types/context.ts index 97164bfb209..242b498e0da 100644 --- a/packages/core/src/types/context.ts +++ b/packages/core/src/types/context.ts @@ -15,7 +15,7 @@ import { type BaseKeystoneTypeInfo, type BaseListTypeInfo, } from './type-info' -import { type MaybePromise } from '../../types' +import { type MaybePromise } from './utils' export type KeystoneContext = { req?: IncomingMessage @@ -27,7 +27,14 @@ export type KeystoneContext KeystoneContext withRequest: (req: IncomingMessage, res?: ServerResponse) => Promise> prisma: TypeInfo['prisma'] - transaction: (f: (context: KeystoneContext) => MaybePromise) => Promise + transaction: ( + f: (context: KeystoneContext) => MaybePromise, + options?: { + maxWait?: number + timeout?: number + isolationLevel?: TypeInfo['prismaTypes']['TransactionIsolationLevel'] + } + ) => Promise files: FilesContext images: ImagesContext diff --git a/packages/core/src/types/type-info.ts b/packages/core/src/types/type-info.ts index ad3daa73c6b..aa7a67a876a 100644 --- a/packages/core/src/types/type-info.ts +++ b/packages/core/src/types/type-info.ts @@ -32,5 +32,6 @@ export type KeystoneContextFromListTypeInfo = { lists: Record> prisma: any + prismaTypes: any session: any } diff --git a/packages/core/src/types/type-tests.ts b/packages/core/src/types/type-tests.ts index 4be970bd2d0..3a04ce572b0 100644 --- a/packages/core/src/types/type-tests.ts +++ b/packages/core/src/types/type-tests.ts @@ -8,6 +8,7 @@ const someContext: KeystoneContext<{ ListOrSingleton: BaseListTypeInfo } prisma: any + prismaTypes: any session: any }> = undefined! diff --git a/tests/cli-tests/__snapshots__/artifacts.test.ts.snap b/tests/cli-tests/__snapshots__/artifacts.test.ts.snap index 0b1dc00c2b3..dec92c6ea88 100644 --- a/tests/cli-tests/__snapshots__/artifacts.test.ts.snap +++ b/tests/cli-tests/__snapshots__/artifacts.test.ts.snap @@ -148,6 +148,7 @@ export type TypeInfo = { readonly Todo: Lists.Todo.TypeInfo } prisma: import('@prisma/client').PrismaClient + prismaTypes: import('@prisma/client').Prisma session: Session } @@ -159,4 +160,4 @@ export type Lists = { export {} -` +`;