@jarenjs/db 0.49.2 → 0.66.1

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 (110) hide show
  1. package/ARCHITECTURE.md +420 -71
  2. package/README.md +711 -79
  3. package/docs/HOSTS.md +269 -0
  4. package/docs/JOBS-FORMAT.md +309 -45
  5. package/docs/LIVE-FORMAT.md +156 -19
  6. package/docs/MIGRATION-FORMAT.md +247 -40
  7. package/docs/MODEL-FORMAT.md +968 -86
  8. package/package.json +21 -8
  9. package/schemas/jaren-migration.draft-07.schema.json +73 -0
  10. package/schemas/jaren-migration.schema.json +73 -0
  11. package/schemas/jaren-model.draft-07.schema.json +224 -162
  12. package/schemas/jaren-model.schema.json +224 -162
  13. package/src/algebra.js +227 -9
  14. package/src/backup.js +161 -0
  15. package/src/cancellation.js +48 -0
  16. package/src/capture.js +255 -44
  17. package/src/cli.js +337 -50
  18. package/src/cursor.js +411 -0
  19. package/src/dag-job.js +154 -21
  20. package/src/ddl.js +125 -11
  21. package/src/dialect.js +267 -112
  22. package/src/dialects/expression-read.js +158 -0
  23. package/src/dialects/postgres.js +618 -0
  24. package/src/dialects/rtree-ddl.js +129 -0
  25. package/src/dialects/sqlite.js +245 -12
  26. package/src/document-files.js +311 -0
  27. package/src/document-steps.js +422 -0
  28. package/src/documents.js +335 -0
  29. package/src/driver.js +503 -69
  30. package/src/drivers/bun.js +37 -1
  31. package/src/drivers/indexeddb-snapshot.js +149 -0
  32. package/src/drivers/node-pool.js +11 -0
  33. package/src/drivers/node-worker-endpoint.js +105 -0
  34. package/src/drivers/node-worker.js +204 -0
  35. package/src/drivers/node.js +41 -7
  36. package/src/drivers/postgres.js +331 -0
  37. package/src/drivers/wasm-oo1.js +97 -0
  38. package/src/drivers/wasm-session.js +67 -0
  39. package/src/drivers/wasm.js +18 -83
  40. package/src/drivers/worker-pool.js +183 -0
  41. package/src/drivers/worker-protocol.js +79 -0
  42. package/src/drivers/worker-queue.js +60 -0
  43. package/src/emit-model.js +14 -0
  44. package/src/emit.js +349 -51
  45. package/src/entity.js +102 -59
  46. package/src/errors.js +430 -2
  47. package/src/expression.js +284 -0
  48. package/src/graph.js +64 -8
  49. package/src/index.js +48 -19
  50. package/src/introspect.js +583 -0
  51. package/src/jobs.js +870 -99
  52. package/src/json-bytes.js +58 -0
  53. package/src/live-time.js +12 -3
  54. package/src/live.js +11 -1
  55. package/src/maintenance.js +175 -0
  56. package/src/migrate.js +606 -333
  57. package/src/model.js +241 -8
  58. package/src/plan.js +1238 -160
  59. package/src/pragmas.js +314 -0
  60. package/src/profile.js +151 -3
  61. package/src/query.js +1748 -312
  62. package/src/residual.js +17 -0
  63. package/src/series.js +12 -4
  64. package/src/store.js +1672 -276
  65. package/src/tracker.js +367 -68
  66. package/src/udf.js +88 -7
  67. package/types/index.d.ts +1246 -32
  68. package/types/node-pool.d.ts +28 -0
  69. package/types/node-worker.d.ts +54 -0
  70. package/types/node.d.ts +72 -3
  71. package/types/postgres.d.ts +46 -0
  72. package/types/typed.d.ts +81 -3
  73. package/types/wasm.d.ts +21 -0
  74. package/dist/types/algebra.d.ts +0 -230
  75. package/dist/types/app.d.ts +0 -49
  76. package/dist/types/capture.d.ts +0 -85
  77. package/dist/types/cli.d.ts +0 -2
  78. package/dist/types/dag-job.d.ts +0 -40
  79. package/dist/types/ddl.d.ts +0 -229
  80. package/dist/types/derive.d.ts +0 -250
  81. package/dist/types/dialect.d.ts +0 -154
  82. package/dist/types/dialects/sqlite.d.ts +0 -9
  83. package/dist/types/driver.d.ts +0 -110
  84. package/dist/types/drivers/bun.d.ts +0 -47
  85. package/dist/types/drivers/node.d.ts +0 -37
  86. package/dist/types/drivers/wasm.d.ts +0 -65
  87. package/dist/types/emit-model.d.ts +0 -44
  88. package/dist/types/emit.d.ts +0 -75
  89. package/dist/types/entity.d.ts +0 -23
  90. package/dist/types/errors.d.ts +0 -170
  91. package/dist/types/graph.d.ts +0 -28
  92. package/dist/types/index.d.ts +0 -37
  93. package/dist/types/jobs.d.ts +0 -140
  94. package/dist/types/knn.d.ts +0 -69
  95. package/dist/types/live-time.d.ts +0 -141
  96. package/dist/types/live.d.ts +0 -64
  97. package/dist/types/migrate.d.ts +0 -170
  98. package/dist/types/model.d.ts +0 -36
  99. package/dist/types/patch-sql.d.ts +0 -37
  100. package/dist/types/plan.d.ts +0 -142
  101. package/dist/types/profile.d.ts +0 -80
  102. package/dist/types/query.d.ts +0 -112
  103. package/dist/types/residual.d.ts +0 -64
  104. package/dist/types/series.d.ts +0 -227
  105. package/dist/types/store.d.ts +0 -60
  106. package/dist/types/tracker.d.ts +0 -43
  107. package/dist/types/typed.d.ts +0 -15
  108. package/dist/types/types.d.ts +0 -26
  109. package/dist/types/udf.d.ts +0 -75
  110. package/dist/types/window.d.ts +0 -52
