@based/db 0.0.15 → 0.0.17

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 (94) hide show
  1. package/dist/lib/darwin_aarch64/include/selva/db.h +3 -0
  2. package/dist/lib/darwin_aarch64/include/selva/fields.h +1 -2
  3. package/dist/lib/darwin_aarch64/include/selva/history.h +16 -1
  4. package/dist/lib/darwin_aarch64/include/selva/hll.h +2 -1
  5. package/dist/lib/darwin_aarch64/include/selva/types.h +4 -0
  6. package/dist/lib/darwin_aarch64/libnode-v20.11.1.node +0 -0
  7. package/dist/lib/darwin_aarch64/libnode-v20.18.1.node +0 -0
  8. package/dist/lib/darwin_aarch64/libnode-v22.13.0.node +0 -0
  9. package/dist/lib/darwin_aarch64/libselva.dylib +0 -0
  10. package/dist/lib/linux_aarch64/include/selva/db.h +3 -0
  11. package/dist/lib/linux_aarch64/include/selva/fields.h +1 -2
  12. package/dist/lib/linux_aarch64/include/selva/history.h +16 -1
  13. package/dist/lib/linux_aarch64/include/selva/hll.h +2 -1
  14. package/dist/lib/linux_aarch64/include/selva/types.h +4 -0
  15. package/dist/lib/linux_aarch64/libnode-v20.11.1.node +0 -0
  16. package/dist/lib/linux_aarch64/libnode-v20.18.1.node +0 -0
  17. package/dist/lib/linux_aarch64/libnode-v22.13.0.node +0 -0
  18. package/dist/lib/linux_aarch64/libselva.so +0 -0
  19. package/dist/lib/linux_x86_64/include/selva/db.h +3 -0
  20. package/dist/lib/linux_x86_64/include/selva/fields.h +1 -2
  21. package/dist/lib/linux_x86_64/include/selva/history.h +16 -1
  22. package/dist/lib/linux_x86_64/include/selva/hll.h +2 -1
  23. package/dist/lib/linux_x86_64/include/selva/types.h +4 -0
  24. package/dist/lib/linux_x86_64/libnode-v20.11.1.node +0 -0
  25. package/dist/lib/linux_x86_64/libnode-v20.18.1.node +0 -0
  26. package/dist/lib/linux_x86_64/libnode-v22.13.0.node +0 -0
  27. package/dist/lib/linux_x86_64/libselva.so +0 -0
  28. package/dist/src/client/bitWise.js +1 -0
  29. package/dist/src/client/crc32.js +0 -276
  30. package/dist/src/client/modify/ModifyRes.d.ts +4 -2
  31. package/dist/src/client/modify/ModifyRes.js +11 -6
  32. package/dist/src/client/modify/alias.js +2 -2
  33. package/dist/src/client/modify/cardinality.js +14 -10
  34. package/dist/src/client/modify/create.js +8 -9
  35. package/dist/src/client/modify/references/reference.js +14 -7
  36. package/dist/src/client/modify/references/references.js +10 -4
  37. package/dist/src/client/modify/string.js +2 -2
  38. package/dist/src/client/modify/text.js +1 -1
  39. package/dist/src/client/modify/types.d.ts +3 -1
  40. package/dist/src/client/modify/types.js +2 -0
  41. package/dist/src/client/modify/update.js +1 -1
  42. package/dist/src/client/operations.d.ts +2 -2
  43. package/dist/src/client/operations.js +19 -3
  44. package/dist/src/client/query/BasedDbQuery.d.ts +5 -4
  45. package/dist/src/client/query/BasedDbQuery.js +28 -13
  46. package/dist/src/client/query/BasedIterable.js +3 -10
  47. package/dist/src/client/query/display.d.ts +6 -0
  48. package/dist/src/client/query/display.js +63 -4
  49. package/dist/src/client/query/filter/FilterBranch.js +8 -3
  50. package/dist/src/client/query/filter/convertFilter.d.ts +4 -0
  51. package/dist/src/client/query/filter/convertFilter.js +56 -0
  52. package/dist/src/client/query/filter/createFixedFilterBuffer.js +9 -8
  53. package/dist/src/client/query/filter/createReferenceFilter.js +5 -4
  54. package/dist/src/client/query/filter/createVariableFilterBuffer.js +6 -10
  55. package/dist/src/client/query/filter/filter.d.ts +1 -2
  56. package/dist/src/client/query/filter/filter.js +5 -56
  57. package/dist/src/client/query/filter/primitiveFilter.d.ts +2 -2
  58. package/dist/src/client/query/filter/primitiveFilter.js +5 -1
  59. package/dist/src/client/query/filter/toBuffer.js +1 -6
  60. package/dist/src/client/query/filter/types.d.ts +2 -1
  61. package/dist/src/client/query/filter/types.js +23 -12
  62. package/dist/src/client/query/include/props.js +14 -6
  63. package/dist/src/client/query/include/utils.js +2 -2
  64. package/dist/src/client/query/include/walk.js +1 -7
  65. package/dist/src/client/query/queryDef.d.ts +1 -1
  66. package/dist/src/client/query/queryDef.js +4 -3
  67. package/dist/src/client/query/read/read.js +21 -32
  68. package/dist/src/client/query/read/types.d.ts +4 -0
  69. package/dist/src/client/query/read/types.js +5 -0
  70. package/dist/src/client/query/registerQuery.js +1 -0
  71. package/dist/src/client/query/search/index.js +12 -6
  72. package/dist/src/client/query/sort.js +7 -6
  73. package/dist/src/client/query/thresholds.d.ts +0 -1
  74. package/dist/src/client/query/thresholds.js +0 -8
  75. package/dist/src/client/query/toBuffer.js +0 -3
  76. package/dist/src/client/query/types.d.ts +2 -0
  77. package/dist/src/client/query/validation.d.ts +39 -1
  78. package/dist/src/client/query/validation.js +299 -93
  79. package/dist/src/client/xxHash64.d.ts +1 -1
  80. package/dist/src/client/xxHash64.js +7 -2
  81. package/dist/src/index.d.ts +6 -1
  82. package/dist/src/index.js +31 -1
  83. package/dist/src/native.d.ts +3 -1
  84. package/dist/src/native.js +8 -2
  85. package/dist/src/server/index.d.ts +8 -6
  86. package/dist/src/server/index.js +56 -30
  87. package/dist/src/server/migrate/index.js +10 -2
  88. package/dist/src/server/migrate/worker.js +1 -1
  89. package/dist/src/server/schema/typeDef.js +35 -25
  90. package/dist/src/server/schema/types.d.ts +10 -5
  91. package/dist/src/server/schema/types.js +1 -1
  92. package/dist/src/server/schema/utils.d.ts +1 -0
  93. package/dist/src/server/schema/utils.js +18 -5
  94. package/package.json +5 -3
