@atscript/db-memory 0.1.134 → 0.1.136

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 CHANGED
@@ -348,7 +348,8 @@ function projectRow(row, projection, opts) {
348
348
  * - null and missing form ONE group; a calendar-bucket key is the
349
349
  * `@uniqu/core` kernel label (the SQLite UDF runs the same kernel);
350
350
  * - `sum` / `avg` over no numeric value are `null`; `min` / `max` order with
351
- * the `$sort` comparator; any other `$fn` is `INVALID_QUERY`;
351
+ * the `$sort` comparator; `countDistinct` counts distinct non-null values
352
+ * by group identity (below); any other `$fn` is `INVALID_QUERY`;
352
353
  * - group identity is type-tagged: `1` and `"1"` never share a group (a
353
354
  * number and a bigint of one value do), `Date`s group by instant, JSON
354
355
  * values by a key-order-independent serialization;
@@ -470,6 +471,7 @@ function numericValue(value) {
470
471
  * there are no rows.
471
472
  */
472
473
  function accumulatorFactory(expr) {
474
+ (0, _atscript_db_agg.assertAggregateFn)(expr.$fn);
473
475
  const field = expr.$field;
474
476
  const read = pathReader(field);
475
477
  switch (expr.$fn) {
@@ -503,6 +505,19 @@ function accumulatorFactory(expr) {
503
505
  };
504
506
  };
505
507
  }
508
+ case "countDistinct": return () => {
509
+ const plain = /* @__PURE__ */ new Set();
510
+ const tokens = /* @__PURE__ */ new Set();
511
+ return {
512
+ add: (row) => {
513
+ const v = read(row);
514
+ if (v == null) return;
515
+ if (typeof v === "string" || typeof v === "boolean") plain.add(v);
516
+ else tokens.add(identityToken(v));
517
+ },
518
+ result: () => plain.size + tokens.size
519
+ };
520
+ };
506
521
  case "min":
507
522
  case "max": {
508
523
  const sign = expr.$fn === "min" ? -1 : 1;
@@ -518,10 +533,7 @@ function accumulatorFactory(expr) {
518
533
  };
519
534
  };
520
535
  }
521
- default: throw new _atscript_db.DbError("INVALID_QUERY", [{
522
- path: "$select",
523
- message: `Unsupported aggregate function "${String(expr.$fn)}" — use count, sum, avg, min or max`
524
- }]);
536
+ default: return expr.$fn;
525
537
  }
526
538
  }
527
539
  //#endregion
