@prisma-next/target-postgres 0.16.0-dev.3 → 0.16.0-dev.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (166) hide show
  1. package/README.md +1 -1
  2. package/dist/{authoring-34L18kA1.mjs → authoring-DnO1B0N9.mjs} +29 -18
  3. package/dist/authoring-DnO1B0N9.mjs.map +1 -0
  4. package/dist/canonicalize-C7zKHw8m.mjs +59 -0
  5. package/dist/canonicalize-C7zKHw8m.mjs.map +1 -0
  6. package/dist/{canonicalize-D1F4vUnP.d.mts → canonicalize-CQlxSR4D.d.mts} +3 -18
  7. package/dist/canonicalize-CQlxSR4D.d.mts.map +1 -0
  8. package/dist/{codec-types-CQ07nOz-.d.mts → codec-types-DdoPc2dy.d.mts} +3 -2
  9. package/dist/codec-types-DdoPc2dy.d.mts.map +1 -0
  10. package/dist/codec-types.d.mts +1 -1
  11. package/dist/{codecs-G4DTY0F_.d.mts → codecs-CGE5dh94.d.mts} +7 -2
  12. package/dist/codecs-CGE5dh94.d.mts.map +1 -0
  13. package/dist/{codecs-TCsGgW-Y.mjs → codecs-Deand7sy.mjs} +84 -26
  14. package/dist/codecs-Deand7sy.mjs.map +1 -0
  15. package/dist/codecs.d.mts +2 -2
  16. package/dist/codecs.mjs +2 -2
  17. package/dist/contract-free.d.mts +1 -1
  18. package/dist/contract-free.d.mts.map +1 -1
  19. package/dist/contract-free.mjs +2 -2
  20. package/dist/{control-policy-CZKdMztu.mjs → control-policy-DEwdS3oK.mjs} +29 -29
  21. package/dist/control-policy-DEwdS3oK.mjs.map +1 -0
  22. package/dist/control.mjs +91 -239
  23. package/dist/control.mjs.map +1 -1
  24. package/dist/data-transform-BOWpliq8.mjs.map +1 -1
  25. package/dist/{ddl-DHXKzQ99.mjs → ddl-BQC6u-L9.mjs} +28 -4
  26. package/dist/{ddl-DHXKzQ99.mjs.map → ddl-BQC6u-L9.mjs.map} +1 -1
  27. package/dist/ddl.d.mts +3 -3
  28. package/dist/ddl.mjs +2 -2
  29. package/dist/{descriptor-meta-qubYaxQA.mjs → descriptor-meta-DGlJurgA.mjs} +4 -4
  30. package/dist/{descriptor-meta-qubYaxQA.mjs.map → descriptor-meta-DGlJurgA.mjs.map} +1 -1
  31. package/dist/{diff-database-schema-C-hnJ5mr.mjs → diff-database-schema-qRPwuYBH.mjs} +37 -15
  32. package/dist/diff-database-schema-qRPwuYBH.mjs.map +1 -0
  33. package/dist/diff-database-schema.mjs +1 -1
  34. package/dist/{errors-ByDS0xJL.mjs → errors-DBSNx7EJ.mjs} +6 -2
  35. package/dist/errors-DBSNx7EJ.mjs.map +1 -0
  36. package/dist/errors.d.mts +4 -1
  37. package/dist/errors.d.mts.map +1 -1
  38. package/dist/errors.mjs +2 -2
  39. package/dist/issue-planner.d.mts +2 -2
  40. package/dist/issue-planner.d.mts.map +1 -1
  41. package/dist/issue-planner.mjs +1 -1
  42. package/dist/migration.d.mts +2 -2
  43. package/dist/migration.mjs +2 -2
  44. package/dist/{nodes-DJC5B8Id.mjs → nodes-04EILM-z.mjs} +65 -2
  45. package/dist/nodes-04EILM-z.mjs.map +1 -0
  46. package/dist/{nodes-CQzM2hR0.d.mts → nodes-DjCsN-uv.d.mts} +69 -4
  47. package/dist/nodes-DjCsN-uv.d.mts.map +1 -0
  48. package/dist/{op-factory-call-DT7kdjVP.d.mts → op-factory-call-0TB5XNij.d.mts} +46 -8
  49. package/dist/op-factory-call-0TB5XNij.d.mts.map +1 -0
  50. package/dist/{op-factory-call-DuRj7WXX.mjs → op-factory-call-CIaPDDUc.mjs} +147 -64
  51. package/dist/op-factory-call-CIaPDDUc.mjs.map +1 -0
  52. package/dist/op-factory-call.d.mts +2 -2
  53. package/dist/op-factory-call.mjs +2 -2
  54. package/dist/pack.d.mts +2 -2
  55. package/dist/pack.d.mts.map +1 -1
  56. package/dist/pack.mjs +1 -1
  57. package/dist/{planner-DPGxFcXo.mjs → planner-BhnkIJX-.mjs} +121 -13
  58. package/dist/planner-BhnkIJX-.mjs.map +1 -0
  59. package/dist/{planner-ddl-builders-BKlmGDJl.mjs → planner-ddl-builders-CVo3L7Nd.mjs} +12 -7
  60. package/dist/planner-ddl-builders-CVo3L7Nd.mjs.map +1 -0
  61. package/dist/planner-ddl-builders.d.mts.map +1 -1
  62. package/dist/planner-ddl-builders.mjs +1 -1
  63. package/dist/{planner-produced-postgres-migration-CFDpqWWE.mjs → planner-produced-postgres-migration-BE3EJIRK.mjs} +9 -6
  64. package/dist/planner-produced-postgres-migration-BE3EJIRK.mjs.map +1 -0
  65. package/dist/{planner-produced-postgres-migration-C-dwGdb2.d.mts → planner-produced-postgres-migration-CRAHl6ak.d.mts} +3 -3
  66. package/dist/planner-produced-postgres-migration-CRAHl6ak.d.mts.map +1 -0
  67. package/dist/planner-produced-postgres-migration.d.mts +1 -1
  68. package/dist/planner-produced-postgres-migration.mjs +1 -1
  69. package/dist/{planner-schema-lookup-CiVaAQP-.mjs → planner-schema-lookup-D3Y_OL-C.mjs} +3 -3
  70. package/dist/planner-schema-lookup-D3Y_OL-C.mjs.map +1 -0
  71. package/dist/planner-schema-lookup.mjs +1 -1
  72. package/dist/{planner-sql-checks-BuB877wH.mjs → planner-sql-checks-CmiQ6Nei.mjs} +3 -3
  73. package/dist/{planner-sql-checks-BuB877wH.mjs.map → planner-sql-checks-CmiQ6Nei.mjs.map} +1 -1
  74. package/dist/planner-sql-checks.mjs +1 -1
  75. package/dist/planner.d.mts +27 -1
  76. package/dist/planner.d.mts.map +1 -1
  77. package/dist/planner.mjs +2 -2
  78. package/dist/{postgres-contract-view-DebHMqGR.mjs → postgres-contract-view-BeIEyZfD.mjs} +3 -3
  79. package/dist/{postgres-contract-view-DebHMqGR.mjs.map → postgres-contract-view-BeIEyZfD.mjs.map} +1 -1
  80. package/dist/{postgres-migration-C_OPFhIF.mjs → postgres-migration--BzvpA92.mjs} +9 -6
  81. package/dist/postgres-migration--BzvpA92.mjs.map +1 -0
  82. package/dist/{postgres-migration-DLIvzHjX.d.mts → postgres-migration-BXelnqHO.d.mts} +15 -10
  83. package/dist/postgres-migration-BXelnqHO.d.mts.map +1 -0
  84. package/dist/{postgres-schema-CewLYHB7.mjs → postgres-schema-CTplQZDK.mjs} +2 -2
  85. package/dist/{postgres-schema-CewLYHB7.mjs.map → postgres-schema-CTplQZDK.mjs.map} +1 -1
  86. package/dist/{render-ops-BREh1kHe.mjs → render-ops-TcY9XEkz.mjs} +7 -2
  87. package/dist/render-ops-TcY9XEkz.mjs.map +1 -0
  88. package/dist/render-ops.d.mts.map +1 -1
  89. package/dist/render-ops.mjs +1 -1
  90. package/dist/{render-typescript-7yqILcwr.mjs → render-typescript-Ta7_6CGf.mjs} +20 -8
  91. package/dist/render-typescript-Ta7_6CGf.mjs.map +1 -0
  92. package/dist/render-typescript.d.mts +2 -0
  93. package/dist/render-typescript.d.mts.map +1 -1
  94. package/dist/render-typescript.mjs +1 -1
  95. package/dist/{resolve-ddl-schema-D0wi_P9Q.mjs → resolve-ddl-schema-BAsJEKW8.mjs} +2 -2
  96. package/dist/{resolve-ddl-schema-D0wi_P9Q.mjs.map → resolve-ddl-schema-BAsJEKW8.mjs.map} +1 -1
  97. package/dist/rls-canonicalize.d.mts +2 -26
  98. package/dist/rls-canonicalize.mjs +2 -2
  99. package/dist/runtime.mjs +1 -1
  100. package/dist/schema-ir-annotations.mjs +1 -1
  101. package/dist/{sql-utils-SU4FDvIV.mjs → sql-utils-B2lzkKfP.mjs} +22 -19
  102. package/dist/sql-utils-B2lzkKfP.mjs.map +1 -0
  103. package/dist/sql-utils.d.mts +4 -9
  104. package/dist/sql-utils.d.mts.map +1 -1
  105. package/dist/sql-utils.mjs +2 -2
  106. package/dist/types.mjs +1 -1
  107. package/package.json +20 -20
  108. package/src/contract-free/ddl.ts +47 -0
  109. package/src/core/authoring.ts +35 -26
  110. package/src/core/codec-helpers.ts +69 -16
  111. package/src/core/codec-type-map.ts +2 -0
  112. package/src/core/codecs.ts +29 -14
  113. package/src/core/ddl/nodes.ts +104 -0
  114. package/src/core/errors.ts +30 -0
  115. package/src/core/migrations/column-ddl-rendering.ts +2 -1
  116. package/src/core/migrations/contract-to-postgres-database-schema-node.ts +38 -14
  117. package/src/core/migrations/issue-planner.ts +40 -13
  118. package/src/core/migrations/op-factory-call.ts +189 -44
  119. package/src/core/migrations/operations/constraints.ts +3 -2
  120. package/src/core/migrations/operations/data-transform.ts +1 -1
  121. package/src/core/migrations/operations/indexes.ts +80 -32
  122. package/src/core/migrations/operations/rls.ts +4 -1
  123. package/src/core/migrations/planner-ddl-builders.ts +16 -6
  124. package/src/core/migrations/planner-produced-postgres-migration.ts +8 -1
  125. package/src/core/migrations/planner-schema-lookup.ts +2 -2
  126. package/src/core/migrations/planner.ts +163 -5
  127. package/src/core/migrations/postgres-migration.ts +26 -8
  128. package/src/core/migrations/render-ops.ts +4 -1
  129. package/src/core/migrations/render-typescript.ts +20 -8
  130. package/src/core/migrations/runner.ts +7 -3
  131. package/src/core/psl-infer/infer-psl-contract.ts +36 -185
  132. package/src/core/psl-infer/postgres-type-map.ts +43 -65
  133. package/src/core/rls/canonicalize.ts +4 -21
  134. package/src/core/sql-utils.ts +16 -27
  135. package/src/exports/codecs.ts +2 -0
  136. package/src/exports/ddl.ts +4 -0
  137. package/src/exports/errors.ts +1 -1
  138. package/src/exports/op-factory-call.ts +1 -0
  139. package/src/exports/rls-canonicalize.ts +0 -6
  140. package/src/exports/sql-utils.ts +0 -1
  141. package/dist/authoring-34L18kA1.mjs.map +0 -1
  142. package/dist/canonicalize-D1F4vUnP.d.mts.map +0 -1
  143. package/dist/codec-types-CQ07nOz-.d.mts.map +0 -1
  144. package/dist/codecs-G4DTY0F_.d.mts.map +0 -1
  145. package/dist/codecs-TCsGgW-Y.mjs.map +0 -1
  146. package/dist/control-policy-CZKdMztu.mjs.map +0 -1
  147. package/dist/diff-database-schema-C-hnJ5mr.mjs.map +0 -1
  148. package/dist/errors-ByDS0xJL.mjs.map +0 -1
  149. package/dist/nodes-CQzM2hR0.d.mts.map +0 -1
  150. package/dist/nodes-DJC5B8Id.mjs.map +0 -1
  151. package/dist/op-factory-call-DT7kdjVP.d.mts.map +0 -1
  152. package/dist/op-factory-call-DuRj7WXX.mjs.map +0 -1
  153. package/dist/planner-DPGxFcXo.mjs.map +0 -1
  154. package/dist/planner-ddl-builders-BKlmGDJl.mjs.map +0 -1
  155. package/dist/planner-produced-postgres-migration-C-dwGdb2.d.mts.map +0 -1
  156. package/dist/planner-produced-postgres-migration-CFDpqWWE.mjs.map +0 -1
  157. package/dist/planner-schema-lookup-CiVaAQP-.mjs.map +0 -1
  158. package/dist/postgres-migration-C_OPFhIF.mjs.map +0 -1
  159. package/dist/postgres-migration-DLIvzHjX.d.mts.map +0 -1
  160. package/dist/render-ops-BREh1kHe.mjs.map +0 -1
  161. package/dist/render-typescript-7yqILcwr.mjs.map +0 -1
  162. package/dist/rls-canonicalize.d.mts.map +0 -1
  163. package/dist/sql-utils-SU4FDvIV.mjs.map +0 -1
  164. package/dist/wire-name-DbQ9-2RC.mjs +0 -104
  165. package/dist/wire-name-DbQ9-2RC.mjs.map +0 -1
  166. package/src/core/rls/wire-name.ts +0 -33
