@cosmicdrift/kumiko-framework 0.387.0 → 0.388.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 (240) hide show
  1. package/AGENTS.md +6 -1
  2. package/agents/examples/anonymous-access.md +1 -1
  3. package/agents/examples/apex-landing.md +204 -0
  4. package/agents/examples/api-worker-split.md +64 -0
  5. package/agents/examples/app-config.md +14 -0
  6. package/agents/examples/auth-mfa.md +115 -0
  7. package/agents/examples/env-composition.md +28 -1
  8. package/agents/examples/legal-pages.md +1 -1
  9. package/agents/examples/prod-boot-options.md +16 -0
  10. package/agents/examples/rate-limiting.md +48 -0
  11. package/agents/examples/record-detail-layout.md +692 -0
  12. package/agents/examples/seed-migration.md +212 -0
  13. package/agents/examples/wizard-form.md +941 -0
  14. package/agents/examples/writeform-section.md +548 -0
  15. package/agents/features.md +19 -2
  16. package/agents/widgets.md +1 -0
  17. package/dist/api/client-error-route.js +8 -2
  18. package/dist/api/index.d.ts +3 -1
  19. package/dist/api/index.js +3 -1
  20. package/dist/api/request-context.d.ts +1 -1
  21. package/dist/api/request-context.js +1 -1
  22. package/dist/api/server-boot-checks.d.ts +10 -0
  23. package/dist/api/server-boot-checks.js +157 -0
  24. package/dist/api/server-event-dispatcher.d.ts +26 -0
  25. package/dist/api/server-event-dispatcher.js +208 -0
  26. package/dist/api/server-extra-routes.d.ts +11 -0
  27. package/dist/api/server-extra-routes.js +203 -0
  28. package/dist/api/server-middleware.d.ts +20 -0
  29. package/dist/api/server-middleware.js +211 -0
  30. package/dist/api/server-routes.d.ts +19 -0
  31. package/dist/api/server-routes.js +293 -0
  32. package/dist/api/server-websocket-routes.d.ts +4 -0
  33. package/dist/api/server-websocket-routes.js +108 -0
  34. package/dist/api/server-wiring.d.ts +32 -0
  35. package/dist/api/server-wiring.js +236 -0
  36. package/dist/api/server.d.ts +10 -15
  37. package/dist/api/server.js +48 -1251
  38. package/dist/api/session-cookies.js +4 -4
  39. package/dist/api/sse-route.js +16 -12
  40. package/dist/crypto/owner-transition-rekey.d.ts +5 -1
  41. package/dist/crypto/owner-transition-rekey.js +1 -1
  42. package/dist/crypto/pii-field-encryption.d.ts +5 -1
  43. package/dist/crypto/pii-field-encryption.js +3 -1
  44. package/dist/db/aggregate-sql.d.ts +21 -0
  45. package/dist/db/aggregate-sql.js +155 -0
  46. package/dist/db/closed-connection.d.ts +9 -0
  47. package/dist/db/closed-connection.js +95 -0
  48. package/dist/db/dialect.js +1 -1
  49. package/dist/db/distinct-on.d.ts +1 -1
  50. package/dist/db/driver-row-coercion.d.ts +2 -0
  51. package/dist/db/driver-row-coercion.js +186 -0
  52. package/dist/db/driver-value-preparation.d.ts +16 -0
  53. package/dist/db/driver-value-preparation.js +99 -0
  54. package/dist/db/entity-field-encryption.d.ts +1 -0
  55. package/dist/db/entity-field-encryption.js +1 -1
  56. package/dist/db/entity-table-meta.js +103 -110
  57. package/dist/db/event-store-executor-context.d.ts +2 -1
  58. package/dist/db/event-store-executor-read-aggregate.d.ts +8 -0
  59. package/dist/db/event-store-executor-read-aggregate.js +112 -0
  60. package/dist/db/event-store-executor-read-filter.d.ts +31 -0
  61. package/dist/db/event-store-executor-read-filter.js +203 -0
  62. package/dist/db/event-store-executor-read-paging.d.ts +14 -0
  63. package/dist/db/event-store-executor-read-paging.js +99 -0
  64. package/dist/db/event-store-executor-read-rows.d.ts +6 -0
  65. package/dist/db/event-store-executor-read-rows.js +32 -0
  66. package/dist/db/event-store-executor-read-search.d.ts +28 -0
  67. package/dist/db/event-store-executor-read-search.js +221 -0
  68. package/dist/db/event-store-executor-read-sort.d.ts +41 -0
  69. package/dist/db/event-store-executor-read-sort.js +130 -0
  70. package/dist/db/event-store-executor-read-where.d.ts +24 -0
  71. package/dist/db/event-store-executor-read-where.js +81 -0
  72. package/dist/db/event-store-executor-read.d.ts +2 -9
  73. package/dist/db/event-store-executor-read.js +48 -701
  74. package/dist/db/event-store-executor-write-steps.d.ts +20 -0
  75. package/dist/db/event-store-executor-write-steps.js +100 -0
  76. package/dist/db/event-store-executor-write.js +395 -499
  77. package/dist/db/index.d.ts +6 -2
  78. package/dist/db/index.js +5 -1
  79. package/dist/db/list-aggregate-labels.d.ts +14 -0
  80. package/dist/db/list-aggregate-labels.js +26 -0
  81. package/dist/db/migrate-generator.js +71 -86
  82. package/dist/db/migrate-runner.js +4 -1
  83. package/dist/db/query.d.ts +2 -15
  84. package/dist/db/query.js +8 -761
  85. package/dist/db/reference-data.js +6 -1
  86. package/dist/db/table-builder.js +113 -166
  87. package/dist/db/table-info.d.ts +10 -0
  88. package/dist/db/table-info.js +45 -0
  89. package/dist/db/temporal-value-guards.d.ts +6 -0
  90. package/dist/db/temporal-value-guards.js +15 -0
  91. package/dist/db/tenant-db-runner.d.ts +1 -0
  92. package/dist/db/tenant-db-runner.js +3 -0
  93. package/dist/db/tenant-db.js +39 -9
  94. package/dist/db/where-clause.d.ts +6 -0
  95. package/dist/db/where-clause.js +236 -0
  96. package/dist/derivatives/delete-stored-file.js +2 -1
  97. package/dist/engine/boot-validator/boot-check.js +1 -1
  98. package/dist/engine/boot-validator/detail-screens.js +2 -2
  99. package/dist/engine/boot-validator/entity-handler.d.ts +1 -0
  100. package/dist/engine/boot-validator/entity-handler.js +17 -0
  101. package/dist/engine/boot-validator/form-derivations.js +35 -1
  102. package/dist/engine/boot-validator/i18n-keys.js +5 -3
  103. package/dist/engine/boot-validator/index.js +2 -0
  104. package/dist/engine/boot-validator/pii-retention.js +233 -155
  105. package/dist/engine/boot-validator/screens-actions.d.ts +1 -0
  106. package/dist/engine/boot-validator/screens-actions.js +8 -1
  107. package/dist/engine/boot-validator/screens-dashboard.js +2 -5
  108. package/dist/engine/boot-validator/screens-forms.js +2 -6
  109. package/dist/engine/boot-validator/screens-kind-config-edit.d.ts +3 -0
  110. package/dist/engine/boot-validator/screens-kind-config-edit.js +88 -0
  111. package/dist/engine/boot-validator/screens-kind-entity-edit.d.ts +3 -0
  112. package/dist/engine/boot-validator/screens-kind-entity-edit.js +55 -0
  113. package/dist/engine/boot-validator/screens-kind-entity-list.d.ts +3 -0
  114. package/dist/engine/boot-validator/screens-kind-entity-list.js +228 -0
  115. package/dist/engine/boot-validator/screens-kind-projection-detail.d.ts +3 -0
  116. package/dist/engine/boot-validator/screens-kind-projection-detail.js +235 -0
  117. package/dist/engine/boot-validator/screens-kind-projection-list.d.ts +3 -0
  118. package/dist/engine/boot-validator/screens-kind-projection-list.js +52 -0
  119. package/dist/engine/boot-validator/screens-kind-secret-mint.d.ts +3 -0
  120. package/dist/engine/boot-validator/screens-kind-secret-mint.js +28 -0
  121. package/dist/engine/boot-validator/screens-kind-shared.d.ts +37 -0
  122. package/dist/engine/boot-validator/screens-kind-shared.js +142 -0
  123. package/dist/engine/boot-validator/screens-lists.d.ts +2 -1
  124. package/dist/engine/boot-validator/screens-lists.js +12 -9
  125. package/dist/engine/boot-validator/screens.d.ts +0 -2
  126. package/dist/engine/boot-validator/screens.js +99 -860
  127. package/dist/engine/build-app-schema.js +56 -96
  128. package/dist/engine/define-feature.js +9 -2
  129. package/dist/engine/define-handler.js +1 -0
  130. package/dist/engine/entity-handlers.d.ts +13 -0
  131. package/dist/engine/entity-handlers.js +10 -21
  132. package/dist/engine/entity-list-runtime-options.d.ts +8 -0
  133. package/dist/engine/entity-list-runtime-options.js +18 -0
  134. package/dist/engine/feature-ast/extractors/handlers.js +1 -0
  135. package/dist/engine/feature-ast/extractors/round2.js +25 -6
  136. package/dist/engine/feature-ast/patch.js +4 -182
  137. package/dist/engine/feature-ast/pattern-change-schema.d.ts +3 -0
  138. package/dist/engine/feature-ast/pattern-change-schema.js +1 -0
  139. package/dist/engine/feature-ast/pattern-id-matchers.d.ts +7 -0
  140. package/dist/engine/feature-ast/pattern-id-matchers.js +210 -0
  141. package/dist/engine/feature-ast/patterns.d.ts +9 -0
  142. package/dist/engine/feature-ast/patterns.js +43 -51
  143. package/dist/engine/feature-ast/render.js +48 -87
  144. package/dist/engine/feature-entity-handlers.js +3 -0
  145. package/dist/engine/form-derivations.js +18 -7
  146. package/dist/engine/handler-field-references.js +1 -1
  147. package/dist/engine/project-app-schema-for-roles.d.ts +2 -0
  148. package/dist/engine/project-app-schema-for-roles.js +6 -4
  149. package/dist/engine/registry-ingest.js +1 -1
  150. package/dist/engine/schema-builder.js +228 -212
  151. package/dist/engine/screen-helpers.js +1 -1
  152. package/dist/engine/screen-kind.d.ts +7 -0
  153. package/dist/engine/screen-kind.js +1 -0
  154. package/dist/engine/strip-disabled-features-from-app-schema.js +51 -14
  155. package/dist/engine/validate-projection-allowlist.js +2 -1
  156. package/dist/entrypoint/index.d.ts +4 -1
  157. package/dist/entrypoint/index.js +7 -1
  158. package/dist/errors/docs-url.d.ts +1 -1
  159. package/dist/errors/docs-url.js +22 -2
  160. package/dist/es-ops/context.js +1 -1
  161. package/dist/event-store/admin-api.js +1 -1
  162. package/dist/event-store/event-store.js +10 -4
  163. package/dist/files/bind-pending-file-refs.js +3 -3
  164. package/dist/files/create-file-ref-from-bytes.js +4 -1
  165. package/dist/files/file-handle.js +2 -3
  166. package/dist/files/file-routes.js +11 -8
  167. package/dist/files/resolve-content-type.js +43 -0
  168. package/dist/files/upload-ledger.js +7 -0
  169. package/dist/i18n/required-surface-keys.js +182 -206
  170. package/dist/internal/stack-support.d.ts +2 -1
  171. package/dist/internal/stack-support.js +1 -1
  172. package/dist/jobs/job-runner-context.d.ts +32 -0
  173. package/dist/jobs/job-runner-context.js +305 -0
  174. package/dist/jobs/job-runner-execution.d.ts +7 -0
  175. package/dist/jobs/job-runner-execution.js +160 -0
  176. package/dist/jobs/job-runner-gates.d.ts +19 -0
  177. package/dist/jobs/job-runner-gates.js +198 -0
  178. package/dist/jobs/job-runner-helpers.d.ts +17 -3
  179. package/dist/jobs/job-runner-helpers.js +61 -2
  180. package/dist/jobs/job-runner-scope.d.ts +44 -0
  181. package/dist/jobs/job-runner-scope.js +102 -0
  182. package/dist/jobs/job-runner-types.d.ts +12 -0
  183. package/dist/jobs/job-runner.d.ts +0 -1
  184. package/dist/jobs/job-runner.js +47 -657
  185. package/dist/observability/escape-hatch-report.d.ts +3 -0
  186. package/dist/observability/escape-hatch-report.js +12 -1
  187. package/dist/pipeline/cascade-handler.js +165 -138
  188. package/dist/pipeline/dispatch-query.js +10 -3
  189. package/dist/pipeline/dispatch-shared.d.ts +13 -3
  190. package/dist/pipeline/dispatch-shared.js +58 -710
  191. package/dist/pipeline/dispatch-write.js +296 -189
  192. package/dist/pipeline/event-dispatcher-admin.js +2 -1
  193. package/dist/pipeline/event-dispatcher-delivery.js +3 -5
  194. package/dist/pipeline/handler-context-auth.d.ts +9 -0
  195. package/dist/pipeline/handler-context-auth.js +54 -0
  196. package/dist/pipeline/handler-context-config.d.ts +14 -0
  197. package/dist/pipeline/handler-context-config.js +113 -0
  198. package/dist/pipeline/handler-context-db.d.ts +24 -0
  199. package/dist/pipeline/handler-context-db.js +149 -0
  200. package/dist/pipeline/handler-context-dispatch.d.ts +21 -0
  201. package/dist/pipeline/handler-context-dispatch.js +89 -0
  202. package/dist/pipeline/handler-context-events.d.ts +43 -0
  203. package/dist/pipeline/handler-context-events.js +237 -0
  204. package/dist/pipeline/handler-context-notify.d.ts +12 -0
  205. package/dist/pipeline/handler-context-notify.js +55 -0
  206. package/dist/pipeline/handler-context-observability.d.ts +7 -0
  207. package/dist/pipeline/handler-context-observability.js +21 -0
  208. package/dist/pipeline/handler-context-scope.d.ts +48 -0
  209. package/dist/pipeline/handler-context-scope.js +88 -0
  210. package/dist/pipeline/notify-scope.js +12 -1
  211. package/dist/pipeline/pre-handler-gates.js +20 -4
  212. package/dist/pipeline/pre-transaction-reservations.d.ts +1 -0
  213. package/dist/pipeline/pre-transaction-reservations.js +9 -3
  214. package/dist/pipeline/projection-rebuild.js +2 -1
  215. package/dist/schema-cli/apply-command.d.ts +2 -0
  216. package/dist/schema-cli/apply-command.js +97 -0
  217. package/dist/schema-cli/baseline-command.d.ts +2 -0
  218. package/dist/schema-cli/baseline-command.js +33 -0
  219. package/dist/schema-cli/generate-command.d.ts +2 -0
  220. package/dist/schema-cli/generate-command.js +90 -0
  221. package/dist/schema-cli/shared.d.ts +39 -0
  222. package/dist/schema-cli/shared.js +95 -0
  223. package/dist/schema-cli/status-command.d.ts +2 -0
  224. package/dist/schema-cli/status-command.js +73 -0
  225. package/dist/schema-cli/validate-boot-input.d.ts +5 -0
  226. package/dist/schema-cli/validate-boot-input.js +130 -0
  227. package/dist/schema-cli/validate-command.d.ts +2 -0
  228. package/dist/schema-cli/validate-command.js +130 -0
  229. package/dist/schema-cli.d.ts +2 -18
  230. package/dist/schema-cli.js +47 -604
  231. package/dist/search/meilisearch-adapter.js +2 -1
  232. package/dist/upgrade-cli.js +149 -121
  233. package/package.json +4 -4
  234. package/src/changes.json +310 -0
  235. package/src/scripts/codemod/feature-layout.ts +10 -2
  236. package/src/scripts/codemod/object-form.ts +41 -4
  237. package/src/scripts/codemod/package-merge.ts +58 -8
  238. package/src/scripts/codemod/pii-personal-migration.ts +175 -145
  239. package/src/scripts/codemod/testing-injection.ts +122 -42
  240. package/src/scripts/codemod/testing-move.ts +29 -7
