@based/db 0.0.21 → 0.0.23

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 (113) hide show
  1. package/README.md +10 -10
  2. package/dist/lib/darwin_aarch64/include/selva/types.h +9 -18
  3. package/dist/lib/darwin_aarch64/libnode-v20.node +0 -0
  4. package/dist/lib/darwin_aarch64/libnode-v21.node +0 -0
  5. package/dist/lib/darwin_aarch64/libnode-v22.node +0 -0
  6. package/dist/lib/darwin_aarch64/libnode-v23.node +0 -0
  7. package/dist/lib/darwin_aarch64/libselva.dylib +0 -0
  8. package/dist/lib/linux_aarch64/include/selva/types.h +9 -18
  9. package/dist/lib/linux_aarch64/libjemalloc_selva.so.2 +0 -0
  10. package/dist/lib/linux_aarch64/libnode-v20.node +0 -0
  11. package/dist/lib/linux_aarch64/libnode-v21.node +0 -0
  12. package/dist/lib/linux_aarch64/libnode-v22.node +0 -0
  13. package/dist/lib/linux_aarch64/libnode-v23.node +0 -0
  14. package/dist/lib/linux_aarch64/libselva.so +0 -0
  15. package/dist/lib/linux_x86_64/include/selva/types.h +9 -18
  16. package/dist/lib/linux_x86_64/libjemalloc_selva.so.2 +0 -0
  17. package/dist/lib/linux_x86_64/libnode-v20.node +0 -0
  18. package/dist/lib/linux_x86_64/libnode-v21.node +0 -0
  19. package/dist/lib/linux_x86_64/libnode-v22.node +0 -0
  20. package/dist/lib/linux_x86_64/libnode-v23.node +0 -0
  21. package/dist/lib/linux_x86_64/libselva.so +0 -0
  22. package/dist/src/client/bitWise.d.ts +0 -1
  23. package/dist/src/client/bitWise.js +0 -10
  24. package/dist/src/client/{operations.d.ts → flushModify.d.ts} +4 -4
  25. package/dist/src/client/{operations.js → flushModify.js} +39 -15
  26. package/dist/src/client/index.d.ts +10 -6
  27. package/dist/src/client/index.js +12 -4
  28. package/dist/src/client/modify/ModifyRes.d.ts +2 -8
  29. package/dist/src/client/modify/ModifyRes.js +23 -29
  30. package/dist/src/client/modify/alias.d.ts +1 -1
  31. package/dist/src/client/modify/alias.js +5 -2
  32. package/dist/src/client/modify/binary.d.ts +1 -1
  33. package/dist/src/client/modify/binary.js +1 -1
  34. package/dist/src/client/modify/cardinality.d.ts +2 -2
  35. package/dist/src/client/modify/cardinality.js +7 -5
  36. package/dist/src/client/modify/create.js +10 -5
  37. package/dist/src/client/modify/delete.d.ts +3 -1
  38. package/dist/src/client/modify/delete.js +12 -5
  39. package/dist/src/client/modify/expire.js +1 -1
  40. package/dist/src/client/modify/fixed.js +11 -4
  41. package/dist/src/client/modify/references/appendRefs.d.ts +4 -0
  42. package/dist/src/client/modify/references/appendRefs.js +27 -0
  43. package/dist/src/client/modify/references/edge.d.ts +0 -1
  44. package/dist/src/client/modify/references/edge.js +191 -71
  45. package/dist/src/client/modify/references/getEdgeSize.d.ts +3 -0
  46. package/dist/src/client/modify/references/getEdgeSize.js +27 -0
  47. package/dist/src/client/modify/references/reference.js +40 -26
  48. package/dist/src/client/modify/references/references.js +18 -6
  49. package/dist/src/client/modify/string.js +2 -1
  50. package/dist/src/client/modify/types.d.ts +1 -0
  51. package/dist/src/client/modify/types.js +1 -0
  52. package/dist/src/client/modify/update.js +3 -4
  53. package/dist/src/client/modify/upsert.js +1 -0
  54. package/dist/src/client/modify/vector.js +0 -2
  55. package/dist/src/client/query/BasedDbQuery.d.ts +4 -3
  56. package/dist/src/client/query/BasedDbQuery.js +12 -3
  57. package/dist/src/client/query/aggregationFn.d.ts +3 -0
  58. package/dist/src/client/query/aggregationFn.js +9 -0
  59. package/dist/src/client/query/debug.js +2 -6
  60. package/dist/src/client/query/display.js +5 -2
  61. package/dist/src/client/query/filter/createFixedFilterBuffer.d.ts +2 -2
  62. package/dist/src/client/query/filter/createFixedFilterBuffer.js +34 -26
  63. package/dist/src/client/query/filter/createReferenceFilter.d.ts +1 -1
  64. package/dist/src/client/query/filter/createReferenceFilter.js +17 -7
  65. package/dist/src/client/query/filter/createVariableFilterBuffer.d.ts +1 -1
  66. package/dist/src/client/query/filter/createVariableFilterBuffer.js +45 -22
  67. package/dist/src/client/query/filter/filter.d.ts +1 -1
  68. package/dist/src/client/query/filter/filter.js +5 -0
  69. package/dist/src/client/query/filter/parseFilterValue.js +1 -1
  70. package/dist/src/client/query/filter/toBuffer.d.ts +2 -2
  71. package/dist/src/client/query/filter/toBuffer.js +16 -9
  72. package/dist/src/client/query/include/props.js +18 -1
  73. package/dist/src/client/query/include/toBuffer.d.ts +1 -1
  74. package/dist/src/client/query/include/toBuffer.js +22 -12
  75. package/dist/src/client/query/include/walk.js +2 -1
  76. package/dist/src/client/query/query.d.ts +1 -0
  77. package/dist/src/client/query/query.js +1 -0
  78. package/dist/src/client/query/queryDef.js +2 -1
  79. package/dist/src/client/query/read/read.d.ts +2 -1
  80. package/dist/src/client/query/read/read.js +95 -62
  81. package/dist/src/client/query/registerQuery.d.ts +1 -1
  82. package/dist/src/client/query/registerQuery.js +2 -1
  83. package/dist/src/client/query/search/index.d.ts +1 -1
  84. package/dist/src/client/query/search/index.js +56 -24
  85. package/dist/src/client/query/subscription/markers.js +2 -1
  86. package/dist/src/client/query/toBuffer.d.ts +1 -1
  87. package/dist/src/client/query/toBuffer.js +80 -32
  88. package/dist/src/client/query/types.d.ts +25 -3
  89. package/dist/src/client/query/types.js +6 -0
  90. package/dist/src/client/query/validation.js +1 -1
  91. package/dist/src/client/string.d.ts +1 -1
  92. package/dist/src/client/string.js +1 -2
  93. package/dist/src/client/xxHash64.d.ts +1 -1
  94. package/dist/src/index.d.ts +4 -3
  95. package/dist/src/index.js +20 -11
  96. package/dist/src/native.d.ts +8 -13
  97. package/dist/src/native.js +9 -31
  98. package/dist/src/server/dbHash.d.ts +5 -0
  99. package/dist/src/server/dbHash.js +27 -0
  100. package/dist/src/server/index.d.ts +10 -10
  101. package/dist/src/server/index.js +54 -35
  102. package/dist/src/server/migrate/index.js +1 -1
  103. package/dist/src/server/migrate/worker.js +2 -2
  104. package/dist/src/server/save.d.ts +1 -1
  105. package/dist/src/server/save.js +10 -10
  106. package/dist/src/server/start.js +4 -2
  107. package/dist/src/server/tree.d.ts +1 -1
  108. package/dist/src/server/tree.js +1 -1
  109. package/dist/src/utils.d.ts +3 -0
  110. package/dist/src/utils.js +23 -5
  111. package/package.json +3 -2
  112. package/dist/src/client/query/read/types.d.ts +0 -4
  113. package/dist/src/client/query/read/types.js +0 -5