package/dist/control.mjs CHANGED
@@ -1,11 +1,12 @@
1
+ import { n as postgresError } from "./errors-DBSNx7EJ.mjs";
1
2
  import { n as postgresResolveDefault } from "./default-normalizer-B9ZUiyUE.mjs";
2
- import { t as postgresTargetDescriptorMeta } from "./descriptor-meta-qubYaxQA.mjs";
3
+ import { t as postgresTargetDescriptorMeta } from "./descriptor-meta-DGlJurgA.mjs";
3
4
  import { a as postgresDiffSubjectGranularity, i as postgresDiffSubjectEntityKind } from "./postgres-table-schema-node-D6LvInCe.mjs";
4
5
  import { i as PostgresDatabaseSchemaNode } from "./postgres-role-schema-node-bg32e7I-.mjs";
5
- import { n as diffPostgresSchema, r as contractToPostgresDatabaseSchemaNode } from "./diff-database-schema-C-hnJ5mr.mjs";
6
- import { r as renderDefaultLiteral } from "./planner-ddl-builders-BKlmGDJl.mjs";
7
- import { n as PostgresContractSerializer } from "./postgres-contract-view-DebHMqGR.mjs";
8
- import { t as createPostgresMigrationPlanner } from "./planner-DPGxFcXo.mjs";
6
+ import { n as diffPostgresSchema, r as contractToPostgresDatabaseSchemaNode } from "./diff-database-schema-qRPwuYBH.mjs";
7
+ import { r as renderDefaultLiteral } from "./planner-ddl-builders-CVo3L7Nd.mjs";
8
+ import { n as PostgresContractSerializer } from "./postgres-contract-view-BeIEyZfD.mjs";
9
+ import { t as createPostgresMigrationPlanner } from "./planner-BhnkIJX-.mjs";
9
10
  import { UNBOUND_NAMESPACE_ID, coordinateKey, elementCoordinates } from "@prisma-next/framework-components/ir";
