@atscript/db-memory 0.1.150 → 0.1.152
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.
- package/dist/index.cjs +447 -114
- package/dist/index.d.cts +53 -17
- package/dist/index.d.mts +53 -17
- package/dist/index.mjs +452 -119
- package/package.json +9 -9
package/dist/index.cjs
CHANGED
|
@@ -2,6 +2,69 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
2
2
|
let _atscript_db = require("@atscript/db");
|
|
3
3
|
let _atscript_db_agg = require("@atscript/db/agg");
|
|
4
4
|
let _uniqu_core = require("@uniqu/core");
|
|
5
|
+
//#region src/lib/memory-clone.ts
|
|
6
|
+
/**
|
|
7
|
+
* Deep clone with the exact observable result of `structuredClone` — fast for
|
|
8
|
+
* the plain data the store holds (plain objects, dense arrays, `Date`s and
|
|
9
|
+
* primitives), and delegating to `structuredClone` for everything else.
|
|
10
|
+
*
|
|
11
|
+
* The fast walk gives up — and the WHOLE value is re-cloned by
|
|
12
|
+
* `structuredClone` — on anything whose `structuredClone` result it would not
|
|
13
|
+
* reproduce exactly: a function or symbol (so the `DataCloneError` still
|
|
14
|
+
* throws), a non-plain prototype (class instances, `Map`, `Set`, typed arrays,
|
|
15
|
+
* `RegExp`, boxed primitives, …), an own `__proto__` key, a sparse array or
|
|
16
|
+
* one carrying extra own properties, and an object reached twice (a cycle or a
|
|
17
|
+
* shared subtree — `structuredClone` preserves both, a tree walk would not).
|
|
18
|
+
*/
|
|
19
|
+
function cloneValue(value) {
|
|
20
|
+
if (value === null || typeof value !== "object") return typeof value === "function" || typeof value === "symbol" ? structuredClone(value) : value;
|
|
21
|
+
const out = walk(value, /* @__PURE__ */ new Set());
|
|
22
|
+
return out === GIVE_UP ? structuredClone(value) : out;
|
|
23
|
+
}
|
|
24
|
+
const GIVE_UP = Symbol("give-up");
|
|
25
|
+
const OBJECT_PROTO = Object.prototype;
|
|
26
|
+
const ARRAY_PROTO = Array.prototype;
|
|
27
|
+
const DATE_PROTO = Date.prototype;
|
|
28
|
+
function walk(value, seen) {
|
|
29
|
+
if (seen.has(value)) return GIVE_UP;
|
|
30
|
+
seen.add(value);
|
|
31
|
+
const proto = Object.getPrototypeOf(value);
|
|
32
|
+
if (proto === ARRAY_PROTO) {
|
|
33
|
+
const src = value;
|
|
34
|
+
const len = src.length;
|
|
35
|
+
const arr = [];
|
|
36
|
+
for (let i = 0; i < len; i++) {
|
|
37
|
+
if (!(i in src)) return GIVE_UP;
|
|
38
|
+
const item = leaf(src[i], seen);
|
|
39
|
+
if (item === GIVE_UP) return GIVE_UP;
|
|
40
|
+
arr.push(item);
|
|
41
|
+
}
|
|
42
|
+
return Object.keys(src).length === len ? arr : GIVE_UP;
|
|
43
|
+
}
|
|
44
|
+
if (proto === OBJECT_PROTO || proto === null) {
|
|
45
|
+
const src = value;
|
|
46
|
+
const obj = {};
|
|
47
|
+
for (const key of Object.keys(src)) {
|
|
48
|
+
if (key === "__proto__") return GIVE_UP;
|
|
49
|
+
const item = leaf(src[key], seen);
|
|
50
|
+
if (item === GIVE_UP) return GIVE_UP;
|
|
51
|
+
obj[key] = item;
|
|
52
|
+
}
|
|
53
|
+
return obj;
|
|
54
|
+
}
|
|
55
|
+
if (proto === DATE_PROTO) return new Date(value.getTime());
|
|
56
|
+
return GIVE_UP;
|
|
57
|
+
}
|
|
58
|
+
function leaf(value, seen) {
|
|
59
|
+
if (value === null) return null;
|
|
60
|
+
switch (typeof value) {
|
|
61
|
+
case "object": return walk(value, seen);
|
|
62
|
+
case "function":
|
|
63
|
+
case "symbol": return GIVE_UP;
|
|
64
|
+
default: return value;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
//#endregion
|
|
5
68
|
//#region src/lib/memory-filter.ts
|
|
6
69
|
/**
|
|
7
70
|
* {@link getPath} compiled for one path: split once, and a single segment is
|
|
@@ -71,9 +134,8 @@ function parseRegexString(value) {
|
|
|
71
134
|
function stringifyLeaf(v) {
|
|
72
135
|
return String(v);
|
|
73
136
|
}
|
|
74
|
-
/** `$eq` semantics, factored out so `$ne` can be its exact negation. */
|
|
75
|
-
function evalEq(
|
|
76
|
-
const fieldValue = (0, _atscript_db.getPath)(row, field);
|
|
137
|
+
/** `$eq` semantics over the field's value, factored out so `$ne` can be its exact negation. */
|
|
138
|
+
function evalEq(fieldValue, value) {
|
|
77
139
|
if (value === null) return matchesScalar(fieldValue, null);
|
|
78
140
|
if (value instanceof RegExp) return fieldValue != null && value.test(stringifyLeaf(fieldValue));
|
|
79
141
|
return matchesScalar(fieldValue, value);
|
|
@@ -89,11 +151,37 @@ function matchesScalar(fieldValue, value) {
|
|
|
89
151
|
if (Array.isArray(fieldValue) && !Array.isArray(value)) return fieldValue.some((el) => valuesEqual(el, value));
|
|
90
152
|
return valuesEqual(fieldValue, value);
|
|
91
153
|
}
|
|
92
|
-
/**
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
154
|
+
/**
|
|
155
|
+
* `$in` membership compiled once per filter leaf, factored out so `$nin` can be
|
|
156
|
+
* its exact negation: true iff the field value {@link matchesScalar matches}
|
|
157
|
+
* SOME element. Primitive elements (and `Date`s, by instant) are looked up in a
|
|
158
|
+
* `Set` instead of scanned per row — `NaN` is left out of it (it equals
|
|
159
|
+
* nothing, while a `Set` would find it), `-0`/`0` collapse as under `===`.
|
|
160
|
+
* Any other element (an object / array — reference equality) keeps the scan.
|
|
161
|
+
*/
|
|
162
|
+
function compileIn(value) {
|
|
163
|
+
if (!Array.isArray(value)) return () => false;
|
|
164
|
+
const primitives = /* @__PURE__ */ new Set();
|
|
165
|
+
const instants = /* @__PURE__ */ new Set();
|
|
166
|
+
const rest = [];
|
|
167
|
+
let hasNull = false;
|
|
168
|
+
for (const element of value) if (element === null) hasNull = true;
|
|
169
|
+
else if (element instanceof Date) {
|
|
170
|
+
const time = element.getTime();
|
|
171
|
+
if (!Number.isNaN(time)) instants.add(time);
|
|
172
|
+
} else if (typeof element === "object") rest.push(element);
|
|
173
|
+
else if (!(typeof element === "number" && Number.isNaN(element))) primitives.add(element);
|
|
174
|
+
const one = (v) => v instanceof Date ? instants.has(v.getTime()) : (v === null || typeof v !== "object") && primitives.has(v);
|
|
175
|
+
return (fieldValue) => {
|
|
176
|
+
if (Array.isArray(fieldValue)) {
|
|
177
|
+
if (hasNull && fieldValue.includes(null)) return true;
|
|
178
|
+
for (const el of fieldValue) if (one(el)) return true;
|
|
179
|
+
return rest.some((el) => Array.isArray(el) ? el === fieldValue : fieldValue.includes(el));
|
|
180
|
+
}
|
|
181
|
+
if (fieldValue === null) return hasNull;
|
|
182
|
+
if (fieldValue === void 0) return hasNull || primitives.has(void 0);
|
|
183
|
+
return one(fieldValue) || rest.includes(fieldValue);
|
|
184
|
+
};
|
|
97
185
|
}
|
|
98
186
|
/**
|
|
99
187
|
* Coerce a leaf to something the JS relational operators can order. `Date`s
|
|
@@ -111,8 +199,7 @@ function toOrdinal(v) {
|
|
|
111
199
|
* lexicographically) — NO collation or locale awareness. This intentionally
|
|
112
200
|
* differs from SQL engines' collated ordering.
|
|
113
201
|
*/
|
|
114
|
-
function evalRelational(
|
|
115
|
-
const fieldValue = (0, _atscript_db.getPath)(row, field);
|
|
202
|
+
function evalRelational(fieldValue, op, value) {
|
|
116
203
|
if (fieldValue === void 0 || fieldValue === null) return false;
|
|
117
204
|
const a = toOrdinal(fieldValue);
|
|
118
205
|
const b = toOrdinal(value);
|
|
@@ -144,32 +231,39 @@ const memoryVisitor = {
|
|
|
144
231
|
return (row) => !child(row);
|
|
145
232
|
},
|
|
146
233
|
comparison(field, op, value) {
|
|
234
|
+
const read = pathReader(field);
|
|
147
235
|
if (op === _atscript_db.INTEGER_REGEX_OP) {
|
|
148
236
|
const { pattern, flags } = parseRegexString(value);
|
|
149
237
|
const regex = new RegExp(pattern, flags);
|
|
150
238
|
return (row) => {
|
|
151
|
-
const fieldValue = (
|
|
239
|
+
const fieldValue = read(row);
|
|
152
240
|
return (typeof fieldValue === "bigint" || typeof fieldValue === "number" && Number.isInteger(fieldValue)) && regex.test(String(fieldValue));
|
|
153
241
|
};
|
|
154
242
|
}
|
|
155
243
|
switch (op) {
|
|
156
|
-
case "$eq": return (row) => evalEq(row,
|
|
157
|
-
case "$ne": return (row) => !evalEq(row,
|
|
244
|
+
case "$eq": return (row) => evalEq(read(row), value);
|
|
245
|
+
case "$ne": return (row) => !evalEq(read(row), value);
|
|
158
246
|
case "$gt":
|
|
159
247
|
case "$gte":
|
|
160
248
|
case "$lt":
|
|
161
|
-
case "$lte": return (row) => evalRelational(row,
|
|
162
|
-
case "$in":
|
|
163
|
-
|
|
249
|
+
case "$lte": return (row) => evalRelational(read(row), op, value);
|
|
250
|
+
case "$in": {
|
|
251
|
+
const isIn = compileIn(value);
|
|
252
|
+
return (row) => isIn(read(row));
|
|
253
|
+
}
|
|
254
|
+
case "$nin": {
|
|
255
|
+
const isIn = compileIn(value);
|
|
256
|
+
return (row) => !isIn(read(row));
|
|
257
|
+
}
|
|
164
258
|
case "$regex": {
|
|
165
259
|
const { pattern, flags } = parseRegexString(value);
|
|
166
260
|
const regex = new RegExp(pattern, flags);
|
|
167
261
|
return (row) => {
|
|
168
|
-
const fieldValue = (
|
|
262
|
+
const fieldValue = read(row);
|
|
169
263
|
return fieldValue != null && regex.test(stringifyLeaf(fieldValue));
|
|
170
264
|
};
|
|
171
265
|
}
|
|
172
|
-
case "$exists": return (row) => value === !evalEq(row,
|
|
266
|
+
case "$exists": return (row) => value === !evalEq(read(row), null);
|
|
173
267
|
default: throw new _atscript_db.DbError("INVALID_QUERY", [{
|
|
174
268
|
path: field,
|
|
175
269
|
message: `Unsupported filter operator: ${op}`
|
|
@@ -366,7 +460,10 @@ function compareLeaves(a, b) {
|
|
|
366
460
|
* nested dot-paths both work.
|
|
367
461
|
*/
|
|
368
462
|
function setPath(target, path, value) {
|
|
369
|
-
|
|
463
|
+
setSegments(target, path.split("."), value);
|
|
464
|
+
}
|
|
465
|
+
/** {@link setPath} over an already-split path. */
|
|
466
|
+
function setSegments(target, segments, value) {
|
|
370
467
|
let current = target;
|
|
371
468
|
for (let i = 0; i < segments.length - 1; i++) {
|
|
372
469
|
const seg = segments[i];
|
|
@@ -386,31 +483,88 @@ function setPath(target, path, value) {
|
|
|
386
483
|
* rows with equal sort keys still order deterministically. When ABSENT the
|
|
387
484
|
* sort falls back to preserving input order among equal keys (via each row's
|
|
388
485
|
* original index), so a consumer with no primary key keeps insertion order.
|
|
486
|
+
* - `topK` (since 0.1.151), when supplied, asks for only the first `topK` rows
|
|
487
|
+
* of that order (e.g. `$skip + $limit`): a small one is selected in one pass
|
|
488
|
+
* instead of sorting everything. The rows returned are exactly the head of
|
|
489
|
+
* the full sort — same comparator, same stable tie handling.
|
|
389
490
|
*
|
|
390
|
-
* NEVER mutates the input array
|
|
391
|
-
*
|
|
392
|
-
*
|
|
393
|
-
* the O(n log n) comparator.
|
|
491
|
+
* NEVER mutates the input array. Each row's sort keys are read ONCE (O(n)) and
|
|
492
|
+
* its `tieBreak` only when two rows tie on every key — not inside the
|
|
493
|
+
* O(n log n) comparator.
|
|
394
494
|
*/
|
|
395
|
-
function sortRows(rows, $sort, tieBreak) {
|
|
495
|
+
function sortRows(rows, $sort, tieBreak, topK) {
|
|
396
496
|
const keys = $sort ? Object.entries($sort) : [];
|
|
397
497
|
if (keys.length === 0) return rows;
|
|
398
|
-
|
|
498
|
+
const readers = keys.map(([field]) => pathReader(field));
|
|
499
|
+
const desc = keys.map(([, dir]) => dir === -1);
|
|
500
|
+
const decorated = rows.map((row, index) => ({
|
|
399
501
|
row,
|
|
400
502
|
index,
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
503
|
+
keys: readers.map((read) => {
|
|
504
|
+
const value = read(row);
|
|
505
|
+
return value instanceof Date ? value.getTime() : value;
|
|
506
|
+
}),
|
|
507
|
+
tie: void 0
|
|
508
|
+
}));
|
|
509
|
+
const compare = (a, b) => {
|
|
510
|
+
for (let i = 0; i < desc.length; i++) {
|
|
511
|
+
const cmp = compareLeaves(a.keys[i], b.keys[i]);
|
|
512
|
+
if (cmp !== 0) return desc[i] ? -cmp : cmp;
|
|
406
513
|
}
|
|
407
514
|
if (tieBreak) {
|
|
408
|
-
const at = a.tie;
|
|
409
|
-
const bt = b.tie;
|
|
515
|
+
const at = a.tie ??= tieBreak(a.row);
|
|
516
|
+
const bt = b.tie ??= tieBreak(b.row);
|
|
410
517
|
return at < bt ? -1 : at > bt ? 1 : 0;
|
|
411
518
|
}
|
|
412
519
|
return a.index - b.index;
|
|
413
|
-
}
|
|
520
|
+
};
|
|
521
|
+
return (topK !== void 0 && topK <= TOP_K_MAX && topK * 4 < decorated.length && totallyOrdered(decorated, desc.length) ? selectTop(decorated, topK, compare) : decorated.toSorted(compare)).map((entry) => entry.row);
|
|
522
|
+
}
|
|
523
|
+
/**
|
|
524
|
+
* Whether every sort key holds values of ONE ordered kind — numbers (`Date`s
|
|
525
|
+
* included) without `NaN`, strings, or booleans — besides `null`/missing.
|
|
526
|
+
* Only then is the comparator a total order, so selecting the head gives
|
|
527
|
+
* exactly the rows the full (stable) sort puts first; mixed kinds compare
|
|
528
|
+
* inconsistently (`"a" < 1` and `"a" > 1` are both false), and their full
|
|
529
|
+
* sort order depends on the algorithm, so it keeps the full sort.
|
|
530
|
+
*/
|
|
531
|
+
function totallyOrdered(entries, keyCount) {
|
|
532
|
+
for (let i = 0; i < keyCount; i++) {
|
|
533
|
+
let kind;
|
|
534
|
+
for (const entry of entries) {
|
|
535
|
+
const value = entry.keys[i];
|
|
536
|
+
if (value === null || value === void 0) continue;
|
|
537
|
+
const type = typeof value;
|
|
538
|
+
if (type === "number" ? Number.isNaN(value) : type !== "string" && type !== "boolean") return false;
|
|
539
|
+
if (kind === void 0) kind = type;
|
|
540
|
+
else if (kind !== type) return false;
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
return true;
|
|
544
|
+
}
|
|
545
|
+
/** Largest `topK` {@link sortRows} selects in one pass instead of sorting. */
|
|
546
|
+
const TOP_K_MAX = 128;
|
|
547
|
+
/**
|
|
548
|
+
* The first `k` entries of the stable sort by `compare`, in order: a bounded
|
|
549
|
+
* sorted buffer, each entry inserted AFTER its equals (entries arrive in input
|
|
550
|
+
* order, so ties keep it — exactly as the stable full sort does).
|
|
551
|
+
*/
|
|
552
|
+
function selectTop(entries, k, compare) {
|
|
553
|
+
const top = [];
|
|
554
|
+
if (k <= 0) return top;
|
|
555
|
+
for (const entry of entries) {
|
|
556
|
+
if (top.length === k && compare(entry, top[k - 1]) >= 0) continue;
|
|
557
|
+
let lo = 0;
|
|
558
|
+
let hi = top.length;
|
|
559
|
+
while (lo < hi) {
|
|
560
|
+
const mid = lo + hi >>> 1;
|
|
561
|
+
if (compare(entry, top[mid]) < 0) hi = mid;
|
|
562
|
+
else lo = mid + 1;
|
|
563
|
+
}
|
|
564
|
+
top.splice(lo, 0, entry);
|
|
565
|
+
if (top.length > k) top.pop();
|
|
566
|
+
}
|
|
567
|
+
return top;
|
|
414
568
|
}
|
|
415
569
|
/**
|
|
416
570
|
* Applies `$skip` then `$limit` (both optional) via a single slice. No-op
|
|
@@ -439,24 +593,121 @@ function paginate(rows, skip, limit) {
|
|
|
439
593
|
*
|
|
440
594
|
* Top-level and nested dot-paths are supported; exotic Mongo projection quirks
|
|
441
595
|
* (array positional, `$slice`, etc.) are intentionally NOT replicated.
|
|
596
|
+
*
|
|
597
|
+
* Clones are `structuredClone`-equivalent. To project many rows the same way,
|
|
598
|
+
* compile once with {@link compileProjection}.
|
|
442
599
|
*/
|
|
443
600
|
function projectRow(row, projection, opts) {
|
|
601
|
+
return compileProjection(projection, opts)(row);
|
|
602
|
+
}
|
|
603
|
+
/**
|
|
604
|
+
* {@link projectRow} compiled for one projection: the projection map is read
|
|
605
|
+
* and every dot-path split once, then applied to each row.
|
|
606
|
+
*/
|
|
607
|
+
function compileProjection(projection, opts) {
|
|
444
608
|
const clone = opts?.clone ?? false;
|
|
445
609
|
const entries = projection ? Object.entries(projection) : [];
|
|
446
|
-
if (entries.length === 0) return clone ?
|
|
610
|
+
if (entries.length === 0) return clone ? cloneValue : (row) => row;
|
|
447
611
|
if (entries[0][1] === 1) {
|
|
448
612
|
const paths = new Set(entries.filter(([, v]) => v === 1).map(([k]) => k));
|
|
449
613
|
for (const pk of opts?.pkFields ?? []) paths.add(pk);
|
|
450
|
-
const
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
614
|
+
const plan = [...paths].map((path) => ({
|
|
615
|
+
read: pathReader(path),
|
|
616
|
+
segments: path.split(".")
|
|
617
|
+
}));
|
|
618
|
+
return (row) => {
|
|
619
|
+
const out = {};
|
|
620
|
+
for (const { read, segments } of plan) {
|
|
621
|
+
const value = read(row);
|
|
622
|
+
if (value !== void 0) setSegments(out, segments, value);
|
|
623
|
+
}
|
|
624
|
+
return clone ? cloneValue(out) : out;
|
|
625
|
+
};
|
|
456
626
|
}
|
|
457
|
-
const
|
|
458
|
-
|
|
459
|
-
|
|
627
|
+
const dropped = entries.filter(([, v]) => v === 0).map(([path]) => path.split("."));
|
|
628
|
+
return (row) => {
|
|
629
|
+
const out = cloneValue(row);
|
|
630
|
+
for (const segments of dropped) (0, _atscript_db.deletePath)(out, segments);
|
|
631
|
+
return out;
|
|
632
|
+
};
|
|
633
|
+
}
|
|
634
|
+
//#endregion
|
|
635
|
+
//#region src/lib/memory-unique.ts
|
|
636
|
+
/** Builds an empty {@link RecordedUniqueIndex}. */
|
|
637
|
+
function recordUniqueIndex(name, fields, optionalFields) {
|
|
638
|
+
return {
|
|
639
|
+
name,
|
|
640
|
+
fields,
|
|
641
|
+
readers: fields.map((field) => pathReader(field)),
|
|
642
|
+
optional: fields.map((field) => optionalFields.has(field)),
|
|
643
|
+
entries: /* @__PURE__ */ new Map()
|
|
644
|
+
};
|
|
645
|
+
}
|
|
646
|
+
/**
|
|
647
|
+
* Identity of `row`'s tuple in `index`, or `undefined` when the row can never
|
|
648
|
+
* collide in it. Two rows collide exactly when every component is equal under
|
|
649
|
+
* the memory filter's strict equality (`valuesEqual`: `===`, `Date`s by
|
|
650
|
+
* instant), so each component is type-tagged:
|
|
651
|
+
*
|
|
652
|
+
* - an optional member absent / `null` → `undefined` (present-only index);
|
|
653
|
+
* - `NaN`, an invalid `Date`, or any other object (arrays included — compared
|
|
654
|
+
* by reference, and rows never share one) → `undefined`: equal to nothing;
|
|
655
|
+
* - a required member that is `null` or missing is a value like any other
|
|
656
|
+
* (`null` ≠ missing).
|
|
657
|
+
*/
|
|
658
|
+
function uniqueTupleKey(index, row) {
|
|
659
|
+
const parts = [];
|
|
660
|
+
for (let i = 0; i < index.readers.length; i++) {
|
|
661
|
+
const value = index.readers[i](row);
|
|
662
|
+
if (index.optional[i] && (value === null || value === void 0)) return;
|
|
663
|
+
const part = tuplePart(value);
|
|
664
|
+
if (part === void 0) return;
|
|
665
|
+
parts.push(part);
|
|
666
|
+
}
|
|
667
|
+
return JSON.stringify(parts);
|
|
668
|
+
}
|
|
669
|
+
function tuplePart(value) {
|
|
670
|
+
switch (typeof value) {
|
|
671
|
+
case "string": return `s:${value}`;
|
|
672
|
+
case "number": return Number.isNaN(value) ? void 0 : `n:${value === 0 ? 0 : value}`;
|
|
673
|
+
case "bigint": return `b:${value}`;
|
|
674
|
+
case "boolean": return `t:${value}`;
|
|
675
|
+
case "undefined": return "u";
|
|
676
|
+
default:
|
|
677
|
+
if (value === null) return "z";
|
|
678
|
+
if (value instanceof Date) {
|
|
679
|
+
const time = value.getTime();
|
|
680
|
+
return Number.isNaN(time) ? void 0 : `d:${time}`;
|
|
681
|
+
}
|
|
682
|
+
return;
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
/** Records `row` (stored under `key`) in every index; `tuples` are its precomputed tuple keys. */
|
|
686
|
+
function indexRow(indexes, key, row, tuples) {
|
|
687
|
+
for (let i = 0; i < indexes.length; i++) {
|
|
688
|
+
const index = indexes[i];
|
|
689
|
+
const tuple = tuples ? tuples[i] : uniqueTupleKey(index, row);
|
|
690
|
+
if (tuple === void 0) continue;
|
|
691
|
+
const holders = index.entries.get(tuple);
|
|
692
|
+
if (holders) holders.add(key);
|
|
693
|
+
else index.entries.set(tuple, new Set([key]));
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
/** Removes `row` (stored under `key`) from every index. */
|
|
697
|
+
function unindexRow(indexes, key, row) {
|
|
698
|
+
for (const index of indexes) {
|
|
699
|
+
const tuple = uniqueTupleKey(index, row);
|
|
700
|
+
if (tuple === void 0) continue;
|
|
701
|
+
const holders = index.entries.get(tuple);
|
|
702
|
+
if (holders?.delete(key) && holders.size === 0) index.entries.delete(tuple);
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
/** Whether a row other than the one stored under `excludeKey` holds `tuple`. */
|
|
706
|
+
function tupleTaken(index, tuple, excludeKey) {
|
|
707
|
+
const holders = index.entries.get(tuple);
|
|
708
|
+
if (!holders) return false;
|
|
709
|
+
if (excludeKey === void 0 || !holders.has(excludeKey)) return holders.size > 0;
|
|
710
|
+
return holders.size > 1;
|
|
460
711
|
}
|
|
461
712
|
//#endregion
|
|
462
713
|
//#region src/lib/memory-aggregate.ts
|
|
@@ -551,7 +802,7 @@ function aggregateRows(rows, controls) {
|
|
|
551
802
|
}
|
|
552
803
|
/** Group values and min / max results may be store-owned objects (JSON values): hand back copies. */
|
|
553
804
|
function copyValue(value) {
|
|
554
|
-
return value !== null && typeof value === "object" ?
|
|
805
|
+
return value !== null && typeof value === "object" ? cloneValue(value) : value;
|
|
555
806
|
}
|
|
556
807
|
/** Reads one `$groupBy` key off a row: a bucket alias → its label, a path → its value. */
|
|
557
808
|
function groupKeyReader(key, controls) {
|
|
@@ -744,6 +995,8 @@ var MemoryAdapter = class MemoryAdapter extends _atscript_db.BaseDbAdapter {
|
|
|
744
995
|
_db = /* @__PURE__ */ new Map();
|
|
745
996
|
/** Memoized physical PK field names — stable for the adapter's lifetime. */
|
|
746
997
|
_pkFieldsCache;
|
|
998
|
+
/** Memoized compiled readers of {@link _physicalPkFields}. */
|
|
999
|
+
_pkReadersCache;
|
|
747
1000
|
/**
|
|
748
1001
|
* Memoized map of PHYSICAL field name → optional `start` for every field
|
|
749
1002
|
* carrying `@db.default.increment`. Stable per adapter (see
|
|
@@ -856,6 +1109,7 @@ var MemoryAdapter = class MemoryAdapter extends _atscript_db.BaseDbAdapter {
|
|
|
856
1109
|
kind: this._table.isView ? "view" : "table",
|
|
857
1110
|
rows: /* @__PURE__ */ new Map(),
|
|
858
1111
|
uniqueIndexes: [],
|
|
1112
|
+
opaquePk: false,
|
|
859
1113
|
incrementCounters: /* @__PURE__ */ new Map()
|
|
860
1114
|
};
|
|
861
1115
|
db.set(name, state);
|
|
@@ -876,6 +1130,10 @@ var MemoryAdapter = class MemoryAdapter extends _atscript_db.BaseDbAdapter {
|
|
|
876
1130
|
this._pkFieldsCache = fields;
|
|
877
1131
|
return fields;
|
|
878
1132
|
}
|
|
1133
|
+
/** Compiled readers of {@link _physicalPkFields} (memoized like it). */
|
|
1134
|
+
_pkReaders() {
|
|
1135
|
+
return this._pkReadersCache ??= this._physicalPkFields().map((field) => pathReader(field));
|
|
1136
|
+
}
|
|
879
1137
|
/**
|
|
880
1138
|
* PHYSICAL field name → optional `start` for every `@db.default.increment`
|
|
881
1139
|
* field. Discovered lazily from the table's field descriptors — each carries
|
|
@@ -955,8 +1213,25 @@ var MemoryAdapter = class MemoryAdapter extends _atscript_db.BaseDbAdapter {
|
|
|
955
1213
|
* `['a:b','c']` differ, and `1` differs from `'1'`.
|
|
956
1214
|
*/
|
|
957
1215
|
pkKey(row) {
|
|
958
|
-
const
|
|
959
|
-
return JSON.stringify(
|
|
1216
|
+
const readers = this._pkReaders();
|
|
1217
|
+
return JSON.stringify(readers.length === 1 ? [readers[0](row)] : readers.map((r) => r(row)));
|
|
1218
|
+
}
|
|
1219
|
+
/**
|
|
1220
|
+
* The storage key a filter pins by exact equality on EVERY primary-key field
|
|
1221
|
+
* — `{ id: v }`, `{ id: { $eq: v } }`, also inside (nested) `$and` — or
|
|
1222
|
+
* `undefined`. Only a string / number / boolean / `Date` value pins: for
|
|
1223
|
+
* those, a row the filter matches is stored exactly under this key (strict
|
|
1224
|
+
* equality ⇒ equal `JSON.stringify`), so a read can look the row up and
|
|
1225
|
+
* still run the full predicate on it. Unavailable once the table holds an
|
|
1226
|
+
* {@link MemoryTableState.opaquePk opaque} key.
|
|
1227
|
+
*/
|
|
1228
|
+
_pinnedKey(state, filter) {
|
|
1229
|
+
if (state.opaquePk) return;
|
|
1230
|
+
const fields = this._physicalPkFields();
|
|
1231
|
+
const pinned = /* @__PURE__ */ new Map();
|
|
1232
|
+
collectPins(filter, fields, pinned);
|
|
1233
|
+
if (pinned.size !== fields.length) return;
|
|
1234
|
+
return JSON.stringify(fields.map((field) => pinned.get(field)));
|
|
960
1235
|
}
|
|
961
1236
|
/**
|
|
962
1237
|
* Builds a DEFINED inserted-id for a table with NO single `@meta.id` — a
|
|
@@ -1065,20 +1340,21 @@ var MemoryAdapter = class MemoryAdapter extends _atscript_db.BaseDbAdapter {
|
|
|
1065
1340
|
/**
|
|
1066
1341
|
* Clones the payload in, applies the version default, generates any
|
|
1067
1342
|
* `@db.default.increment` values, enforces PK + unique constraints, stores
|
|
1068
|
-
* the row, and returns the resolved inserted id. Clone-in (
|
|
1069
|
-
* is what makes post-insert mutation of the
|
|
1343
|
+
* the row, and returns the resolved inserted id. Clone-in ({@link cloneValue},
|
|
1344
|
+
* `structuredClone`-equivalent) is what makes post-insert mutation of the
|
|
1345
|
+
* caller's object never leak into
|
|
1070
1346
|
* the store. Called once per item by both `insertOne` and `insertMany`, so
|
|
1071
1347
|
* increment values advance sequentially across a batch.
|
|
1072
1348
|
*/
|
|
1073
1349
|
_insertRow(state, data) {
|
|
1074
|
-
const row =
|
|
1350
|
+
const row = cloneValue(data);
|
|
1075
1351
|
const versionColumn = this._table.versionColumnPhysical;
|
|
1076
1352
|
if (versionColumn !== void 0 && !(versionColumn in row)) row[versionColumn] = 0;
|
|
1077
1353
|
this._applyIncrements(state.incrementCounters, row);
|
|
1078
1354
|
const key = this.pkKey(row);
|
|
1079
1355
|
if (state.rows.has(key)) throw this._pkConflict();
|
|
1080
|
-
this._enforceUniqueIndexes(state, row);
|
|
1081
|
-
|
|
1356
|
+
const tuples = this._enforceUniqueIndexes(state, row);
|
|
1357
|
+
this._storeRow(state, key, row, tuples);
|
|
1082
1358
|
const fallback = this._table.metaIdPhysical ? void 0 : this._compositeInsertedId(row);
|
|
1083
1359
|
return this._resolveInsertedId(row, fallback);
|
|
1084
1360
|
}
|
|
@@ -1086,38 +1362,40 @@ var MemoryAdapter = class MemoryAdapter extends _atscript_db.BaseDbAdapter {
|
|
|
1086
1362
|
* Enforces every recorded unique index against the current store. A row is
|
|
1087
1363
|
* exempted from an index (present-only semantics) when ANY of that index's
|
|
1088
1364
|
* optional fields is absent/`null`. Otherwise a stored row with an equal
|
|
1089
|
-
* tuple → `CONFLICT`.
|
|
1365
|
+
* tuple → `CONFLICT`. Returns the row's tuple key per index (for
|
|
1366
|
+
* {@link _storeRow}).
|
|
1090
1367
|
*
|
|
1091
1368
|
* `excludeKey` (when given) skips the row stored under that {@link pkKey} — so
|
|
1092
1369
|
* a row updating its own unique value does not false-conflict with itself.
|
|
1093
1370
|
*/
|
|
1094
1371
|
_enforceUniqueIndexes(state, row, excludeKey) {
|
|
1095
|
-
|
|
1096
|
-
const tuple =
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
path: index.fields[0] ?? index.name,
|
|
1116
|
-
message: `Duplicate value for unique index "${index.name}"`
|
|
1117
|
-
}]);
|
|
1118
|
-
}
|
|
1372
|
+
return state.uniqueIndexes.map((index) => {
|
|
1373
|
+
const tuple = uniqueTupleKey(index, row);
|
|
1374
|
+
if (tuple !== void 0 && tupleTaken(index, tuple, excludeKey)) throw new _atscript_db.DbError("CONFLICT", [{
|
|
1375
|
+
path: index.fields[0] ?? index.name,
|
|
1376
|
+
message: `Duplicate value for unique index "${index.name}"`
|
|
1377
|
+
}]);
|
|
1378
|
+
return tuple;
|
|
1379
|
+
});
|
|
1380
|
+
}
|
|
1381
|
+
/**
|
|
1382
|
+
* Stores `row` under `key` and records it in the unique indexes (`tuples`:
|
|
1383
|
+
* its precomputed tuple keys). The ONLY way a row enters the store, so the
|
|
1384
|
+
* indexes and {@link MemoryTableState.opaquePk} never drift from it.
|
|
1385
|
+
*/
|
|
1386
|
+
_storeRow(state, key, row, tuples) {
|
|
1387
|
+
state.rows.set(key, row);
|
|
1388
|
+
indexRow(state.uniqueIndexes, key, row, tuples);
|
|
1389
|
+
if (!state.opaquePk) for (const read of this._pkReaders()) {
|
|
1390
|
+
const value = read(row);
|
|
1391
|
+
if (value !== null && typeof value === "object" && !(value instanceof Date)) state.opaquePk = true;
|
|
1119
1392
|
}
|
|
1120
1393
|
}
|
|
1394
|
+
/** Removes the row stored under `key` (and its unique-index entries). */
|
|
1395
|
+
_removeRow(state, key, row) {
|
|
1396
|
+
state.rows.delete(key);
|
|
1397
|
+
unindexRow(state.uniqueIndexes, key, row);
|
|
1398
|
+
}
|
|
1121
1399
|
async insertOne(data) {
|
|
1122
1400
|
this._assertWritable();
|
|
1123
1401
|
return { insertedId: this._insertRow(this._state(), data) };
|
|
@@ -1171,7 +1449,14 @@ var MemoryAdapter = class MemoryAdapter extends _atscript_db.BaseDbAdapter {
|
|
|
1171
1449
|
if (expectedVersion !== void 0 && versionColumn === void 0) throw new Error("expectedVersion requires a versioned table");
|
|
1172
1450
|
const match = buildMemoryPredicate(filter, relationSets);
|
|
1173
1451
|
const matched = [];
|
|
1174
|
-
|
|
1452
|
+
if (!state) return matched;
|
|
1453
|
+
const pinned = this._pinnedKey(state, filter);
|
|
1454
|
+
let candidates = state.rows;
|
|
1455
|
+
if (pinned !== void 0) {
|
|
1456
|
+
const row = state.rows.get(pinned);
|
|
1457
|
+
candidates = row ? [[pinned, row]] : [];
|
|
1458
|
+
}
|
|
1459
|
+
for (const [key, row] of candidates) {
|
|
1175
1460
|
if (!match(row)) continue;
|
|
1176
1461
|
if (expectedVersion !== void 0 && row[versionColumn] !== expectedVersion) continue;
|
|
1177
1462
|
matched.push({
|
|
@@ -1204,7 +1489,7 @@ var MemoryAdapter = class MemoryAdapter extends _atscript_db.BaseDbAdapter {
|
|
|
1204
1489
|
* because this adapter reports no {@link supportsNativePatch}), so they must
|
|
1205
1490
|
* nest into the stored document — MERGING siblings — exactly like Mongo's
|
|
1206
1491
|
* `$set: { "profile.city": v }`, not create a literal dotted key. Top-level
|
|
1207
|
-
* (dot-free) keys behave as a plain assignment. `data` is
|
|
1492
|
+
* (dot-free) keys behave as a plain assignment. `data` is deep-cloned
|
|
1208
1493
|
* first so nested subtrees from the caller never alias into the store.
|
|
1209
1494
|
* - `ops.inc` / `ops.mul`: numeric increment / multiply on the (dot-path)
|
|
1210
1495
|
* target, coercing a missing or non-numeric current value to `0` (parity with
|
|
@@ -1215,22 +1500,24 @@ var MemoryAdapter = class MemoryAdapter extends _atscript_db.BaseDbAdapter {
|
|
|
1215
1500
|
* bump always wins over whatever `data`/`inc` wrote and lands on `oldVersion + 1`.
|
|
1216
1501
|
*/
|
|
1217
1502
|
_applyUpdate(row, data, ops) {
|
|
1218
|
-
const patch =
|
|
1503
|
+
const patch = cloneValue(data);
|
|
1219
1504
|
for (const k of Object.keys(patch)) setPath(row, k, patch[k]);
|
|
1220
1505
|
if (ops?.inc) for (const [col, n] of Object.entries(ops.inc)) setPath(row, col, (Number((0, _atscript_db.getPath)(row, col)) || 0) + n);
|
|
1221
1506
|
if (ops?.mul) for (const [col, n] of Object.entries(ops.mul)) setPath(row, col, (Number((0, _atscript_db.getPath)(row, col)) || 0) * n);
|
|
1222
1507
|
}
|
|
1223
1508
|
/**
|
|
1224
|
-
* Places `next` into the store under its (possibly changed) {@link pkKey}
|
|
1225
|
-
* re-keying when a mutation/replace
|
|
1226
|
-
* NEW key (some other row already
|
|
1227
|
-
* touching the Map so a failed
|
|
1509
|
+
* Places `next` into the store under its (possibly changed) {@link pkKey} in
|
|
1510
|
+
* place of `oldRow` (stored under `oldKey`), re-keying when a mutation/replace
|
|
1511
|
+
* moved the primary key. A collision on the NEW key (some other row already
|
|
1512
|
+
* owns it) throws `CONFLICT`. Throws BEFORE touching the Map so a failed
|
|
1513
|
+
* re-key leaves the store unchanged. `tuples` are `next`'s unique-index keys.
|
|
1228
1514
|
*/
|
|
1229
|
-
_commitRow(
|
|
1515
|
+
_commitRow(state, oldKey, oldRow, next, tuples) {
|
|
1230
1516
|
const newKey = this.pkKey(next);
|
|
1231
|
-
if (newKey !== oldKey && rows.has(newKey)) throw this._pkConflict();
|
|
1232
|
-
|
|
1233
|
-
|
|
1517
|
+
if (newKey !== oldKey && state.rows.has(newKey)) throw this._pkConflict();
|
|
1518
|
+
unindexRow(state.uniqueIndexes, oldKey, oldRow);
|
|
1519
|
+
if (newKey !== oldKey) state.rows.delete(oldKey);
|
|
1520
|
+
this._storeRow(state, newKey, next, tuples);
|
|
1234
1521
|
}
|
|
1235
1522
|
/**
|
|
1236
1523
|
* Update-then-commit for a single matched row: clones the pristine `row`,
|
|
@@ -1242,11 +1529,11 @@ var MemoryAdapter = class MemoryAdapter extends _atscript_db.BaseDbAdapter {
|
|
|
1242
1529
|
* bump (a version-exempt patch, since 0.1.150).
|
|
1243
1530
|
*/
|
|
1244
1531
|
_commitUpdate(state, oldKey, row, data, ops, keepVersion = false) {
|
|
1245
|
-
const next =
|
|
1532
|
+
const next = cloneValue(row);
|
|
1246
1533
|
this._applyUpdate(next, data, ops);
|
|
1247
1534
|
if (!keepVersion) this._bumpVersion(next, row);
|
|
1248
|
-
this._enforceUniqueIndexes(state, next, oldKey);
|
|
1249
|
-
this._commitRow(state
|
|
1535
|
+
const tuples = this._enforceUniqueIndexes(state, next, oldKey);
|
|
1536
|
+
this._commitRow(state, oldKey, row, next, tuples);
|
|
1250
1537
|
}
|
|
1251
1538
|
async replaceOne(filter, data, expectedVersion) {
|
|
1252
1539
|
this._assertWritable();
|
|
@@ -1258,10 +1545,10 @@ var MemoryAdapter = class MemoryAdapter extends _atscript_db.BaseDbAdapter {
|
|
|
1258
1545
|
modifiedCount: 0
|
|
1259
1546
|
};
|
|
1260
1547
|
const { key, row } = matched[0];
|
|
1261
|
-
const next =
|
|
1548
|
+
const next = cloneValue(data);
|
|
1262
1549
|
this._bumpVersion(next, row);
|
|
1263
|
-
this._enforceUniqueIndexes(state, next, key);
|
|
1264
|
-
this._commitRow(state
|
|
1550
|
+
const tuples = this._enforceUniqueIndexes(state, next, key);
|
|
1551
|
+
this._commitRow(state, key, row, next, tuples);
|
|
1265
1552
|
return {
|
|
1266
1553
|
matchedCount: 1,
|
|
1267
1554
|
modifiedCount: 1
|
|
@@ -1290,21 +1577,22 @@ var MemoryAdapter = class MemoryAdapter extends _atscript_db.BaseDbAdapter {
|
|
|
1290
1577
|
const state = this._peekState();
|
|
1291
1578
|
const matched = this._selectForWrite(state, filter, void 0, false, sets);
|
|
1292
1579
|
if (!state || matched.length === 0) return { deletedCount: 0 };
|
|
1293
|
-
|
|
1580
|
+
this._removeRow(state, matched[0].key, matched[0].row);
|
|
1294
1581
|
return { deletedCount: 1 };
|
|
1295
1582
|
}
|
|
1296
1583
|
/**
|
|
1297
1584
|
* Stable multi-key comparator from `$sort`, with a final tie-break on
|
|
1298
1585
|
* {@link pkKey} for a deterministic total order. Returns the input unchanged
|
|
1299
1586
|
* (insertion order) when there is no `$sort`. Delegates to the shared pure
|
|
1300
|
-
* {@link sortRows}, injecting {@link pkKey} as the total-order tie-break
|
|
1587
|
+
* {@link sortRows}, injecting {@link pkKey} as the total-order tie-break;
|
|
1588
|
+
* `topK` asks for the head of that order only (`$skip + $limit`).
|
|
1301
1589
|
*/
|
|
1302
|
-
_sortRows(rows, $sort) {
|
|
1303
|
-
return sortRows(rows, $sort, (r) => this.pkKey(r));
|
|
1590
|
+
_sortRows(rows, $sort, topK) {
|
|
1591
|
+
return sortRows(rows, $sort, (r) => this.pkKey(r), topK);
|
|
1304
1592
|
}
|
|
1305
1593
|
/**
|
|
1306
|
-
*
|
|
1307
|
-
* so the store can never be mutated through
|
|
1594
|
+
* Compiles the `$select` projection of one read: each returned row is a
|
|
1595
|
+
* fresh, deep-cloned object so the store can never be mutated through it.
|
|
1308
1596
|
*
|
|
1309
1597
|
* - No projection → a full clone.
|
|
1310
1598
|
* - INCLUSION form (`{ field: 1 }`) → a new object with exactly the selected
|
|
@@ -1315,8 +1603,8 @@ var MemoryAdapter = class MemoryAdapter extends _atscript_db.BaseDbAdapter {
|
|
|
1315
1603
|
* Top-level and nested dot-paths are supported; exotic Mongo projection
|
|
1316
1604
|
* quirks (array positional, `$slice`, etc.) are intentionally NOT replicated.
|
|
1317
1605
|
*/
|
|
1318
|
-
|
|
1319
|
-
return
|
|
1606
|
+
_projector($select) {
|
|
1607
|
+
return compileProjection($select?.asProjection, { clone: true });
|
|
1320
1608
|
}
|
|
1321
1609
|
/**
|
|
1322
1610
|
* Reads the pagination/sort/projection controls with their intended types.
|
|
@@ -1338,11 +1626,25 @@ var MemoryAdapter = class MemoryAdapter extends _atscript_db.BaseDbAdapter {
|
|
|
1338
1626
|
* shares. Goes through the {@link _loadRows} seam exactly ONCE per call, so a
|
|
1339
1627
|
* reader (and provider read-through mode) has one place that materializes the
|
|
1340
1628
|
* working set — one provider invocation per logical read.
|
|
1629
|
+
*
|
|
1630
|
+
* Stored mode reads the table's Map in place (no snapshot copy), resolves a
|
|
1631
|
+
* filter pinning the whole primary key by direct lookup, and stops after
|
|
1632
|
+
* `limit` matches when given (an unsorted `findOne`).
|
|
1341
1633
|
*/
|
|
1342
|
-
async _filteredRows(query) {
|
|
1634
|
+
async _filteredRows(query, limit) {
|
|
1343
1635
|
if (!(0, _atscript_db.containsRelationFilter)(query.filter)) {
|
|
1344
1636
|
const match = buildMemoryPredicate(query.filter);
|
|
1345
|
-
|
|
1637
|
+
if (!this._provider && this._loadRows === MemoryAdapter.prototype._loadRows) {
|
|
1638
|
+
const state = this._peekState();
|
|
1639
|
+
if (!state) return [];
|
|
1640
|
+
const pinned = this._pinnedKey(state, query.filter);
|
|
1641
|
+
if (pinned !== void 0) {
|
|
1642
|
+
const row = state.rows.get(pinned);
|
|
1643
|
+
return row && match(row) ? [row] : [];
|
|
1644
|
+
}
|
|
1645
|
+
return collectMatches(state.rows.values(), match, limit);
|
|
1646
|
+
}
|
|
1647
|
+
return collectMatches(await this._loadRows(), match, limit);
|
|
1346
1648
|
}
|
|
1347
1649
|
const own = Promise.resolve(this._loadRows());
|
|
1348
1650
|
own.catch(() => {});
|
|
@@ -1350,10 +1652,10 @@ var MemoryAdapter = class MemoryAdapter extends _atscript_db.BaseDbAdapter {
|
|
|
1350
1652
|
return (await own).filter(buildMemoryPredicate(query.filter, sets));
|
|
1351
1653
|
}
|
|
1352
1654
|
async findOne(query) {
|
|
1353
|
-
const filtered = await this._filteredRows(query);
|
|
1354
1655
|
const { $sort, $skip, $select } = this._readControls(query.controls ?? {});
|
|
1355
|
-
const
|
|
1356
|
-
|
|
1656
|
+
const index = $skip ?? 0;
|
|
1657
|
+
const row = (hasSort($sort) ? this._sortRows(await this._filteredRows(query), $sort, index + 1) : await this._filteredRows(query, index + 1))[index];
|
|
1658
|
+
return row ? this._projector($select)(row) : null;
|
|
1357
1659
|
}
|
|
1358
1660
|
async findMany(query) {
|
|
1359
1661
|
return (await this.findManyWithCount(query)).data;
|
|
@@ -1371,8 +1673,9 @@ var MemoryAdapter = class MemoryAdapter extends _atscript_db.BaseDbAdapter {
|
|
|
1371
1673
|
async findManyWithCount(query) {
|
|
1372
1674
|
const filtered = await this._filteredRows(query);
|
|
1373
1675
|
const { $sort, $skip, $limit, $select } = this._readControls(query.controls ?? {});
|
|
1676
|
+
const topK = $limit === void 0 ? void 0 : ($skip ?? 0) + $limit;
|
|
1374
1677
|
return {
|
|
1375
|
-
data: paginate(this._sortRows(filtered, $sort), $skip, $limit).map(
|
|
1678
|
+
data: paginate(this._sortRows(filtered, $sort, topK), $skip, $limit).map(this._projector($select)),
|
|
1376
1679
|
count: filtered.length
|
|
1377
1680
|
};
|
|
1378
1681
|
}
|
|
@@ -1446,7 +1749,7 @@ var MemoryAdapter = class MemoryAdapter extends _atscript_db.BaseDbAdapter {
|
|
|
1446
1749
|
const state = this._peekState();
|
|
1447
1750
|
if (!state) return { deletedCount: 0 };
|
|
1448
1751
|
const matched = this._selectForWrite(state, filter, void 0, true, sets);
|
|
1449
|
-
for (const { key } of matched)
|
|
1752
|
+
for (const { key, row } of matched) this._removeRow(state, key, row);
|
|
1450
1753
|
return { deletedCount: matched.length };
|
|
1451
1754
|
}
|
|
1452
1755
|
/**
|
|
@@ -1458,13 +1761,11 @@ var MemoryAdapter = class MemoryAdapter extends _atscript_db.BaseDbAdapter {
|
|
|
1458
1761
|
const uniqueIndexes = [];
|
|
1459
1762
|
for (const index of this._table.indexes.values()) {
|
|
1460
1763
|
if (index.type !== "unique") continue;
|
|
1461
|
-
uniqueIndexes.push(
|
|
1462
|
-
name: index.name,
|
|
1463
|
-
fields: index.fields.map((f) => f.name),
|
|
1464
|
-
optionalFields: new Set(index.fields.filter((f) => f.optional).map((f) => f.name))
|
|
1465
|
-
});
|
|
1764
|
+
uniqueIndexes.push(recordUniqueIndex(index.name, index.fields.map((f) => f.name), new Set(index.fields.filter((f) => f.optional).map((f) => f.name))));
|
|
1466
1765
|
}
|
|
1467
|
-
this._state()
|
|
1766
|
+
const state = this._state();
|
|
1767
|
+
state.uniqueIndexes = uniqueIndexes;
|
|
1768
|
+
for (const [key, row] of state.rows) indexRow(uniqueIndexes, key, row);
|
|
1468
1769
|
}
|
|
1469
1770
|
/** Creates the table's (empty) state when it does not exist. Idempotent. */
|
|
1470
1771
|
async ensureTable() {
|
|
@@ -1513,6 +1814,38 @@ var MemoryAdapter = class MemoryAdapter extends _atscript_db.BaseDbAdapter {
|
|
|
1513
1814
|
this._db.delete(name);
|
|
1514
1815
|
}
|
|
1515
1816
|
};
|
|
1817
|
+
/** Whether a `$sort` control orders by at least one key. */
|
|
1818
|
+
function hasSort($sort) {
|
|
1819
|
+
return $sort !== void 0 && Object.keys($sort).length > 0;
|
|
1820
|
+
}
|
|
1821
|
+
/** The rows of `rows` matching `match`, in order — at most `limit` when given. */
|
|
1822
|
+
function collectMatches(rows, match, limit) {
|
|
1823
|
+
const out = [];
|
|
1824
|
+
if (limit !== void 0 && limit <= 0) return out;
|
|
1825
|
+
for (const row of rows) if (match(row)) {
|
|
1826
|
+
out.push(row);
|
|
1827
|
+
if (out.length === limit) break;
|
|
1828
|
+
}
|
|
1829
|
+
return out;
|
|
1830
|
+
}
|
|
1831
|
+
/**
|
|
1832
|
+
* Collects into `pinned` the primary-key fields `filter` pins by exact equality
|
|
1833
|
+
* on a string / number / boolean / `Date` value — at its top level and inside
|
|
1834
|
+
* (nested) `$and`s, which `walkFilter` conjoins the same way. The first pin of
|
|
1835
|
+
* a field wins: a contradicting second one is still checked by the predicate.
|
|
1836
|
+
*/
|
|
1837
|
+
function collectPins(filter, fields, pinned) {
|
|
1838
|
+
if (filter === null || typeof filter !== "object" || Array.isArray(filter)) return;
|
|
1839
|
+
for (const [key, value] of Object.entries(filter)) {
|
|
1840
|
+
if (key === "$and") {
|
|
1841
|
+
if (Array.isArray(value)) for (const child of value) collectPins(child, fields, pinned);
|
|
1842
|
+
continue;
|
|
1843
|
+
}
|
|
1844
|
+
if (pinned.has(key) || !fields.includes(key)) continue;
|
|
1845
|
+
const operand = (0, _atscript_db.isPlainObject)(value) ? value.$eq : value;
|
|
1846
|
+
if (typeof operand === "string" || typeof operand === "number" || typeof operand === "boolean" || operand instanceof Date) pinned.set(key, operand);
|
|
1847
|
+
}
|
|
1848
|
+
}
|
|
1516
1849
|
/**
|
|
1517
1850
|
* Ergonomic late-binding entry point for provider (read-through) mode. Resolves
|
|
1518
1851
|
* the ALREADY-BUILT {@link MemoryAdapter} backing `type` on `space` (reached
|