@based/db 0.0.25 → 0.0.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. package/dist/lib/darwin_aarch64/include/cdefs.h +1 -1
  2. package/dist/lib/darwin_aarch64/include/selva/db.h +45 -11
  3. package/dist/lib/darwin_aarch64/include/selva/fields.h +48 -13
  4. package/dist/lib/darwin_aarch64/include/selva/hll.h +59 -0
  5. package/dist/lib/darwin_aarch64/include/selva/types.h +2 -0
  6. package/dist/lib/darwin_aarch64/include/tree.h +69 -69
  7. package/dist/lib/darwin_aarch64/libnode-v20.node +0 -0
  8. package/dist/lib/darwin_aarch64/libnode-v21.node +0 -0
  9. package/dist/lib/darwin_aarch64/libnode-v22.node +0 -0
  10. package/dist/lib/darwin_aarch64/libnode-v23.node +0 -0
  11. package/dist/lib/darwin_aarch64/libselva.dylib +0 -0
  12. package/dist/lib/linux_aarch64/include/cdefs.h +1 -1
  13. package/dist/lib/linux_aarch64/include/selva/db.h +45 -11
  14. package/dist/lib/linux_aarch64/include/selva/fields.h +48 -13
  15. package/dist/lib/linux_aarch64/include/selva/hll.h +59 -0
  16. package/dist/lib/linux_aarch64/include/selva/types.h +2 -0
  17. package/dist/lib/linux_aarch64/include/tree.h +69 -69
  18. package/dist/lib/linux_aarch64/libdeflate.so +0 -0
  19. package/dist/lib/linux_aarch64/libnode-v20.node +0 -0
  20. package/dist/lib/linux_aarch64/libnode-v21.node +0 -0
  21. package/dist/lib/linux_aarch64/libnode-v22.node +0 -0
  22. package/dist/lib/linux_aarch64/libnode-v23.node +0 -0
  23. package/dist/lib/linux_aarch64/libselva.so +0 -0
  24. package/dist/lib/linux_x86_64/include/cdefs.h +1 -1
  25. package/dist/lib/linux_x86_64/include/selva/db.h +45 -11
  26. package/dist/lib/linux_x86_64/include/selva/fields.h +48 -13
  27. package/dist/lib/linux_x86_64/include/selva/hll.h +59 -0
  28. package/dist/lib/linux_x86_64/include/selva/types.h +2 -0
  29. package/dist/lib/linux_x86_64/include/tree.h +69 -69
  30. package/dist/lib/linux_x86_64/libdeflate.so +0 -0
  31. package/dist/lib/linux_x86_64/libnode-v20.node +0 -0
  32. package/dist/lib/linux_x86_64/libnode-v21.node +0 -0
  33. package/dist/lib/linux_x86_64/libnode-v22.node +0 -0
  34. package/dist/lib/linux_x86_64/libnode-v23.node +0 -0
  35. package/dist/lib/linux_x86_64/libselva.so +0 -0
  36. package/dist/src/client/flushModify.js +5 -1
  37. package/dist/src/client/index.d.ts +12 -6
  38. package/dist/src/client/index.js +33 -1
  39. package/dist/src/client/modify/alias.js +3 -0
  40. package/dist/src/client/modify/binary.js +1 -1
  41. package/dist/src/client/modify/cardinality.d.ts +2 -2
  42. package/dist/src/client/modify/cardinality.js +17 -6
  43. package/dist/src/client/modify/create.js +17 -1
  44. package/dist/src/client/modify/fixed.js +27 -23
  45. package/dist/src/client/modify/json.js +15 -1
  46. package/dist/src/client/modify/modify.js +3 -1
  47. package/dist/src/client/modify/references/edge.js +4 -2
  48. package/dist/src/client/modify/references/references.js +21 -6
  49. package/dist/src/client/modify/string.js +5 -6
  50. package/dist/src/client/modify/text.js +0 -11
  51. package/dist/src/client/modify/upsert.js +1 -1
  52. package/dist/src/client/modify/vector.js +3 -3
  53. package/dist/src/client/query/BasedDbQuery.js +9 -1
  54. package/dist/src/client/query/BasedIterable.js +10 -3
  55. package/dist/src/client/query/aggregation.d.ts +3 -0
  56. package/dist/src/client/query/aggregation.js +9 -0
  57. package/dist/src/client/query/display.js +12 -2
  58. package/dist/src/client/query/filter/parseFilterValue.js +2 -4
  59. package/dist/src/client/query/filter/toBuffer.js +2 -2
  60. package/dist/src/client/query/include/walk.js +1 -0
  61. package/dist/src/client/query/query.d.ts +1 -1
  62. package/dist/src/client/query/query.js +1 -1
  63. package/dist/src/client/query/queryDef.js +0 -1
  64. package/dist/src/client/query/read/read.js +17 -5
  65. package/dist/src/client/query/toBuffer.js +2 -2
  66. package/dist/src/client/query/types.d.ts +4 -3
  67. package/dist/src/client/query/validation.js +8 -1
  68. package/dist/src/client/string.js +1 -1
  69. package/dist/src/index.d.ts +7 -1
  70. package/dist/src/index.js +19 -4
  71. package/dist/src/native.d.ts +1 -1
  72. package/dist/src/native.js +2 -2
  73. package/dist/src/server/csmt/tree.js +2 -2
  74. package/dist/src/server/index.d.ts +1 -1
  75. package/dist/src/server/index.js +36 -4
  76. package/dist/src/server/migrate/index.js +9 -5
  77. package/dist/src/server/migrate/worker.js +26 -1
  78. package/dist/src/server/save.js +20 -12
  79. package/dist/src/server/start.js +0 -2
  80. package/dist/src/utils.d.ts +6 -0
  81. package/dist/src/utils.js +81 -9
  82. package/package.json +4 -3
  83. package/dist/src/client/bitWise.d.ts +0 -6
  84. package/dist/src/client/bitWise.js +0 -72
  85. package/dist/src/client/query/aggregationFn.d.ts +0 -3
  86. package/dist/src/client/query/aggregationFn.js +0 -9
  87. package/dist/src/client/tree.d.ts +0 -1
  88. package/dist/src/client/tree.js +0 -5