10
11
  import { blindCast } from "@prisma-next/utils/casts";
11
12
  import { buildNativeTypeExpander, runnerFailure, runnerSuccess } from "@prisma-next/family-sql/control";
@@ -13,9 +14,10 @@ import { ifDefined } from "@prisma-next/utils/defined";
13
14
  import { SqlSchemaIR, SqlTableIR } from "@prisma-next/sql-schema-ir/types";
14
15
  import { APP_SPACE_ID } from "@prisma-next/framework-components/control";
15
16
  import { notOk, ok, okVoid } from "@prisma-next/utils/result";
17
+ import { InternalError } from "@prisma-next/utils/internal-error";
16
18
  import { SqlSchemaVerifierBase } from "@prisma-next/family-sql/ir";
17
19
  import { SqlQueryError } from "@prisma-next/sql-errors";
18
- import { buildChildRelationField, deriveRelationFieldName, inferRelations, mapDefault, parseRawDefault, toEnumMemberName, toEnumName, toFieldName, toModelName, toNamedTypeName } from "@prisma-next/family-sql/psl-infer";
20
+ import { buildChildRelationField, deriveRelationFieldName, inferRelations, mapDefault, parseRawDefault, toEnumMemberName, toEnumName, toFieldName, toModelName } from "@prisma-next/family-sql/psl-infer";
19
21
  import { UNSPECIFIED_PSL_NAMESPACE_ID, makePslNamespace, makePslNamespaceEntries } from "@prisma-next/framework-components/psl-ast";
20
22
  //#region src/core/migrations/runner.ts
