@jarenjs/db 0.34.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 (83) hide show
  1. package/ARCHITECTURE.md +397 -0
  2. package/README.md +218 -0
  3. package/dist/types/algebra.d.ts +133 -0
  4. package/dist/types/app.d.ts +49 -0
  5. package/dist/types/capture.d.ts +85 -0
  6. package/dist/types/cli.d.ts +2 -0
  7. package/dist/types/dag-job.d.ts +40 -0
  8. package/dist/types/ddl.d.ts +170 -0
  9. package/dist/types/dialect.d.ts +130 -0
  10. package/dist/types/dialects/sqlite.d.ts +9 -0
  11. package/dist/types/driver.d.ts +128 -0
  12. package/dist/types/drivers/bun.d.ts +47 -0
  13. package/dist/types/drivers/node.d.ts +37 -0
  14. package/dist/types/drivers/wasm.d.ts +65 -0
  15. package/dist/types/emit-model.d.ts +44 -0
  16. package/dist/types/emit.d.ts +72 -0
  17. package/dist/types/entity.d.ts +23 -0
  18. package/dist/types/errors.d.ts +165 -0
  19. package/dist/types/graph.d.ts +28 -0
  20. package/dist/types/index.d.ts +35 -0
  21. package/dist/types/jobs.d.ts +134 -0
  22. package/dist/types/live.d.ts +62 -0
  23. package/dist/types/migrate.d.ts +163 -0
  24. package/dist/types/model.d.ts +36 -0
  25. package/dist/types/patch-sql.d.ts +37 -0
  26. package/dist/types/plan.d.ts +119 -0
  27. package/dist/types/profile.d.ts +80 -0
  28. package/dist/types/query.d.ts +100 -0
  29. package/dist/types/residual.d.ts +50 -0
  30. package/dist/types/store.d.ts +53 -0
  31. package/dist/types/tracker.d.ts +43 -0
  32. package/dist/types/typed.d.ts +15 -0
  33. package/dist/types/types.d.ts +26 -0
  34. package/dist/types/udf.d.ts +70 -0
  35. package/dist/types/window.d.ts +52 -0
  36. package/docs/JOBS-FORMAT.md +218 -0
  37. package/docs/LIVE-FORMAT.md +348 -0
  38. package/docs/MIGRATION-FORMAT.md +302 -0
  39. package/docs/MODEL-FORMAT.md +928 -0
  40. package/package.json +81 -0
  41. package/schemas/jaren-migration.draft-07.schema.json +144 -0
  42. package/schemas/jaren-migration.schema.json +144 -0
  43. package/schemas/jaren-model.draft-07.schema.json +149 -0
  44. package/schemas/jaren-model.schema.json +149 -0
  45. package/src/algebra.js +105 -0
  46. package/src/app.js +108 -0
  47. package/src/capture.js +584 -0
  48. package/src/cli.js +264 -0
  49. package/src/dag-job.js +86 -0
  50. package/src/ddl.js +588 -0
  51. package/src/dialect.js +297 -0
  52. package/src/dialects/sqlite.js +175 -0
  53. package/src/driver.js +419 -0
  54. package/src/drivers/bun.js +101 -0
  55. package/src/drivers/node.js +93 -0
  56. package/src/drivers/wasm.js +178 -0
  57. package/src/emit-model.js +208 -0
  58. package/src/emit.js +393 -0
  59. package/src/entity.js +367 -0
  60. package/src/errors.js +173 -0
  61. package/src/graph.js +101 -0
  62. package/src/index.js +64 -0
  63. package/src/jobs.js +507 -0
  64. package/src/live.js +899 -0
  65. package/src/migrate.js +1411 -0
  66. package/src/model.js +476 -0
  67. package/src/patch-sql.js +150 -0
  68. package/src/plan.js +1038 -0
  69. package/src/profile.js +131 -0
  70. package/src/query.js +1010 -0
  71. package/src/residual.js +91 -0
  72. package/src/store.js +1422 -0
  73. package/src/tracker.js +776 -0
  74. package/src/typed.js +19 -0
  75. package/src/types.js +36 -0
  76. package/src/udf.js +132 -0
  77. package/src/window.js +125 -0
  78. package/types/app.d.ts +36 -0
  79. package/types/bun.d.ts +9 -0
  80. package/types/index.d.ts +592 -0
  81. package/types/node.d.ts +15 -0
  82. package/types/typed.d.ts +108 -0
  83. package/types/wasm.d.ts +5 -0
