@prisma-next/target-postgres 0.14.0-dev.8 → 0.14.0-dev.80

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 (266) hide show
  1. package/dist/authoring-Bi9aa8zG.mjs +574 -0
  2. package/dist/authoring-Bi9aa8zG.mjs.map +1 -0
  3. package/dist/canonicalize-CAbXtVtB.d.mts +37 -0
  4. package/dist/canonicalize-CAbXtVtB.d.mts.map +1 -0
  5. package/dist/{codec-ids-CnXu9Qy3.d.mts → codec-ids-BzrFF-I4.d.mts} +3 -2
  6. package/dist/codec-ids-BzrFF-I4.d.mts.map +1 -0
  7. package/dist/{codec-ids-BvytN2P8.mjs → codec-ids-C_-Hj6bL.mjs} +3 -2
  8. package/dist/codec-ids-C_-Hj6bL.mjs.map +1 -0
  9. package/dist/codec-ids.d.mts +2 -2
  10. package/dist/codec-ids.mjs +2 -2
  11. package/dist/{codec-types-DHCkwPKE.d.mts → codec-types-BiIWziRO.d.mts} +3 -2
  12. package/dist/codec-types-BiIWziRO.d.mts.map +1 -0
  13. package/dist/codec-types.d.mts +1 -1
  14. package/dist/codecs-BemGS69L.mjs +927 -0
  15. package/dist/codecs-BemGS69L.mjs.map +1 -0
  16. package/dist/{codecs--0A5_4Bq.d.mts → codecs-Cm-nS4mQ.d.mts} +111 -4
  17. package/dist/codecs-Cm-nS4mQ.d.mts.map +1 -0
  18. package/dist/codecs.d.mts +2 -2
  19. package/dist/codecs.mjs +3 -757
  20. package/dist/codecs.mjs.map +1 -1
  21. package/dist/contract-free.d.mts +33 -3
  22. package/dist/contract-free.d.mts.map +1 -1
  23. package/dist/contract-free.mjs +23 -7
  24. package/dist/contract-free.mjs.map +1 -1
  25. package/dist/control-policy-5LE2kwfb.mjs +1285 -0
  26. package/dist/control-policy-5LE2kwfb.mjs.map +1 -0
  27. package/dist/control.d.mts +2 -2
  28. package/dist/control.d.mts.map +1 -1
  29. package/dist/control.mjs +866 -36
  30. package/dist/control.mjs.map +1 -1
  31. package/dist/data-transform-BOWpliq8.mjs.map +1 -1
  32. package/dist/{data-transform-DDgWdB5o.d.mts → data-transform-DPzwBMhW.d.mts} +3 -3
  33. package/dist/{data-transform-DDgWdB5o.d.mts.map → data-transform-DPzwBMhW.d.mts.map} +1 -1
  34. package/dist/data-transform.d.mts +1 -1
  35. package/dist/{ddl-QDyOSeLc.mjs → ddl-BQzITB_e.mjs} +128 -6
  36. package/dist/ddl-BQzITB_e.mjs.map +1 -0
  37. package/dist/ddl.d.mts +3 -2
  38. package/dist/ddl.mjs +2 -2
  39. package/dist/default-normalizer.d.mts +1 -1
  40. package/dist/default-normalizer.d.mts.map +1 -1
  41. package/dist/default-normalizer.mjs +247 -1
  42. package/dist/default-normalizer.mjs.map +1 -0
  43. package/dist/descriptor-meta-DzL6vXAz.mjs +22 -0
  44. package/dist/descriptor-meta-DzL6vXAz.mjs.map +1 -0
  45. package/dist/descriptor-meta-runtime-UwYdV8fD.mjs +12 -0
  46. package/dist/descriptor-meta-runtime-UwYdV8fD.mjs.map +1 -0
  47. package/dist/diff-database-schema-DULhiCNM.mjs +305 -0
  48. package/dist/diff-database-schema-DULhiCNM.mjs.map +1 -0
  49. package/dist/diff-database-schema.d.mts +46 -0
  50. package/dist/diff-database-schema.d.mts.map +1 -0
  51. package/dist/diff-database-schema.mjs +2 -0
  52. package/dist/errors-DO-w0EUS.mjs +35 -0
  53. package/dist/errors-DO-w0EUS.mjs.map +1 -0
  54. package/dist/errors.d.mts +11 -9
  55. package/dist/errors.d.mts.map +1 -1
  56. package/dist/errors.mjs +1 -1
  57. package/dist/issue-planner.d.mts +35 -11
  58. package/dist/issue-planner.d.mts.map +1 -1
  59. package/dist/issue-planner.mjs +2 -2
  60. package/dist/migration.d.mts +5 -5
  61. package/dist/migration.d.mts.map +1 -1
  62. package/dist/migration.mjs +4 -4
  63. package/dist/namespace-ids-xtp_ZY86.mjs +7 -0
  64. package/dist/namespace-ids-xtp_ZY86.mjs.map +1 -0
  65. package/dist/native-type-normalizer.mjs +37 -1
  66. package/dist/native-type-normalizer.mjs.map +1 -0
  67. package/dist/nodes-Bc2N3tMy.d.mts +170 -0
  68. package/dist/nodes-Bc2N3tMy.d.mts.map +1 -0
  69. package/dist/nodes-DJC5B8Id.mjs +192 -0
  70. package/dist/nodes-DJC5B8Id.mjs.map +1 -0
  71. package/dist/{op-factory-call-DmQEc3XV.d.mts → op-factory-call-Bn9zDx4_.d.mts} +114 -15
  72. package/dist/op-factory-call-Bn9zDx4_.d.mts.map +1 -0
  73. package/dist/{op-factory-call-D_p5vxwt.mjs → op-factory-call-D6yu-PlS.mjs} +469 -32
  74. package/dist/op-factory-call-D6yu-PlS.mjs.map +1 -0
  75. package/dist/op-factory-call.d.mts +2 -2
  76. package/dist/op-factory-call.mjs +2 -2
  77. package/dist/pack.d.mts +300 -3
  78. package/dist/pack.d.mts.map +1 -1
  79. package/dist/pack.mjs +1 -1
  80. package/dist/planner-DwDxdYxi.mjs +390 -0
  81. package/dist/planner-DwDxdYxi.mjs.map +1 -0
  82. package/dist/{planner-ddl-builders-B2wOwLqI.mjs → planner-ddl-builders-BKlmGDJl.mjs} +17 -28
  83. package/dist/planner-ddl-builders-BKlmGDJl.mjs.map +1 -0
  84. package/dist/planner-ddl-builders.d.mts +4 -6
  85. package/dist/planner-ddl-builders.d.mts.map +1 -1
  86. package/dist/planner-ddl-builders.mjs +2 -2
  87. package/dist/planner-identity-values-CJPha2Sz.mjs.map +1 -1
  88. package/dist/planner-identity-values.d.mts +8 -2
  89. package/dist/planner-identity-values.d.mts.map +1 -1
  90. package/dist/planner-identity-values.mjs +2 -2
  91. package/dist/{planner-produced-postgres-migration-QqHa2C2l.d.mts → planner-produced-postgres-migration-BEY1T0Xm.d.mts} +4 -4
  92. package/dist/{planner-produced-postgres-migration-QqHa2C2l.d.mts.map → planner-produced-postgres-migration-BEY1T0Xm.d.mts.map} +1 -1
  93. package/dist/{planner-produced-postgres-migration-Cji5vxUf.mjs → planner-produced-postgres-migration-BGPpq56_.mjs} +3 -3
  94. package/dist/{planner-produced-postgres-migration-Cji5vxUf.mjs.map → planner-produced-postgres-migration-BGPpq56_.mjs.map} +1 -1
  95. package/dist/planner-produced-postgres-migration.d.mts +1 -1
  96. package/dist/planner-produced-postgres-migration.mjs +1 -1
  97. package/dist/{planner-sql-checks-jqUUGyQR.mjs → planner-sql-checks-CDXn2uaS.mjs} +5 -5
  98. package/dist/{planner-sql-checks-jqUUGyQR.mjs.map → planner-sql-checks-CDXn2uaS.mjs.map} +1 -1
  99. package/dist/planner-sql-checks.d.mts +2 -2
  100. package/dist/planner-sql-checks.d.mts.map +1 -1
  101. package/dist/planner-sql-checks.mjs +1 -1
  102. package/dist/{planner-target-details-CIY6tLeo.d.mts → planner-target-details-HkP4RrRO.d.mts} +2 -2
  103. package/dist/planner-target-details-HkP4RrRO.d.mts.map +1 -0
  104. package/dist/planner-target-details.d.mts +1 -1
  105. package/dist/planner.d.mts +72 -17
  106. package/dist/planner.d.mts.map +1 -1
  107. package/dist/planner.mjs +3 -2
  108. package/dist/{postgres-contract-serializer-k3TAcPMY.mjs → postgres-contract-view-C3LPCLP_.mjs} +45 -41
  109. package/dist/postgres-contract-view-C3LPCLP_.mjs.map +1 -0
  110. package/dist/postgres-contract-view-C9j5cqP0.d.mts +34 -0
  111. package/dist/postgres-contract-view-C9j5cqP0.d.mts.map +1 -0
  112. package/dist/postgres-database-schema-node-CJv5bfbs.d.mts +262 -0
  113. package/dist/postgres-database-schema-node-CJv5bfbs.d.mts.map +1 -0
  114. package/dist/{postgres-migration-Y4YBJqkS.d.mts → postgres-migration-DbckK1su.d.mts} +58 -9
  115. package/dist/postgres-migration-DbckK1su.d.mts.map +1 -0
  116. package/dist/{postgres-migration-B5jKrXv3.mjs → postgres-migration-et1td40f.mjs} +78 -45
  117. package/dist/postgres-migration-et1td40f.mjs.map +1 -0
  118. package/dist/postgres-native-enum-BwVNf0Ku.mjs +45 -0
  119. package/dist/postgres-native-enum-BwVNf0Ku.mjs.map +1 -0
  120. package/dist/postgres-rls-policy--CSddvMR.d.mts +52 -0
  121. package/dist/postgres-rls-policy--CSddvMR.d.mts.map +1 -0
  122. package/dist/postgres-role-schema-node-Cm-j0ZGM.mjs +190 -0
  123. package/dist/postgres-role-schema-node-Cm-j0ZGM.mjs.map +1 -0
  124. package/dist/postgres-schema-DKq6s1P3.d.mts +269 -0
  125. package/dist/postgres-schema-DKq6s1P3.d.mts.map +1 -0
  126. package/dist/{postgres-schema-COGZ1ark.mjs → postgres-schema-DYATroNx.mjs} +200 -11
  127. package/dist/postgres-schema-DYATroNx.mjs.map +1 -0
  128. package/dist/postgres-table-schema-node-D6LvInCe.mjs +216 -0
  129. package/dist/postgres-table-schema-node-D6LvInCe.mjs.map +1 -0
  130. package/dist/render-ops.d.mts +1 -1
  131. package/dist/{render-typescript-KMgosran.mjs → render-typescript-7yqILcwr.mjs} +45 -16
  132. package/dist/render-typescript-7yqILcwr.mjs.map +1 -0
  133. package/dist/render-typescript.mjs +1 -1
  134. package/dist/resolve-ddl-schema-DpGte50u.mjs +22 -0
  135. package/dist/resolve-ddl-schema-DpGte50u.mjs.map +1 -0
  136. package/dist/rls-canonicalize.d.mts +20 -0
  137. package/dist/rls-canonicalize.d.mts.map +1 -0
  138. package/dist/rls-canonicalize.mjs +2 -0
  139. package/dist/runtime.d.mts +6 -8
  140. package/dist/runtime.d.mts.map +1 -1
  141. package/dist/runtime.mjs +3 -3
  142. package/dist/runtime.mjs.map +1 -1
  143. package/dist/schema-ir-annotations.d.mts +15 -0
  144. package/dist/schema-ir-annotations.d.mts.map +1 -0
  145. package/dist/schema-ir-annotations.mjs +2 -0
  146. package/dist/{sql-utils-DcfMz4MQ.mjs → sql-utils-SU4FDvIV.mjs} +12 -3
  147. package/dist/{sql-utils-DcfMz4MQ.mjs.map → sql-utils-SU4FDvIV.mjs.map} +1 -1
  148. package/dist/sql-utils.d.mts +8 -1
  149. package/dist/sql-utils.d.mts.map +1 -1
  150. package/dist/sql-utils.mjs +2 -2
  151. package/dist/types.d.mts +4 -140
  152. package/dist/types.mjs +5 -2
  153. package/dist/wire-name-rHHtr1h0.mjs +75 -0
  154. package/dist/wire-name-rHHtr1h0.mjs.map +1 -0
  155. package/package.json +26 -20
  156. package/src/contract-free/checks.ts +153 -0
  157. package/src/contract-free/control-bootstrap.ts +20 -3
  158. package/src/contract-free/ddl.ts +98 -1
  159. package/src/core/authoring.ts +509 -2
  160. package/src/core/codec-helpers.ts +33 -7
  161. package/src/core/codec-ids.ts +1 -0
  162. package/src/core/codec-type-map.ts +2 -0
  163. package/src/core/codecs.ts +193 -12
  164. package/src/core/ddl/nodes.ts +185 -2
  165. package/src/core/default-normalizer.ts +122 -2
  166. package/src/core/descriptor-meta.ts +10 -1
  167. package/src/core/entity-kinds.ts +41 -0
  168. package/src/core/errors.ts +14 -12
  169. package/src/core/migrations/bound-schema.ts +10 -0
  170. package/src/core/migrations/column-ddl-rendering.ts +117 -0
  171. package/src/core/migrations/contract-to-postgres-database-schema-node.ts +189 -0
  172. package/src/core/migrations/control-policy.ts +217 -65
  173. package/src/core/migrations/diff-database-schema.ts +252 -0
  174. package/src/core/migrations/issue-planner.ts +817 -656
  175. package/src/core/migrations/op-factory-call.ts +383 -23
  176. package/src/core/migrations/operations/columns.ts +24 -12
  177. package/src/core/migrations/operations/constraints.ts +1 -1
  178. package/src/core/migrations/operations/data-transform.ts +6 -0
  179. package/src/core/migrations/operations/dependencies.ts +2 -1
  180. package/src/core/migrations/operations/native-enum-types.ts +62 -0
  181. package/src/core/migrations/operations/rls.ts +187 -0
  182. package/src/core/migrations/operations/shared.ts +0 -30
  183. package/src/core/migrations/planner-ddl-builders.ts +31 -59
  184. package/src/core/migrations/planner-identity-values.ts +1 -1
  185. package/src/core/migrations/planner-recipes.ts +31 -13
  186. package/src/core/migrations/planner-sql-checks.ts +1 -1
  187. package/src/core/migrations/planner-strategies.ts +248 -216
  188. package/src/core/migrations/planner-target-details.ts +3 -1
  189. package/src/core/migrations/planner.ts +357 -67
  190. package/src/core/migrations/postgres-migration.ts +118 -86
  191. package/src/core/migrations/render-typescript.ts +42 -15
  192. package/src/core/migrations/resolve-ddl-schema.ts +26 -0
  193. package/src/core/migrations/runner.ts +11 -16
  194. package/src/core/migrations/verify-postgres-namespaces.ts +46 -31
  195. package/src/core/namespace-ids.ts +3 -0
  196. package/src/core/postgres-contract-serializer.ts +46 -71
  197. package/src/core/postgres-contract-view.ts +48 -0
  198. package/src/core/postgres-native-enum.ts +61 -0
  199. package/src/core/postgres-rls-enablement.ts +34 -0
  200. package/src/core/postgres-rls-policy.ts +64 -0
  201. package/src/core/postgres-role.ts +59 -0
  202. package/src/core/postgres-schema-verifier.ts +10 -7
  203. package/src/core/postgres-schema.ts +85 -15
  204. package/src/core/postgres-validators.ts +34 -0
  205. package/src/core/psl-infer/infer-psl-contract.ts +1273 -0
  206. package/src/core/psl-infer/postgres-default-mapping.ts +16 -0
  207. package/src/core/psl-infer/postgres-type-map.ts +128 -0
  208. package/src/core/rls/canonicalize.ts +48 -0
  209. package/src/core/rls/wire-name.ts +33 -0
  210. package/src/core/schema-ir/node-storage-coordinate.ts +26 -0
  211. package/src/core/schema-ir/postgres-database-schema-node.ts +61 -0
  212. package/src/core/schema-ir/postgres-namespace-schema-node.ts +70 -0
  213. package/src/core/schema-ir/postgres-native-enum-schema-node.ts +83 -0
  214. package/src/core/schema-ir/postgres-policy-schema-node.ts +90 -0
  215. package/src/core/schema-ir/postgres-role-schema-node.ts +65 -0
  216. package/src/core/schema-ir/postgres-table-schema-node.ts +140 -0
  217. package/src/core/schema-ir/schema-node-kinds.ts +122 -0
  218. package/src/core/sql-utils.ts +15 -2
  219. package/src/exports/codecs.ts +3 -0
  220. package/src/exports/contract-free.ts +11 -1
  221. package/src/exports/control.ts +27 -33
  222. package/src/exports/ddl.ts +8 -0
  223. package/src/exports/diff-database-schema.ts +7 -0
  224. package/src/exports/issue-planner.ts +1 -1
  225. package/src/exports/migration.ts +1 -0
  226. package/src/exports/op-factory-call.ts +4 -0
  227. package/src/exports/planner-ddl-builders.ts +0 -2
  228. package/src/exports/planner-identity-values.ts +4 -1
  229. package/src/exports/planner.ts +1 -0
  230. package/src/exports/rls-canonicalize.ts +7 -0
  231. package/src/exports/runtime.ts +1 -0
  232. package/src/exports/schema-ir-annotations.ts +1 -0
  233. package/src/exports/sql-utils.ts +1 -0
  234. package/src/exports/types.ts +37 -0
  235. package/dist/codec-ids-BvytN2P8.mjs.map +0 -1
  236. package/dist/codec-ids-CnXu9Qy3.d.mts.map +0 -1
  237. package/dist/codec-types-DHCkwPKE.d.mts.map +0 -1
  238. package/dist/codecs--0A5_4Bq.d.mts.map +0 -1
  239. package/dist/ddl-QDyOSeLc.mjs.map +0 -1
  240. package/dist/default-normalizer-DyyCHQWs.mjs +0 -131
  241. package/dist/default-normalizer-DyyCHQWs.mjs.map +0 -1
  242. package/dist/descriptor-meta-CpGygXpI.mjs +0 -140
  243. package/dist/descriptor-meta-CpGygXpI.mjs.map +0 -1
  244. package/dist/errors-CUk87ByX.mjs +0 -33
  245. package/dist/errors-CUk87ByX.mjs.map +0 -1
  246. package/dist/issue-planner-DL6g3CmE.mjs +0 -824
  247. package/dist/issue-planner-DL6g3CmE.mjs.map +0 -1
  248. package/dist/native-type-normalizer-Bc9XJzWC.mjs +0 -38
  249. package/dist/native-type-normalizer-Bc9XJzWC.mjs.map +0 -1
  250. package/dist/nodes-Bbhs2rwj.mjs +0 -76
  251. package/dist/nodes-Bbhs2rwj.mjs.map +0 -1
  252. package/dist/nodes-pLeLgdis.d.mts +0 -67
  253. package/dist/nodes-pLeLgdis.d.mts.map +0 -1
  254. package/dist/op-factory-call-D_p5vxwt.mjs.map +0 -1
  255. package/dist/op-factory-call-DmQEc3XV.d.mts.map +0 -1
  256. package/dist/planner-Bs_baQax.mjs +0 -324
  257. package/dist/planner-Bs_baQax.mjs.map +0 -1
  258. package/dist/planner-ddl-builders-B2wOwLqI.mjs.map +0 -1
  259. package/dist/planner-target-details-CIY6tLeo.d.mts.map +0 -1
  260. package/dist/postgres-contract-serializer-k3TAcPMY.mjs.map +0 -1
  261. package/dist/postgres-migration-B5jKrXv3.mjs.map +0 -1
  262. package/dist/postgres-migration-Y4YBJqkS.d.mts.map +0 -1
  263. package/dist/postgres-schema-COGZ1ark.mjs.map +0 -1
  264. package/dist/render-typescript-KMgosran.mjs.map +0 -1
  265. package/dist/types.d.mts.map +0 -1
  266. package/src/core/postgres-enum-type-schema.ts +0 -17