@@ -1,4 +1,4 @@
1
- import { RANGE_ERR } from './types.js';
1
+ import { RANGE_ERR, CREATE, } from './types.js';
2
2
  import { ModifyError } from './ModifyRes.js';
3
3
  import { setCursor } from './setCursor.js';
4
4
  import { xxHash64 } from '../xxHash64.js';
@@ -14,28 +14,32 @@ export function writeHll(value, ctx, def, t, parentId, modifyOp) {
14
14
  value = [value];
15
15
  }
16
16
  const err = addHll(value, ctx, def, t, parentId, modifyOp);
17
+ if (!err && modifyOp === CREATE) {
18
+ def.seperateSort.bufferTmp[t.prop] = 2;
19
+ ctx.hasSortField++;
20
+ }
21
+ return err;
17
22
  }
18
23
  function addHll(value, ctx, def, t, parentId, modifyOp) {
19
24
  const len = value.length;
20
- let size = 2 + len * 8;
25
+ let size = 4 + len * 8;
21
26
  if (ctx.len + size + 11 > ctx.max) {
22
27
  return RANGE_ERR;
23
28
  }
24
29
  setCursor(ctx, def, t.prop, t.typeIndex, parentId, modifyOp);
25
30
  ctx.buf[ctx.len++] = modifyOp;
26
- ctx.buf.writeUint16LE(len, ctx.len);
27
- ctx.len += 2;
31
+ ctx.buf.writeUint32LE(len, ctx.len);
32
+ ctx.len += 4;
28
33
  for (let val of value) {
29
- let b;
30
34
  if (typeof val === 'string') {
31
- b = Buffer.from(val);
35
+ xxHash64(Buffer.from(val), ctx.buf, ctx.len);
36
+ }
37
+ else if (val instanceof Buffer && val.byteLength === 8) {
38
+ ctx.buf.set(val, ctx.len);
32
39
  }
33
- else if (!(val instanceof Buffer)) {
34
- b = val;
40
+ else {
35
41
  return new ModifyError(t, val);
36
42
  }
37
- const hash = xxHash64(b);
38
- ctx.buf.writeBigUInt64LE(hash, ctx.len);
39
43
  ctx.len += 8;
40
44
  }
41
45
  }
@@ -3,7 +3,7 @@ import { startDrain, flushBuffer } from '../operations.js';
3
3
  import { setCursor } from './setCursor.js';
4
4
  import { modify } from './modify.js';
5
5
  import { ModifyState } from './ModifyRes.js';
6
- import { CREATE, RANGE_ERR } from './types.js';
6
+ import { CREATE, RANGE_ERR, ADD_EMPTY_SORT, } from './types.js';
7
7
  import { writeFixedValue } from './fixed.js';
8
8
  import { getSubscriptionMarkers } from '../query/subscription/index.js';
9
9
  const appendCreate = (ctx, def, obj, res, unsafe) => {
@@ -38,17 +38,16 @@ const appendCreate = (ctx, def, obj, res, unsafe) => {
38
38
  }
39
39
  }
40
40
  }
