@based/db 0.0.31 → 0.0.33

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 (83) hide show
  1. package/README.md +565 -3
  2. package/dist/lib/darwin_aarch64/include/selva/db.h +1 -1
  3. package/dist/lib/darwin_aarch64/include/selva/fields.h +0 -2
  4. package/dist/lib/darwin_aarch64/libdeflate.dylib +0 -0
  5. package/dist/lib/darwin_aarch64/libjemalloc_selva.2.dylib +0 -0
  6. package/dist/lib/darwin_aarch64/libnode-v20.node +0 -0
  7. package/dist/lib/darwin_aarch64/libnode-v21.node +0 -0
  8. package/dist/lib/darwin_aarch64/libnode-v22.node +0 -0
  9. package/dist/lib/darwin_aarch64/libnode-v23.node +0 -0
  10. package/dist/lib/darwin_aarch64/libselva.dylib +0 -0
  11. package/dist/lib/linux_aarch64/include/selva/db.h +1 -1
  12. package/dist/lib/linux_aarch64/include/selva/fields.h +0 -2
  13. package/dist/lib/linux_aarch64/libnode-v20.node +0 -0
  14. package/dist/lib/linux_aarch64/libnode-v21.node +0 -0
  15. package/dist/lib/linux_aarch64/libnode-v22.node +0 -0
  16. package/dist/lib/linux_aarch64/libnode-v23.node +0 -0
  17. package/dist/lib/linux_aarch64/libselva.so +0 -0
  18. package/dist/lib/linux_x86_64/include/selva/db.h +1 -1
  19. package/dist/lib/linux_x86_64/include/selva/fields.h +0 -2
  20. package/dist/lib/linux_x86_64/libnode-v20.node +0 -0
  21. package/dist/lib/linux_x86_64/libnode-v21.node +0 -0
  22. package/dist/lib/linux_x86_64/libnode-v22.node +0 -0
  23. package/dist/lib/linux_x86_64/libnode-v23.node +0 -0
  24. package/dist/lib/linux_x86_64/libselva.so +0 -0
  25. package/dist/src/client/flushModify.js +5 -4
  26. package/dist/src/client/index.d.ts +3 -2
  27. package/dist/src/client/modify/binary.js +1 -1
  28. package/dist/src/client/modify/cardinality.js +1 -1
  29. package/dist/src/client/modify/references/appendEdgeRefs.js +3 -0
  30. package/dist/src/client/modify/references/edge.js +6 -0
  31. package/dist/src/client/modify/references/getEdgeSize.js +1 -1
  32. package/dist/src/client/modify/string.js +10 -4
  33. package/dist/src/client/modify/text.js +1 -9
  34. package/dist/src/client/modify/types.d.ts +1 -0
  35. package/dist/src/client/modify/types.js +1 -0
  36. package/dist/src/client/modify/upsert.js +33 -21
  37. package/dist/src/client/query/BasedDbQuery.js +1 -1
  38. package/dist/src/client/query/BasedIterable.d.ts +2 -2
  39. package/dist/src/client/query/BasedIterable.js +7 -1
  40. package/dist/src/client/query/debug.js +3 -2
  41. package/dist/src/client/query/display.js +1 -1
  42. package/dist/src/client/query/filter/createFixedFilterBuffer.js +1 -1
  43. package/dist/src/client/query/filter/createVariableFilterBuffer.js +1 -1
  44. package/dist/src/client/query/filter/parseFilterValue.js +1 -2
  45. package/dist/src/client/query/queryDef.js +2 -2
  46. package/dist/src/client/query/read/read.js +4 -14
  47. package/dist/src/client/query/registerQuery.js +1 -1
  48. package/dist/src/client/query/search/index.js +1 -1
  49. package/dist/src/client/query/toBuffer.js +11 -15
  50. package/dist/src/client/query/types.d.ts +7 -0
  51. package/dist/src/client/query/types.js +8 -0
  52. package/dist/src/client/query/validation.d.ts +1 -1
  53. package/dist/src/client/query/validation.js +4 -5
  54. package/dist/src/client/string.js +1 -3
  55. package/dist/src/index.d.ts +1 -0
  56. package/dist/src/index.js +5 -1
  57. package/dist/src/native.d.ts +1 -2
  58. package/dist/src/native.js +2 -5
  59. package/dist/src/server/csmt/draw-dot.d.ts +3 -1
  60. package/dist/src/server/csmt/draw-dot.js +7 -2
  61. package/dist/src/server/csmt/memebership-proof.d.ts +1 -1
  62. package/dist/src/server/csmt/tree-utils.d.ts +4 -4
  63. package/dist/src/server/csmt/tree.d.ts +1 -1
  64. package/dist/src/server/csmt/tree.js +1 -1
  65. package/dist/src/server/csmt/types.d.ts +10 -10
  66. package/dist/src/server/dbHash.js +1 -1
  67. package/dist/src/server/index.d.ts +6 -2
  68. package/dist/src/server/index.js +28 -13
  69. package/dist/src/server/migrate/worker.js +11 -0
  70. package/dist/src/server/save.js +3 -2
  71. package/dist/src/server/start.js +9 -5
  72. package/dist/src/utils.d.ts +0 -10
  73. package/dist/src/utils.js +0 -152
  74. package/package.json +3 -3
  75. package/dist/lib/darwin_aarch64/include/selva/xxhash64.h +0 -23
  76. package/dist/lib/linux_aarch64/include/selva/xxhash64.h +0 -23
  77. package/dist/lib/linux_x86_64/include/selva/xxhash64.h +0 -23
  78. package/dist/src/client/query/subscription/markers.d.ts +0 -10
  79. package/dist/src/client/query/subscription/markers.js +0 -213
  80. package/dist/src/client/query/subscription/run.d.ts +0 -5
  81. package/dist/src/client/query/subscription/run.js +0 -76
  82. package/dist/src/client/tmpBuffer.d.ts +0 -3
  83. package/dist/src/client/tmpBuffer.js +0 -20