@@ -2,9 +2,11 @@ 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
6
  const DEFAULT_SCORE = new Uint8Array(new Float32Array([0.5]).buffer);
6
7
  const parseValue = (value, prop, ctx, lang) => {
7
8
  let val = value;
9
+ // TODO should we do .normalize('NFKD') for all strings?
8
10
  if (ctx.operation === HAS_TO_LOWER_CASE && typeof val === 'string') {
9
11
  val = val.toLowerCase();
10
12
  }
@@ -28,25 +30,28 @@ const parseValue = (value, prop, ctx, lang) => {
28
30
  typeof value === 'string' ||
29
31
  !prop.separate ||
30
32
  ctx.operation !== EQUAL) {
31
- if (prop.typeIndex === TEXT) {
32
- // can be optmized replace when using uint8array
33
- val = Buffer.concat([Buffer.from(val), Buffer.from([lang])]);
33
+ if (typeof val === 'string') {
34
+ val = ENCODER.encode(val);
34
35
  }
35
- else {
36
- val = Buffer.from(val);
36
+ if (prop.typeIndex === TEXT) {
37
+ const tmp = new Uint8Array(val.byteLength + 1);
38
+ tmp.set(val);
39
+ tmp[tmp.byteLength - 1] = lang;
40
+ val = tmp;
37
41
  }
38
42
  }
39
43
  if (val?.BYTES_PER_ELEMENT > 1) {
40
44
  val = val.buffer;
41
45
  }
42
- if (!(val instanceof Buffer || val instanceof ArrayBuffer)) {
46
+ if (!(val instanceof Uint8Array || val instanceof ArrayBuffer)) {
43
47
  throw new Error(`Incorrect value for filter: ${prop.path}`);
44
48
  }
45
49
  if (ctx.operation === LIKE && prop.typeIndex !== VECTOR) {
46
- // @ts-ignore
47
- val = Buffer.concat([val, Buffer.from([ctx.opts.score ?? 2])]);
50
+ const tmp = new Uint8Array(val.byteLength + 1);
51
+ tmp.set((val instanceof ArrayBuffer) ? new Uint8Array(val) : val);
52
+ tmp[tmp.byteLength - 1] = ctx.opts.score ?? 2;
53
+ val = tmp;
48
54
  }
49
- // @ts-ignore
50
55
  return val;
51
56
  };
52
57
  export const createVariableFilterBuffer = (value, prop, ctx, lang) => {
@@ -59,11 +64,12 @@ export const createVariableFilterBuffer = (value, prop, ctx, lang) => {
59
64
  const x = [];
60
65
  for (const v of value) {
61
66
  const a = parseValue(v, prop, ctx, lang);
62
- const size = Buffer.allocUnsafe(2);
63
- size.writeUint16LE(a.byteLength);
67
+ const size = new Uint8Array(2);
68
+ size[0] = a.byteLength;
69
+ size[1] = a.byteLength >>> 8;
64
70
  x.push(size, a);
65
71
  }
66
- val = Buffer.concat(x);
72
+ val = concatUint8Arr(x);
67
73
  }
68
74
  else {
69
75
  const x = [];
@@ -85,21 +91,33 @@ export const createVariableFilterBuffer = (value, prop, ctx, lang) => {
85
91
  prop.typeIndex !== ALIAS &&
86
92
  prop.typeIndex !== VECTOR) {
87
93
  if (prop.typeIndex === TEXT) {
88
- buf = writeVarFilter(mode, Buffer.concat([
89
- Buffer.from(new Uint32Array([crc32(val.slice(0, -1)), val.byteLength - 1])
90
- .buffer),
91
- Buffer.from(new Uint8Array([val[val.length - 1]]).buffer),
92
- ]), ctx, prop, 0, 0);
94
+ const crc = crc32(val.slice(0, -1));
95
+ const len = val.byteLength - 1;
96
+ const v = new Uint8Array(9);
97
+ v[0] = crc;
98
+ v[1] = crc >>> 8;
99
+ v[2] = crc >>> 16;
100
+ v[3] = crc >>> 24;
101
+ v[4] = len;
102
+ v[5] = len >>> 8;
103
+ v[6] = len >>> 16;
104
+ v[7] = len >>> 24;
105
+ v[8] = val[val.length - 1];
106
+ buf = writeVarFilter(mode, v, ctx, prop, 0, 0);
93
107
  }
94
108
  else {
95
109
  buf = createFixedFilterBuffer(prop, 8, { operation: EQUAL_CRC32, type: ctx.type, opts: ctx.opts }, val, false);
96
110
  }
97
111
  }
98
112
  else {
113
+ if (val instanceof ArrayBuffer) {
114
+ val = new Uint8Array(val);
115
+ }
99
116
  buf = writeVarFilter(mode, val, ctx, prop, 0, 0);
100
117
  }
101
118
  }
102
119
  else {
120
+ // RFE is val always an Uint8Array?
103
121
  buf = writeVarFilter(mode, val, ctx, prop, prop.start, prop.len);
104
122
  }
105
123
  }
@@ -107,17 +125,22 @@ export const createVariableFilterBuffer = (value, prop, ctx, lang) => {
107
125
  };
108
126
  function writeVarFilter(mode, val, ctx, prop, start, len) {
109
127
  const size = val.byteLength;
110
- const buf = Buffer.allocUnsafe(12 + size);
128
+ const buf = new Uint8Array(12 + size);
111
129
  buf[0] = ctx.type;
112
130
  buf[1] = mode;
113
131
  buf[2] = prop.typeIndex;
114
- buf.writeUInt16LE(start, 3);
115
- buf.writeUint16LE(len, 5);
116
- buf.writeUint32LE(size, 7);
132
+ buf[3] = start;
133
+ buf[4] = start >>> 8;
134
+ buf[5] = len;
135
+ buf[6] = len >>> 8;
136
+ buf[7] = size;
137
+ buf[8] = size >>> 8;
138
+ buf[9] = size >>> 16;
139
+ buf[10] = size >>> 24;
117
140
  buf[11] = ctx.operation;
118
141
  // need to pas LANG FROM QUERY
119
142
  // need to set on 12 if TEXT
120
- buf.set(Buffer.from(val), 12);
143
+ buf.set(val, 12);
121
144
  return buf;
122
145
  }
123
146
  //# sourceMappingURL=createVariableFilterBuffer.js.map
@@ -6,4 +6,4 @@ import { DbClient } from '../../index.js';
6
6
  export { Operator, Filter };
7
7
  export declare const filterRaw: (db: DbClient, filter: Filter, schema: SchemaTypeDef, conditions: QueryDefFilter, def: QueryDef) => number;
8
8
  export declare const filter: (db: DbClient, def: QueryDef, filterAst: FilterAst, conditions: QueryDefFilter) => void;
9
- export declare const filterOr: (db: DbClient, def: QueryDef, filterAst: FilterAst[], conditions: QueryDefFilter) => QueryDefFilter;
9
+ export declare const filterOr: (db: DbClient, def: QueryDef, filterAst: FilterAst[], conditions: QueryDefFilter) => any;
@@ -100,6 +100,11 @@ export const filterOr = (db, def, filterAst, conditions) => {
100
100
  conditions: new Map(),
101
101
  };
102
102
  }
103
+ else {
104
+ const r = filterOr(db, def, filterAst, conditions.or);
105
+ conditions.size += r.size + 7;
106
+ return r;
107
+ }
103
108
  filter(db, def, filterAst, conditions.or);
104
109
  conditions.size += conditions.or.size;
105
110
  return conditions.or;
@@ -1,7 +1,7 @@
1
1
  import { TIMESTAMP, CREATED, UPDATED, ENUM, BOOLEAN, STRING, BINARY, TEXT, } from '@based/schema/def';
2
2
  import { crc32 } from '../../crc32.js';
3
3
  import { convertToTimestamp } from '../../timestamp.js';
4
- const ENCODER = new TextEncoder();
4
+ import { ENCODER } from '../../../utils.js';
5
5
  // -------------------------------------------
6
6
  // conditions normal
7
7
  // field, [size 2]
@@ -1,3 +1,3 @@
1
1
  import { QueryDefFilter } from '../types.js';
2
- export declare const fillConditionsBuffer: (result: Buffer, conditions: QueryDefFilter, offset: number) => number;
3
- export declare const filterToBuffer: (conditions: QueryDefFilter) => Buffer;
2
+ export declare const fillConditionsBuffer: (result: Uint8Array, conditions: QueryDefFilter, offset: number) => number;
3
+ export declare const filterToBuffer: (conditions: QueryDefFilter) => Uint8Array;
@@ -26,7 +26,8 @@ const writeConditions = (result, k, offset, conditions) => {
26
26
  result.set(condition, lastWritten);
27
27
  lastWritten += condition.byteLength;
28
28
  }
29
- result.writeUint16LE(conditionSize, sizeIndex);
29
+ result[sizeIndex] = conditionSize;
30
+ result[sizeIndex + 1] = conditionSize >>> 8;
30
31
  return lastWritten - offset;
31
32
  };
32
33
  export const fillConditionsBuffer = (result, conditions, offset) => {
@@ -48,12 +49,14 @@ export const fillConditionsBuffer = (result, conditions, offset) => {
48
49
  lastWritten++;
49
50
  result[lastWritten] = refField;
50
51
  lastWritten++;
51
- result.writeUint16LE(refConditions.schema.id, lastWritten);
52
+ result[lastWritten] = refConditions.schema.id;
53
+ result[lastWritten + 1] = refConditions.schema.id >>> 8;
52
54
  lastWritten += 2;
53
55
  const sizeIndex = lastWritten;
54
56
  lastWritten += 2;
55
57
  const size = fillConditionsBuffer(result, refConditions, lastWritten);
56
- result.writeUint16LE(size, sizeIndex);
58
+ result[sizeIndex] = size;
59
+ result[sizeIndex + 1] = size >>> 8;
57
60
  lastWritten += size;
58
61
  }
59
62
  }
@@ -65,13 +68,18 @@ export const fillConditionsBuffer = (result, conditions, offset) => {
65
68
  lastWritten += 2;
66
69
  const size = writeConditions(result, k, lastWritten, v);
67
70
  lastWritten += size;
68
- result.writeUint16LE(size, sizeIndex);
71
+ result[sizeIndex] = size;
72
+ result[sizeIndex + 1] = size >>> 8;
69
73
  });