41
- // if touched lets see perf impact here
42
- if (def.hasStringProp) {
43
- if (ctx.hasStringField !== def.stringPropsSize - 1) {
41
+ if (def.hasSeperateSort) {
42
+ if (ctx.hasSortField !== def.seperateSort.size - 1) {
44
43
  if (ctx.len + 3 > ctx.max) {
45
44
  return RANGE_ERR;
46
45
  }
47
- ctx.buf[ctx.len++] = 7;
46
+ ctx.buf[ctx.len++] = ADD_EMPTY_SORT;
48
47
  let sizepos = ctx.len;
49
48
  ctx.len += 2;
50
- for (const { prop } of def.stringPropsLoop) {
51
- if (def.stringPropsCurrent[prop] === 1) {
49
+ for (const { prop } of def.seperateSort.props) {
50
+ if (def.seperateSort.bufferTmp[prop] === 1) {
52
51
  if (ctx.len + 1 > ctx.max) {
53
52
  return RANGE_ERR;
54
53
  }
@@ -59,8 +58,8 @@ const appendCreate = (ctx, def, obj, res, unsafe) => {
59
58
  ctx.buf[sizepos++] = size;
60
59
  ctx.buf[sizepos] = size >>>= 8;
61
60
  }
62
- if (ctx.hasStringField !== -1) {
63
- def.stringProps.copy(def.stringPropsCurrent);
61
+ if (ctx.hasSortField !== -1) {
62
+ def.seperateSort.buffer.copy(def.seperateSort.bufferTmp);
64
63
  }
65
64
  }
66
65
  };
@@ -3,14 +3,19 @@ import { setCursor } from '../setCursor.js';
3
3
  import { DELETE, RANGE_ERR } from '../types.js';
4
4
  import { getEdgeSize, writeEdges } from './edge.js';
5
5
  import { dbUpdateFromUpsert } from './references.js';
6
- function writeRef(id, ctx, schema, def, parentId, modifyOp, hasEdges) {
6
+ function writeRef(id, ctx, schema, def, parentId, modifyOp, hasEdges, isTmpId) {
7
7
  if (ctx.len + 16 > ctx.max) {
8
8
  return RANGE_ERR;
9
9
  }
10
10
  ctx.markNodeDirty(ctx.db.schemaTypesParsed[def.inverseTypeName], id);
11
11
  setCursor(ctx, schema, def.prop, def.typeIndex, parentId, modifyOp);
12
12
  ctx.buf[ctx.len++] = modifyOp;
13
- ctx.buf[ctx.len++] = hasEdges ? 1 : 0;
13
+ if (isTmpId) {
14
+ ctx.buf[ctx.len++] = hasEdges ? 2 : 3;
15
+ }
16
+ else {
17
+ ctx.buf[ctx.len++] = hasEdges ? 1 : 0;
18
+ }
14
19
  ctx.buf[ctx.len++] = id;
15
20
  ctx.buf[ctx.len++] = id >>>= 8;
16
21
  ctx.buf[ctx.len++] = id >>>= 8;
@@ -18,20 +23,22 @@ function writeRef(id, ctx, schema, def, parentId, modifyOp, hasEdges) {
18
23
  }
19
24
  function singleReferenceEdges(ref, ctx, schema, def, parentId, modifyOp) {
20
25
  let id = ref.id;
26
+ let isTmpId;
21
27
  if (typeof id !== 'number') {
22
28
  if (id instanceof ModifyState) {
23
29
  if (id.error) {
24
30
  return id.error;
25
31
  }
32
+ isTmpId = !id.resolved;
26
33
  id = id.tmpId;
27
34
  }
28
35
  }
29
36
  if (id > 0) {
30
37
  const edgesLen = def.edgesTotalLen || getEdgeSize(def, ref);
31
38
  if (edgesLen === 0) {
32
- return writeRef(id, ctx, schema, def, parentId, modifyOp, false);
39
+ return writeRef(id, ctx, schema, def, parentId, modifyOp, false, isTmpId);
33
40
  }
34
- let err = writeRef(id, ctx, schema, def, parentId, modifyOp, true);
41
+ let err = writeRef(id, ctx, schema, def, parentId, modifyOp, true, isTmpId);
35
42
  if (err) {
36
43
  return err;
37
44
  }
@@ -63,20 +70,20 @@ export function writeReference(value, ctx, schema, def, res, modifyOp) {
63
70
  ctx.buf[ctx.len++] = DELETE;
64
71
  }
65
72
  else if (typeof value === 'number') {
66
- return writeRef(value, ctx, schema, def, res.tmpId, modifyOp, false);
73
+ return writeRef(value, ctx, schema, def, res.tmpId, modifyOp, false, false);
67
74
  }
68
75
  else if (value instanceof ModifyState) {
69
76
  if (value.error) {
70
77
  return value.error;
71
78
  }
72
- return writeRef(value.tmpId, ctx, schema, def, res.tmpId, modifyOp, false);
79
+ return writeRef(value.tmpId, ctx, schema, def, res.tmpId, modifyOp, false, !value.resolved);
73
80
  }
74
81
  else if (typeof value === 'object' && value !== null) {
75
82
  if (def.edges) {
76
83
  return singleReferenceEdges(value, ctx, schema, def, res.tmpId, modifyOp);
77
84
  }
78
85
  else if (typeof value.id === 'number') {
79
- return writeRef(value.id, ctx, schema, def, res.tmpId, modifyOp, false);
86
+ return writeRef(value.id, ctx, schema, def, res.tmpId, modifyOp, false, false);
80
87
  }
81
88
  else if (typeof value.upsert === 'object' && value.upsert !== null) {
82
89
  dbUpdateFromUpsert(ctx, schema, def, res, ctx.db.upsert(def.inverseTypeName, value.upsert));
@@ -130,12 +130,14 @@ function appendRefs(def, ctx, modifyOp, refs, op, remaining) {
130
130
  const ref = refs[i];
131
131
  let id;
132
132
  let index;
133
+ let isTmpId;
133
134
  if (typeof ref === 'object') {
134
135
  if (ref instanceof ModifyState) {
135
136
  if (ref.error) {
136
137
  return ref.error;
137
138
  }
138
139
  id = ref.tmpId;
140
+ isTmpId = !ref.resolved;
139
141
  }
140
142
  else if (ref.id instanceof ModifyState) {
141
143
  if (ref.id.error) {
@@ -143,6 +145,7 @@ function appendRefs(def, ctx, modifyOp, refs, op, remaining) {
143
145
  }
144
146
  id = ref.id.tmpId;
145
147
  index = ref.$index;
148
+ isTmpId = !ref.id.resolved;
146
149
  }
147
150
  else if (ref.id > 0) {
148
151
  id = ref.id;
@@ -163,7 +166,7 @@ function appendRefs(def, ctx, modifyOp, refs, op, remaining) {
163
166
  if (ctx.len + 9 > ctx.max) {
164
167
  return RANGE_ERR;
165
168
  }
166
- ctx.buf[ctx.len++] = 1;
169
+ ctx.buf[ctx.len++] = isTmpId ? 5 : 1;
167
170
  ctx.buf[ctx.len++] = id;
168
171
  ctx.buf[ctx.len++] = id >>>= 8;
169
172
  ctx.buf[ctx.len++] = id >>>= 8;
@@ -173,7 +176,7 @@ function appendRefs(def, ctx, modifyOp, refs, op, remaining) {
173
176
  if (ctx.len + 13 > ctx.max) {
174
177
  return RANGE_ERR;
175
178
  }
176
- ctx.buf[ctx.len++] = 2;
179
+ ctx.buf[ctx.len++] = isTmpId ? 6 : 2;
177
180
  ctx.buf[ctx.len++] = id;
178
181
  ctx.buf[ctx.len++] = id >>>= 8;
179
182
  ctx.buf[ctx.len++] = id >>>= 8;
@@ -202,7 +205,7 @@ function appendRefs(def, ctx, modifyOp, refs, op, remaining) {
202
205
  if (ctx.len + 5 > ctx.max) {
203
206
  return RANGE_ERR;
204
207
  }
205
- ctx.buf[ctx.len++] = 0;
208
+ ctx.buf[ctx.len++] = isTmpId ? 4 : 0;
206
209
  ctx.buf[ctx.len++] = id;
207
210
  ctx.buf[ctx.len++] = id >>>= 8;
208
211
  ctx.buf[ctx.len++] = id >>>= 8;
@@ -212,7 +215,7 @@ function appendRefs(def, ctx, modifyOp, refs, op, remaining) {
212
215
  if (ctx.len + 9 > ctx.max) {
213
216
  return RANGE_ERR;
214
217
  }
215
- ctx.buf[ctx.len++] = 3;
218
+ ctx.buf[ctx.len++] = isTmpId ? 7 : 3;
216
219
  ctx.buf[ctx.len++] = id;
217
220
  ctx.buf[ctx.len++] = id >>>= 8;
218
221
  ctx.buf[ctx.len++] = id >>>= 8;
@@ -255,6 +258,9 @@ function putRefs(def, ctx, modifyOp, refs, op) {
255
258
  if (ref.error) {
256
259
  return ref.error;
257
260
  }
261
+ if (!ref.resolved) {
262
+ break;
263
+ }
258
264
  ref = ref.tmpId;
259
265
  ctx.buf[ctx.len++] = ref;
260
266
  ctx.buf[ctx.len++] = ref >>>= 8;
@@ -29,8 +29,8 @@ export function writeString(lang, value, ctx, def, t, parentId, modifyOp) {
29
29
  return RANGE_ERR;
30
30
  }
31
31
  if (modifyOp === CREATE) {
32
- def.stringPropsCurrent[t.prop] = 2;
33
- ctx.hasStringField++;
32
+ def.seperateSort.bufferTmp[t.prop] = 2;
33
+ ctx.hasSortField++;
34
34
  }
35
35
  setCursor(ctx, def, t.prop, t.typeIndex, parentId, modifyOp);
36
36
  // TODO if buffer check if second byte is zero or one
@@ -3,7 +3,7 @@ import { writeString } from './string.js';
3
3
  export function writeText(value, ctx, def, t, res, modifyOp) {
4
4
  // todo proper fallback as well
5
5
  if (value && typeof value !== 'object') {
6
- const err = writeString(res.i18n ?? langCodesMap.get('en'), value, ctx, def, t, res.tmpId, modifyOp);
6
+ const err = writeString(res.locale ?? langCodesMap.get('en'), value, ctx, def, t, res.tmpId, modifyOp);
7
7
  }
8
8
  else {
9
9
  for (const lang in value) {
@@ -8,6 +8,8 @@ export declare const RANGE_ERR = 1;
8
8
  export declare const INCREMENT = 12;
9
9
  export declare const DECREMENT = 13;
10
10
  export declare const EXPIRE = 14;
11
+ export declare const ADD_EMPTY_SORT_TEXT = 15;
12
+ export declare const ADD_EMPTY_SORT = 7;
11
13
  export declare const SWITCH_TYPE = 2;
12
14
  export declare const SWITCH_FIELD = 0;
13
15
  export declare const SWITCH_ID_CREATE = 9;
@@ -16,6 +18,6 @@ export type ModifyErr = typeof RANGE_ERR | ModifyError | void;
16
18
  export type ModifyOp = typeof CREATE | typeof UPDATE | typeof INCREMENT | typeof EXPIRE;
17
19
  export type ModifyOpts = {
18
20
  unsafe?: boolean;
19
- i18n?: LangName;
21
+ locale?: LangName;
20
22
  overwrite?: boolean;
21
23
  };
@@ -6,6 +6,8 @@ export const RANGE_ERR = 1;
6
6
  export const INCREMENT = 12;
7
7
  export const DECREMENT = 13;
8
8
  export const EXPIRE = 14;
9
+ export const ADD_EMPTY_SORT_TEXT = 15;
10
+ export const ADD_EMPTY_SORT = 7;
9
11
  export const SWITCH_TYPE = 2;
10
12
  export const SWITCH_FIELD = 0;
11
13
  export const SWITCH_ID_CREATE = 9;
@@ -59,7 +59,7 @@ export const update = (db, type, id, obj, opts) => {
59
59
  }
60
60
  const ctx = db.modifyCtx;
61
61
  const pos = ctx.len;
62
- const res = new ModifyState(def.id, id, db, getSubscriptionMarkers(db, def.id, id, false), opts);
62
+ const res = new ModifyState(def.id, id, db, getSubscriptionMarkers(db, def.id, id, false), opts, true);
63
63
  const err = appendUpdate(ctx, def, obj, res, opts?.overwrite);
64
64
  if (err) {
65
65
  ctx.prefix0 = -1; // force a new cursor
@@ -6,7 +6,7 @@ export declare class ModifyCtx {
6
6
  len: number;
7
7
  id: number;
8
8
  lastMain: number;
9
- hasStringField: number;
9
+ hasSortField: number;
10
10
  queue: Map<(payload: any) => void, ModifyState>;
11
11
  ctx: {
12
12
  offsets?: Record<number, number>;
@@ -25,7 +25,7 @@ export declare class ModifyCtx {
25
25
  markNodeDirty(schema: SchemaTypeDef, nodeId: number): void;
26
26
  markTypeDirty(schema: SchemaTypeDef): void;
27
27
  updateMax(): void;
28
- getData(): Buffer;
28
+ getData(lastIds: Record<number, number>): Buffer;
29
29
  }
30
30
  export declare const flushBuffer: (db: DbClient) => Promise<void>;
31
31
  export declare const startDrain: (db: DbClient) => void;
@@ -9,7 +9,7 @@ export class ModifyCtx {
9
9
  len = 0;
10
10
  id = -1;
11
11
  lastMain = -1;
12
- hasStringField = -1;
12
+ hasSortField = -1;
13
13
  queue = new Map();
14
14
  ctx = {}; // maybe make this different?
15
15
  payload;
@@ -47,7 +47,7 @@ export class ModifyCtx {
47
47
  this.dirtyTypes.size * 10 -
48
48
  this.dirtyRanges.size * 8;
49
49
  }
50
- getData() {
50
+ getData(lastIds) {
51
51
  const rangesSize = this.dirtyRanges.size;
52
52
  const typesSize = this.dirtyTypes.size;
53
53
  const data = this.buf.subarray(0, this.len + 4 + 2 + typesSize * 10 + rangesSize * 8);
@@ -56,6 +56,7 @@ export class ModifyCtx {
56
56
  i += 2;
57
57
  for (const [id, startId] of this.dirtyTypes) {
58
58
  const lastId = this.db.schemaTypesParsedById[id].lastId;
59
+ lastIds[id] = lastId;
59
60
  data.writeUint16LE(id, i);
60
61
  i += 2;
61
62
  data.writeUint32LE(startId, i);
@@ -76,11 +77,26 @@ export const flushBuffer = (db) => {
76
77
  let flushPromise;
77
78
  if (ctx.len) {
78
79
  const d = Date.now();
79
- const data = ctx.getData();
80
+ const lastIds = {};
81
+ const data = ctx.getData(lastIds);
80
82
  const resCtx = ctx.ctx;
81
83
  const queue = ctx.queue;
82
84
  flushPromise = db.hooks.flushModify(data).then(({ offsets }) => {
83
85
  resCtx.offsets = offsets;
86
+ for (const typeId in lastIds) {
87
+ if (typeId in offsets) {
88
+ const lastId = lastIds[typeId] + offsets[typeId];
89
+ const def = db.schemaTypesParsedById[typeId];
90
+ const delta = lastId - def.lastId;
91
+ if (delta > 0) {
92
+ def.lastId += delta;
93
+ def.total += delta;
94
+ }
95
+ }
96
+ else {
97
+ console.warn('no offset returned, very wrong');
98
+ }
99
+ }
84
100
  db.writeTime += Date.now() - d;
85
101
  if (queue.size) {
86
102
  flushPromise.then(() => {
@@ -14,12 +14,12 @@ export declare class QueryBranch<T> {
14
14
  constructor(db: DbClient, def: QueryDef);
15
15
  sort(field: string, order?: 'asc' | 'desc'): T;
16
16
  filter<O extends Operator>(field: string, operator?: O | boolean, value?: any, opts?: FilterOpts<O>): T;
17
+ filterBatch(f: FilterAst): this;
17
18
  search(query: string, ...fields: Search[]): T;
18
19
  search(query: ArrayBufferView, field: string, opts?: Omit<FilterOpts, 'lowerCase'>): T;
19
- filterBatch(f: FilterAst): this;
20
20
  or(fn: FilterBranchFn): T;
21
21
  or(field: string, operator?: Operator | boolean, value?: any, opts?: FilterOpts): T;
22
- range(offset: number, limit: number): T;
22
+ range(offset: number, limit?: number): T;
23
23
  include(...fields: (string | BranchInclude | string[])[]): T;
24
24
  }
25
25
  export declare class BasedDbReferenceQuery extends QueryBranch<BasedDbReferenceQuery> {
@@ -31,12 +31,13 @@ declare class GetPromise extends Promise<BasedQueryResponse> {
31
31
  }
32
32
  export declare class BasedDbQuery extends QueryBranch<BasedDbQuery> {
33
33
  #private;
34
- constructor(db: DbClient, type: string, id?: QueryByAliasObj | number | Uint32Array | (QueryByAliasObj | number)[]);
34
+ skipValidation: boolean;
35
+ constructor(db: DbClient, type: string, id?: QueryByAliasObj | number | Uint32Array | (QueryByAliasObj | number)[], skipValidation?: boolean);
35
36
  id: number;
36
37
  get(): GetPromise;
37
38
  buffer: Buffer;
38
39
  register(): void;
39
- i18n(locale: LangName): this;
40
+ locale(locale: LangName): this;
40
41
  subscribe(onData: OnData, onError?: OnError): import("./subscription/types.js").OnClose;
41
42
  _getSync(dbCtxExternal: any): BasedQueryResponse;
42
43
  toBuffer(): Buffer;
@@ -1,4 +1,4 @@
1
- import { createQueryDef, QueryDefType, filter, sort, defToBuffer, filterOr, convertFilter, isAlias, includeField, includeFields, } from './query.js';
1
+ import { createQueryDef, QueryDefType, filter, sort, defToBuffer, filterOr, isAlias, includeField, includeFields, } from './query.js';
2
2
  import { BasedQueryResponse } from './BasedIterable.js';
3
3
  import { createOrGetEdgeRefQueryDef, createOrGetRefQueryDef, } from './include/utils.js';
4
4
  import { FilterBranch } from './filter/FilterBranch.js';
@@ -8,6 +8,9 @@ import { REFERENCE, REFERENCES } from '../../server/schema/types.js';
8
8
  import { subscribe } from './subscription/index.js';
9
9
  import { registerQuery } from './registerQuery.js';
10
10
  import { langCodesMap } from '@based/schema';
11
+ import { convertFilter } from './filter/convertFilter.js';
12
+ import { validateLocale, validateRange } from './validation.js';
13
+ import { DEF_RANGE_PROP_LIMIT } from './thresholds.js';
11
14
  export class QueryBranch {
12
15
  db;
13
16
  def;
@@ -22,6 +25,15 @@ export class QueryBranch {
22
25
  }
23
26
  filter(field, operator, value, opts) {
24
27
  const f = convertFilter(this.def, field, operator, value, opts);
28
+ if (!f) {
29
+ // @ts-ignore
30
+ return this;
31
+ }
32
+ filter(this.db, this.def, f, this.def.filter);
33
+ // @ts-ignore
34
+ return this;
35
+ }
36
+ filterBatch(f) {
25
37
  filter(this.db, this.def, f, this.def.filter);
26
38
  // @ts-ignore
27
39
  return this;
@@ -81,11 +93,6 @@ export class QueryBranch {
81
93
  // @ts-ignore
82
94
  return this;
83
95
  }
84
- filterBatch(f) {
85
- filter(this.db, this.def, f, this.def.filter);
86
- // @ts-ignore
87
- return this;
88
- }
89
96
  or(field, operator, value, opts) {
90
97
  if (typeof field === 'function') {
91
98
  const f = new FilterBranch(this.db, filterOr(this.db, this.def, [], this.def.filter), this.def);
@@ -94,12 +101,20 @@ export class QueryBranch {
94
101
  }
95
102
  else {
96
103
  const f = convertFilter(this.def, field, operator, value, opts);
97
- filterOr(this.db, this.def, f, this.def.filter);
104
+ if (f) {
105
+ filterOr(this.db, this.def, f, this.def.filter);
106
+ }
98
107
  }
99
108
  // @ts-ignore
100
109
  return this;
101
110
  }
102
- range(offset, limit) {
111
+ range(offset, limit = DEF_RANGE_PROP_LIMIT) {
112
+ if (validateRange(this.def, offset, limit)) {
113
+ this.def.range.offset = 0;
114
+ this.def.range.limit = DEF_RANGE_PROP_LIMIT;
115
+ // @ts-ignore
116
+ return this;
117
+ }
103
118
  this.def.range.offset = offset;
104
119
  this.def.range.limit = limit;
105
120
  // @ts-ignore
@@ -165,7 +180,8 @@ class GetPromise extends Promise {
165
180
  }
166
181
  }
167
182
  export class BasedDbQuery extends QueryBranch {
168
- constructor(db, type, id) {
183
+ skipValidation = false;
184
+ constructor(db, type, id, skipValidation) {
169
185
  const target = {
170
186
  type,
171
187
  };
@@ -178,15 +194,13 @@ export class BasedDbQuery extends QueryBranch {
178
194
  // TODO ADD MULTI ALIAS
179
195
  // @ts-ignore
180
196
  target.ids = id;
181
- // target.ids = new Uint32Array(id)
182
- // target.ids.sort()
183
197
  }
184
198
  else {
185
199
  target.id = id;
186
200
  }
187
201
  }
188
202
  }
189
- const def = createQueryDef(db, QueryDefType.Root, target);
203
+ const def = createQueryDef(db, QueryDefType.Root, target, skipValidation);
190
204
  super(db, def);
191
205
  }
192
206
  #getInternal = async (resolve, reject) => {
@@ -219,7 +233,8 @@ export class BasedDbQuery extends QueryBranch {
219
233
  register() {
220
234
  registerQuery(this);
221
235
  }
222
- i18n(locale) {
236
+ locale(locale) {
237
+ validateLocale(this.def, locale);
223
238
  this.def.lang = langCodesMap.get(locale) ?? 0;
224
239
  return this;
225
240
  }
@@ -1,7 +1,7 @@
1
1
  import { inspect } from 'node:util';
2
2
  import picocolors from 'picocolors';
3
3
  import { debug, resultToObject, readAllFields } from './query.js';
4
- import { size, time, inspectData } from './display.js';
4
+ import { size, time, inspectData, defHasId, displayTarget } from './display.js';
5
5
  import { readFloatLE, readUint32 } from '../bitWise.js';
6
6
  export { time, size, inspectData };
7
7
  export class BasedQueryResponse {
@@ -21,15 +21,8 @@ export class BasedQueryResponse {
21
21
  return this.result.length;
22
22
  }
23
23
  [inspect.custom](depth) {
24
- const hasId = 'id' in this.def.target || 'alias' in this.def.target;
25
- const target = hasId
26
- ? this.def.schema.type +
27
- ':' +
28
- ('alias' in this.def.target
29
- ? inspect(this.def.target.alias)
30
- : // @ts-ignore
31
- this.def.target.id)
32
- : this.def.schema.type;
24
+ const hasId = defHasId(this.def);
25
+ const target = displayTarget(this.def);
33
26
  let str = '';
34
27
  str += '\n execTime: ' + time(this.execTime);
35
28
  str += '\n size: ' + size(this.result.byteLength);
@@ -1,5 +1,11 @@
1
1
  import { QueryDef } from './types.js';
2
+ import { TypeIndex } from '../../server/schema/types.js';
2
3
  import { BasedQueryResponse } from './BasedIterable.js';
3
4
  export declare const size: (size: number) => string;
4
5
  export declare const time: (time: number) => string;
6
+ export declare const prettyPrintVal: (v: any, type: TypeIndex) => string;
7
+ export declare const parseUint8Array: (p: any) => any;
8
+ export declare const safeStringify: (p: any, nr?: number) => string;
5
9
  export declare const inspectData: (q: BasedQueryResponse, def: QueryDef, level: number, top: boolean, depth: number, hasId?: boolean) => string;
10
+ export declare const defHasId: (def: QueryDef) => boolean;
11
+ export declare const displayTarget: (def: QueryDef) => string;
@@ -1,5 +1,5 @@
1
1
  import picocolors from 'picocolors';
2
- import { BINARY, NUMBER, REFERENCE, REFERENCES, STRING, TEXT, TIMESTAMP, } from '../../server/schema/types.js';
2
+ import { BINARY, CARDINALITY, NUMBER, REFERENCE, REFERENCES, STRING, TEXT, TIMESTAMP, } from '../../server/schema/types.js';
3
3
  const decimals = (v) => ~~(v * 100) / 100;
4
4
  const sizeCalc = (size) => {
5
5
  if (size > 1e6) {
@@ -34,7 +34,7 @@ export const time = (time) => {
34
34
  return picocolors.green(str);
35
35
  }
36
36
  };
37
- const prettyPrintVal = (v, type) => {
37
+ export const prettyPrintVal = (v, type) => {
38
38
  if (type === BINARY) {
39
39
  const nr = 12;
40
40
  const isLarger = v.length > nr;
@@ -59,6 +59,9 @@ const prettyPrintVal = (v, type) => {
59
59
  return `"${v}"`;
60
60
  }
61
61
  }
62
+ if (type === CARDINALITY) {
63
+ return `${picocolors.blue(v)} ${picocolors.italic(picocolors.dim('unique'))}`;
64
+ }
62
65
  if (type === TIMESTAMP) {
63
66
  if (v === 0) {
64
67
  return `0 ${picocolors.italic(picocolors.dim('No date'))}`;
@@ -69,6 +72,37 @@ const prettyPrintVal = (v, type) => {
69
72
  }
70
73
  return v;
71
74
  };
75
+ export const parseUint8Array = (p) => {
76
+ if (ArrayBuffer.isView(p)) {
77
+ const x = [];
78
+ // @ts-ignore
79
+ for (let i = 0; i < p.length; i++) {
80
+ x[i] = p[i];
81
+ }
82
+ p = x;
83
+ return p;
84
+ }
85
+ return p;
86
+ };
87
+ export const safeStringify = (p, nr = 30) => {
88
+ var v;
89
+ try {
90
+ p = parseUint8Array(p);
91
+ if (typeof p === 'object') {
92
+ for (const key in p) {
93
+ p[key] = parseUint8Array(p[key]);
94
+ }
95
+ }
96
+ v = JSON.stringify(p).replace(/"/g, '').slice(0, nr);
97
+ if (v.length === nr) {
98
+ v += '...';
99
+ }
100
+ }
101
+ catch (err) {
102
+ v = '';
103
+ }
104
+ return v;
105
+ };
72
106
  const inspectObject = (object, q, path, level, isLast, isFirst, isObject, depth) => {
73
107
  const prefix = ''.padEnd(level, ' ');
74
108
  let str = '';
@@ -100,8 +134,10 @@ const inspectObject = (object, q, path, level, isLast, isFirst, isObject, depth)
100
134
  // skip
101
135
  }
102
136
  else if (key === 'id') {
103
- // @ts-ignore
104
- str += v + picocolors.italic(picocolors.dim(` ${q.target.type}`));
137
+ str +=
138
+ picocolors.blue(v) +
139
+ // @ts-ignore
140
+ picocolors.italic(picocolors.dim(` ${q.target.type}`));
105
141
  str += ',\n';
106
142
  }
107
143
  else if (!def) {
@@ -146,6 +182,9 @@ const inspectObject = (object, q, path, level, isLast, isFirst, isObject, depth)
146
182
  }
147
183
  str += prettyPrintVal(v, def.typeIndex);
148
184
  }
185
+ else if (def.typeIndex === CARDINALITY) {
186
+ str += prettyPrintVal(v, def.typeIndex);
187
+ }
149
188
  else if (def.typeIndex === TIMESTAMP) {
150
189
  str += prettyPrintVal(v, def.typeIndex);
151
190
  }
@@ -245,4 +284,24 @@ export const inspectData = (q, def, level, top, depth, hasId = false) => {
245
284
  }
246
285
  return str;
247
286
  };
287
+ export const defHasId = (def) => {
288
+ return 'id' in def.target || 'alias' in def.target;
289
+ };
290
+ export const displayTarget = (def) => {
291
+ // ids
292
+ const hasId = defHasId(def);
293
+ const hasIds = 'ids' in def.target;
294
+ const target = hasId || hasIds
295
+ ? def.schema.type +
296
+ ':' +
297
+ (hasIds
298
+ ? // @ts-ignore
299
+ `ids(${def.target?.ids?.length ?? 0})`
300
+ : 'alias' in def.target
301
+ ? safeStringify(def.target.alias, 30)
302
+ : // @ts-ignore
303
+ def.target.id)
304
+ : def.schema.type;
305
+ return target;
306
+ };
248
307
  //# sourceMappingURL=display.js.map
@@ -1,4 +1,5 @@
1
- import { convertFilter, filter, filterOr } from './filter.js';
1
+ import { filter, filterOr } from './filter.js';
2
+ import { convertFilter } from './convertFilter.js';
2
3
  export class FilterBranch {
3
4
  constructor(db, filterBranch, def) {
4
5
  this.def = def;
@@ -16,13 +17,17 @@ export class FilterBranch {
16
17
  }
17
18
  else {
18
19
  const f = convertFilter(this.def, field, operator, value, opts);
19
- filterOr(this.db, this.def, f, this.filterBranch);
20
+ if (f) {
21
+ filterOr(this.db, this.def, f, this.filterBranch);
22
+ }
20
23
  }
21
24
  return this;
22
25
  }
23
26
  filter(field, operator, value) {
24
27
  const f = convertFilter(this.def, field, operator, value);
25
- filter(this.db, this.def, f, this.filterBranch);
28
+ if (f) {
29
+ filter(this.db, this.def, f, this.filterBranch);
30
+ }
26
31
  return this;
27
32
  }
28
33
  }