Binary file
@@ -93,10 +93,11 @@ export const flushBuffer = (db) => {
93
93
  const lastIds = {};
94
94
  const data = ctx.getData(lastIds);
95
95
  const resCtx = ctx.ctx;
96
- const d = performance.now();
97
- flushPromise = db.hooks.flushModify(data).then(({ offsets }) => {
98
- // return time there
99
- db.writeTime += performance.now() - d;
96
+ // pass from flushModify
97
+ flushPromise = db.hooks
98
+ .flushModify(data)
99
+ .then(({ offsets, dbWriteTime }) => {
100
+ db.writeTime += dbWriteTime ?? 0;
100
101
  resCtx.offsets = offsets;
101
102
  for (const typeId in lastIds) {
102
103
  if (typeId in offsets) {
@@ -12,6 +12,7 @@ export type DbClientHooks = {
12
12
  setSchema(schema: StrictSchema, fromStart?: boolean, transformFns?: TransformFns): Promise<DbServer['schema']>;
13
13
  flushModify(buf: Uint8Array): Promise<{
14
14
  offsets: Record<number, number>;
15
+ dbWriteTime?: number;
15
16
  }>;
16
17
  getQueryBuf(buf: Uint8Array): Promise<Uint8Array>;
17
18
  subscribe(q: BasedDbQuery, onData: OnData, onError?: OnError): OnClose;
@@ -39,7 +40,7 @@ export declare class DbClient {
39
40
  isDraining: boolean;
40
41
  modifyCtx: ModifyCtx;
41
42
  maxModifySize: number;
42
- upserting: Map<string, {
43
+ upserting: Map<number, {
43
44
  o: Record<string, any>;
44
45
  p: Promise<number | ModifyRes>;
45
46
  }>;
@@ -54,7 +55,7 @@ export declare class DbClient {
54
55
  update(type: string, value: Record<string, any> & {
55
56
  id: number | ModifyRes;
56
57
  }, opts?: ModifyOpts): ModifyRes;
57
- update(value: any, opts?: ModifyOpts): ModifyRes;
58
+ update(value: Record<string, any>, opts?: ModifyOpts): ModifyRes;
58
59
  upsert(type: string, obj: Record<string, any>, opts?: ModifyOpts): Promise<any>;
59
60
  delete(type: string, id: number | ModifyRes): ModifyRes;
60
61
  expire(type: string, id: number | ModifyRes, seconds: number): void;
@@ -1,4 +1,4 @@
1
- import { ENCODER } from '../../utils.js';
1
+ import { ENCODER } from '@saulx/utils';
2
2
  import { UPDATE, RANGE_ERR, DELETE, } from './types.js';
3
3
  import { ModifyError } from './ModifyRes.js';
4
4
  import { setCursor } from './setCursor.js';
@@ -2,7 +2,7 @@ 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';
5
- import { ENCODER } from '../../utils.js';
5
+ import { ENCODER } from '@saulx/utils';
6
6
  export function writeHll(value, ctx, def, t, parentId, modifyOp) {
7
7
  if (!value) {
8
8
  return new ModifyError(t, value);
@@ -9,6 +9,9 @@ export function appendEdgeRefs(t, ctx, value) {
9
9
  }
10
10
  id = id.tmpId;
11
11
  }
12
+ else if (typeof id === 'object' && id !== null && id.id) {
13
+ id = id.id;
14
+ }
12
15
  else {
13
16
  return new ModifyError(t, value);
14
17
  }
@@ -106,8 +106,14 @@ export function writeEdges(t, ref, ctx) {
106
106
  // add null
107
107
  if (typeof value !== 'number') {
108
108
  if (value instanceof ModifyState) {
109
+ if (value.error) {
110
+ return value.error;
111
+ }
109
112
  value = value.tmpId;
110
113
  }
114
+ else if (typeof value === 'object' && value !== null && value.id) {
115
+ value = value.id;
116
+ }
111
117
  else {
112
118
  return new ModifyError(edge, value);
113
119
  }
@@ -1,5 +1,5 @@
1
1
  import { STRING, REFERENCE, REFERENCES } from '@based/schema/def';
2
- import { ENCODER } from '../../../utils.js';
2
+ import { ENCODER } from '@saulx/utils';
3
3
  export function getEdgeSize(t, ref) {
4
4
  let size = 0;
5
5
  for (const key in t.edges) {
@@ -1,5 +1,5 @@
1
1
  import { ENCODER } from '../../index.js';
2
- import { CREATE, UPDATE, RANGE_ERR, DELETE, } from './types.js';
2
+ import { CREATE, UPDATE, RANGE_ERR, DELETE, DELETE_TEXT_FIELD, } from './types.js';
3
3
  import { ModifyError } from './ModifyRes.js';
4
4
  import { setCursor } from './setCursor.js';
5
5
  import { write } from '../string.js';
@@ -8,13 +8,19 @@ 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 (value === '' || value === null) {
11
+ if (value === null || value === '') {
12
12
  if (modifyOp === UPDATE) {
13
- if (ctx.len + 11 /* SIZE.DEFAULT_CURSOR */ + 1 > ctx.max) {
13
+ if (ctx.len + 11 /* SIZE.DEFAULT_CURSOR */ + 2 > ctx.max) {
14
14
  return RANGE_ERR;
15
15
  }
16
16
  setCursor(ctx, def, t.prop, t.typeIndex, parentId, modifyOp);
17
- ctx.buf[ctx.len++] = DELETE;
17
+ if (lang === 0) {
18
+ ctx.buf[ctx.len++] = DELETE;
19
+ }
20
+ else {
21
+ ctx.buf[ctx.len++] = DELETE_TEXT_FIELD;
22
+ ctx.buf[ctx.len++] = lang;
23
+ }
18
24
  }
19
25
  }
20
26
  else {
@@ -13,7 +13,7 @@ export function writeText(value, ctx, def, t, res, parentId, modifyOp) {
13
13
  }
14
14
  return;
15
15
  }
16
- if ((value && typeof value !== 'object') || value === null) {
16
+ if ((value && typeof value !== 'object') || value === null || value === '') {
17
17
  let locale = res.locale;
18
18
  if (!locale) {
19
19
  // TODO: Add def lang option...
@@ -25,10 +25,6 @@ export function writeText(value, ctx, def, t, res, parentId, modifyOp) {
25
25
  if (!def.seperateTextSort.localeToIndex.has(locale)) {
26
26
  return new ModifyError(t, locale, 'Invalid locale');
27
27
  }
28
- if (value == null) {
29
- // @ts-ignore
30
- value = '';
31
- }
32
28
  const err = writeString(res.locale, value, ctx, def, t, res.tmpId, modifyOp);
33
29
  if (modifyOp === CREATE) {
34
30
  const index = t.prop * (def.localeSize + 1);
@@ -47,10 +43,6 @@ export function writeText(value, ctx, def, t, res, parentId, modifyOp) {
47
43
  return new ModifyError(t, lang, 'Invalid locale');
48
44
  }
49
45
  let s = value[lang];
50
- if (s == null) {
51
- // @ts-ignore
52
- s = '';
53
- }
54
46
  const err = writeString(langC[1], s, ctx, def, t, res.tmpId, modifyOp);
55
47
  if (err) {
56
48
  return err;
@@ -12,6 +12,7 @@ export declare const DECREMENT = 13;
12
12
  export declare const EXPIRE = 14;
13
13
  export declare const ADD_EMPTY_SORT_TEXT = 15;
14
14
  export declare const ADD_EMPTY_SORT = 7;
15
+ export declare const DELETE_TEXT_FIELD = 16;
15
16
  export declare const SWITCH_TYPE = 2;
16
17
  export declare const SWITCH_FIELD = 0;
17
18
  export declare const SWITCH_ID_CREATE = 9;
@@ -10,6 +10,7 @@ export const DECREMENT = 13;
10
10
  export const EXPIRE = 14;
11
11
  export const ADD_EMPTY_SORT_TEXT = 15;
12
12
  export const ADD_EMPTY_SORT = 7;
13
+ export const DELETE_TEXT_FIELD = 16;
13
14
  export const SWITCH_TYPE = 2;
14
15
  export const SWITCH_FIELD = 0;
15
16
  export const SWITCH_ID_CREATE = 9;
@@ -1,41 +1,53 @@
1
- import { ALIAS } from '@based/schema/def';
2
- export async function upsert(db, type, obj, opts) {
3
- const tree = db.schemaTypesParsed[type].tree;
4
- let q;
5
- let id = type;
1
+ import { deepMerge } from '@saulx/utils';
2
+ import { ALIAS, isPropDef } from '@based/schema/def';
3
+ const filterAliases = (obj, tree) => {
4
+ let aliases;
6
5
  for (const key in obj) {
7
- if (tree[key].typeIndex === ALIAS) {
8
- id += `${key}:${obj[key]};`;
9
- if (q) {
10
- q = q.or(key, '=', obj[key]);
6
+ const def = tree[key];
7
+ if (def === undefined) {
8
+ return;
9
+ }
10
+ if (isPropDef(def)) {
11
+ if (def.typeIndex === ALIAS) {
12
+ aliases ??= {};
13
+ aliases[key] = obj[key];
11
14
  }
12
- else {
13
- q = db.query(type).include('id').filter(key, '=', obj[key]);
15
+ }
16
+ else {
17
+ const nested = filterAliases(obj[key], def);
18
+ if (nested) {
19
+ aliases ??= {};
20
+ aliases[key] = nested;
14
21
  }
15
22
  }
16
23
  }
17
- if (!q) {
18
- // fix with promise
19
- throw new Error('no alias found for upsert operation');
20
- }
21
- if (db.upserting.has(id)) {
22
- const store = db.upserting.get(id);
23
- store.o = { ...store.o, ...obj };
24
+ return aliases;
25
+ };
26
+ export async function upsert(db, type, obj, opts) {
27
+ const tree = db.schemaTypesParsed[type].tree;
28
+ const aliases = filterAliases(obj, tree);
29
+ const q = db.query(type, aliases);
30
+ q.register();
31
+ if (db.upserting.has(q.id)) {
32
+ const store = db.upserting.get(q.id);
33
+ deepMerge(store.o, obj);
24
34
  return store.p;
25
35
  }
26
36
  const store = {
27
37
  o: obj,
28
38
  p: q.get().then((res) => {
29
- db.upserting.delete(id);
39
+ db.upserting.delete(q.id);
30
40
  if (res.length === 0) {
31
41
  return db.create(type, store.o, opts);
32
42
  }
33
43
  else {
34
- return db.update(type, res.toObject()[0].id, store.o, opts);
44
+ const obj = res.toObject();
45
+ const id = Array.isArray(obj) ? obj[0].id : obj.id;
46
+ return db.update(type, id, store.o, opts);
35
47
  }
36
48
  }),
37
49
  };
38
- db.upserting.set(id, store);
50
+ db.upserting.set(q.id, store);
39
51
  return store.p;
40
52
  }
41
53
  //# sourceMappingURL=upsert.js.map
@@ -11,7 +11,7 @@ import { langCodesMap } from '@based/schema';
11
11
  import { convertFilter } from './filter/convertFilter.js';
12
12
  import { validateLocale, validateRange } from './validation.js';
13
13
  import { DEF_RANGE_PROP_LIMIT } from './thresholds.js';
14
- import { concatUint8Arr } from '../../utils.js';
14
+ import { concatUint8Arr } from '@saulx/utils';
15
15
  export class QueryBranch {
16
16
  db;
17
17
  def;
@@ -1,6 +1,5 @@
1
1
  import { inspect } from 'node:util';
2
2
  import { QueryDef } from './types.js';
3
- import { Item } from './query.js';
4
3
  import { size, time, inspectData } from './display.js';
5
4
  export { time, size, inspectData };
6
5
  export declare class BasedQueryResponse {
@@ -10,11 +9,12 @@ export declare class BasedQueryResponse {
10
9
  end: number;
11
10
  id: number;
12
11
  constructor(id: number, def: QueryDef, result: Uint8Array, execTime: number, end?: number);
12
+ get version(): number;
13
13
  get size(): number;
14
14
  [inspect.custom](depth: number): string;
15
15
  debug(): this;
16
16
  node(index?: number): any;
17
- [Symbol.iterator](): Generator<Partial<Item>, void, unknown>;
17
+ [Symbol.iterator](): Generator<Partial<import("./query.js").Item>, void, unknown>;
18
18
  inspect(depth?: number, raw?: boolean): this;
19
19
  forEach(fn: (item: any, key: number) => void): void;
20
20
  map(fn: (item: any, key: number) => any): any[];
@@ -2,8 +2,11 @@ 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 '../../utils.js';
5
+ import { readFloatLE, readUint32 } from '@saulx/utils';
6
6
  export { time, size, inspectData };
7
+ const BITS_FOR_BYTE_LEN = 21;
8
+ const FACTOR = 2 ** BITS_FOR_BYTE_LEN;
9
+ const MASK_B = FACTOR - 1;
7
10
  export class BasedQueryResponse {
8
11
  result;
9
12
  def;
@@ -17,6 +20,9 @@ export class BasedQueryResponse {
17
20
  this.execTime = execTime;
18
21
  this.end = end;
19
22
  }
23
+ get version() {
24
+ return (this.checksum >>> 0) * FACTOR + (this.result.byteLength & MASK_B);
25
+ }
20
26
  get size() {
21
27
  return this.result.length;
22
28
  }
@@ -1,14 +1,15 @@
1
1
  import picocolors from 'picocolors';
2
2
  import { isPropDef, REVERSE_TYPE_INDEX_MAP } from '@based/schema/def';
3
3
  import { QueryDefType } from './types.js';
4
- import { concatUint8Arr } from '../../utils.js';
4
+ import { concatUint8Arr } from '@saulx/utils';
5
5
  export const debugQueryDef = (q, returnIt) => {
6
6
  const loggableObject = { type: 'bla', schema: null };
7
7
  const f = (a) => {
8
8
  if (a === null) {
9
9
  return null;
10
10
  }
11
- if (a instanceof Buffer) { // RFE is this necessary?
11
+ if (a instanceof Buffer) {
12
+ // RFE is this necessary?
12
13
  return new Uint8Array(a);
13
14
  }
14
15
  if (a instanceof Uint8Array) {
@@ -1,6 +1,6 @@
1
1
  import picocolors from 'picocolors';
2
2
  import { BINARY, CARDINALITY, NUMBER, REFERENCE, REFERENCES, STRING, TEXT, TIMESTAMP, } from '@based/schema/def';
3
- import { ENCODER } from '../../utils.js';
3
+ import { ENCODER } from '@saulx/utils';
4
4
  const decimals = (v) => ~~(v * 100) / 100;
5
5
  const sizeCalc = (size) => {
6
6
  if (size > 1e6) {
@@ -1,7 +1,7 @@
1
1
  import { BINARY, STRING, REFERENCES, } from '@based/schema/def';
2
2
  import { ALIGNMENT_NOT_SET, EQUAL, MODE_AND_FIXED, MODE_DEFAULT, MODE_OR_FIXED, } from './types.js';
3
3
  import { parseFilterValue } from './parseFilterValue.js';
4
- import { ENCODER } from '../../../utils.js';
4
+ import { ENCODER } from '@saulx/utils';
5
5
  export const writeFixed = (prop, buf, value, size, offset) => {
6
6
  if (prop.typeIndex === BINARY || prop.typeIndex === STRING) {
7
7
  if (typeof value === 'string') {
@@ -2,7 +2,7 @@ import { ALIAS, TEXT, VECTOR } from '@based/schema/def';
2
2
  import { EQUAL, EQUAL_CRC32, HAS, HAS_TO_LOWER_CASE, LIKE, MODE_DEFAULT_VAR, MODE_OR_VAR, getVectorFn, } from './types.js';
3
3
  import { createFixedFilterBuffer } from './createFixedFilterBuffer.js';
4
4
  import { crc32 } from '../../crc32.js';
5
- import { ENCODER, concatUint8Arr } from '../../../utils.js';
5
+ import { ENCODER, concatUint8Arr } from '@saulx/utils';
6
6
  const DEFAULT_SCORE = new Uint8Array(new Float32Array([0.5]).buffer);
7
7
  const parseValue = (value, prop, ctx, lang) => {
8
8
  let val = value;
@@ -1,7 +1,6 @@
1
1
  import { TIMESTAMP, ENUM, BOOLEAN, STRING, BINARY, TEXT, } from '@based/schema/def';
2
2
  import { crc32 } from '../../crc32.js';
3
- import { convertToTimestamp } from '@saulx/utils';
4
- import { ENCODER } from '../../../utils.js';
3
+ import { convertToTimestamp, ENCODER } from '@saulx/utils';
5
4
  // -------------------------------------------
6
5
  // conditions normal
7
6
  // field, [size 2]
@@ -21,7 +21,7 @@ const createEmptySharedDef = (skipValidation) => {
21
21
  },
22
22
  sort: null,
23
23
  references: new Map(),
24
- aggregation: null,
24
+ aggregation: 0 /* AggFlag.NONE */,
25
25
  };
26
26
  return q;
27
27
  };
@@ -51,7 +51,7 @@ export const createQueryDef = (db, type, target, skipValidation) => {
51
51
  q.range.limit = t.ids.length;
52
52
  }
53
53
  else if (t.alias) {
54
- t.resolvedAlias = validateAlias(t.alias, [], q);
54
+ t.resolvedAlias = validateAlias(q, t.alias);
55
55
  }
56
56
  else {
57
57
  q.range.limit = DEF_RANGE_PROP_LIMIT;
@@ -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 '../../../utils.js';
4
+ import { readDoubleLE, readFloatLE, readInt16, readInt32, readUint16, readUint32, } from '@saulx/utils';
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) => {
@@ -47,14 +47,7 @@ const getEmptyField = (p, item) => {
47
47
  else {
48
48
  for (; i < len; i++) {
49
49
  const field = path[i];
50
- if (i === len - 1) {
51
- if (!(field in select)) {
52
- select = select[field] = {};
53
- }
54
- }
55
- else {
56
- select = select[field] ?? (select[field] = {});
57
- }
50
+ select = select[field] ?? (select[field] = {});
58
51
  }
59
52
  }
60
53
  return select;
@@ -132,7 +125,7 @@ const readMain = (q, result, offset, item) => {
132
125
  return i - offset;
133
126
  };
134
127
  const handleUndefinedProps = (id, q, item) => {
135
- if (q.aggregation == 0 /* AggFlag.NONE */ || q.aggregation == null) {
128
+ if (q.aggregation === 0 /* AggFlag.NONE */ || q.aggregation === null) {
136
129
  for (const k in q.include.propsRead) {
137
130
  if (q.include.propsRead[k] !== id) {
138
131
  // Only relevant for seperate props
@@ -145,8 +138,7 @@ const handleUndefinedProps = (id, q, item) => {
145
138
  const lang = q.include.langTextFields.get(prop.prop).codes;
146
139
  if (lang.has(0)) {
147
140
  for (const locale in q.schema.locales) {
148
- if (!lan[locale]) {
149
- // console.log('???', locale, prop.default)
141
+ if (lan[locale] == undefined) {
150
142
  lan[locale] = prop.default[locale] || '';
151
143
  }
152
144
  }
@@ -164,8 +156,6 @@ const handleUndefinedProps = (id, q, item) => {
164
156
  addField(prop, prop.default, item);
165
157
  }
166
158
  else if (prop.typeIndex === TEXT) {
167
- // q.lang
168
- // console.log('???')
169
159
  addField(prop, '', item);
170
160
  }
171
161
  else {
@@ -1,5 +1,5 @@
1
1
  import native from '../../native.js';
2
- import { concatUint8Arr } from '../../utils.js';
2
+ import { concatUint8Arr } from '@saulx/utils';
3
3
  import { defToBuffer } from './toBuffer.js';
4
4
  import { handleErrors } from './validation.js';
5
5
  export const registerQuery = (q) => {
@@ -2,7 +2,7 @@ import { langCodesMap } from '@based/schema';
2
2
  import { STRING, TEXT, VECTOR } from '@based/schema/def';
3
3
  import { getVectorFn } from '../filter/types.js';
4
4
  import { searchDoesNotExist, searchIncorrecQueryValue, searchIncorrectType, } from '../validation.js';
5
- import { ENCODER, concatUint8Arr } from '../../../utils.js';
5
+ import { ENCODER, concatUint8Arr } from '@saulx/utils';
6
6
  // vector
7
7
  export const vectorSearch = (def, q, field, opts) => {
8
8
  let prop = def.props[field];
@@ -1,10 +1,10 @@
1
1
  import { createSortBuffer } from './sort.js';
2
- import { QueryDefType } from './types.js';
2
+ import { QueryDefType, QueryType } 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
6
  import { createAggFlagBuffer } from './aggregation.js';
7
- import { ENCODER } from '../../utils.js';
7
+ import { ENCODER } from '@saulx/utils';
8
8
  const byteSize = (arr) => {
9
9
  return arr.reduce((a, b) => {
10
10
  return a + b.byteLength;
@@ -20,6 +20,7 @@ export function defToBuffer(db, def) {
20
20
  def.errors.push(...ref.errors);
21
21
  }
22
22
  });
23
+ const aggregation = createAggFlagBuffer(def.aggregation || 0 /* AggFlag.NONE */);
23
24
  let edges;
24
25
  let edgesSize = 0;
25
26
  if (def.edges) {
@@ -49,7 +50,7 @@ export function defToBuffer(db, def) {
49
50
  const aliasStr = ENCODER.encode(alias.value);
50
51
  const aliasLen = aliasStr.byteLength;
51
52
  const buf = new Uint8Array(8 + filterSize + aliasLen);
52
- buf[0] = 3;
53
+ buf[0] = QueryType.alias;
53
54
  buf[1] = def.schema.idUint8[0];
54
55
  buf[2] = def.schema.idUint8[1];
55
56
  buf[3] = alias.def.prop;
@@ -65,7 +66,7 @@ export function defToBuffer(db, def) {
65
66
  }
66
67
  else if (def.target.id) {
67
68
  const buf = new Uint8Array(9 + filterSize);
68
- buf[0] = 0;
69
+ buf[0] = QueryType.id;
69
70
  buf[1] = def.schema.idUint8[0];
70
71
  buf[2] = def.schema.idUint8[1];
71
72
  buf[3] = def.target.id;
@@ -86,10 +87,6 @@ export function defToBuffer(db, def) {
86
87
  sort = createSortBuffer(def.sort);
87
88
  sortSize = sort.byteLength;
88
89
  }
89
- let aggregation;
90
- if (def.aggregation) {
91
- aggregation = createAggFlagBuffer(def.aggregation);
92
- }
93
90
  if (def.target.ids) {
94
91
  if (!sortSize &&
95
92
  (def.range.offset || def.range.limit < def.target.ids.length)) {
@@ -97,7 +94,7 @@ export function defToBuffer(db, def) {
97
94
  }
98
95
  const idsSize = def.target.ids.length * 4;
99
96
  const buf = new Uint8Array(21 + idsSize + filterSize + sortSize + searchSize);
100
- buf[0] = 1;
97
+ buf[0] = QueryType.ids;
101
98
  buf[1] = def.schema.idUint8[0];
102
99
  buf[2] = def.schema.idUint8[1];
103
100
  buf[3] = idsSize;
@@ -134,7 +131,7 @@ export function defToBuffer(db, def) {
134
131
  }
135
132
  else {
136
133
  const buf = new Uint8Array(18 + filterSize + sortSize + searchSize);
137
- buf[0] = 2;
134
+ buf[0] = QueryType.default;
138
135
  buf[1] = def.schema.idUint8[0];
139
136
  buf[2] = def.schema.idUint8[1];
140
137
  buf[3] = def.range.offset;
@@ -160,9 +157,7 @@ export function defToBuffer(db, def) {
160
157
  if (searchSize) {
161
158
  buf.set(search, 17 + filterSize + sortSize);
162
159
  }
163
- if (aggregation) {
164
- buf.set(aggregation, 17 + filterSize + sortSize + searchSize);
165
- }
160
+ buf.set(aggregation, 17 + filterSize + sortSize + searchSize);
166
161
  result.push(buf);
167
162
  }
168
163
  }
@@ -175,8 +170,8 @@ export function defToBuffer(db, def) {
175
170
  }
176
171
  const sortSize = sort?.byteLength ?? 0;
177
172
  const modsSize = filterSize + sortSize;
178
- const meta = new Uint8Array(modsSize + 10 + 8);
179
- const sz = size + 7 + modsSize + 8;
173
+ const meta = new Uint8Array(modsSize + 10 + 8 + 1);
174
+ const sz = size + 7 + modsSize + 8 + 1;
180
175
  meta[0] = 254;
181
176
  meta[1] = sz;
182
177
  meta[2] = sz >>> 8;
@@ -201,6 +196,7 @@ export function defToBuffer(db, def) {
201
196
  meta[15 + modsSize] = def.schema.idUint8[0];
202
197
  meta[15 + 1 + modsSize] = def.schema.idUint8[1];
203
198
  meta[15 + 2 + modsSize] = def.target.propDef.prop;
199
+ meta[15 + 3 + modsSize] = aggregation[0];
204
200
  result.push(meta);
205
201
  }
206
202
  else if (def.type === QueryDefType.Reference) {
@@ -6,6 +6,13 @@ export type MainIncludes = {
6
6
  [start: string]: [number, PropDef];
7
7
  };
8
8
  export type IncludeTreeArr = (string | PropDef | IncludeTreeArr)[];
9
+ export declare enum QueryType {
10
+ id = 0,
11
+ ids = 1,
12
+ default = 2,
13
+ alias = 3,
14
+ aggregates = 4
15
+ }
9
16
  declare enum QueryDefType {
10
17
  Edge = 1,
11
18
  Reference = 2,
@@ -1,3 +1,11 @@
1
+ export var QueryType;
2
+ (function (QueryType) {
3
+ QueryType[QueryType["id"] = 0] = "id";
4
+ QueryType[QueryType["ids"] = 1] = "ids";
5
+ QueryType[QueryType["default"] = 2] = "default";
6
+ QueryType[QueryType["alias"] = 3] = "alias";
7
+ QueryType[QueryType["aggregates"] = 4] = "aggregates";
8
+ })(QueryType || (QueryType = {}));
1
9
  var QueryDefType;
2
10
  (function (QueryDefType) {
3
11
  QueryDefType[QueryDefType["Edge"] = 1] = "Edge";
@@ -71,7 +71,7 @@ export declare const includeDoesNotExist: (def: QueryDef, field: string) => void
71
71
  export declare const includeLangDoesNotExist: (def: QueryDef, field: string) => void;
72
72
  export declare const validateLocale: (def: QueryDef, lang: string) => void;
73
73
  export declare const validateSort: (def: QueryDef, field: string, orderInput?: "asc" | "desc") => QueryDef["sort"];
74
- export declare const validateAlias: (alias: QueryByAliasObj, path: string[], def: QueryDef) => {
74
+ export declare const validateAlias: (def: QueryDef, alias: QueryByAliasObj, path?: string) => {
75
75
  def: PropDef;
76
76
  value: string;
77
77
  };
@@ -31,7 +31,7 @@ export const ERR_SORT_LANG = 24;
31
31
  const messages = {
32
32
  [ERR_TARGET_INVAL_TYPE]: (p) => `Type "${p}" does not exist`,
33
33
  [ERR_TARGET_INVAL_ALIAS]: (p) => {
34
- return `Invalid alias prodived to query\n ${picocolors.italic(safeStringify(p, 100))}`;
34
+ return `Invalid alias provided to query\n ${picocolors.italic(safeStringify(p, 100))}`;
35
35
  },
36
36
  [ERR_TARGET_EXCEED_MAX_IDS]: (p) => `Exceeds max ids ${~~(p.length / 1e3)}k (max ${MAX_IDS_PER_QUERY / 1e3}k)`,
37
37
  [ERR_TARGET_INVAL_IDS]: (p) => `Ids should be of type array or Uint32Array with valid ids \n ${picocolors.italic(safeStringify(p, 100))}`,
@@ -305,18 +305,18 @@ export const validateSort = (def, field, orderInput) => {
305
305
  lang,
306
306
  };
307
307
  };
308
- export const validateAlias = (alias, path, def) => {
308
+ export const validateAlias = (def, alias, path) => {
309
309
  const schema = def.schema;
310
310
  for (const k in alias) {
311
311
  if (typeof alias[k] === 'string') {
312
- const p = path.join('.') + k;
312
+ const p = path ? `${path}.${k}` : k;
313
313
  const prop = schema.props[p];
314
314
  if (prop.typeIndex === ALIAS) {
315
315
  return { def: prop, value: alias[k] };
316
316
  }
317
317
  }
318
318
  else if (typeof alias[k] === 'object') {
319
- const propDef = validateAlias(alias[k], [...path, k], def);
319
+ const propDef = validateAlias(def, alias[k], path ? `${path}.${k}` : k);
320
320
  if (propDef) {
321
321
  return propDef;
322
322
  }
@@ -438,7 +438,6 @@ export const EMPTY_SCHEMA_DEF = {
438
438
  ...createEmptyDef('_error', { props: {} }, {}),
439
439
  buf: new Uint8Array([]),
440
440
  propNames: new Uint8Array([]),
441
- packed: new Uint8Array([]),
442
441
  idUint8: new Uint8Array([0, 0]),
443
442
  mainEmptyAllZeroes: true,
444
443
  };