@prisma-next/target-postgres 0.14.0-dev.6 → 0.14.0-dev.60

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 (253) hide show
  1. package/dist/authoring-WkqG_MkU.mjs +419 -0
  2. package/dist/authoring-WkqG_MkU.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-CkVQrZ4G.mjs +913 -0
  15. package/dist/codecs-CkVQrZ4G.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 +4 -4
  24. package/dist/control-policy-Co_13Hxa.mjs +1128 -0
  25. package/dist/control-policy-Co_13Hxa.mjs.map +1 -0
  26. package/dist/control.d.mts +2 -2
  27. package/dist/control.d.mts.map +1 -1
  28. package/dist/control.mjs +900 -35
  29. package/dist/control.mjs.map +1 -1
  30. package/dist/data-transform-BOWpliq8.mjs.map +1 -1
  31. package/dist/{data-transform-DDgWdB5o.d.mts → data-transform-DPzwBMhW.d.mts} +3 -3
  32. package/dist/{data-transform-DDgWdB5o.d.mts.map → data-transform-DPzwBMhW.d.mts.map} +1 -1
  33. package/dist/data-transform.d.mts +1 -1
  34. package/dist/{ddl-QDyOSeLc.mjs → ddl-B-krEXY1.mjs} +77 -6
  35. package/dist/ddl-B-krEXY1.mjs.map +1 -0
  36. package/dist/ddl.d.mts +3 -2
  37. package/dist/ddl.mjs +2 -2
  38. package/dist/default-normalizer.d.mts +1 -1
  39. package/dist/default-normalizer.d.mts.map +1 -1
  40. package/dist/default-normalizer.mjs +247 -1
  41. package/dist/default-normalizer.mjs.map +1 -0
  42. package/dist/descriptor-meta-BOcKPaLE.mjs +22 -0
  43. package/dist/descriptor-meta-BOcKPaLE.mjs.map +1 -0
  44. package/dist/descriptor-meta-runtime-UwYdV8fD.mjs +12 -0
  45. package/dist/descriptor-meta-runtime-UwYdV8fD.mjs.map +1 -0
  46. package/dist/diff-database-schema-BOsDnH64.mjs +266 -0
  47. package/dist/diff-database-schema-BOsDnH64.mjs.map +1 -0
  48. package/dist/diff-database-schema.d.mts +46 -0
  49. package/dist/diff-database-schema.d.mts.map +1 -0
  50. package/dist/diff-database-schema.mjs +2 -0
  51. package/dist/errors-DO-w0EUS.mjs +35 -0
  52. package/dist/errors-DO-w0EUS.mjs.map +1 -0
  53. package/dist/errors.d.mts +11 -9
  54. package/dist/errors.d.mts.map +1 -1
  55. package/dist/errors.mjs +1 -1
  56. package/dist/issue-planner.d.mts +35 -11
  57. package/dist/issue-planner.d.mts.map +1 -1
  58. package/dist/issue-planner.mjs +2 -2
  59. package/dist/migration.d.mts +5 -5
  60. package/dist/migration.d.mts.map +1 -1
  61. package/dist/migration.mjs +4 -4
  62. package/dist/namespace-ids-xtp_ZY86.mjs +7 -0
  63. package/dist/namespace-ids-xtp_ZY86.mjs.map +1 -0
  64. package/dist/native-type-normalizer.mjs +37 -1
  65. package/dist/native-type-normalizer.mjs.map +1 -0
  66. package/dist/nodes-B815pj_X.mjs +162 -0
  67. package/dist/nodes-B815pj_X.mjs.map +1 -0
  68. package/dist/nodes-BtQyZha6.d.mts +146 -0
  69. package/dist/nodes-BtQyZha6.d.mts.map +1 -0
  70. package/dist/{op-factory-call-DmQEc3XV.d.mts → op-factory-call-D6_7-7Vc.d.mts} +68 -15
  71. package/dist/op-factory-call-D6_7-7Vc.d.mts.map +1 -0
  72. package/dist/{op-factory-call-D_p5vxwt.mjs → op-factory-call-DxNNKUeZ.mjs} +300 -31
  73. package/dist/op-factory-call-DxNNKUeZ.mjs.map +1 -0
  74. package/dist/op-factory-call.d.mts +2 -2
  75. package/dist/op-factory-call.mjs +2 -2
  76. package/dist/pack.d.mts +144 -3
  77. package/dist/pack.d.mts.map +1 -1
  78. package/dist/pack.mjs +1 -1
  79. package/dist/planner-CNrc1Odu.mjs +422 -0
  80. package/dist/planner-CNrc1Odu.mjs.map +1 -0
  81. package/dist/{planner-ddl-builders-B2wOwLqI.mjs → planner-ddl-builders-B8Nn6nHN.mjs} +12 -27
  82. package/dist/planner-ddl-builders-B8Nn6nHN.mjs.map +1 -0
  83. package/dist/planner-ddl-builders.d.mts +4 -6
  84. package/dist/planner-ddl-builders.d.mts.map +1 -1
  85. package/dist/planner-ddl-builders.mjs +2 -2
  86. package/dist/planner-identity-values-CJPha2Sz.mjs.map +1 -1
  87. package/dist/planner-identity-values.d.mts +8 -2
  88. package/dist/planner-identity-values.d.mts.map +1 -1
  89. package/dist/planner-identity-values.mjs +2 -2
  90. package/dist/{planner-produced-postgres-migration-Cji5vxUf.mjs → planner-produced-postgres-migration-NI-oGKcw.mjs} +3 -3
  91. package/dist/{planner-produced-postgres-migration-Cji5vxUf.mjs.map → planner-produced-postgres-migration-NI-oGKcw.mjs.map} +1 -1
  92. package/dist/{planner-produced-postgres-migration-QqHa2C2l.d.mts → planner-produced-postgres-migration-a-tjfKsA.d.mts} +4 -4
  93. package/dist/{planner-produced-postgres-migration-QqHa2C2l.d.mts.map → planner-produced-postgres-migration-a-tjfKsA.d.mts.map} +1 -1
  94. package/dist/planner-produced-postgres-migration.d.mts +1 -1
  95. package/dist/planner-produced-postgres-migration.mjs +1 -1
  96. package/dist/{planner-sql-checks-jqUUGyQR.mjs → planner-sql-checks-AXoxB1Jl.mjs} +2 -2
  97. package/dist/{planner-sql-checks-jqUUGyQR.mjs.map → planner-sql-checks-AXoxB1Jl.mjs.map} +1 -1
  98. package/dist/planner-sql-checks.d.mts +2 -2
  99. package/dist/planner-sql-checks.d.mts.map +1 -1
  100. package/dist/planner-sql-checks.mjs +1 -1
  101. package/dist/{planner-target-details-CIY6tLeo.d.mts → planner-target-details-HkP4RrRO.d.mts} +2 -2
  102. package/dist/planner-target-details-HkP4RrRO.d.mts.map +1 -0
  103. package/dist/planner-target-details.d.mts +1 -1
  104. package/dist/planner.d.mts +72 -17
  105. package/dist/planner.d.mts.map +1 -1
  106. package/dist/planner.mjs +3 -2
  107. package/dist/postgres-contract-view-C9j5cqP0.d.mts +34 -0
  108. package/dist/postgres-contract-view-C9j5cqP0.d.mts.map +1 -0
  109. package/dist/{postgres-contract-serializer-k3TAcPMY.mjs → postgres-contract-view-Dg-N7KA7.mjs} +45 -41
  110. package/dist/postgres-contract-view-Dg-N7KA7.mjs.map +1 -0
  111. package/dist/postgres-database-schema-node-CfdTY-Qs.d.mts +218 -0
  112. package/dist/postgres-database-schema-node-CfdTY-Qs.d.mts.map +1 -0
  113. package/dist/{postgres-migration-Y4YBJqkS.d.mts → postgres-migration-Bzzi0Asr.d.mts} +28 -9
  114. package/dist/postgres-migration-Bzzi0Asr.d.mts.map +1 -0
  115. package/dist/{postgres-migration-B5jKrXv3.mjs → postgres-migration-ChyRsWvE.mjs} +53 -45
  116. package/dist/postgres-migration-ChyRsWvE.mjs.map +1 -0
  117. package/dist/postgres-native-enum-xdiqmBf_.mjs +41 -0
  118. package/dist/postgres-native-enum-xdiqmBf_.mjs.map +1 -0
  119. package/dist/postgres-rls-policy--CSddvMR.d.mts +52 -0
  120. package/dist/postgres-rls-policy--CSddvMR.d.mts.map +1 -0
  121. package/dist/postgres-role-schema-node-BiSTCTNg.mjs +195 -0
  122. package/dist/postgres-role-schema-node-BiSTCTNg.mjs.map +1 -0
  123. package/dist/postgres-schema-CU4gF6QZ.d.mts +257 -0
  124. package/dist/postgres-schema-CU4gF6QZ.d.mts.map +1 -0
  125. package/dist/{postgres-schema-COGZ1ark.mjs → postgres-schema-DTLA_aRi.mjs} +171 -10
  126. package/dist/postgres-schema-DTLA_aRi.mjs.map +1 -0
  127. package/dist/postgres-table-schema-node-9FCtCUz2.mjs +153 -0
  128. package/dist/postgres-table-schema-node-9FCtCUz2.mjs.map +1 -0
  129. package/dist/render-ops.d.mts +1 -1
  130. package/dist/{render-typescript-KMgosran.mjs → render-typescript-7yqILcwr.mjs} +45 -16
  131. package/dist/render-typescript-7yqILcwr.mjs.map +1 -0
  132. package/dist/render-typescript.mjs +1 -1
  133. package/dist/resolve-ddl-schema-Ca88Tg6W.mjs +22 -0
  134. package/dist/resolve-ddl-schema-Ca88Tg6W.mjs.map +1 -0
  135. package/dist/rls-canonicalize.d.mts +20 -0
  136. package/dist/rls-canonicalize.d.mts.map +1 -0
  137. package/dist/rls-canonicalize.mjs +2 -0
  138. package/dist/runtime.d.mts +6 -8
  139. package/dist/runtime.d.mts.map +1 -1
  140. package/dist/runtime.mjs +3 -3
  141. package/dist/runtime.mjs.map +1 -1
  142. package/dist/schema-ir-annotations.d.mts +15 -0
  143. package/dist/schema-ir-annotations.d.mts.map +1 -0
  144. package/dist/schema-ir-annotations.mjs +2 -0
  145. package/dist/types.d.mts +4 -140
  146. package/dist/types.mjs +5 -2
  147. package/dist/wire-name-rHHtr1h0.mjs +75 -0
  148. package/dist/wire-name-rHHtr1h0.mjs.map +1 -0
  149. package/package.json +23 -17
  150. package/src/contract-free/checks.ts +73 -0
  151. package/src/contract-free/ddl.ts +71 -1
  152. package/src/core/authoring.ts +356 -2
  153. package/src/core/codec-ids.ts +1 -0
  154. package/src/core/codec-type-map.ts +2 -0
  155. package/src/core/codecs.ts +180 -1
  156. package/src/core/ddl/nodes.ts +141 -2
  157. package/src/core/default-normalizer.ts +122 -2
  158. package/src/core/descriptor-meta.ts +10 -1
  159. package/src/core/entity-kinds.ts +41 -0
  160. package/src/core/errors.ts +14 -12
  161. package/src/core/migrations/bound-schema.ts +10 -0
  162. package/src/core/migrations/column-ddl-rendering.ts +117 -0
  163. package/src/core/migrations/contract-to-postgres-database-schema-node.ts +160 -0
  164. package/src/core/migrations/control-policy.ts +99 -46
  165. package/src/core/migrations/diff-database-schema.ts +217 -0
  166. package/src/core/migrations/issue-planner.ts +692 -652
  167. package/src/core/migrations/op-factory-call.ts +189 -21
  168. package/src/core/migrations/operations/columns.ts +24 -12
  169. package/src/core/migrations/operations/constraints.ts +1 -1
  170. package/src/core/migrations/operations/data-transform.ts +6 -0
  171. package/src/core/migrations/operations/dependencies.ts +2 -1
  172. package/src/core/migrations/operations/rls.ts +187 -0
  173. package/src/core/migrations/operations/shared.ts +0 -30
  174. package/src/core/migrations/planner-ddl-builders.ts +20 -58
  175. package/src/core/migrations/planner-identity-values.ts +1 -1
  176. package/src/core/migrations/planner-recipes.ts +31 -13
  177. package/src/core/migrations/planner-sql-checks.ts +1 -1
  178. package/src/core/migrations/planner-strategies.ts +248 -216
  179. package/src/core/migrations/planner-target-details.ts +3 -1
  180. package/src/core/migrations/planner.ts +381 -59
  181. package/src/core/migrations/postgres-migration.ts +70 -86
  182. package/src/core/migrations/render-typescript.ts +42 -15
  183. package/src/core/migrations/resolve-ddl-schema.ts +26 -0
  184. package/src/core/migrations/runner.ts +10 -16
  185. package/src/core/migrations/verify-postgres-namespaces.ts +47 -31
  186. package/src/core/namespace-ids.ts +3 -0
  187. package/src/core/postgres-contract-serializer.ts +46 -71
  188. package/src/core/postgres-contract-view.ts +48 -0
  189. package/src/core/postgres-native-enum.ts +51 -0
  190. package/src/core/postgres-rls-enablement.ts +34 -0
  191. package/src/core/postgres-rls-policy.ts +64 -0
  192. package/src/core/postgres-role.ts +37 -0
  193. package/src/core/postgres-schema-verifier.ts +10 -7
  194. package/src/core/postgres-schema.ts +64 -15
  195. package/src/core/postgres-validators.ts +33 -0
  196. package/src/core/psl-infer/infer-psl-contract.ts +1325 -0
  197. package/src/core/psl-infer/postgres-default-mapping.ts +16 -0
  198. package/src/core/psl-infer/postgres-type-map.ts +128 -0
  199. package/src/core/rls/canonicalize.ts +48 -0
  200. package/src/core/rls/wire-name.ts +33 -0
  201. package/src/core/schema-ir/postgres-database-schema-node.ts +60 -0
  202. package/src/core/schema-ir/postgres-namespace-schema-node.ts +79 -0
  203. package/src/core/schema-ir/postgres-policy-schema-node.ts +90 -0
  204. package/src/core/schema-ir/postgres-role-schema-node.ts +63 -0
  205. package/src/core/schema-ir/postgres-table-schema-node.ts +140 -0
  206. package/src/core/schema-ir/schema-node-kinds.ts +106 -0
  207. package/src/exports/codecs.ts +3 -0
  208. package/src/exports/contract-free.ts +11 -1
  209. package/src/exports/control.ts +27 -33
  210. package/src/exports/ddl.ts +6 -0
  211. package/src/exports/diff-database-schema.ts +7 -0
  212. package/src/exports/issue-planner.ts +1 -1
  213. package/src/exports/migration.ts +1 -0
  214. package/src/exports/op-factory-call.ts +1 -0
  215. package/src/exports/planner-ddl-builders.ts +0 -2
  216. package/src/exports/planner-identity-values.ts +4 -1
  217. package/src/exports/planner.ts +1 -0
  218. package/src/exports/rls-canonicalize.ts +7 -0
  219. package/src/exports/runtime.ts +1 -0
  220. package/src/exports/schema-ir-annotations.ts +1 -0
  221. package/src/exports/types.ts +34 -0
  222. package/dist/codec-ids-BvytN2P8.mjs.map +0 -1
  223. package/dist/codec-ids-CnXu9Qy3.d.mts.map +0 -1
  224. package/dist/codec-types-DHCkwPKE.d.mts.map +0 -1
  225. package/dist/codecs--0A5_4Bq.d.mts.map +0 -1
  226. package/dist/ddl-QDyOSeLc.mjs.map +0 -1
  227. package/dist/default-normalizer-DyyCHQWs.mjs +0 -131
  228. package/dist/default-normalizer-DyyCHQWs.mjs.map +0 -1
  229. package/dist/descriptor-meta-CpGygXpI.mjs +0 -140
  230. package/dist/descriptor-meta-CpGygXpI.mjs.map +0 -1
  231. package/dist/errors-CUk87ByX.mjs +0 -33
  232. package/dist/errors-CUk87ByX.mjs.map +0 -1
  233. package/dist/issue-planner-DL6g3CmE.mjs +0 -824
  234. package/dist/issue-planner-DL6g3CmE.mjs.map +0 -1
  235. package/dist/native-type-normalizer-Bc9XJzWC.mjs +0 -38
  236. package/dist/native-type-normalizer-Bc9XJzWC.mjs.map +0 -1
  237. package/dist/nodes-Bbhs2rwj.mjs +0 -76
  238. package/dist/nodes-Bbhs2rwj.mjs.map +0 -1
  239. package/dist/nodes-pLeLgdis.d.mts +0 -67
  240. package/dist/nodes-pLeLgdis.d.mts.map +0 -1
  241. package/dist/op-factory-call-D_p5vxwt.mjs.map +0 -1
  242. package/dist/op-factory-call-DmQEc3XV.d.mts.map +0 -1
  243. package/dist/planner-Bs_baQax.mjs +0 -324
  244. package/dist/planner-Bs_baQax.mjs.map +0 -1
  245. package/dist/planner-ddl-builders-B2wOwLqI.mjs.map +0 -1
  246. package/dist/planner-target-details-CIY6tLeo.d.mts.map +0 -1
  247. package/dist/postgres-contract-serializer-k3TAcPMY.mjs.map +0 -1
  248. package/dist/postgres-migration-B5jKrXv3.mjs.map +0 -1
  249. package/dist/postgres-migration-Y4YBJqkS.d.mts.map +0 -1
  250. package/dist/postgres-schema-COGZ1ark.mjs.map +0 -1
  251. package/dist/render-typescript-KMgosran.mjs.map +0 -1
  252. package/dist/types.d.mts.map +0 -1
  253. package/src/core/postgres-enum-type-schema.ts +0 -17
