@based/db 0.0.14 → 0.0.16

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 (61) hide show
  1. package/dist/lib/darwin_aarch64/include/selva/hll.h +2 -1
  2. package/dist/lib/darwin_aarch64/libnode-v20.11.1.node +0 -0
  3. package/dist/lib/darwin_aarch64/libnode-v20.18.1.node +0 -0
  4. package/dist/lib/darwin_aarch64/libnode-v22.13.0.node +0 -0
  5. package/dist/lib/darwin_aarch64/libselva.dylib +0 -0
  6. package/dist/lib/darwin_aarch64/libxxhash.0.8.2.dylib +0 -0
  7. package/dist/lib/darwin_aarch64/libxxhash.0.dylib +0 -0
  8. package/dist/lib/darwin_aarch64/libxxhash.dylib +0 -0
  9. package/dist/lib/linux_aarch64/include/selva/hll.h +2 -1
  10. package/dist/lib/linux_aarch64/libnode-v20.11.1.node +0 -0
  11. package/dist/lib/linux_aarch64/libnode-v20.18.1.node +0 -0
  12. package/dist/lib/linux_aarch64/libnode-v22.13.0.node +0 -0
  13. package/dist/lib/linux_aarch64/libselva.so +0 -0
  14. package/dist/lib/linux_aarch64/libxxhash.so +0 -0
  15. package/dist/lib/linux_aarch64/libxxhash.so.0 +0 -0
  16. package/dist/lib/linux_x86_64/include/selva/hll.h +2 -1
  17. package/dist/lib/linux_x86_64/libnode-v20.11.1.node +0 -0
  18. package/dist/lib/linux_x86_64/libnode-v20.18.1.node +0 -0
  19. package/dist/lib/linux_x86_64/libnode-v22.13.0.node +0 -0
  20. package/dist/lib/linux_x86_64/libselva.so +0 -0
  21. package/dist/lib/linux_x86_64/libxxhash.so +0 -0
  22. package/dist/lib/linux_x86_64/libxxhash.so.0 +0 -0
  23. package/dist/src/client/bitWise.js +1 -0
  24. package/dist/src/client/modify/cardinality.js +8 -9
  25. package/dist/src/client/query/BasedDbQuery.d.ts +1 -1
  26. package/dist/src/client/query/BasedDbQuery.js +14 -7
  27. package/dist/src/client/query/display.js +11 -3
  28. package/dist/src/client/query/filter/FilterBranch.js +8 -3
  29. package/dist/src/client/query/filter/convertFilter.d.ts +4 -0
  30. package/dist/src/client/query/filter/convertFilter.js +56 -0
  31. package/dist/src/client/query/filter/filter.d.ts +1 -2
  32. package/dist/src/client/query/filter/filter.js +5 -56
  33. package/dist/src/client/query/filter/primitiveFilter.d.ts +2 -2
  34. package/dist/src/client/query/filter/primitiveFilter.js +5 -1
  35. package/dist/src/client/query/filter/types.d.ts +2 -1
  36. package/dist/src/client/query/filter/types.js +23 -12
  37. package/dist/src/client/query/read/read.js +21 -32
  38. package/dist/src/client/query/read/types.d.ts +4 -0
  39. package/dist/src/client/query/read/types.js +5 -0
  40. package/dist/src/client/query/validation.d.ts +7 -1
  41. package/dist/src/client/query/validation.js +129 -46
  42. package/dist/src/client/xxHash64.d.ts +1 -1
  43. package/dist/src/client/xxHash64.js +7 -2
  44. package/dist/src/index.d.ts +4 -1
  45. package/dist/src/index.js +16 -1
  46. package/dist/src/native.d.ts +1 -1
  47. package/dist/src/native.js +2 -2
  48. package/dist/src/server/schema/utils.d.ts +1 -0
  49. package/dist/src/server/schema/utils.js +18 -5
  50. package/package.json +1 -1
  51. package/dist/lib/darwin_aarch64/libnode-v22.8.0.node +0 -0
  52. package/dist/src/client/modify/create copy.d.ts +0 -5
  53. package/dist/src/client/modify/create copy.js +0 -112
  54. package/dist/src/client/modify/hll.d.ts +0 -4
  55. package/dist/src/client/modify/hll.js +0 -58
  56. package/dist/src/client/modify/remove.d.ts +0 -1
  57. package/dist/src/client/modify/remove.js +0 -39
  58. package/dist/src/client/modify/vector copy.d.ts +0 -4
  59. package/dist/src/client/modify/vector copy.js +0 -46
  60. package/dist/src/client/query/filter/operators.d.ts +0 -6
  61. package/dist/src/client/query/filter/operators.js +0 -98
