@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
package/README.md CHANGED
@@ -78,10 +78,10 @@ const db = new BasedDb({
78
78
 
79
79
  ## Schema Definition
80
80
 
81
- Define your schema using the `putSchema` method. Here is an example:
81
+ Define your schema using the `setSchema` method. Here is an example:
82
82
 
83
83
  ```typescript
84
- await db.putSchema({
84
+ await db.setSchema({
85
85
  types: {
86
86
  user: {
87
87
  name: 'string',
@@ -277,8 +277,8 @@ const server = new DbServer({
277
277
  await server.start({ clean: true })
278
278
 
279
279
  const hooks: DbClientHooks = {
280
- async putSchema(schema, fromStart, transformFns) {
281
- return server.putSchema(schema, fromStart, transformFns)
280
+ async setSchema(schema, fromStart, transformFns) {
281
+ return server.setSchema(schema, fromStart, transformFns)
282
282
  },
283
283
  async flushModify(buf) {
284
284
  const offsets = server.modify(buf)
@@ -292,7 +292,7 @@ const hooks: DbClientHooks = {
292
292
  const client1 = new DbClient({ hooks })
293
293
  const client2 = new DbClient({ hooks })
294
294
 
295
- await client1.putSchema({
295
+ await client1.setSchema({
296
296
  types: {
297
297
  user: {
298
298
  name: 'string',
@@ -322,7 +322,7 @@ await db.query('article').include('*', '_checksum')
322
322
  Use cardinality for unique value counts:
323
323
 
324
324
  ```typescript
325
- await db.putSchema({
325
+ await db.setSchema({
326
326
  types: {
327
327
  article: {
328
328
  myUniqueValuesCount: 'cardinality',
@@ -340,7 +340,7 @@ const myArticle = await db.create('article', {
340
340
  Handle boolean properties:
341
341
 
342
342
  ```typescript
343
- await db.putSchema({
343
+ await db.setSchema({
344
344
  types: {
345
345
  user: {
346
346
  props: {
@@ -368,7 +368,7 @@ deepEqual((await db.query('user').get()).toObject(), [
368
368
  Handle binary data:
369
369
 
370
370
  ```typescript
371
- await db.putSchema({
371
+ await db.setSchema({
372
372
  types: {
373
373
  user: {
374
374
  props: {
@@ -397,7 +397,7 @@ deepEqual((await db.query('user').get()).toObject(), [
397
397
  Perform analytics on data:
398
398
 
399
399
  ```typescript
400
- await db.putSchema({
400
+ await db.setSchema({
401
401
  types: {
402
402
  page: {
403
403
  name: 'string',
@@ -435,7 +435,7 @@ const page = await db.create('page', {
435
435
  Use aliases for properties:
436
436
 
437
437
  ```typescript
438
- await db.putSchema({
438
+ await db.setSchema({
439
439
  types: {
440
440
  user: {
441
441
  props: {
@@ -23,24 +23,15 @@ struct SelvaFieldsSchema;
23
23
 
24
24
  enum SelvaFieldType {
25
25
  SELVA_FIELD_TYPE_NULL = 0,
26
- SELVA_FIELD_TYPE_NUMBER = 4,
27
- SELVA_FIELD_TYPE_INT8 = 20,
28
- SELVA_FIELD_TYPE_UINT8 = 6,
29
- SELVA_FIELD_TYPE_INT16 = 21,
30
- SELVA_FIELD_TYPE_UINT16 = 22,
31
- SELVA_FIELD_TYPE_INT32 = 23,
32
- SELVA_FIELD_TYPE_UINT32 = 7,
33
- SELVA_FIELD_TYPE_BOOLEAN = 9,
34
- SELVA_FIELD_TYPE_ENUM = 10,
35
- SELVA_FIELD_TYPE_STRING = 11,
36
- SELVA_FIELD_TYPE_TEXT = 12,
37
- SELVA_FIELD_TYPE_REFERENCE = 13,
38
- SELVA_FIELD_TYPE_REFERENCES = 14,
39
- SELVA_FIELD_TYPE_WEAK_REFERENCE = 15,
40
- SELVA_FIELD_TYPE_WEAK_REFERENCES = 16,
41
- SELVA_FIELD_TYPE_MICRO_BUFFER = 17,
42
- SELVA_FIELD_TYPE_ALIAS = 18,
43
- SELVA_FIELD_TYPE_ALIASES = 19,
26
+ SELVA_FIELD_TYPE_MICRO_BUFFER = 1,
27
+ SELVA_FIELD_TYPE_STRING = 2,
28
+ SELVA_FIELD_TYPE_TEXT = 3,
29
+ SELVA_FIELD_TYPE_REFERENCE = 4,
30
+ SELVA_FIELD_TYPE_REFERENCES = 5,
31
+ SELVA_FIELD_TYPE_WEAK_REFERENCE = 6,
32
+ SELVA_FIELD_TYPE_WEAK_REFERENCES = 7,
33
+ SELVA_FIELD_TYPE_ALIAS = 8,
34
+ SELVA_FIELD_TYPE_ALIASES = 9,
44
35
  } __packed;
45
36
 
46
37
  struct EdgeFieldConstraint {
@@ -23,24 +23,15 @@ struct SelvaFieldsSchema;
23
23
 
24
24
  enum SelvaFieldType {
25
25
  SELVA_FIELD_TYPE_NULL = 0,
26
- SELVA_FIELD_TYPE_NUMBER = 4,
27
- SELVA_FIELD_TYPE_INT8 = 20,
28
- SELVA_FIELD_TYPE_UINT8 = 6,
29
- SELVA_FIELD_TYPE_INT16 = 21,
30
- SELVA_FIELD_TYPE_UINT16 = 22,
31
- SELVA_FIELD_TYPE_INT32 = 23,
32
- SELVA_FIELD_TYPE_UINT32 = 7,
33
- SELVA_FIELD_TYPE_BOOLEAN = 9,
34
- SELVA_FIELD_TYPE_ENUM = 10,
35
- SELVA_FIELD_TYPE_STRING = 11,
36
- SELVA_FIELD_TYPE_TEXT = 12,
37
- SELVA_FIELD_TYPE_REFERENCE = 13,
38
- SELVA_FIELD_TYPE_REFERENCES = 14,
39
- SELVA_FIELD_TYPE_WEAK_REFERENCE = 15,
40
- SELVA_FIELD_TYPE_WEAK_REFERENCES = 16,
41
- SELVA_FIELD_TYPE_MICRO_BUFFER = 17,
42
- SELVA_FIELD_TYPE_ALIAS = 18,
43
- SELVA_FIELD_TYPE_ALIASES = 19,
26
+ SELVA_FIELD_TYPE_MICRO_BUFFER = 1,
27
+ SELVA_FIELD_TYPE_STRING = 2,
28
+ SELVA_FIELD_TYPE_TEXT = 3,
29
+ SELVA_FIELD_TYPE_REFERENCE = 4,
30
+ SELVA_FIELD_TYPE_REFERENCES = 5,
31
+ SELVA_FIELD_TYPE_WEAK_REFERENCE = 6,
32
+ SELVA_FIELD_TYPE_WEAK_REFERENCES = 7,
33
+ SELVA_FIELD_TYPE_ALIAS = 8,
34
+ SELVA_FIELD_TYPE_ALIASES = 9,
44
35
  } __packed;
45
36
 
46
37
  struct EdgeFieldConstraint {
Binary file
@@ -23,24 +23,15 @@ struct SelvaFieldsSchema;
23
23
 
24
24
  enum SelvaFieldType {
25
25
  SELVA_FIELD_TYPE_NULL = 0,
26
- SELVA_FIELD_TYPE_NUMBER = 4,
27
- SELVA_FIELD_TYPE_INT8 = 20,
28
- SELVA_FIELD_TYPE_UINT8 = 6,
29
- SELVA_FIELD_TYPE_INT16 = 21,
30
- SELVA_FIELD_TYPE_UINT16 = 22,
31
- SELVA_FIELD_TYPE_INT32 = 23,
32
- SELVA_FIELD_TYPE_UINT32 = 7,
33
- SELVA_FIELD_TYPE_BOOLEAN = 9,
34
- SELVA_FIELD_TYPE_ENUM = 10,
35
- SELVA_FIELD_TYPE_STRING = 11,
36
- SELVA_FIELD_TYPE_TEXT = 12,
37
- SELVA_FIELD_TYPE_REFERENCE = 13,
38
- SELVA_FIELD_TYPE_REFERENCES = 14,
39
- SELVA_FIELD_TYPE_WEAK_REFERENCE = 15,
40
- SELVA_FIELD_TYPE_WEAK_REFERENCES = 16,
41
- SELVA_FIELD_TYPE_MICRO_BUFFER = 17,
42
- SELVA_FIELD_TYPE_ALIAS = 18,
43
- SELVA_FIELD_TYPE_ALIASES = 19,
26
+ SELVA_FIELD_TYPE_MICRO_BUFFER = 1,
27
+ SELVA_FIELD_TYPE_STRING = 2,
28
+ SELVA_FIELD_TYPE_TEXT = 3,
29
+ SELVA_FIELD_TYPE_REFERENCE = 4,
30
+ SELVA_FIELD_TYPE_REFERENCES = 5,
31
+ SELVA_FIELD_TYPE_WEAK_REFERENCE = 6,
32
+ SELVA_FIELD_TYPE_WEAK_REFERENCES = 7,
33
+ SELVA_FIELD_TYPE_ALIAS = 8,
34
+ SELVA_FIELD_TYPE_ALIASES = 9,
44
35
  } __packed;
45
36
 
46
37
  struct EdgeFieldConstraint {
Binary file
@@ -4,4 +4,3 @@ export declare const readUint32: (val: Uint8Array, offset: number) => number;
4
4
  export declare const readInt32: (val: Uint8Array, offset: number) => number;
5
5
  export declare const readInt16: (val: Uint8Array, offset: number) => number;
6
6
  export declare const readUint16: (val: Uint8Array, offset: number) => number;
7
- export declare const concatUint8Arr: (arrs: Uint8Array[]) => Uint8Array;
@@ -69,14 +69,4 @@ export const readInt16 = (val, offset) => {
69
69
  export const readUint16 = (val, offset) => {
70
70
  return (val[offset] | (val[offset + 1] << 8)) >>> 0;
71
71
  };
72
- export const concatUint8Arr = (arrs) => {
73
- const totalLength = arrs.reduce((total, uint8array) => total + uint8array.byteLength, 0);
74
- const result = new Uint8Array(totalLength);
75
- let offset = 0;
76
- arrs.forEach((u8) => {
77
- result.set(u8, offset);
78
- offset += u8.byteLength;
79
- });
80
- return result;
81
- };
82
72
  //# sourceMappingURL=bitWise.js.map
@@ -5,19 +5,19 @@ export declare class ModifyCtx {
5
5
  constructor(db: DbClient);
6
6
  len: number;
7
7
  id: number;
8
- lastMain: number;
9
8
  hasSortField: number;
10
9
  hasSortText: number;
11
10
  queue: Map<(payload: any) => void, ModifyState>;
12
11
  ctx: {
13
12
  offsets?: Record<number, number>;
14
13
  };
15
- payload: Buffer;
14
+ payload: Uint8Array;
16
15
  max: number;
17
- buf: Buffer;
16
+ buf: Uint8Array;
18
17
  field: number;
19
18
  prefix0: number;
20
19
  prefix1: number;
20
+ lastMain: number;
21
21
  mergeMain: (PropDef | any)[] | null;
22
22
  mergeMainSize: number;
23
23
  db: DbClient;
@@ -26,7 +26,7 @@ export declare class ModifyCtx {
26
26
  markNodeDirty(schema: SchemaTypeDef, nodeId: number): void;
27
27
  markTypeDirty(schema: SchemaTypeDef): void;
28
28
  updateMax(): void;
29
- getData(lastIds: Record<number, number>): Buffer;
29
+ getData(lastIds: Record<number, number>): Uint8Array;
30
30
  }
31
31
  export declare const flushBuffer: (db: DbClient) => Promise<void>;
32
32
  export declare const startDrain: (db: DbClient) => void;
@@ -3,12 +3,11 @@ export class ModifyCtx {
3
3
  constructor(db) {
4
4
  this.max = db.maxModifySize;
5
5
  this.db = db;
6
- this.buf = Buffer.allocUnsafe(db.maxModifySize);
6
+ this.buf = new Uint8Array(db.maxModifySize);
7
7
  }
8
8
  // default values
9
9
  len = 0;
10
10
  id = -1;
11
- lastMain = -1;
12
11
  hasSortField = -1;
13
12
  hasSortText = -1;
14
13
  queue = new Map();
@@ -19,6 +18,7 @@ export class ModifyCtx {
19
18
  field;
20
19
  prefix0 = -1;
21
20
  prefix1 = -1;
21
+ lastMain = -1;
22
22
  mergeMain;
23
23
  mergeMainSize;
24
24
  db;
@@ -53,23 +53,33 @@ export class ModifyCtx {
53
53
  const typesSize = this.dirtyTypes.size;
54
54
  const data = this.buf.subarray(0, this.len + 4 + 2 + typesSize * 10 + rangesSize * 8);
55
55
  let i = this.len;
56
- data.writeUint16LE(typesSize, i);
56
+ data[i] = typesSize;
57
+ data[i + 1] = typesSize >>> 8;
57
58
  i += 2;
58
59
  for (const [id, startId] of this.dirtyTypes) {
59
60
  const lastId = this.db.schemaTypesParsedById[id].lastId;
60
61
  lastIds[id] = lastId;
61
- data.writeUint16LE(id, i);
62
+ data[i] = id;
63
+ data[i + 1] = id >>> 8;
62
64
  i += 2;
63
- data.writeUint32LE(startId, i);
64
- i += 4;
65
- data.writeUint32LE(lastId, i);
66
- i += 4;
65
+ data[i++] = startId;
66
+ data[i++] = startId >>> 8;
67
+ data[i++] = startId >>> 16;
68
+ data[i++] = startId >>> 24;
69
+ data[i++] = lastId;
70
+ data[i++] = lastId >>> 8;
71
+ data[i++] = lastId >>> 16;
72
+ data[i++] = lastId >>> 24;
67
73
  }
74
+ const view = new DataView(data.buffer, data.byteOffset);
68
75
  for (let key of this.dirtyRanges) {
69
- data.writeDoubleLE(key, i);
76
+ view.setFloat64(i, key, true);
70
77
  i += 8;
71
78
  }
72
- data.writeUint32LE(this.len, i);
79
+ data[i++] = this.len;
80
+ data[i++] = this.len >>> 8;
81
+ data[i++] = this.len >>> 16;
82
+ data[i++] = this.len >>> 24;
73
83
  return data;
74
84
  }
75
85
  }
@@ -102,10 +112,11 @@ export const flushBuffer = (db) => {
102
112
  if (queue.size) {
103
113
  flushPromise.then(() => {
104
114
  for (const [resolve, res] of queue) {
105
- resolve(res.getId(offsets));
115
+ resolve(res.getId());
106
116
  }
107
117
  });
108
118
  }
119
+ db.hooks.flushReady();
109
120
  });
110
121
  ctx.dirtyTypes.clear();
111
122
  ctx.dirtyRanges.clear();
@@ -115,13 +126,26 @@ export const flushBuffer = (db) => {
115
126
  ctx.max = db.maxModifySize;
116
127
  ctx.ctx = {};
117
128
  }
129
+ else {
130
+ db.hooks.flushReady();
131
+ }
118
132
  db.isDraining = false;
119
133
  return flushPromise;
120
134
  };
121
135
  export const startDrain = (db) => {
122
- db.isDraining = true;
123
- process.nextTick(() => {
124
- flushBuffer(db);
136
+ db.hooks.flushIsReady = new Promise((resolve) => {
137
+ db.hooks.flushReady = resolve;
125
138
  });
139
+ db.isDraining = true;
140
+ if (db.hooks.flushTime === 0) {
141
+ process.nextTick(() => {
142
+ flushBuffer(db);
143
+ });
144
+ }
145
+ else {
146
+ setTimeout(() => {
147
+ flushBuffer(db);
148
+ }, db.hooks.flushTime);
149
+ }
126
150
  };
127
- //# sourceMappingURL=operations.js.map
151
+ //# sourceMappingURL=flushModify.js.map
@@ -1,7 +1,7 @@
1
1
  import { Schema, StrictSchema } from '@based/schema';
2
2
  import { CreateObj } from './modify/create.js';
3
3
  import { SchemaTypeDef } from '@based/schema/def';
4
- import { ModifyCtx } from './operations.js';
4
+ import { ModifyCtx } from './flushModify.js';
5
5
  import { SubscriptionMarkerMap, SubscriptionsMap, SubscriptionsToRun } from './query/subscription/index.js';
6
6
  import { BasedDbQuery, QueryByAliasObj } from './query/BasedDbQuery.js';
7
7
  import { ModifyRes } from './modify/ModifyRes.js';
@@ -9,11 +9,14 @@ import { DbServer } from '../server/index.js';
9
9
  import { TransformFns } from '../server/migrate/index.js';
10
10
  import { ModifyOpts } from './modify/types.js';
11
11
  export type DbClientHooks = {
12
- putSchema(schema: StrictSchema, fromStart?: boolean, transformFns?: TransformFns): Promise<DbServer['schema']>;
13
- flushModify(buf: Buffer): Promise<{
12
+ setSchema(schema: StrictSchema, fromStart?: boolean, transformFns?: TransformFns): Promise<DbServer['schema']>;
13
+ flushModify(buf: Uint8Array): Promise<{
14
14
  offsets: Record<number, number>;
15
15
  }>;
16
- getQueryBuf(buf: Buffer): Promise<Uint8Array>;
16
+ flushIsReady: Promise<any>;
17
+ flushReady: (v?: any) => void;
18
+ getQueryBuf(buf: Uint8Array): Promise<Uint8Array>;
19
+ flushTime: number;
17
20
  };
18
21
  type DbClientOpts = {
19
22
  hooks: DbClientHooks;
@@ -41,7 +44,7 @@ export declare class DbClient {
41
44
  subscriptionMarkers: SubscriptionMarkerMap;
42
45
  subscriptionsToRun: SubscriptionsToRun;
43
46
  schemaChecksum: number;
44
- putSchema(schema: Schema, fromStart?: boolean, transformFns?: TransformFns): Promise<StrictSchema>;
47
+ setSchema(schema: Schema, fromStart?: boolean, transformFns?: TransformFns): Promise<StrictSchema>;
45
48
  putLocalSchema(schema: any): StrictSchema & {
46
49
  lastId: number;
47
50
  };
@@ -55,10 +58,11 @@ export declare class DbClient {
55
58
  }, opts?: ModifyOpts): ModifyRes;
56
59
  update(value: any, opts?: ModifyOpts): ModifyRes;
57
60
  upsert(type: string, obj: Record<string, any>, opts?: ModifyOpts): Promise<any>;
58
- delete(type: string, id: number | ModifyRes): boolean;
61
+ delete(type: string, id: number | ModifyRes): ModifyRes;
59
62
  expire(type: string, id: number | ModifyRes, seconds: number): void;
60
63
  destroy(): void;
61
64
  stop(): void;
62
65
  drain(): Promise<number>;
66
+ isModified(): Promise<void>;
63
67
  }
64
68
  export {};
@@ -1,7 +1,7 @@
1
1
  import { parse } from '@based/schema';
2
2
  import { create } from './modify/create.js';
3
3
  import { updateTypeDefs, schemaToSelvaBuffer, } from '@based/schema/def';
4
- import { flushBuffer, ModifyCtx } from './operations.js';
4
+ import { flushBuffer, ModifyCtx, startDrain } from './flushModify.js';
5
5
  import { BasedDbQuery } from './query/BasedDbQuery.js';
6
6
  import { ModifyState } from './modify/ModifyRes.js';
7
7
  import { upsert } from './modify/upsert.js';
@@ -37,13 +37,13 @@ export class DbClient {
37
37
  subscriptionMarkers = {};
38
38
  subscriptionsToRun = [];
39
39
  schemaChecksum;
40
- async putSchema(schema, fromStart, transformFns) {
40
+ async setSchema(schema, fromStart, transformFns) {
41
41
  const checksum = hash(schema);
42
42
  if (checksum === this.schemaChecksum) {
43
43
  return this.schema;
44
44
  }
45
45
  const strictSchema = fromStart ? schema : parse(schema).schema;
46
- const remoteSchema = await this.hooks.putSchema(strictSchema, fromStart, transformFns);
46
+ const remoteSchema = await this.hooks.setSchema(strictSchema, fromStart, transformFns);
47
47
  this.schemaChecksum = checksum;
48
48
  return this.putLocalSchema(remoteSchema);
49
49
  }
@@ -59,7 +59,7 @@ export class DbClient {
59
59
  }
60
60
  }
61
61
  updateTypeDefs(this.schema, this.schemaTypesParsed, this.schemaTypesParsedById);
62
- // TODO should not need this, but it modifies the schema
62
+ // Adds bidrectional refs on defs
63
63
  schemaToSelvaBuffer(this.schemaTypesParsed);
64
64
  return this.schema;
65
65
  }
@@ -164,11 +164,19 @@ export class DbClient {
164
164
  stop() {
165
165
  this.modifyCtx.len = 0;
166
166
  }
167
+ // For more advanced / internal usage - use isModified instead for most cases
167
168
  async drain() {
168
169
  await flushBuffer(this);
169
170
  const t = this.writeTime;
170
171
  this.writeTime = 0;
171
172
  return t;
172
173
  }
174
+ async isModified() {
175
+ if (!this.isDraining) {
176
+ startDrain(this);
177
+ }
178
+ await this.hooks.flushIsReady;
179
+ return;
180
+ }
173
181
  }
174
182
  //# sourceMappingURL=index.js.map
@@ -1,5 +1,4 @@
1
1
  import { PropDef, PropDefEdge, SchemaPropTree } from '@based/schema/def';
2
- import { inspect } from 'node:util';
3
2
  import { SubscriptionMarkersCheck } from '../query/subscription/index.js';
4
3
  import { DbClient } from '../index.js';
5
4
  import { ModifyOpts } from './types.js';
@@ -8,11 +7,8 @@ export type ModifyRes = {
8
7
  tmpId: number;
9
8
  error?: Error;
10
9
  } & Promise<number>;
11
- export declare class ModifyError {
12
- #private;
10
+ export declare class ModifyError extends Error {
13
11
  constructor(prop: PropDef | PropDefEdge | SchemaPropTree, val: any, msg?: string);
14
- toString(): string;
15
- [inspect.custom](): string;
16
12
  }
17
13
  export declare class ModifyState {
18
14
  #private;
@@ -23,9 +19,7 @@ export declare class ModifyState {
23
19
  tmpId: number;
24
20
  error?: ModifyError;
25
21
  promises?: Promise<any>[];
26
- resolved?: boolean;
27
- [Symbol.toPrimitive](): number;
28
- getId(offsets: Record<number, number>): number;
22
+ getId(): number;
29
23
  then(resolve: any, reject: any): Promise<unknown>;
30
24
  catch(handler: any): this | Promise<unknown>;
31
25
  }
@@ -1,5 +1,4 @@
1
1
  import { isPropDef, REVERSE_TYPE_INDEX_MAP, } from '@based/schema/def';
2
- import { inspect } from 'node:util';
3
2
  import { langCodesMap } from '@based/schema';
4
3
  const MAGIC_KEY = Math.random().toString(36).substring(2);
5
4
  const MAGIC_REG = RegExp(`("${MAGIC_KEY}|${MAGIC_KEY}")`, 'g');
@@ -18,30 +17,28 @@ const walk = (val) => {
18
17
  };
19
18
  const parseVal = (val) => {
20
19
  if (typeof val === 'object' && val !== null) {
21
- return JSON.stringify(walk(val)).replace(MAGIC_REG, '');
20
+ const str = JSON.stringify(walk(val)).replace(MAGIC_REG, '');
21
+ val = str;
22
+ }
23
+ if (typeof val === 'string' && val.length > 35) {
24
+ return val.slice(0, 35) + `... (${val.length - 35} more characters)`;
22
25
  }
23
26
  return val;
24
27
  };
25
- export class ModifyError {
26
- constructor(prop, val, msg) {
27
- this.#prop = prop;
28
- this.#val = val;
29
- this.#msg = msg;
30
- }
31
- #msg;
32
- #prop;
33
- #val;
34
- toString() {
35
- if (isPropDef(this.#prop)) {
36
- if (this.#msg) {
37
- return `Invalid value at '${this.#prop.path.join('.')}'. Expected ${this.#msg} received '${parseVal(this.#val)}'`;
38
- }
39
- return `Invalid value at '${this.#prop.path.join('.')}'. Expected ${REVERSE_TYPE_INDEX_MAP[this.#prop.typeIndex]}, received '${parseVal(this.#val)}'`;
28
+ const parseErrorMsg = (prop, val, msg) => {
29
+ if (isPropDef(prop)) {
30
+ if (msg) {
31
+ return `Invalid value at '${prop.path.join('.')}'. Expected ${msg} received '${parseVal(val)}'`;
40
32
  }
41
- return `Unknown property '${this.#val}'. Expected one of: ${Object.keys(this.#prop).join(', ')}`;
33
+ return `Invalid value at '${prop.path.join('.')}'. Expected ${REVERSE_TYPE_INDEX_MAP[prop.typeIndex]}, received '${parseVal(val)}'`;
42
34
  }
43
- [inspect.custom]() {
44
- return this.toString();
35
+ return `Unknown property '${val}'. Expected one of: ${Object.keys(prop).join(', ')}`;
36
+ };
37
+ export class ModifyError extends Error {
38
+ constructor(prop, val, msg) {
39
+ super(parseErrorMsg(prop, val, msg));
40
+ const a = this.stack.split('\n');
41
+ this.stack = a[0] + '\n' + a.slice(6, -1).join('\n');
45
42
  }
46
43
  }
47
44
  export class ModifyState {
@@ -65,17 +62,14 @@ export class ModifyState {
65
62
  tmpId;
66
63
  error;
67
64
  promises;
68
- resolved;
69
- [Symbol.toPrimitive]() {
70
- return this.tmpId;
71
- }
72
- getId(offsets) {
73
- this.resolved = true;
65
+ getId() {
74
66
  if (this.update) {
75
67
  return this.tmpId;
76
68
  }
77
- const offset = offsets[this.#typeId] || 0;
78
- return this.tmpId + offset;
69
+ if ('offsets' in this.#ctx) {
70
+ const offset = this.#ctx.offsets[this.#typeId] || 0;
71
+ return this.tmpId + offset;
72
+ }
79
73
  }
80
74
  then(resolve, reject) {
81
75
  const promise = new Promise((resolve) => {
@@ -83,7 +77,7 @@ export class ModifyState {
83
77
  reject(new Error(this.error.toString()));
84
78
  }
85
79
  else if ('offsets' in this.#ctx) {
86
- resolve(this.getId(this.#ctx.offsets));
80
+ resolve(this.getId());
87
81
  }
88
82
  else {
89
83
  this.#buf.queue.set(resolve, this);
@@ -1,4 +1,4 @@
1
1
  import { ModifyCtx } from '../../index.js';
2
2
  import { SchemaTypeDef, PropDef } from '@based/schema/def';
3
3
  import { ModifyOp, ModifyErr } from './types.js';
4
- export declare function writeAlias(value: string | null | Buffer, ctx: ModifyCtx, def: SchemaTypeDef, t: PropDef, parentId: number, modifyOp: ModifyOp): ModifyErr;
4
+ export declare function writeAlias(value: string | null, ctx: ModifyCtx, def: SchemaTypeDef, t: PropDef, parentId: number, modifyOp: ModifyOp): ModifyErr;
@@ -1,3 +1,4 @@
1
+ import { ENCODER } from '../../index.js';
1
2
  import { CREATE, UPDATE, RANGE_ERR, DELETE, } from './types.js';
2
3
  import { ModifyError } from './ModifyRes.js';
3
4
  import { setCursor } from './setCursor.js';
@@ -13,7 +14,8 @@ export function writeAlias(value, ctx, def, t, parentId, modifyOp) {
13
14
  }
14
15
  }
15
16
  else {
16
- let size = Buffer.byteLength(value, 'utf8');
17
+ const valueBuf = ENCODER.encode(value);
18
+ let size = valueBuf.byteLength;
17
19
  if (ctx.len + 11 /* SIZE.DEFAULT_CURSOR */ + 5 + size > ctx.max) {
18
20
  // 5 compression size
19
21
  return RANGE_ERR;
@@ -28,7 +30,8 @@ export function writeAlias(value, ctx, def, t, parentId, modifyOp) {
28
30
  ctx.buf[ctx.len++] = size >>>= 8;
29
31
  ctx.buf[ctx.len++] = size >>>= 8;
30
32
  ctx.buf[ctx.len++] = size >>>= 8;
31
- ctx.len += ctx.buf.write(value, ctx.len, 'utf8');
33
+ ctx.buf.set(valueBuf, ctx.len);
34
+ ctx.len += valueBuf.byteLength;
32
35
  }
33
36
  }
34
37
  else if (value === null) {
@@ -2,5 +2,5 @@ import { ModifyCtx } from '../../index.js';
2
2
  import { PropDef, SchemaTypeDef } from '@based/schema/def';
3
3
  import { ModifyOp, ModifyErr } from './types.js';
4
4
  export declare function getBuffer(value: any): Uint8Array | undefined;
5
- export declare function writeBinaryRaw(value: Buffer, ctx: ModifyCtx): void;
5
+ export declare function writeBinaryRaw(value: Uint8Array, ctx: ModifyCtx): void;
6
6
  export declare function writeBinary(value: any, ctx: ModifyCtx, schema: SchemaTypeDef, t: PropDef, parentId: number, modifyOp: ModifyOp): ModifyErr;
@@ -1,8 +1,8 @@
1
+ import { ENCODER } from '../../utils.js';
1
2
  import { UPDATE, RANGE_ERR, DELETE, } from './types.js';
2
3
  import { ModifyError } from './ModifyRes.js';
3
4
  import { setCursor } from './setCursor.js';
4
5
  import native from '../../native.js';
5
- const ENCODER = new TextEncoder();
6
6
  export function getBuffer(value) {
7
7
  if (typeof value === 'object') {
8
8
  if (value instanceof Uint8Array) {