@@ -1,170 +0,0 @@
1
- /**
2
- * @file Document migrations (D12): two model documents diff into a
3
- * migration document whose steps are rendered DDL, JSLT data
4
- * transforms and query assertions; the migration replays on a shadow
5
- * database first; a history table records what ran with a
6
- * signature-grade checksum. This is the phase-A payoff for storing
7
- * documents rather than rows: a shape change is a transformation of
8
- * VALUES, not a table rebuild.
9
- *
10
- * Identity is a hash, not a version number: `from`/`to` are
11
- * `hashContent(canonicalizeJson(model))` — the identity of a SHAPE,
12
- * which nobody has to remember to bump. The checksum discipline is
13
- * D12's: `canonicalizeJson` + `hashContent` (signature-grade — throws
14
- * on the unserializable), never the memo-grade `contentKey`.
15
- *
16
- * Like the query emitter, this module is part of the emitter layer:
17
- * the structural SQL it composes (the history table's statements, the
18
- * batched row walk) is built from dialect primitives, and every
19
- * planner-produced statement is rendered by the dialect into the
20
- * migration DOCUMENT — shown before it is ever executed.
21
- */
22
- /** The migration format version. */
23
- export declare const MIGRATION_VERSION = "0.1";
24
- /** The history table name (outside the model's identifier namespace
25
- * conventions on purpose — a collection cannot collide with it). */
26
- export declare const HISTORY_TABLE = "_jaren_migrations";
27
- /**
28
- * The signature-grade identity of a model SHAPE.
29
- * @param {any} model - A jaren-model document
30
- * @returns {string}
31
- */
32
- export declare function shapeHash(model: any): string;
33
- /**
34
- * The signature-grade checksum of a migration document.
35
- * @param {any} migration
36
- * @returns {string}
37
- */
38
- export declare function migrationChecksum(migration: any): string;
39
- /**
40
- * Plan a migration between two model documents. The planner diffs the
41
- * PHYSICAL plans (columns, indexes) and renders DDL through the
42
- * dialect; a changed schema gets a DRAFT identity transform that
43
- * refuses to run until the author fills it in — the planner cannot
44
- * infer a data transform and does not pretend to. Renames are declared
45
- * (`x-rename` on the target collection), never guessed.
46
- * The physical mapping of a DERIVED index column depends on the driver
47
- * that will run the migration (`derived`), because the two mappings
48
- * really are different columns; a migration document planned for one is
49
- * not the document the other needs.
50
- * @param {any} fromModel
51
- * @param {any} toModel
52
- * @param {{ id?: string, dialect?: any,
53
- * derived?: 'virtual' | 'stored', rtree?: boolean }} [options]
54
- * @returns {{ migration: any, report: {
55
- * renamed: { from: string, to: string }[],
56
- * added: string[], removed: string[],
57
- * schemaChanged: string[], drafts: string[],
58
- * destructive: boolean } }}
59
- */
60
- export declare function planMigration(fromModel: any, toModel: any, options?: {
61
- id?: string;
62
- dialect?: any;
63
- derived?: 'virtual' | 'stored';
64
- rtree?: boolean;
65
- }): {
66
- migration: any;
67
- report: {
68
- renamed: {
69
- from: string;
70
- to: string;
71
- }[];
72
- added: string[];
73
- removed: string[];
74
- schemaChanged: string[];
75
- drafts: string[];
76
- destructive: boolean;
77
- };
78
- };
79
- /** `planMigration` handles the whole model — collections AND entities
80
- * — since the relational order; this name says so. */
81
- export declare const planModelMigration: typeof planMigration;
82
- /**
83
- * Create a model's WHOLE physical shape on a connection: collections,
84
- * entity tables and join tables, exactly as `openStore` would. Used
85
- * by the shadow baseline, the fresh reference database that shape
86
- * equality compares against, and the tests.
87
- * @param {any} connection
88
- * @param {any} model
89
- * @returns {any} value-or-promise
90
- */
91
- export declare function createModelShape(connection: any, model: any): any;
92
- /**
93
- * The declared schema of a database, normalized for comparison: every
94
- * object carrying SQL text (tables, indexes), whitespace-collapsed,
95
- * history table excluded, sorted. Shape equality after a migration —
96
- * this dump versus a fresh {@link createModelShape} — is the
97
- * acceptance criterion for every rebuild.
98
- * @param {any} connection
99
- * @returns {any} value-or-promise of `{ type, name, owner, sql }[]`
100
- */
101
- export declare function schemaShapeOf(connection: any): any;
102
- /**
103
- * Compare a migrated database's schema against the shape a fresh
104
- * `createModelShape(model)` produces, via a throwaway reference
105
- * database. Returns `null` when equal, or a one-line difference.
106
- * @param {any} driver
107
- * @param {any} connection - the migrated database
108
- * @param {any} model - the target model
109
- * @param {((connection: any) => any) | undefined} registerFunctions
110
- * @returns {any} value-or-promise of `string | null`
111
- */
112
- export declare function compareShapeToModel(driver: any, connection: any, model: any, registerFunctions: ((connection: any) => any) | undefined): any;
113
- /**
114
- * Report a database's migration state without touching it: what is
115
- * applied, what is pending, whether an applied migration was edited,
116
- * and — once the chain is fully applied — whether the physical shape
117
- * DRIFTED from the model (someone changed the database by hand, §12).
118
- * @param {{ driver: any, path?: string }} target
119
- * @param {any[]} migrations - the full ordered list
120
- * @param {{ baseline: any, model?: any,
121
- * registerFunctions?: (connection: any) => any }} options
122
- * @returns {Promise<{ applied: string[], pending: string[],
123
- * drift: string | null, upToDate: boolean }>}
124
- */
125
- export declare function migrationStatus(target: {
126
- driver: any;
127
- path?: string;
128
- }, migrations: any[], options: {
129
- baseline: any;
130
- model?: any;
131
- registerFunctions?: (connection: any) => any;
132
- }): Promise<{
133
- applied: string[];
134
- pending: string[];
135
- drift: string | null;
136
- upToDate: boolean;
137
- }>;
138
- /**
139
- * Apply pending migrations to a database.
140
- *
141
- * The contract: `migrations` is the FULL ordered list (applied and
142
- * pending — the migrations directory); `baseline` is the model the
143
- * store was first created with (the chain's anchor and the shadow's
144
- * starting shape); `model` is the target model the code now carries.
145
- * Each pending migration runs in ONE exclusive transaction with a
146
- * savepoint per step; a failing step rolls the whole migration back.
147
- * The whole chain replays on a `:memory:` shadow before the real
148
- * store is touched.
149
- *
150
- * @param {{ driver: any, path?: string, busyTimeout?: number }} target
151
- * @param {any[]} migrations
152
- * @param {{ baseline: any, model?: any, compileSchema?: Function,
153
- * dryRun?: boolean, batchSize?: number, onProgress?: Function,
154
- * shadow?: boolean, shadowPath?: string }} options
155
- * @returns {Promise<any>}
156
- */
157
- export declare function migrate(target: {
158
- driver: any;
159
- path?: string;
160
- busyTimeout?: number;
161
- }, migrations: any[], options: {
162
- baseline: any;
163
- model?: any;
164
- compileSchema?: Function;
165
- dryRun?: boolean;
166
- batchSize?: number;
167
- onProgress?: Function;
168
- shadow?: boolean;
169
- shadowPath?: string;
170
- }): Promise<any>;
@@ -1,36 +0,0 @@
1
- /**
2
- * @file The entity model walk: `x-entity` normalization with a CLOSED
3
- * vocabulary, relation resolution with inverse agreement, and
4
- * `explainMapping` — the derived physical shape as plain data, so the
5
- * hybrid mapping rule is golden-testable rather than folklore.
6
- *
7
- * THE DESCENT DECISION (recorded here because TODO's D22 demands it
8
- * be explicit): six copies of the `properties`/`prefixItems`/`items`/
9
- * `allOf` descent spine exist in this repository, and this walk was
10
- * the candidate seventh. It is NOT one. The entity walk is
11
- * deliberately ONE level deep — it enumerates the TOP-LEVEL
12
- * properties of an entity schema, resolves `$ref` and shallow-merges
13
- * `allOf` at each property through the resolvers
14
- * `@jarenjs/validate/normalize` exports for exactly this purpose, and
15
- * never recurses further, because the mapping rule sends every nested
16
- * shape to the JSONB document wholesale. A consumer with no recursion
17
- * has no descent spine to share, so the shared-enumerator question
18
- * (three different termination strategies across the six copies)
19
- * stays open for the first consumer that actually recurses. No
20
- * seventh copy was added.
21
- */
22
- /**
23
- * Normalize the `entities` member of a model document.
24
- * @param {any} model
25
- * @returns {Map<string, any>} entity name -> normalized entity
26
- */
27
- export declare function normalizeEntities(model: any): Map<string, any>;
28
- /**
29
- * The hybrid mapping, derived mechanically from §9.3's table and
30
- * returned as DATA: per entity, the columns (name, type, source),
31
- * the checks, the foreign keys, the indexes, and which properties
32
- * live in the JSONB document.
33
- * @param {any} model - a model document with `entities`
34
- * @returns {any}
35
- */
36
- export declare function explainMapping(model: any): any;
@@ -1,37 +0,0 @@
1
- /**
2
- * @file RFC 6902 → dialect JSON-set primitives, so a one-field update
3
- * does not rewrite a large document. The translation is decided
4
- * AGAINST THE LIVE DOCUMENT: an RFC 6901 pointer cannot say whether
5
- * `/a/0` names an array position or an object member called `"0"`, so
6
- * each segment is discriminated by walking the document the patch was
7
- * validated against, and the walked state is advanced op by op so a
8
- * later operation sees what the earlier ones produced.
9
- *
10
- * Translatable in 0.1: `replace` anywhere, `add` of an object member,
11
- * `add` at an array's end (`/-` or the index equal to its length), and
12
- * `remove`. Everything else — `test`, `move`, `copy`, a mid-array
13
- * insert (the shift has no single JSON-function spelling) — returns
14
- * `null` and the store falls back to a whole-document write. The
15
- * fallback is counted and exposed by the store, measured rather than
16
- * assumed.
17
- */
18
- export type JsonPathSegment = import('./dialect.js').JsonPathSegment;
19
- /**
20
- * Translate a whole patch into a dialect expression builder, or `null`
21
- * when any operation needs the whole-document fallback. The caller has
22
- * already applied the patch in memory (the copy-on-write engine
23
- * validates the RESULT); this translation only decides how the same
24
- * outcome reaches the database.
25
- * @param {any[]} ops - RFC 6902 operations, already known applicable
26
- * @param {any} doc - The stored document the patch applies to
27
- * @param {any} dialect
28
- * @returns {{ build: (docColumnSql: string,
29
- * parameterIndexBase: number) => { expression: string,
30
- * params: string[] } } | null}
31
- */
32
- export declare function translatePatch(ops: any[], doc: any, dialect: any): {
33
- build: (docColumnSql: string, parameterIndexBase: number) => {
34
- expression: string;
35
- params: string[];
36
- };
37
- } | null;
@@ -1,142 +0,0 @@
1
- /**
2
- * @file AST → Plan. The planner walks the engine's PUBLISHED normalized
3
- * AST (never the raw document), dispatches EXHAUSTIVELY on node kind —
4
- * an unrecognised kind is an internal error naming the kind and the
5
- * `AST_VERSION`, never a silent residual — and promotes constructs to
6
- * native form strictly residual-by-default: everything starts as a
7
- * residual and earns native status only where the equivalence proof
8
- * exists (the truth table in ARCHITECTURE.md, pinned by the
9
- * differential tests).
10
- *
11
- * The outcome of planning one document:
12
- *
13
- * { plan, mode: 'native' | 'row' | 'set' | 'knn', reasons, rowReturn,
14
- * prefilters }
15
- *
16
- * - `native` — everything translated; the plan alone answers.
17
- * - `row` — predicates, ordering and window pushed; only the
18
- * projection runs in the engine, per fetched row (streams).
19
- * `rowReturn` is the COMPLETE one-row document to run, binding
20
- * included — the collection binding is named by the document, so a
21
- * wrapper built anywhere else would have to guess it.
22
- * - `set` — the pushed conjuncts narrow candidates; the WHOLE
23
- * compiled document runs over the materialized candidates.
24
- * - `knn` — the pushed conjuncts narrow, the vector column CUTS the
25
- * candidates of a k-nearest window (`plan.rank`), and the whole
26
- * compiled document runs over the cut — a set residual whose
27
- * candidate set an ordering, not a predicate, chose (see "The
28
- * k-nearest promotion" below).
29
- *
30
- * `reasons` names every construct that forced work off the database,
31
- * with reason text drawn from the deliberate-residual table.
32
- * `prefilters` names the IMPLIED conjuncts — predicates the planner
33
- * ADDED because a spatial one provably implies them (see "Spatial
34
- * promotions" below) — with the columns each reads and whether it
35
- * decided or merely narrowed.
36
- */
37
- /**
38
- * Assert a node kind is one this planner has decided. Called on every
39
- * dispatch; the throw names the kind and the AST version so a language
40
- * change breaks the build instead of becoming an accidental residual.
41
- * Exported so the throw itself is pinned by a test.
42
- * @param {any} node
43
- */
44
- export declare function assertDecidedKind(node: any): void;
45
- /**
46
- * Plan a whole document against one collection. The store's registered
47
- * operators (Ring 2) ride in `shape.operators` — the planner recognises
48
- * them as vocabulary but keeps them in the residual, and names them in
49
- * the reasons when it does.
50
- * @param {any} document - The raw query document (kept beside the AST
51
- * for residual construction — the AST has no unparser)
52
- * @param {any} shape - { collection, schema, columnByCanonical, operators? }
53
- * @param {{ udf?: (fragment: any) => { name: string, key: string } | null }} [options]
54
- * @returns {{
55
- * analysis: any,
56
- * plan: import('./algebra.js').Plan | null,
57
- * mode: 'native' | 'row' | 'set' | 'knn',
58
- * reasons: { construct: string, reason: string }[],
59
- * rowReturn: any,
60
- * udfs: string[],
61
- * prefilters: { construct: string, via: 'columns' | 'rtree',
62
- * columns: string[], exact: boolean }[],
63
- * series: any,
64
- * }}
65
- */
66
- export declare function planQuery(document: any, shape: any, options?: {
67
- udf?: (fragment: any) => {
68
- name: string;
69
- key: string;
70
- } | null;
71
- }): {
72
- analysis: any;
73
- plan: import('./algebra.js').Plan | null;
74
- mode: 'native' | 'row' | 'set' | 'knn';
75
- reasons: {
76
- construct: string;
77
- reason: string;
78
- }[];
79
- rowReturn: any;
80
- udfs: string[];
81
- prefilters: {
82
- construct: string;
83
- via: 'columns' | 'rtree';
84
- columns: string[];
85
- exact: boolean;
86
- }[];
87
- series: any;
88
- };
89
- /**
90
- * Build the planner shape for one entity: canonical top-level paths
91
- * map to REAL columns (flavor `entity-column`), epoch date columns to
92
- * their derived integer twins (flavor `entity-epoch`), and everything
93
- * else stays a document path over the entity's JSONB column (the
94
- * phase-A guarded forms).
95
- * @param {any} entity - normalized entity (model.js)
96
- * @param {any} entityMapping - explainMapping(...).entities[name]
97
- * @returns {any}
98
- */
99
- export declare function entityShape(entity: any, entityMapping: any): any;
100
- /**
101
- * Resolve a singular member path on an entity binding to a flavored
102
- * PlanRef.
103
- * @param {any} node - a path AST node
104
- * @param {number} slot
105
- * @param {any} shape - from {@link entityShape}
106
- * @returns {any | null}
107
- */
108
- export declare function entityPathRef(node: any, slot: number, shape: any): any | null;
109
- /**
110
- * Plan one predicate over an entity binding: the same operator
111
- * grammar as phase A, with entity-flavored refs. Reuses
112
- * {@link planPredicate} for the recognition, then re-resolves refs
113
- * through the flavor table.
114
- * @param {any} node
115
- * @param {number} slot
116
- * @param {any} shape
117
- * @returns {{ pred: any } | { refusal: { construct: string, reason: string } }}
118
- */
119
- export declare function planEntityPredicate(node: any, slot: number, shape: any): {
120
- pred: any;
121
- } | {
122
- refusal: {
123
- construct: string;
124
- reason: string;
125
- };
126
- };
127
- /**
128
- * Plan an ENTITY query document (one planner, two document kinds). The
129
- * store's registered operators (Ring 2) are recognised as vocabulary and
130
- * kept in the set residual over the fetched root, named in the reasons.
131
- * @param {any} document
132
- * @param {Map<string, any>} entities - normalized entities
133
- * @param {any} mapping - explainMapping result
134
- * @param {{ functions?: any, extensions?: any } | null} [operators]
135
- * @returns {any}
136
- */
137
- export declare function planEntityQuery(document: any, entities: Map<string, any>, mapping: any, operators?: {
138
- functions?: any;
139
- extensions?: any;
140
- } | null): any;
141
- /** The entity names a document's root paths reference (`$.Name[*]`). */
142
- export declare function collectEntityRoots(document: any, entities: any): Set<any>;
@@ -1,80 +0,0 @@
1
- /**
2
- * @file The safe execution profile (D15): a query document that arrives
3
- * from a tenant, a remote client or a language model can reach a
4
- * database, and injection being structurally impossible (parameter
5
- * binding) says nothing about resource exhaustion or cross-tenant
6
- * reads. A profile composes four INDEPENDENT bounds:
7
- *
8
- * 1. engine limits — `{sequenceItems, resultItems, steps, depth}`
9
- * wired into every residual compilation, so the JavaScript
10
- * portion of a query is bounded by the engine's own enforcement;
11
- * 2. the mandatory row bound — every non-aggregate fetch carries a
12
- * LIMIT of `maxRows + 1`, and fetching more than `maxRows` rows is
13
- * the coded `JD2007`, never a silent truncation (D14);
14
- * 3. reference containment — undeclared externals, host functions,
15
- * collations or collections are the compile error `JD0011`; no UDF
16
- * registration happens under a profile; optionally, a plan whose
17
- * database narrative shows a full-table SCAN is refused;
18
- * 4. mandatory predicates — a per-collection predicate conjoined into
19
- * EVERY plan at its root, after translation, so no document shape
20
- * can produce a fetch without it.
21
- *
22
- * The non-claims are part of the contract and live in
23
- * MODEL-FORMAT.md §8: no statement timeout exists on the SQLite
24
- * drivers (the capability slot is empty), so a long-running native
25
- * aggregate is bounded by nothing here; the row bound covers fetched
26
- * rows, not database-internal work.
27
- */
28
- /** The `'safe'` profile: the documented defaults. */
29
- export declare const SAFE_PROFILE: Readonly<{
30
- limits: Readonly<{
31
- sequenceItems: 100000;
32
- resultItems: 10000;
33
- steps: 1000000;
34
- depth: 32;
35
- }>;
36
- maxRows: 1000;
37
- externals: readonly never[];
38
- functions: readonly never[];
39
- collations: readonly never[];
40
- collections: null;
41
- predicates: Readonly<{}>;
42
- refuseFullScan: false;
43
- }>;
44
- /**
45
- * Normalize a profile option: the string `'safe'` is the default
46
- * table; an object overrides individual members over those defaults
47
- * (limits merge member-wise). The result is plain JSON — cacheable by
48
- * content key — and frozen.
49
- * @param {any} profile - `'safe'` or a partial profile object
50
- * @returns {any}
51
- */
52
- export declare function normalizeProfile(profile: any): any;
53
- /**
54
- * Translate a profile's mandatory predicate for one collection into a
55
- * plan predicate. The predicate is HOST-authored configuration, so a
56
- * predicate that does not translate natively is a host programming
57
- * error (TypeError), not a coded document failure — there is no
58
- * residual to hide it in: the whole point is that it binds the
59
- * database-side fetch.
60
- * @param {any} expression - A query expression over `$it`
61
- * @param {any} shape - The collection's plan shape
62
- * @returns {import('./algebra.js').PlanPredicate}
63
- */
64
- export declare function translateProfilePredicate(expression: any, shape: any): import('./algebra.js').PlanPredicate;
65
- /**
66
- * Conjoin a mandatory predicate into a plan's root filter.
67
- * @param {import('./algebra.js').Plan} plan
68
- * @param {import('./algebra.js').PlanPredicate | null} predicate
69
- * @returns {import('./algebra.js').Plan}
70
- */
71
- export declare function applyMandatoryPredicate(plan: import('./algebra.js').Plan, predicate: import('./algebra.js').PlanPredicate | null): import('./algebra.js').Plan;
72
- /**
73
- * Cap a plan's window at the profile's detection bound
74
- * (`maxRows + 1`): a result crossing `maxRows` is detected and
75
- * refused, never silently truncated. Aggregates are exempt (one row).
76
- * @param {import('./algebra.js').Plan} plan
77
- * @param {number} maxRows
78
- * @returns {import('./algebra.js').Plan}
79
- */
80
- export declare function applyRowBound(plan: import('./algebra.js').Plan, maxRows: number): import('./algebra.js').Plan;
@@ -1,112 +0,0 @@
1
- /**
2
- * @file The query surface over one collection: the D2 provider
3
- * (`execute(document, options)` — how a linq chain runs here with no
4
- * import edge), the streaming cursor (`query`), and `explain()`.
5
- *
6
- * The statement cache is a CALLER of the core primitives:
7
- * `createSemanticCache` keyed by the whole discriminating tuple —
8
- * document plus collection, dialect, strictness, pushdown and profile.
9
- * The identity is the tuple's COMPLETE serialization, never a
10
- * fingerprint of it: a 32-bit content hash collides after tens of
11
- * thousands of documents, and a collision here answers one query with
12
- * another query's plan and rows. `store.stats()` exposes hits, misses
13
- * and evictions so the cache is proven rather than assumed.
14
- *
15
- * Bind-time diversion: if any referenced external is missing or not a
16
- * string or finite number, the call runs the always-compilable set
17
- * residual over the full collection instead of the native statement —
18
- * SQLite cannot bind a boolean, a `null` needs Jaren's semantics, and
19
- * a missing external must raise the ENGINE's error, not a driver's.
20
- * Two externals never bind at all and divert by their own rule: a
21
- * region reaching the statement through derived slots diverts when it
22
- * has no box, and a k-nearest probe — which the plan scores in the
23
- * engine, never in SQL — diverts when it is not a vector of the
24
- * column's width, so the engine answers what it answers everywhere.
25
- *
26
- * The k-nearest mode (`plan.rank`) is a set residual whose candidates
27
- * an ordering chose: the statement fetches (identity, column) under
28
- * the pushed WHERE, the engine scores and cuts (`knn.js`), the
29
- * winners' documents are fetched by identity through the dialect, and
30
- * the whole document runs over them.
31
- */
32
- /**
33
- * The store-wide query state shared by every collection's engine: one
34
- * bounded statement cache, its counters, the UDF registration set, and
35
- * the store's resolved registered operators (Ring 2 — `{ functions,
36
- * extensions }` or `null`), threaded to every engine that builds a
37
- * residual.
38
- * @param {number} [bound]
39
- * @param {{ functions?: any, extensions?: any } | null} [operators]
40
- * @param {any} [zoneProvider] - D7's injected clock, or absent
41
- * @returns {any}
42
- */
43
- export declare function createQueryState(bound?: number, operators?: {
44
- functions?: any;
45
- extensions?: any;
46
- } | null, zoneProvider?: any): any;
47
- /**
48
- * The query engine for one collection.
49
- * @param {{ connection: any, state: any, collection: any,
50
- * physicalPlan: any, profile?: any }} context - `collection` is the
51
- * normalized collection; `physicalPlan` is the DDL plan
52
- * (columns, indexes); `profile` is the store-level normalized
53
- * profile, if one was opened with
54
- * @returns {{ execute: Function, query: Function, explain: Function }}
55
- */
56
- export declare function createQueryEngine(context: {
57
- connection: any;
58
- state: any;
59
- collection: any;
60
- physicalPlan: any;
61
- profile?: any;
62
- }): {
63
- execute: Function;
64
- query: Function;
65
- explain: Function;
66
- };
67
- /** The default include depth bound (D14: printed, never silent). */
68
- export declare const INCLUDE_DEPTH_DEFAULT = 3;
69
- /**
70
- * The store-level entity query engine: documents over the
71
- * multi-entity root (`$.<Entity>[*]` bindings), planned to guarded
72
- * selections and INNER equijoins, with the set residual running the
73
- * whole document over the fetched root — the same honesty contract as
74
- * phase A.
75
- * @param {{ connection: any, entities: Map<string, any>, mapping: any,
76
- * state: any }} context
77
- * @returns {any}
78
- */
79
- export declare function createEntityQueryEngine(context: {
80
- connection: any;
81
- entities: Map<string, any>;
82
- mapping: any;
83
- state: any;
84
- }): any;
85
- /**
86
- * The one-statement graph loader: `entity.load(spec)` compiles an
87
- * include tree to correlated subqueries projected as JSON — one
88
- * statement regardless of depth (asserted by a counting driver in the
89
- * tests, because N+1 is a test, not a promise). Per-relation `where`,
90
- * `orderBy` and `take` are applied INSIDE the subquery; depth is
91
- * bounded with the default printed in the refusal; cycles in the
92
- * specification are rejected; and keyset pagination is chosen over a
93
- * growing OFFSET whenever the top-level ordering is a single unique
94
- * column, with the choice reported by `explainLoad`.
95
- * @param {{ connection: any, entities: Map<string, any>, mapping: any,
96
- * state: any }} context
97
- * @param {string} entityName
98
- * @returns {any}
99
- */
100
- export declare function createLoadEngine(context: {
101
- connection: any;
102
- entities: Map<string, any>;
103
- mapping: any;
104
- state: any;
105
- }, entityName: string): any;
106
- export type ParamCollector = {
107
- literal?: any;
108
- external?: string;
109
- }[];
110
- /**
111
- * @typedef {{ literal?: any, external?: string }[]} ParamCollector
112
- */
@@ -1,64 +0,0 @@
1
- /**
2
- * @file Residual compilation: the part of a query that stays in
3
- * JavaScript is a REAL compiled Jaren query — the same engine, the
4
- * same semantics — never a reimplementation.
5
- *
6
- * Two modes (ARCHITECTURE.md):
7
- *
8
- * - `set` — the whole original document compiled once, run over the
9
- * materialized candidate array. Re-applying pushed conjuncts is
10
- * idempotent, so SQL-side narrowing never changes the answer.
11
- * - `row` — only the projection stayed behind: each fetched row runs
12
- * the planner's one-row document over the one-row array; the array
13
- * wrapper packs the item sequence so an array-VALUED item stays
14
- * unambiguous, and the per-row results concatenate in row order
15
- * (streamable).
16
- *
17
- * Neither mode builds a query document here. The collection binding is
18
- * named by the caller's document — `it`, `user`, anything — so a wrapper
19
- * synthesized in this file could only guess it, and a guess that
20
- * disagreed with the projection's references would surface as an
21
- * unbound-external error at request time rather than at compile time.
22
- * The planner knows the name and hands both modes something complete.
23
- */
24
- /**
25
- * Compile the whole document for set-mode evaluation. A profile's
26
- * engine limits ride into the compilation so the JavaScript portion is
27
- * bounded by the engine's own enforcement.
28
- * @param {any} document
29
- * @param {any} [limits]
30
- * @param {{ functions?: any, extensions?: any } | null} [operators] -
31
- * the store's registered operators, so the residual can evaluate them
32
- * @param {any} [zoneProvider] - D7's injected clock, so a calendar
33
- * ladder on a named zone compiles rather than being refused
34
- * @returns {(candidates: any[], externals: any) => any}
35
- */
36
- export declare function compileSetResidual(document: any, limits?: any, operators?: {
37
- functions?: any;
38
- extensions?: any;
39
- } | null, zoneProvider?: any): (candidates: any[], externals: any) => any;
40
- /**
41
- * Compile the per-row projection for row-mode evaluation.
42
- * @param {any} rowDocument - The planner's complete one-row document
43
- * (`{ $for: { <the document's own binding>: '$[*]' },
44
- * $return: [ <its $return> ] }`). It arrives whole because the binding
45
- * and the projection that references it must agree, and the planner is
46
- * the only place that knows the name.
47
- * @param {any} [limits]
48
- * @param {{ functions?: any, extensions?: any } | null} [operators]
49
- * @param {any} [zoneProvider] - D7's injected clock
50
- * @returns {(row: any, externals: any) => any[]} the row's items
51
- */
52
- export declare function compileRowResidual(rowDocument: any, limits?: any, operators?: {
53
- functions?: any;
54
- extensions?: any;
55
- } | null, zoneProvider?: any): (row: any, externals: any) => any[];
56
- /**
57
- * Map a flat item array onto the engine's result shape: an empty
58
- * sequence is `undefined`, a singleton is the item, anything longer is
59
- * the array (probed engine behaviour, pinned by the differential
60
- * tests).
61
- * @param {any[]} items
62
- * @returns {any}
63
- */
64
- export declare function sequenceResult(items: any[]): any;