@based/db 0.0.24 → 0.0.26

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 (66) hide show
  1. package/dist/lib/darwin_aarch64/include/selva/db.h +37 -3
  2. package/dist/lib/darwin_aarch64/include/selva/fields.h +3 -3
  3. package/dist/lib/darwin_aarch64/include/selva/selva_string.h +2 -2
  4. package/dist/lib/darwin_aarch64/libdeflate.dylib +0 -0
  5. package/dist/lib/darwin_aarch64/libjemalloc_selva.2.dylib +0 -0
  6. package/dist/lib/darwin_aarch64/libnode-v20.node +0 -0
  7. package/dist/lib/darwin_aarch64/libnode-v21.node +0 -0
  8. package/dist/lib/darwin_aarch64/libnode-v22.node +0 -0
  9. package/dist/lib/darwin_aarch64/libnode-v23.node +0 -0
  10. package/dist/lib/darwin_aarch64/libselva.dylib +0 -0
  11. package/dist/lib/linux_aarch64/include/selva/db.h +37 -3
  12. package/dist/lib/linux_aarch64/include/selva/fields.h +3 -3
  13. package/dist/lib/linux_aarch64/include/selva/selva_string.h +2 -2
  14. package/dist/lib/linux_aarch64/libnode-v20.node +0 -0
  15. package/dist/lib/linux_aarch64/libnode-v21.node +0 -0
  16. package/dist/lib/linux_aarch64/libnode-v22.node +0 -0
  17. package/dist/lib/linux_aarch64/libnode-v23.node +0 -0
  18. package/dist/lib/linux_aarch64/libselva.so +0 -0
  19. package/dist/lib/linux_x86_64/include/selva/db.h +37 -3
  20. package/dist/lib/linux_x86_64/include/selva/fields.h +3 -3
  21. package/dist/lib/linux_x86_64/include/selva/selva_string.h +2 -2
  22. package/dist/lib/linux_x86_64/libnode-v20.node +0 -0
  23. package/dist/lib/linux_x86_64/libnode-v21.node +0 -0
  24. package/dist/lib/linux_x86_64/libnode-v22.node +0 -0
  25. package/dist/lib/linux_x86_64/libnode-v23.node +0 -0
  26. package/dist/lib/linux_x86_64/libselva.so +0 -0
  27. package/dist/src/client/flushModify.js +5 -1
  28. package/dist/src/client/index.d.ts +12 -6
  29. package/dist/src/client/index.js +33 -1
  30. package/dist/src/client/modify/create.js +17 -1
  31. package/dist/src/client/modify/fixed.js +53 -4
  32. package/dist/src/client/modify/modify.js +9 -4
  33. package/dist/src/client/modify/references/edge.js +3 -1
  34. package/dist/src/client/modify/upsert.js +1 -1
  35. package/dist/src/client/operations.d.ts +32 -0
  36. package/dist/src/client/operations.js +137 -0
  37. package/dist/src/client/query/BasedDbQuery.js +3 -0
  38. package/dist/src/client/query/BasedIterable.d.ts +1 -1
  39. package/dist/src/client/query/BasedIterable.js +18 -5
  40. package/dist/src/client/query/aggregation.d.ts +3 -0
  41. package/dist/src/client/query/aggregation.js +9 -0
  42. package/dist/src/client/query/display.js +12 -2
  43. package/dist/src/client/query/filter/toBuffer.js +2 -2
  44. package/dist/src/client/query/query.d.ts +1 -1
  45. package/dist/src/client/query/query.js +1 -1
  46. package/dist/src/client/query/queryDef.js +0 -1
  47. package/dist/src/client/query/read/read.js +10 -5
  48. package/dist/src/client/query/toBuffer.js +2 -2
  49. package/dist/src/client/query/types.d.ts +4 -3
  50. package/dist/src/client/query/validation.js +5 -1
  51. package/dist/src/client/string.js +1 -1
  52. package/dist/src/index.d.ts +4 -1
  53. package/dist/src/index.js +11 -2
  54. package/dist/src/native.d.ts +1 -1
  55. package/dist/src/native.js +2 -2
  56. package/dist/src/server/csmt/tree.js +2 -2
  57. package/dist/src/server/index.d.ts +6 -2
  58. package/dist/src/server/index.js +31 -5
  59. package/dist/src/server/save.d.ts +20 -1
  60. package/dist/src/server/save.js +66 -30
  61. package/dist/src/server/start.js +4 -9
  62. package/dist/src/server/tree.d.ts +1 -1
  63. package/dist/src/server/tree.js +1 -1
  64. package/dist/src/utils.d.ts +6 -0
  65. package/dist/src/utils.js +81 -9
  66. package/package.json +4 -4
