@agentic-kit/db-tools 0.12.46 → 0.13.0

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 (180) hide show
  1. package/README.md +18 -4
  2. package/access/findings.d.ts +93 -0
  3. package/access/findings.js +434 -0
  4. package/access/index.d.ts +5 -0
  5. package/access/index.js +27 -0
  6. package/access/policies.d.ts +142 -0
  7. package/access/policies.js +338 -0
  8. package/access/provisioning.d.ts +121 -0
  9. package/access/provisioning.js +381 -0
  10. package/access/rule-check.d.ts +52 -0
  11. package/access/rule-check.js +307 -0
  12. package/access/words.d.ts +38 -0
  13. package/access/words.js +530 -0
  14. package/app-workspace.d.ts +1 -0
  15. package/app-workspace.js +7 -9
  16. package/blueprint/blueprint-defaults.d.ts +9 -0
  17. package/blueprint/blueprint-defaults.js +77 -0
  18. package/blueprint/blueprint-schema.d.ts +68 -0
  19. package/blueprint/blueprint-schema.js +139 -0
  20. package/blueprint/field-type.d.ts +4 -0
  21. package/blueprint/field-type.js +100 -0
  22. package/blueprint/internal-policies.d.ts +1 -0
  23. package/blueprint/internal-policies.js +10 -0
  24. package/blueprint-construction.d.ts +10 -6
  25. package/blueprint-construction.js +34 -25
  26. package/context.d.ts +15 -0
  27. package/context.js +132 -69
  28. package/db-probe.d.ts +1 -1
  29. package/db-probe.js +19 -4
  30. package/esm/access/findings.d.ts +93 -0
  31. package/esm/access/findings.js +428 -0
  32. package/esm/access/index.d.ts +5 -0
  33. package/esm/access/index.js +5 -0
  34. package/esm/access/policies.d.ts +142 -0
  35. package/esm/access/policies.js +315 -0
  36. package/esm/access/provisioning.d.ts +121 -0
  37. package/esm/access/provisioning.js +375 -0
  38. package/esm/access/rule-check.d.ts +52 -0
  39. package/esm/access/rule-check.js +297 -0
  40. package/esm/access/words.d.ts +38 -0
  41. package/esm/access/words.js +524 -0
  42. package/esm/app-workspace.d.ts +1 -0
  43. package/esm/app-workspace.js +7 -10
  44. package/esm/blueprint/blueprint-defaults.d.ts +9 -0
  45. package/esm/blueprint/blueprint-defaults.js +74 -0
  46. package/esm/blueprint/blueprint-schema.d.ts +68 -0
  47. package/esm/blueprint/blueprint-schema.js +134 -0
  48. package/esm/blueprint/field-type.d.ts +4 -0
  49. package/esm/blueprint/field-type.js +95 -0
  50. package/esm/blueprint/internal-policies.d.ts +1 -0
  51. package/esm/blueprint/internal-policies.js +7 -0
  52. package/esm/blueprint-construction.d.ts +10 -6
  53. package/esm/blueprint-construction.js +32 -24
  54. package/esm/context.d.ts +15 -0
  55. package/esm/context.js +133 -70
  56. package/esm/db-probe.d.ts +1 -1
  57. package/esm/db-probe.js +21 -6
  58. package/esm/failure.d.ts +89 -0
  59. package/esm/failure.js +362 -0
  60. package/esm/gate.d.ts +6 -4
  61. package/esm/gate.js +42 -14
  62. package/esm/graphql-http.d.ts +27 -6
  63. package/esm/graphql-http.js +96 -24
  64. package/esm/host.d.ts +33 -8
  65. package/esm/host.js +42 -2
  66. package/esm/index.d.ts +22 -12
  67. package/esm/index.js +21 -39
  68. package/esm/provision-database/env-file.d.ts +7 -0
  69. package/esm/provision-database/env-file.js +30 -0
  70. package/esm/provision-database/preset-match.js +1 -2
  71. package/esm/provision-database/provision.d.ts +21 -4
  72. package/esm/provision-database/provision.js +330 -85
  73. package/esm/provision-database/request-database.d.ts +22 -3
  74. package/esm/provision-database/request-database.js +46 -13
  75. package/esm/ready.d.ts +20 -0
  76. package/esm/ready.js +45 -0
  77. package/esm/run-codegen/barrels.js +1 -1
  78. package/esm/run-codegen/endpoints.js +5 -6
  79. package/esm/schema-model/normalize.js +1 -1
  80. package/esm/schema-model.js +4 -5
  81. package/esm/schema-resolve.d.ts +50 -0
  82. package/esm/schema-resolve.js +200 -14
  83. package/esm/table-access.d.ts +116 -0
  84. package/esm/table-access.js +418 -0
  85. package/esm/tools/access.d.ts +218 -0
  86. package/esm/tools/access.js +651 -0
  87. package/esm/tools/add-policies.d.ts +44 -12
  88. package/esm/tools/add-policies.js +84 -84
  89. package/esm/tools/add-records.d.ts +23 -2
  90. package/esm/tools/add-records.js +113 -75
  91. package/esm/tools/add-relation-schema.d.ts +5 -5
  92. package/esm/tools/add-relation-schema.js +13 -14
  93. package/esm/tools/add-relation.d.ts +11 -2
  94. package/esm/tools/add-relation.js +68 -31
  95. package/esm/tools/create-api-key.d.ts +18 -4
  96. package/esm/tools/create-api-key.js +105 -54
  97. package/esm/tools/describe-schema.d.ts +5 -7
  98. package/esm/tools/describe-schema.js +41 -118
  99. package/esm/tools/entity-types.d.ts +54 -0
  100. package/esm/tools/entity-types.js +233 -0
  101. package/esm/tools/index.d.ts +26 -0
  102. package/esm/tools/index.js +151 -0
  103. package/esm/tools/mutations.d.ts +36 -7
  104. package/esm/tools/mutations.js +245 -99
  105. package/esm/tools/provision-blueprint.d.ts +25 -1
  106. package/esm/tools/provision-blueprint.js +184 -42
  107. package/esm/tools/provision-database.d.ts +7 -5
  108. package/esm/tools/provision-database.js +31 -17
  109. package/esm/tools/run-codegen.d.ts +27 -4
  110. package/esm/tools/run-codegen.js +143 -76
  111. package/esm/tools/templates.d.ts +30 -7
  112. package/esm/tools/templates.js +166 -95
  113. package/failure.d.ts +89 -0
  114. package/failure.js +375 -0
  115. package/gate.d.ts +6 -4
  116. package/gate.js +41 -13
  117. package/graphql-http.d.ts +27 -6
  118. package/graphql-http.js +105 -25
  119. package/host.d.ts +33 -8
  120. package/host.js +45 -2
  121. package/index.d.ts +22 -12
  122. package/index.js +86 -43
  123. package/package.json +6 -5
  124. package/provision-database/env-file.d.ts +7 -0
  125. package/provision-database/env-file.js +32 -1
  126. package/provision-database/preset-match.js +1 -2
  127. package/provision-database/provision.d.ts +21 -4
  128. package/provision-database/provision.js +327 -81
  129. package/provision-database/request-database.d.ts +22 -3
  130. package/provision-database/request-database.js +49 -14
  131. package/ready.d.ts +20 -0
  132. package/ready.js +52 -0
  133. package/run-codegen/barrels.js +1 -1
  134. package/run-codegen/endpoints.js +5 -6
  135. package/schema-model/normalize.js +4 -4
  136. package/schema-model.js +4 -5
  137. package/schema-resolve.d.ts +50 -0
  138. package/schema-resolve.js +209 -14
  139. package/table-access.d.ts +116 -0
  140. package/table-access.js +437 -0
  141. package/tools/access.d.ts +218 -0
  142. package/tools/access.js +669 -0
  143. package/tools/add-policies.d.ts +44 -12
  144. package/tools/add-policies.js +86 -84
  145. package/tools/add-records.d.ts +23 -2
  146. package/tools/add-records.js +113 -74
  147. package/tools/add-relation-schema.d.ts +5 -5
  148. package/tools/add-relation-schema.js +13 -14
  149. package/tools/add-relation.d.ts +11 -2
  150. package/tools/add-relation.js +69 -31
  151. package/tools/create-api-key.d.ts +18 -4
  152. package/tools/create-api-key.js +104 -53
  153. package/tools/describe-schema.d.ts +5 -7
  154. package/tools/describe-schema.js +40 -117
  155. package/tools/entity-types.d.ts +54 -0
  156. package/tools/entity-types.js +240 -0
  157. package/tools/index.d.ts +26 -0
  158. package/tools/index.js +154 -0
  159. package/tools/mutations.d.ts +36 -7
  160. package/tools/mutations.js +252 -101
  161. package/tools/provision-blueprint.d.ts +25 -1
  162. package/tools/provision-blueprint.js +186 -43
  163. package/tools/provision-database.d.ts +7 -5
  164. package/tools/provision-database.js +33 -18
  165. package/tools/run-codegen.d.ts +27 -4
  166. package/tools/run-codegen.js +141 -73
  167. package/tools/templates.d.ts +30 -7
  168. package/tools/templates.js +167 -94
  169. package/esm/policy/add-policies-to-table.d.ts +0 -26
  170. package/esm/policy/add-policies-to-table.js +0 -66
  171. package/esm/policy/provision-helpers.d.ts +0 -38
  172. package/esm/policy/provision-helpers.js +0 -50
  173. package/esm/tools/manage-entity-types.d.ts +0 -40
  174. package/esm/tools/manage-entity-types.js +0 -201
  175. package/policy/add-policies-to-table.d.ts +0 -26
  176. package/policy/add-policies-to-table.js +0 -71
  177. package/policy/provision-helpers.d.ts +0 -38
  178. package/policy/provision-helpers.js +0 -58
  179. package/tools/manage-entity-types.d.ts +0 -40
  180. package/tools/manage-entity-types.js +0 -206