@@ -1095,6 +1107,10 @@ var MemoryAdapter = class extends _atscript_db.BaseDbAdapter {
1095
1107
  calendarBucketUnits() {
1096
1108
  return _atscript_db.ALL_BUCKET_UNITS;
1097
1109
  }
1110
+ /** Every aggregate function, `countDistinct` included. */
1111
+ aggregateFns() {
1112
+ return _atscript_db.ALL_AGGREGATE_FNS;
1113
+ }
1098
1114
  async updateMany(filter, data, ops) {
1099
1115
  this._assertWritable();
1100
1116
  const matched = this._selectForWrite(filter, void 0, true);
package/dist/index.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { BaseDbAdapter, BucketUnit, DbQuery, DbSpace, FilterExpr, TDbDeleteResult, TDbFieldMeta, TDbInsertManyResult, TDbInsertResult, TDbUpdateResult, TFieldOps } from "@atscript/db";
1
+ import { AggregateFn, BaseDbAdapter, BucketUnit, DbQuery, DbSpace, FilterExpr, TDbDeleteResult, TDbFieldMeta, TDbInsertManyResult, TDbInsertResult, TDbUpdateResult, TFieldOps } from "@atscript/db";
2
2
  import { TAtscriptAnnotatedType } from "@atscript/typescript/utils";
3
3
 
4
4
  //#region src/lib/memory-adapter.d.ts
@@ -367,6 +367,8 @@ declare class MemoryAdapter extends BaseDbAdapter {
367
367
  * there is no zone this adapter cannot resolve).
368
368
  */
369
369
  calendarBucketUnits(): ReadonlySet<BucketUnit>;
370
+ /** Every aggregate function, `countDistinct` included. */
371
+ aggregateFns(): ReadonlySet<AggregateFn>;
370
372
  updateMany(filter: FilterExpr, data: Record<string, unknown>, ops?: TFieldOps): Promise<TDbUpdateResult>;
371
373
  replaceMany(filter: FilterExpr, data: Record<string, unknown>): Promise<TDbUpdateResult>;
372
374
  deleteMany(filter: FilterExpr): Promise<TDbDeleteResult>;
package/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { BaseDbAdapter, BucketUnit, DbQuery, DbSpace, FilterExpr, TDbDeleteResult, TDbFieldMeta, TDbInsertManyResult, TDbInsertResult, TDbUpdateResult, TFieldOps } from "@atscript/db";
1
+ import { AggregateFn, BaseDbAdapter, BucketUnit, DbQuery, DbSpace, FilterExpr, TDbDeleteResult, TDbFieldMeta, TDbInsertManyResult, TDbInsertResult, TDbUpdateResult, TFieldOps } from "@atscript/db";
2
2
  import { TAtscriptAnnotatedType } from "@atscript/typescript/utils";
3
3
 
4
4
  //#region src/lib/memory-adapter.d.ts
@@ -367,6 +367,8 @@ declare class MemoryAdapter extends BaseDbAdapter {
367
367
  * there is no zone this adapter cannot resolve).
368
368
  */
369
369
  calendarBucketUnits(): ReadonlySet<BucketUnit>;
370
+ /** Every aggregate function, `countDistinct` included. */
371
+ aggregateFns(): ReadonlySet<AggregateFn>;
370
372
  updateMany(filter: FilterExpr, data: Record<string, unknown>, ops?: TFieldOps): Promise<TDbUpdateResult>;
371
373
  replaceMany(filter: FilterExpr, data: Record<string, unknown>): Promise<TDbUpdateResult>;
372
374
  deleteMany(filter: FilterExpr): Promise<TDbDeleteResult>;
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
- import { ALL_BUCKET_UNITS, BaseDbAdapter, DbError, DbSpace, walkFilter } from "@atscript/db";
2
- import { resolveAlias } from "@atscript/db/agg";
1
+ import { ALL_AGGREGATE_FNS, ALL_BUCKET_UNITS, BaseDbAdapter, DbError, DbSpace, walkFilter } from "@atscript/db";
2
+ import { assertAggregateFn, resolveAlias } from "@atscript/db/agg";
3
3
  import { bucketer } from "@uniqu/core";
4
4
  //#region src/lib/memory-filter.ts
5
5
  /**
@@ -347,7 +347,8 @@ function projectRow(row, projection, opts) {
347
347
  * - null and missing form ONE group; a calendar-bucket key is the
348
348
  * `@uniqu/core` kernel label (the SQLite UDF runs the same kernel);
349
349
  * - `sum` / `avg` over no numeric value are `null`; `min` / `max` order with
350
- * the `$sort` comparator; any other `$fn` is `INVALID_QUERY`;
350
+ * the `$sort` comparator; `countDistinct` counts distinct non-null values
351
+ * by group identity (below); any other `$fn` is `INVALID_QUERY`;
351
352
  * - group identity is type-tagged: `1` and `"1"` never share a group (a
352
353
  * number and a bigint of one value do), `Date`s group by instant, JSON
353
354
  * values by a key-order-independent serialization;
@@ -469,6 +470,7 @@ function numericValue(value) {
469
470
  * there are no rows.
470
471
  */
471
472
  function accumulatorFactory(expr) {
473
+ assertAggregateFn(expr.$fn);
472
474
  const field = expr.$field;
473
475
  const read = pathReader(field);
474
476
  switch (expr.$fn) {
@@ -502,6 +504,19 @@ function accumulatorFactory(expr) {
502
504
  };
503
505
  };
504
506
  }
507
+ case "countDistinct": return () => {
508
+ const plain = /* @__PURE__ */ new Set();
509
+ const tokens = /* @__PURE__ */ new Set();
510
+ return {
511
+ add: (row) => {
512
+ const v = read(row);
513
+ if (v == null) return;
514
+ if (typeof v === "string" || typeof v === "boolean") plain.add(v);
515
+ else tokens.add(identityToken(v));
516
+ },
517
+ result: () => plain.size + tokens.size
518
+ };
519
+ };
505
520
  case "min":
506
521
  case "max": {
507
522
  const sign = expr.$fn === "min" ? -1 : 1;
@@ -517,10 +532,7 @@ function accumulatorFactory(expr) {
517
532
  };
518
533
  };
519
534
  }
520
- default: throw new DbError("INVALID_QUERY", [{
521
- path: "$select",
522
- message: `Unsupported aggregate function "${String(expr.$fn)}" — use count, sum, avg, min or max`
523
- }]);
535
+ default: return expr.$fn;
524
536
  }
525
537
  }
526
538
  //#endregion
@@ -1094,6 +1106,10 @@ var MemoryAdapter = class extends BaseDbAdapter {
1094
1106
  calendarBucketUnits() {
1095
1107
  return ALL_BUCKET_UNITS;
1096
1108
  }
1109
+ /** Every aggregate function, `countDistinct` included. */
1110
+ aggregateFns() {
1111
+ return ALL_AGGREGATE_FNS;
1112
+ }
1097
1113
  async updateMany(filter, data, ops) {
1098
1114
  this._assertWritable();
1099
1115
  const matched = this._selectForWrite(filter, void 0, true);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atscript/db-memory",
3
- "version": "0.1.134",
3
+ "version": "0.1.136",
4
4
  "description": "In-memory adapter for atscript-db.",
5
5
  "keywords": [
6
6
  "atscript",
@@ -38,14 +38,14 @@
38
38
  "devDependencies": {
39
39
  "@atscript/core": "^0.1.92",
40
40
  "@atscript/typescript": "^0.1.92",
41
- "@uniqu/core": "^0.1.10",
41
+ "@uniqu/core": "^0.1.11",
42
42
  "unplugin-atscript": "^0.1.92"
43
43
  },
44
44
  "peerDependencies": {
45
45
  "@atscript/core": "^0.1.92",
46
46
  "@atscript/typescript": "^0.1.92",
47
- "@uniqu/core": "^0.1.10",
48
- "@atscript/db": "^0.1.134"
47
+ "@uniqu/core": "^0.1.11",
48
+ "@atscript/db": "^0.1.136"
49
49
  },
50
50
  "scripts": {
51
51
  "postinstall": "asc -f dts",