@@ -0,0 +1,99 @@
1
+ import { Temporal } from "@cosmicdrift/kumiko-types/temporal";
2
+ import { isPiiCiphertext } from "../crypto/pii-ciphertext-format.js";
3
+ import { SQL_EXPR_BRAND } from "./dialect.js";
4
+ import { isTemporalInstant, isTemporalPlainDate, isTemporalPlainDateTimeLike, } from "./temporal-value-guards.js";
5
+ // Checks the brand Symbol, not the `kind` string — a client-supplied jsonb
6
+ // value can fake `kind: "sql-expr"` over JSON but can never carry a Symbol,
7
+ // so request data can't be smuggled in as a raw SQL literal.
8
+ export function isSqlExpression(v) {
9
+ return typeof v === "object" && v !== null && SQL_EXPR_BRAND in v;
10
+ }
11
+ // A `date` column takes a plain "yyyy-mm-dd" string (or PlainDate.toString())
12
+ // as-is — no Instant detour. Postgres parses a DATE literal without ever
13
+ // consulting the session timezone, so this is TZ-independent by construction
14
+ // (verified empirically — see plainDateFromDriver above for the read side).
15
+ // Compat cushion: code (this field used to be an Instant) may still
16
+ // hand a Temporal.Instant into a where-filter or write on a `date` column —
17
+ // same UTC anchor as plainDateFromDriver's read side, so a filter built with
18
+ // `Temporal.Now.instant()` keeps working instead of binding a raw Instant the
19
+ // driver can't serialize.
20
+ function prepareDateValue(value) {
21
+ if (isTemporalPlainDate(value)) {
22
+ return { kind: "param", sql: "", bound: value.toString() };
23
+ }
24
+ if (isTemporalPlainDateTimeLike(value)) {
25
+ return { kind: "param", sql: "", bound: value.toPlainDate().toString() };
26
+ }
27
+ if (isTemporalInstant(value)) {
28
+ const day = value.toZonedDateTimeISO("UTC").toPlainDate();
29
+ return { kind: "param", sql: "", bound: day.toString() };
30
+ }
31
+ return undefined;
32
+ }
33
+ // structured values bind directly with an ::jsonb cast; JSON.stringify first
34
+ // would produce a JSON-string scalar instead of the structured value.
35
+ export function prepareJsonbValue(value) {
36
+ if (typeof value === "boolean") {
37
+ return { kind: "param", sql: "::text::jsonb", bound: JSON.stringify(value) };
38
+ }
39
+ if (typeof value !== "object" || isTemporalInstant(value)) {
40
+ return undefined;
41
+ }
42
+ if (!Array.isArray(value)) {
43
+ return { kind: "param", sql: "::jsonb", bound: value };
44
+ }
45
+ // All-boolean arrays are inferred as boolean[] by postgres-js; route via text::jsonb.
46
+ if (value.length > 0 && value.every((entry) => typeof entry === "boolean")) {
47
+ return { kind: "param", sql: "::text::jsonb", bound: JSON.stringify(value) };
48
+ }
49
+ return { kind: "param", sql: "::jsonb", bound: value };
50
+ }
51
+ const INTEGER_PG_TYPES = new Set([
52
+ "integer",
53
+ "int4",
54
+ "smallint",
55
+ "int2",
56
+ "bigint",
57
+ "int8",
58
+ "serial",
59
+ "bigserial",
60
+ ]);
61
+ // Postgres rounds sub-millisecond params into timestamptz(3); truncating to ms here keeps the
62
+ // stored instant identical across drivers and equal to what postgres.js sends via Date.
63
+ export function instantToTimestamptzParam(instant) {
64
+ return Temporal.Instant.fromEpochMilliseconds(instant.epochMilliseconds).toString();
65
+ }
66
+ function prepareDriverNeutralScalar(value, pgType) {
67
+ if ((pgType === "timestamptz" || pgType === "timestamptz(3)") && isTemporalInstant(value)) {
68
+ return { kind: "param", sql: "", bound: instantToTimestamptzParam(value) };
69
+ }
70
+ // postgres.js sends text, so Postgres rejects 3.5 for an integer column instead of rounding it.
71
+ if (typeof value === "number" && pgType !== undefined && INTEGER_PG_TYPES.has(pgType)) {
72
+ return { kind: "param", sql: "", bound: String(value) };
73
+ }
74
+ return undefined;
75
+ }
76
+ export function prepareValue(value, pgType) {
77
+ if (isSqlExpression(value)) {
78
+ return { kind: "literal", literal: value.text };
79
+ }
80
+ // A PII-encrypted jsonb field is a JSON string scalar; a raw string bound to
81
+ // ::jsonb would be parsed as JSON text (or double-encoded).
82
+ if (pgType === "jsonb" && isPiiCiphertext(value)) {
83
+ return { kind: "param", sql: "::text::jsonb", bound: JSON.stringify(value) };
84
+ }
85
+ if (pgType === "jsonb" && value !== null) {
86
+ const prepared = prepareJsonbValue(value);
87
+ if (prepared)
88
+ return prepared;
89
+ }
90
+ const scalar = prepareDriverNeutralScalar(value, pgType);
91
+ if (scalar)
92
+ return scalar;
93
+ if (pgType === "date") {
94
+ const prepared = prepareDateValue(value);
95
+ if (prepared)
96
+ return prepared;
97
+ }
98
+ return { kind: "param", sql: "", bound: value };
99
+ }
@@ -3,6 +3,7 @@ import { type Logger } from "../logging/index.ts";
3
3
  import type { EnvelopeCipher } from "../secrets/envelope-cipher.ts";