@@ -0,0 +1,381 @@
1
+ "use strict";
2
+ // What a rule needs on its table: the Data node that adds the columns an Authz node reads, the columns each Data node
3
+ // adds (with their defaults, as the backend's generators create them), and which rows such a default fills.
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.ROW_ID = exports.ADDED_FIELDS = exports.TABLE_NEEDS = void 0;
6
+ exports.defaultFills = defaultFills;
7
+ exports.fieldsAddedByNodes = fieldsAddedByNodes;
8
+ exports.dataNodesForPolicies = dataNodesForPolicies;
9
+ const node_type_registry_1 = require("node-type-registry");
10
+ const policies_1 = require("./policies");
11
+ exports.TABLE_NEEDS = {
12
+ AuthzAllowAll: { kind: 'no-fields' },
13
+ AuthzAppMemberOwner: {
14
+ kind: 'has-module',
15
+ data_node: { type: 'DataDirectOwner', columns: { owner_field: { param: 'owner_field_name' } } }
16
+ },
17
+ AuthzAppMembership: { kind: 'no-fields' },
18
+ AuthzColumnSecurity: { kind: 'needs-fields' },
19
+ AuthzComposite: { kind: 'mixed' },
20
+ AuthzDatabaseClaim: { kind: 'needs-fields' },
21
+ AuthzDenyAll: { kind: 'no-fields' },
22
+ AuthzDirectOwner: {
23
+ kind: 'has-module',
24
+ data_node: { type: 'DataDirectOwner', columns: { entity_field: { param: 'owner_field_name' } } }
25
+ },
26
+ AuthzDirectOwnerAny: { kind: 'needs-fields' },
27
+ AuthzEntityMembership: {
28
+ kind: 'has-module',
29
+ data_node: { type: 'DataEntityMembership', columns: { entity_field: { param: 'entity_field_name' } } }
30
+ },
31
+ AuthzFilePath: { kind: 'needs-table' },
32
+ AuthzHumanOnly: { kind: 'no-fields' },
33
+ AuthzMemberList: { kind: 'needs-fields' },
34
+ AuthzMemberOwner: {
35
+ kind: 'has-module',
36
+ data_node: {
37
+ type: 'DataOwnershipInEntity',
38
+ columns: { owner_field: { name: 'owner_id' }, entity_field: { name: 'entity_id' } }
39
+ }
40
+ },
41
+ AuthzNotReadOnly: {
42
+ kind: 'has-module',
43
+ data_node: { type: 'DataEntityMembership', columns: { entity_field: { param: 'entity_field_name' } } }
44
+ },
45
+ AuthzOrgHierarchy: {
46
+ kind: 'has-module',
47
+ data_node: {
48
+ type: 'DataOwnershipInEntity',
49
+ columns: { anchor_field: { name: 'owner_id' }, entity_field: { name: 'entity_id' } }
50
+ }
51
+ },
52
+ AuthzPeerOwnership: { kind: 'needs-fields' },
53
+ AuthzPublishable: {
54
+ kind: 'has-module',
55
+ data_node: {
56
+ type: 'DataPublishable',
57
+ columns: {
58
+ is_published_field: { param: 'is_published_field_name' },
59
+ published_at_field: { param: 'published_at_field_name' }
60
+ }
61
+ }
62
+ },
63
+ AuthzRelatedEntityMembership: { kind: 'needs-table' },
64
+ AuthzRelatedMemberList: { kind: 'needs-table' },
65
+ AuthzRelatedMemberOwner: { kind: 'needs-table' },
66
+ AuthzRelatedPeerOwnership: { kind: 'needs-table' },
67
+ AuthzSystemOnly: { kind: 'no-fields' },
68
+ AuthzTemporal: { kind: 'needs-fields' },
69
+ AuthzValueAllowed: { kind: 'needs-fields' },
70
+ AuthzValueExists: { kind: 'needs-table' },
71
+ AuthzValueMatch: { kind: 'needs-table' }
72
+ };
73
+ /**
74
+ * The columns each node of the data module adds. `[]` when it adds none (a trigger, an index, a CHECK constraint, or
75
+ * columns on another table); `'unknown'` where the backend's answer has not been checked.
76
+ */
77
+ exports.ADDED_FIELDS = {
78
+ CheckGreaterThan: [],
79
+ CheckLessThan: [],
80
+ CheckNotEqual: [],
81
+ CheckOneOf: [],
82
+ CheckOwnedRelation: [],
83
+ DataArchivable: [
84
+ { name: 'is_archived', type: 'boolean', required: true, default: { value: false } },
85
+ { name: 'archived_at', type: 'timestamptz', required: false, default: null }
86
+ ],
87
+ DataBulk: 'unknown',
88
+ DataCapabilities: [{ name: { param: 'field' }, type: 'bit', required: true, default: 'unknown' }],
89
+ DataCompositeField: [{ name: { param: 'target' }, type: 'text', required: false, default: null }],
90
+ DataDenormalized: 'unknown',
91
+ DataDirectOwner: [
92
+ {
93
+ name: { param: 'owner_field_name' },
94
+ type: 'uuid',
95
+ required: true,
96
+ default: { function: 'current_user_id', schema: 'jwt_public' }
97
+ }
98
+ ],
99
+ DataEntityMembership: [{ name: { param: 'entity_field_name' }, type: 'uuid', required: true, default: null }],
100
+ DataForceCurrentUser: [],
101
+ DataGenerated: [
102
+ {
103
+ name: { param: 'target' },
104
+ type: { param: 'type' },
105
+ required: { param: 'is_required' },
106
+ default: { generated: 'expression' }
107
+ }
108
+ ],
109
+ DataHistory: [],
110
+ DataI18n: [],
111
+ DataId: [{ name: { param: 'field_name' }, type: 'uuid', required: true, default: { function: 'uuidv7' } }],
112
+ DataIdentity: [
113
+ {
114
+ name: { param: 'target' },
115
+ type: { param: 'type' },
116
+ required: { param: 'is_required' },
117
+ default: { generated: 'identity' }
118
+ }
119
+ ],
120
+ DataImmutableFields: [],
121
+ DataInflection: [],
122
+ DataInheritFromParent: [],
123
+ DataJsonb: [
124
+ {
125
+ name: { param: 'field_name' },
126
+ type: 'jsonb',
127
+ required: { param: 'is_required' },
128
+ default: { param: 'default_value' }
129
+ }
130
+ ],
131
+ DataLock: [
132
+ {
133
+ name: { param: 'lock_field' },
134
+ type: 'boolean',
135
+ required: true,
136
+ default: { value: { param: 'default_locked' } }
137
+ }
138
+ ],
139
+ DataMemberOwner: [
140
+ {
141
+ name: { param: 'owner_field_name' },
142
+ type: 'uuid',
143
+ required: true,
144
+ default: { function: 'current_user_id', schema: 'jwt_public' }
145
+ },
146
+ { name: { param: 'entity_field_name' }, type: 'uuid', required: true, default: null }
147
+ ],
148
+ DataOwnedFields: [],
149
+ DataOwnershipInEntity: [
150
+ {
151
+ name: 'owner_id',
152
+ type: 'uuid',
153
+ required: true,
154
+ default: { function: 'current_user_id', schema: 'jwt_public' }
155
+ },
156
+ { name: 'entity_id', type: 'uuid', required: true, default: null }
157
+ ],
158
+ DataPeoplestamps: [
159
+ { name: { param: 'created_by_field' }, type: 'uuid', required: false, default: null },
160
+ {
161
+ name: { param: 'updated_by_field' },
162
+ type: 'uuid',
163
+ required: false,
164
+ default: null,
165
+ unless: 'append_only'
166
+ }
167
+ ],
168
+ DataPrincipalstamps: [
169
+ { name: { param: 'created_by_principal_field' }, type: 'uuid', required: false, default: null },
170
+ {
171
+ name: { param: 'updated_by_principal_field' },
172
+ type: 'uuid',
173
+ required: false,
174
+ default: null,
175
+ unless: 'append_only'
176
+ }
177
+ ],
178
+ DataPublishable: [
179
+ {
180
+ name: { param: 'is_published_field_name' },
181
+ type: 'boolean',
182
+ required: true,
183
+ default: { value: false }
184
+ },
185
+ { name: { param: 'published_at_field_name' }, type: 'timestamptz', required: false, default: null }
186
+ ],
187
+ DataRealtime: [],
188
+ DataSlug: [],
189
+ DataSoftDelete: [
190
+ { name: 'deleted_at', type: 'timestamptz', required: false, default: null },
191
+ { name: 'is_deleted', type: 'boolean', required: true, default: { value: false } }
192
+ ],
193
+ DataStatusField: [
194
+ {
195
+ name: { param: 'field_name' },
196
+ type: { param: 'type' },
197
+ required: { param: 'is_required' },
198
+ default: { param: 'default_value' }
199
+ }
200
+ ],
201
+ DataTags: [
202
+ {
203
+ name: { param: 'field_name' },
204
+ type: 'citext[]',
205
+ required: { param: 'is_required' },
206
+ default: { param: 'default_value' }
207
+ }
208
+ ],
209
+ DataTimestamps: [
210
+ { name: 'created_at', type: 'timestamptz', required: true, default: { function: 'now' } },
211
+ { name: 'updated_at', type: 'timestamptz', required: true, default: { function: 'now' } }
212
+ ],
213
+ SearchBm25: [],
214
+ SearchFullText: [{ name: { param: 'field_name' }, type: 'tsvector', required: false, default: null }],
215
+ SearchSpatial: 'unknown',
216
+ SearchSpatialAggregate: 'unknown',
217
+ SearchTrgm: [],
218
+ SearchUnified: 'unknown',
219
+ SearchVector: 'unknown',
220
+ TableOrganizationSettings: 'unknown',
221
+ TableUserProfiles: 'unknown',
222
+ TableUserSettings: 'unknown'
223
+ };
224
+ const NODES = new Map(node_type_registry_1.allNodeTypes.map((node) => [node.name, node]));
225
+ function withDefaults(node, given) {
226
+ const values = {};
227
+ for (const [param, schema] of Object.entries(node.parameter_schema.properties ?? {})) {
228
+ if (schema.default !== undefined)
229
+ values[param] = schema.default;
230
+ }
231
+ return { ...values, ...given };
232
+ }
233
+ function typeName(value) {
234
+ if (typeof value === 'string')
235
+ return value;
236
+ if (!(0, policies_1.isObject)(value) || typeof value.name !== 'string')
237
+ return undefined;
238
+ const { name, array_dimensions } = value;
239
+ return name + '[]'.repeat(array_dimensions ?? 0);
240
+ }
241
+ function defaultOf(added, values) {
242
+ if (added === null)
243
+ return null;
244
+ if (added === 'unknown')
245
+ return 'unknown';
246
+ if ('param' in added) {
247
+ const given = values[added.param];
248
+ if (given === undefined || given === null)
249
+ return null;
250
+ return typeof given === 'object' ? given : { value: given };
251
+ }
252
+ if ('value' in added) {
253
+ const value = added.value;
254
+ return { value: typeof value === 'object' ? values[value.param] : value };
255
+ }
256
+ return added;
257
+ }
258
+ // A value, or an operator over values, is a constant; a function or a keyword is not.
259
+ function constant(value) {
260
+ if (value === null || typeof value !== 'object')
261
+ return true;
262
+ const entry = value;
263
+ if (entry.function !== undefined || entry.sql_keyword !== undefined)
264
+ return false;
265
+ if (entry.operator !== undefined)
266
+ return constant(entry.left) && constant(entry.right);
267
+ return 'value' in entry;
268
+ }
269
+ /**
270
+ * Which rows a column's default fills when the backend adds the column: a constant rides ADD COLUMN and fills the rows
271
+ * the table has; any other default is set after SET NOT NULL, so it fills new rows only, and a required column with one
272
+ * fails on a table that has rows. An identity or generated column fills every row.
273
+ */
274
+ function defaultFills(value) {
275
+ if (value === 'unknown')
276
+ return { existing_rows: 'unknown', new_rows: 'unknown' };
277
+ if (value === null)
278
+ return { existing_rows: false, new_rows: false };
279
+ if ('generated' in value)
280
+ return { existing_rows: true, new_rows: true };
281
+ return { existing_rows: constant(value), new_rows: true };
282
+ }
283
+ /**
284
+ * The columns a list of blueprint nodes adds to its table, from `ADDED_FIELDS` and each node's data (registry defaults
285
+ * apply). A node whose columns are not known, or whose data leaves a column unnamed, is listed in `unknown` instead.
286
+ */
287
+ function fieldsAddedByNodes(list) {
288
+ const fields = [];
289
+ const unknown = [];
290
+ for (const entry of list) {
291
+ const type = typeof entry === 'string' ? entry : entry.$type;
292
+ const node = NODES.get(type);
293
+ const added = exports.ADDED_FIELDS[type];
294
+ if (!node || !added || added === 'unknown') {
295
+ unknown.push(type);
296
+ continue;
297
+ }
298
+ const values = withDefaults(node, typeof entry === 'string' ? {} : (entry.data ?? {}));
299
+ const own = [];
300
+ for (const field of added) {
301
+ if (field.unless && values[field.unless] === true)
302
+ continue;
303
+ const name = typeof field.name === 'string' ? field.name : values[field.name.param];
304
+ const kind = typeof field.type === 'string' ? field.type : typeName(values[field.type.param]);
305
+ const required = typeof field.required === 'boolean' ? field.required : values[field.required.param] === true;
306
+ if (typeof name !== 'string' || name === '' || !kind)
307
+ break;
308
+ own.push({ name, type: kind, nullable: !required, default: defaultOf(field.default, values), node: type });
309
+ }
310
+ if (own.length < added.filter((field) => !(field.unless && values[field.unless] === true)).length)
311
+ unknown.push(type);
312
+ else
313
+ fields.push(...own.filter((column) => !fields.some((known) => known.name === column.name)));
314
+ }
315
+ return { fields, unknown };
316
+ }
317
+ /**
318
+ * The table's own id: DataId's column. A rule that reads it as its owner or team column reads the row as the person
319
+ * (the users table: each row is a person, and their own organization), and no Data node makes that column.
320
+ */
321
+ exports.ROW_ID = 'id';
322
+ /**
323
+ * The Data nodes a table needs for its rules to have their columns: each rule's Data node with its column names, nested
324
+ * nodes of a combination included. A node is left out when another one adds all its columns, and when the rule reads the
325
+ * row's own id (`ROW_ID`), which no Data node makes. A rule that names a column its Data node adds under another name
326
+ * (the node's columns have fixed names) needs no node when the table has every column the rule reads (`columns`); else
327
+ * it is a problem, not a guess, since the node would add columns the rule does not read.
328
+ */
329
+ function dataNodesForPolicies(policies, columns = new Set()) {
330
+ const wanted = [];
331
+ const problems = [];
332
+ const nested = (value) => {
333
+ if (Array.isArray(value))
334
+ return value.forEach(nested);
335
+ if (!(0, policies_1.isObject)(value))
336
+ return;
337
+ const keys = Object.keys(value);
338
+ if (keys.length === 1 && NODES.get(keys[0])?.category === 'authz' && (0, policies_1.isObject)(value[keys[0]])) {
339
+ return visit(keys[0], value[keys[0]]);
340
+ }
341
+ Object.values(value).forEach(nested);
342
+ };
343
+ const visit = (policyType, policyData) => {
344
+ const effective = (0, policies_1.effectivePolicy)(policyType, policyData ?? {});
345
+ const node = NODES.get(effective.type);
346
+ const needs = exports.TABLE_NEEDS[effective.type];
347
+ if (needs?.kind === 'mixed')
348
+ return nested(effective.data);
349
+ const need = needs?.data_node;
350
+ if (!node || !need)
351
+ return;
352
+ const values = withDefaults(node, effective.data);
353
+ if (Object.keys(need.columns).some((param) => values[param] === exports.ROW_ID))
354
+ return;
355
+ const named = Object.entries(need.columns).map(([param, how]) => ({ param, how, column: values[param] }));
356
+ const unnamed = named.find(({ column }) => typeof column !== 'string' || column === '');
357
+ if (unnamed) {
358
+ problems.push(`${effective.type} names no column in ${unnamed.param}; add "${unnamed.param}" to the rule's data.`);
359
+ return;
360
+ }
361
+ const renamed = named.find(({ how, column }) => 'name' in how && column !== how.name);
362
+ if (renamed && 'name' in renamed.how) {
363
+ const missing = [...new Set(named.map(({ column }) => column).filter((column) => !columns.has(column)))];
364
+ if (missing.length === 0)
365
+ return;
366
+ problems.push(`${effective.type} names the column "${renamed.column}" in ${renamed.param}, but ${need.type} always adds "${renamed.how.name}"; ` +
367
+ `add ${missing.map((column) => `"${column}"`).join(' and ')} to the table first, or use "${renamed.how.name}".`);
368
+ return;
369
+ }
370
+ wanted.push({ $type: need.type, data: Object.fromEntries(named.flatMap(({ how, column }) => ('param' in how ? [[how.param, column]] : []))) });
371
+ };
372
+ for (const entry of policies)
373
+ visit(entry.type, entry.data);
374
+ const distinct = wanted.filter((entry, i) => wanted.findIndex((other) => JSON.stringify(other) === JSON.stringify(entry)) === i);
375
+ const names = distinct.map((entry) => fieldsAddedByNodes([entry]).fields.map((column) => column.name));
376
+ const kept = distinct.filter((_entry, i) => {
377
+ const mine = names[i];
378
+ return !names.some((other, j) => j !== i && mine.every((name) => other.includes(name)) && (other.length > mine.length || j < i));
379
+ });
380
+ return { nodes: kept, problems };
381
+ }
@@ -0,0 +1,52 @@
1
+ import { type ValidateFunction } from 'ajv';
2
+ import { type RuleProblem } from './policies';
3
+ /**
4
+ * The roles a rule can name: the request roles of the GraphQL API (`authenticated`, `anonymous`), the function lane's
5
+ * floor (`authenticated_client`), and PostgreSQL's `public`, which is every role. The backend creates the policy for
6
+ * the role as given, so another name fails there. An API may name its own request roles, so a caller can pass them.
7
+ */
8
+ export declare const POLICY_ROLES: readonly ["authenticated", "anonymous", "authenticated_client", "public"];
9
+ /**
10
+ * A copy to validate: the validator fills registry defaults into what it checks, and a JSON copy shares no object
11
+ * between two places, so a default filled in one rule never lands in another.
12
+ */
13
+ export declare const jsonCopy: <T>(value: T) => T;
14
+ /**
15
+ * A validator of one definition of the registry's blueprint JSON Schema (`BlueprintDefinition`, `AuthzEntityMembershipParams`,
16
+ * ...), with the formats the schema names. Registry defaults fill missing keys before the required keys are checked, as the
17
+ * backend does, so a caller validates a copy.
18
+ */
19
+ export declare function blueprintValidator(definition: string): ValidateFunction;
20
+ /** The keys the backend reads from a member rule's data that NOT_SUPPORTED keeps out of a rule you write. */
21
+ export declare const READ_BUT_NOT_SUPPORTED: readonly string[];
22
+ /** A table a rule names: by id, or by schema and name. */
23
+ export type TableRef = {
24
+ id?: string;
25
+ schema?: string;
26
+ name?: string;
27
+ };
28
+ /**
29
+ * What a rule is checked with: the role it names and the roles it may name (`POLICY_ROLES` when not given), whether a
30
+ * blueprint builds it (only a blueprint resolves a module's table), and the columns of another table it names, when
31
+ * the caller knows them.
32
+ */
33
+ export type RuleContext = {
34
+ role?: string;
35
+ roles?: readonly string[];
36
+ inBlueprint?: boolean;
37
+ columnsOf?: (table: TableRef) => readonly string[] | undefined;
38
+ };
39
+ /**
40
+ * The rule shapes these tools do not support, which the parameter schema cannot say: a module's table outside a
41
+ * blueprint, and the shapes the backend builds into a weaker check than the data says. A stored rule of such a shape
42
+ * is one its words cannot fully state.
43
+ */
44
+ export declare function unsupportedShapes(type: string, data: Record<string, unknown>, context: RuleContext): RuleProblem[];
45
+ /**
46
+ * The problems of one rule a caller writes for Authz node `type`: its data against the node's closed parameter_schema
47
+ * (nested Authz nodes included), the rule shapes these tools do not support, and the role it names. An empty list
48
+ * means the backend builds the rule as written. A key the backend or the platform sets is `stored-only-key`, a key
49
+ * these tools do not support is `not-supported`, any other key the node does not take is `unknown-key`, and a part
50
+ * that is not an Authz node or AND, OR, NOT is refused.
51
+ */
52
+ export declare function checkRule(type: string, data: unknown, context?: RuleContext): RuleProblem[];