@@ -4,7 +4,7 @@
4
4
  * Each codec ships as three artifacts:
5
5
  *
6
6
  * 1. A `PgXCodec` class extending {@link CodecImpl} that wraps the module-level encode/decode/encodeJson/decodeJson constants exported from `codec-helpers.ts` (the single source of truth for non-trivial runtime conversions; trivial identity passthroughs are inlined). 2. A `PgXDescriptor` class extending {@link CodecDescriptorImpl} declaring the codec id, traits, target types, params schema, meta, and (where applicable)
7
- * the emit-path `renderOutputType`. 3. A per-codec column helper (`pgXColumn`) that calls `descriptor.factory(...)` directly and packages the result into a {@link ColumnSpec} via the framework {@link column} packager. The helper is tied to its descriptor with `satisfies ColumnHelperFor` (and `ColumnHelperForStrict` where the resolved codec type is well-defined).
7
+ * the emit-path `renderOutputType`. 3. A per-codec column helper (`pgXColumn`) that calls `descriptor.factory(...)` directly and packages the result into a framework `ColumnSpec` via the framework {@link column} packager. The helper is tied to its descriptor with `satisfies ColumnHelperFor` (and `ColumnHelperForStrict` where the resolved codec type is well-defined).
8
8
  *
9
9
  * After TML-2357 this is the canonical source of Postgres codec metadata and runtime behaviour — the legacy `mkCodec` / `defineCodec` carriers (and the parallel `byScalar`/`codecDescriptorDefinitions`/ `codecDescriptorList` collection exports) retired with the deletion sweep.