70
74
  }
71
75
  if (conditions.or) {
72
76
  const size = fillConditionsBuffer(result, conditions.or, lastWritten);
73
- result.writeUint16LE(size, orJumpIndex);
74
- result.writeUint32LE(lastWritten, orJumpIndex + 2);
77
+ result[orJumpIndex] = size;
78
+ result[orJumpIndex + 1] = size >>> 8;
79
+ result[orJumpIndex + 2] = lastWritten;
80
+ result[orJumpIndex + 3] = lastWritten >>> 8;
81
+ result[orJumpIndex + 4] = lastWritten >>> 16;
82
+ result[orJumpIndex + 5] = lastWritten >>> 24;
75
83
  lastWritten += size;
76
84
  }
77
85
  if (conditions.exists) {
@@ -88,15 +96,14 @@ export const fillConditionsBuffer = (result, conditions, offset) => {
88
96
  }
89
97
  return lastWritten - offset;
90
98
  };
91
- // TODO convert to UINT8ARRAY
92
99
  export const filterToBuffer = (conditions) => {
93
100
  let result;
94
101
  if (conditions.size > 0) {
95
- result = Buffer.allocUnsafe(conditions.size);
102
+ result = new Uint8Array(conditions.size);
96
103
  fillConditionsBuffer(result, conditions, 0);
97
104
  }
98
105
  else {
99
- result = Buffer.alloc(0);
106
+ result = new Uint8Array(0);
100
107
  }
101
108
  return result;
102
109
  };
