Accessing extended operations on the prisma client with typescript

I'm trying to make some abstract types around the Prisma Client, like so:
import type { Prisma } from "@prisma/client";
import type { Operation as PrismaOperation } from "@prisma/client/runtime/library";

import type { Database } from "@/libs/database";
import type { FilterDollarSignKeys } from "@/utils/types";

export type Model = keyof Database;

export type Operation = FilterDollarSignKeys<PrismaOperation>;

export type Args<M extends Model, O extends Operation> = Prisma.Args<Database[M], O>;

export type Payload<M extends Model, O extends Operation> = Prisma.Payload<Database[M], O>;

export type Result<M extends Model, O extends Operation, A extends Args<M, O>> = Prisma.Result<
Database[M],
A,
O
>;

// Example Usage:
const model: Model = "user";
const operation: Operation = "findMany";
const _args: Args<typeof model, typeof operation> = { select: { id: true } };
const _result: Result<typeof model, typeof operation, { select: { id: true } }> = [{ id: "1" }];
import type { Prisma } from "@prisma/client";
import type { Operation as PrismaOperation } from "@prisma/client/runtime/library";

import type { Database } from "@/libs/database";
import type { FilterDollarSignKeys } from "@/utils/types";

export type Model = keyof Database;

export type Operation = FilterDollarSignKeys<PrismaOperation>;

export type Args<M extends Model, O extends Operation> = Prisma.Args<Database[M], O>;

export type Payload<M extends Model, O extends Operation> = Prisma.Payload<Database[M], O>;

export type Result<M extends Model, O extends Operation, A extends Args<M, O>> = Prisma.Result<
Database[M],
A,
O
>;

// Example Usage:
const model: Model = "user";
const operation: Operation = "findMany";
const _args: Args<typeof model, typeof operation> = { select: { id: true } };
const _result: Result<typeof model, typeof operation, { select: { id: true } }> = [{ id: "1" }];
But the issue I'm facing is when I try to access any operation that has been extended on the client. For example, I have this prisma-bark-extension on the Prisma Client. When I use prisma.modelName.operation, I see the additional actions provided by the middleware, but import type { Operation } from "@prisma/client/runtime/library"; does not contain a list of them. Would anyone have an idea on how I can get the extended list of operations?
3 Replies
JTB
JTB5mo ago
Defube Custom Op:
import type { Prisma } from "@prisma/client";
import type { Operation as PrismaOperation } from "@prisma/client/runtime/library";

// Define your custom operations here
type CustomOperations = 'bark' | 'growl';

// Extend the Prisma operations with the custom ones
type ExtendedOperations = PrismaOperation | CustomOperations;
import type { Prisma } from "@prisma/client";
import type { Operation as PrismaOperation } from "@prisma/client/runtime/library";

// Define your custom operations here
type CustomOperations = 'bark' | 'growl';

// Extend the Prisma operations with the custom ones
type ExtendedOperations = PrismaOperation | CustomOperations;
Extend Types:
import type { Database } from "@/libs/database";
import type { FilterDollarSignKeys } from "@/utils/types";

// Using the extended operations type
export type Operation = FilterDollarSignKeys<ExtendedOperations>;

export type Args<M extends Model, O extends Operation> = Prisma.Args<Database[M], O>;

export type Payload<M extends Model, O extends Operation> = Prisma.Payload<Database[M], O>;

export type Result<M extends Model, O extends Operation, A extends Args<M, O>> = Prisma.Result<
Database[M],
A,
O
>;
import type { Database } from "@/libs/database";
import type { FilterDollarSignKeys } from "@/utils/types";

// Using the extended operations type
export type Operation = FilterDollarSignKeys<ExtendedOperations>;

export type Args<M extends Model, O extends Operation> = Prisma.Args<Database[M], O>;

export type Payload<M extends Model, O extends Operation> = Prisma.Payload<Database[M], O>;

export type Result<M extends Model, O extends Operation, A extends Args<M, O>> = Prisma.Result<
Database[M],
A,
O
>;
Example Usage:
// Example Usage:
const model: Model = "user";
const operation: Operation = "findMany";
const customOperation: Operation = "bark"; // Custom operation

const _args: Args<typeof model, typeof operation> = { select: { id: true } };
const _result: Result<typeof model, typeof operation, { select: { id: true } }> = [{ id: "1" }];

const _customArgs: Args<typeof model, typeof customOperation> = { volume: 'loud' };
const _customResult: Result<typeof model, typeof customOperation, { volume: 'loud' }> = [{ bark: "loud" }];
// Example Usage:
const model: Model = "user";
const operation: Operation = "findMany";
const customOperation: Operation = "bark"; // Custom operation

const _args: Args<typeof model, typeof operation> = { select: { id: true } };
const _result: Result<typeof model, typeof operation, { select: { id: true } }> = [{ id: "1" }];

const _customArgs: Args<typeof model, typeof customOperation> = { volume: 'loud' };
const _customResult: Result<typeof model, typeof customOperation, { volume: 'loud' }> = [{ bark: "loud" }];
Amruth Pillai
Amruth PillaiOP5mo ago
I tried to achieve a similar result by simply getting the keyof Database[M] as the list of operations, and this indeed does work. But it fails when it tries to match the types with Prisma.Args, Payload or Result (which is the main use-case).
No description
Amruth Pillai
Amruth PillaiOP5mo ago
Similar errors when trying out your solution by creating a union of operation types as well.
No description
Want results from more Discord servers?
Add your server