@@ -1,13 +1,288 @@
1
1
  import { temporalAuthoringPresets } from '@prisma-next/family-sql/control';
2
2
  import type {
3
+ AuthoringEntityContext,
4
+ AuthoringEntityTypeFactoryOutput,
3
5
  AuthoringEntityTypeNamespace,
4
6
  AuthoringFieldNamespace,
7
+ AuthoringModelAttributeContext,
8
+ AuthoringModelAttributeDescriptorNamespace,
9
+ AuthoringPslBlockDescriptorNamespace,
5
10
  AuthoringTypeNamespace,
11
+ PslExtensionBlock,
6
12
  } from '@prisma-next/framework-components/authoring';
13
+ import { modelAttribute } from '@prisma-next/psl-parser';
14
+ import type { SqlValueSetDerivingEntityTypeOutput } from '@prisma-next/sql-contract/value-set-derivation-hook';
15
+ import { PG_ENUM_CODEC_ID } from './codec-ids';
16
+ import { PostgresNativeEnum } from './postgres-native-enum';
17
+ import { PostgresRlsEnablement, type PostgresRlsEnablementInput } from './postgres-rls-enablement';
18
+ import { PostgresRlsPolicy } from './postgres-rls-policy';
19
+ import { PostgresRole, type PostgresRoleInput } from './postgres-role';
20
+ import {
21
+ PostgresNativeEnumSchema,
22
+ PostgresRlsEnablementSchema,
23
+ PostgresRlsPolicySchema,
24
+ PostgresRoleSchema,
25
+ } from './postgres-validators';
26
+ import { computeContentHash, normalizePredicate } from './rls/canonicalize';
27
+ import { formatRlsPolicyWireName } from './rls/wire-name';
7
28
 