@@ -1,4 +1,5 @@
1
1
  import { REFERENCE, REFERENCES, TEXT, } from '@based/schema/def';
2
+ import { QueryDefType } from '../types.js';
2
3
  export const getAll = (props) => {
3
4
  const fields = [];
4
5
  for (const key in props) {
@@ -14,13 +15,29 @@ export const getAllRefs = (props, affix = '') => {
14
15
  for (const key in props) {
15
16
  const prop = props[key];
16
17
  if (prop.typeIndex === REFERENCE || prop.typeIndex === REFERENCES) {
17
- fields.push(prop.path.join('.') + affix);
18
+ const refPath = prop.path.join('.') + affix;
19
+ fields.push(refPath);
20
+ if (prop.edges) {
21
+ for (const edge in prop.edges) {
22
+ fields.push(refPath + '.' + edge);
23
+ }
24
+ }
18
25
  }
19
26
  }
20
27
  return fields;
21
28
  };
22
29
  export const includeField = (def, field) => {
23
30
  if (field === '*') {
31
+ if (def.type === QueryDefType.Reference ||
32
+ def.type === QueryDefType.References) {
33
+ const fields = [];
34
+ if (def.target.propDef.edges) {
35
+ for (const edge in def.target.propDef.edges) {
36
+ fields.push(edge);
37
+ }
38
+ }
39
+ includeFields(def, fields);
40
+ }
24
41
  includeFields(def, getAll(def.props));
25
42
  }
26
43
  else if (field === '**') {
@@ -1,3 +1,3 @@
1
1
  import { DbClient } from '../../index.js';
2
2
  import { QueryDef } from '../types.js';
3
- export declare const includeToBuffer: (db: DbClient, def: QueryDef) => Buffer[];
3
+ export declare const includeToBuffer: (db: DbClient, def: QueryDef) => Uint8Array[];
@@ -1,5 +1,6 @@
1
+ import { QueryDefType } from '../types.js';
1
2
  import { walkDefs } from './walk.js';
2
- const EMPTY_BUFFER = Buffer.alloc(0);
3
+ const EMPTY_BUFFER = new Uint8Array(0);
3
4
  export const includeToBuffer = (db, def) => {
4
5
  const result = [];
5
6
  if (!def.include.stringFields.size &&
@@ -17,8 +18,13 @@ export const includeToBuffer = (db, def) => {
17
18
  walkDefs(db, def, f);
18
19
  }
19
20
  }
21
+ // main
20
22
  if (def.include.main.len > 0) {
21
- if (def.include.main.len === def.schema.mainLen) {
23
+ // if (def.target.)
24
+ const len = def.type === QueryDefType.Edge
25
+ ? def.target.ref.edgeMainLen
26
+ : def.schema.mainLen;
27
+ if (def.include.main.len === len) {
22
28
  // GET ALL MAIN FIELDS
23
29
  let m = 0;
24
30
  for (const key in def.include.main.include) {
@@ -32,16 +38,19 @@ export const includeToBuffer = (db, def) => {
32
38
  else {
33
39
  // GET SOME MAIN FIELDS
34
40
  const size = Object.keys(def.include.main.include).length;
35
- mainBuffer = Buffer.allocUnsafe(size * 4 + 2);
36
- mainBuffer.writeUint16LE(def.include.main.len, 0);
41
+ mainBuffer = new Uint8Array(size * 4 + 2);
42
+ mainBuffer[0] = def.include.main.len;
43
+ mainBuffer[1] = def.include.main.len >>> 8;
37
44
  let i = 2;
38
45
  let m = 0;
39
46
  for (const key in def.include.main.include) {
40
47
  const v = def.include.main.include[key];
41
- mainBuffer.writeUint16LE(v[1].start, i);
48
+ mainBuffer[i] = v[1].start;
49
+ mainBuffer[i + 1] = v[1].start >>> 8;
42
50
  const len = v[1].len;
43
51
  v[0] = m;
44
- mainBuffer.writeUint16LE(len, i + 2);
52
+ mainBuffer[i + 2] = len;
53
+ mainBuffer[i + 3] = len >>> 8;
45
54
  i += 4;
46
55
  m += len;
47
56
  }
@@ -51,7 +60,7 @@ export const includeToBuffer = (db, def) => {
51
60
  for (const [prop, { codes, def: propDef },] of def.include.langTextFields.entries()) {
52
61
  def.include.propsRead[prop] = 0;
53
62
  if (codes.has(0)) {
54
- const b = Buffer.allocUnsafe(3);
63
+ const b = new Uint8Array(3);
55
64
  b[0] = prop;
56
65
  b[1] = propDef.typeIndex;
57
66
  b[2] = 0;
@@ -59,7 +68,7 @@ export const includeToBuffer = (db, def) => {
59
68
  }
60
69
  else {
61
70
  for (const code of codes) {
62
- const b = Buffer.allocUnsafe(3);
71
+ const b = new Uint8Array(3);
63
72
  b[0] = prop;
64
73
  b[1] = propDef.typeIndex;
65
74
  b[2] = code;
@@ -71,11 +80,12 @@ export const includeToBuffer = (db, def) => {
71
80
  const propSize = def.include.props.size ?? 0;
72
81
  if (mainBuffer) {
73
82
  len = mainBuffer.byteLength + 3 + propSize * 2;
74
- includeBuffer = Buffer.allocUnsafe(len);
83
+ includeBuffer = new Uint8Array(len);
75
84
  includeBuffer[0] = 0;
76
- includeBuffer.writeInt16LE(mainBuffer.byteLength, 1);
85
+ includeBuffer[1] = mainBuffer.byteLength;
86
+ includeBuffer[2] = mainBuffer.byteLength >>> 8;
77
87
  const offset = 3 + mainBuffer.byteLength;
78
- mainBuffer.copy(includeBuffer, 3);
88
+ includeBuffer.set(mainBuffer, 3);
79
89
  if (propSize) {
80
90
  let i = 0;
81
91
  for (const [prop, propDef] of def.include.props.entries()) {
@@ -86,7 +96,7 @@ export const includeToBuffer = (db, def) => {
86
96
  }
87
97
  }
88
98
  else if (propSize) {
89
- const buf = Buffer.allocUnsafe(propSize * 2);
99
+ const buf = new Uint8Array(propSize * 2);
90
100
  let i = 0;
91
101
  for (const [prop, propDef] of def.include.props.entries()) {
92
102
  buf[i] = prop;
@@ -35,7 +35,8 @@ export const walkDefs = (db, def, f) => {
35
35
  }
36
36
  }
37
37
  else {
38
- def.edges.include.props.set(edgeProp.prop, edgeProp);
38
+ // use include here
39
+ includeProp(def.edges, edgeProp);
39
40
  }
40
41
  return;
41
42
  }
@@ -8,3 +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';
@@ -8,4 +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
12
  //# sourceMappingURL=query.js.map
@@ -21,6 +21,7 @@ const createEmptySharedDef = (skipValidation) => {
21
21
  },
22
22
  sort: null,
23
23
  references: new Map(),
24
+ aggregation: null,
24
25
  };
25
26
  return q;
26
27
  };
@@ -30,7 +31,7 @@ export const createQueryDef = (db, type, target, skipValidation) => {
30
31
  const t = target;
31
32
  const q = queryDef;
32
33
  q.props = t.ref.edges;
33
- q.reverseProps = t.ref.reverseEdges;
34
+ // q.reverseProps = t.ref.reverseEdges
34
35
  q.type = type;
35
36
  q.target = t;
36
37
  return q;
@@ -4,5 +4,6 @@ export type Item = {
4
4
  } & {
5
5
  [key: string]: any;
6
6
  };
7
- export declare const readAllFields: (q: QueryDef, result: Uint8Array, offset: number, end: number, item: Item, id: number) => number;
7
+ export type AggItem = Partial<Item>;
8
+ export declare const readAllFields: (q: QueryDef, result: Uint8Array, offset: number, end: number, item: Item | AggItem, id: number) => number;
8
9
  export declare const resultToObject: (q: QueryDef, result: Uint8Array, end: number, offset?: number) => any;
@@ -1,8 +1,9 @@
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
+ import { QueryDefType } from '../types.js';
2
3
  import { read, readUtf8 } from '../../string.js';
3
4
  import { readDoubleLE, readFloatLE, readInt16, readInt32, readUint16, readUint32, } from '../../bitWise.js';
4
5
  import { inverseLangMap } from '@based/schema';
5
- import { READ_EDGE, READ_ID, READ_REFERENCE, READ_REFERENCES } from './types.js';
6
+ import { READ_EDGE, READ_ID, READ_REFERENCE, READ_REFERENCES, READ_AGGREGATION, CREATE_AGGREGATION, } from '../types.js';
6
7
  const addField = (p, value, item, defaultOnly = false, lang = 0) => {
7
8
  let i = p.__isEdge === true ? 1 : 0;
8
9
  // TODO OPTMIZE
@@ -112,11 +113,14 @@ const readMainValue = (prop, result, index, item) => {
112
113
  const readMain = (q, result, offset, item) => {
113
114
  const mainInclude = q.include.main;
114
115
  let i = offset;
115
- if (mainInclude.len === q.schema.mainLen) {
116
- for (const start in q.schema.main) {
117
- readMainValue(q.schema.main[start], result, Number(start) + i, item);
116
+ const isEdge = q.type === QueryDefType.Edge;
117
+ const main = isEdge ? q.target.ref.reverseMainEdges : q.schema.main;
118
+ const len = isEdge ? q.target.ref.edgeMainLen : q.schema.mainLen;
119
+ if (mainInclude.len === len) {
120
+ for (const start in main) {
121
+ readMainValue(main[start], result, Number(start) + i, item);
118
122
  }
119
- i += q.schema.mainLen;
123
+ i += len;
120
124
  }
121
125
  else {
122
126
  for (const k in mainInclude.include) {
@@ -128,33 +132,36 @@ const readMain = (q, result, offset, item) => {
128
132
  return i - offset;
129
133
  };
130
134
  const handleUndefinedProps = (id, q, item) => {
131
- for (const k in q.include.propsRead) {
132
- if (q.include.propsRead[k] !== id) {
133
- const prop = q.schema.reverseProps[k];
134
- if (prop.typeIndex === CARDINALITY) {
135
- addField(prop, 0, item);
136
- }
137
- else if (prop.typeIndex === TEXT && q.lang == 0) {
138
- const lan = getEmptyField(prop, item);
139
- const lang = q.include.langTextFields.get(prop.prop).codes;
140
- if (lang.has(0)) {
141
- for (const locale in q.schema.locales) {
142
- if (!lan[locale]) {
143
- lan[locale] = '';
135
+ if (q.aggregation != -999 /* AggFn.NONE */) {
136
+ for (const k in q.include.propsRead) {
137
+ if (q.include.propsRead[k] !== id) {
138
+ // Only relvant for seperate props
139
+ const prop = q.schema.reverseProps[k];
140
+ if (prop.typeIndex === CARDINALITY) {
141
+ addField(prop, 0, item);
142
+ }
143
+ else if (prop.typeIndex === TEXT && q.lang == 0) {
144
+ const lan = getEmptyField(prop, item);
145
+ const lang = q.include.langTextFields.get(prop.prop).codes;
146
+ if (lang.has(0)) {
147
+ for (const locale in q.schema.locales) {
148
+ if (!lan[locale]) {
149
+ lan[locale] = '';
150
+ }
144
151
  }
145
152
  }
146
- }
147
- else {
148
- for (const code of lang) {
149
- const locale = inverseLangMap.get(code);
150
- if (!lan[locale]) {
151
- lan[locale] = '';
153
+ else {
154
+ for (const code of lang) {
155
+ const locale = inverseLangMap.get(code);
156
+ if (!lan[locale]) {
157
+ lan[locale] = '';
158
+ }
152
159
  }
153
160
  }
154
161
  }
155
- }
156
- else {
157
- addField(prop, prop.typeIndex === JSON ? null : '', item);
162
+ else {
163
+ addField(prop, prop.typeIndex === JSON ? null : '', item);
164
+ }
158
165
  }
159
166
  }
160
167
  }
@@ -209,41 +216,40 @@ export const readAllFields = (q, result, offset, end, item, id) => {
209
216
  // ----------------
210
217
  }
211
218
  else {
212
- const edgeDef = q.edges.reverseProps[prop];
213
- const t = edgeDef.typeIndex;
214
- if (t === JSON) {
215
- i++;
216
- const size = readUint32(result, i);
217
- addField(edgeDef, global.JSON.parse(readUtf8(result, i + 6, size + i)), item);
218
- i += size + 4;
219
- }
220
- else if (t === BINARY) {
221
- i++;
222
- const size = readUint32(result, i);
223
- addField(edgeDef, result.subarray(i + 6, size + i), item);
224
- i += size + 4;
219
+ i++;
220
+ const target = 'ref' in q.edges.target && q.edges.target.ref;
221
+ if (prop === 0) {
222
+ i += readMain(q.edges, result, i, item);
223
+ // i += edgeDef.len
225
224
  }
226
- else if (t === STRING || t === ALIAS || t === ALIASES) {
227
- i++;
228
- const size = readUint32(result, i);
229
- if (size === 0) {
230
- addField(edgeDef, '', item);
225
+ else {
226
+ const edgeDef = target.reverseSeperateEdges[prop];
227
+ const t = edgeDef.typeIndex;
228
+ if (t === JSON) {
229
+ const size = readUint32(result, i);
230
+ addField(edgeDef, global.JSON.parse(readUtf8(result, i + 6, size + i)), item);
231
+ i += size + 4;
231
232
  }
232
- else {
233
- addField(edgeDef, read(result, i + 4, size), item);
233
+ else if (t === BINARY) {
234
+ const size = readUint32(result, i);
235
+ addField(edgeDef, result.subarray(i + 6, size + i), item);
236
+ i += size + 4;
237
+ }
238
+ else if (t === STRING || t === ALIAS || t === ALIASES) {
239
+ const size = readUint32(result, i);
240
+ if (size === 0) {
241
+ addField(edgeDef, '', item);
242
+ }
243
+ else {
244
+ addField(edgeDef, read(result, i + 4, size), item);
245
+ }
246
+ i += size + 4;
247
+ }
248
+ else if (t === CARDINALITY) {
249
+ const size = readUint32(result, i);
250
+ addField(edgeDef, readUint32(result, i + 4), item);
251
+ i += size + 4;
234
252
  }
235
- i += size + 4;
236
- }
237
- else if (t === CARDINALITY) {
238
- i++;
239
- const size = readUint32(result, i);
240
- addField(edgeDef, readUint32(result, i + 4), item);
241
- i += size + 4;
242
- }
243
- else {
244
- i++;
245
- readMainValue(edgeDef, result, i, item);
246
- i += edgeDef.len;
247
253
  }
248
254
  }
249
255
  }
@@ -282,6 +288,23 @@ export const readAllFields = (q, result, offset, end, item, id) => {
282
288
  addField(ref.target.propDef, refs, item);
283
289
  i += size + 4;
284
290
  }
291
+ else if (index === CREATE_AGGREGATION) {
292
+ i--;
293
+ result[i] = READ_AGGREGATION;
294
+ q.aggregation = -999 /* AggFn.NONE */;
295
+ return i - offset - 4 - (q.search ? 4 : 0);
296
+ }
297
+ 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
+ const propAgg = {
300
+ name: 'count',
301
+ path: ['count'],
302
+ typeIndex: UINT32,
303
+ };
304
+ const size = readUint32(result, i);
305
+ addField(propAgg, readUint32(result, i + 4), item);
306
+ i += 4 + size + 4;
307
+ }
285
308
  else if (index === 0) {
286
309
  i += readMain(q, result, i, item);
287
310
  }
@@ -359,9 +382,13 @@ export const readAllFields = (q, result, offset, end, item, id) => {
359
382
  handleUndefinedProps(id, q, item);
360
383
  return i - offset;
361
384
  };
385
+ let cnt = 0;
362
386
  export const resultToObject = (q, result, end, offset = 0) => {
363
387
  const len = readUint32(result, offset);
364
388
  if (len === 0) {
389
+ if ('id' in q.target || 'alias' in q.target) {
390
+ return null;
391
+ }
365
392
  return [];
366
393
  }
367
394
  let items = [];
@@ -369,9 +396,15 @@ export const resultToObject = (q, result, end, offset = 0) => {
369
396
  while (i < end) {
370
397
  const id = readUint32(result, i);
371
398
  i += 4;
372
- const item = {
373
- id,
374
- };
399
+ var item;
400
+ if (q.aggregation == -999 /* AggFn.NONE */) {
401
+ item = {};
402
+ }
403
+ else {
404
+ item = {
405
+ id,
406
+ };
407
+ }
375
408
  if (q.search) {
376
409
  item.$searchScore = readFloatLE(result, i);
377
410
  i += 4;
@@ -1,2 +1,2 @@
1
1
  import { BasedDbQuery } from './BasedDbQuery.js';
2
- export declare const registerQuery: (q: BasedDbQuery) => Buffer;
2
+ export declare const registerQuery: (q: BasedDbQuery) => Uint8Array;
@@ -1,10 +1,11 @@
1
1
  import native from '../../native.js';
2
+ import { concatUint8Arr } from '../../utils.js';
2
3
  import { defToBuffer } from './toBuffer.js';
3
4
  import { handleErrors } from './validation.js';
4
5
  export const registerQuery = (q) => {
5
6
  if (!q.id) {
6
7
  const b = defToBuffer(q.db, q.def);
7
- const buf = Buffer.concat(b);
8
+ const buf = concatUint8Arr(b);
8
9
  let id = native.crc32(buf);
9
10
  q.id = id;
10
11
  q.buffer = buf;