@@ -0,0 +1,91 @@
1
+ //@ts-check
2
+ /**
3
+ * @file Residual compilation: the part of a query that stays in
4
+ * JavaScript is a REAL compiled Jaren query — the same engine, the
5
+ * same semantics — never a reimplementation.
6
+ *
7
+ * Two modes (ARCHITECTURE.md):
8
+ *
9
+ * - `set` — the whole original document compiled once, run over the
10
+ * materialized candidate array. Re-applying pushed conjuncts is
11
+ * idempotent, so SQL-side narrowing never changes the answer.
12
+ * - `row` — only the projection stayed behind: each fetched row runs
13
+ * `{ $for: { it: '$[*]' }, $return: [ <ret> ] }` over the one-row
14
+ * array; the array wrapper packs the item sequence so an
15
+ * array-VALUED item stays unambiguous, and the per-row results
16
+ * concatenate in row order (streamable).
17
+ */
18
+
19
+ import { compileJsonQuery } from '@jarenjs/json/query';
20
+
21
+ /**
22
+ * The compile options for a residual: the profile's engine limits plus
23
+ * the store's registered operators (Ring 2 — a registered operator runs
24
+ * in the residual, so the residual compilation must carry its
25
+ * `{ functions, extensions }` or it would fail `JQ0002`/`JQ0010`).
26
+ * Returns `undefined` when neither is present, so a store opened with no
27
+ * profile and no registry compiles byte-identically to before.
28
+ * @param {any} limits
29
+ * @param {{ functions?: any, extensions?: any } | null} [operators]
30
+ * @returns {any}
31
+ */
32
+ function residualOptions(limits, operators) {
33
+ const functions = operators?.functions;
34
+ const extensions = operators?.extensions;
35
+ if (limits === undefined && functions === undefined && extensions === undefined)
36
+ return undefined;
37
+ /** @type {any} */
38
+ const options = {};
39
+ if (limits !== undefined) options.limits = limits;
40
+ if (functions !== undefined) options.functions = functions;
41
+ if (extensions !== undefined) options.extensions = extensions;
42
+ return options;
43
+ }
44
+
45
+ /**
46
+ * Compile the whole document for set-mode evaluation. A profile's
47
+ * engine limits ride into the compilation so the JavaScript portion is
48
+ * bounded by the engine's own enforcement.
49
+ * @param {any} document
50
+ * @param {any} [limits]
51
+ * @param {{ functions?: any, extensions?: any } | null} [operators] -
52
+ * the store's registered operators, so the residual can evaluate them
53
+ * @returns {(candidates: any[], externals: any) => any}
54
+ */
55
+ export function compileSetResidual(document, limits, operators) {
56
+ const compiled = compileJsonQuery(document, residualOptions(limits, operators));
57
+ return (candidates, externals) => compiled(candidates, externals);
58
+ }
59
+
60
+ /**
61
+ * Compile the per-row projection for row-mode evaluation.
62
+ * @param {any} returnExpression - The document's raw `$return` value
63
+ * @param {any} [limits]
64
+ * @param {{ functions?: any, extensions?: any } | null} [operators]
65
+ * @returns {(row: any, externals: any) => any[]} the row's items
66
+ */
67
+ export function compileRowResidual(returnExpression, limits, operators) {
68
+ const compiled = compileJsonQuery({
69
+ $for: { it: '$[*]' },
70
+ $return: [returnExpression],
71
+ }, residualOptions(limits, operators));
72
+ return (row, externals) => {
73
+ const packed = compiled([row], externals);
74
+ // one binding → exactly one packed array of that row's items
75
+ return /** @type {any[]} */ (packed);
76
+ };
77
+ }
78
+
79
+ /**
80
+ * Map a flat item array onto the engine's result shape: an empty
81
+ * sequence is `undefined`, a singleton is the item, anything longer is
82
+ * the array (probed engine behaviour, pinned by the differential
83
+ * tests).
84
+ * @param {any[]} items
85
+ * @returns {any}
86
+ */
87
+ export function sequenceResult(items) {
88
+ if (items.length === 0) return undefined;
89
+ if (items.length === 1) return items[0];
90
+ return items;
91
+ }