10
10
  *
@@ -19,11 +19,14 @@ import {
19
19
  CodecDescriptorImpl,
20
20
  CodecImpl,
21
21
  type CodecInstanceContext,
22
+ type CodecMeta,
22
23
  type ColumnHelperFor,
23
24
  type ColumnHelperForStrict,
24
25
  column,
26
+ renderTsLiteral,
25
27
  voidParamsSchema,
26
28
  } from '@prisma-next/framework-components/codec';
29
+ import { UNBOUND_NAMESPACE_ID } from '@prisma-next/framework-components/ir';
27
30
  import {
28
31
  SqlCharCodec,
29
32
  SqlFloatCodec,
@@ -40,6 +43,8 @@ import { blindCast } from '@prisma-next/utils/casts';
40
43
  import type { StandardSchemaV1 } from '@standard-schema/spec';
41
44
  import { type as arktype } from 'arktype';
42
45
  import {
46
+ pgByteaDecodeJson,
47
+ pgByteaEncodeJson,
43
48
  pgIntervalDecode,
44
49
  pgJsonbDecode,
45
50
  pgJsonbEncode,
@@ -59,6 +64,7 @@ import {
59
64
  PG_BOOL_CODEC_ID,
60
65
  PG_BYTEA_CODEC_ID,
61
66
  PG_CHAR_CODEC_ID,
67
+ PG_ENUM_CODEC_ID,
62
68
  PG_FLOAT_CODEC_ID,
63
69
  PG_FLOAT4_CODEC_ID,
64
70
  PG_FLOAT8_CODEC_ID,
@@ -80,6 +86,8 @@ import {
80
86
  PG_VARBIT_CODEC_ID,
81
87
  PG_VARCHAR_CODEC_ID,
82
88
  } from './codec-ids';
89
+ import { DEFAULT_NAMESPACE_ID } from './namespace-ids';
90
+ import { PostgresNativeEnum } from './postgres-native-enum';
83
91
 
84
92
  type LengthParams = { readonly length?: number };
85
93
  type PrecisionParams = { readonly precision?: number };
@@ -148,6 +156,9 @@ export class PgTextDescriptor extends CodecDescriptorImpl<void> {
148
156
  override readonly targetTypes = ['text'] as const;
149
157
  override readonly meta = PG_TEXT_META;
150
158
  override readonly paramsSchema: StandardSchemaV1<void> = voidParamsSchema;
159
+ override renderValueLiteral(value: JsonValue): string | undefined {
160
+ return renderTsLiteral(value);
161
+ }
151
162
  override factory(): (ctx: CodecInstanceContext) => PgTextCodec {
152
163
  return () => new PgTextCodec(this);
153
164
  }
@@ -161,6 +172,145 @@ export const pgTextColumn = () =>
161
172
  pgTextColumn satisfies ColumnHelperFor<PgTextDescriptor>;
162
173
  pgTextColumn satisfies ColumnHelperForStrict<PgTextDescriptor>;
163
174
 
175
+ /**
176
+ * Codec for a `pg.enum(Ref)` column bound to a native Postgres enum type.
177
+ * Text passthrough, identical to `pg/text@1` — encode/decode do not carry the
178
+ * enum's member values; membership is enforced by the native type itself, not
179
+ * by this codec. `renderValueLiteral` renders a member value as its TS
180
+ * literal, which is what drives the column's typed value-union (via
181
+ * `renderValueSetType` reading the column's `valueSet` ref) — the codec
182
+ * itself carries no params of its own; typing comes entirely from the
183
+ * column's value-set, not from `pg/enum@1`.
184
+ *
185
+ * A distinct codec id (rather than reusing `pg/text@1` on a plain text
186
+ * column) keeps native-enum columns independently identifiable — from a
187
+ * column's `codecId` alone, without also inspecting `nativeType` — which
188
+ * the managed (DDL) phase needs to target `CREATE TYPE`/`ALTER TYPE`
189
+ * operations at exactly the columns that use one.
190
+ */
191
+ export class PgEnumCodec extends CodecImpl<
192
+ typeof PG_ENUM_CODEC_ID,
193
+ readonly ['equality', 'order', 'textual'],
194
+ string,
195
+ string
196
+ > {
197
+ async encode(value: string, _ctx: CodecCallContext): Promise<string> {
198
+ return value;
199
+ }
200
+ async decode(wire: string, _ctx: CodecCallContext): Promise<string> {
201
+ return wire;
202
+ }
203
+ encodeJson(value: string): JsonValue {
204
+ return value;
205
+ }
206
+ decodeJson(json: JsonValue): string {
207
+ return blindCast<
208
+ string,
209
+ 'text codec: a native-enum member value is stored as its wire string form'
210
+ >(json);
211
+ }
212
+ }
213
+
214
+ type PgEnumParams = { readonly typeName: string };
215
+
216
+ const pgEnumParamsSchema = arktype({
217
+ typeName: 'string',
218
+ }) satisfies StandardSchemaV1<PgEnumParams>;
219
+
220
+ function isJsonObject(
221
+ value: JsonValue | undefined,
222
+ ): value is { readonly [key: string]: JsonValue } {
223
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
224
+ }
225
+
226
+ export class PgEnumDescriptor extends CodecDescriptorImpl<PgEnumParams> {
227
+ override readonly codecId = PG_ENUM_CODEC_ID;
228
+ override readonly traits = ['equality', 'order', 'textual'] as const;
229
+ override readonly targetTypes = ['text'] as const;
230
+ override readonly meta = PG_TEXT_META;
231
+ override readonly paramsSchema = pgEnumParamsSchema satisfies StandardSchemaV1<PgEnumParams>;
232
+ override renderValueLiteral(value: JsonValue): string | undefined {
233
+ return renderTsLiteral(value);
234
+ }
235
+ override metaFor(typeParams: JsonValue | undefined): CodecMeta | undefined {
236
+ if (!isJsonObject(typeParams)) return this.meta;
237
+ const typeName = typeParams['typeName'];
238
+ return typeof typeName === 'string'
239
+ ? { db: { sql: { postgres: { nativeType: typeName } } } }
240
+ : this.meta;
241
+ }
242
+ override factory(_params: PgEnumParams): (ctx: CodecInstanceContext) => PgEnumCodec {
243
+ return () => new PgEnumCodec(this);
244
+ }
245
+
246
+ /**
247
+ * Authoring-time hook a `pg.enum(<ref>)` type constructor calls once it has
248
+ * resolved its ref argument to the referenced `native_enum` entity:
249
+ * produces this codec's per-column `typeParams` and native type from the
250
+ * entity's bare type name. Schema-qualification (`auth.aal_level` for a
251
+ * named non-default schema) is not this hook's concern — the field's
252
+ * namespace isn't known at this call site for every authoring path (the TS
253
+ * builder resolves a column before it knows its model's namespace), so it is
254
+ * applied later, at contract construction, by {@link qualifyNativeType} via
255
+ * the target's `authoring.qualifyColumnType` hook. `nativeType` mirrors
256
+ * `typeParams.typeName` — the same value {@link metaFor} derives at render
257
+ * time — so the column's declared native type and the render-time cast
258
+ * agree. Returns `undefined` if `entity` is not a `PostgresNativeEnum` (a
259
+ * contributor bug, not a user-schema error — the caller decides how to
260
+ * report it).
261
+ */
262
+ columnFromEntity(
263
+ entity: object,
264
+ ): { readonly typeParams: PgEnumParams; readonly nativeType: string } | undefined {
265
+ if (!PostgresNativeEnum.is(entity)) return undefined;
266
+ return { typeParams: { typeName: entity.typeName }, nativeType: entity.typeName };
267
+ }
268
+
269
+ /**
270
+ * Schema-qualifies this native enum type's name for the namespace the
271
+ * consuming column lives in: `${namespaceId}.${typeName}` for a named
272
+ * non-default schema, bare for the target's default schema (`public`) or
273
+ * the late-bound unbound sentinel (whose schema `search_path` resolves at
274
+ * runtime). Postgres's `format_type()` reports the bare name for a
275
+ * public-schema type, so a public column's declared native type must stay
276
+ * bare to match. Owned here because the codec owns its native type.
277
+ */
278
+ qualifyNativeType(typeName: string, namespaceId: string): string {
279
+ return namespaceId === DEFAULT_NAMESPACE_ID || namespaceId === UNBOUND_NAMESPACE_ID
280
+ ? typeName
281
+ : `${namespaceId}.${typeName}`;
282
+ }
283
+ }
284
+
285
+ export const pgEnumDescriptor = new PgEnumDescriptor();
286
+
287
+ /**
288
+ * Contract-construction-time column-type qualifier the Postgres target
289
+ * contributes through `authoring.qualifyColumnType`.
290
+ * `buildSqlContractFromDefinition` calls this for every column as it is
291
+ * constructed, passing the column's bare type info and its owning
292
+ * `namespaceId`; a native-enum column (`pg/enum@1`) gets its type name
293
+ * schema-qualified for that namespace (via
294
+ * {@link PgEnumDescriptor.qualifyNativeType}), keeping `nativeType` and
295
+ * `typeParams.typeName` in sync. Every other codec passes through unchanged.
296
+ * Both the PSL `pg.enum(Ref)` path and the TS `pg.enum(handle)` path route
297
+ * through here — the dispatch keys off the codec id, not authoring surface.
298
+ */
299
+ export function postgresQualifyColumnType(
300
+ input: {
301
+ readonly codecId: string;
302
+ readonly nativeType: string;
303
+ readonly typeParams?: Record<string, unknown>;
304
+ },
305
+ namespaceId: string,
306
+ ): { readonly nativeType: string; readonly typeParams?: Record<string, unknown> } {
307
+ if (input.codecId !== PG_ENUM_CODEC_ID) return input;
308
+ const bareTypeName = input.typeParams?.['typeName'];
309
+ if (typeof bareTypeName !== 'string') return input;
310
+ const qualified = pgEnumDescriptor.qualifyNativeType(bareTypeName, namespaceId);
311
+ return { nativeType: qualified, typeParams: { ...input.typeParams, typeName: qualified } };
312
+ }
313
+
164
314
  /**
165
315
  * Postgres `text[]` codec. Encode is an identity pass-through: the pg wire
166
316
  * driver serialises a JS `string[]` to a Postgres array literal under the
@@ -228,6 +378,9 @@ export class PgInt4Descriptor extends CodecDescriptorImpl<void> {
228
378
  override readonly targetTypes = ['int4'] as const;
229
379
  override readonly meta = PG_INT4_META;
230
380
  override readonly paramsSchema: StandardSchemaV1<void> = voidParamsSchema;
381
+ override renderValueLiteral(value: JsonValue): string | undefined {
382
+ return renderTsLiteral(value);
383
+ }
231
384
  override factory(): (ctx: CodecInstanceContext) => PgInt4Codec {
232
385
  return () => new PgInt4Codec(this);
233
386
  }
@@ -267,6 +420,9 @@ export class PgInt2Descriptor extends CodecDescriptorImpl<void> {
267
420
  override readonly targetTypes = ['int2'] as const;
268
421
  override readonly meta = PG_INT2_META;
269
422
  override readonly paramsSchema: StandardSchemaV1<void> = voidParamsSchema;
423
+ override renderValueLiteral(value: JsonValue): string | undefined {
424
+ return renderTsLiteral(value);
425
+ }
270
426
  override factory(): (ctx: CodecInstanceContext) => PgInt2Codec {
271
427
  return () => new PgInt2Codec(this);
272
428
  }
@@ -306,6 +462,9 @@ export class PgInt8Descriptor extends CodecDescriptorImpl<void> {
306
462
  override readonly targetTypes = ['int8'] as const;
307
463
  override readonly meta = PG_INT8_META;
308
464
  override readonly paramsSchema: StandardSchemaV1<void> = voidParamsSchema;
465
+ override renderValueLiteral(value: JsonValue): string | undefined {
466
+ return renderTsLiteral(value);
467
+ }
309
468
  override factory(): (ctx: CodecInstanceContext) => PgInt8Codec {
310
469
  return () => new PgInt8Codec(this);
311
470
  }
@@ -345,6 +504,9 @@ export class PgFloat4Descriptor extends CodecDescriptorImpl<void> {
345
504
  override readonly targetTypes = ['float4'] as const;
346
505
  override readonly meta = PG_FLOAT4_META;
347
506
  override readonly paramsSchema: StandardSchemaV1<void> = voidParamsSchema;
507
+ override renderValueLiteral(value: JsonValue): string | undefined {
508
+ return renderTsLiteral(value);
509
+ }
348
510
  override factory(): (ctx: CodecInstanceContext) => PgFloat4Codec {
349
511
  return () => new PgFloat4Codec(this);
350
512
  }
@@ -384,6 +546,9 @@ export class PgFloat8Descriptor extends CodecDescriptorImpl<void> {
384
546
  override readonly targetTypes = ['float8'] as const;
385
547
  override readonly meta = PG_FLOAT8_META;
386
548
  override readonly paramsSchema: StandardSchemaV1<void> = voidParamsSchema;
549
+ override renderValueLiteral(value: JsonValue): string | undefined {
550
+ return renderTsLiteral(value);
551
+ }
387
552
  override factory(): (ctx: CodecInstanceContext) => PgFloat8Codec {
388
553
  return () => new PgFloat8Codec(this);
389
554
  }
@@ -423,6 +588,9 @@ export class PgBoolDescriptor extends CodecDescriptorImpl<void> {
423
588
  override readonly targetTypes = ['bool'] as const;
424
589
  override readonly meta = PG_BOOL_META;
425
590
  override readonly paramsSchema: StandardSchemaV1<void> = voidParamsSchema;
591
+ override renderValueLiteral(value: JsonValue): string | undefined {
592
+ return renderTsLiteral(value);
593
+ }
426
594
  override factory(): (ctx: CodecInstanceContext) => PgBoolCodec {
427
595
  return () => new PgBoolCodec(this);
428
596
  }
@@ -449,10 +617,17 @@ export class PgNumericCodec extends CodecImpl<
449
617
  return pgNumericDecode(wire);
450
618
  }
451
619
  encodeJson(value: string): JsonValue {
452
- return value;
620
+ const number = Number(value);
621
+ if (!Number.isFinite(number)) {
622
+ throw new TypeError('pg/numeric@1 database JSON value must be a finite number');
623
+ }
624
+ return number;
453
625
  }
454
626
  decodeJson(json: JsonValue): string {
455
- return json as string;
627
+ if (typeof json !== 'number') {
628
+ throw new TypeError('pg/numeric@1 database JSON value must be a number');
629
+ }
630
+ return pgNumericDecode(json);
456
631
  }
457
632
  }
458
633
 
@@ -755,17 +930,10 @@ export class PgByteaCodec extends CodecImpl<
755
930
  : new Uint8Array(wire.buffer, wire.byteOffset, wire.byteLength);
756
931
  }
757
932
  encodeJson(value: Uint8Array): JsonValue {
758
- return Buffer.from(value).toString('base64');
933
+ return pgByteaEncodeJson(value);
759
934
  }
760
935
  decodeJson(json: JsonValue): Uint8Array {
761
- if (typeof json !== 'string') {
762
- throw new Error(`Expected base64 string for pg/bytea@1, got ${typeof json}`);
763
- }
764
- const decoded = Buffer.from(json, 'base64');
765
- if (decoded.toString('base64') !== json) {
766
- throw new Error(`Invalid base64 string for pg/bytea@1 (length: ${json.length})`);
767
- }
768
- return new Uint8Array(decoded);
936
+ return pgByteaDecodeJson(json);
769
937
  }
770
938
  }
771
939
 
@@ -968,6 +1136,9 @@ export class PgCharDescriptor extends CodecDescriptorImpl<LengthParams> {
968
1136
  override renderOutputType(params: LengthParams): string | undefined {
969
1137
  return sqlCharDescriptor.renderOutputType(params);
970
1138
  }
1139
+ override renderValueLiteral(value: JsonValue): string | undefined {
1140
+ return renderTsLiteral(value);
1141
+ }
971
1142
  override factory(_params: LengthParams): (ctx: CodecInstanceContext) => SqlCharCodec {
972
1143
  return () => new SqlCharCodec(this);
973
1144
  }
@@ -989,6 +1160,9 @@ export class PgVarcharDescriptor extends CodecDescriptorImpl<LengthParams> {
989
1160
  override renderOutputType(params: LengthParams): string | undefined {
990
1161
  return sqlVarcharDescriptor.renderOutputType(params);
991
1162
  }
1163
+ override renderValueLiteral(value: JsonValue): string | undefined {
1164
+ return renderTsLiteral(value);
1165
+ }
992
1166
  override factory(_params: LengthParams): (ctx: CodecInstanceContext) => SqlVarcharCodec {
993
1167
  return () => new SqlVarcharCodec(this);
994
1168
  }
@@ -1012,6 +1186,9 @@ export class PgIntDescriptor extends CodecDescriptorImpl<void> {
1012
1186
  override readonly meta = PG_INT_META;
1013
1187
  override readonly traits = sqlIntDescriptor.traits;
1014
1188
  override readonly paramsSchema = sqlIntDescriptor.paramsSchema;
1189
+ override renderValueLiteral(value: JsonValue): string | undefined {
1190
+ return renderTsLiteral(value);
1191
+ }
1015
1192
  override factory(): (ctx: CodecInstanceContext) => SqlIntCodec {
1016
1193
  return () => new SqlIntCodec(this);
1017
1194
  }
@@ -1030,6 +1207,9 @@ export class PgFloatDescriptor extends CodecDescriptorImpl<void> {
1030
1207
  override readonly meta = PG_FLOAT_META;
1031
1208
  override readonly traits = sqlFloatDescriptor.traits;
1032
1209
  override readonly paramsSchema = sqlFloatDescriptor.paramsSchema;
1210
+ override renderValueLiteral(value: JsonValue): string | undefined {
1211
+ return renderTsLiteral(value);
1212
+ }
1033
1213
  override factory(): (ctx: CodecInstanceContext) => SqlFloatCodec {
1034
1214
  return () => new SqlFloatCodec(this);
1035
1215
  }
@@ -1052,6 +1232,7 @@ export const codecDescriptors: readonly AnyCodecDescriptor[] = [
1052
1232
  sqlTextDescriptor,
1053
1233
  sqlTimestampDescriptor,
1054
1234
  pgTextDescriptor,
1235
+ pgEnumDescriptor,
1055
1236
  pgCharDescriptor,
1056
1237
  pgVarcharDescriptor,
1057
1238
  pgIntDescriptor,
@@ -3,6 +3,7 @@ import {
3
3
  DdlNode,
4
4
  type DdlTableConstraint,
5
5
  } from '@prisma-next/sql-relational-core/ast';
6
+ import type { RlsPolicyOperation } from '../rls/canonicalize';
6
7
 
7
8
  // ---------------------------------------------------------------------------
8
9
  // AlterTableAction — nested polymorphic hierarchy
@@ -10,6 +11,7 @@ import {
10
11
 
11
12
  export interface AlterTableActionVisitor<R> {
12
13
  addColumn(action: AddColumnAction): R;
14
+ dropDefault(action: DropDefaultAction): R;
13
15
  }
14
16
 
15
17
  export abstract class AlterTableAction {
@@ -32,7 +34,31 @@ export class AddColumnAction extends AlterTableAction {
32
34
  }
33
35
  }
34
36
 
35
- export type AnyAlterTableAction = AddColumnAction;
37
+ export class DropDefaultAction extends AlterTableAction {
38
+ readonly kind = 'drop-default' as const;
39
+ readonly columnName: string;
40
+
41
+ constructor(columnName: string) {
42
+ super();
43
+ this.columnName = columnName;
44
+ Object.freeze(this);
45
+ }
46
+
47
+ override accept<R>(visitor: AlterTableActionVisitor<R>): R {
48
+ return visitor.dropDefault(this);
49
+ }
50
+ }
51
+
52
+ /**
53
+ * The set of ALTER TABLE subactions currently expressible as typed DDL.
54
+ *
55
+ * The remaining actions — SetDefault, SetNotNull, DropNotNull,
56
+ * AlterColumnType — are still emitted as raw SQL by `operations/columns.ts`
57
+ * and join this union as they are converted to typed DDL. Until then it is
58
+ * intentionally partial: only the ALTER subactions used by the
59
+ * already-converted ops (AddColumn, DropDefault) appear here.
60
+ */
61
+ export type AnyAlterTableAction = AddColumnAction | DropDefaultAction;
36
62
 
37
63
  // ---------------------------------------------------------------------------
38
64
  // Top-level DDL visitor
@@ -41,7 +67,13 @@ export type AnyAlterTableAction = AddColumnAction;
41
67
  export interface PostgresDdlVisitor<R> {
42
68
  createTable(node: PostgresCreateTable): R;
43
69
  createSchema(node: PostgresCreateSchema): R;
70
+ createType(node: PostgresCreateType): R;
71
+ dropType(node: PostgresDropType): R;
44
72
  alterTable(node: PostgresAlterTable): R;
73
+ createPolicy(node: PostgresCreatePolicy): R;
74
+ dropPolicy(node: PostgresDropPolicy): R;
75
+ alterPolicyRename(node: PostgresAlterPolicyRename): R;
76
+ disableRowLevelSecurity(node: PostgresDisableRowLevelSecurity): R;
45
77
  }
46
78
 
47
79
  export abstract class PostgresDdlNode extends DdlNode {
@@ -104,6 +136,46 @@ export class PostgresCreateSchema extends PostgresDdlNode {
104
136
  }
105
137
  }
106
138
 
139
+ export class PostgresCreateType extends PostgresDdlNode {
140
+ readonly kind = 'create-type' as const;
141
+ readonly schema: string | undefined;
142
+ readonly name: string;
143
+ readonly values: ReadonlyArray<string>;
144
+
145
+ constructor(options: {
146
+ readonly schema?: string;
147
+ readonly name: string;
148
+ readonly values: readonly string[];
149
+ }) {
150
+ super();
151
+ this.schema = options.schema;
152
+ this.name = options.name;
153
+ this.values = Object.freeze([...options.values]);
154
+ this.freeze();
155
+ }
156
+
157
+ override accept<R>(visitor: PostgresDdlVisitor<R>): R {
158
+ return visitor.createType(this);
159
+ }
160
+ }
161
+
162
+ export class PostgresDropType extends PostgresDdlNode {
163
+ readonly kind = 'drop-type' as const;
164
+ readonly schema: string | undefined;
165
+ readonly name: string;
166
+
167
+ constructor(options: { readonly schema?: string; readonly name: string }) {
168
+ super();
169
+ this.schema = options.schema;
170
+ this.name = options.name;
171
+ this.freeze();
172
+ }
173
+
174
+ override accept<R>(visitor: PostgresDdlVisitor<R>): R {
175
+ return visitor.dropType(this);
176
+ }
177
+ }
178
+
107
179
  export class PostgresAlterTable extends PostgresDdlNode {
108
180
  readonly kind = 'alter-table' as const;
109
181
  readonly table: string;
@@ -127,4 +199,115 @@ export class PostgresAlterTable extends PostgresDdlNode {
127
199
  }
128
200
  }
129
201
 
130
- export type AnyPostgresDdlNode = PostgresCreateTable | PostgresCreateSchema | PostgresAlterTable;
202
+ export type { RlsPolicyOperation };
203
+
204
+ export class PostgresCreatePolicy extends PostgresDdlNode {
205
+ readonly kind = 'create-policy' as const;
206
+ readonly schema: string;
207
+ readonly table: string;
208
+ readonly name: string;
209
+ readonly permissive: boolean;
210
+ readonly operation: RlsPolicyOperation;
211
+ readonly roles: ReadonlyArray<string>;
212
+ readonly using: string | undefined;
213
+ readonly withCheck: string | undefined;
214
+
215
+ constructor(options: {
216
+ readonly schema: string;
217
+ readonly table: string;
218
+ readonly name: string;
219
+ readonly permissive: boolean;
220
+ readonly operation: RlsPolicyOperation;
221
+ readonly roles: readonly string[];
222
+ readonly using?: string;
223
+ readonly withCheck?: string;
224
+ }) {
225
+ super();
226
+ this.schema = options.schema;
227
+ this.table = options.table;
228
+ this.name = options.name;
229
+ this.permissive = options.permissive;
230
+ this.operation = options.operation;
231
+ this.roles = Object.freeze([...options.roles]);
232
+ this.using = options.using;
233
+ this.withCheck = options.withCheck;
234
+ this.freeze();
235
+ }
236
+
237
+ override accept<R>(visitor: PostgresDdlVisitor<R>): R {
238
+ return visitor.createPolicy(this);
239
+ }
240
+ }
241
+
242
+ export class PostgresDropPolicy extends PostgresDdlNode {
243
+ readonly kind = 'drop-policy' as const;
244
+ readonly schema: string;
245
+ readonly table: string;
246
+ readonly name: string;
247
+
248
+ constructor(options: { readonly schema: string; readonly table: string; readonly name: string }) {
249
+ super();
250
+ this.schema = options.schema;
251
+ this.table = options.table;
252
+ this.name = options.name;
253
+ this.freeze();
254
+ }
255
+
256
+ override accept<R>(visitor: PostgresDdlVisitor<R>): R {
257
+ return visitor.dropPolicy(this);
258
+ }
259
+ }
260
+
261
+ export class PostgresAlterPolicyRename extends PostgresDdlNode {
262
+ readonly kind = 'alter-policy-rename' as const;
263
+ readonly schema: string;
264
+ readonly table: string;
265
+ readonly name: string;
266
+ readonly newName: string;
267
+
268
+ constructor(options: {
269
+ readonly schema: string;
270
+ readonly table: string;
271
+ readonly name: string;
272
+ readonly newName: string;
273
+ }) {
274
+ super();
275
+ this.schema = options.schema;
276
+ this.table = options.table;
277
+ this.name = options.name;
278
+ this.newName = options.newName;
279
+ this.freeze();
280
+ }
281
+
282
+ override accept<R>(visitor: PostgresDdlVisitor<R>): R {
283
+ return visitor.alterPolicyRename(this);
284
+ }
285
+ }
286
+
287
+ export class PostgresDisableRowLevelSecurity extends PostgresDdlNode {
288
+ readonly kind = 'disable-row-level-security' as const;
289
+ readonly schema: string;
290
+ readonly table: string;
291
+
292
+ constructor(options: { readonly schema: string; readonly table: string }) {
293
+ super();
294
+ this.schema = options.schema;
295
+ this.table = options.table;
296
+ this.freeze();
297
+ }
298
+
299
+ override accept<R>(visitor: PostgresDdlVisitor<R>): R {
300
+ return visitor.disableRowLevelSecurity(this);
301
+ }
302
+ }
303
+
304
+ export type AnyPostgresDdlNode =
305
+ | PostgresCreateTable
306
+ | PostgresCreateSchema
307
+ | PostgresCreateType
308
+ | PostgresDropType
309
+ | PostgresAlterTable
310
+ | PostgresCreatePolicy
311
+ | PostgresDropPolicy
312
+ | PostgresAlterPolicyRename
313
+ | PostgresDisableRowLevelSecurity;