@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.
- package/README.md +10 -10
- package/dist/lib/darwin_aarch64/include/selva/types.h +9 -18
- package/dist/lib/darwin_aarch64/libnode-v20.node +0 -0
- package/dist/lib/darwin_aarch64/libnode-v21.node +0 -0
- package/dist/lib/darwin_aarch64/libnode-v22.node +0 -0
- package/dist/lib/darwin_aarch64/libnode-v23.node +0 -0
- package/dist/lib/darwin_aarch64/libselva.dylib +0 -0
- package/dist/lib/linux_aarch64/include/selva/types.h +9 -18
- package/dist/lib/linux_aarch64/libjemalloc_selva.so.2 +0 -0
- package/dist/lib/linux_aarch64/libnode-v20.node +0 -0
- package/dist/lib/linux_aarch64/libnode-v21.node +0 -0
- package/dist/lib/linux_aarch64/libnode-v22.node +0 -0
- package/dist/lib/linux_aarch64/libnode-v23.node +0 -0
- package/dist/lib/linux_aarch64/libselva.so +0 -0
- package/dist/lib/linux_x86_64/include/selva/types.h +9 -18
- package/dist/lib/linux_x86_64/libjemalloc_selva.so.2 +0 -0
- package/dist/lib/linux_x86_64/libnode-v20.node +0 -0
- package/dist/lib/linux_x86_64/libnode-v21.node +0 -0
- package/dist/lib/linux_x86_64/libnode-v22.node +0 -0
- package/dist/lib/linux_x86_64/libnode-v23.node +0 -0
- package/dist/lib/linux_x86_64/libselva.so +0 -0
- package/dist/src/client/bitWise.d.ts +0 -1
- package/dist/src/client/bitWise.js +0 -10
- package/dist/src/client/{operations.d.ts → flushModify.d.ts} +4 -4
- package/dist/src/client/{operations.js → flushModify.js} +39 -15
- package/dist/src/client/index.d.ts +10 -6
- package/dist/src/client/index.js +12 -4
- package/dist/src/client/modify/ModifyRes.d.ts +2 -8
- package/dist/src/client/modify/ModifyRes.js +23 -29
- package/dist/src/client/modify/alias.d.ts +1 -1
- package/dist/src/client/modify/alias.js +5 -2
- package/dist/src/client/modify/binary.d.ts +1 -1
- package/dist/src/client/modify/binary.js +1 -1
- package/dist/src/client/modify/cardinality.d.ts +2 -2
- package/dist/src/client/modify/cardinality.js +7 -5
- package/dist/src/client/modify/create.js +10 -5
- package/dist/src/client/modify/delete.d.ts +3 -1
- package/dist/src/client/modify/delete.js +12 -5
- package/dist/src/client/modify/expire.js +1 -1
- package/dist/src/client/modify/fixed.js +11 -4
- package/dist/src/client/modify/references/appendRefs.d.ts +4 -0
- package/dist/src/client/modify/references/appendRefs.js +27 -0
- package/dist/src/client/modify/references/edge.d.ts +0 -1
- package/dist/src/client/modify/references/edge.js +191 -71
- package/dist/src/client/modify/references/getEdgeSize.d.ts +3 -0
- package/dist/src/client/modify/references/getEdgeSize.js +27 -0
- package/dist/src/client/modify/references/reference.js +40 -26
- package/dist/src/client/modify/references/references.js +18 -6
- package/dist/src/client/modify/string.js +2 -1
- package/dist/src/client/modify/types.d.ts +1 -0
- package/dist/src/client/modify/types.js +1 -0
- package/dist/src/client/modify/update.js +3 -4
- package/dist/src/client/modify/upsert.js +1 -0
- package/dist/src/client/modify/vector.js +0 -2
- package/dist/src/client/query/BasedDbQuery.d.ts +4 -3
- package/dist/src/client/query/BasedDbQuery.js +12 -3
- package/dist/src/client/query/aggregationFn.d.ts +3 -0
- package/dist/src/client/query/aggregationFn.js +9 -0
- package/dist/src/client/query/debug.js +2 -6
- package/dist/src/client/query/display.js +5 -2
- package/dist/src/client/query/filter/createFixedFilterBuffer.d.ts +2 -2
- package/dist/src/client/query/filter/createFixedFilterBuffer.js +34 -26
- package/dist/src/client/query/filter/createReferenceFilter.d.ts +1 -1
- package/dist/src/client/query/filter/createReferenceFilter.js +17 -7
- package/dist/src/client/query/filter/createVariableFilterBuffer.d.ts +1 -1
- package/dist/src/client/query/filter/createVariableFilterBuffer.js +45 -22
- package/dist/src/client/query/filter/filter.d.ts +1 -1
- package/dist/src/client/query/filter/filter.js +5 -0
- package/dist/src/client/query/filter/parseFilterValue.js +1 -1
- package/dist/src/client/query/filter/toBuffer.d.ts +2 -2
- package/dist/src/client/query/filter/toBuffer.js +16 -9
- package/dist/src/client/query/include/props.js +18 -1
- package/dist/src/client/query/include/toBuffer.d.ts +1 -1
- package/dist/src/client/query/include/toBuffer.js +22 -12
- package/dist/src/client/query/include/walk.js +2 -1
- package/dist/src/client/query/query.d.ts +1 -0
- package/dist/src/client/query/query.js +1 -0
- package/dist/src/client/query/queryDef.js +2 -1
- package/dist/src/client/query/read/read.d.ts +2 -1
- package/dist/src/client/query/read/read.js +95 -62
- package/dist/src/client/query/registerQuery.d.ts +1 -1
- package/dist/src/client/query/registerQuery.js +2 -1
- package/dist/src/client/query/search/index.d.ts +1 -1
- package/dist/src/client/query/search/index.js +56 -24
- package/dist/src/client/query/subscription/markers.js +2 -1
- package/dist/src/client/query/toBuffer.d.ts +1 -1
- package/dist/src/client/query/toBuffer.js +80 -32
- package/dist/src/client/query/types.d.ts +25 -3
- package/dist/src/client/query/types.js +6 -0
- package/dist/src/client/query/validation.js +1 -1
- package/dist/src/client/string.d.ts +1 -1
- package/dist/src/client/string.js +1 -2
- package/dist/src/client/xxHash64.d.ts +1 -1
- package/dist/src/index.d.ts +4 -3
- package/dist/src/index.js +20 -11
- package/dist/src/native.d.ts +8 -13
- package/dist/src/native.js +9 -31
- package/dist/src/server/dbHash.d.ts +5 -0
- package/dist/src/server/dbHash.js +27 -0
- package/dist/src/server/index.d.ts +10 -10
- package/dist/src/server/index.js +54 -35
- package/dist/src/server/migrate/index.js +1 -1
- package/dist/src/server/migrate/worker.js +2 -2
- package/dist/src/server/save.d.ts +1 -1
- package/dist/src/server/save.js +10 -10
- package/dist/src/server/start.js +4 -2
- package/dist/src/server/tree.d.ts +1 -1
- package/dist/src/server/tree.js +1 -1
- package/dist/src/utils.d.ts +3 -0
- package/dist/src/utils.js +23 -5
- package/package.json +3 -2
- package/dist/src/client/query/read/types.d.ts +0 -4
- 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 `
|
|
81
|
+
Define your schema using the `setSchema` method. Here is an example:
|
|
82
82
|
|
|
83
83
|
```typescript
|
|
84
|
-
await db.
|
|
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
|
|
281
|
-
return server.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -23,24 +23,15 @@ struct SelvaFieldsSchema;
|
|
|
23
23
|
|
|
24
24
|
enum SelvaFieldType {
|
|
25
25
|
SELVA_FIELD_TYPE_NULL = 0,
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -23,24 +23,15 @@ struct SelvaFieldsSchema;
|
|
|
23
23
|
|
|
24
24
|
enum SelvaFieldType {
|
|
25
25
|
SELVA_FIELD_TYPE_NULL = 0,
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
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:
|
|
14
|
+
payload: Uint8Array;
|
|
16
15
|
max: number;
|
|
17
|
-
buf:
|
|
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>):
|
|
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 =
|
|
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
|
|
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
|
|
62
|
+
data[i] = id;
|
|
63
|
+
data[i + 1] = id >>> 8;
|
|
62
64
|
i += 2;
|
|
63
|
-
data
|
|
64
|
-
i
|
|
65
|
-
data
|
|
66
|
-
i
|
|
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
|
-
|
|
76
|
+
view.setFloat64(i, key, true);
|
|
70
77
|
i += 8;
|
|
71
78
|
}
|
|
72
|
-
data
|
|
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(
|
|
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.
|
|
123
|
-
|
|
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=
|
|
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 './
|
|
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
|
-
|
|
13
|
-
flushModify(buf:
|
|
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
|
-
|
|
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
|
-
|
|
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):
|
|
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 {};
|
package/dist/src/client/index.js
CHANGED
|
@@ -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 './
|
|
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
|
|
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.
|
|
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
|
-
//
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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 `
|
|
33
|
+
return `Invalid value at '${prop.path.join('.')}'. Expected ${REVERSE_TYPE_INDEX_MAP[prop.typeIndex]}, received '${parseVal(val)}'`;
|
|
42
34
|
}
|
|
43
|
-
|
|
44
|
-
|
|
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
|
-
|
|
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
|
-
|
|
78
|
-
|
|
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(
|
|
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
|
|
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
|
-
|
|
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.
|
|
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:
|
|
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) {
|