@@ -71,6 +71,16 @@ struct SelvaTypeEntry *selva_get_type_by_index(const struct SelvaDb *db, node_ty
71
71
  SELVA_EXPORT
72
72
  struct SelvaTypeEntry *selva_get_type_by_node(const struct SelvaDb *db, struct SelvaNode *node) __attribute__((nonnull, pure));
73
73
 
74
+ SELVA_EXPORT
75
+ inline block_id_t selva_get_block_capacity(const struct SelvaTypeEntry *te)
76
+ #if !__zig
77
+ {
78
+ return te->blocks->block_capacity;
79
+ }
80
+ #else
81
+ ;
82
+ #endif
83
+
74
84
  /**
75
85
  * Get the node schema for type.
76
86
  */
@@ -298,7 +308,29 @@ size_t selva_node_count(const struct SelvaTypeEntry *type) __attribute__((nonnul
298
308
  * Get the node id of of node.
299
309
  */
300
310
  SELVA_EXPORT
301
- node_id_t selva_get_node_id(const struct SelvaNode *node) __attribute__((nonnull, pure));
311
+ __attribute__((nonnull, pure))
312
+ inline node_id_t selva_get_node_id(const struct SelvaNode *node)
313
+ #if !__zig
314
+ {
315
+ return node->node_id;
316
+ }
317
+ #else
318
+ ;
319
+ #endif
320
+
321
+ /**
322
+ * Get the type of of node.
323
+ */
324
+ SELVA_EXPORT
325
+ __attribute__((nonnull, pure))
326
+ inline node_type_t selva_get_node_type(const struct SelvaNode *node)
327
+ #if !__zig
328
+ {
329
+ return node->type;
330
+ }
331
+ #else
332
+ ;
333
+ #endif
302
334
 
303
335
  /**
304
336
  * \addtogroup node_hash
@@ -332,15 +364,17 @@ size_t selva_alias_count(const struct SelvaAliases *aliases);
332
364
  /**
333
365
  * Set new alias.
334
366
  * @param name is copied.
367
+ * @returns the previous node_id the alias was pointing to; Otherwise 0.
335
368
  */
336
369
  SELVA_EXPORT
337
- void selva_set_alias(struct SelvaAliases *aliases, node_id_t dest, const char *name_str, size_t name_len);
370
+ node_id_t selva_set_alias(struct SelvaAliases *aliases, node_id_t dest, const char *name_str, size_t name_len);
338
371
 
339
372
  /**
340
373
  * Delete alias by name.
374
+ * @return the destination node_id the alias was pointing to; 0 if SELVA_ENOENT.
341
375
  */
342
376
  SELVA_EXPORT
343
- int selva_del_alias_by_name(struct SelvaAliases *aliases, const char *name_str, size_t name_len);
377
+ node_id_t selva_del_alias_by_name(struct SelvaAliases *aliases, const char *name_str, size_t name_len);
344
378
 
345
379
  /**
346
380
  * Delete all aliases pointing to dest.
@@ -266,10 +266,10 @@ int selva_fields_get_text(
266
266
  size_t *len);
267
267
 
268
268
  SELVA_EXPORT
269
- struct SelvaNodeReference *selva_fields_get_reference(struct SelvaDb *db, struct SelvaNode *node, field_t field);
269
+ struct SelvaNodeReference *selva_fields_get_reference(struct SelvaDb *db, struct SelvaNode *node, const struct SelvaFieldSchema *fs);
270
270
 
271
271
  SELVA_EXPORT
272
- struct SelvaNodeReferences *selva_fields_get_references(struct SelvaDb *db, struct SelvaNode *node, field_t field);
272
+ struct SelvaNodeReferences *selva_fields_get_references(struct SelvaDb *db, struct SelvaNode *node, const struct SelvaFieldSchema *fs);
273
273
 
274
274
  SELVA_EXPORT
275
275
  struct SelvaNodeWeakReference selva_fields_get_weak_reference(struct SelvaDb *db, struct SelvaFields *fields, field_t field);
@@ -309,7 +309,7 @@ int selva_fields_del(struct SelvaDb *db, struct SelvaNode *node, const struct Se
309
309
  * Delete an edge from a references field.
310
310
  */
311
311
  SELVA_EXPORT
312
- int selva_fields_del_ref(struct SelvaDb *db, struct SelvaNode *node, field_t field, node_id_t dst_node_id);
312
+ int selva_fields_del_ref(struct SelvaDb *db, struct SelvaNode *node, const struct SelvaFieldSchema *fs, node_id_t dst_node_id);
313
313
 
314
314
  SELVA_EXPORT
315
315
  void selva_fields_clear_references(struct SelvaDb *db, struct SelvaNode *node, const struct SelvaFieldSchema *fs);
@@ -123,7 +123,7 @@ int selva_string_init_crc(struct selva_string *s, const char *str, size_t len, u
123
123
  [[nodiscard]]
124
124
  SELVA_EXPORT
125
125
  struct selva_string *selva_string_create(const char *str, size_t len, enum selva_string_flags flags)
126
- __attribute__((access(read_only, 1, 2)));
126
+ __attribute__((access(read_only, 1)));
127
127
 
128
128
  /**
129
129
  * Create a new string with a user provided CRC.
@@ -205,7 +205,7 @@ int selva_string_truncate(struct selva_string *s, size_t newlen)
205
205
  */
206
206
  SELVA_EXPORT
207
207
  int selva_string_append(struct selva_string *s, const char *str, size_t len)
208
- __attribute__((access(read_only, 2, 3)));
208
+ __attribute__((access(read_only, 2)));
209
209
 
210
210
  /**
211
211
  * Replace current value of the string s with str.
@@ -71,6 +71,16 @@ struct SelvaTypeEntry *selva_get_type_by_index(const struct SelvaDb *db, node_ty
71
71
  SELVA_EXPORT
72
72
  struct SelvaTypeEntry *selva_get_type_by_node(const struct SelvaDb *db, struct SelvaNode *node) __attribute__((nonnull, pure));
73
73
 
74
+ SELVA_EXPORT
75
+ inline block_id_t selva_get_block_capacity(const struct SelvaTypeEntry *te)
76
+ #if !__zig
77
+ {
78
+ return te->blocks->block_capacity;
79
+ }
80
+ #else
81
+ ;
82
+ #endif
83
+
74
84
  /**
75
85
  * Get the node schema for type.
76
86
  */
@@ -298,7 +308,29 @@ size_t selva_node_count(const struct SelvaTypeEntry *type) __attribute__((nonnul
298
308
  * Get the node id of of node.
299
309
  */
300
310
  SELVA_EXPORT
301
- node_id_t selva_get_node_id(const struct SelvaNode *node) __attribute__((nonnull, pure));
311
+ __attribute__((nonnull, pure))
312
+ inline node_id_t selva_get_node_id(const struct SelvaNode *node)
313
+ #if !__zig
314
+ {
315
+ return node->node_id;
316
+ }
317
+ #else
318
+ ;
319
+ #endif
320
+
321
+ /**
322
+ * Get the type of of node.
323
+ */
324
+ SELVA_EXPORT
325
+ __attribute__((nonnull, pure))
326
+ inline node_type_t selva_get_node_type(const struct SelvaNode *node)
327
+ #if !__zig
328
+ {
329
+ return node->type;
330
+ }
331
+ #else
332
+ ;
333
+ #endif
302
334
 
303
335
  /**
304
336
  * \addtogroup node_hash
@@ -332,15 +364,17 @@ size_t selva_alias_count(const struct SelvaAliases *aliases);
332
364
  /**
333
365
  * Set new alias.
334
366
  * @param name is copied.
367
+ * @returns the previous node_id the alias was pointing to; Otherwise 0.
335
368
  */
336
369
  SELVA_EXPORT
337
- void selva_set_alias(struct SelvaAliases *aliases, node_id_t dest, const char *name_str, size_t name_len);
370
+ node_id_t selva_set_alias(struct SelvaAliases *aliases, node_id_t dest, const char *name_str, size_t name_len);
338
371
 
339
372
  /**
340
373
  * Delete alias by name.
374
+ * @return the destination node_id the alias was pointing to; 0 if SELVA_ENOENT.
341
375
  */
342
376
  SELVA_EXPORT
343
- int selva_del_alias_by_name(struct SelvaAliases *aliases, const char *name_str, size_t name_len);
377
+ node_id_t selva_del_alias_by_name(struct SelvaAliases *aliases, const char *name_str, size_t name_len);
344
378
 
345
379
  /**
346
380
  * Delete all aliases pointing to dest.
@@ -266,10 +266,10 @@ int selva_fields_get_text(
266
266
  size_t *len);
267
267
 
268
268
  SELVA_EXPORT
269
- struct SelvaNodeReference *selva_fields_get_reference(struct SelvaDb *db, struct SelvaNode *node, field_t field);
269
+ struct SelvaNodeReference *selva_fields_get_reference(struct SelvaDb *db, struct SelvaNode *node, const struct SelvaFieldSchema *fs);
270
270
 
271
271
  SELVA_EXPORT
272
- struct SelvaNodeReferences *selva_fields_get_references(struct SelvaDb *db, struct SelvaNode *node, field_t field);
272
+ struct SelvaNodeReferences *selva_fields_get_references(struct SelvaDb *db, struct SelvaNode *node, const struct SelvaFieldSchema *fs);
273
273
 
274
274
  SELVA_EXPORT
275
275
  struct SelvaNodeWeakReference selva_fields_get_weak_reference(struct SelvaDb *db, struct SelvaFields *fields, field_t field);
@@ -309,7 +309,7 @@ int selva_fields_del(struct SelvaDb *db, struct SelvaNode *node, const struct Se
309
309
  * Delete an edge from a references field.
310
310
  */
311
311
  SELVA_EXPORT
312
- int selva_fields_del_ref(struct SelvaDb *db, struct SelvaNode *node, field_t field, node_id_t dst_node_id);
312
+ int selva_fields_del_ref(struct SelvaDb *db, struct SelvaNode *node, const struct SelvaFieldSchema *fs, node_id_t dst_node_id);
313
313
 
314
314
  SELVA_EXPORT
315
315
  void selva_fields_clear_references(struct SelvaDb *db, struct SelvaNode *node, const struct SelvaFieldSchema *fs);
@@ -123,7 +123,7 @@ int selva_string_init_crc(struct selva_string *s, const char *str, size_t len, u
123
123
  [[nodiscard]]
124
124
  SELVA_EXPORT
125
125
  struct selva_string *selva_string_create(const char *str, size_t len, enum selva_string_flags flags)
126
- __attribute__((access(read_only, 1, 2)));
126
+ __attribute__((access(read_only, 1)));
127
127
 
128
128
  /**
129
129
  * Create a new string with a user provided CRC.
@@ -205,7 +205,7 @@ int selva_string_truncate(struct selva_string *s, size_t newlen)
205
205
  */
206
206
  SELVA_EXPORT
207
207
  int selva_string_append(struct selva_string *s, const char *str, size_t len)
208
- __attribute__((access(read_only, 2, 3)));
208
+ __attribute__((access(read_only, 2)));
209
209
 
210
210
  /**
211
211
  * Replace current value of the string s with str.
Binary file
@@ -71,6 +71,16 @@ struct SelvaTypeEntry *selva_get_type_by_index(const struct SelvaDb *db, node_ty
71
71
  SELVA_EXPORT
72
72
  struct SelvaTypeEntry *selva_get_type_by_node(const struct SelvaDb *db, struct SelvaNode *node) __attribute__((nonnull, pure));
73
73
 
74
+ SELVA_EXPORT
75
+ inline block_id_t selva_get_block_capacity(const struct SelvaTypeEntry *te)
76
+ #if !__zig
77
+ {
78
+ return te->blocks->block_capacity;
79
+ }
80
+ #else
81
+ ;
82
+ #endif
83
+
74
84
  /**
75
85
  * Get the node schema for type.
76
86
  */
@@ -298,7 +308,29 @@ size_t selva_node_count(const struct SelvaTypeEntry *type) __attribute__((nonnul
298
308
  * Get the node id of of node.
299
309
  */
300
310
  SELVA_EXPORT
301
- node_id_t selva_get_node_id(const struct SelvaNode *node) __attribute__((nonnull, pure));
311
+ __attribute__((nonnull, pure))
312
+ inline node_id_t selva_get_node_id(const struct SelvaNode *node)
313
+ #if !__zig
314
+ {
315
+ return node->node_id;
316
+ }
317
+ #else
318
+ ;
319
+ #endif
320
+
321
+ /**
322
+ * Get the type of of node.
323
+ */
324
+ SELVA_EXPORT
325
+ __attribute__((nonnull, pure))
326
+ inline node_type_t selva_get_node_type(const struct SelvaNode *node)
327
+ #if !__zig
328
+ {
329
+ return node->type;
330
+ }
331
+ #else
332
+ ;
333
+ #endif
302
334
 
303
335
  /**
304
336
  * \addtogroup node_hash
@@ -332,15 +364,17 @@ size_t selva_alias_count(const struct SelvaAliases *aliases);
332
364
  /**
333
365
  * Set new alias.
334
366
  * @param name is copied.
367
+ * @returns the previous node_id the alias was pointing to; Otherwise 0.
335
368
  */
336
369
  SELVA_EXPORT
337
- void selva_set_alias(struct SelvaAliases *aliases, node_id_t dest, const char *name_str, size_t name_len);
370
+ node_id_t selva_set_alias(struct SelvaAliases *aliases, node_id_t dest, const char *name_str, size_t name_len);
338
371
 
339
372
  /**
340
373
  * Delete alias by name.
374
+ * @return the destination node_id the alias was pointing to; 0 if SELVA_ENOENT.
341
375
  */
342
376
  SELVA_EXPORT
343
- int selva_del_alias_by_name(struct SelvaAliases *aliases, const char *name_str, size_t name_len);
377
+ node_id_t selva_del_alias_by_name(struct SelvaAliases *aliases, const char *name_str, size_t name_len);
344
378
 
345
379
  /**
346
380
  * Delete all aliases pointing to dest.
@@ -266,10 +266,10 @@ int selva_fields_get_text(
266
266
  size_t *len);
267
267
 
268
268
  SELVA_EXPORT
269
- struct SelvaNodeReference *selva_fields_get_reference(struct SelvaDb *db, struct SelvaNode *node, field_t field);
269
+ struct SelvaNodeReference *selva_fields_get_reference(struct SelvaDb *db, struct SelvaNode *node, const struct SelvaFieldSchema *fs);
270
270
 
271
271
  SELVA_EXPORT
272
- struct SelvaNodeReferences *selva_fields_get_references(struct SelvaDb *db, struct SelvaNode *node, field_t field);
272
+ struct SelvaNodeReferences *selva_fields_get_references(struct SelvaDb *db, struct SelvaNode *node, const struct SelvaFieldSchema *fs);
273
273
 
274
274
  SELVA_EXPORT
275
275
  struct SelvaNodeWeakReference selva_fields_get_weak_reference(struct SelvaDb *db, struct SelvaFields *fields, field_t field);
@@ -309,7 +309,7 @@ int selva_fields_del(struct SelvaDb *db, struct SelvaNode *node, const struct Se
309
309
  * Delete an edge from a references field.
310
310
  */
311
311
  SELVA_EXPORT
312
- int selva_fields_del_ref(struct SelvaDb *db, struct SelvaNode *node, field_t field, node_id_t dst_node_id);
312
+ int selva_fields_del_ref(struct SelvaDb *db, struct SelvaNode *node, const struct SelvaFieldSchema *fs, node_id_t dst_node_id);
313
313
 
314
314
  SELVA_EXPORT
315
315
  void selva_fields_clear_references(struct SelvaDb *db, struct SelvaNode *node, const struct SelvaFieldSchema *fs);
@@ -123,7 +123,7 @@ int selva_string_init_crc(struct selva_string *s, const char *str, size_t len, u
123
123
  [[nodiscard]]
124
124
  SELVA_EXPORT
125
125
  struct selva_string *selva_string_create(const char *str, size_t len, enum selva_string_flags flags)
126
- __attribute__((access(read_only, 1, 2)));
126
+ __attribute__((access(read_only, 1)));
127
127
 
128
128
  /**
129
129
  * Create a new string with a user provided CRC.
@@ -205,7 +205,7 @@ int selva_string_truncate(struct selva_string *s, size_t newlen)
205
205
  */
206
206
  SELVA_EXPORT
207
207
  int selva_string_append(struct selva_string *s, const char *str, size_t len)
208
- __attribute__((access(read_only, 2, 3)));
208
+ __attribute__((access(read_only, 2)));
209
209
 
210
210
  /**
211
211
  * Replace current value of the string s with str.
Binary file
@@ -1,4 +1,8 @@
1
- import { makeCsmtKeyFromNodeId } from './tree.js';
1
+ // TODO This definitely shouldn't be copy-pasted here from server/tree.ts
2
+ const makeCsmtKeyFromNodeId = (typeId, blockCapacity, nodeId) => {
3
+ const tmp = nodeId - +!(nodeId % blockCapacity);
4
+ return typeId * 4294967296 + ((tmp / blockCapacity) | 0) * blockCapacity + 1;
5
+ };
2
6
  export class ModifyCtx {
3
7
  constructor(db) {
4
8
  this.max = db.maxModifySize;
@@ -22,12 +22,14 @@ type DbClientOpts = {
22
22
  hooks: DbClientHooks;
23
23
  maxModifySize?: number;
24
24
  };
25
+ type DbClientSchema = StrictSchema & {
26
+ lastId: number;
27
+ };
25
28
  export declare class DbClient {
26
29
  constructor({ hooks, maxModifySize }: DbClientOpts);
27
30
  hooks: DbClientHooks;
28
- schema: StrictSchema & {
29
- lastId: number;
30
- };
31
+ schema: DbClientSchema;
32
+ schemaIsSetValue: boolean;
31
33
  schemaTypesParsed: Record<string, SchemaTypeDef>;
32
34
  schemaTypesParsedById: Record<number, SchemaTypeDef>;
33
35
  writeTime: number;
@@ -45,9 +47,7 @@ export declare class DbClient {
45
47
  subscriptionsToRun: SubscriptionsToRun;
46
48
  schemaChecksum: number;
47
49
  setSchema(schema: Schema, fromStart?: boolean, transformFns?: TransformFns): Promise<StrictSchema>;
48
- putLocalSchema(schema: any): StrictSchema & {
49
- lastId: number;
50
- };
50
+ putLocalSchema(schema: any): DbClientSchema;
51
51
  create(type: string, obj?: CreateObj, opts?: ModifyOpts): ModifyRes;
52
52
  copy(type: string, target: number | ModifyRes, objOrTransformFn?: Record<string, any> | ((item: Record<string, any>) => Promise<any>)): Promise<ModifyRes>;
53
53
  query(type: string, id?: number | ModifyRes | (number | ModifyRes)[] | QueryByAliasObj | QueryByAliasObj[] | Uint32Array): BasedDbQuery;
@@ -64,5 +64,11 @@ export declare class DbClient {
64
64
  stop(): void;
65
65
  drain(): Promise<number>;
66
66
  isModified(): Promise<void>;
67
+ schemaIsSet(): Promise<true>;
68
+ listeners?: {
69
+ schema?: Set<(schema: DbClientSchema) => void>;
70
+ };
71
+ on(event: 'schema', cb: (schema: DbClientSchema) => void): void;
72
+ off(event: 'schema', cb: (schema: DbClientSchema) => void): void;
67
73
  }
68
74
  export {};
@@ -17,11 +17,11 @@ export class DbClient {
17
17
  this.modifyCtx = new ModifyCtx(this);
18
18
  }
19
19
  hooks;
20
- // schema
21
20
  schema = {
22
21
  lastId: 1, // we reserve one for root props
23
22
  types: {},
24
23
  };
24
+ schemaIsSetValue = false;
25
25
  schemaTypesParsed = {};
26
26
  schemaTypesParsedById = {};
27
27
  // modify
@@ -38,6 +38,7 @@ export class DbClient {
38
38
  subscriptionsToRun = [];
39
39
  schemaChecksum;
40
40
  async setSchema(schema, fromStart, transformFns) {
41
+ this.schemaIsSetValue = true;
41
42
  const checksum = hash(schema);
42
43
  if (checksum === this.schemaChecksum) {
43
44
  return this.schema;
@@ -48,6 +49,7 @@ export class DbClient {
48
49
  return this.putLocalSchema(remoteSchema);
49
50
  }
50
51
  putLocalSchema(schema) {
52
+ this.schemaIsSetValue = true;
51
53
  if (deepEqual(this.schema, schema)) {
52
54
  return this.schema;
53
55
  }
@@ -61,6 +63,11 @@ export class DbClient {
61
63
  updateTypeDefs(this.schema, this.schemaTypesParsed, this.schemaTypesParsedById);
62
64
  // Adds bidrectional refs on defs
63
65
  schemaToSelvaBuffer(this.schemaTypesParsed);
66
+ if (this.listeners?.schema) {
67
+ for (const cb of this.listeners.schema) {
68
+ cb(this.schema);
69
+ }
70
+ }
64
71
  return this.schema;
65
72
  }
66
73
  create(type, obj = {}, opts) {
@@ -166,6 +173,9 @@ export class DbClient {
166
173
  }
167
174
  // For more advanced / internal usage - use isModified instead for most cases
168
175
  async drain() {
176
+ if (this.upserting.size) {
177
+ await Promise.all(Array.from(this.upserting).map(([, { p }]) => p));
178
+ }
169
179
  await flushBuffer(this);
170
180
  const t = this.writeTime;
171
181
  this.writeTime = 0;
@@ -178,5 +188,27 @@ export class DbClient {
178
188
  await this.hooks.flushIsReady;
179
189
  return;
180
190
  }
191
+ schemaIsSet() {
192
+ return new Promise((resolve) => {
193
+ if (this.schemaIsSetValue) {
194
+ resolve(true);
195
+ }
196
+ else {
197
+ setTimeout(() => {
198
+ // TODO use subscription when its done
199
+ resolve(this.schemaIsSet());
200
+ }, 12);
201
+ }
202
+ });
203
+ }
204
+ listeners;
205
+ on(event, cb) {
206
+ this.listeners ??= {};
207
+ this.listeners[event] ??= new Set();
208
+ this.listeners[event].add(cb);
209
+ }
210
+ off(event, cb) {
211
+ this.listeners?.[event]?.delete(cb);
212
+ }
181
213
  }
182
214
  //# sourceMappingURL=index.js.map
@@ -30,7 +30,8 @@ const appendCreate = (ctx, def, obj, res, unsafe) => {
30
30
  ctx.buf[ctx.len++] = mainLenU32 >>>= 8;
31
31
  ctx.buf[ctx.len++] = mainLenU32 >>>= 8;
32
32
  ctx.lastMain = ctx.len;
33
- ctx.buf.fill(0, ctx.len, (ctx.len += def.mainLen));
33
+ ctx.buf.set(def.mainEmpty, ctx.len);
34
+ ctx.len += def.mainLen;
34
35
  }
35
36
  err = writeFixedValue(ctx, createTs, prop, ctx.lastMain + prop.start);
36
37
  if (err) {
@@ -38,6 +39,21 @@ const appendCreate = (ctx, def, obj, res, unsafe) => {
38
39
  }
39
40
  }
40
41
  }
42
+ else if (ctx.lastMain === -1 && !def.mainEmptyAllZeroes) {
43
+ // this is there to handle different defaults
44
+ if (ctx.lastMain === -1) {
45
+ let mainLenU32 = def.mainLen;
46
+ setCursor(ctx, def, 0, MICRO_BUFFER, res.tmpId, CREATE);
47
+ ctx.buf[ctx.len++] = CREATE;
48
+ ctx.buf[ctx.len++] = mainLenU32;
49
+ ctx.buf[ctx.len++] = mainLenU32 >>>= 8;
50
+ ctx.buf[ctx.len++] = mainLenU32 >>>= 8;
51
+ ctx.buf[ctx.len++] = mainLenU32 >>>= 8;
52
+ ctx.lastMain = ctx.len;
53
+ ctx.buf.set(def.mainEmpty, ctx.len);
54
+ ctx.len += def.mainLen;
55
+ }
56
+ }
41
57
  if (def.hasSeperateSort) {
42
58
  if (ctx.hasSortField !== def.seperateSort.size - 1) {
43
59
  if (ctx.len + 3 > ctx.max) {
@@ -56,7 +56,7 @@ map[ENUM] = (ctx, val, def) => {
56
56
  return RANGE_ERR;
57
57
  }
58
58
  if (val === null) {
59
- ctx.buf[ctx.len++] = 1;
59
+ ctx.buf[ctx.len++] = 0;
60
60
  }
61
61
  else if (val in def.reverseEnum) {
62
62
  ctx.buf[ctx.len++] = def.reverseEnum[val] + 1;
@@ -84,6 +84,9 @@ map[TIMESTAMP] = (ctx, val, def) => {
84
84
  if (ctx.len + 8 > ctx.max) {
85
85
  return RANGE_ERR;
86
86
  }
87
+ if (val < 0) {
88
+ return new ModifyError(def, val);
89
+ }
87
90
  const view = new DataView(ctx.buf.buffer, ctx.buf.byteOffset + ctx.len, 8);
88
91
  ctx.len += 8;
89
92
  view.setFloat64(0, parsedValue, true);
@@ -95,6 +98,9 @@ map[UINT32] = (ctx, val, def) => {
95
98
  if (ctx.len + 4 > ctx.max) {
96
99
  return RANGE_ERR;
97
100
  }
101
+ if (val > 4294967295 || val < 0) {
102
+ return new ModifyError(def, val);
103
+ }
98
104
  ctx.buf[ctx.len++] = val;
99
105
  ctx.buf[ctx.len++] = val >>>= 8;
100
106
  ctx.buf[ctx.len++] = val >>>= 8;
@@ -107,6 +113,9 @@ map[UINT16] = (ctx, val, def) => {
107
113
  if (ctx.len + 2 > ctx.max) {
108
114
  return RANGE_ERR;
109
115
  }
116
+ if (val > 65535 || val < 0) {
117
+ return new ModifyError(def, val);
118
+ }
110
119
  ctx.buf[ctx.len++] = val;
111
120
  ctx.buf[ctx.len++] = val >>>= 8;
112
121
  };
@@ -117,11 +126,51 @@ map[UINT8] = (ctx, val, def) => {
117
126
  if (ctx.len + 1 > ctx.max) {
118
127
  return RANGE_ERR;
119
128
  }
129
+ if (val > 255 || val < 0) {
130
+ return new ModifyError(def, val);
131
+ }
132
+ ctx.buf[ctx.len++] = val;
133
+ };
134
+ map[INT32] = (ctx, val, def) => {
135
+ if (typeof val !== 'number') {
136
+ return new ModifyError(def, val);
137
+ }
138
+ if (ctx.len + 4 > ctx.max) {
139
+ return RANGE_ERR;
140
+ }
141
+ if (val > 2147483647 || val < -2147483648) {
142
+ return new ModifyError(def, val);
143
+ }
144
+ ctx.buf[ctx.len++] = val;
145
+ ctx.buf[ctx.len++] = val >>>= 8;
146
+ ctx.buf[ctx.len++] = val >>>= 8;
147
+ ctx.buf[ctx.len++] = val >>>= 8;
148
+ };
149
+ map[INT16] = (ctx, val, def) => {
150
+ if (typeof val !== 'number') {
151
+ return new ModifyError(def, val);
152
+ }
153
+ if (ctx.len + 2 > ctx.max) {
154
+ return RANGE_ERR;
155
+ }
156
+ if (val > 32767 || val < -32768) {
157
+ return new ModifyError(def, val);
158
+ }
159
+ ctx.buf[ctx.len++] = val;
160
+ ctx.buf[ctx.len++] = val >>>= 8;
161
+ };
162
+ map[INT8] = (ctx, val, def) => {
163
+ if (typeof val !== 'number') {
164
+ return new ModifyError(def, val);
165
+ }
166
+ if (ctx.len + 1 > ctx.max) {
167
+ return RANGE_ERR;
168
+ }
169
+ if (val > 127 || val < -128) {
170
+ return new ModifyError(def, val);
171
+ }
120
172
  ctx.buf[ctx.len++] = val;
121
173
  };
122
- map[INT32] = map[UINT32];
123
- map[INT16] = map[UINT16];
124
- map[INT8] = map[UINT8];
125
174
  export const writeFixedValue = (ctx, val, def, pos) => {
126
175
  const len = ctx.len;
127
176
  ctx.len = pos;
@@ -24,10 +24,13 @@ function _modify(ctx, res, obj, schema, mod, tree, overwrite, unsafe) {
24
24
  }
25
25
  let err;
26
26
  if (isPropDef(def)) {
27
+ const val = obj[key];
28
+ if (val === undefined) {
29
+ continue;
30
+ }
27
31
  if (res.subMarkers) {
28
32
  checkSubscriptionMarkers(ctx.db, res.subMarkers, def);
29
33
  }
30
- const val = obj[key];
31
34
  const type = def.typeIndex;
32
35
  if (def.separate) {
33
36
  if (type === STRING) {
@@ -72,7 +75,9 @@ function _modify(ctx, res, obj, schema, mod, tree, overwrite, unsafe) {
72
75
  ctx.buf[ctx.len++] = mainLenU32 >>>= 8;
73
76
  ctx.buf[ctx.len++] = mainLenU32 >>>= 8;
74
77
  ctx.lastMain = ctx.len;
75
- ctx.buf.fill(0, ctx.len, (ctx.len += schema.mainLen));
78
+ ctx.buf.set(schema.mainEmpty, ctx.len);
79
+ ctx.len += schema.mainLen;
80
+ // ctx.buf.fill(0, ctx.len, (ctx.len += schema.mainLen))
76
81
  }
77
82
  if (typeof val === 'object' && val !== null && 'increment' in val) {
78
83
  err = writeFixedValue(ctx, val.increment, def, ctx.lastMain + def.start);
@@ -81,8 +86,8 @@ function _modify(ctx, res, obj, schema, mod, tree, overwrite, unsafe) {
81
86
  err = writeFixedValue(ctx, val, def, ctx.lastMain + def.start);
82
87
  }
83
88
  }
84
- else if (typeof val === 'object') {
85
- if (val !== null && 'increment' in val) {
89
+ else if (typeof val === 'object' && val !== null) {
90
+ if ('increment' in val) {
86
91
  let increment = val.increment;
87
92
  if (increment === 0) {
88
93
  continue;