@based/db 0.0.42 → 0.0.43

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.
Binary file
Binary file
@@ -3,3 +3,4 @@ import { AggregateType } from './types.js';
3
3
  export declare const aggregateToBuffer: (aggregates: QueryDefAggregation) => Uint8Array;
4
4
  export declare const groupBy: (def: QueryDef, field: string) => void;
5
5
  export declare const addAggregate: (type: AggregateType, def: QueryDef, fields: (string | string[])[]) => void;
6
+ export declare const isRootCountOnly: (def: QueryDef, filterSize: number) => boolean;
@@ -1,4 +1,5 @@
1
1
  import { writeUint16 } from '@saulx/utils';
2
+ import { QueryDefType } from '../types.js';
2
3
  import { UINT32 } from '@based/schema/def';
3
4
  import { aggregationFieldDoesNotExist } from '../validation.js';
4
5
  export const aggregateToBuffer = (aggregates) => {
@@ -107,4 +108,29 @@ export const addAggregate = (type, def, fields) => {
107
108
  }
108
109
  }
109
110
  };
111
+ export const isRootCountOnly = (def, filterSize) => {
112
+ if (filterSize != 0) {
113
+ return false;
114
+ }
115
+ if (def.type !== QueryDefType.Root) {
116
+ return false;
117
+ }
118
+ if (def.aggregate.groupBy) {
119
+ return false;
120
+ }
121
+ if (def.aggregate.aggregates.size !== 1) {
122
+ return false;
123
+ }
124
+ if (!def.aggregate.aggregates.has(255)) {
125
+ return false;
126
+ }
127
+ const aggs = def.aggregate.aggregates.get(255);
128
+ if (aggs.length !== 1) {
129
+ return false;
130
+ }
131
+ if (aggs[0].type !== 2 /* AggregateType.COUNT */) {
132
+ return false;
133
+ }
134
+ return true;
135
+ };
110
136
  //# sourceMappingURL=aggregation.js.map
@@ -4,7 +4,7 @@ import { includeToBuffer } from './include/toBuffer.js';
4
4
  import { filterToBuffer } from './query.js';
5
5
  import { searchToBuffer } from './search/index.js';
6
6
  import { ENCODER } from '@saulx/utils';
7
- import { aggregateToBuffer } from './aggregates/aggregation.js';
7
+ import { aggregateToBuffer, isRootCountOnly } from './aggregates/aggregation.js';
8
8
  const byteSize = (arr) => {
9
9
  return arr.reduce((a, b) => {
10
10
  return a + b.byteLength;
@@ -33,9 +33,6 @@ export function defToBuffer(db, def) {
33
33
  edgesSize = byteSize(edges);
34
34
  }
35
35
  const size = (edges ? edgesSize + 3 : 0) + byteSize(include);
36
- // ---------------------------------------
37
- // move down and will handle size after store the size Var
38
- // only for references | edges
39
36
  if (def.aggregate) {
40
37
  const aggregateSize = def.aggregate.size || 0;
41
38
  if (aggregateSize === 0) {
@@ -43,7 +40,9 @@ export function defToBuffer(db, def) {
43
40
  }
44
41
  const filterSize = def.filter.size || 0;
45
42
  const buf = new Uint8Array(16 + filterSize + aggregateSize);
46
- buf[0] = QueryType.aggregates;
43
+ buf[0] = isRootCountOnly(def, filterSize)
44
+ ? QueryType.aggregatesCountType
45
+ : QueryType.aggregates;
47
46
  buf[1] = def.schema.idUint8[0];
48
47
  buf[2] = def.schema.idUint8[1];
49
48
  buf[3] = def.range.offset;
@@ -66,8 +65,6 @@ export function defToBuffer(db, def) {
66
65
  result.push(buf);
67
66
  // ignore this for now...
68
67
  // result.push(...include)
69
- // console.log('toBuffer > result: ', result)
70
- // later this has to be a branch
71
68
  return result;
72
69
  }
73
70
  if (def.type === QueryDefType.Root) {
@@ -12,7 +12,8 @@ export declare enum QueryType {
12
12
  ids = 1,
13
13
  default = 2,
14
14
  alias = 3,
15
- aggregates = 4
15
+ aggregates = 4,
16
+ aggregatesCountType = 5
16
17
  }
17
18
  declare enum QueryDefType {
18
19
  Edge = 1,
@@ -5,6 +5,7 @@ export var QueryType;
5
5
  QueryType[QueryType["default"] = 2] = "default";
6
6
  QueryType[QueryType["alias"] = 3] = "alias";
7
7
  QueryType[QueryType["aggregates"] = 4] = "aggregates";
8
+ QueryType[QueryType["aggregatesCountType"] = 5] = "aggregatesCountType";
8
9
  })(QueryType || (QueryType = {}));
9
10
  var QueryDefType;
10
11
  (function (QueryDefType) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@based/db",
3
- "version": "0.0.42",
3
+ "version": "0.0.43",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "./dist/src/index.js",