-
-
Save hiramhuang/779ec74a7548c4d5cfc127e5606e0ca4 to your computer and use it in GitHub Desktop.
Revisions
-
aniravi24 revised this gist
Mar 6, 2025 . 1 changed file with 74 additions and 31 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,59 +1,54 @@ import { Prisma } from "@prisma/client"; import type { Replace, UnionToTuple } from "type-fest"; import { Effect, Option } from "effect"; import { UnknownException } from "effect/Cause"; type PrismaModelOp = Exclude< // You can import operation types from the generated Prisma client Operation, | "$executeRaw" | "$executeRawUnsafe" | "$queryRaw" | "$queryRawUnsafe" | "$runCommandRaw" | "aggregateRaw" | "findFirst" | "findFirstOrThrow" | "findRaw" | "findUnique" | "findUniqueOrThrow" >; const PrismaModelOps: UnionToTuple<PrismaModelOp> = [ "findMany", "create", "createMany", "createManyAndReturn", "update", "updateMany", "updateManyAndReturn", "upsert", "delete", "deleteMany", "aggregate", "count", "groupBy", ] as const; export const prismaEffectExtension = { client: { // $executeRaw: (query: TemplateStringsArray | Sql, ...values: any[]) => PrismaPromise<number> $executeRawEffect: function <T>( this: T, // This type will work after generating the Prisma client query: Prisma.Sql | TemplateStringsArray, // eslint-disable-next-line @typescript-eslint/no-explicit-any ...values: any[] ): Effect.Effect<number, UnknownException, never> { return Effect.tryPromise<number>(() => { const prismaExtensionContextClient = Prisma.getExtensionContext(this); // @ts-expect-error Can't predict these in advance return prismaExtensionContextClient["$executeRaw"](query, ...values); }); }, @@ -162,6 +157,54 @@ export const prismaEffectExtension = { x?: Prisma.Exact<A, Prisma.Args<T, O>> ) => Effect.Effect<Prisma.Result<T, A, O>, UnknownException, never>; }), findFirstEffect<T, A, O extends "findFirstOrThrow">( this: T, x?: Prisma.Exact<A, Prisma.Args<T, O>> ): Effect.Effect<Prisma.Result<T, A, O>, UnknownException, never> { return Effect.tryPromise<Prisma.Result<T, A, O>>(() => { const prismaExtensionContextClient = Prisma.getExtensionContext(this); // @ts-expect-error Can't predict these in advance return prismaExtensionContextClient.findFirstOrThrow(x); }); }, findFirstOption<T, A, O extends "findFirst">( this: T, x?: Prisma.Exact<A, Prisma.Args<T, O>> ): Effect.Effect< Option.Option<NonNullable<Prisma.Result<T, A, O>>>, UnknownException, never > { return Effect.tryPromise<Prisma.Result<T, A, O>>(() => { const prismaExtensionContextClient = Prisma.getExtensionContext(this); // @ts-expect-error Can't predict these in advance return prismaExtensionContextClient.findFirst(x); }).pipe(Effect.map((result) => Option.fromNullable(result))); }, findUniqueEffect<T, A, O extends "findUniqueOrThrow">( this: T, x?: Prisma.Exact<A, Prisma.Args<T, O>> ): Effect.Effect<Prisma.Result<T, A, O>, UnknownException, never> { return Effect.tryPromise<Prisma.Result<T, A, O>>(() => { const prismaExtensionContextClient = Prisma.getExtensionContext(this); // @ts-expect-error Can't predict these in advance return prismaExtensionContextClient.findUniqueOrThrow(x); }); }, findUniqueOption<T, A, O extends "findUnique">( this: T, x?: Prisma.Exact<A, Prisma.Args<T, O>> ): Effect.Effect< Option.Option<NonNullable<Prisma.Result<T, A, O>>>, UnknownException, never > { return Effect.tryPromise<Prisma.Result<T, A, O>>(() => { const prismaExtensionContextClient = Prisma.getExtensionContext(this); // @ts-expect-error Can't predict these in advance return prismaExtensionContextClient.findUnique(x); }).pipe(Effect.map((result) => Option.fromNullable(result))); }, }, }, }; -
aniravi24 revised this gist
Mar 6, 2025 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -53,7 +53,7 @@ export const prismaEffectExtension = { return prismaExtensionContextClient["$executeRaw"]( query, ...values ); }); }, @@ -164,4 +164,4 @@ export const prismaEffectExtension = { }), }, }, }; -
aniravi24 revised this gist
Mar 6, 2025 . 1 changed file with 123 additions and 72 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,78 +1,124 @@ import { Prisma } from "@prisma/client"; import type { Replace } from "type-fest"; import { Effect } from "effect"; import { UnknownException } from "effect/Cause"; type PrismaModelOp = | "aggregate" | "count" | "create" | "createMany" | "delete" | "deleteMany" | "findFirst" | "findFirstOrThrow" | "findMany" | "findUnique" | "findUniqueOrThrow" | "groupBy" | "update" | "updateMany" | "upsert"; const PrismaModelOps = [ "aggregate", "count", "create", "createMany", "delete", "deleteMany", "findFirst", "findFirstOrThrow", "findMany", "findUnique", "findUniqueOrThrow", "groupBy", "update", "updateMany", "upsert", ] as const satisfies readonly PrismaModelOp[]; export const prismaEffectExtension = { client: { // $executeRaw: (query: TemplateStringsArray | Sql, ...values: any[]) => PrismaPromise<number> $executeRawEffect: function <T>( this: T, query: Prisma.Sql | TemplateStringsArray, // eslint-disable-next-line @typescript-eslint/no-explicit-any ...values: any[] ): Effect.Effect<number, UnknownException, never> { return Effect.tryPromise<number>(() => { const prismaExtensionContextClient = Prisma.getExtensionContext(this); // @ts-expect-error Can't predict these in advance return prismaExtensionContextClient["$executeRaw"]( query, ...values ) as any; }); }, // $executeRawUnsafe: (query: string, ...values: any[]) => PrismaPromise<number> $executeRawUnsafeEffect: function <T>( this: T, query: string, // eslint-disable-next-line @typescript-eslint/no-explicit-any ...values: any[] ): Effect.Effect<number, UnknownException, never> { return Effect.tryPromise<number>(() => { const prismaExtensionContextClient = Prisma.getExtensionContext(this); // @ts-expect-error Can't predict these in advance return prismaExtensionContextClient["$executeRawUnsafe"]( query, ...values ); }); }, // $queryRaw: <T = unknown>(query: TemplateStringsArray | Sql, ...values: any[]) => PrismaPromise<T> // eslint-disable-next-line @typescript-eslint/no-explicit-any $queryRawEffect: function <A = unknown, T = any>( this: T, query: Prisma.Sql | TemplateStringsArray, // eslint-disable-next-line @typescript-eslint/no-explicit-any ...values: any[] ): Effect.Effect<A, UnknownException, never> { return Effect.tryPromise<A>(() => { const prismaExtensionContextClient = Prisma.getExtensionContext(this); // @ts-expect-error Can't predict these in advance return prismaExtensionContextClient["$queryRaw"](query, ...values); }); }, // $queryRawTyped: <T>(query: TypedSql<unknown[], T>) => PrismaPromise<T[]> // eslint-disable-next-line @typescript-eslint/no-explicit-any $queryRawTypedEffect: function <A = unknown, T = any>( this: T, query: TypedSql<unknown[], T> ): Effect.Effect<A, UnknownException, never> { return Effect.tryPromise<A>(() => { const prismaExtensionContextClient = Prisma.getExtensionContext(this); // @ts-expect-error Can't predict these in advance return prismaExtensionContextClient["$queryRawTyped"](query); }); }, // $queryRawUnsafe: <T = unknown>(query: string, ...values: any[]) => PrismaPromise<T> // eslint-disable-next-line @typescript-eslint/no-explicit-any $queryRawUnsafeEffect: function <A = unknown, T = any>( this: T, query: string, // eslint-disable-next-line @typescript-eslint/no-explicit-any ...values: any[] ): Effect.Effect<A, UnknownException, never> { return Effect.tryPromise<A>(() => { const prismaExtensionContextClient = Prisma.getExtensionContext(this); // @ts-expect-error Can't predict these in advance return prismaExtensionContextClient["$queryRawUnsafe"]( query, ...values ); }); }, }, model: { $allModels: { @@ -88,7 +134,7 @@ export const prismaEffectExtension = { // For `customCall`, use the arguments from model `T` and the // operation `findFirst`. Add `customProperty` to the operation. Prisma.Args<T, O> > // Get the correct result types for the model of type `T`, // and the arguments of type `A` for `findFirst`. @@ -98,17 +144,22 @@ export const prismaEffectExtension = { ): Effect.Effect<Prisma.Result<T, A, O>, UnknownException, never> { return Effect.tryPromise<Prisma.Result<T, A, O>>(() => { // eslint-disable-next-line no-invalid-this const prismaExtensionContextClient = Prisma.getExtensionContext(this); // @ts-expect-error Can't predict these in advance // eslint-disable-next-line @typescript-eslint/no-explicit-any return prismaExtensionContextClient[method](x) as any; }); }, ]) ) as { [K in `${(typeof PrismaModelOps)[number]}Effect`]: < T, A, O extends Replace<K, "Effect", "">, >( this: T, x?: Prisma.Exact<A, Prisma.Args<T, O>> ) => Effect.Effect<Prisma.Result<T, A, O>, UnknownException, never>; }), }, -
aniravi24 revised this gist
Jan 22, 2025 . 1 changed file with 93 additions and 106 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,129 +1,116 @@ import { Prisma } from '@prisma/client'; import type { Replace } from 'type-fest'; import { Effect } from 'effect'; import { UnknownException } from 'effect/Cause'; type PrismaClientOp = '$executeRaw' | '$executeRawUnsafe' | '$queryRaw' | '$queryRawUnsafe' | '$runCommandRaw'; type PrismaModelOp = | 'aggregate' | 'count' | 'create' | 'createMany' | 'delete' | 'deleteMany' | 'findFirst' | 'findFirstOrThrow' | 'findMany' | 'findUnique' | 'findUniqueOrThrow' | 'groupBy' | 'update' | 'updateMany' | 'upsert'; const PrismaClientOps = [ '$executeRaw', '$executeRawUnsafe', '$queryRaw', '$queryRawUnsafe', '$runCommandRaw', ] as const satisfies readonly PrismaClientOp[]; const PrismaModelOps = [ 'aggregate', 'count', 'create', 'createMany', 'delete', 'deleteMany', 'findFirst', 'findFirstOrThrow', 'findMany', 'findUnique', 'findUniqueOrThrow', 'groupBy', 'update', 'updateMany', 'upsert', ] as const satisfies readonly PrismaModelOp[]; export const prismaEffectExtension = { client: { ...(Object.fromEntries( PrismaClientOps.map((method) => [ `${method}Effect`, function <T, A, O extends typeof method>( this: T, x?: Prisma.Exact<A, Prisma.Args<T, O>>, ): Effect.Effect<Prisma.Result<T, A, O>, UnknownException, never> { return Effect.tryPromise<Prisma.Result<T, A, O>>(() => { // eslint-disable-next-line no-invalid-this const prismaExtensionContextClient = Prisma.getExtensionContext(this); // @ts-expect-error Can't predict these in advance // eslint-disable-next-line @typescript-eslint/no-explicit-any return prismaExtensionContextClient[method](x) as any; }); }, ]), ) as { [K in `${(typeof PrismaClientOps)[number]}Effect`]: <T, A, O extends Replace<K, 'Effect', ''>>( this: T, x?: Prisma.Exact<A, Prisma.Args<T, O>>, ) => Effect.Effect<Prisma.Result<T, A, O>, UnknownException, never>; }), }, model: { $allModels: { ...(Object.fromEntries( PrismaModelOps.map((method) => [ `${method}Effect`, function <T, A, O extends typeof method>( // `this` is the current type (for example // it might be `prisma.user` at runtime). this: T, x?: Prisma.Exact< A, // For `customCall`, use the arguments from model `T` and the // operation `findFirst`. Add `customProperty` to the operation. Prisma.Args<T, O> >, // Get the correct result types for the model of type `T`, // and the arguments of type `A` for `findFirst`. // `Prisma.Result` computes the result for a given operation // such as `select {id: true}` in function `main` below. //, ): Effect.Effect<Prisma.Result<T, A, O>, UnknownException, never> { return Effect.tryPromise<Prisma.Result<T, A, O>>(() => { // eslint-disable-next-line no-invalid-this const prismaExtensionContextClient = Prisma.getExtensionContext(this); // @ts-expect-error Can't predict these in advance // eslint-disable-next-line @typescript-eslint/no-explicit-any return prismaExtensionContextClient[method](x) as any; }); }, ]), ) as { [K in `${(typeof PrismaModelOps)[number]}Effect`]: <T, A, O extends Replace<K, 'Effect', ''>>( this: T, x?: Prisma.Exact<A, Prisma.Args<T, O>>, ) => Effect.Effect<Prisma.Result<T, A, O>, UnknownException, never>; }), }, }, }; -
aniravi24 revised this gist
Jun 6, 2024 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -54,7 +54,7 @@ const PrismaModelOps = [ "upsert", ] as const satisfies readonly PrismaModelOp[]; export const prisma = new PrismaClient().$extends({ client: { $allModels: Object.fromEntries( PrismaClientOps.map((method) => [ -
aniravi24 created this gist
Jun 6, 2024 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,129 @@ type PrismaClientOp = | "$executeRaw" | "$executeRawUnsafe" | "$queryRaw" | "$queryRawUnsafe" | "$runCommandRaw"; type PrismaModelOp = | "aggregate" | "count" | "create" | "createMany" | "delete" | "deleteMany" | "findFirst" | "findFirstOrThrow" | "findMany" | "findUnique" | "findUniqueOrThrow" | "groupBy" | "update" | "updateMany" | "upsert"; const PrismaClientOps = [ "$executeRaw", "$executeRawUnsafe", "$queryRaw", "$queryRawUnsafe", "$runCommandRaw", ] as const satisfies readonly PrismaClientOp[]; const PrismaModelOps = [ // TODO These need to be implemented at the client level, not at the model level // "$executeRaw", // "$executeRawUnsafe", // "$queryRaw", // "$queryRawUnsafe", // "$runCommandRaw", "aggregate", "count", "create", "createMany", "delete", "deleteMany", "findFirst", "findFirstOrThrow", "findMany", "findUnique", "findUniqueOrThrow", "groupBy", "update", "updateMany", "upsert", ] as const satisfies readonly PrismaModelOp[]; export const rawPrismaClient = new PrismaClient().$extends({ client: { $allModels: Object.fromEntries( PrismaClientOps.map((method) => [ `${method}Effect`, function <T, A, O extends typeof method>( this: T, x?: Prisma.Exact<A, Prisma.Args<T, O>> ): Effect.Effect<never, never, Prisma.Result<T, A, O>> { // @ts-expect-error Can't predict these in advance return Effect.tryCatchPromise( // @ts-expect-error Can't predict these in advance // eslint-disable-next-line no-invalid-this () => this[method](x) as any, // handle error case however you want ); }, ]) ) as { [K in `${typeof PrismaClientOps[number]}Effect`]: < T, A, O extends Replace<K, "Effect", ""> >( this: T, x?: Prisma.Exact<A, Prisma.Args<T, O>> ) => Effect.Effect<never, never, Prisma.Result<T, A, O>>; }, }, model: { $allModels: Object.fromEntries( PrismaModelOps.map((method) => [ `${method}Effect`, function <T, A, O extends typeof method>( // `this` is the current type (for example // it might be `prisma.user` at runtime). this: T, x?: Prisma.Exact< A, // For `customCall`, use the arguments from model `T` and the // operation `findFirst`. Add `customProperty` to the operation. Prisma.Args<T, O> > // Get the correct result types for the model of type `T`, // and the arguments of type `A` for `findFirst`. // `Prisma.Result` computes the result for a given operation // such as `select {id: true}` in function `main` below. // ): Effect.Effect<never, never, Prisma.Result<T, A, O>> { // Override type safety here, because we cannot // predict the result types in advance. // @ts-expect-error Can't predict these in advance return Effect.tryCatchPromise( // @ts-expect-error Can't predict these in advance // eslint-disable-next-line no-invalid-this () => this[method](x) as any, // handle error case however you want ); }, ]) ) as { [K in `${typeof PrismaModelOps[number]}Effect`]: < T, A, O extends Replace<K, "Effect", ""> >( this: T, x?: Prisma.Exact<A, Prisma.Args<T, O>> ) => Effect.Effect<never, never, Prisma.Result<T, A, O>>; }, }, });