@atscript/db-memory 0.1.167 → 0.1.169
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 +108 -38
- package/dist/index.d.cts +13 -18
- package/dist/index.d.mts +13 -18
- package/dist/index.mjs +109 -39
- package/package.json +9 -9
package/dist/index.cjs
CHANGED
|
@@ -103,7 +103,7 @@ function valuesEqual(a, b) {
|
|
|
103
103
|
return a === b;
|
|
104
104
|
}
|
|
105
105
|
/**
|
|
106
|
-
* Mirror of `mongo-
|
|
106
|
+
* Mirror of `mongo-view-expr.ts`'s `parseRegexString`: normalize a `$regex` value
|
|
107
107
|
* (or a bare `RegExp`) into a `{ pattern, flags }` pair. Accepts a `RegExp`
|
|
108
108
|
* instance, a `/pattern/flags` string literal, or a plain string (treated as
|
|
109
109
|
* the literal pattern with no flags).
|
|
@@ -194,10 +194,12 @@ function toOrdinal(v) {
|
|
|
194
194
|
}
|
|
195
195
|
/**
|
|
196
196
|
* `$gt`/`$gte`/`$lt`/`$lte`. A missing or `null` field never matches an
|
|
197
|
-
* ordering comparison
|
|
198
|
-
*
|
|
199
|
-
*
|
|
200
|
-
*
|
|
197
|
+
* ordering comparison, and neither does a `null` operand — the visitor
|
|
198
|
+
* compiles that to "no match" before reaching here (SQL's UNKNOWN, MongoDB's
|
|
199
|
+
* type bracketing — JS would read it as `0`). `Date` operands are
|
|
200
|
+
* normalized to epoch millis; all other comparisons use plain JS ordering
|
|
201
|
+
* (numbers numerically, strings lexicographically) — NO collation or locale
|
|
202
|
+
* awareness. This intentionally differs from SQL engines' collated ordering.
|
|
201
203
|
*/
|
|
202
204
|
function evalRelational(fieldValue, op, value) {
|
|
203
205
|
if (fieldValue === void 0 || fieldValue === null) return false;
|
|
@@ -246,7 +248,9 @@ const memoryVisitor = {
|
|
|
246
248
|
case "$gt":
|
|
247
249
|
case "$gte":
|
|
248
250
|
case "$lt":
|
|
249
|
-
case "$lte":
|
|
251
|
+
case "$lte":
|
|
252
|
+
if (value == null) return () => false;
|
|
253
|
+
return (row) => evalRelational(read(row), op, value);
|
|
250
254
|
case "$in": {
|
|
251
255
|
const isIn = compileIn(value);
|
|
252
256
|
return (row) => isIn(read(row));
|
|
@@ -746,7 +750,16 @@ function tupleTaken(index, tuple, excludeKey) {
|
|
|
746
750
|
* `@uniqu/core` kernel label (the SQLite UDF runs the same kernel);
|
|
747
751
|
* - `sum` / `avg` over no numeric value are `null`; `min` / `max` order with
|
|
748
752
|
* the `$sort` comparator; `countDistinct` counts distinct non-null values
|
|
749
|
-
* by group identity (below)
|
|
753
|
+
* by group identity (below) — over a field list (since 0.1.168), distinct
|
|
754
|
+
* combinations, a row with any null / missing field not counted; any other
|
|
755
|
+
* `$fn` is `INVALID_QUERY`;
|
|
756
|
+
* - a conditional aggregate (`$where`, since 0.1.168) feeds its accumulator
|
|
757
|
+
* only the rows its condition matches — the adapter's own filter predicate
|
|
758
|
+
* ({@link buildMemoryPredicate}), so it reads like the query filter;
|
|
759
|
+
* - a two-level aggregate (`controls.$from`, since 0.1.168) groups the rows
|
|
760
|
+
* of its inner level (this function over `$from`) by the outer controls —
|
|
761
|
+
* every inner column (an inner `$groupBy` key at its path, an inner alias
|
|
762
|
+
* at the top) is read like a field of a table row;
|
|
750
763
|
* - group identity is type-tagged: `1` and `"1"` never share a group (a
|
|
751
764
|
* number and a bigint of one value do), `Date`s group by instant, JSON
|
|
752
765
|
* values by a key-order-independent serialization;
|
|
@@ -755,16 +768,17 @@ function tupleTaken(index, tuple, excludeKey) {
|
|
|
755
768
|
* object-valued leaves copied so no store-owned value escapes.
|
|
756
769
|
*/
|
|
757
770
|
function aggregateRows(rows, controls) {
|
|
771
|
+
if (controls.$from) rows = aggregateRows(rows, controls.$from);
|
|
758
772
|
const $select = controls.$select;
|
|
759
773
|
const groupBy = controls.$groupBy ?? [];
|
|
760
774
|
const entries = [
|
|
761
775
|
...($select?.aggregates ?? []).map((expr) => ({
|
|
762
776
|
alias: (0, _atscript_db_agg.resolveAlias)(expr),
|
|
763
|
-
create: accumulatorFactory(expr)
|
|
777
|
+
create: conditional(accumulatorFactory(expr), expr.$where)
|
|
764
778
|
})),
|
|
765
779
|
...($select?.exprAggregates ?? []).map((e) => ({
|
|
766
780
|
alias: e.alias,
|
|
767
|
-
create: exprAccumulatorFactory(e)
|
|
781
|
+
create: conditional(exprAccumulatorFactory(e), e.where)
|
|
768
782
|
})),
|
|
769
783
|
...($select?.firstLast ?? []).map((fl) => ({
|
|
770
784
|
alias: fl.alias,
|
|
@@ -872,6 +886,23 @@ function numericValue(value) {
|
|
|
872
886
|
return Number.isNaN(n) ? void 0 : n;
|
|
873
887
|
}
|
|
874
888
|
}
|
|
889
|
+
/**
|
|
890
|
+
* An accumulator fed only the rows `where` matches (a conditional aggregate,
|
|
891
|
+
* since 0.1.168) — `create` unchanged without a condition.
|
|
892
|
+
*/
|
|
893
|
+
function conditional(create, where) {
|
|
894
|
+
if (where === void 0) return create;
|
|
895
|
+
const matches = buildMemoryPredicate(where);
|
|
896
|
+
return () => {
|
|
897
|
+
const acc = create();
|
|
898
|
+
return {
|
|
899
|
+
add: (row) => {
|
|
900
|
+
if (matches(row)) acc.add(row);
|
|
901
|
+
},
|
|
902
|
+
result: () => acc.result()
|
|
903
|
+
};
|
|
904
|
+
};
|
|
905
|
+
}
|
|
875
906
|
/** `sum` / `avg` / `min` / `max` over a per-row arithmetic expression; NULL results are skipped like NULL fields. */
|
|
876
907
|
function exprAccumulatorFactory(e) {
|
|
877
908
|
const readers = new Map(e.names.map((name) => [name, pathReader(name)]));
|
|
@@ -942,12 +973,39 @@ function fnAccumulator(fn, read) {
|
|
|
942
973
|
};
|
|
943
974
|
}
|
|
944
975
|
/**
|
|
976
|
+
* `countDistinct` over a field list: distinct combinations by their
|
|
977
|
+
* length-prefixed {@link identityToken}s (type-tagged — `1` and `"1"`
|
|
978
|
+
* differ; no value forges a separator); a row with any null / missing field
|
|
979
|
+
* is not counted (SQL's `COUNT(DISTINCT a, b)`).
|
|
980
|
+
*/
|
|
981
|
+
function distinctListFactory(fields) {
|
|
982
|
+
const readers = fields.map((field) => pathReader(field));
|
|
983
|
+
return () => {
|
|
984
|
+
const seen = /* @__PURE__ */ new Set();
|
|
985
|
+
return {
|
|
986
|
+
add: (row) => {
|
|
987
|
+
let identity = "";
|
|
988
|
+
for (const read of readers) {
|
|
989
|
+
const v = read(row);
|
|
990
|
+
if (v == null) return;
|
|
991
|
+
const token = identityToken(v);
|
|
992
|
+
identity += `${token.length}:${token}`;
|
|
993
|
+
}
|
|
994
|
+
seen.add(identity);
|
|
995
|
+
},
|
|
996
|
+
result: () => seen.size
|
|
997
|
+
};
|
|
998
|
+
};
|
|
999
|
+
}
|
|
1000
|
+
/**
|
|
945
1001
|
* Resolves a plain aggregate (`first` / `last` are {@link firstLastFactory}'s)
|
|
946
|
-
* to an accumulator factory (one accumulator per group
|
|
947
|
-
* so an unknown `$fn` is
|
|
1002
|
+
* to an accumulator factory (one accumulator per group; a `$where` is applied
|
|
1003
|
+
* by {@link conditional}). Validated up front, so an unknown `$fn` is
|
|
1004
|
+
* rejected even when there are no rows.
|
|
948
1005
|
*/
|
|
949
1006
|
function accumulatorFactory(expr) {
|
|
950
1007
|
(0, _atscript_db_agg.assertAggregateFn)(expr.$fn);
|
|
1008
|
+
if (typeof expr.$field !== "string") return distinctListFactory((0, _atscript_db.aggregateColumns)(expr));
|
|
951
1009
|
const field = expr.$field;
|
|
952
1010
|
const read = pathReader(field);
|
|
953
1011
|
if (expr.$fn === "count") {
|
|
@@ -1245,27 +1303,6 @@ var MemoryAdapter = class MemoryAdapter extends _atscript_db.BaseDbAdapter {
|
|
|
1245
1303
|
return JSON.stringify(fields.map((field) => pinned.get(field)));
|
|
1246
1304
|
}
|
|
1247
1305
|
/**
|
|
1248
|
-
* Builds a DEFINED inserted-id for a table with NO single `@meta.id` — a
|
|
1249
|
-
* composite (or single non-meta) primary key, where
|
|
1250
|
-
* {@link BaseDbAdapter._resolveInsertedId} would otherwise fall back to
|
|
1251
|
-
* `undefined` (memory has no rowid/`_id` to hand back). Returns an object
|
|
1252
|
-
* mapping each PRIMARY KEY PHYSICAL field name → its value in the stored row
|
|
1253
|
-
* (e.g. `{ part1: "a", part2: "b" }`), read by physical name via
|
|
1254
|
-
* {@link getPath} so it matches how {@link pkKey} derives the storage key and
|
|
1255
|
-
* honours column renames. A single-field non-meta PK yields the one-key object
|
|
1256
|
-
* form for consistency (documented shape).
|
|
1257
|
-
*
|
|
1258
|
-
* Passed as the `dbGeneratedId` fallback in {@link _insertRow} ONLY when
|
|
1259
|
-
* {@link AtscriptDbReadable.metaIdPhysical} is null; single-`@meta.id` tables
|
|
1260
|
-
* keep an `undefined` fallback, so their scalar `insertedId`
|
|
1261
|
-
* (`row[metaIdPhysical]`) is byte-identical to before.
|
|
1262
|
-
*/
|
|
1263
|
-
_compositeInsertedId(row) {
|
|
1264
|
-
const id = {};
|
|
1265
|
-
for (const field of this._physicalPkFields()) id[field] = (0, _atscript_db.getPath)(row, field);
|
|
1266
|
-
return id;
|
|
1267
|
-
}
|
|
1268
|
-
/**
|
|
1269
1306
|
* Switches this adapter into PROVIDER-BACKED mode: `fn` is invoked on every
|
|
1270
1307
|
* read to recompute the table's rows (e.g. a Redis/job-manager snapshot), so a
|
|
1271
1308
|
* runtime-owned entity with NO database can be observed as a read-only
|
|
@@ -1366,8 +1403,7 @@ var MemoryAdapter = class MemoryAdapter extends _atscript_db.BaseDbAdapter {
|
|
|
1366
1403
|
if (state.rows.has(key)) throw this._pkConflict();
|
|
1367
1404
|
const tuples = this._enforceUniqueIndexes(state, row);
|
|
1368
1405
|
this._storeRow(state, key, row, tuples);
|
|
1369
|
-
|
|
1370
|
-
return this._resolveInsertedId(row, fallback);
|
|
1406
|
+
return this._resolveInsertedId(row, void 0);
|
|
1371
1407
|
}
|
|
1372
1408
|
/**
|
|
1373
1409
|
* Enforces every recorded unique index against the current store. A row is
|
|
@@ -1728,7 +1764,9 @@ var MemoryAdapter = class MemoryAdapter extends _atscript_db.BaseDbAdapter {
|
|
|
1728
1764
|
* pure {@link aggregateRows} engine, so provider (read-through) mode works
|
|
1729
1765
|
* unchanged. SQL-adapter semantics: null and missing group together,
|
|
1730
1766
|
* `sum` / `avg` over no numeric value are `null`, `$count` counts the groups
|
|
1731
|
-
* that survive `$having`.
|
|
1767
|
+
* that survive `$having`. Conditional aggregates, `countDistinct` field
|
|
1768
|
+
* lists and two-level aggregates (`$from`, over the same snapshot) run in
|
|
1769
|
+
* the engine too (since 0.1.168).
|
|
1732
1770
|
*
|
|
1733
1771
|
* `$search` never reaches here: this adapter is not searchable, so the core
|
|
1734
1772
|
* rejects a grouped search before dispatch.
|
|
@@ -1752,6 +1790,18 @@ var MemoryAdapter = class MemoryAdapter extends _atscript_db.BaseDbAdapter {
|
|
|
1752
1790
|
supportsAggregateExpressions() {
|
|
1753
1791
|
return true;
|
|
1754
1792
|
}
|
|
1793
|
+
/** Conditional aggregates (`$where`) — the memory filter predicate over each row. @since 0.1.168 */
|
|
1794
|
+
supportsConditionalAggregates() {
|
|
1795
|
+
return true;
|
|
1796
|
+
}
|
|
1797
|
+
/** `countDistinct` over a field list — distinct combinations by type-tagged identity. @since 0.1.168 */
|
|
1798
|
+
supportsDistinctFieldLists() {
|
|
1799
|
+
return true;
|
|
1800
|
+
}
|
|
1801
|
+
/** Two-level aggregates (`$from`) — the grouping engine over its own output rows. @since 0.1.168 */
|
|
1802
|
+
supportsNestedAggregates() {
|
|
1803
|
+
return true;
|
|
1804
|
+
}
|
|
1755
1805
|
/** NULL placement (`$nulls`, `@db.sort.nulls`) — `sortRows` honours it per key. */
|
|
1756
1806
|
supportsNullsPlacement() {
|
|
1757
1807
|
return true;
|
|
@@ -1819,16 +1869,36 @@ var MemoryAdapter = class MemoryAdapter extends _atscript_db.BaseDbAdapter {
|
|
|
1819
1869
|
* Records the model's `unique` indexes for insert-time enforcement. Idempotent
|
|
1820
1870
|
* (replaces the recorded set). Non-unique index types are ignored — an
|
|
1821
1871
|
* in-memory scan needs no plain/fulltext/geo index to answer queries.
|
|
1872
|
+
*
|
|
1873
|
+
* A unique index the stored rows already break is not recorded and the call
|
|
1874
|
+
* throws `CONFLICT` naming it — as `CREATE UNIQUE INDEX` fails over
|
|
1875
|
+
* duplicates on SQL — after recording the indexes that hold.
|
|
1822
1876
|
*/
|
|
1823
1877
|
async syncIndexes() {
|
|
1878
|
+
const state = this._state();
|
|
1824
1879
|
const uniqueIndexes = [];
|
|
1880
|
+
const broken = [];
|
|
1825
1881
|
for (const index of this._table.indexes.values()) {
|
|
1826
1882
|
if (index.type !== "unique") continue;
|
|
1827
|
-
|
|
1883
|
+
const recorded = recordUniqueIndex(index.name, index.fields.map((f) => f.name), new Set(index.fields.filter((f) => f.optional).map((f) => f.name)));
|
|
1884
|
+
let holds = true;
|
|
1885
|
+
for (const [key, row] of state.rows) {
|
|
1886
|
+
const tuple = uniqueTupleKey(recorded, row);
|
|
1887
|
+
if (tuple === void 0) continue;
|
|
1888
|
+
if (recorded.entries.has(tuple)) {
|
|
1889
|
+
holds = false;
|
|
1890
|
+
break;
|
|
1891
|
+
}
|
|
1892
|
+
recorded.entries.set(tuple, new Set([key]));
|
|
1893
|
+
}
|
|
1894
|
+
(holds ? uniqueIndexes : broken).push(recorded);
|
|
1828
1895
|
}
|
|
1829
|
-
const state = this._state();
|
|
1830
1896
|
state.uniqueIndexes = uniqueIndexes;
|
|
1831
|
-
|
|
1897
|
+
const [first] = broken;
|
|
1898
|
+
if (first) throw (0, _atscript_db.constraintViolation)("CONFLICT", {
|
|
1899
|
+
name: first.name,
|
|
1900
|
+
columns: first.fields
|
|
1901
|
+
}, `Cannot create unique index ${broken.map((i) => `"${i.name}"`).join(", ")}: stored rows hold duplicate values`);
|
|
1832
1902
|
}
|
|
1833
1903
|
/** Creates the table's (empty) state when it does not exist. Idempotent. */
|
|
1834
1904
|
async ensureTable() {
|
package/dist/index.d.cts
CHANGED
|
@@ -189,23 +189,6 @@ declare class MemoryAdapter extends BaseDbAdapter {
|
|
|
189
189
|
* {@link MemoryTableState.opaquePk opaque} key.
|
|
190
190
|
*/
|
|
191
191
|
private _pinnedKey;
|
|
192
|
-
/**
|
|
193
|
-
* Builds a DEFINED inserted-id for a table with NO single `@meta.id` — a
|
|
194
|
-
* composite (or single non-meta) primary key, where
|
|
195
|
-
* {@link BaseDbAdapter._resolveInsertedId} would otherwise fall back to
|
|
196
|
-
* `undefined` (memory has no rowid/`_id` to hand back). Returns an object
|
|
197
|
-
* mapping each PRIMARY KEY PHYSICAL field name → its value in the stored row
|
|
198
|
-
* (e.g. `{ part1: "a", part2: "b" }`), read by physical name via
|
|
199
|
-
* {@link getPath} so it matches how {@link pkKey} derives the storage key and
|
|
200
|
-
* honours column renames. A single-field non-meta PK yields the one-key object
|
|
201
|
-
* form for consistency (documented shape).
|
|
202
|
-
*
|
|
203
|
-
* Passed as the `dbGeneratedId` fallback in {@link _insertRow} ONLY when
|
|
204
|
-
* {@link AtscriptDbReadable.metaIdPhysical} is null; single-`@meta.id` tables
|
|
205
|
-
* keep an `undefined` fallback, so their scalar `insertedId`
|
|
206
|
-
* (`row[metaIdPhysical]`) is byte-identical to before.
|
|
207
|
-
*/
|
|
208
|
-
private _compositeInsertedId;
|
|
209
192
|
/**
|
|
210
193
|
* Switches this adapter into PROVIDER-BACKED mode: `fn` is invoked on every
|
|
211
194
|
* read to recompute the table's rows (e.g. a Redis/job-manager snapshot), so a
|
|
@@ -443,7 +426,9 @@ declare class MemoryAdapter extends BaseDbAdapter {
|
|
|
443
426
|
* pure {@link aggregateRows} engine, so provider (read-through) mode works
|
|
444
427
|
* unchanged. SQL-adapter semantics: null and missing group together,
|
|
445
428
|
* `sum` / `avg` over no numeric value are `null`, `$count` counts the groups
|
|
446
|
-
* that survive `$having`.
|
|
429
|
+
* that survive `$having`. Conditional aggregates, `countDistinct` field
|
|
430
|
+
* lists and two-level aggregates (`$from`, over the same snapshot) run in
|
|
431
|
+
* the engine too (since 0.1.168).
|
|
447
432
|
*
|
|
448
433
|
* `$search` never reaches here: this adapter is not searchable, so the core
|
|
449
434
|
* rejects a grouped search before dispatch.
|
|
@@ -459,6 +444,12 @@ declare class MemoryAdapter extends BaseDbAdapter {
|
|
|
459
444
|
aggregateFns(): ReadonlySet<AggregateFn>;
|
|
460
445
|
/** Arithmetic in an aggregate `$select` (`{ $expr }`, `{ $fn, $expr }`). */
|
|
461
446
|
supportsAggregateExpressions(): boolean;
|
|
447
|
+
/** Conditional aggregates (`$where`) — the memory filter predicate over each row. @since 0.1.168 */
|
|
448
|
+
supportsConditionalAggregates(): boolean;
|
|
449
|
+
/** `countDistinct` over a field list — distinct combinations by type-tagged identity. @since 0.1.168 */
|
|
450
|
+
supportsDistinctFieldLists(): boolean;
|
|
451
|
+
/** Two-level aggregates (`$from`) — the grouping engine over its own output rows. @since 0.1.168 */
|
|
452
|
+
supportsNestedAggregates(): boolean;
|
|
462
453
|
/** NULL placement (`$nulls`, `@db.sort.nulls`) — `sortRows` honours it per key. */
|
|
463
454
|
supportsNullsPlacement(): boolean;
|
|
464
455
|
/**
|
|
@@ -479,6 +470,10 @@ declare class MemoryAdapter extends BaseDbAdapter {
|
|
|
479
470
|
* Records the model's `unique` indexes for insert-time enforcement. Idempotent
|
|
480
471
|
* (replaces the recorded set). Non-unique index types are ignored — an
|
|
481
472
|
* in-memory scan needs no plain/fulltext/geo index to answer queries.
|
|
473
|
+
*
|
|
474
|
+
* A unique index the stored rows already break is not recorded and the call
|
|
475
|
+
* throws `CONFLICT` naming it — as `CREATE UNIQUE INDEX` fails over
|
|
476
|
+
* duplicates on SQL — after recording the indexes that hold.
|
|
482
477
|
*/
|
|
483
478
|
syncIndexes(): Promise<void>;
|
|
484
479
|
/** Creates the table's (empty) state when it does not exist. Idempotent. */
|
package/dist/index.d.mts
CHANGED
|
@@ -189,23 +189,6 @@ declare class MemoryAdapter extends BaseDbAdapter {
|
|
|
189
189
|
* {@link MemoryTableState.opaquePk opaque} key.
|
|
190
190
|
*/
|
|
191
191
|
private _pinnedKey;
|
|
192
|
-
/**
|
|
193
|
-
* Builds a DEFINED inserted-id for a table with NO single `@meta.id` — a
|
|
194
|
-
* composite (or single non-meta) primary key, where
|
|
195
|
-
* {@link BaseDbAdapter._resolveInsertedId} would otherwise fall back to
|
|
196
|
-
* `undefined` (memory has no rowid/`_id` to hand back). Returns an object
|
|
197
|
-
* mapping each PRIMARY KEY PHYSICAL field name → its value in the stored row
|
|
198
|
-
* (e.g. `{ part1: "a", part2: "b" }`), read by physical name via
|
|
199
|
-
* {@link getPath} so it matches how {@link pkKey} derives the storage key and
|
|
200
|
-
* honours column renames. A single-field non-meta PK yields the one-key object
|
|
201
|
-
* form for consistency (documented shape).
|
|
202
|
-
*
|
|
203
|
-
* Passed as the `dbGeneratedId` fallback in {@link _insertRow} ONLY when
|
|
204
|
-
* {@link AtscriptDbReadable.metaIdPhysical} is null; single-`@meta.id` tables
|
|
205
|
-
* keep an `undefined` fallback, so their scalar `insertedId`
|
|
206
|
-
* (`row[metaIdPhysical]`) is byte-identical to before.
|
|
207
|
-
*/
|
|
208
|
-
private _compositeInsertedId;
|
|
209
192
|
/**
|
|
210
193
|
* Switches this adapter into PROVIDER-BACKED mode: `fn` is invoked on every
|
|
211
194
|
* read to recompute the table's rows (e.g. a Redis/job-manager snapshot), so a
|
|
@@ -443,7 +426,9 @@ declare class MemoryAdapter extends BaseDbAdapter {
|
|
|
443
426
|
* pure {@link aggregateRows} engine, so provider (read-through) mode works
|
|
444
427
|
* unchanged. SQL-adapter semantics: null and missing group together,
|
|
445
428
|
* `sum` / `avg` over no numeric value are `null`, `$count` counts the groups
|
|
446
|
-
* that survive `$having`.
|
|
429
|
+
* that survive `$having`. Conditional aggregates, `countDistinct` field
|
|
430
|
+
* lists and two-level aggregates (`$from`, over the same snapshot) run in
|
|
431
|
+
* the engine too (since 0.1.168).
|
|
447
432
|
*
|
|
448
433
|
* `$search` never reaches here: this adapter is not searchable, so the core
|
|
449
434
|
* rejects a grouped search before dispatch.
|
|
@@ -459,6 +444,12 @@ declare class MemoryAdapter extends BaseDbAdapter {
|
|
|
459
444
|
aggregateFns(): ReadonlySet<AggregateFn>;
|
|
460
445
|
/** Arithmetic in an aggregate `$select` (`{ $expr }`, `{ $fn, $expr }`). */
|
|
461
446
|
supportsAggregateExpressions(): boolean;
|
|
447
|
+
/** Conditional aggregates (`$where`) — the memory filter predicate over each row. @since 0.1.168 */
|
|
448
|
+
supportsConditionalAggregates(): boolean;
|
|
449
|
+
/** `countDistinct` over a field list — distinct combinations by type-tagged identity. @since 0.1.168 */
|
|
450
|
+
supportsDistinctFieldLists(): boolean;
|
|
451
|
+
/** Two-level aggregates (`$from`) — the grouping engine over its own output rows. @since 0.1.168 */
|
|
452
|
+
supportsNestedAggregates(): boolean;
|
|
462
453
|
/** NULL placement (`$nulls`, `@db.sort.nulls`) — `sortRows` honours it per key. */
|
|
463
454
|
supportsNullsPlacement(): boolean;
|
|
464
455
|
/**
|
|
@@ -479,6 +470,10 @@ declare class MemoryAdapter extends BaseDbAdapter {
|
|
|
479
470
|
* Records the model's `unique` indexes for insert-time enforcement. Idempotent
|
|
480
471
|
* (replaces the recorded set). Non-unique index types are ignored — an
|
|
481
472
|
* in-memory scan needs no plain/fulltext/geo index to answer queries.
|
|
473
|
+
*
|
|
474
|
+
* A unique index the stored rows already break is not recorded and the call
|
|
475
|
+
* throws `CONFLICT` naming it — as `CREATE UNIQUE INDEX` fails over
|
|
476
|
+
* duplicates on SQL — after recording the indexes that hold.
|
|
482
477
|
*/
|
|
483
478
|
syncIndexes(): Promise<void>;
|
|
484
479
|
/** Creates the table's (empty) state when it does not exist. Idempotent. */
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ALL_AGGREGATE_FNS, ALL_BUCKET_UNITS, ALL_ISOLATION_LEVELS, ALL_VIEW_CAPABILITIES, BaseDbAdapter, DbError, DbSpace, INTEGER_REGEX_OP, constraintViolation, containsRelationFilter, deletePath, evaluateExpr, forEachResolvedRelation, getPath, isAtscriptDbView, isConflict, isPlainObject, isResolvedRelationFilter, stableJson, walkFilter } from "@atscript/db";
|
|
1
|
+
import { ALL_AGGREGATE_FNS, ALL_BUCKET_UNITS, ALL_ISOLATION_LEVELS, ALL_VIEW_CAPABILITIES, BaseDbAdapter, DbError, DbSpace, INTEGER_REGEX_OP, aggregateColumns, constraintViolation, containsRelationFilter, deletePath, evaluateExpr, forEachResolvedRelation, getPath, isAtscriptDbView, isConflict, isPlainObject, isResolvedRelationFilter, stableJson, walkFilter } from "@atscript/db";
|
|
2
2
|
import { assertAggregateFn, resolveAlias } from "@atscript/db/agg";
|
|
3
3
|
import { bucketer } from "@uniqu/core";
|
|
4
4
|
//#region src/lib/memory-clone.ts
|
|
@@ -102,7 +102,7 @@ function valuesEqual(a, b) {
|
|
|
102
102
|
return a === b;
|
|
103
103
|
}
|
|
104
104
|
/**
|
|
105
|
-
* Mirror of `mongo-
|
|
105
|
+
* Mirror of `mongo-view-expr.ts`'s `parseRegexString`: normalize a `$regex` value
|
|
106
106
|
* (or a bare `RegExp`) into a `{ pattern, flags }` pair. Accepts a `RegExp`
|
|
107
107
|
* instance, a `/pattern/flags` string literal, or a plain string (treated as
|
|
108
108
|
* the literal pattern with no flags).
|
|
@@ -193,10 +193,12 @@ function toOrdinal(v) {
|
|
|
193
193
|
}
|
|
194
194
|
/**
|
|
195
195
|
* `$gt`/`$gte`/`$lt`/`$lte`. A missing or `null` field never matches an
|
|
196
|
-
* ordering comparison
|
|
197
|
-
*
|
|
198
|
-
*
|
|
199
|
-
*
|
|
196
|
+
* ordering comparison, and neither does a `null` operand — the visitor
|
|
197
|
+
* compiles that to "no match" before reaching here (SQL's UNKNOWN, MongoDB's
|
|
198
|
+
* type bracketing — JS would read it as `0`). `Date` operands are
|
|
199
|
+
* normalized to epoch millis; all other comparisons use plain JS ordering
|
|
200
|
+
* (numbers numerically, strings lexicographically) — NO collation or locale
|
|
201
|
+
* awareness. This intentionally differs from SQL engines' collated ordering.
|
|
200
202
|
*/
|
|
201
203
|
function evalRelational(fieldValue, op, value) {
|
|
202
204
|
if (fieldValue === void 0 || fieldValue === null) return false;
|
|
@@ -245,7 +247,9 @@ const memoryVisitor = {
|
|
|
245
247
|
case "$gt":
|
|
246
248
|
case "$gte":
|
|
247
249
|
case "$lt":
|
|
248
|
-
case "$lte":
|
|
250
|
+
case "$lte":
|
|
251
|
+
if (value == null) return () => false;
|
|
252
|
+
return (row) => evalRelational(read(row), op, value);
|
|
249
253
|
case "$in": {
|
|
250
254
|
const isIn = compileIn(value);
|
|
251
255
|
return (row) => isIn(read(row));
|
|
@@ -745,7 +749,16 @@ function tupleTaken(index, tuple, excludeKey) {
|
|
|
745
749
|
* `@uniqu/core` kernel label (the SQLite UDF runs the same kernel);
|
|
746
750
|
* - `sum` / `avg` over no numeric value are `null`; `min` / `max` order with
|
|
747
751
|
* the `$sort` comparator; `countDistinct` counts distinct non-null values
|
|
748
|
-
* by group identity (below)
|
|
752
|
+
* by group identity (below) — over a field list (since 0.1.168), distinct
|
|
753
|
+
* combinations, a row with any null / missing field not counted; any other
|
|
754
|
+
* `$fn` is `INVALID_QUERY`;
|
|
755
|
+
* - a conditional aggregate (`$where`, since 0.1.168) feeds its accumulator
|
|
756
|
+
* only the rows its condition matches — the adapter's own filter predicate
|
|
757
|
+
* ({@link buildMemoryPredicate}), so it reads like the query filter;
|
|
758
|
+
* - a two-level aggregate (`controls.$from`, since 0.1.168) groups the rows
|
|
759
|
+
* of its inner level (this function over `$from`) by the outer controls —
|
|
760
|
+
* every inner column (an inner `$groupBy` key at its path, an inner alias
|
|
761
|
+
* at the top) is read like a field of a table row;
|
|
749
762
|
* - group identity is type-tagged: `1` and `"1"` never share a group (a
|
|
750
763
|
* number and a bigint of one value do), `Date`s group by instant, JSON
|
|
751
764
|
* values by a key-order-independent serialization;
|
|
@@ -754,16 +767,17 @@ function tupleTaken(index, tuple, excludeKey) {
|
|
|
754
767
|
* object-valued leaves copied so no store-owned value escapes.
|
|
755
768
|
*/
|
|
756
769
|
function aggregateRows(rows, controls) {
|
|
770
|
+
if (controls.$from) rows = aggregateRows(rows, controls.$from);
|
|
757
771
|
const $select = controls.$select;
|
|
758
772
|
const groupBy = controls.$groupBy ?? [];
|
|
759
773
|
const entries = [
|
|
760
774
|
...($select?.aggregates ?? []).map((expr) => ({
|
|
761
775
|
alias: resolveAlias(expr),
|
|
762
|
-
create: accumulatorFactory(expr)
|
|
776
|
+
create: conditional(accumulatorFactory(expr), expr.$where)
|
|
763
777
|
})),
|
|
764
778
|
...($select?.exprAggregates ?? []).map((e) => ({
|
|
765
779
|
alias: e.alias,
|
|
766
|
-
create: exprAccumulatorFactory(e)
|
|
780
|
+
create: conditional(exprAccumulatorFactory(e), e.where)
|
|
767
781
|
})),
|
|
768
782
|
...($select?.firstLast ?? []).map((fl) => ({
|
|
769
783
|
alias: fl.alias,
|
|
@@ -871,6 +885,23 @@ function numericValue(value) {
|
|
|
871
885
|
return Number.isNaN(n) ? void 0 : n;
|
|
872
886
|
}
|
|
873
887
|
}
|
|
888
|
+
/**
|
|
889
|
+
* An accumulator fed only the rows `where` matches (a conditional aggregate,
|
|
890
|
+
* since 0.1.168) — `create` unchanged without a condition.
|
|
891
|
+
*/
|
|
892
|
+
function conditional(create, where) {
|
|
893
|
+
if (where === void 0) return create;
|
|
894
|
+
const matches = buildMemoryPredicate(where);
|
|
895
|
+
return () => {
|
|
896
|
+
const acc = create();
|
|
897
|
+
return {
|
|
898
|
+
add: (row) => {
|
|
899
|
+
if (matches(row)) acc.add(row);
|
|
900
|
+
},
|
|
901
|
+
result: () => acc.result()
|
|
902
|
+
};
|
|
903
|
+
};
|
|
904
|
+
}
|
|
874
905
|
/** `sum` / `avg` / `min` / `max` over a per-row arithmetic expression; NULL results are skipped like NULL fields. */
|
|
875
906
|
function exprAccumulatorFactory(e) {
|
|
876
907
|
const readers = new Map(e.names.map((name) => [name, pathReader(name)]));
|
|
@@ -941,12 +972,39 @@ function fnAccumulator(fn, read) {
|
|
|
941
972
|
};
|
|
942
973
|
}
|
|
943
974
|
/**
|
|
975
|
+
* `countDistinct` over a field list: distinct combinations by their
|
|
976
|
+
* length-prefixed {@link identityToken}s (type-tagged — `1` and `"1"`
|
|
977
|
+
* differ; no value forges a separator); a row with any null / missing field
|
|
978
|
+
* is not counted (SQL's `COUNT(DISTINCT a, b)`).
|
|
979
|
+
*/
|
|
980
|
+
function distinctListFactory(fields) {
|
|
981
|
+
const readers = fields.map((field) => pathReader(field));
|
|
982
|
+
return () => {
|
|
983
|
+
const seen = /* @__PURE__ */ new Set();
|
|
984
|
+
return {
|
|
985
|
+
add: (row) => {
|
|
986
|
+
let identity = "";
|
|
987
|
+
for (const read of readers) {
|
|
988
|
+
const v = read(row);
|
|
989
|
+
if (v == null) return;
|
|
990
|
+
const token = identityToken(v);
|
|
991
|
+
identity += `${token.length}:${token}`;
|
|
992
|
+
}
|
|
993
|
+
seen.add(identity);
|
|
994
|
+
},
|
|
995
|
+
result: () => seen.size
|
|
996
|
+
};
|
|
997
|
+
};
|
|
998
|
+
}
|
|
999
|
+
/**
|
|
944
1000
|
* Resolves a plain aggregate (`first` / `last` are {@link firstLastFactory}'s)
|
|
945
|
-
* to an accumulator factory (one accumulator per group
|
|
946
|
-
* so an unknown `$fn` is
|
|
1001
|
+
* to an accumulator factory (one accumulator per group; a `$where` is applied
|
|
1002
|
+
* by {@link conditional}). Validated up front, so an unknown `$fn` is
|
|
1003
|
+
* rejected even when there are no rows.
|
|
947
1004
|
*/
|
|
948
1005
|
function accumulatorFactory(expr) {
|
|
949
1006
|
assertAggregateFn(expr.$fn);
|
|
1007
|
+
if (typeof expr.$field !== "string") return distinctListFactory(aggregateColumns(expr));
|
|
950
1008
|
const field = expr.$field;
|
|
951
1009
|
const read = pathReader(field);
|
|
952
1010
|
if (expr.$fn === "count") {
|
|
@@ -1244,27 +1302,6 @@ var MemoryAdapter = class MemoryAdapter extends BaseDbAdapter {
|
|
|
1244
1302
|
return JSON.stringify(fields.map((field) => pinned.get(field)));
|
|
1245
1303
|
}
|
|
1246
1304
|
/**
|
|
1247
|
-
* Builds a DEFINED inserted-id for a table with NO single `@meta.id` — a
|
|
1248
|
-
* composite (or single non-meta) primary key, where
|
|
1249
|
-
* {@link BaseDbAdapter._resolveInsertedId} would otherwise fall back to
|
|
1250
|
-
* `undefined` (memory has no rowid/`_id` to hand back). Returns an object
|
|
1251
|
-
* mapping each PRIMARY KEY PHYSICAL field name → its value in the stored row
|
|
1252
|
-
* (e.g. `{ part1: "a", part2: "b" }`), read by physical name via
|
|
1253
|
-
* {@link getPath} so it matches how {@link pkKey} derives the storage key and
|
|
1254
|
-
* honours column renames. A single-field non-meta PK yields the one-key object
|
|
1255
|
-
* form for consistency (documented shape).
|
|
1256
|
-
*
|
|
1257
|
-
* Passed as the `dbGeneratedId` fallback in {@link _insertRow} ONLY when
|
|
1258
|
-
* {@link AtscriptDbReadable.metaIdPhysical} is null; single-`@meta.id` tables
|
|
1259
|
-
* keep an `undefined` fallback, so their scalar `insertedId`
|
|
1260
|
-
* (`row[metaIdPhysical]`) is byte-identical to before.
|
|
1261
|
-
*/
|
|
1262
|
-
_compositeInsertedId(row) {
|
|
1263
|
-
const id = {};
|
|
1264
|
-
for (const field of this._physicalPkFields()) id[field] = getPath(row, field);
|
|
1265
|
-
return id;
|
|
1266
|
-
}
|
|
1267
|
-
/**
|
|
1268
1305
|
* Switches this adapter into PROVIDER-BACKED mode: `fn` is invoked on every
|
|
1269
1306
|
* read to recompute the table's rows (e.g. a Redis/job-manager snapshot), so a
|
|
1270
1307
|
* runtime-owned entity with NO database can be observed as a read-only
|
|
@@ -1365,8 +1402,7 @@ var MemoryAdapter = class MemoryAdapter extends BaseDbAdapter {
|
|
|
1365
1402
|
if (state.rows.has(key)) throw this._pkConflict();
|
|
1366
1403
|
const tuples = this._enforceUniqueIndexes(state, row);
|
|
1367
1404
|
this._storeRow(state, key, row, tuples);
|
|
1368
|
-
|
|
1369
|
-
return this._resolveInsertedId(row, fallback);
|
|
1405
|
+
return this._resolveInsertedId(row, void 0);
|
|
1370
1406
|
}
|
|
1371
1407
|
/**
|
|
1372
1408
|
* Enforces every recorded unique index against the current store. A row is
|
|
@@ -1727,7 +1763,9 @@ var MemoryAdapter = class MemoryAdapter extends BaseDbAdapter {
|
|
|
1727
1763
|
* pure {@link aggregateRows} engine, so provider (read-through) mode works
|
|
1728
1764
|
* unchanged. SQL-adapter semantics: null and missing group together,
|
|
1729
1765
|
* `sum` / `avg` over no numeric value are `null`, `$count` counts the groups
|
|
1730
|
-
* that survive `$having`.
|
|
1766
|
+
* that survive `$having`. Conditional aggregates, `countDistinct` field
|
|
1767
|
+
* lists and two-level aggregates (`$from`, over the same snapshot) run in
|
|
1768
|
+
* the engine too (since 0.1.168).
|
|
1731
1769
|
*
|
|
1732
1770
|
* `$search` never reaches here: this adapter is not searchable, so the core
|
|
1733
1771
|
* rejects a grouped search before dispatch.
|
|
@@ -1751,6 +1789,18 @@ var MemoryAdapter = class MemoryAdapter extends BaseDbAdapter {
|
|
|
1751
1789
|
supportsAggregateExpressions() {
|
|
1752
1790
|
return true;
|
|
1753
1791
|
}
|
|
1792
|
+
/** Conditional aggregates (`$where`) — the memory filter predicate over each row. @since 0.1.168 */
|
|
1793
|
+
supportsConditionalAggregates() {
|
|
1794
|
+
return true;
|
|
1795
|
+
}
|
|
1796
|
+
/** `countDistinct` over a field list — distinct combinations by type-tagged identity. @since 0.1.168 */
|
|
1797
|
+
supportsDistinctFieldLists() {
|
|
1798
|
+
return true;
|
|
1799
|
+
}
|
|
1800
|
+
/** Two-level aggregates (`$from`) — the grouping engine over its own output rows. @since 0.1.168 */
|
|
1801
|
+
supportsNestedAggregates() {
|
|
1802
|
+
return true;
|
|
1803
|
+
}
|
|
1754
1804
|
/** NULL placement (`$nulls`, `@db.sort.nulls`) — `sortRows` honours it per key. */
|
|
1755
1805
|
supportsNullsPlacement() {
|
|
1756
1806
|
return true;
|
|
@@ -1818,16 +1868,36 @@ var MemoryAdapter = class MemoryAdapter extends BaseDbAdapter {
|
|
|
1818
1868
|
* Records the model's `unique` indexes for insert-time enforcement. Idempotent
|
|
1819
1869
|
* (replaces the recorded set). Non-unique index types are ignored — an
|
|
1820
1870
|
* in-memory scan needs no plain/fulltext/geo index to answer queries.
|
|
1871
|
+
*
|
|
1872
|
+
* A unique index the stored rows already break is not recorded and the call
|
|
1873
|
+
* throws `CONFLICT` naming it — as `CREATE UNIQUE INDEX` fails over
|
|
1874
|
+
* duplicates on SQL — after recording the indexes that hold.
|
|
1821
1875
|
*/
|
|
1822
1876
|
async syncIndexes() {
|
|
1877
|
+
const state = this._state();
|
|
1823
1878
|
const uniqueIndexes = [];
|
|
1879
|
+
const broken = [];
|
|
1824
1880
|
for (const index of this._table.indexes.values()) {
|
|
1825
1881
|
if (index.type !== "unique") continue;
|
|
1826
|
-
|
|
1882
|
+
const recorded = recordUniqueIndex(index.name, index.fields.map((f) => f.name), new Set(index.fields.filter((f) => f.optional).map((f) => f.name)));
|
|
1883
|
+
let holds = true;
|
|
1884
|
+
for (const [key, row] of state.rows) {
|
|
1885
|
+
const tuple = uniqueTupleKey(recorded, row);
|
|
1886
|
+
if (tuple === void 0) continue;
|
|
1887
|
+
if (recorded.entries.has(tuple)) {
|
|
1888
|
+
holds = false;
|
|
1889
|
+
break;
|
|
1890
|
+
}
|
|
1891
|
+
recorded.entries.set(tuple, new Set([key]));
|
|
1892
|
+
}
|
|
1893
|
+
(holds ? uniqueIndexes : broken).push(recorded);
|
|
1827
1894
|
}
|
|
1828
|
-
const state = this._state();
|
|
1829
1895
|
state.uniqueIndexes = uniqueIndexes;
|
|
1830
|
-
|
|
1896
|
+
const [first] = broken;
|
|
1897
|
+
if (first) throw constraintViolation("CONFLICT", {
|
|
1898
|
+
name: first.name,
|
|
1899
|
+
columns: first.fields
|
|
1900
|
+
}, `Cannot create unique index ${broken.map((i) => `"${i.name}"`).join(", ")}: stored rows hold duplicate values`);
|
|
1831
1901
|
}
|
|
1832
1902
|
/** Creates the table's (empty) state when it does not exist. Idempotent. */
|
|
1833
1903
|
async ensureTable() {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atscript/db-memory",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.169",
|
|
4
4
|
"description": "In-memory adapter for atscript-db.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"atscript",
|
|
@@ -36,16 +36,16 @@
|
|
|
36
36
|
"access": "public"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@atscript/core": "^0.2.
|
|
40
|
-
"@atscript/typescript": "^0.2.
|
|
41
|
-
"@uniqu/core": "^0.
|
|
42
|
-
"unplugin-atscript": "^0.2.
|
|
39
|
+
"@atscript/core": "^0.2.4",
|
|
40
|
+
"@atscript/typescript": "^0.2.4",
|
|
41
|
+
"@uniqu/core": "^0.2.0",
|
|
42
|
+
"unplugin-atscript": "^0.2.4"
|
|
43
43
|
},
|
|
44
44
|
"peerDependencies": {
|
|
45
|
-
"@atscript/core": "^0.2.
|
|
46
|
-
"@atscript/typescript": "^0.2.
|
|
47
|
-
"@uniqu/core": "^0.
|
|
48
|
-
"@atscript/db": "^0.1.
|
|
45
|
+
"@atscript/core": "^0.2.4",
|
|
46
|
+
"@atscript/typescript": "^0.2.4",
|
|
47
|
+
"@uniqu/core": "^0.2.0",
|
|
48
|
+
"@atscript/db": "^0.1.169"
|
|
49
49
|
},
|
|
50
50
|
"scripts": {
|
|
51
51
|
"build": "vp pack",
|