The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { Contract } from "@prisma/orm-postgres/contract/types"; |
| 2 | import type { ExtractCodecTypes, SqlStorage } from "@prisma/orm-postgres/family-contract/types"; |
| 3 | import type { |
| 4 | BindSiteParams, |
| 5 | Declaration, |
| 6 | ParamsFromDeclaration, |
| 7 | PreparedExecution, |
| 8 | PreparedFor, |
| 9 | PreparedStatement, |
| 10 | Runtime, |
| 11 | RuntimeQueryable, |
| 12 | } from "@prisma/orm-postgres/family-runtime"; |
| 13 | import type { CodecTypesBase } from "@prisma/orm-postgres/relational-core/expression"; |
| 14 | import type { SqlQueryPlan } from "@prisma/orm-postgres/relational-core/plan"; |
| 15 | import type { PostgresStaticContext } from "@prisma/orm-postgres/static"; |
| 16 | import * as Effect from "effect/Effect"; |
| 17 | import * as Stream from "effect/Stream"; |
| 18 | import { type ClientError, wrapPrismaError } from "./Errors.ts"; |
| 19 | import type { QueryResult } from "./OrmClient.ts"; |
| 20 | |
| 21 | type QueryOptions<Params, Row> = Parameters<PreparedStatement<Params, Row>["query"]>[2]; |
| 22 | type ExecutionOptions<Params> = Parameters<PreparedExecution<Params>["execute"]>[2]; |
| 23 | |
| 24 | /** A reusable native statement with Effect and Stream consumption. */ |
| 25 | export interface PreparedQuery<Params, Row, E = never, R = never> extends Omit< |
| 26 | PreparedStatement<Params, Row>, |
| 27 | "query" |
| 28 | > { |
| 29 | query(params: Params, options?: QueryOptions<Params, Row>): QueryResult<Row, E, R>; |
| 30 | } |
| 31 | |
| 32 | /** A prepared mutation that reports statistics rather than rows. */ |
| 33 | export interface PreparedMutation<Params, E = never, R = never> extends Omit< |
| 34 | PreparedExecution<Params>, |
| 35 | "execute" |
| 36 | > { |
| 37 | execute( |
| 38 | params: Params, |
| 39 | options?: ExecutionOptions<Params>, |
| 40 | ): Effect.Effect<Awaited<ReturnType<PreparedExecution<Params>["execute"]>>, E, R>; |
| 41 | } |
| 42 | |
| 43 | export type Prepared<Params, Row, E = never, R = never> = |
| 44 | PreparedFor<Params, Row> extends PreparedStatement<Params, Row> |
| 45 | ? PreparedQuery<Params, Row, E, R> |
| 46 | : PreparedMutation<Params, E, R>; |
| 47 | |
| 48 | export interface Prepare<C extends Contract<SqlStorage>, E = never, R = never> { |
| 49 | <D extends Declaration<CT>, Row, CT extends CodecTypesBase = ExtractCodecTypes<C>>( |
| 50 | declaration: D, |
| 51 | build: (sql: PostgresStaticContext<C>["sql"], params: BindSiteParams<D>) => SqlQueryPlan<Row>, |
| 52 | ): Effect.Effect< |
| 53 | Prepared<ParamsFromDeclaration<D, CT>, Row, ClientError | E, R>, |
| 54 | ClientError | E, |
| 55 | R |
| 56 | >; |
| 57 | } |
| 58 | |
| 59 | export const makePrepare = |
| 60 | <C extends Contract<SqlStorage>, E, R>( |
| 61 | runtime: Effect.Effect<Runtime, E, R>, |
| 62 | sql: PostgresStaticContext<C>["sql"], |
| 63 | queryTarget: Effect.Effect<RuntimeQueryable, E, R> = runtime, |
| 64 | ): Prepare<C, E, R> => |
| 65 | <D extends Declaration<CT>, Row, CT extends CodecTypesBase = ExtractCodecTypes<C>>( |
| 66 | declaration: D, |
| 67 | build: (sql: PostgresStaticContext<C>["sql"], params: BindSiteParams<D>) => SqlQueryPlan<Row>, |
| 68 | ) => |
| 69 | Effect.gen(function* () { |
| 70 | const target = yield* runtime; |
| 71 | const statement = yield* Effect.tryPromise({ |
| 72 | try: () => target.prepare<D, Row, CT>(declaration, (params) => build(sql, params)), |
| 73 | catch: wrapPrismaError, |
| 74 | }); |
| 75 | type Params = ParamsFromDeclaration<D, CT>; |
| 76 | const metadata = { |
| 77 | sql: statement.sql, |
| 78 | ast: statement.ast, |
| 79 | meta: statement.meta, |
| 80 | slots: statement.slots, |
| 81 | }; |
| 82 | const result = |
| 83 | "query" in statement |
| 84 | ? { |
| 85 | ...metadata, |
| 86 | query: (params: Params, options?: QueryOptions<Params, Row>) => |
| 87 | Object.assign( |
| 88 | Effect.flatMap(queryTarget, (current) => |
| 89 | Effect.tryPromise({ |
| 90 | try: (signal) => |
| 91 | statement.query(current, params, { ...options, signal }).toArray(), |
| 92 | catch: wrapPrismaError, |
| 93 | }), |
| 94 | ), |
| 95 | { |
| 96 | stream: Stream.unwrap( |
| 97 | Effect.flatMap(queryTarget, (current) => |
| 98 | Effect.try({ |
| 99 | try: () => |
| 100 | Stream.fromAsyncIterable( |
| 101 | statement.query(current, params, options), |
| 102 | wrapPrismaError, |
| 103 | ), |
| 104 | catch: wrapPrismaError, |
| 105 | }), |
| 106 | ), |
| 107 | ), |
| 108 | }, |
| 109 | ), |
| 110 | } |
| 111 | : { |
| 112 | ...metadata, |
| 113 | execute: (params: Params, options?: ExecutionOptions<Params>) => |
| 114 | Effect.flatMap(queryTarget, (current) => |
| 115 | Effect.tryPromise({ |
| 116 | try: (signal) => |
| 117 | statement.execute(current, params, { |
| 118 | ...options, |
| 119 | signal, |
| 120 | }), |
| 121 | catch: wrapPrismaError, |
| 122 | }), |
| 123 | ), |
| 124 | }; |
| 125 | // Prisma selects the consumption method from the plan's affected-count brand. |
| 126 | return result as Prepared<Params, Row, ClientError | E, R>; |
| 127 | }); |