Skip to content

Commit

Permalink
Add support for providing prisma transaction options (#9280)
Browse files Browse the repository at this point in the history
  • Loading branch information
acburdine authored Aug 12, 2024
1 parent 3dc6c5a commit 6bdf320
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/kind-grapes-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@keystone-6/core": minor
---

Adds context.transaction passthrough for prisma $transaction options
1 change: 1 addition & 0 deletions examples/custom-output-paths/my-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ export type TypeInfo<Session = any> = {
readonly Post: Lists.Post.TypeInfo<Session>
}
prisma: import('./node_modules/.myprisma/client').PrismaClient
prismaTypes: import('./node_modules/.myprisma/client').Prisma
session: Session
}

Expand Down
1 change: 1 addition & 0 deletions examples/extend-graphql-schema-nexus/keystone-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ export type TypeInfo<Session = any> = {
readonly Author: Lists.Author.TypeInfo<Session>
}
prisma: import('./node_modules/.myprisma/client').PrismaClient
prismaTypes: import('./node_modules/.myprisma/client').Prisma
session: Session
}

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/lib/context/createContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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_,
Expand All @@ -108,7 +108,7 @@ export function createContext ({
})

return await f(newContext)
})
}, opts)
},

req,
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/lib/typescript-schema-printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ export function printGeneratedTypes (
})(),
` }`,
` prisma: import('${prismaClientPath}').PrismaClient`,
` prismaTypes: import('${prismaClientPath}').Prisma`,
` session: Session`,
`}`,
``,
Expand Down
11 changes: 9 additions & 2 deletions packages/core/src/types/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TypeInfo extends BaseKeystoneTypeInfo = BaseKeystoneTypeInfo> = {
req?: IncomingMessage
Expand All @@ -27,7 +27,14 @@ export type KeystoneContext<TypeInfo extends BaseKeystoneTypeInfo = BaseKeystone
withSession: (session?: TypeInfo['session']) => KeystoneContext<TypeInfo>
withRequest: (req: IncomingMessage, res?: ServerResponse) => Promise<KeystoneContext<TypeInfo>>
prisma: TypeInfo['prisma']
transaction: <T>(f: (context: KeystoneContext<TypeInfo>) => MaybePromise<T>) => Promise<T>
transaction: <T>(
f: (context: KeystoneContext<TypeInfo>) => MaybePromise<T>,
options?: {
maxWait?: number
timeout?: number
isolationLevel?: TypeInfo['prismaTypes']['TransactionIsolationLevel']
}
) => Promise<T>

files: FilesContext
images: ImagesContext
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/types/type-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ export type KeystoneContextFromListTypeInfo<ListTypeInfo extends BaseListTypeInf
export type BaseKeystoneTypeInfo<Session = any> = {
lists: Record<string, BaseListTypeInfo<Session>>
prisma: any
prismaTypes: any
session: any
}
1 change: 1 addition & 0 deletions packages/core/src/types/type-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const someContext: KeystoneContext<{
ListOrSingleton: BaseListTypeInfo
}
prisma: any
prismaTypes: any
session: any
}> = undefined!

Expand Down
3 changes: 2 additions & 1 deletion tests/cli-tests/__snapshots__/artifacts.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ export type TypeInfo<Session = any> = {
readonly Todo: Lists.Todo.TypeInfo<Session>
}
prisma: import('@prisma/client').PrismaClient
prismaTypes: import('@prisma/client').Prisma
session: Session
}
Expand All @@ -159,4 +160,4 @@ export type Lists<Session = any> = {
export {}
`
`;

0 comments on commit 6bdf320

Please sign in to comment.