The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import type { QueryOperationTypes } from "@prisma/orm-postgres/adapter/operation-types"; |
| 2 | import { |
| 3 | defineContract as nativeDefineContract, |
| 4 | field as nativeField, |
| 5 | model as nativeModel, |
| 6 | } from "@prisma/orm-postgres/contract-builder"; |
| 7 | import type { Contract, NamespaceId } from "@prisma/orm-postgres/contract/types"; |
| 8 | import type { |
| 9 | ContractWithTypeMaps, |
| 10 | ExtractTypeMapsFromContract, |
| 11 | SqlStorage, |
| 12 | TypeMapsPhantomKey, |
| 13 | } from "@prisma/orm-postgres/family-contract/types"; |
| 14 | import type { CodecTypes } from "@prisma/orm-postgres/target/codec-types"; |
| 15 | import type { FieldHelpers, Helpers, ModelHelper } from "./AuthoringTypes.ts"; |
| 16 | import type { PostgresAggregateTypes } from "./PostgresAggregateTypes.ts"; |
| 17 | |
| 18 | // Native authoring stays behind an opt-in module to preserve the optional peer. |
| 19 | export * from "@prisma/orm-postgres/contract-builder"; |
| 20 | export type { DescriptorCodecInput } from "@prisma/orm-postgres/relational-core/ast"; |
| 21 | export const field = nativeField as FieldHelpers<typeof nativeField>; |
| 22 | export const model = nativeModel as ModelHelper; |
| 23 | |
| 24 | type NativeFactory = Parameters<typeof nativeDefineContract>[1]; |
| 25 | type Authored = ReturnType<NativeFactory>; |
| 26 | type Types = NonNullable<Authored["types"]>; |
| 27 | type Models = NonNullable<Authored["models"]>; |
| 28 | type Enums = NonNullable<Authored["enums"]>; |
| 29 | type Extensions = Parameters<typeof nativeDefineContract>[0]["extensions"]; |
| 30 | type Empty = Record<never, never>; |
| 31 | type IdentityNaming = { |
| 32 | readonly naming?: { |
| 33 | readonly tables?: "identity"; |
| 34 | readonly columns?: "identity"; |
| 35 | }; |
| 36 | }; |
| 37 | type ContractShape = Pick<Contract<SqlStorage>, "domain"> & { |
| 38 | readonly storage: Pick<SqlStorage, "namespaces">; |
| 39 | }; |
| 40 | type Known<T> = { |
| 41 | [K in keyof T as string extends K ? never : number extends K ? never : K]: T[K]; |
| 42 | }; |
| 43 | type State<F> = F extends { readonly __state: infer S } ? S : never; |
| 44 | type Fields<M> = M extends { readonly stageOne: { readonly fields: infer F } } ? F : never; |
| 45 | type Relations<M> = M extends { |
| 46 | readonly stageOne: { readonly relations: infer R }; |
| 47 | } |
| 48 | ? R |
| 49 | : never; |
| 50 | type Namespace<M> = M extends { |
| 51 | readonly stageOne: { readonly namespace?: infer N extends string }; |
| 52 | } |
| 53 | ? N extends "unbound" |
| 54 | ? "__unbound__" |
| 55 | : N |
| 56 | : "public"; |
| 57 | type Namespaces<Ms extends Models> = Namespace<Ms[keyof Ms]>; |
| 58 | type ModelRef<R> = R extends { readonly modelName: infer M extends string } |
| 59 | ? M |
| 60 | : R extends { readonly resolve: () => infer M extends string } |
| 61 | ? M |
| 62 | : never; |
| 63 | type Tuple<T> = T extends readonly string[] ? T : T extends string ? readonly [T] : readonly []; |
| 64 | type InlineIds<M> = { |
| 65 | [K in keyof Fields<M>]: State<Fields<M>[K]> extends { readonly id: object } ? K : never; |
| 66 | }[keyof Fields<M>] & |
| 67 | string; |
| 68 | type PrimaryFields<M> = M extends { |
| 69 | readonly __attributes: { |
| 70 | readonly id: { readonly fields: infer F extends readonly string[] }; |
| 71 | }; |
| 72 | } |
| 73 | ? F |
| 74 | : readonly [InlineIds<M>]; |
| 75 | type NullableFields<M> = { |
| 76 | [K in keyof Fields<M>]: State<Fields<M>[K]> extends { |
| 77 | readonly nullable: true; |
| 78 | } |
| 79 | ? K |
| 80 | : never; |
| 81 | }[keyof Fields<M>]; |
| 82 | type IsNullable<M, F> = Extract<Tuple<F>[number], NullableFields<M>> extends never ? false : true; |
| 83 | type NativeModels<C extends ContractShape> = C["domain"]["namespaces"][string]["models"]; |
| 84 | type Table< |
| 85 | C extends ContractShape, |
| 86 | M extends keyof NativeModels<C>, |
| 87 | > = NativeModels<C>[M]["storage"]["table"] & string; |
| 88 | type Column< |
| 89 | C extends ContractShape, |
| 90 | M extends keyof NativeModels<C>, |
| 91 | F, |
| 92 | > = F extends keyof NativeModels<C>[M]["storage"]["fields"] |
| 93 | ? NativeModels<C>[M]["storage"]["fields"][F] extends { |
| 94 | readonly column: infer Col extends string; |
| 95 | } |
| 96 | ? Col |
| 97 | : never |
| 98 | : never; |
| 99 | type Columns< |
| 100 | C extends ContractShape, |
| 101 | M extends keyof NativeModels<C>, |
| 102 | Fs extends readonly string[], |
| 103 | > = { readonly [K in keyof Fs]: Column<C, M, Fs[K]> }; |
| 104 | |
| 105 | type RelationType<C extends ContractShape, Ms extends Models, M extends keyof Ms, R> = R extends { |
| 106 | readonly kind: infer Kind; |
| 107 | readonly toModel: infer To; |
| 108 | } |
| 109 | ? ModelRef<To> extends keyof Ms |
| 110 | ? { |
| 111 | readonly to: { |
| 112 | readonly namespace: Namespace<Ms[ModelRef<To>]> & NamespaceId; |
| 113 | readonly model: ModelRef<To>; |
| 114 | }; |
| 115 | } & (R extends { |
| 116 | readonly kind: "belongsTo"; |
| 117 | readonly from: infer From; |
| 118 | readonly to: infer Target; |
| 119 | } |
| 120 | ? { |
| 121 | readonly cardinality: "N:1"; |
| 122 | readonly nullable: IsNullable<Ms[M], From>; |
| 123 | readonly on: { |
| 124 | readonly localFields: Tuple<From>; |
| 125 | readonly targetFields: Tuple<Target>; |
| 126 | }; |
| 127 | } |
| 128 | : R extends { |
| 129 | readonly kind: "hasMany" | "hasOne"; |
| 130 | readonly by: infer By; |
| 131 | } |
| 132 | ? { |
| 133 | readonly cardinality: Kind extends "hasOne" ? "1:1" : "1:N"; |
| 134 | readonly nullable: true; |
| 135 | readonly on: { |
| 136 | readonly localFields: PrimaryFields<Ms[M]>; |
| 137 | readonly targetFields: Tuple<By>; |
| 138 | }; |
| 139 | } |
| 140 | : R extends { |
| 141 | readonly kind: "manyToMany"; |
| 142 | readonly through: infer Through; |
| 143 | readonly from: infer From; |
| 144 | readonly to: infer Target; |
| 145 | } |
| 146 | ? ModelRef<Through> extends keyof Ms & keyof NativeModels<C> |
| 147 | ? { |
| 148 | readonly cardinality: "N:M"; |
| 149 | readonly on: { |
| 150 | readonly localFields: PrimaryFields<Ms[M]>; |
| 151 | readonly targetFields: PrimaryFields<Ms[ModelRef<To>]>; |
| 152 | }; |
| 153 | readonly through: { |
| 154 | readonly namespaceId: Namespace<Ms[ModelRef<Through>]>; |
| 155 | readonly table: Table<C, ModelRef<Through>>; |
| 156 | readonly parentColumns: Columns<C, ModelRef<Through>, Tuple<From>>; |
| 157 | readonly childColumns: Columns<C, ModelRef<Through>, Tuple<Target>>; |
| 158 | readonly targetColumns: Columns< |
| 159 | C, |
| 160 | ModelRef<To> & keyof NativeModels<C>, |
| 161 | PrimaryFields<Ms[ModelRef<To>]> |
| 162 | >; |
| 163 | }; |
| 164 | } |
| 165 | : never |
| 166 | : never) |
| 167 | : never |
| 168 | : never; |
| 169 | |
| 170 | type Unmany<T> = T extends readonly (infer A)[] ? A : T; |
| 171 | type FixFields<C extends ContractShape, Ms extends Models, Map> = Map extends { |
| 172 | readonly public: infer ModelsMap; |
| 173 | } |
| 174 | ? { |
| 175 | readonly [Ns in Namespaces<Ms>]: { |
| 176 | readonly [M in keyof ModelsMap & keyof Ms as Namespace<Ms[M]> extends Ns ? M : never]: { |
| 177 | readonly [F in keyof ModelsMap[M]]: F extends keyof Fields<Ms[M]> |
| 178 | ? true extends State<Fields<Ms[M]>[F]>["many"] |
| 179 | ? ModelsMap[M][F] |
| 180 | : M extends keyof NativeModels<C> |
| 181 | ? Column<C, M, F> extends keyof NativeColumns<C, M> |
| 182 | ? NativeColumns<C, M>[Column<C, M, F>] extends { |
| 183 | readonly many: true; |
| 184 | } |
| 185 | ? Unmany<ModelsMap[M][F]> |
| 186 | : ModelsMap[M][F] |
| 187 | : ModelsMap[M][F] |
| 188 | : ModelsMap[M][F] |
| 189 | : ModelsMap[M][F]; |
| 190 | }; |
| 191 | }; |
| 192 | } |
| 193 | : never; |
| 194 | type StorageFields<C extends ContractShape, Ms extends Models, Map> = { |
| 195 | readonly [Ns in keyof Map]: { |
| 196 | readonly [M in keyof Map[Ns] & keyof Ms & keyof NativeModels<C> as Table<C, M>]: { |
| 197 | readonly [F in keyof Map[Ns][M] as Column<C, M, F>]: Map[Ns][M][F]; |
| 198 | }; |
| 199 | }; |
| 200 | }; |
| 201 | export interface AuthoringTypeMaps< |
| 202 | Inputs, |
| 203 | Outputs, |
| 204 | StorageInputs, |
| 205 | StorageOutputs, |
| 206 | AdditionalCodecs, |
| 207 | > { |
| 208 | readonly codecTypes: CodecTypes & AdditionalCodecs; |
| 209 | readonly fieldInputTypes: Inputs; |
| 210 | readonly fieldOutputTypes: Outputs; |
| 211 | readonly storageColumnInputTypes: StorageInputs; |
| 212 | readonly storageColumnTypes: StorageOutputs; |
| 213 | readonly queryOperationTypes: QueryOperationTypes<CodecTypes>; |
| 214 | readonly aggregateTypes: PostgresAggregateTypes; |
| 215 | } |
| 216 | type FixedMaps< |
| 217 | C extends ContractShape, |
| 218 | Ms extends Models, |
| 219 | Maps = ExtractTypeMapsFromContract<C>, |
| 220 | > = Maps extends { |
| 221 | readonly codecTypes: infer Codecs; |
| 222 | readonly fieldInputTypes: infer Inputs; |
| 223 | readonly fieldOutputTypes: infer Outputs; |
| 224 | } |
| 225 | ? AuthoringTypeMaps< |
| 226 | Materialize<FixFields<C, Ms, Inputs>>, |
| 227 | Materialize<FixFields<C, Ms, Outputs>>, |
| 228 | Materialize<StorageFields<C, Ms, FixFields<C, Ms, Inputs>>>, |
| 229 | Materialize<StorageFields<C, Ms, FixFields<C, Ms, Outputs>>>, |
| 230 | Omit<Known<Codecs>, keyof CodecTypes> |
| 231 | > |
| 232 | : never; |
| 233 | type FixedModels<C extends ContractShape, Ms extends Models, Ns extends string> = { |
| 234 | readonly [M in keyof Ms & keyof NativeModels<C> as Namespace<Ms[M]> extends Ns ? M : never]: Omit< |
| 235 | NativeModels<C>[M], |
| 236 | "relations" | "storage" |
| 237 | > & { |
| 238 | readonly storage: NativeModels<C>[M]["storage"] & { |
| 239 | readonly namespaceId: Namespace<Ms[M]>; |
| 240 | }; |
| 241 | readonly relations: { |
| 242 | readonly [R in keyof Relations<Ms[M]>]: RelationType<C, Ms, M, State<Relations<Ms[M]>[R]>>; |
| 243 | }; |
| 244 | }; |
| 245 | }; |
| 246 | type InlineUniques< |
| 247 | C extends ContractShape, |
| 248 | Ms extends Models, |
| 249 | M extends keyof Ms & keyof NativeModels<C>, |
| 250 | > = { |
| 251 | [F in keyof Fields<Ms[M]> & string]: State<Fields<Ms[M]>[F]> extends { |
| 252 | readonly unique: object; |
| 253 | } |
| 254 | ? { readonly columns: readonly [Column<C, M, F>] } |
| 255 | : never; |
| 256 | }[keyof Fields<Ms[M]> & string]; |
| 257 | type AttributeUniques< |
| 258 | C extends ContractShape, |
| 259 | Ms extends Models, |
| 260 | M extends keyof Ms & keyof NativeModels<C>, |
| 261 | > = Ms[M] extends { |
| 262 | readonly __attributes: { readonly uniques: readonly (infer U)[] }; |
| 263 | } |
| 264 | ? U extends { readonly fields: infer Fs extends readonly string[] } |
| 265 | ? { readonly columns: Columns<C, M, Fs> } |
| 266 | : never |
| 267 | : never; |
| 268 | type NativeTables<C extends ContractShape> = |
| 269 | C["storage"]["namespaces"]["public"]["entries"]["table"]; |
| 270 | type NativeColumns<C extends ContractShape, M extends keyof NativeModels<C>> = |
| 271 | Table<C, M> extends keyof NativeTables<C> |
| 272 | ? NativeTables<C>[Table<C, M>] extends { readonly columns: infer Cols } |
| 273 | ? Cols |
| 274 | : never |
| 275 | : never; |
| 276 | type FixColumn<Col, S> = Omit<Col, "many" | "default"> & |
| 277 | (S extends { readonly many?: infer Many } |
| 278 | ? true extends Many |
| 279 | ? { readonly many: true } |
| 280 | : Empty |
| 281 | : Empty) & |
| 282 | (S extends { readonly default: infer D } ? { readonly default: D } : Empty); |
| 283 | type FixedTable< |
| 284 | C extends ContractShape, |
| 285 | Ms extends Models, |
| 286 | M extends keyof Ms & keyof NativeModels<C>, |
| 287 | > = |
| 288 | Table<C, M> extends keyof NativeTables<C> |
| 289 | ? Omit<NativeTables<C>[Table<C, M>], "columns" | "uniques"> & { |
| 290 | readonly columns: { |
| 291 | readonly [F in keyof Fields<Ms[M]> as Column<C, M, F>]: Column< |
| 292 | C, |
| 293 | M, |
| 294 | F |
| 295 | > extends keyof NativeColumns<C, M> |
| 296 | ? FixColumn<NativeColumns<C, M>[Column<C, M, F>], State<Fields<Ms[M]>[F]>> |
| 297 | : never; |
| 298 | }; |
| 299 | readonly uniques: readonly (InlineUniques<C, Ms, M> | AttributeUniques<C, Ms, M>)[]; |
| 300 | } |
| 301 | : never; |
| 302 | type FixStorage<C extends ContractShape, Ms extends Models> = Omit<C["storage"], "namespaces"> & { |
| 303 | readonly namespaces: { |
| 304 | readonly [Ns in Namespaces<Ms> | "public"]: { |
| 305 | readonly id: Ns; |
| 306 | readonly kind: string; |
| 307 | readonly entries: { |
| 308 | readonly table: { |
| 309 | readonly [ |
| 310 | M in keyof Ms & keyof NativeModels<C> as Namespace<Ms[M]> extends Ns |
| 311 | ? Table<C, M> |
| 312 | : never |
| 313 | ]: FixedTable<C, Ms, M>; |
| 314 | }; |
| 315 | }; |
| 316 | }; |
| 317 | }; |
| 318 | }; |
| 319 | |
| 320 | type PostgresCapabilities = { |
| 321 | readonly postgres: { |
| 322 | readonly distinctOn: true; |
| 323 | readonly jsonAgg: true; |
| 324 | readonly lateral: true; |
| 325 | readonly limit: true; |
| 326 | readonly orderBy: true; |
| 327 | readonly returning: true; |
| 328 | }; |
| 329 | readonly sql: { |
| 330 | readonly checkConstraint: true; |
| 331 | readonly defaultInInsert: true; |
| 332 | readonly enums: true; |
| 333 | readonly lateral: true; |
| 334 | readonly returning: true; |
| 335 | readonly scalarList: true; |
| 336 | }; |
| 337 | }; |
| 338 | type ExecutionDefaults<C extends ContractShape, Ms extends Models> = { |
| 339 | [M in keyof Ms & keyof NativeModels<C>]: { |
| 340 | [F in keyof Fields<Ms[M]>]: State<Fields<Ms[M]>[F]> extends { |
| 341 | readonly executionDefaults: infer D; |
| 342 | } |
| 343 | ? D & { |
| 344 | readonly ref: { |
| 345 | readonly namespace: Namespace<Ms[M]>; |
| 346 | readonly table: Table<C, M>; |
| 347 | readonly column: Column<C, M, F>; |
| 348 | }; |
| 349 | } |
| 350 | : never; |
| 351 | }[keyof Fields<Ms[M]>]; |
| 352 | }[keyof Ms & keyof NativeModels<C>]; |
| 353 | type Depths = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8]; |
| 354 | type Materialize<T, Depth extends number = 9> = Depth extends 0 |
| 355 | ? T |
| 356 | : T extends |
| 357 | | string |
| 358 | | number |
| 359 | | bigint |
| 360 | | boolean |
| 361 | | symbol |
| 362 | | null |
| 363 | | undefined |
| 364 | | Date |
| 365 | | Uint8Array |
| 366 | | ((...args: never[]) => unknown) |
| 367 | ? T |
| 368 | : T extends object |
| 369 | ? { |
| 370 | [K in keyof T]: K extends TypeMapsPhantomKey ? T[K] : Materialize<T[K], Depths[Depth]>; |
| 371 | } |
| 372 | : T; |
| 373 | /** Native runtime contract with rc.11's erased authoring metadata restored. */ |
| 374 | export type AuthoredContract<C extends ContractShape, Ms extends Models> = ContractWithTypeMaps< |
| 375 | Omit<C, "domain" | "storage" | "capabilities" | "execution" | TypeMapsPhantomKey> & { |
| 376 | readonly domain: Omit<C["domain"], "namespaces"> & { |
| 377 | readonly namespaces: { |
| 378 | readonly [Ns in Namespaces<Ms>]: { |
| 379 | readonly models: FixedModels<C, Ms, Ns>; |
| 380 | }; |
| 381 | }; |
| 382 | }; |
| 383 | readonly storage: FixStorage<C, Ms>; |
| 384 | readonly capabilities: PostgresCapabilities; |
| 385 | } & ([ExecutionDefaults<C, Ms>] extends [never] |
| 386 | ? Empty |
| 387 | : { |
| 388 | readonly execution: { |
| 389 | readonly executionHash: NonNullable<Contract["execution"]>["executionHash"]; |
| 390 | readonly mutations: { |
| 391 | readonly defaults: readonly ExecutionDefaults<C, Ms>[]; |
| 392 | }; |
| 393 | }; |
| 394 | }), |
| 395 | FixedMaps<C, Ms> |
| 396 | >; |
| 397 | interface DefineContract { |
| 398 | < |
| 399 | const T extends Types = Empty, |
| 400 | const M extends Models = Empty, |
| 401 | const E extends Extensions = undefined, |
| 402 | const EN extends Enums = Empty, |
| 403 | >( |
| 404 | definition: Omit< |
| 405 | Parameters<typeof nativeDefineContract<T, M, E, EN, Empty>>[0], |
| 406 | "types" | "models" | "naming" |
| 407 | > & |
| 408 | IdentityNaming & { readonly types?: T; readonly models?: M }, |
| 409 | ): Materialize< |
| 410 | AuthoredContract< |
| 411 | ReturnType<typeof nativeDefineContract<T, M, E extends undefined ? Empty : E, EN, Empty>>, |
| 412 | M |
| 413 | > |
| 414 | >; |
| 415 | < |
| 416 | const T extends Types = Empty, |
| 417 | const M extends Models = Empty, |
| 418 | const E extends Extensions = undefined, |
| 419 | const SE extends Enums = Empty, |
| 420 | const FE extends Enums = Empty, |
| 421 | >( |
| 422 | scaffold: Omit<Parameters<typeof nativeDefineContract<T, M, E, SE, FE>>[0], "naming"> & |
| 423 | IdentityNaming, |
| 424 | factory: ( |
| 425 | helpers: Helpers< |
| 426 | Parameters<Parameters<typeof nativeDefineContract<T, M, NoInfer<E>, SE, FE>>[1]>[0] |
| 427 | >, |
| 428 | ) => { readonly types?: T; readonly models?: M; readonly enums?: FE }, |
| 429 | ): Materialize< |
| 430 | AuthoredContract< |
| 431 | ReturnType<typeof nativeDefineContract<T, M, E extends undefined ? Empty : E, SE, FE>>, |
| 432 | M |
| 433 | > |
| 434 | >; |
| 435 | } |
| 436 | /** |
| 437 | * Preserve native Prisma authoring types without generated application imports. |
| 438 | * This is Prisma's own function; only its TypeScript declaration is adapted. |
| 439 | * Upstream: https://github.com/prisma/orm/issues/30341. |
| 440 | */ |
| 441 | export const defineContract = nativeDefineContract as unknown as DefineContract; |