@@ -1,7 +1,8 @@
1
- import { ALIAS, ALIASES, BINARY, BOOLEAN, ENUM, INT16, INT32, INT8, NUMBER, STRING, TEXT, TIMESTAMP, UINT16, UINT32, UINT8, VECTOR, JSON, } from '../../../server/schema/types.js';
1
+ import { ALIAS, ALIASES, BINARY, BOOLEAN, ENUM, INT16, INT32, INT8, NUMBER, STRING, TEXT, TIMESTAMP, UINT16, UINT32, UINT8, VECTOR, JSON, CARDINALITY, } from '../../../server/schema/types.js';
2
2
  import { read, readUtf8 } from '../../string.js';
3
3
  import { readDoubleLE, readFloatLE, readInt16, readInt32, readUint16, readUint32, } from '../../bitWise.js';
4
4
  import { inverseLangMap } from '@based/schema';
5
+ import { READ_EDGE, READ_ID, READ_REFERENCE, READ_REFERENCES } from './types.js';
5
6
  const addField = (p, value, item, defaultOnly = false, lang = 0) => {
6
7
  let i = p.__isEdge === true ? 1 : 0;
7
8
  // TODO OPTMIZE
@@ -58,19 +59,15 @@ const getEmptyField = (p, item) => {
58
59
  return select;
59
60
  };
60
61
  const readMainValue = (prop, result, index, item) => {
61
- // 1: timestamp, 4: number
62
62
  if (prop.typeIndex === TIMESTAMP || prop.typeIndex === NUMBER) {
63
63
  addField(prop, readDoubleLE(result, index), item);
64
64
  }
65
- // 5: uint32
66
65
  else if (prop.typeIndex === UINT32) {
67
66
  addField(prop, readUint32(result, index), item);
68
67
  }
69
- // 9: boolean
70
68
  else if (prop.typeIndex === BOOLEAN) {
71
69
  addField(prop, Boolean(result[index]), item);
72
70
  }
73
- // 10: Enum
74
71
  else if (prop.typeIndex === ENUM) {
75
72
  if (result[index] === 0) {
76
73
  addField(prop, undefined, item);
@@ -79,9 +76,7 @@ const readMainValue = (prop, result, index, item) => {
79
76
  addField(prop, prop.enum[result[index] - 1], item);
80
77
  }
81
78
  }
82
- // 11: string
83
79
  else if (prop.typeIndex === STRING) {
84
- // Also delete this default then (same as other string)
85
80
  const len = result[index];
86
81
  if (len !== 0) {
87
82
  const str = readUtf8(result, index + 1, len);
@@ -91,32 +86,25 @@ const readMainValue = (prop, result, index, item) => {
91
86
  addField(prop, '', item);
92
87
  }
93
88
  }
94
- // ?: json
95
89
  else if (prop.typeIndex === JSON) {
96
- addField(prop, global.JSON.parse(Buffer.from(result.subarray(index + 1, index + 1 + result[index])).toString()), item);
90
+ addField(prop, global.JSON.parse(readUtf8(result, index + 1, index + 1 + result[index])), item);
97
91
  }
98
- // 25: binary
99
92
  else if (prop.typeIndex === BINARY) {
100
93
  addField(prop, result.subarray(index + 1, index + 1 + result[index]), item);
101
94
  }
102
- // 18: int8
103
95
  else if (prop.typeIndex === INT8) {
104
96
  const signedVal = (result[index] << 24) >> 24;
105
97
  addField(prop, signedVal, item);
106
98
  }
107
- // 19: uint8
108
99
  else if (prop.typeIndex === UINT8) {
109
100
  addField(prop, result[index], item);
110
101
  }
111
- // 20: int16
112
102
  else if (prop.typeIndex === INT16) {
113
103
  addField(prop, readInt16(result, index), item);
114
104
  }
115
- // 21: uint16
116
105
  else if (prop.typeIndex === UINT16) {
117
106
  addField(prop, readUint16(result, index), item);
118
107
  }
119
- // 22: int32
120
108
  else if (prop.typeIndex === INT32) {
121
109
  addField(prop, readInt32(result, index), item);
122
110
  }
@@ -143,7 +131,10 @@ const handleUndefinedProps = (id, q, item) => {
143
131
  for (const k in q.include.propsRead) {
144
132
  if (q.include.propsRead[k] !== id) {
145
133
  const prop = q.schema.reverseProps[k];
146
- if (prop.typeIndex === TEXT && q.lang == 0) {
134
+ if (prop.typeIndex === CARDINALITY) {
135
+ addField(prop, 0, item);
136
+ }
137
+ else if (prop.typeIndex === TEXT && q.lang == 0) {
147
138
  const lan = getEmptyField(prop, item);
148
139
  const lang = q.include.langTextFields.get(prop.prop).codes;
149
140
  if (lang.has(0)) {
@@ -173,16 +164,13 @@ export const readAllFields = (q, result, offset, end, item, id) => {
173
164
  while (i < end) {
174
165
  const index = result[i];
175
166
  i++;
176
- // index:255 id
177
- if (index === 255) {
167
+ if (index === READ_ID) {
178
168
  handleUndefinedProps(id, q, item);
179
169
  return i - offset;
180
170
  }
181
- // index:252 edge
182
- if (index === 252) {
171
+ if (index === READ_EDGE) {
183
172
  let prop = result[i];
184
- // index:254 ref
185
- if (prop === 254) {
173
+ if (prop === READ_REFERENCE) {
186
174
  i++;
187
175
  const field = result[i];
188
176
  i++;
@@ -206,9 +194,8 @@ export const readAllFields = (q, result, offset, end, item, id) => {
206
194
  addField(ref.target.propDef, refItem, item);
207
195
  i += size - 5;
208
196
  }
209
- // index:253 refs
210
197
  }
211
- else if (prop === 253) {
198
+ else if (prop === READ_REFERENCES) {
212
199
  i++;
213
200
  const field = result[i];
214
201
  i++;
@@ -227,7 +214,7 @@ export const readAllFields = (q, result, offset, end, item, id) => {
227
214
  if (t === JSON) {
228
215
  i++;
229
216
  const size = readUint32(result, i);
230
- addField(edgeDef, global.JSON.parse(Buffer.from(result.subarray(i + 6, size + i)).toString()), item);
217
+ addField(edgeDef, global.JSON.parse(readUtf8(result, i + 6, size + i)), item);
231
218
  i += size + 4;
232
219
  }
233
220
  else if (t === BINARY) {
@@ -253,9 +240,8 @@ export const readAllFields = (q, result, offset, end, item, id) => {
253
240
  i += edgeDef.len;
254
241
  }
255
242
  }
256
- // index:254 ref
257
243
  }
258
- else if (index === 254) {
244
+ else if (index === READ_REFERENCE) {
259
245
  const field = result[i];
260
246
  i++;
261
247
  const size = readUint32(result, i);
@@ -278,9 +264,8 @@ export const readAllFields = (q, result, offset, end, item, id) => {
278
264
  addField(ref.target.propDef, refItem, item);
279
265
  i += size - 5;
280
266
  }
281
- // index:253 refs
282
267
  }
283
- else if (index === 253) {
268
+ else if (index === READ_REFERENCES) {
284
269
  const field = result[i];
285
270
  i++;
286
271
  const ref = q.references.get(field);
@@ -296,7 +281,13 @@ export const readAllFields = (q, result, offset, end, item, id) => {
296
281
  }
297
282
  else {
298
283
  const prop = q.schema.reverseProps[index];
299
- if (prop.typeIndex === JSON) {
284
+ if (prop.typeIndex === CARDINALITY) {
285
+ q.include.propsRead[index] = id;
286
+ const size = readUint32(result, i);
287
+ addField(prop, readUint32(result, i + 4), item);
288
+ i += size + 4;
289
+ }
290
+ else if (prop.typeIndex === JSON) {
300
291
  q.include.propsRead[index] = id;
301
292
  const size = readUint32(result, i);
302
293
  addField(prop, global.JSON.parse(Buffer.from(result.subarray(i + 6, i + size)).toString()), item);
@@ -329,7 +320,6 @@ export const readAllFields = (q, result, offset, end, item, id) => {
329
320
  addField(prop, read(result, i + 4, size), item);
330
321
  }
331
322
  else {
332
- // q.include.propsRead[index] = id
333
323
  addField(prop, read(result, i + 4, size), item, false, result[i + 4]);
334
324
  }
335
325
  }
@@ -360,7 +350,6 @@ export const readAllFields = (q, result, offset, end, item, id) => {
360
350
  }
361
351
  }
362
352
  }
363
- // to add defaults - may not optimal for performance
364
353
  handleUndefinedProps(id, q, item);
365
354
  return i - offset;
366
355
  };
@@ -0,0 +1,4 @@
1
+ export declare const READ_ID = 255;
2
+ export declare const READ_EDGE = 252;
3
+ export declare const READ_REFERENCES = 253;
4
+ export declare const READ_REFERENCE = 254;
@@ -0,0 +1,5 @@
1
+ export const READ_ID = 255;
2
+ export const READ_EDGE = 252;
3
+ export const READ_REFERENCES = 253;
4
+ export const READ_REFERENCE = 254;
5
+ //# sourceMappingURL=types.js.map
@@ -1,5 +1,6 @@
1
- import { PropDef, SchemaTypeDef } from '../../server/schema/types.js';
1
+ import { PropDef, PropDefEdge, SchemaTypeDef } from '../../server/schema/types.js';
2
2
  import { DbClient } from '../index.js';
3
+ import { Filter } from './query.js';
3
4
  import { QueryByAliasObj, QueryDef } from './types.js';
4
5
  export type QueryError = {
5
6
  code: number;
@@ -29,8 +30,13 @@ declare const messages: {
29
30
  7: (p: any) => string;
30
31
  12: (p: any) => string;
31
32
  9: (p: any) => string;
33
+ 8: (p: Filter) => string;
34
+ 11: (p: any) => string;
35
+ 10: (p: any) => string;
32
36
  };
33
37
  export type ErrorCode = keyof typeof messages;
38
+ export declare const isValidId: (id: number) => boolean;
39
+ export declare const validateFilter: (def: QueryDef, prop: PropDef | PropDefEdge, f: Filter) => boolean;
34
40
  export declare const validateType: (db: DbClient, def: QueryDef, type: string) => SchemaTypeDef;
35
41
  export declare const filterOperatorDoesNotExist: (def: QueryDef, field: string) => void;
36
42
  export declare const filterInvalidLang: (def: QueryDef, field: string) => void;
@@ -1,5 +1,7 @@
1
- import { ALIAS } from '../../server/schema/types.js';
2
- import { MAX_ID, MAX_IDS_PER_QUERY } from './thresholds.js';
1
+ import { ALIAS, BOOLEAN, REFERENCE, REFERENCES, STRING, TEXT, VECTOR, } from '../../server/schema/types.js';
2
+ import { propIsNumerical } from '../../server/schema/utils.js';
3
+ import { EQUAL, HAS, isNumerical, LIKE, operatorReverseMap, VECTOR_FNS, } from './filter/types.js';
4
+ import { MAX_ID, MAX_ID_VALUE, MAX_IDS_PER_QUERY, MIN_ID_VALUE, } from './thresholds.js';
3
5
  export const ERR_TARGET_INVAL_TYPE = 1;
4
6
  export const ERR_TARGET_INVAL_ALIAS = 2;
5
7
  export const ERR_TARGET_EXCEED_MAX_IDS = 3;
@@ -13,17 +15,20 @@ export const ERR_FILTER_INVALID_VAL = 10;
13
15
  export const ERR_FILTER_INVALID_OPTS = 11;
14
16
  export const ERR_FILTER_INVALID_LANG = 12;
15
17
  export const ERR_INCLUDE_INVALID_LANG = 13;
18
+ const safeStringify = (p) => {
19
+ var v;
20
+ try {
21
+ v = JSON.stringify(p).replace(/"/g, '');
22
+ }
23
+ catch (err) {
24
+ v = '';
25
+ }
26
+ return v;
27
+ };
16
28
  const messages = {
17
29
  [ERR_TARGET_INVAL_TYPE]: (p) => `Type "${p}" does not exist`,
18
30
  [ERR_TARGET_INVAL_ALIAS]: (p) => {
19
- var v;
20
- try {
21
- v = JSON.stringify(p).replace(/"/g, '');
22
- }
23
- catch (err) {
24
- v = '';
25
- }
26
- return `Invalid alias prodived to query "${v}"`;
31
+ return `Invalid alias prodived to query "${safeStringify(p)}"`;
27
32
  },
28
33
  [ERR_TARGET_EXCEED_MAX_IDS]: (p) => `Exceeds max ids ${~~(p.length / 1e3)}k (max ${MAX_IDS_PER_QUERY / 1e3}k)`,
29
34
  [ERR_TARGET_INVAL_IDS]: (p) => `Ids should be of type array or Uint32Array with valid ids`,
@@ -33,6 +38,118 @@ const messages = {
33
38
  [ERR_FILTER_ENOENT]: (p) => `Filter: field does not exist "${p}"`,
34
39
  [ERR_FILTER_INVALID_LANG]: (p) => `Filter: invalid lang "${p}"`,
35
40
  [ERR_FILTER_OP_ENOENT]: (p) => `Filter: invalid operator "${p}"`,
41
+ [ERR_FILTER_OP_FIELD]: (p) => `Cannot use operator "${operatorReverseMap[p[1].operation]}" on field "${p[0]}"`,
42
+ [ERR_FILTER_INVALID_OPTS]: (p) => {
43
+ return `Filter: Invalid opts "${safeStringify(p)}"`;
44
+ },
45
+ [ERR_FILTER_INVALID_VAL]: (p) => {
46
+ return `Filter: Invalid value ${p[0]} ${operatorReverseMap[p[1].operation]} "${safeStringify(p[2])}"`;
47
+ },
48
+ };
49
+ export const isValidId = (id) => {
50
+ if (typeof id != 'number') {
51
+ return false;
52
+ }
53
+ else if (id < MIN_ID_VALUE || id > MAX_ID_VALUE) {
54
+ return false;
55
+ }
56
+ return true;
57
+ };
58
+ export const validateFilter = (def, prop, f) => {
59
+ const t = prop.typeIndex;
60
+ const op = f[1].operation;
61
+ const value = f[2];
62
+ if (t === REFERENCES) {
63
+ if (op == LIKE) {
64
+ def.errors.push({
65
+ code: ERR_FILTER_OP_FIELD,
66
+ payload: f,
67
+ });
68
+ return true;
69
+ }
70
+ }
71
+ else if (t === REFERENCE) {
72
+ if (op != EQUAL) {
73
+ def.errors.push({
74
+ code: ERR_FILTER_OP_FIELD,
75
+ payload: f,
76
+ });
77
+ return true;
78
+ }
79
+ if (Array.isArray(value)) {
80
+ for (const v of value) {
81
+ if (!isValidId(v)) {
82
+ def.errors.push({
83
+ code: ERR_FILTER_INVALID_VAL,
84
+ payload: f,
85
+ });
86
+ return true;
87
+ }
88
+ }
89
+ }
90
+ else if (!isValidId(value)) {
91
+ def.errors.push({
92
+ code: ERR_FILTER_INVALID_VAL,
93
+ payload: f,
94
+ });
95
+ return true;
96
+ }
97
+ }
98
+ else if (t === VECTOR) {
99
+ if (isNumerical(op) || op === HAS) {
100
+ def.errors.push({
101
+ code: ERR_FILTER_OP_FIELD,
102
+ payload: f,
103
+ });
104
+ return true;
105
+ }
106
+ if (op === LIKE) {
107
+ const opts = f[1].opts;
108
+ if ((opts.fn && !VECTOR_FNS.includes(opts.fn)) ||
109
+ (opts.score != undefined && typeof opts.score !== 'number')) {
110
+ def.errors.push({
111
+ code: ERR_FILTER_INVALID_OPTS,
112
+ payload: f,
113
+ });
114
+ return true;
115
+ }
116
+ }
117
+ }
118
+ else if (t === TEXT || t === STRING) {
119
+ if (isNumerical(op)) {
120
+ def.errors.push({
121
+ code: ERR_FILTER_OP_FIELD,
122
+ payload: f,
123
+ });
124
+ return true;
125
+ }
126
+ if (op === LIKE) {
127
+ const opts = f[1].opts;
128
+ if (opts.score &&
129
+ (typeof opts.score !== 'number' || opts.score < 0 || opts.score > 255)) {
130
+ def.errors.push({
131
+ code: ERR_FILTER_INVALID_OPTS,
132
+ payload: f,
133
+ });
134
+ return true;
135
+ }
136
+ }
137
+ }
138
+ else if (propIsNumerical(prop) && op !== EQUAL && !isNumerical(op)) {
139
+ def.errors.push({
140
+ code: ERR_FILTER_OP_FIELD,
141
+ payload: f,
142
+ });
143
+ return true;
144
+ }
145
+ else if (t === BOOLEAN && op !== EQUAL) {
146
+ def.errors.push({
147
+ code: ERR_FILTER_OP_FIELD,
148
+ payload: f,
149
+ });
150
+ return true;
151
+ }
152
+ return false;
36
153
  };
37
154
  export const validateType = (db, def, type) => {
38
155
  const r = db.schemaTypesParsed[type];
@@ -155,8 +272,8 @@ export const handleErrors = (def) => {
155
272
  for (const err of def.errors) {
156
273
  name += ` ${messages[err.code](err.payload)}\n`;
157
274
  }
158
- const err = new Error(name);
159
- err.stack = '';
275
+ const err = new Error(`Query\n`);
276
+ err.stack = name;
160
277
  throw err;
161
278
  }
162
279
  };
@@ -193,40 +310,6 @@ export const EMPTY_SCHEMA_DEF = {
193
310
  stringProps: Buffer.from([]),
194
311
  stringPropsLoop: [],
195
312
  };
196
- // import { ALIAS, PropDef, PropDefEdge } from '../../server/schema/types.js'
197
- // import {
198
- // MAX_IDS_PER_QUERY,
199
- // MIN_ID_VALUE,
200
- // MAX_ID_VALUE,
201
- // MAX_BUFFER_SIZE,
202
- // } from './thresholds.js'
203
- // import { QueryByAliasObj, QueryDef } from './types.js'
204
- // import { DbClient } from '../index.js'
205
- // export const isValidId = (id: number): void => {
206
- // if (typeof id != 'number') {
207
- // throw new Error('Id has to be a number')
208
- // } else if (id < MIN_ID_VALUE || id > MAX_ID_VALUE) {
209
- // throw new Error(
210
- // `Invalid Id: The Id should range between ${MIN_ID_VALUE} and ${MAX_ID_VALUE}.)`,
211
- // )
212
- // }
213
- // }
214
- // export const isValidType = (
215
- // type: string,
216
- // schema: DbClient['schemaTypesParsed'],
217
- // ): void => {
218
- // if (!schema[type]) {
219
- // throw new Error(`Incorrect type provided to query "${type}"`)
220
- // }
221
- // }
222
- // export const isValidAlias = (def: QueryDef, id: QueryByAliasObj) => {
223
- // for (const key in id) {
224
- // const prop = def.schema.props[key]
225
- // if (!prop || prop.typeIndex !== ALIAS) {
226
- // throw new Error(`Incorrect alias provided to query "${key}"`)
227
- // }
228
- // }
229
- // }
230
313
  // export const checkMaxIdsPerQuery = (
231
314
  // ids: (number | QueryByAliasObj)[],
232
315
  // ): void => {
@@ -1 +1 @@
1
- export declare const xxHash64: (buf: Buffer) => any;
1
+ export declare const xxHash64: (buf: Buffer, target?: Buffer, index?: number) => Buffer;
@@ -1,5 +1,10 @@
1
1
  import native from '../native.js';
2
- export const xxHash64 = (buf) => {
3
- return native.xxHash64(buf);
2
+ export const xxHash64 = (buf, target, index) => {
3
+ if (!target) {
4
+ target = Buffer.allocUnsafe(8);
5
+ index = 0;
6
+ }
7
+ native.xxHash64(buf, target, index);
8
+ return target;
4
9
  };
5
10
  //# sourceMappingURL=xxHash64.js.map
@@ -7,12 +7,14 @@ export * from './client/modify/modify.js';
7
7
  export { compress, decompress };
8
8
  export { ModifyCtx };
9
9
  export { DbClient, DbServer };
10
+ export { xxHash64 } from './client/xxHash64.js';
10
11
  export declare class BasedDb {
12
+ #private;
11
13
  client: DbClient;
12
14
  server: DbServer;
13
15
  fileSystemPath: string;
14
16
  maxModifySize: number;
15
- constructor({ path, maxModifySize, }: {
17
+ constructor(opt: {
16
18
  path: string;
17
19
  maxModifySize?: number;
18
20
  });
@@ -31,4 +33,5 @@ export declare class BasedDb {
31
33
  save: DbServer['save'];
32
34
  migrateSchema: DbServer['migrateSchema'];
33
35
  destroy(): Promise<void>;
36
+ wipe(): Promise<void>;
34
37
  }
package/dist/src/index.js CHANGED
@@ -7,12 +7,18 @@ export * from './client/modify/modify.js';
7
7
  export { compress, decompress };
8
8
  export { ModifyCtx }; // TODO move this somewhere
9
9
  export { DbClient, DbServer };
10
+ export { xxHash64 } from './client/xxHash64.js';
10
11
  export class BasedDb {
11
12
  client;
12
13
  server;
13
14
  fileSystemPath;
14
15
  maxModifySize;
15
- constructor({ path, maxModifySize, }) {
16
+ constructor(opt) {
17
+ this.#init(opt);
18
+ }
19
+ #init({ path, maxModifySize }) {
20
+ this.fileSystemPath = path;
21
+ this.maxModifySize = maxModifySize;
16
22
  const server = new DbServer({
17
23
  path,
18
24
  maxModifySize,
@@ -91,5 +97,14 @@ export class BasedDb {
91
97
  this.client.destroy();
92
98
  return this.server.destroy();
93
99
  }
100
+ async wipe() {
101
+ const opts = {
102
+ maxModifySize: this.maxModifySize,
103
+ path: this.fileSystemPath,
104
+ };
105
+ await this.destroy();
106
+ this.#init(opts);
107
+ await this.start({ clean: true });
108
+ }
94
109
  }
95
110
  //# sourceMappingURL=index.js.map
@@ -24,6 +24,6 @@ declare const _default: {
24
24
  crc32: (buf: Buffer) => any;
25
25
  createSortIndex: (buf: Buffer, dbCtx: any) => any;
26
26
  destroySortIndex: (buf: Buffer, dbCtx: any) => any;
27
- xxHash64: (buf: Buffer) => any;
27
+ xxHash64: (buf: Buffer, target: Buffer, index: number) => any;
28
28
  };
29
29
  export default _default;
@@ -93,8 +93,8 @@ export default {
93
93
  destroySortIndex: (buf, dbCtx) => {
94
94
  return db.destroySortIndex(dbCtx, buf);
95
95
  },
96
- xxHash64: (buf) => {
97
- return db.xxHash64(buf);
96
+ xxHash64: (buf, target, index) => {
97
+ return db.xxHash64(buf, target, index);
98
98
  },
99
99
  };
100
100
  //# sourceMappingURL=native.js.map
@@ -2,5 +2,6 @@ import { DbClient } from '../../client/index.js';
2
2
  import { DbServer } from '../index.js';
3
3
  import { PropDef, PropDefEdge } from './types.js';
4
4
  export declare const propIsSigned: (prop: PropDef | PropDefEdge) => boolean;
5
+ export declare const propIsNumerical: (prop: PropDef | PropDefEdge) => boolean;
5
6
  export declare const genRootId: () => number;
6
7
  export declare const genId: (db: DbServer | DbClient) => number;
@@ -1,9 +1,22 @@
1
- import { INT16, INT32, INT64, INT8 } from './types.js';
1
+ import { INT16, INT32, INT64, INT8, UINT16, UINT32, UINT8, NUMBER, TIMESTAMP, } from './types.js';
2
2
  export const propIsSigned = (prop) => {
3
- if (prop.typeIndex === INT16 ||
4
- prop.typeIndex === INT32 ||
5
- prop.typeIndex === INT64 ||
6
- prop.typeIndex === INT8) {
3
+ const t = prop.typeIndex;
4
+ if (t === INT16 || t === INT32 || t === INT64 || t === INT8) {
5
+ return true;
6
+ }
7
+ return false;
8
+ };
9
+ export const propIsNumerical = (prop) => {
10
+ const t = prop.typeIndex;
11
+ if (t === INT16 ||
12
+ t === INT32 ||
13
+ t === INT64 ||
14
+ t === INT8 ||
15
+ t === UINT8 ||
16
+ t === UINT16 ||
17
+ t === UINT32 ||
18
+ t === NUMBER ||
19
+ t === TIMESTAMP) {
7
20
  return true;
8
21
  }
9
22
  return false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@based/db",
3
- "version": "0.0.14",
3
+ "version": "0.0.16",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "./dist/src/index.js",
@@ -1,5 +0,0 @@
1
- import { ModifyRes } from './ModifyRes.js';
2
- import { ModifyOpts } from './types.js';
3
- import { DbClient } from '../index.js';
4
- export type CreateObj = Record<string, any>;
5
- export declare function create(db: DbClient, type: string, obj: CreateObj, opts?: ModifyOpts): ModifyRes;
@@ -1,112 +0,0 @@
1
- import { MICRO_BUFFER } from '../../server/schema/schema.js';
2
- import { startDrain, flushBuffer } from '../operations.js';
3
- import { setCursor } from './setCursor.js';
4
- import { modify } from './modify.js';
5
- import { ModifyState } from './ModifyRes.js';
6
- import { CREATE, RANGE_ERR } from './types.js';
7
- import { writeFixedValue } from './fixed.js';
8
- import { getSubscriptionMarkers } from '../query/subscription/index.js';
9
- const appendCreate = (ctx, def, obj, res, unsafe) => {
10
- const len = ctx.len;
11
- let err = modify(ctx, res, obj, def, CREATE, def.tree, true, unsafe);
12
- if (err) {
13
- return err;
14
- }
15
- if (ctx.len === len || def.mainLen === 0) {
16
- if (ctx.len + 10 > ctx.max) {
17
- return RANGE_ERR;
18
- }
19
- setCursor(ctx, def, 0, MICRO_BUFFER, res.tmpId, CREATE);
20
- }
21
- if (def.createTs) {
22
- const createTs = Date.now();
23
- for (const prop of def.createTs) {
24
- if (ctx.lastMain === -1) {
25
- let mainLenU32 = def.mainLen;
26
- setCursor(ctx, def, prop.prop, MICRO_BUFFER, res.tmpId, CREATE);
27
- ctx.buf[ctx.len++] = CREATE;
28
- ctx.buf[ctx.len++] = mainLenU32;
29
- ctx.buf[ctx.len++] = mainLenU32 >>>= 8;
30
- ctx.buf[ctx.len++] = mainLenU32 >>>= 8;
31
- ctx.buf[ctx.len++] = mainLenU32 >>>= 8;
32
- ctx.lastMain = ctx.len;
33
- ctx.buf.fill(0, ctx.len, (ctx.len += def.mainLen));
34
- }
35
- err = writeFixedValue(ctx, createTs, prop, ctx.lastMain + prop.start);
36
- if (err) {
37
- return err;
38
- }
39
- }
40
- }
41
- // if touched lets see perf impact here
42
- if (def.hasStringProp) {
43
- if (ctx.hasStringField !== def.stringPropsSize - 1) {
44
- if (ctx.len + 3 > ctx.max) {
45
- return RANGE_ERR;
46
- }
47
- ctx.buf[ctx.len++] = 7;
48
- let sizepos = ctx.len;
49
- ctx.len += 2;
50
- for (const { prop } of def.stringPropsLoop) {
51
- if (def.stringPropsCurrent[prop] === 1) {
52
- if (ctx.len + 1 > ctx.max) {
53
- return RANGE_ERR;
54
- }
55
- ctx.buf[ctx.len++] = prop;
56
- }
57
- }
58
- let size = ctx.len - sizepos - 2;
59
- ctx.buf[sizepos++] = size;
60
- ctx.buf[sizepos] = size >>>= 8;
61
- }
62
- if (ctx.hasStringField !== -1) {
63
- def.stringProps.copy(def.stringPropsCurrent);
64
- }
65
- }
66
- };
67
- export function create(db, type, obj, opts) {
68
- const def = db.schemaTypesParsed[type];
69
- if (!def) {
70
- throw new Error(`Unknown type: ${type}. Did you mean on of: ${Object.keys(db.schemaTypesParsed).join(', ')}`);
71
- }
72
- let id;
73
- if ('id' in obj) {
74
- if (opts?.unsafe) {
75
- id = obj.id;
76
- }
77
- else {
78
- throw Error('create with "id" is not allowed');
79
- }
80
- }
81
- else {
82
- id = def.lastId + 1;
83
- }
84
- const ctx = db.modifyCtx;
85
- const res = new ModifyState(def.id, id, db, getSubscriptionMarkers(db, def.id, id, true), opts);
86
- const pos = ctx.len;
87
- const err = appendCreate(ctx, def, obj, res, opts?.unsafe);
88
- if (err) {
89
- ctx.prefix0 = -1; // force a new cursor
90
- ctx.len = pos;
91
- if (err === RANGE_ERR) {
92
- if (pos === 0) {
93
- throw new Error('out of range');
94
- }
95
- flushBuffer(db);
96
- return db.create(type, obj, opts);
97
- }
98
- res.error = err;
99
- // @ts-ignore
100
- return res;
101
- }
102
- if (!db.isDraining) {
103
- startDrain(db);
104
- }
105
- if (id > def.lastId) {
106
- def.lastId = id;
107
- def.total++;
108
- }
109
- // @ts-ignore
110
- return res;
111
- }
112
- //# sourceMappingURL=create%20copy.js.map