21
23
  const LOCK_DOMAIN = "prisma_next.contract.marker";
@@ -47,7 +49,10 @@ var PostgresMigrationRunner = class {
47
49
  async executeOnConnection(options) {
48
50
  const schema = options.schemaName ?? Object.keys(options.destinationContract.storage.namespaces).find((id) => id !== UNBOUND_NAMESPACE_ID) ?? UNBOUND_NAMESPACE_ID;
49
51
  const driver = options.driver;
50
- if (options.space !== void 0 && options.space !== options.plan.spaceId) throw new Error(`SqlMigrationRunner: options.space (${options.space}) does not match plan.spaceId (${options.plan.spaceId})`);
52
+ if (options.space !== void 0 && options.space !== options.plan.spaceId) throw postgresError("MIGRATION.CONTRACT_SPACE_VIOLATION", `SqlMigrationRunner: options.space (${options.space}) does not match plan.spaceId (${options.plan.spaceId})`, { meta: {
53
+ space: options.space,
54
+ planSpaceId: options.plan.spaceId
55
+ } });
51
56
  const space = options.plan.spaceId;
52
57
  const lockKey = `${LOCK_DOMAIN}:${schema}:${space}`;
53
58
  const planOps = blindCast(await Promise.all(options.plan.operations));
@@ -177,7 +182,7 @@ var PostgresMigrationRunner = class {
177
182
  async ensureControlTables(driver, contract) {
178
183
  const lowererContext = { contract };
179
184
  const [schemaQuery, ...tableQueries] = this.family.bootstrapControlTableQueries();
180
- if (schemaQuery === void 0) throw new Error("Postgres control-table bootstrap must include CREATE SCHEMA");
185
+ if (schemaQuery === void 0) throw new InternalError("Postgres control-table bootstrap must include CREATE SCHEMA");
181
186
  await this.executeStatement(driver, await this.family.lowerAst(schemaQuery, lowererContext));
182
187
  const legacyDetection = await this.detectLegacyMarkerShape(driver);
183
188
  if (!legacyDetection.ok) return legacyDetection;
@@ -347,7 +352,7 @@ var PostgresMigrationRunner = class {
347
352
  const space = options.plan.spaceId;
348
353
  const edges = options.migrationEdges;
349
354
  const totalEdgeOps = edges.reduce((sum, edge) => sum + edge.operationCount, 0);
350
- if (totalEdgeOps !== planOpsLength) throw new Error(`Ledger write: plan.operations length (${planOpsLength}) does not match sum of migrationEdges operationCount (${totalEdgeOps})`);
355
+ if (totalEdgeOps !== planOpsLength) throw new InternalError(`Ledger write: plan.operations length (${planOpsLength}) does not match sum of migrationEdges operationCount (${totalEdgeOps})`);
351
356
  let offset = 0;
352
357
  for (const edge of edges) {
353
358
  const edgeOps = executedOperations.slice(offset, offset + edge.operationCount);
@@ -432,170 +437,82 @@ const POSTGRES_TO_PSL = {
432
437
  bigint: "BigInt",
433
438
  float8: "Float",
434
439
  "double precision": "Float",
435
- numeric: "Decimal",
436
- decimal: "Decimal",
437
- timestamptz: "DateTime",
438
- "timestamp with time zone": "DateTime",
439
- jsonb: "Json",
440
+ jsonb: "Jsonb",
440
441
  bytea: "Bytes"
441
442
  };
442
443
  const PRESERVED_NATIVE_TYPES = {
443
- "character varying": {
444
- pslType: "String",
445
- attributeName: "db.VarChar"
446
- },
447
- character: {
448
- pslType: "String",
449
- attributeName: "db.Char"
450
- },
451
- char: {
452
- pslType: "String",
453
- attributeName: "db.Char"
454
- },
455
- varchar: {
456
- pslType: "String",
457
- attributeName: "db.VarChar"
458
- },
459
- uuid: {
460
- pslType: "String",
461
- attributeName: "db.Uuid"
462
- },
463
- inet: {
464
- pslType: "String",
465
- attributeName: "db.Inet"
466
- },
467
- int2: {
468
- pslType: "Int",
469
- attributeName: "db.SmallInt"
470
- },
471
- smallint: {
472
- pslType: "Int",
473
- attributeName: "db.SmallInt"
474
- },
475
- float4: {
476
- pslType: "Float",
477
- attributeName: "db.Real"
478
- },
479
- real: {
480
- pslType: "Float",
481
- attributeName: "db.Real"
482
- },
483
- timestamp: {
484
- pslType: "DateTime",
485
- attributeName: "db.Timestamp"
486
- },
487
- "timestamp without time zone": {
488
- pslType: "DateTime",
489
- attributeName: "db.Timestamp"
490
- },
491
- date: {
492
- pslType: "DateTime",
493
- attributeName: "db.Date"
494
- },
495
- time: {
496
- pslType: "DateTime",
497
- attributeName: "db.Time"
498
- },
499
- "time without time zone": {
500
- pslType: "DateTime",
501
- attributeName: "db.Time"
502
- },
503
- timetz: {
504
- pslType: "DateTime",
505
- attributeName: "db.Timetz"
506
- },
507
- "time with time zone": {
508
- pslType: "DateTime",
509
- attributeName: "db.Timetz"
510
- },
511
- json: {
512
- pslType: "Json",
513
- attributeName: "db.Json"
514
- }
444
+ "character varying": "VarChar",
445
+ character: "Char",
446
+ char: "Char",
447
+ varchar: "VarChar",
448
+ uuid: "Uuid",
449
+ inet: "Inet",
450
+ int2: "SmallInt",
451
+ smallint: "SmallInt",
452
+ float4: "Real",
453
+ real: "Real",
454
+ numeric: "Numeric",
455
+ decimal: "Numeric",
456
+ timestamp: "Timestamp",
457
+ "timestamp without time zone": "Timestamp",
458
+ timestamptz: "Timestamptz",
459
+ "timestamp with time zone": "Timestamptz",
460
+ date: "Date",
461
+ time: "Time",
462
+ "time without time zone": "Time",
463
+ timetz: "Timetz",
464
+ "time with time zone": "Timetz",
465
+ json: "Json"
515
466
  };
516
467
  const PARAMETERIZED_NATIVE_TYPES = {
517
- "character varying": {
518
- pslType: "String",
519
- attributeName: "db.VarChar"
520
- },
521
- character: {
522
- pslType: "String",
523
- attributeName: "db.Char"
524
- },
525
- char: {
526
- pslType: "String",
527
- attributeName: "db.Char"
528
- },
529
- varchar: {
530
- pslType: "String",
531
- attributeName: "db.VarChar"
532
- },
533
- numeric: {
534
- pslType: "Decimal",
535
- attributeName: "db.Numeric"
536
- },
537
- timestamp: {
538
- pslType: "DateTime",
539
- attributeName: "db.Timestamp"
540
- },
541
- timestamptz: {
542
- pslType: "DateTime",
543
- attributeName: "db.Timestamptz"
544
- },
545
- time: {
546
- pslType: "DateTime",
547
- attributeName: "db.Time"
548
- },
549
- timetz: {
550
- pslType: "DateTime",
551
- attributeName: "db.Timetz"
552
- }
468
+ "character varying": "VarChar",
469
+ character: "Char",
470
+ char: "Char",
471
+ varchar: "VarChar",
472
+ numeric: "Numeric",
473
+ decimal: "Numeric",
474
+ timestamp: "Timestamp",
475
+ timestamptz: "Timestamptz",
476
+ time: "Time",
477
+ timetz: "Timetz"
553
478
  };
554
479
  const PARAMETERIZED_TYPE_PATTERN = /^(.+?)\((.+)\)$/;
555
480
  function getOwnMappingValue(map, key) {
556
481
  return Object.hasOwn(map, key) ? map[key] : void 0;
557
482
  }
558
- function getOwnRecordValue(map, key) {
559
- return Object.hasOwn(map, key) ? map[key] : void 0;
560
- }
561
- function createNativeTypeAttribute(name, args) {
562
- return args && args.length > 0 ? {
563
- name,
564
- args
565
- } : { name };
566
- }
567
483
  function splitTypeParameterList(params) {
568
484
  return params.split(",").map((part) => part.trim()).filter((part) => part.length > 0);
569
485
  }
570
486
  function createPostgresTypeMap(enumTypeNames) {
571
487
  return { resolve(nativeType) {
572
488
  if (enumTypeNames?.has(nativeType)) return {
573
- pslType: nativeType,
489
+ pslType: { name: nativeType },
574
490
  nativeType
575
491
  };
576
492
  const paramMatch = nativeType.match(PARAMETERIZED_TYPE_PATTERN);
577
493
  if (paramMatch) {
578
494
  const [, baseType = nativeType, params = ""] = paramMatch;
579
- const template = getOwnRecordValue(PARAMETERIZED_NATIVE_TYPES, baseType);
580
- if (template) return {
581
- pslType: template.pslType,
495
+ const typeName = getOwnMappingValue(PARAMETERIZED_NATIVE_TYPES, baseType);
496
+ if (typeName) return {
497
+ pslType: {
498
+ name: typeName,
499
+ args: splitTypeParameterList(params)
500
+ },
582
501
  nativeType,
583
502
  typeParams: {
584
503
  baseType,
585
504
  params
586
- },
587
- nativeTypeAttribute: createNativeTypeAttribute(template.attributeName, splitTypeParameterList(params))
505
+ }
588
506
  };
589
507
  }
590
- const preservedType = getOwnRecordValue(PRESERVED_NATIVE_TYPES, nativeType);
508
+ const preservedType = getOwnMappingValue(PRESERVED_NATIVE_TYPES, nativeType);
591
509
  if (preservedType) return {
592
- pslType: preservedType.pslType,
593
- nativeType,
594
- nativeTypeAttribute: createNativeTypeAttribute(preservedType.attributeName)
510
+ pslType: { name: preservedType },
511
+ nativeType
595
512
  };
596
513
  const pslType = getOwnMappingValue(POSTGRES_TO_PSL, nativeType);
597
514
  if (pslType) return {
598
- pslType,
515
+ pslType: { name: pslType },
599
516
  nativeType
600
517
  };
601
518
  return {
@@ -704,7 +621,10 @@ function resolveForeignKeys(tables, owners) {
704
621
  }));
705
622
  if (owner !== void 0) {
706
623
  const target = resolveCrossSpaceTarget(owner, fk.referencedSchema, fk.referencedTable);
707
- if (target === void 0) throw new Error(`contract infer: described contract space "${owner.spaceId}" owns storage coordinate "${fk.referencedSchema}.${fk.referencedTable}" but declares no domain model mapped to it. A pack that describes a table must also declare the domain model it maps to; this pack is malformed.`);
624
+ if (target === void 0) throw postgresError("CONTRACT.PACK_CONTRIBUTION_INVALID", `contract infer: described contract space "${owner.spaceId}" owns storage coordinate "${fk.referencedSchema}.${fk.referencedTable}" but declares no domain model mapped to it. A pack that describes a table must also declare the domain model it maps to; this pack is malformed.`, { meta: {
625
+ spaceId: owner.spaceId,
626
+ tableName: fk.referencedTable
627
+ } });
708
628
  if (!crossSpaceFieldNamesByTable.has(fk.referencedTable)) crossSpaceFieldNamesByTable.set(fk.referencedTable, target.fieldNamesByColumn);
709
629
  const fieldName = deriveRelationFieldName(fk.columns, fk.referencedTable);
710
630
  const optional = fk.columns.some((columnName) => table.columns[columnName]?.nullable ?? false);
@@ -799,10 +719,13 @@ function inferPostgresPslContract(tree, describedContracts) {
799
719
  entityKind: "table",
800
720
  entityName: tableName
801
721
  }))) continue;
802
- if (tables[tableName] !== void 0) throw new Error(`contract infer: duplicate table name "${tableName}" across schemas is not yet supported (single-namespace PSL inference emits one flat bucket; multi-namespace \`namespace { … }\` output is a later slice).`);
722
+ if (tables[tableName] !== void 0) throw postgresError("CONTRACT.INFER_UNSUPPORTED", `contract infer: duplicate table name "${tableName}" across schemas is not yet supported (single-namespace PSL inference emits one flat bucket; multi-namespace \`namespace { … }\` output is a later slice).`, { meta: { tableName } });
803
723
  if (ownedEnumTypes !== void 0) for (const column of Object.values(table.columns)) {
804
724
  const owningSpaceId = ownedEnumTypes.get(column.nativeType);
805
- if (owningSpaceId !== void 0) throw new Error(`contract infer: column "${tableName}"."${column.name}" is typed by native enum type "${column.nativeType}", which extension pack space "${owningSpaceId}" already describes. A cross-space enum-typed column has no authorable PSL form yet; describe the table in that pack's contract or retype the column before re-running contract infer.`);
725
+ if (owningSpaceId !== void 0) throw postgresError("CONTRACT.INFER_UNSUPPORTED", `contract infer: column "${tableName}"."${column.name}" is typed by native enum type "${column.nativeType}", which extension pack space "${owningSpaceId}" already describes. A cross-space enum-typed column has no authorable PSL form yet; describe the table in that pack's contract or retype the column before re-running contract infer.`, { meta: {
726
+ tableName,
727
+ columnName: column.name
728
+ } });
806
729
  }
807
730
  tables[tableName] = new SqlTableIR(table);
808
731
  tableNamespaceNames.add(namespace.schemaName);
@@ -811,7 +734,7 @@ function inferPostgresPslContract(tree, describedContracts) {
811
734
  let wrapNamespaceName;
812
735
  if (enumDefinitions.size > 0) {
813
736
  const contentNamespaces = /* @__PURE__ */ new Set([...enumNamespaceNames, ...tableNamespaceNames]);
814
- if (contentNamespaces.size > 1) throw new Error(`contract infer: native enum adoption with content across multiple schemas is not yet supported (single-namespace PSL inference emits one \`namespace { … }\` block; multi-namespace output is a later slice). Schemas: ${[...contentNamespaces].sort().join(", ")}.`);
737
+ if (contentNamespaces.size > 1) throw postgresError("CONTRACT.INFER_UNSUPPORTED", `contract infer: native enum adoption with content across multiple schemas is not yet supported (single-namespace PSL inference emits one \`namespace { … }\` block; multi-namespace output is a later slice). Schemas: ${[...contentNamespaces].sort().join(", ")}.`, { meta: { schemas: [...contentNamespaces].sort() } });
815
738
  wrapNamespaceName = [...contentNamespaces][0];
816
739
  }
817
740
  const { tables: resolvedTables, extraRelationsByTable, crossSpaceFieldNamesByTable, danglingForeignKeysByTable } = resolveForeignKeys(tables, owners);
@@ -841,19 +764,11 @@ function buildPslDocumentAst(schemaIR, options, foreignKeyExtras, namespaceName)
841
764
  const { enumNameMap: bareEnumNameMap, enumBlocks } = buildNativeEnumBlocks(options.enumInfo?.definitions ?? /* @__PURE__ */ new Map(), modelNames);
842
765
  const enumNameMap = new Map(bareEnumNameMap);
843
766
  if (namespaceName !== void 0) for (const [typeName, pslName] of bareEnumNameMap) enumNameMap.set(`${namespaceName}.${typeName}`, pslName);
844
- const reservedNamedTypeNames = createReservedNamedTypeNames(modelNames, enumNameMap);
845
767
  const fieldNamesByTable = new Map([...crossSpaceFieldNamesByTable, ...buildFieldNamesByTable(schemaIR.tables)]);
846
768
  const { relationsByTable } = inferRelations(schemaIR.tables, modelNameMap);
847
- const namedTypes = seedNamedTypeRegistry(schemaIR, typeMap, enumNameMap, reservedNamedTypeNames);
848
769
  const models = [];
849
- for (const table of Object.values(schemaIR.tables)) models.push(buildModel(table, typeMap, enumNameMap, fieldNamesByTable, namedTypes, defaultMapping, rawDefaultParser, [...relationsByTable.get(table.name) ?? [], ...extraRelationsByTable.get(table.name) ?? []], danglingForeignKeysByTable.get(table.name) ?? []));
770
+ for (const table of Object.values(schemaIR.tables)) models.push(buildModel(table, typeMap, enumNameMap, fieldNamesByTable, defaultMapping, rawDefaultParser, [...relationsByTable.get(table.name) ?? [], ...extraRelationsByTable.get(table.name) ?? []], danglingForeignKeysByTable.get(table.name) ?? []));
850
771
  const sortedModels = topologicalSort(models, schemaIR.tables, modelNameMap);
851
- const namedTypeEntries = [...namedTypes.entriesByKey.values()].sort((a, b) => a.name.localeCompare(b.name));
852
- const types = namedTypeEntries.length > 0 ? {
853
- kind: "types",
854
- declarations: namedTypeEntries.map(buildNamedTypeDeclaration),
855
- span: SYNTHETIC_SPAN
856
- } : void 0;
857
772
  return {
858
773
  kind: "document",
859
774
  sourceId: "<sql-schema-ir>",
@@ -863,7 +778,6 @@ function buildPslDocumentAst(schemaIR, options, foreignKeyExtras, namespaceName)
863
778
  entries: makePslNamespaceEntries(sortedModels, [], enumBlocks),
864
779
  span: SYNTHETIC_SPAN
865
780
  })],
866
- ...types ? { types } : {},
867
781
  span: SYNTHETIC_SPAN
868
782
  };
869
783
  }
@@ -923,7 +837,7 @@ function buildNativeEnumBlock(name, typeName, values) {
923
837
  span: SYNTHETIC_SPAN
924
838
  };
925
839
  }
926
- function buildModel(table, typeMap, enumNameMap, fieldNamesByTable, namedTypes, defaultMapping, rawDefaultParser, relationFields, danglingForeignKeys) {
840
+ function buildModel(table, typeMap, enumNameMap, fieldNamesByTable, defaultMapping, rawDefaultParser, relationFields, danglingForeignKeys) {
927
841
  const { name: modelName, map: mapName } = toModelName(table.name);
928
842
  const fieldNameMap = fieldNamesByTable.get(table.name);
929
843
  const pkColumns = new Set(table.primaryKey?.columns ?? []);
@@ -936,7 +850,7 @@ function buildModel(table, typeMap, enumNameMap, fieldNamesByTable, namedTypes,
936
850
  if (!uniqueColumns.has(columnName) || existingConstraintName === void 0 && unique.name) uniqueColumns.set(columnName, unique.name);
937
851
  }
938
852
  const fields = [];
939
- for (const column of Object.values(table.columns)) fields.push(buildScalarField(column, table, typeMap, enumNameMap, fieldNameMap, namedTypes, defaultMapping, rawDefaultParser, pkColumns, isSinglePk, singlePkConstraintName, uniqueColumns));
853
+ for (const column of Object.values(table.columns)) fields.push(buildScalarField(column, table, typeMap, enumNameMap, fieldNameMap, defaultMapping, rawDefaultParser, pkColumns, isSinglePk, singlePkConstraintName, uniqueColumns));
940
854
  const usedFieldNames = new Set(fields.map((field) => field.name));
941
855
  for (const rel of relationFields) fields.push(buildRelationField(rel, table.name, fieldNamesByTable, usedFieldNames));
942
856
  const modelAttributes = [];
@@ -948,9 +862,12 @@ function buildModel(table, typeMap, enumNameMap, fieldNamesByTable, namedTypes,
948
862
  const uniqueFieldNames = unique.columns.map((columnName) => resolveColumnFieldName(fieldNamesByTable, table.name, columnName));
949
863
  modelAttributes.push(buildModelConstraintAttribute("unique", uniqueFieldNames, unique.name));
950
864
  }
951
- for (const index of table.indexes) if (!index.unique) {
952
- const indexFieldNames = index.columns.map((columnName) => resolveColumnFieldName(fieldNamesByTable, table.name, columnName));
953
- modelAttributes.push(buildModelConstraintAttribute("index", indexFieldNames, index.name, index.type));
865
+ for (const index of table.indexes) {
866
+ if (index.columns === void 0 || index.where !== void 0) continue;
867
+ if (!index.unique) {
868
+ const indexFieldNames = index.columns.map((columnName) => resolveColumnFieldName(fieldNamesByTable, table.name, columnName));
869
+ modelAttributes.push(buildModelConstraintAttribute("index", indexFieldNames, index.name, index.type));
870
+ }
954
871
  }
955
872
  if (mapName) modelAttributes.push(buildMapAttribute("model", mapName));
956
873
  const warnings = [];
@@ -978,9 +895,9 @@ function buildDanglingForeignKeyWarning(danglingForeignKeys, fieldNamesByTable,
978
895
  const fieldNames = fk.columns.map((columnName) => resolveColumnFieldName(fieldNamesByTable, tableName, columnName));
979
896
  const target = fk.referencedSchema !== void 0 ? `${fk.referencedSchema}.${fk.referencedTable}` : fk.referencedTable;
980
897
  return `"${fieldNames.join(", ")}" -> "${target}"`;
981
- }).join(", ")} exists in the database, but its target schema is outside the introspected scope, so no relation field was generated. If the target schema is described by an extension pack, add it to extensionPacks and re-run infer.`;
898
+ }).join(", ")} exists in the database, but its target schema is outside the introspected scope, so no relation field was generated. If the target schema is described by an extension pack, add it to extensions and re-run infer.`;
982
899
  }
983
- function buildScalarField(column, table, typeMap, enumNameMap, fieldNameMap, namedTypes, defaultMapping, rawDefaultParser, pkColumns, isSinglePk, singlePkConstraintName, uniqueColumns) {
900
+ function buildScalarField(column, table, typeMap, enumNameMap, fieldNameMap, defaultMapping, rawDefaultParser, pkColumns, isSinglePk, singlePkConstraintName, uniqueColumns) {
984
901
  const resolvedField = fieldNameMap?.get(column.name);
985
902
  const fieldName = resolvedField?.fieldName ?? toFieldName(column.name).name;
986
903
  const fieldMap = resolvedField?.fieldMap;
@@ -998,8 +915,13 @@ function buildScalarField(column, table, typeMap, enumNameMap, fieldNameMap, nam
998
915
  span: SYNTHETIC_SPAN
999
916
  };
1000
917
  }
1001
- let typeName = resolution.pslType;
1002
- let typeConstructor;
918
+ let typeName = resolution.pslType.name;
919
+ let typeConstructor = resolution.pslType.args ? {
920
+ kind: "typeConstructor",
921
+ path: [resolution.pslType.name],
922
+ args: resolution.pslType.args.map(positionalArg),
923
+ span: SYNTHETIC_SPAN
924
+ } : void 0;
1003
925
  const enumPslName = enumNameMap.get(column.nativeType);
1004
926
  if (enumPslName) {
1005
927
  typeName = enumPslName;
@@ -1010,7 +932,6 @@ function buildScalarField(column, table, typeMap, enumNameMap, fieldNameMap, nam
1010
932
  span: SYNTHETIC_SPAN
1011
933
  };
1012
934
  }
1013
- if (resolution.nativeTypeAttribute && !enumPslName) typeName = resolveNamedTypeName(namedTypes, resolution);
1014
935
  const attributes = [];
1015
936
  const isId = isSinglePk && pkColumns.has(column.name);
1016
937
  if (isId) attributes.push(buildSimpleConstraintFieldAttribute("id", singlePkConstraintName));
@@ -1115,16 +1036,6 @@ function namedArg(name, value) {
1115
1036
  span: SYNTHETIC_SPAN
1116
1037
  };
1117
1038
  }
1118
- function buildNamedTypeDeclaration(entry) {
1119
- const attribute = buildAttribute("namedType", entry.nativeTypeAttribute.name, (entry.nativeTypeAttribute.args ?? []).map(positionalArg));
1120
- return {
1121
- kind: "namedType",
1122
- name: entry.name,
1123
- baseType: entry.baseType,
1124
- attributes: [attribute],
1125
- span: SYNTHETIC_SPAN
1126
- };
1127
- }
1128
1039
  function escapePslString(value) {
1129
1040
  return value.replace(/\\/g, "\\\\").replace(/"/g, "\\\"").replace(/\n/g, "\\n").replace(/\r/g, "\\r");
1130
1041
  }
@@ -1202,71 +1113,12 @@ function buildTopLevelNameMap(sources, normalize, kind, sourceKind) {
1202
1113
  normalizedToSources.set(normalized.name, [...normalizedToSources.get(normalized.name) ?? [], source]);
1203
1114
  }
1204
1115
  const duplicates = [...normalizedToSources.entries()].filter(([, conflictingSources]) => conflictingSources.length > 1);
1205
- if (duplicates.length > 0) {
1206
- const details = duplicates.map(([normalizedName, conflictingSources]) => `- ${kind} "${normalizedName}" from ${sourceKind}s ${conflictingSources.map((source) => `"${source}"`).join(", ")}`);
1207
- throw new Error(`PSL ${kind} name collisions detected:\n${details.join("\n")}`);
1208
- }
1116
+ if (duplicates.length > 0) throw postgresError("CONTRACT.NAME_DUPLICATE", `PSL ${kind} name collisions detected:\n${duplicates.map(([normalizedName, conflictingSources]) => `- ${kind} "${normalizedName}" from ${sourceKind}s ${conflictingSources.map((source) => `"${source}"`).join(", ")}`).join("\n")}`, { meta: {
1117
+ kind,
1118
+ names: duplicates.map(([normalizedName]) => normalizedName)
1119
+ } });
1209
1120
  return results;
1210
1121
  }
1211
- function createReservedNamedTypeNames(modelNames, enumNameMap) {
1212
- const reservedNames = new Set(PSL_SCALAR_TYPE_NAMES);
1213
- for (const result of modelNames.values()) reservedNames.add(result.name);
1214
- for (const enumPslName of enumNameMap.values()) reservedNames.add(enumPslName);
1215
- return reservedNames;
1216
- }
1217
- function seedNamedTypeRegistry(schemaIR, typeMap, enumNameMap, reservedNames) {
1218
- const seeds = /* @__PURE__ */ new Map();
1219
- for (const tableName of Object.keys(schemaIR.tables).sort()) {
1220
- const table = schemaIR.tables[tableName];
1221
- if (!table) continue;
1222
- for (const columnName of Object.keys(table.columns).sort()) {
1223
- const column = table.columns[columnName];
1224
- if (!column) continue;
1225
- const resolution = typeMap.resolve(column.nativeType, table.annotations);
1226
- if ("unsupported" in resolution || enumNameMap.has(column.nativeType) || !resolution.nativeTypeAttribute) continue;
1227
- const signatureKey = createNamedTypeSignatureKey(resolution);
1228
- if (!seeds.has(signatureKey)) seeds.set(signatureKey, {
1229
- baseType: resolution.pslType,
1230
- desiredName: toNamedTypeName(column.name),
1231
- nativeTypeAttribute: resolution.nativeTypeAttribute
1232
- });
1233
- }
1234
- }
1235
- const registry = {
1236
- entriesByKey: /* @__PURE__ */ new Map(),
1237
- usedNames: new Set(reservedNames)
1238
- };
1239
- const sortedSeeds = [...seeds.entries()].sort((left, right) => {
1240
- const desiredNameComparison = left[1].desiredName.localeCompare(right[1].desiredName);
1241
- if (desiredNameComparison !== 0) return desiredNameComparison;
1242
- return left[0].localeCompare(right[0]);
1243
- });
1244
- for (const [signatureKey, seed] of sortedSeeds) {
1245
- const name = createUniqueFieldName(seed.desiredName, registry.usedNames);
1246
- registry.entriesByKey.set(signatureKey, {
1247
- name,
1248
- baseType: seed.baseType,
1249
- nativeTypeAttribute: seed.nativeTypeAttribute
1250
- });
1251
- registry.usedNames.add(name);
1252
- }
1253
- return registry;
1254
- }
1255
- function resolveNamedTypeName(registry, resolution) {
1256
- const key = createNamedTypeSignatureKey(resolution);
1257
- const existing = registry.entriesByKey.get(key);
1258
- if (existing) return existing.name;
1259
- throw new Error(`Named type registry was not seeded for native type "${resolution.nativeType}"`);
1260
- }
1261
- function createNamedTypeSignatureKey(resolution) {
1262
- return JSON.stringify({
1263
- baseType: resolution.pslType,
1264
- nativeTypeAttribute: resolution.nativeTypeAttribute ? {
1265
- name: resolution.nativeTypeAttribute.name,
1266
- args: resolution.nativeTypeAttribute.args ?? null
1267
- } : null
1268
- });
1269
- }
1270
1122
  function topologicalSort(models, tables, modelNameMap) {
1271
1123
  const modelByName = /* @__PURE__ */ new Map();
1272
1124
  for (const model of models) modelByName.set(model.name, model);