8
- export const postgresAuthoringTypes = {} as const satisfies AuthoringTypeNamespace;
29
+ /**
30
+ * `pg.enum(<ref>)` registers as an ordinary type constructor whose sole
31
+ * positional argument names a `native_enum` entity instead of carrying a
32
+ * literal value. The interpreter resolves the ref to the `native_enum`
33
+ * entity generically (driven by `entityRefArg`); the `pg/enum@1` codec
34
+ * descriptor's `columnFromEntity` hook (see `codecs.ts`) converts that
35
+ * entity into the column's `typeParams` and native type.
36
+ */
37
+ export const postgresAuthoringTypes = {
38
+ pg: {
39
+ enum: {
40
+ kind: 'typeConstructor',
41
+ entityRefArg: { index: 0, entityKind: 'native_enum' },
42
+ output: {
43
+ codecId: PG_ENUM_CODEC_ID,
44
+ },
45
+ },
46
+ },
47
+ } as const satisfies AuthoringTypeNamespace;
48
+
49
+ export interface RlsPolicyExtensionBlock extends PslExtensionBlock {
50
+ readonly namespaceId: string;
51
+ }
9
52
 
10
- export const postgresAuthoringEntityTypes = {} as const satisfies AuthoringEntityTypeNamespace;
53
+ function readRefParam(block: PslExtensionBlock, key: string): string | undefined {
54
+ const param = block.parameters[key];
55
+ return param?.kind === 'ref' ? param.identifier : undefined;
56
+ }
57
+
58
+ function readValueParam(block: PslExtensionBlock, key: string): string | undefined {
59
+ const param = block.parameters[key];
60
+ return param?.kind === 'value' ? param.raw : undefined;
61
+ }
62
+
63
+ function readListRefParams(block: PslExtensionBlock, key: string): string[] {
64
+ const param = block.parameters[key];
65
+ if (param?.kind !== 'list') return [];
66
+ return param.items.flatMap((item) => (item.kind === 'ref' ? [item.identifier] : []));
67
+ }
68
+
69
+ /**
70
+ * Unwraps a quoted PSL string argument, inverting the printer's
71
+ * `escapePslString` escapes (`\\`, `\"`, `\n`, `\r`). An unknown escape
72
+ * sequence is kept verbatim, matching the printer-side `unescapePslString`
73
+ * convention.
74
+ */
75
+ function unwrapQuotedString(raw: string): string {
76
+ if (!(raw.startsWith('"') && raw.endsWith('"') && raw.length >= 2)) {
77
+ return raw;
78
+ }
79
+ const inner = raw.slice(1, -1);
80
+ let result = '';
81
+ for (let i = 0; i < inner.length; i++) {
82
+ if (inner[i] !== '\\' || i + 1 >= inner.length) {
83
+ result += inner[i];
84
+ continue;
85
+ }
86
+ const next = inner[i + 1];
87
+ if (next === '\\' || next === '"') {
88
+ result += next;
89
+ } else if (next === 'n') {
90
+ result += '\n';
91
+ } else if (next === 'r') {
92
+ result += '\r';
93
+ } else {
94
+ result += '\\';
95
+ result += next;
96
+ }
97
+ i++;
98
+ }
99
+ return result;
100
+ }
101
+
102
+ function lowerRlsPolicyFromBlock(
103
+ block: RlsPolicyExtensionBlock,
104
+ _ctx: AuthoringEntityContext,
105
+ ): PostgresRlsPolicy {
106
+ const prefix = block.name;
107
+ const targetModelName = readRefParam(block, 'target') ?? '';
108
+ const tableName = targetModelName.charAt(0).toLowerCase() + targetModelName.slice(1);
109
+ const roles = [...readListRefParams(block, 'roles')].sort();
110
+ const using = unwrapQuotedString(readValueParam(block, 'using') ?? '');
111
+
112
+ const wireHash = computeContentHash({
113
+ using: normalizePredicate(using),
114
+ roles,
115
+ operation: 'select',
116
+ permissive: true,
117
+ });
118
+ const wireName = formatRlsPolicyWireName(prefix, wireHash);
119
+
120
+ return new PostgresRlsPolicy({
121
+ name: wireName,
122
+ prefix,
123
+ tableName,
124
+ namespaceId: block.namespaceId,
125
+ operation: 'select',
126
+ roles,
127
+ using,
128
+ permissive: true,
129
+ });
130
+ }
131
+
132
+ /**
133
+ * Lowers a `native_enum { memberName = "value" … @@map("type_name") }` block
134
+ * into a {@link PostgresNativeEnum}. Members must be authored as explicit
135
+ * `key = "value"` pairs — a bare (value-less) member is a diagnostic, not
136
+ * accepted (authoring-design.md §2.1). The parsed `memberName` is only used
137
+ * to duplicate-check and report diagnostics; the lowered entity carries just
138
+ * the member values (a native enum is value-only — the member "name" isn't
139
+ * a separate authoring concept from the value). `typeName` comes from
140
+ * `@@map` or defaults to the block name verbatim.
141
+ */
142
+ function lowerNativeEnumFromBlock(
143
+ block: PslExtensionBlock,
144
+ ctx: AuthoringEntityContext,
145
+ ): PostgresNativeEnum | undefined {
146
+ const sourceId = ctx.sourceId ?? 'unknown';
147
+ const diagnostics = ctx.diagnostics;
148
+
149
+ const mapAttr = block.blockAttributes.find((a) => a.name === 'map');
150
+ let typeName = block.name;
151
+ if (mapAttr) {
152
+ const rawArg = mapAttr.args[0]?.value;
153
+ const mapped = rawArg !== undefined ? unwrapQuotedString(rawArg) : undefined;
154
+ if (mapped === undefined) {
155
+ diagnostics?.push({
156
+ code: 'PSL_NATIVE_ENUM_INVALID_MAP',
157
+ message: `native_enum "${block.name}" @@map attribute must have a quoted type-name argument`,
158
+ sourceId,
159
+ span: mapAttr.span,
160
+ });
161
+ return undefined;
162
+ }
163
+ typeName = mapped;
164
+ }
165
+
166
+ let memberError = false;
167
+ const seenValues = new Set<string>();
168
+ const members: string[] = [];
169
+ for (const [memberName, paramValue] of Object.entries(block.parameters)) {
170
+ if (paramValue.kind === 'bare') {
171
+ diagnostics?.push({
172
+ code: 'PSL_NATIVE_ENUM_BARE_MEMBER',
173
+ message: `native_enum "${block.name}" member "${memberName}" has no value; members must be authored as "${memberName} = \\"value\\""`,
174
+ sourceId,
175
+ span: paramValue.span,
176
+ });
177
+ memberError = true;
178
+ continue;
179
+ }
180
+ if (paramValue.kind !== 'value') continue;
181
+
182
+ let jsonValue: unknown;
183
+ try {
184
+ jsonValue = JSON.parse(paramValue.raw);
185
+ } catch {
186
+ diagnostics?.push({
187
+ code: 'PSL_EXTENSION_INVALID_VALUE',
188
+ message: `native_enum "${block.name}" member "${memberName}" value "${paramValue.raw}" is not valid JSON`,
189
+ sourceId,
190
+ span: paramValue.span,
191
+ });
192
+ memberError = true;
193
+ continue;
194
+ }
195
+ if (typeof jsonValue !== 'string') {
196
+ diagnostics?.push({
197
+ code: 'PSL_EXTENSION_INVALID_VALUE',
198
+ message: `native_enum "${block.name}" member "${memberName}" value must be a string`,
199
+ sourceId,
200
+ span: paramValue.span,
201
+ });
202
+ memberError = true;
203
+ continue;
204
+ }
205
+ if (seenValues.has(jsonValue)) {
206
+ diagnostics?.push({
207
+ code: 'PSL_NATIVE_ENUM_DUPLICATE_MEMBER_VALUE',
208
+ message: `native_enum "${block.name}": duplicate member value "${jsonValue}"`,
209
+ sourceId,
210
+ span: paramValue.span,
211
+ });
212
+ memberError = true;
213
+ continue;
214
+ }
215
+ seenValues.add(jsonValue);
216
+ members.push(jsonValue);
217
+ }
218
+
219
+ if (memberError) return undefined;
220
+
221
+ if (members.length === 0) {
222
+ diagnostics?.push({
223
+ code: 'PSL_NATIVE_ENUM_MISSING_MEMBERS',
224
+ message: `native_enum "${block.name}" must have at least one member`,
225
+ sourceId,
226
+ span: block.span,
227
+ });
228
+ return undefined;
229
+ }
230
+
231
+ // `control` stays unset — the effective grade is resolved at read time via `effectiveControlPolicy`, like `StorageTable`/`StorageColumn`.
232
+ return new PostgresNativeEnum({ typeName, members });
233
+ }
234
+
235
+ /**
236
+ * `native_enum`'s entity-type factory output, checked separately from the assembled
237
+ * `postgresAuthoringEntityTypes` map below: `deriveValueSet` is SQL-family surface
238
+ * ({@link SqlValueSetDerivingEntityTypeOutput}), not part of the framework
239
+ * `AuthoringEntityTypeFactoryOutput` shape, so folding it directly into the map's single
240
+ * `satisfies AuthoringEntityTypeNamespace` check would trip an excess-property error. Checking it
241
+ * here against the intersection of both shapes keeps it structurally valid against each without
242
+ * widening the map's own check.
243
+ */
244
+ const nativeEnumEntityTypeOutput = {
245
+ factory: lowerNativeEnumFromBlock,
246
+ deriveValueSet: (entity: PostgresNativeEnum) => ({
247
+ kind: 'valueSet' as const,
248
+ values: [...entity.members],
249
+ }),
250
+ } satisfies AuthoringEntityTypeFactoryOutput<PslExtensionBlock, PostgresNativeEnum | undefined> &
251
+ SqlValueSetDerivingEntityTypeOutput;
252
+
253
+ export const postgresAuthoringEntityTypes = {
254
+ role: {
255
+ kind: 'entity',
256
+ discriminator: 'role',
257
+ validatorSchema: PostgresRoleSchema,
258
+ output: {
259
+ factory: (input: PostgresRoleInput): PostgresRole => new PostgresRole(input),
260
+ },
261
+ },
262
+ rls: {
263
+ kind: 'entity',
264
+ discriminator: 'rls',
265
+ validatorSchema: PostgresRlsEnablementSchema,
266
+ output: {
267
+ factory: (input: PostgresRlsEnablementInput): PostgresRlsEnablement =>
268
+ new PostgresRlsEnablement(input),
269
+ },
270
+ },
271
+ policy: {
272
+ kind: 'entity',
273
+ discriminator: 'policy',
274
+ validatorSchema: PostgresRlsPolicySchema,
275
+ output: {
276
+ factory: lowerRlsPolicyFromBlock,
277
+ },
278
+ },
279
+ native_enum: {
280
+ kind: 'entity',
281
+ discriminator: 'native_enum',
282
+ validatorSchema: PostgresNativeEnumSchema,
283
+ output: nativeEnumEntityTypeOutput,
284
+ },
285
+ } as const satisfies AuthoringEntityTypeNamespace;
11
286
 