@@ -30,7 +30,8 @@ const appendCreate = (ctx, def, obj, res, unsafe) => {
30
30
  ctx.buf[ctx.len++] = mainLenU32 >>>= 8;
31
31
  ctx.buf[ctx.len++] = mainLenU32 >>>= 8;
32
32
  ctx.lastMain = ctx.len;
33
- ctx.buf.fill(0, ctx.len, (ctx.len += def.mainLen));
33
+ ctx.buf.set(def.mainEmpty, ctx.len);
34
+ ctx.len += def.mainLen;
34
35
  }
35
36
  err = writeFixedValue(ctx, createTs, prop, ctx.lastMain + prop.start);
36
37
  if (err) {
@@ -38,6 +39,21 @@ const appendCreate = (ctx, def, obj, res, unsafe) => {
38
39
  }
39
40
  }
40
41
  }
42
+ else if (ctx.lastMain === -1 && !def.mainEmptyAllZeroes) {
43
+ // this is there to handle different defaults
44
+ if (ctx.lastMain === -1) {
45
+ let mainLenU32 = def.mainLen;
46
+ setCursor(ctx, def, 0, MICRO_BUFFER, res.tmpId, CREATE);
47
+ ctx.buf[ctx.len++] = CREATE;
48
+ ctx.buf[ctx.len++] = mainLenU32;
49
+ ctx.buf[ctx.len++] = mainLenU32 >>>= 8;
50
+ ctx.buf[ctx.len++] = mainLenU32 >>>= 8;
51
+ ctx.buf[ctx.len++] = mainLenU32 >>>= 8;
52
+ ctx.lastMain = ctx.len;
53
+ ctx.buf.set(def.mainEmpty, ctx.len);
54
+ ctx.len += def.mainLen;
55
+ }
56
+ }
41
57
  if (def.hasSeperateSort) {
42
58
  if (ctx.hasSortField !== def.seperateSort.size - 1) {
43
59
  if (ctx.len + 3 > ctx.max) {
@@ -19,12 +19,6 @@ map[BINARY] = (ctx, val, def) => {
19
19
  ctx.len += buf.byteLength;
20
20
  };
21
21
  map[STRING] = (ctx, val, def) => {
22
- if (typeof val !== 'string') {
23
- if (val !== null) {
24
- return new ModifyError(def, val);
25
- }
26
- val = '';
27
- }
28
22
  const valBuf = ENCODER.encode(val);
29
23
  const size = valBuf.byteLength;
30
24
  if (size + 1 > def.len) {
@@ -66,9 +60,6 @@ map[ENUM] = (ctx, val, def) => {
66
60
  }
67
61
  };
68
62
  map[NUMBER] = (ctx, val, def) => {
69
- if (typeof val !== 'number') {
70
- return new ModifyError(def, val);
71
- }
72
63
  if (ctx.len + 8 > ctx.max) {
73
64
  return RANGE_ERR;
74
65
  }
@@ -78,9 +69,6 @@ map[NUMBER] = (ctx, val, def) => {
78
69
  };
79
70
  map[TIMESTAMP] = (ctx, val, def) => {
80
71
  const parsedValue = convertToTimestamp(val);
81
- if (typeof parsedValue !== 'number') {
82
- return new ModifyError(def, val);
83
- }
84
72
  if (ctx.len + 8 > ctx.max) {
85
73
  return RANGE_ERR;
86
74
  }
@@ -89,9 +77,6 @@ map[TIMESTAMP] = (ctx, val, def) => {
89
77
  view.setFloat64(0, parsedValue, true);
90
78
  };
91
79
  map[UINT32] = (ctx, val, def) => {
92
- if (typeof val !== 'number') {
93
- return new ModifyError(def, val);
94
- }
95
80
  if (ctx.len + 4 > ctx.max) {
96
81
  return RANGE_ERR;
97
82
  }
@@ -101,9 +86,6 @@ map[UINT32] = (ctx, val, def) => {
101
86
  ctx.buf[ctx.len++] = val >>>= 8;
102
87
  };
103
88
  map[UINT16] = (ctx, val, def) => {
104
- if (typeof val !== 'number') {
105
- return new ModifyError(def, val);
106
- }
107
89
  if (ctx.len + 2 > ctx.max) {
108
90
  return RANGE_ERR;
109
91
  }
@@ -111,18 +93,37 @@ map[UINT16] = (ctx, val, def) => {
111
93
  ctx.buf[ctx.len++] = val >>>= 8;
112
94
  };
113
95
  map[UINT8] = (ctx, val, def) => {
114
- if (typeof val !== 'number') {
115
- return new ModifyError(def, val);
96
+ if (ctx.len + 1 > ctx.max) {
97
+ return RANGE_ERR;
98
+ }
99
+ ctx.buf[ctx.len++] = val;
100
+ };
101
+ map[INT32] = (ctx, val, def) => {
102
+ if (ctx.len + 4 > ctx.max) {
103
+ return RANGE_ERR;
104
+ }
105
+ ctx.buf[ctx.len++] = val;
106
+ ctx.buf[ctx.len++] = val >>>= 8;
107
+ ctx.buf[ctx.len++] = val >>>= 8;
108
+ ctx.buf[ctx.len++] = val >>>= 8;
109
+ };
110
+ map[INT16] = (ctx, val, def) => {
111
+ if (ctx.len + 2 > ctx.max) {
112
+ return RANGE_ERR;
116
113
  }
114
+ ctx.buf[ctx.len++] = val;
115
+ ctx.buf[ctx.len++] = val >>>= 8;
116
+ };
117
+ map[INT8] = (ctx, val, def) => {
117
118
  if (ctx.len + 1 > ctx.max) {
118
119
  return RANGE_ERR;
119
120
  }
120
121
  ctx.buf[ctx.len++] = val;
121
122
  };
122
- map[INT32] = map[UINT32];
123
- map[INT16] = map[UINT16];
124
- map[INT8] = map[UINT8];
125
123
  export const writeFixedValue = (ctx, val, def, pos) => {
124
+ if (!def.validation(val, def)) {
125
+ return new ModifyError(def, val);
126
+ }
126
127
  const len = ctx.len;
127
128
  ctx.len = pos;
128
129
  const res = map[def.typeIndex](ctx, val, def);
@@ -130,6 +131,9 @@ export const writeFixedValue = (ctx, val, def, pos) => {
130
131
  return res;
131
132
  };
132
133
  export const appendFixedValue = (ctx, val, def) => {
134
+ if (!def.validation(val, def)) {
135
+ return new ModifyError(def, val);
136
+ }
133
137
  return map[def.typeIndex](ctx, val, def);
134
138
  };
135
139
  //# sourceMappingURL=fixed.js.map
@@ -1,5 +1,19 @@
1
1
  import { writeBinary } from './binary.js';
2
+ import { ModifyError } from './ModifyRes.js';
2
3
  export function writeJson(value, ctx, schema, t, parentId, modifyOp) {
3
- return writeBinary(value === null ? null : JSON.stringify(value), ctx, schema, t, parentId, modifyOp);
4
+ try {
5
+ if (value === null) {
6
+ return writeBinary(null, ctx, schema, t, parentId, modifyOp);
7
+ }
8
+ else {
9
+ if (!t.validation(value, t)) {
10
+ return new ModifyError(t, value);
11
+ }
12
+ return writeBinary(JSON.stringify(value), ctx, schema, t, parentId, modifyOp);
13
+ }
14
+ }
15
+ catch (err) {
16
+ return new ModifyError(t, value);
17
+ }
4
18
  }
5
19
  //# sourceMappingURL=json.js.map
@@ -75,7 +75,9 @@ function _modify(ctx, res, obj, schema, mod, tree, overwrite, unsafe) {
75
75
  ctx.buf[ctx.len++] = mainLenU32 >>>= 8;
76
76
  ctx.buf[ctx.len++] = mainLenU32 >>>= 8;
77
77
  ctx.lastMain = ctx.len;
78
- ctx.buf.fill(0, ctx.len, (ctx.len += schema.mainLen));
78
+ ctx.buf.set(schema.mainEmpty, ctx.len);
79
+ ctx.len += schema.mainLen;
80
+ // ctx.buf.fill(0, ctx.len, (ctx.len += schema.mainLen))
79
81
  }
80
82
  if (typeof val === 'object' && val !== null && 'increment' in val) {
81
83
  err = writeFixedValue(ctx, val.increment, def, ctx.lastMain + def.start);
@@ -75,7 +75,8 @@ export function writeEdges(t, ref, ctx) {
75
75
  if (typeof value !== 'string') {
76
76
  return new ModifyError(edge, value);
77
77
  }
78
- if (ctx.len + EDGE_HEADER_SIZE + ENCODER.encode(value).byteLength > ctx.max) {
78
+ if (ctx.len + EDGE_HEADER_SIZE + ENCODER.encode(value).byteLength >
79
+ ctx.max) {
79
80
  return RANGE_ERR;
80
81
  }
81
82
  ctx.buf[ctx.len++] = STRING;
@@ -132,7 +133,7 @@ export function writeEdges(t, ref, ctx) {
132
133
  return RANGE_ERR;
133
134
  }
134
135
  ctx.buf[ctx.len++] = CARDINALITY;
135
- writeHllBuf(value, ctx, t, size);
136
+ writeHllBuf(value, ctx, edge, size);
136
137
  }
137
138
  }
138
139
  else {
@@ -271,6 +272,7 @@ export function writeEdges(t, ref, ctx) {
271
272
  // Index of start of fields
272
273
  const sIndex = ctx.len;
273
274
  ctx.len += mainFieldsStartSize;
275
+ // this has to be replaced
274
276
  // Add zeroes
275
277
  ctx.buf.fill(0, ctx.len, ctx.len + t.edgeMainLen);
276
278
  // Keep track of written bytes from append fixed
@@ -27,7 +27,14 @@ export function writeReferences(value, ctx, schema, def, res, mod) {
27
27
  else if (key === 'delete') {
28
28
  err = deleteRefs(def, ctx, schema, mod, val, res.tmpId);
29
29
  }
30
- else if (key === 'set') {
30
+ else if (key === 'update') {
31
+ // and add add: []
32
+ // replace this with update
33
+ err = updateRefs(def, ctx, schema, mod, val, res.tmpId, 1);
34
+ }
35
+ else if (key === 'add') {
36
+ // and add add: []
37
+ // replace this with update
31
38
  err = updateRefs(def, ctx, schema, mod, val, res.tmpId, 1);
32
39
  }
33
40
  else if (key === 'upsert') {
@@ -173,6 +180,9 @@ function appendRefs(def, ctx, modifyOp, refs, op, remaining) {
173
180
  else {
174
181
  return new ModifyError(def, refs);
175
182
  }
183
+ if (!def.validation(id, def)) {
184
+ return new ModifyError(def, refs);
185
+ }
176
186
  if (hasEdges) {
177
187
  if (index === undefined) {
178
188
  if (ctx.len + 9 > ctx.max) {
@@ -261,17 +271,22 @@ function putRefs(def, ctx, modifyOp, refs, op) {
261
271
  for (; i < refs.length; i++) {
262
272
  let ref = refs[i];
263
273
  if (typeof ref === 'number') {
264
- ctx.buf[ctx.len++] = ref;
265
- ctx.buf[ctx.len++] = ref >>>= 8;
266
- ctx.buf[ctx.len++] = ref >>>= 8;
267
- ctx.buf[ctx.len++] = ref >>>= 8;
274
+ if (!def.validation(ref, def)) {
275
+ break;
276
+ }
277
+ else {
278
+ ctx.buf[ctx.len++] = ref;
279
+ ctx.buf[ctx.len++] = ref >>>= 8;
280
+ ctx.buf[ctx.len++] = ref >>>= 8;
281
+ ctx.buf[ctx.len++] = ref >>>= 8;
282
+ }
268
283
  }
269
284
  else if (ref instanceof ModifyState) {
270
285
  if (ref.error) {
271
286
  return ref.error;
272
287
  }
273
288
  ref = ref.getId();
274
- if (!ref) {
289
+ if (!def.validation(ref, def)) {
275
290
  break;
276
291
  }
277
292
  ctx.buf[ctx.len++] = ref;
@@ -8,11 +8,7 @@ import { write } from '../string.js';
8
8
  // add compression handling for edge fields
9
9
  export function writeString(lang, value, ctx, def, t, parentId, modifyOp) {
10
10
  const isBuffer = value instanceof Uint8Array;
11
- if (typeof value !== 'string' && value !== null && !isBuffer) {
12
- return new ModifyError(t, value);
13
- }
14
- const len = value?.length;
15
- if (!len) {
11
+ if (value === '' || value === null) {
16
12
  if (modifyOp === UPDATE) {
17
13
  if (ctx.len + 11 /* SIZE.DEFAULT_CURSOR */ + 1 > ctx.max) {
18
14
  return RANGE_ERR;
@@ -22,11 +18,14 @@ export function writeString(lang, value, ctx, def, t, parentId, modifyOp) {
22
18
  }
23
19
  }
24
20
  else {
21
+ if (!t.validation(value, t)) {
22
+ return new ModifyError(t, value);
23
+ }
25
24
  let size = isBuffer
26
25
  ? value.byteLength
27
26
  : ENCODER.encode(value).byteLength + 6;
28
27
  if (ctx.len + 11 /* SIZE.DEFAULT_CURSOR */ + 11 + size > ctx.max) {
29
- // +10 OR +11, teh original check was +20 but
28
+ // +10 OR +11, the original check was +20 but
30
29
  // there are 10 addtional bytes in this scope
31
30
  // 5 compression size
32
31
  return RANGE_ERR;
@@ -4,16 +4,6 @@ import { writeString } from './string.js';
4
4
  import { ModifyError } from './ModifyRes.js';
5
5
  import { setCursor } from './setCursor.js';
6
6
  export function writeText(value, ctx, def, t, res, parentId, modifyOp) {
7
- const isBuffer = value instanceof Uint8Array;
8
- if (typeof value !== 'string' &&
9
- value !== null &&
10
- !isBuffer &&
11
- value &&
12
- typeof value !== 'object') {
13
- return new ModifyError(t, value);
14
- }
15
- // const len = value?.length
16
- // think about this
17
7
  if (value === null && !res.locale) {
18
8
  if (modifyOp === UPDATE) {
19
9
  if (ctx.len + 11 /* SIZE.DEFAULT_CURSOR */ + 1 > ctx.max) {
@@ -31,7 +21,6 @@ export function writeText(value, ctx, def, t, res, parentId, modifyOp) {
31
21
  // @ts-ignore
32
22
  value = '';
33
23
  }
34
- // @ts-ignore
35
24
  const err = writeString(res.locale, value, ctx, def, t, res.tmpId, modifyOp);
36
25
  if (modifyOp === CREATE) {
37
26
  const index = t.prop * (def.localeSize + 1);
@@ -2,7 +2,7 @@ import { ALIAS } from '@based/schema/def';
2
2
  export async function upsert(db, type, obj, opts) {
3
3
  const tree = db.schemaTypesParsed[type].tree;
4
4
  let q;
5
- let id = '';
5
+ let id = type;
6
6
  for (const key in obj) {
7
7
  if (tree[key].typeIndex === ALIAS) {
8
8
  id += `${key}:${obj[key]};`;
@@ -18,11 +18,11 @@ export function writeVector(value, ctx, schema, t, parentId, modifyOp) {
18
18
  size = 0;
19
19
  }
20
20
  else {
21
- if (!value) {
22
- return new ModifyError(t, value);
23
- }
24
21
  size = value.byteLength + 4;
25
22
  }
23
+ if (!t.validation(value, t)) {
24
+ return new ModifyError(t, value);
25
+ }
26
26
  if (size === 0) {
27
27
  if (modifyOp === UPDATE) {
28
28
  if (ctx.len + 11 /* SIZE.DEFAULT_CURSOR */ + 1 > ctx.max) {
@@ -162,7 +162,12 @@ export class QueryBranch {
162
162
  });
163
163
  }
164
164
  else if (Array.isArray(f)) {
165
- includeFields(this.def, f);
165
+ if (f.length === 0) {
166
+ includeFields(this.def, ['id']);
167
+ }
168
+ else {
169
+ includeFields(this.def, f);
170
+ }
166
171
  }
167
172
  else if (f !== undefined) {
168
173
  throw new Error('Invalid include statement: expected props, refs and edges (string or array) or function');
@@ -208,6 +213,9 @@ export class BasedDbQuery extends QueryBranch {
208
213
  }
209
214
  }
210
215
  }
216
+ if (!db.schemaIsSetValue) {
217
+ throw new Error('Query: No schema yet - use await db.schemaIsSet()');
218
+ }
211
219
  const def = createQueryDef(db, QueryDefType.Root, target, skipValidation);
212
220
  super(db, def);
213
221
  }
@@ -2,7 +2,7 @@ import { inspect } from 'node:util';
2
2
  import picocolors from 'picocolors';
3
3
  import { debug, resultToObject, readAllFields } from './query.js';
4
4
  import { size, time, inspectData, defHasId, displayTarget } from './display.js';
5
- import { readFloatLE, readUint32 } from '../bitWise.js';
5
+ import { readFloatLE, readUint32 } from '../../utils.js';
6
6
  export { time, size, inspectData };
7
7
  export class BasedQueryResponse {
8
8
  result;
@@ -60,8 +60,9 @@ export class BasedQueryResponse {
60
60
  let id = readUint32(result, i);
61
61
  i += 4;
62
62
  let item;
63
- if (this.def.aggregation == -999 /* AggFn.NONE */) {
63
+ if (this.def.aggregation == 255 /* AggFlag.TEMP */) {
64
64
  item = {};
65
+ this.def.aggregation = 4 /* AggFlag.COUNT */;
65
66
  }
66
67
  else {
67
68
  item = {
@@ -101,7 +102,13 @@ export class BasedQueryResponse {
101
102
  return readUint32(result, offset);
102
103
  }
103
104
  get length() {
104
- return readUint32(this.result, 0);
105
+ const l = readUint32(this.result, 0);
106
+ if (this.def.aggregation != 0 /* AggFlag.NONE */ && this.def.aggregation != null) {
107
+ return l + 1;
108
+ }
109
+ else {
110
+ return l;
111
+ }
105
112
  }
106
113
  toObject() {
107
114
  return resultToObject(this.def, this.result, this.end - 4, 0);
@@ -0,0 +1,3 @@
1
+ import { QueryDef, AggFlag } from './types.js';
2
+ export declare const createAggFlagBuffer: (aggregation: AggFlag) => Uint8Array;
3
+ export declare const count: (def: QueryDef) => void;
@@ -0,0 +1,9 @@
1
+ export const createAggFlagBuffer = (aggregation) => {
2
+ const buf = new Uint8Array(1);
3
+ buf[0] = aggregation;
4
+ return buf;
5
+ };
6
+ export const count = (def) => {
7
+ def.aggregation = 4 /* AggFlag.COUNT */;
8
+ };
9
+ //# sourceMappingURL=aggregation.js.map
@@ -119,7 +119,8 @@ const inspectObject = (object, q, path, level, isLast, isFirst, isObject, depth)
119
119
  let edges = [];
120
120
  for (const k in object) {
121
121
  const key = path ? path + '.' + k : k;
122
- let def = q.props[key];
122
+ let def;
123
+ def = q.props[key];
123
124
  let v = object[k];
124
125
  const isEdge = k[0] === '$';
125
126
  if (k === '$searchScore') {
@@ -144,7 +145,16 @@ const inspectObject = (object, q, path, level, isLast, isFirst, isObject, depth)
144
145
  str += ',\n';
145
146
  }
146
147
  else if (!def) {
147
- str += inspectObject(v, q, key, level + 2, false, false, true, depth) + '';
148
+ if (Object.keys(object)[0] == 'count') {
149
+ // TODO: to flag the agg someway. This is ugly as hell!!!
150
+ str += picocolors.blue(v);
151
+ str += picocolors.italic(picocolors.dim(' count'));
152
+ str += ',\n';
153
+ }
154
+ else {
155
+ str +=
156
+ inspectObject(v, q, key, level + 2, false, false, true, depth) + '';
157
+ }
148
158
  }
149
159
  else if ('__isPropDef' in def) {
150
160
  if (def.typeIndex === REFERENCES) {
@@ -1,4 +1,4 @@
1
- import { TIMESTAMP, CREATED, UPDATED, ENUM, BOOLEAN, STRING, BINARY, TEXT, } from '@based/schema/def';
1
+ import { TIMESTAMP, ENUM, BOOLEAN, STRING, BINARY, TEXT, } from '@based/schema/def';
2
2
  import { crc32 } from '../../crc32.js';
3
3
  import { convertToTimestamp } from '../../timestamp.js';
4
4
  import { ENCODER } from '../../../utils.js';
@@ -39,9 +39,7 @@ export const parseFilterValue = (prop, value) => {
39
39
  else if (prop.typeIndex === ENUM) {
40
40
  return prop.reverseEnum[value] + 1;
41
41
  }
42
- else if (prop.typeIndex === TIMESTAMP ||
43
- prop.typeIndex === CREATED ||
44
- prop.typeIndex === UPDATED) {
42
+ else if (prop.typeIndex === TIMESTAMP) {
45
43
  const v = convertToTimestamp(value);
46
44
  if (typeof v !== 'number') {
47
45
  throw new Error(`Incorrect value for timestamp ${prop.path.join('.')}`);
@@ -33,7 +33,7 @@ const writeConditions = (result, k, offset, conditions) => {
33
33
  export const fillConditionsBuffer = (result, conditions, offset) => {
34
34
  let lastWritten = offset;
35
35
  let orJumpIndex = 0;
36
- if (conditions.or) {
36
+ if (conditions.or && conditions.or.size != 0) {
37
37
  result[lastWritten] = META_OR_BRANCH;
38
38
  lastWritten++;
39
39
  orJumpIndex = lastWritten;
@@ -72,7 +72,7 @@ export const fillConditionsBuffer = (result, conditions, offset) => {
72
72
  result[sizeIndex + 1] = size >>> 8;
73
73
  });
74
74
  }
75
- if (conditions.or) {
75
+ if (conditions.or && conditions.or.size != 0) {
76
76
  const size = fillConditionsBuffer(result, conditions.or, lastWritten);
77
77
  result[orJumpIndex] = size;
78
78
  result[orJumpIndex + 1] = size >>> 8;
@@ -20,6 +20,7 @@ export const walkDefs = (db, def, f) => {
20
20
  def.edges.lang = def.lang;
21
21
  }
22
22
  const edgeProp = def.edges.props[p];
23
+ // console.log(p, !!edgeProp, Object.keys(def.edges.props), def.target)
23
24
  if (edgeProp.typeIndex === REFERENCE ||
24
25
  edgeProp.typeIndex === REFERENCES) {
25
26
  const refDef = createOrGetRefQueryDef(db, def.edges, edgeProp);
@@ -8,4 +8,4 @@ export * from './filter/toBuffer.js';
8
8
  export * from './sort.js';
9
9
  export * from './debug.js';
10
10
  export * from './read/read.js';
11
- export * from './aggregationFn.js';
11
+ export * from './aggregation.js';
@@ -8,5 +8,5 @@ export * from './filter/toBuffer.js';
8
8
  export * from './sort.js';
9
9
  export * from './debug.js';
10
10
  export * from './read/read.js';
11
- export * from './aggregationFn.js';
11
+ export * from './aggregation.js';
12
12
  //# sourceMappingURL=query.js.map
@@ -31,7 +31,6 @@ export const createQueryDef = (db, type, target, skipValidation) => {
31
31
  const t = target;
32
32
  const q = queryDef;
33
33
  q.props = t.ref.edges;
34
- // q.reverseProps = t.ref.reverseEdges
35
34
  q.type = type;
36
35
  q.target = t;
37
36
  return q;
@@ -1,7 +1,7 @@
1
1
  import { ALIAS, ALIASES, BINARY, BOOLEAN, ENUM, INT16, INT32, INT8, NUMBER, STRING, TEXT, TIMESTAMP, UINT16, UINT32, UINT8, VECTOR, JSON, CARDINALITY, } from '@based/schema/def';
2
2
  import { QueryDefType } from '../types.js';
3
3
  import { read, readUtf8 } from '../../string.js';
4
- import { readDoubleLE, readFloatLE, readInt16, readInt32, readUint16, readUint32, } from '../../bitWise.js';
4
+ import { readDoubleLE, readFloatLE, readInt16, readInt32, readUint16, readUint32, } from '../../../utils.js';
5
5
  import { inverseLangMap } from '@based/schema';
6
6
  import { READ_EDGE, READ_ID, READ_REFERENCE, READ_REFERENCES, READ_AGGREGATION, CREATE_AGGREGATION, } from '../types.js';
7
7
  const addField = (p, value, item, defaultOnly = false, lang = 0) => {
@@ -132,7 +132,7 @@ const readMain = (q, result, offset, item) => {
132
132
  return i - offset;
133
133
  };
134
134
  const handleUndefinedProps = (id, q, item) => {
135
- if (q.aggregation != -999 /* AggFn.NONE */) {
135
+ if (q.aggregation == 0 /* AggFlag.NONE */ || q.aggregation == null) {
136
136
  for (const k in q.include.propsRead) {
137
137
  if (q.include.propsRead[k] !== id) {
138
138
  // Only relvant for seperate props
@@ -291,11 +291,12 @@ export const readAllFields = (q, result, offset, end, item, id) => {
291
291
  else if (index === CREATE_AGGREGATION) {
292
292
  i--;
293
293
  result[i] = READ_AGGREGATION;
294
- q.aggregation = -999 /* AggFn.NONE */;
294
+ result[0] = result[0] + 1;
295
+ q.aggregation = 255 /* AggFlag.TEMP */;
295
296
  return i - offset - 4 - (q.search ? 4 : 0);
296
297
  }
297
298
  else if (index === READ_AGGREGATION) {
298
- // TODO: To change to a map and also to get the aggregate field name from a query function parameter
299
+ // TODO: Change to a map and also to get the aggregate field name from a query function parameter
299
300
  const propAgg = {
300
301
  name: 'count',
301
302
  path: ['count'],
@@ -303,6 +304,9 @@ export const readAllFields = (q, result, offset, end, item, id) => {
303
304
  };
304
305
  const size = readUint32(result, i);
305
306
  addField(propAgg, readUint32(result, i + 4), item);
307
+ result[0] = result[0] - 1;
308
+ i--;
309
+ result[i] = CREATE_AGGREGATION;
306
310
  i += 4 + size + 4;
307
311
  }
308
312
  else if (index === 0) {
@@ -310,6 +314,9 @@ export const readAllFields = (q, result, offset, end, item, id) => {
310
314
  }
311
315
  else {
312
316
  const prop = q.schema.reverseProps[index];
317
+ if (!prop) {
318
+ console.log({ prop: !!prop }, index);
319
+ }
313
320
  if (prop.typeIndex === CARDINALITY) {
314
321
  q.include.propsRead[index] = id;
315
322
  const size = readUint32(result, i);
@@ -397,8 +404,9 @@ export const resultToObject = (q, result, end, offset = 0) => {
397
404
  const id = readUint32(result, i);
398
405
  i += 4;
399
406
  let item;
400
- if (q.aggregation == -999 /* AggFn.NONE */) {
407
+ if (q.aggregation == 255 /* AggFlag.TEMP */) {
401
408
  item = {};
409
+ q.aggregation = 4 /* AggFlag.COUNT */;
402
410
  }
403
411
  else {
404
412
  item = {
@@ -414,6 +422,10 @@ export const resultToObject = (q, result, end, offset = 0) => {
414
422
  items.push(item);
415
423
  }
416
424
  if ('id' in q.target || 'alias' in q.target) {
425
+ if (q.type === QueryDefType.Root && q.target.type === '_root') {
426
+ // Todo can be optimized
427
+ delete items[0].id;
428
+ }
417
429
  return items[0];
418
430
  }
419
431
  return items;
@@ -3,7 +3,7 @@ import { QueryDefType } from './types.js';
3
3
  import { includeToBuffer } from './include/toBuffer.js';
4
4
  import { filterToBuffer } from './query.js';
5
5
  import { searchToBuffer } from './search/index.js';
6
- import { createAggFnBuffer } from './aggregationFn.js';
6
+ import { createAggFlagBuffer } from './aggregation.js';
7
7
  import { ENCODER } from '../../utils.js';
8
8
  const byteSize = (arr) => {
9
9
  return arr.reduce((a, b) => {
@@ -95,7 +95,7 @@ export function defToBuffer(db, def) {
95
95
  sortSize = sort.byteLength;
96
96
  }
97
97
  let aggregation;
98
- aggregation = createAggFnBuffer(def.aggregation);
98
+ aggregation = createAggFlagBuffer(def.aggregation);
99
99
  if (def.target.ids) {
100
100
  // type 1
101
101
  // 1: 4 + ids * 4 [ids len] [id,id,id]
@@ -92,7 +92,7 @@ export type QueryDefShared = {
92
92
  };
93
93
  references: Map<number, QueryDef>;
94
94
  edges?: QueryDef;
95
- aggregation: AggFn;
95
+ aggregation: AggFlag;
96
96
  };
97
97
  export type QueryDefEdges = {
98
98
  type: QueryDefType.Edge;
@@ -120,7 +120,8 @@ export declare const READ_REFERENCES = 253;
120
120
  export declare const READ_REFERENCE = 254;
121
121
  export declare const CREATE_AGGREGATION = 250;
122
122
  export declare const READ_AGGREGATION = 251;
123
- export declare const enum AggFn {
123
+ export declare const enum AggFlag {
124
+ NONE = 0,
124
125
  AVG = 1,
125
126
  CARDINALITY = 2,
126
127
  CONCAT = 3,// string aggregation, delimiter should be an argument
@@ -133,5 +134,5 @@ export declare const enum AggFn {
133
134
  STDDEV = 10,// population or sample should be optional parameters, default = sample
134
135
  SUM = 11,
135
136
  VARIANCE = 12,
136
- NONE = -999
137
+ TEMP = 255
137
138
  }