4
4
  export declare function collectEncryptedFieldNames(entity: EntityDefinition): ReadonlySet<string>;
5
5
  export declare function isSensitiveLabelField(entity: EntityDefinition, field: string): boolean;
6
+ export declare function holdsCiphertextOrSecret(entity: EntityDefinition, field: string): boolean;
6
7
  export declare function isListFilterAllowed(entity: EntityDefinition, field: string, filter: {
7
8
  readonly op: string;
8
9
  readonly value: unknown;
@@ -24,7 +24,7 @@ export function isSensitiveLabelField(entity, field) {
24
24
  }
25
25
  // A `sensitive` (find: "secret") field is never comparable either, even while its value is still
26
26
  // legacy plaintext.
27
- function holdsCiphertextOrSecret(entity, field) {
27
+ export function holdsCiphertextOrSecret(entity, field) {
28
28
  const def = entity.fields[field];
29
29
  if (def === undefined)
30
30
  return false;
@@ -210,30 +210,48 @@ export function resolveTableName(entityName, entity, featureName) {
210
210
  export function deriveEntityTableMeta(entityName, entity, options) {
211
211
  const tableName = resolveTableName(entityName, entity, options?.featureName);
212
212
  const source = options?.source ?? "managed";
213
- if (source === "unmanaged") {
214
- assertUnmanagedTableName(tableName, "deriveEntityTableMeta");
215
- }
216
213
  const idType = entity.idType ?? "uuid";
217
- if (source === "unmanaged" && idType === "serial") {
218
- // Unmanaged direct-write stores bypass validateRecordOwnedSubjects (it only walks
219
- // feature.entities), yet encryptForDirectWrite still keys recordOwned and
220
- // whenAbsent: "self" fields as `record:<entity>:<id>`; forgetSubject requires a UUID,
221
- // so the value could never be shredded.
222
- const recordOwnedField = Object.entries(entity.fields).find(([, field]) => isRecordKeyedField(field));
223
- if (recordOwnedField) {
224
- throw new Error(`deriveEntityTableMeta("${entityName}", { source: "unmanaged" }): field "${recordOwnedField[0]}" is recordOwned (or whenAbsent: "self") but the entity declares idType: "serial" — the record subject key is "record:${entityName}:<id>" and forgetSubject validates that id as a UUID, so this field could never be shredded. Use idType: "uuid", or give the field a different personal stance.`);
225
- }
214
+ if (source === "unmanaged")
215
+ assertUnmanagedRules(entityName, entity, tableName, idType);
216
+ const layout = collectEntityColumns(entity, idType);
217
+ const indexes = [
218
+ { name: `${tableName}_tenant_id_idx`, columns: ["tenant_id"] },
219
+ ...foreignKeyIndexes(tableName, entity, options?.relations, layout.fieldNameToSnake),
220
+ ...blindIndexIndexes(tableName, layout.bidxSnakeByFieldSnake),
221
+ ...declaredIndexes(tableName, entity, layout),
222
+ ];
223
+ const piiSubjectFields = collectPiiEncryptedFields(entity);
224
+ const meta = {
225
+ tableName,
226
+ columns: layout.columns,
227
+ indexes,
228
+ source,
229
+ ...(piiSubjectFields.length > 0 && { piiSubjectFields }),
230
+ ...(entity.tenancy === "global" && { tenancy: "global" }),
231
+ };
232
+ return source === "unmanaged" ? brandWritable(meta) : meta;
233
+ }
234
+ function assertUnmanagedRules(entityName, entity, tableName, idType) {
235
+ assertUnmanagedTableName(tableName, "deriveEntityTableMeta");
236
+ if (idType === "serial")
237
+ assertRecordSubjectsShreddable(entityName, entity);
238
+ }
239
+ // Unmanaged direct-write stores bypass validateRecordOwnedSubjects (it only walks
240
+ // feature.entities), yet encryptForDirectWrite still keys recordOwned and
241
+ // whenAbsent: "self" fields as `record:<entity>:<id>`; forgetSubject requires a UUID,
242
+ // so the value could never be shredded.
243
+ function assertRecordSubjectsShreddable(entityName, entity) {
244
+ const recordOwnedField = Object.entries(entity.fields).find(([, field]) => isRecordKeyedField(field));
245
+ if (recordOwnedField) {
246
+ throw new Error(`deriveEntityTableMeta("${entityName}", { source: "unmanaged" }): field "${recordOwnedField[0]}" is recordOwned (or whenAbsent: "self") but the entity declares idType: "serial" — the record subject key is "record:${entityName}:<id>" and forgetSubject validates that id as a UUID, so this field could never be shredded. Use idType: "uuid", or give the field a different personal stance.`);
226
247
  }
227
- // Base columns first, then user-fields. User-fields with the same
228
- // pg-name as a base-column OVERRIDE the base-column (last-wins, same
229
- // behavior as drizzle's `{ ...base, ...fields }` spread in the table
230
- // builder). Use case: user-session has `tenantId` as a field to enforce
231
- // access control, fileRef has `insertedAt` as a field for sortable/
232
- // filterable marking. The DB column stays the same, only the application
233
- // metadata on the field side changes.
234
- const baseCols = fullBaseColumns(idType, entity.softDelete === true);
248
+ }
249
+ // A field column with a base column's pg name overrides it in place (last wins,
250
+ // like `{ ...base, ...fields }`): user-session declares `tenantId` for access
251
+ // control, fileRef `insertedAt` for sort/filter flags; the DB column is unchanged.
252
+ function collectEntityColumns(entity, idType) {
235
253
  const colByName = new Map();
236
- for (const c of baseCols)
254
+ for (const c of fullBaseColumns(idType, entity.softDelete === true))
237
255
  colByName.set(c.name, c);
238
256
  const fieldNameToSnake = new Map();
239
257
  const bidxSnakeByFieldSnake = new Map();
@@ -242,8 +260,7 @@ export function deriveEntityTableMeta(entityName, entity, options) {
242
260
  for (const c of fieldCols)
243
261
  colByName.set(c.name, c);
244
262
  // Multi-column fields map to their primary column when its name IS the
245
- // field's snake (text+bidx, money+currency) — matches the toSnakeCase
246
- // fallback below, so explicit indexes keep resolving.
263
+ // field's snake (text+bidx, money+currency), so explicit indexes keep resolving.
247
264
  const primary = fieldCols[0];
248
265
  if (primary && primary.name === toSnakeCase(name))
249
266
  fieldNameToSnake.set(name, primary.name);
@@ -251,101 +268,77 @@ export function deriveEntityTableMeta(entityName, entity, options) {
251
268
  if (primary && bidxCol)
252
269
  bidxSnakeByFieldSnake.set(primary.name, bidxCol.name);
253
270
  }
254
- // Preserve base-col order, then any new user-col-names in fields-order.
255
- const columns = [];
256
- const seen = new Set();
257
- for (const c of baseCols) {
258
- const final = colByName.get(c.name);
259
- if (final && !seen.has(final.name)) {
260
- columns.push(final);
261
- seen.add(final.name);
262
- }
263
- }
264
- for (const c of colByName.values()) {
265
- if (!seen.has(c.name)) {
266
- columns.push(c);
267
- seen.add(c.name);
268
- }
269
- }
270
- const indexes = [{ name: `${tableName}_tenant_id_idx`, columns: ["tenant_id"] }];
271
- // FK-Indexes: file/image-Felder + belongsTo-Relations
271
+ // Map insertion order keeps base columns first (overrides stay in place), then new field columns.
272
+ return { columns: [...colByName.values()], fieldNameToSnake, bidxSnakeByFieldSnake };
273
+ }
274
+ function foreignKeyIndexes(tableName, entity, relations, fieldNameToSnake) {
272
275
  const fkSnakeNames = new Set();
273
276
  for (const [name, field] of Object.entries(entity.fields)) {
274
277
  if (field.type === "file" || field.type === "image")
275
278
  fkSnakeNames.add(toSnakeCase(name));
276
279
  }
277
- if (options?.relations) {
278
- for (const rel of Object.values(options.relations)) {
279
- if (rel.type === "belongsTo") {
280
- const snake = fieldNameToSnake.get(rel.foreignKey) ?? toSnakeCase(rel.foreignKey);
281
- fkSnakeNames.add(snake);
282
- }
283
- }
284
- }
285
- for (const snake of fkSnakeNames) {
286
- indexes.push({ name: `${tableName}_${snake}_idx`, columns: [snake] });
287
- }
288
- // lookupable fields: index on the bidx column — the query compiler's
289
- // OR rewrite hits it on every equality lookup.
290
- for (const bidxSnake of bidxSnakeByFieldSnake.values()) {
291
- indexes.push({ name: `${tableName}_${bidxSnake}_idx`, columns: [bidxSnake] });
292
- }
293
- // Explicitly declared indexes (EntityIndexDef). `def.where` is a
294
- // SqlExpression (`sql\`…\`` from @cosmicdrift/kumiko-framework/db) —
295
- // renderable via `.text`. Unknown where-shapes stay needsManualWhere.
296
- for (const def of entity.indexes ?? []) {
297
- const cols = def.columns.map((fieldName) => fieldNameToSnake.get(fieldName) ?? toSnakeCase(fieldName));
298
- const suffix = def.unique === true ? "unique" : "idx";
299
- const indexName = def.name ?? `${tableName}_${cols.join("_")}_${suffix}`;
300
- const explicitWhereSql = sqlExpressionText(def.where);
301
- // Soft-deleted rows keep their value, so a full unique index would
302
- // permanently block reuse; kept in lock-step with table-builder.ts.
303
- const whereSql = explicitWhereSql ??
304
- (def.where === undefined && def.unique === true && entity.softDelete === true
305
- ? SOFT_DELETE_LIVE_ROW_PREDICATE
306
- : undefined);
307
- indexes.push({
308
- name: indexName,
309
- columns: cols,
310
- ...(def.unique === true && { unique: true }),
311
- ...(whereSql !== undefined && { whereSql }),
312
- ...(def.where !== undefined && explicitWhereSql === undefined && { needsManualWhere: true }),
313
- });
314
- // Unique index over lookupable columns: partial bidx counterpart, so
315
- // uniqueness also holds for encrypted rows. The original stays
316
- // for plaintext legacy rows; partial (IS NOT NULL) because bidx is
317
- // NULL for erased/key-less rows.
318
- if (def.unique === true && def.where === undefined) {
319
- const bidxCols = cols.map((c) => bidxSnakeByFieldSnake.get(c) ?? c);
320
- if (bidxCols.some((c, i) => c !== cols[i])) {
321
- const notNullParts = bidxCols
322
- .filter((c, i) => c !== cols[i])
323
- .map((c) => `"${c}" IS NOT NULL`);
324
- // Soft-deleted rows keep their bidx hash — without this, a restored
325
- // row can't reuse a value a soft-deleted sibling still holds
326
- // Kept in lock-step with table-builder.ts.
327
- const whereParts = entity.softDelete === true
328
- ? [...notNullParts, SOFT_DELETE_LIVE_ROW_PREDICATE]
329
- : notNullParts;
330
- indexes.push({
331
- name: `${indexName}_bidx`,
332
- columns: bidxCols,
333
- unique: true,
334
- whereSql: whereParts.join(" AND "),
335
- });
336
- }
280
+ for (const rel of Object.values(relations ?? {})) {
281
+ if (rel.type === "belongsTo") {
282
+ fkSnakeNames.add(fieldNameToSnake.get(rel.foreignKey) ?? toSnakeCase(rel.foreignKey));
337
283
  }
338
284
  }
339
- const piiSubjectFields = collectPiiEncryptedFields(entity);
340
- const meta = {
341
- tableName,
342
- columns,
343
- indexes,
344
- source,
345
- ...(piiSubjectFields.length > 0 && { piiSubjectFields }),
346
- ...(entity.tenancy === "global" && { tenancy: "global" }),
285
+ return [...fkSnakeNames].map((snake) => ({
286
+ name: `${tableName}_${snake}_idx`,
287
+ columns: [snake],
288
+ }));
289
+ }
290
+ // The query compiler's OR rewrite hits the bidx column on every equality lookup.
291
+ function blindIndexIndexes(tableName, bidxSnakeByFieldSnake) {
292
+ return [...bidxSnakeByFieldSnake.values()].map((bidxSnake) => ({
293
+ name: `${tableName}_${bidxSnake}_idx`,
294
+ columns: [bidxSnake],
295
+ }));
296
+ }
297
+ function declaredIndexes(tableName, entity, layout) {
298
+ return (entity.indexes ?? []).flatMap((def) => declaredIndexWithBidxCounterpart(tableName, entity, def, layout));
299
+ }
300
+ // `def.where` is a SqlExpression renderable via `.text`; unknown where-shapes
301
+ // stay needsManualWhere.
302
+ function declaredIndexWithBidxCounterpart(tableName, entity, def, layout) {
303
+ const cols = def.columns.map((fieldName) => layout.fieldNameToSnake.get(fieldName) ?? toSnakeCase(fieldName));
304
+ const suffix = def.unique === true ? "unique" : "idx";
305
+ const indexName = def.name ?? `${tableName}_${cols.join("_")}_${suffix}`;
306
+ const explicitWhereSql = sqlExpressionText(def.where);
307
+ // Soft-deleted rows keep their value, so a full unique index would
308
+ // permanently block reuse; kept in lock-step with table-builder.ts.
309
+ const whereSql = explicitWhereSql ??
310
+ (def.where === undefined && def.unique === true && entity.softDelete === true
311
+ ? SOFT_DELETE_LIVE_ROW_PREDICATE
312
+ : undefined);
313
+ const declared = {
314
+ name: indexName,
315
+ columns: cols,
316
+ ...(def.unique === true && { unique: true }),
317
+ ...(whereSql !== undefined && { whereSql }),
318
+ ...(def.where !== undefined && explicitWhereSql === undefined && { needsManualWhere: true }),
319
+ };
320
+ if (def.unique !== true || def.where !== undefined)
321
+ return [declared];
322
+ const counterpart = bidxUniqueCounterpart(indexName, cols, entity.softDelete === true, layout.bidxSnakeByFieldSnake);
323
+ return counterpart ? [declared, counterpart] : [declared];
324
+ }
325
+ // Unique index over lookupable columns gets a partial bidx counterpart so
326
+ // uniqueness also holds for encrypted rows; IS NOT NULL because bidx is NULL
327
+ // for erased/key-less rows.
328
+ function bidxUniqueCounterpart(indexName, cols, softDelete, bidxSnakeByFieldSnake) {
329
+ const bidxCols = cols.map((c) => bidxSnakeByFieldSnake.get(c) ?? c);
330
+ const notNullParts = bidxCols.filter((c, i) => c !== cols[i]).map((c) => `"${c}" IS NOT NULL`);
331
+ if (notNullParts.length === 0)
332
+ return undefined;
333
+ // Soft-deleted rows keep their bidx hash, so without the live-row predicate a
334
+ // restored row could not reuse a value a soft-deleted sibling still holds.
335
+ const whereParts = softDelete ? [...notNullParts, SOFT_DELETE_LIVE_ROW_PREDICATE] : notNullParts;
336
+ return {
337
+ name: `${indexName}_bidx`,
338
+ columns: bidxCols,
339
+ unique: true,
340
+ whereSql: whereParts.join(" AND "),
347
341
  };
348
- return source === "unmanaged" ? brandWritable(meta) : meta;
349
342
  }
350
343
  function sqlExpressionText(where) {
351
344
  return isSqlExpression(where) ? where.text : undefined;
@@ -1,3 +1,4 @@
1
+ import type { ErasedSubjectHandling } from "../crypto/pii-field-encryption.ts";
1
2
  import type { WhereObject } from "../db/query.ts";
2
3
  import type { EntityDefinition, EntityId, SessionUser, TenantId } from "../engine/types/index.ts";
3
4
  import { type WriteFailure } from "../errors/index.ts";
@@ -62,7 +63,7 @@ export type ExecutorContext = {
62
63
  subjectSource?: Record<string, unknown>;
63
64
  keyTenantId?: TenantId | undefined;
64
65
  ownerFallback?: Record<string, unknown>;
65
- onErasedSubject?: "sentinel";
66
+ onErasedSubject?: ErasedSubjectHandling;
66
67
  }) => Promise<Record<string, unknown>>;
67
68
  readonly decryptForRead: (row: Record<string, unknown>, opts?: DecryptForReadOptions) => Promise<Record<string, unknown>>;
68
69
  readonly ownerTransitionRekeys: (previous: Record<string, unknown>, mergedNew: Record<string, unknown>, submittedChanges: Record<string, unknown>) => Record<string, unknown>;
@@ -0,0 +1,8 @@
1
+ import type { ListAggregatePayload, ListAggregateResult } from "@cosmicdrift/kumiko-types/list-aggregate-types";
2
+ import type { SessionUser } from "../engine/types/index.ts";
3
+ import type { ExecutorContext } from "./event-store-executor-context.ts";
4
+ import { type ListRuntimeOptions, type ListWherePayload } from "./event-store-executor-read-where.ts";
5
+ import type { TenantDb } from "./tenant-db.ts";
6
+ export declare function aggregateList(ctx: ExecutorContext, payload: ListWherePayload & {
7
+ readonly aggregate: ListAggregatePayload;
8
+ }, user: SessionUser, db: TenantDb, runtimeOptions: ListRuntimeOptions | undefined): Promise<ListAggregateResult>;
@@ -0,0 +1,112 @@
1
+ import { ValidationError } from "../errors/index.js";
2
+ import { aggregateKeyOf, decodeListAggregateValue, emptyListAggregateValue, groupedKeyOrderLimitSql, isListAggregateGroupable, isListAggregateMeasureSupported, listAggregateKeySql, listAggregateMeasureSql, } from "./aggregate-sql.js";
3
+ import { holdsCiphertextOrSecret } from "./entity-field-encryption.js";
4
+ import { appendShiftedClause, bindShiftedClause, } from "./event-store-executor-read-filter.js";
5
+ import { buildListWhere, createFieldReadClause, } from "./event-store-executor-read-where.js";
6
+ import { executeRawQueryRead } from "./queries/raw-sql.js";
7
+ import { schemaTableName } from "./table-introspection.js";
8
+ import { tenantDbRunner } from "./tenant-db-runner.js";
9
+ // One group more than the cap is fetched to tell "exactly 200" from "more".
10
+ const MAX_AGGREGATE_GROUPS = 200;
11
+ function rejectAggregate(path, field, hint) {
12
+ throw new ValidationError({
13
+ fields: [
14
+ {
15
+ path,
16
+ code: "field_not_aggregatable",
17
+ i18nKey: "errors.validation.invalid_value",
18
+ params: { field, hint },
19
+ },
20
+ ],
21
+ });
22
+ }
23
+ // Unknown and unreadable fields share one silent outcome so the response cannot
24
+ // confirm that a field exists; only then are the sensitive-field and type gates applied.
25
+ function gateField(ctx, fieldReadClause, field, path) {
26
+ const def = ctx.entity.fields[field];
27
+ if (def === undefined ||
28
+ !Object.hasOwn(ctx.entity.fields, field) ||
29
+ !Object.hasOwn(ctx.table, field))
30
+ return { kind: "silent" };
31
+ const clause = fieldReadClause(field);
32
+ if (clause.kind === "empty")
33
+ return { kind: "silent" };
34
+ if (holdsCiphertextOrSecret(ctx.entity, field)) {
35
+ rejectAggregate(path, field, "Encrypted, personal and secret fields cannot be aggregated.");
36
+ }
37
+ return { kind: "ok", def, clause };
38
+ }
39
+ function planAggregate(ctx, fieldReadClause, aggregate) {
40
+ const groupBy = aggregate.groupBy === undefined
41
+ ? undefined
42
+ : {
43
+ field: aggregate.groupBy,
44
+ gate: gateField(ctx, fieldReadClause, aggregate.groupBy, "aggregate.groupBy"),
45
+ };
46
+ if (groupBy?.gate.kind === "ok") {
47
+ const { def } = groupBy.gate;
48
+ const multiple = def.type === "reference" && def.multiple === true;
49
+ if (!isListAggregateGroupable(def.type, multiple)) {
50
+ rejectAggregate("aggregate.groupBy", groupBy.field, "This field type cannot be grouped by.");
51
+ }
52
+ }
53
+ const measures = aggregate.measures.map((m, i) => {
54
+ const path = `aggregate.measures.${i}`;
55
+ const gate = gateField(ctx, fieldReadClause, m.field, path);
56
+ if (gate.kind === "ok" && !isListAggregateMeasureSupported(m.kind, gate.def.type)) {
57
+ rejectAggregate(path, m.field, `"${m.kind}" is not supported on a ${gate.def.type} field.`);
58
+ }
59
+ return { kind: m.kind, field: m.field, gate };
60
+ });
61
+ return { groupBy, measures };
62
+ }
63
+ function emptyMeasureValues(plan) {
64
+ return plan.measures.map((m) => m.gate.kind === "silent" ? null : emptyListAggregateValue(m.kind));
65
+ }
66
+ function renderSelectList(plan, where) {
67
+ const parts = [];
68
+ if (plan.groupBy?.gate.kind === "ok") {
69
+ const { def, clause } = plan.groupBy.gate;
70
+ parts.push(listAggregateKeySql(def.type, where.colSql(plan.groupBy.field), "k0"));
71
+ // WHERE, not CASE: rows the viewer may not read must not collapse into the real NULL group.
72
+ if (clause.kind === "sql")
73
+ appendShiftedClause(where, clause);
74
+ }
75
+ parts.push("COUNT(*)::float8 AS c");
76
+ for (const [i, m] of plan.measures.entries()) {
77
+ if (m.gate.kind === "silent")
78
+ continue; // skip: an unreadable measure stays out of the SELECT and decodes to null
79
+ const filterSql = m.gate.clause.kind === "sql" ? bindShiftedClause(where.params, m.gate.clause) : undefined;
80
+ parts.push(`${listAggregateMeasureSql(m.kind, m.gate.def.type, where.colSql(m.field), filterSql)} AS m${i}`);
81
+ }
82
+ return parts.join(", ");
83
+ }
84
+ export async function aggregateList(ctx, payload, user, db, runtimeOptions) {
85
+ const plan = planAggregate(ctx, createFieldReadClause(ctx, user), payload.aggregate);
86
+ const grouped = plan.groupBy !== undefined;
87
+ if (plan.groupBy?.gate.kind === "silent")
88
+ return { groups: [] };
89
+ const built = await buildListWhere(ctx, payload, user, db, runtimeOptions);
90
+ if (built.kind === "empty") {
91
+ return {
92
+ groups: grouped ? [] : [{ key: null, count: 0, measures: emptyMeasureValues(plan) }],
93
+ };
94
+ }
95
+ const { where, searchTruncated } = built;
96
+ const selectList = renderSelectList(plan, where);
97
+ const whereText = where.whereSql.length > 0 ? ` WHERE ${where.whereSql.join(" AND ")}` : "";
98
+ const groupText = grouped ? groupedKeyOrderLimitSql(MAX_AGGREGATE_GROUPS + 1) : "";
99
+ const tableName = String(schemaTableName(ctx.table));
100
+ const rows = await executeRawQueryRead(tenantDbRunner(db), `SELECT ${selectList} FROM "${tableName}"${whereText}${groupText}`, where.params);
101
+ const truncated = rows.length > MAX_AGGREGATE_GROUPS;
102
+ const groups = rows.slice(0, MAX_AGGREGATE_GROUPS).map((row) => ({
103
+ key: grouped ? aggregateKeyOf(row["k0"]) : null,
104
+ count: Number(row["c"]),
105
+ measures: plan.measures.map((m, i) => m.gate.kind === "silent" ? null : decodeListAggregateValue(row[`m${i}`])),
106
+ }));
107
+ return {
108
+ groups,
109
+ ...(truncated && { truncated: true }),
110
+ ...(searchTruncated && { searchTruncated: true }),
111
+ };
112
+ }
@@ -0,0 +1,31 @@
1
+ import { type OwnershipClause } from "../engine/ownership.ts";
2
+ import type { EntityDefinition } from "../engine/types/index.ts";
3
+ import { type Table } from "./event-store-executor-context.ts";
4
+ export type ListWhere = {
5
+ readonly whereSql: string[];
6
+ readonly params: unknown[];
7
+ readonly colSql: (field: string) => string;
8
+ };
9
+ export declare function bindShiftedClause(params: unknown[], clause: {
10
+ readonly sqlText: string;
11
+ readonly params: readonly unknown[];
12
+ }): string;
13
+ export declare function appendShiftedClause(where: ListWhere, clause: {
14
+ readonly sqlText: string;
15
+ readonly params: readonly unknown[];
16
+ }): void;
17
+ export declare function bindPlaceholders(params: unknown[], values: readonly unknown[]): string;
18
+ export type ListFilter = {
19
+ readonly field: string;
20
+ readonly op: "eq" | "ne" | "lt" | "gt" | "lte" | "gte" | "in";
21
+ readonly value: unknown;
22
+ };
23
+ export declare function deriveParentNarrowTypes(entity: EntityDefinition, payload: {
24
+ readonly filter?: ListFilter;
25
+ readonly filters?: readonly ListFilter[];
26
+ }): ReadonlySet<string> | undefined;
27
+ export declare function collectListFilters(payload: {
28
+ readonly filter?: ListFilter | undefined;
29
+ readonly filters?: readonly ListFilter[] | undefined;
30
+ }): readonly ListFilter[];
31
+ export declare function applyListFilter(where: ListWhere, f: ListFilter, table: Table, entity: EntityDefinition, fieldReadClause: (field: string) => OwnershipClause): void;