12
287
  /**
13
288
  * Field presets contributed by the Postgres target pack.
@@ -22,6 +297,85 @@ export const postgresAuthoringEntityTypes = {} as const satisfies AuthoringEntit
22
297
  * portability use `uuidString` / `id.uuidv4String` / `id.uuidv7String` from
23
298
  * the family pack instead.
24
299
  */
300
+ /**
301
+ * PSL block descriptor for `policy_select`.
302
+ *
303
+ * The parser learns the block shape from this descriptor; lowering from
304
+ * `PslExtensionBlock` to `PostgresRlsPolicy` is wired in the PSL
305
+ * interpreter (a later dispatch). The `discriminator` matches
306
+ * `PostgresRlsPolicy.kind` so the parsed block node carries the same
307
+ * discriminant as the IR class it will lower to.
308
+ *
309
+ * The `roles` list uses `scope:'cross-space'` because same-namespace
310
+ * role ref resolution requires PSL namespace entries keyed by `refKind`
311
+ * (i.e. `'role'`), which in turn requires the role block discriminator to
312
+ * equal `'role'`. Aligning discriminator with refKind is tracked for
313
+ * slice 4 (cross-space roles). Until then cross-space passes validation
314
+ * unconditionally and the authored role names flow through unchanged.
315
+ */
316
+ export const postgresAuthoringPslBlockDescriptors = {
317
+ policy_select: {
318
+ kind: 'pslBlock',
319
+ keyword: 'policy_select',
320
+ discriminator: 'policy',
321
+ name: { required: true },
322
+ parameters: {
323
+ target: { kind: 'ref', refKind: 'model', scope: 'same-namespace', required: true },
324
+ roles: {
325
+ kind: 'list',
326
+ of: { kind: 'ref', refKind: 'role', scope: 'cross-space' },
327
+ },
328
+ using: { kind: 'value', codecId: 'pg/text@1', required: true },
329
+ },
330
+ // A policy may only target an RLS-controlled model: the model named by
331
+ // `target` must declare `@@rls`, or the load fails with a diagnostic
332
+ // naming the model and the policy prefix.
333
+ requiresModelAttribute: { parameter: 'target', attribute: 'rls' },
334
+ },
335
+ /**
336
+ * PSL block descriptor for `native_enum`.
337
+ *
338
+ * Reuses the existing variadic-block mechanism (the same shape the SQL
339
+ * family's `enum` block ships): the body is an open `memberName = "value"`
340
+ * list. `variadicParameters: true` opens the block to arbitrary keys
341
+ * beyond the declared (empty) `parameters` set — the lowering factory
342
+ * (`lowerNativeEnumFromBlock`) turns the variadic entries into ordered
343
+ * members and rejects a bare (value-less) member.
344
+ */
345
+ native_enum: {
346
+ kind: 'pslBlock',
347
+ keyword: 'native_enum',
348
+ discriminator: 'native_enum',
349
+ name: { required: true },
350
+ parameters: {},
351
+ variadicParameters: true,
352
+ },
353
+ } as const satisfies AuthoringPslBlockDescriptorNamespace;
354
+
355
+ /**
356
+ * `@@` model attributes contributed by the Postgres target pack.
357
+ *
358
+ * `@@rls` is argument-less: presence marks the model's table
359
+ * RLS-controlled. It lowers to a {@link PostgresRlsEnablement} marker in
360
+ * the namespace's `entries.rls`, keyed by the model's table name — the
361
+ * marker (never the policy set) is what drives
362
+ * `ENABLE`/`DISABLE ROW LEVEL SECURITY` planning.
363
+ */
364
+ export const postgresAuthoringModelAttributes = {
365
+ rls: {
366
+ kind: 'modelAttribute',
367
+ attribute: 'rls',
368
+ spec: modelAttribute('rls', {}),
369
+ lower: (_parsed: Record<never, never>, ctx: AuthoringModelAttributeContext) => ({
370
+ key: ctx.storageName,
371
+ entity: new PostgresRlsEnablement({
372
+ tableName: ctx.storageName,
373
+ namespaceId: ctx.namespaceId,
374
+ }),
375
+ }),
376
+ },
377
+ } as const satisfies AuthoringModelAttributeDescriptorNamespace;
378
+
25
379
  export const postgresAuthoringFieldPresets = {
26
380
  text: {
27
381
  kind: 'fieldPreset',
@@ -8,6 +8,7 @@ export {
8
8
  } from '@prisma-next/sql-relational-core/ast';
9
9
  export const PG_TEXT_CODEC_ID = 'pg/text@1' as const;
10
10
  export const PG_TEXT_ARRAY_CODEC_ID = 'pg/text-array@1' as const;
11
+ export const PG_ENUM_CODEC_ID = 'pg/enum@1' as const;
11
12
  export const PG_CHAR_CODEC_ID = 'pg/char@1' as const;
12
13
  export const PG_VARCHAR_CODEC_ID = 'pg/varchar@1' as const;
13
14
  export const PG_INT_CODEC_ID = 'pg/int@1' as const;
@@ -21,6 +21,7 @@ import {
21
21
  pgBoolDescriptor,
22
22
  pgByteaDescriptor,
23
23
  pgCharDescriptor,
24
+ pgEnumDescriptor,
24
25
  pgFloat4Descriptor,
25
26
  pgFloat8Descriptor,
26
27
  pgFloatDescriptor,
@@ -50,6 +51,7 @@ export const codecDescriptorMap = {
50
51
  'sql-text': sqlTextDescriptor,
51
52
  'sql-timestamp': sqlTimestampDescriptor,
52
53
  text: pgTextDescriptor,
54
+ enum: pgEnumDescriptor,
53
55
  character: pgCharDescriptor,
54
56
  'character varying': pgVarcharDescriptor,
55
57
  integer: pgIntDescriptor,