@based/db 0.0.52 → 0.0.56

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 (65) hide show
  1. package/dist/lib/darwin_aarch64/libdeflate.dylib +0 -0
  2. package/dist/lib/darwin_aarch64/libjemalloc_selva.2.dylib +0 -0
  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/libnode-v24.node +0 -0
  8. package/dist/lib/darwin_aarch64/libselva.dylib +0 -0
  9. package/dist/lib/linux_aarch64/libnode-v20.node +0 -0
  10. package/dist/lib/linux_aarch64/libnode-v21.node +0 -0
  11. package/dist/lib/linux_aarch64/libnode-v22.node +0 -0
  12. package/dist/lib/linux_aarch64/libnode-v23.node +0 -0
  13. package/dist/lib/linux_aarch64/libnode-v24.node +0 -0
  14. package/dist/lib/linux_aarch64/libselva.so +0 -0
  15. package/dist/lib/linux_x86_64/libnode-v20.node +0 -0
  16. package/dist/lib/linux_x86_64/libnode-v21.node +0 -0
  17. package/dist/lib/linux_x86_64/libnode-v22.node +0 -0
  18. package/dist/lib/linux_x86_64/libnode-v23.node +0 -0
  19. package/dist/lib/linux_x86_64/libnode-v24.node +0 -0
  20. package/dist/lib/linux_x86_64/libselva.so +0 -0
  21. package/dist/src/client/flushModify.d.ts +1 -0
  22. package/dist/src/client/flushModify.js +8 -0
  23. package/dist/src/client/index.d.ts +8 -32
  24. package/dist/src/client/index.js +22 -84
  25. package/dist/src/client/modify/ModifyRes.js +4 -1
  26. package/dist/src/client/modify/references/reference.js +3 -0
  27. package/dist/src/client/query/BasedDbQuery.d.ts +2 -3
  28. package/dist/src/client/query/BasedDbQuery.js +150 -139
  29. package/dist/src/client/query/aggregates/aggregation.js +3 -0
  30. package/dist/src/client/query/registerQuery.js +16 -0
  31. package/dist/src/client/query/subscription/index.d.ts +1 -1
  32. package/dist/src/client/query/subscription/index.js +32 -9
  33. package/dist/src/client/setLocalClientSchema.d.ts +3 -0
  34. package/dist/src/client/setLocalClientSchema.js +30 -0
  35. package/dist/src/hooks.d.ts +9 -8
  36. package/dist/src/hooks.js +10 -2
  37. package/dist/src/index.d.ts +6 -7
  38. package/dist/src/index.js +24 -41
  39. package/dist/src/schema.d.ts +5 -2
  40. package/dist/src/schema.js +1 -93
  41. package/dist/src/server/DbWorker.d.ts +12 -0
  42. package/dist/src/server/DbWorker.js +42 -0
  43. package/dist/src/server/index.d.ts +6 -38
  44. package/dist/src/server/index.js +39 -146
  45. package/dist/src/server/migrate/index.d.ts +2 -2
  46. package/dist/src/server/migrate/index.js +66 -61
  47. package/dist/src/server/migrate/types.d.ts +4 -0
  48. package/dist/src/server/migrate/types.js +6 -0
  49. package/dist/src/server/migrate/utils.d.ts +3 -0
  50. package/dist/src/server/migrate/utils.js +16 -0
  51. package/dist/src/server/migrate/worker.js +18 -19
  52. package/dist/src/server/resizeModifyDirtyRanges.d.ts +2 -0
  53. package/dist/src/server/resizeModifyDirtyRanges.js +17 -0
  54. package/dist/src/server/save.d.ts +1 -1
  55. package/dist/src/server/save.js +5 -3
  56. package/dist/src/server/schema.d.ts +7 -0
  57. package/dist/src/server/schema.js +111 -0
  58. package/dist/src/server/start.js +10 -4
  59. package/dist/src/shared/DbBase.d.ts +13 -0
  60. package/dist/src/shared/DbBase.js +7 -0
  61. package/dist/src/shared/Emitter.d.ts +17 -0
  62. package/dist/src/shared/Emitter.js +66 -0
  63. package/dist/src/types.d.ts +3 -0
  64. package/dist/src/types.js +4 -0
  65. package/package.json +3 -3
Binary file
Binary file
@@ -32,3 +32,4 @@ export declare class ModifyCtx {
32
32
  export declare const execCtxQueue: (resCtx: ModifyCtx["ctx"], error?: boolean) => void;
33
33
  export declare const flushBuffer: (db: DbClient) => Promise<void>;
34
34
  export declare const startDrain: (db: DbClient) => void;
35
+ export declare const makeFlushIsReady: (dbClient: DbClient) => void;
@@ -178,4 +178,12 @@ export const startDrain = (db) => {
178
178
  }, db.flushTime);
179
179
  }
180
180
  };
181
+ export const makeFlushIsReady = (dbClient) => {
182
+ dbClient.flushIsReady = new Promise((resolve) => {
183
+ dbClient.flushReady = () => {
184
+ resolve();
185
+ makeFlushIsReady(dbClient);
186
+ };
187
+ });
188
+ };
181
189
  //# sourceMappingURL=flushModify.js.map
@@ -1,40 +1,27 @@
1
- import { Schema, StrictSchema } from '@based/schema';
1
+ import { Schema } from '@based/schema';
2
2
  import { CreateObj } from './modify/create.js';
3
- import { SchemaTypeDef } from '@based/schema/def';
4
3
  import { ModifyCtx } from './flushModify.js';
5
4
  import { BasedDbQuery, QueryByAliasObj } from './query/BasedDbQuery.js';
6
5
  import { ModifyRes } from './modify/ModifyRes.js';
7
- import { DbServer } from '../server/index.js';
8
6
  import { TransformFns } from '../server/migrate/index.js';
9
7
  import { ModifyOpts } from './modify/types.js';
10
- import { OnClose, OnData, OnError } from './query/subscription/types.js';
11
8
  import { SubStore } from './query/subscription/index.js';
12
- export type DbClientHooks = {
13
- setSchema(schema: StrictSchema, fromStart?: boolean, transformFns?: TransformFns): Promise<DbServer['schema']>;
14
- flushModify(buf: Uint8Array): Promise<{
15
- offsets: Record<number, number>;
16
- dbWriteTime?: number;
17
- }>;
18
- getQueryBuf(buf: Uint8Array): Promise<Uint8Array>;
19
- subscribe(q: BasedDbQuery, onData: (buf: Uint8Array) => ReturnType<OnData>, onError?: OnError): OnClose;
20
- };
9
+ import { DbShared } from '../shared/DbBase.js';
10
+ import { DbClientHooks } from '../hooks.js';
11
+ import { SchemaChecksum } from '../schema.js';
21
12
  type DbClientOpts = {
22
13
  hooks: DbClientHooks;
23
14
  maxModifySize?: number;
24
15
  flushTime?: number;
25
16
  debug?: boolean;
26
17
  };
27
- type DbClientSchema = DbServer['schema'];
28
- export declare class DbClient {
18
+ export declare class DbClient extends DbShared {
29
19
  constructor({ hooks, maxModifySize, flushTime, debug, }: DbClientOpts);
30
20
  subs: Map<BasedDbQuery, SubStore>;
21
+ hooks: DbClientHooks;
31
22
  flushTime: number;
32
23
  flushReady: () => void;
33
24
  flushIsReady: Promise<void>;
34
- hooks: DbClientHooks;
35
- schema: DbClientSchema;
36
- schemaTypesParsed: Record<string, SchemaTypeDef>;
37
- schemaTypesParsedById: Record<number, SchemaTypeDef>;
38
25
  writeTime: number;
39
26
  isDraining: boolean;
40
27
  modifyCtx: ModifyCtx;
@@ -43,13 +30,8 @@ export declare class DbClient {
43
30
  o: Record<string, any>;
44
31
  p: Promise<number | ModifyRes>;
45
32
  }>;
46
- schemaProcessing: number;
47
- schemaPromise: Promise<DbServer['schema']>;
48
- setSchema(schema: Schema, fromStart?: boolean, transformFns?: TransformFns): Promise<StrictSchema>;
49
- putLocalSchema(schema: DbServer['schema']): StrictSchema & {
50
- lastId: number;
51
- hash?: number;
52
- };
33
+ schemaIsSet(): Promise<void>;
34
+ setSchema(schema: Schema, transformFns?: TransformFns): Promise<SchemaChecksum>;
53
35
  create(type: string, obj?: CreateObj, opts?: ModifyOpts): ModifyRes;
54
36
  copy(type: string, target: number | ModifyRes, objOrTransformFn?: Record<string, any> | ((item: Record<string, any>) => Promise<any>)): Promise<ModifyRes>;
55
37
  query(type: string, id?: number | ModifyRes | (number | ModifyRes)[] | QueryByAliasObj | QueryByAliasObj[] | Uint32Array): BasedDbQuery;
@@ -66,11 +48,5 @@ export declare class DbClient {
66
48
  stop(): void;
67
49
  drain(): Promise<number>;
68
50
  isModified(): Promise<void>;
69
- schemaIsSet(): Promise<true>;
70
- listeners?: {
71
- schema?: Set<(schema: DbClientSchema) => void>;
72
- };
73
- on(event: 'schema', cb: (schema: DbClientSchema) => void): void;
74
- off(event: 'schema', cb: (schema: DbClientSchema) => void): void;
75
51
  }
76
52
  export {};
@@ -1,27 +1,18 @@
1
1
  import { parse } from '@based/schema';
2
2
  import { create } from './modify/create.js';
3
- import { updateTypeDefs, schemaToSelvaBuffer, } from '@based/schema/def';
4
- import { execCtxQueue, flushBuffer, ModifyCtx, startDrain, } from './flushModify.js';
3
+ import { flushBuffer, makeFlushIsReady, ModifyCtx, startDrain, } from './flushModify.js';
5
4
  import { BasedDbQuery } from './query/BasedDbQuery.js';
6
5
  import { ModifyState } from './modify/ModifyRes.js';
7
6
  import { upsert } from './modify/upsert.js';
8
7
  import { update } from './modify/update.js';
9
8
  import { deleteFn } from './modify/delete.js';
10
- import { wait } from '@saulx/utils';
11
- import { hash } from '@saulx/hash';
12
9
  import { expire } from './modify/expire.js';
13
10
  import { debugMode } from '../utils.js';
14
- import { parseSchema, schemaLooseEqual } from '../schema.js';
15
- const makeFlushIsReady = (dbClient) => {
16
- dbClient.flushIsReady = new Promise((resolve) => {
17
- dbClient.flushReady = () => {
18
- resolve();
19
- makeFlushIsReady(dbClient);
20
- };
21
- });
22
- };
23
- export class DbClient {
11
+ import { DbShared } from '../shared/DbBase.js';
12
+ import { setLocalClientSchema } from './setLocalClientSchema.js';
13
+ export class DbClient extends DbShared {
24
14
  constructor({ hooks, maxModifySize = 100 * 1e3 * 1e3, flushTime = 0, debug, }) {
15
+ super();
25
16
  this.hooks = hooks;
26
17
  this.maxModifySize = maxModifySize;
27
18
  this.modifyCtx = new ModifyCtx(this);
@@ -30,68 +21,35 @@ export class DbClient {
30
21
  if (debug) {
31
22
  debugMode(this);
32
23
  }
24
+ this.hooks.subscribeSchema((schema) => {
25
+ setLocalClientSchema(this, schema);
26
+ });
33
27
  }
34
28
  subs = new Map();
29
+ hooks;
30
+ // modify
35
31
  flushTime;
36
32
  flushReady;
37
33
  flushIsReady;
38
- hooks;
39
- schema;
40
- schemaTypesParsed = {};
41
- schemaTypesParsedById = {};
42
- // modify
43
34
  writeTime = 0;
44
35
  isDraining = false;
45
36
  modifyCtx;
46
37
  maxModifySize;
47
38
  upserting = new Map();
48
- schemaProcessing;
49
- schemaPromise;
50
- async setSchema(schema, fromStart, transformFns) {
51
- const strictSchema = fromStart ? schema : parse(schema).schema;
52
- const parsedSchema = parseSchema(strictSchema);
53
- // this one excludes all the ids
54
- if (schemaLooseEqual(parsedSchema, this.schema)) {
55
- return this.schema;
56
- }
57
- // drain current things
58
- await this.drain();
59
- const checksum = hash(strictSchema);
60
- if (checksum !== this.schemaProcessing) {
61
- this.schemaProcessing = checksum;
62
- this.schemaPromise = this.hooks.setSchema(strictSchema, fromStart, transformFns);
39
+ async schemaIsSet() {
40
+ if (!this.schema) {
41
+ await this.once('schema');
63
42
  }
64
- const remoteSchema = await this.schemaPromise;
65
- this.schemaProcessing = null;
66
- this.schemaPromise = null;
67
- return this.putLocalSchema(remoteSchema);
68
43
  }
69
- putLocalSchema(schema) {
70
- if (this.schema && this.schema.hash === schema.hash) {
71
- return this.schema;
72
- }
73
- this.schema = schema;
74
- updateTypeDefs(this.schema, this.schemaTypesParsed, this.schemaTypesParsedById);
75
- // Adds bidrectional refs on defs
76
- schemaToSelvaBuffer(this.schemaTypesParsed);
77
- // this has to happen before the listeners
78
- if (this.modifyCtx.len > 8) {
79
- console.info('Modify cancelled - schema updated');
80
- }
81
- // cancel modify queue
82
- const resCtx = this.modifyCtx.ctx;
83
- this.modifyCtx.reset();
84
- execCtxQueue(resCtx, true);
85
- // resubscribe
86
- for (const [q, store] of this.subs) {
87
- store.resubscribe(q);
88
- }
89
- if (this.listeners?.schema) {
90
- for (const cb of this.listeners.schema) {
91
- cb(this.schema);
92
- }
44
+ async setSchema(schema, transformFns) {
45
+ const strictSchema = parse(schema).schema;
46
+ await this.drain();
47
+ const schemaChecksum = await this.hooks.setSchema(strictSchema, transformFns);
48
+ if (schemaChecksum !== this.schema?.hash) {
49
+ await this.once('schema');
50
+ return this.schema.hash;
93
51
  }
94
- return this.schema;
52
+ return schemaChecksum;
95
53
  }
96
54
  create(type, obj = {}, opts) {
97
55
  return create(this, type, obj, opts);
@@ -189,6 +147,7 @@ export class DbClient {
189
147
  }
190
148
  destroy() {
191
149
  this.stop();
150
+ delete this.listeners;
192
151
  this.modifyCtx.db = null; // Make sure we don't have a circular ref and leak mem
193
152
  }
194
153
  stop() {
@@ -215,26 +174,5 @@ export class DbClient {
215
174
  await this.flushIsReady;
216
175
  return;
217
176
  }
218
- async schemaIsSet() {
219
- if (this.schema) {
220
- return true;
221
- }
222
- if (this.schemaPromise) {
223
- await this.schemaPromise;
224
- }
225
- else {
226
- await wait(12);
227
- }
228
- return this.schemaIsSet();
229
- }
230
- listeners;
231
- on(event, cb) {
232
- this.listeners ??= {};
233
- this.listeners[event] ??= new Set();
234
- this.listeners[event].add(cb);
235
- }
236
- off(event, cb) {
237
- this.listeners?.[event]?.delete(cb);
238
- }
239
177
  }
240
178
  //# sourceMappingURL=index.js.map
@@ -1,5 +1,5 @@
1
1
  import { isPropDef, REVERSE_TYPE_INDEX_MAP, } from '@based/schema/def';
2
- import { langCodesMap } from '@based/schema';
2
+ import { isValidId, langCodesMap } from '@based/schema';
3
3
  const MAGIC_KEY = Math.random().toString(36).substring(2);
4
4
  const MAGIC_REG = RegExp(`("${MAGIC_KEY}|${MAGIC_KEY}")`, 'g');
5
5
  const walk = (val) => {
@@ -43,6 +43,9 @@ export class ModifyError extends Error {
43
43
  }
44
44
  export class ModifyState {
45
45
  constructor(typeId, tmpId, db, opts, update = false) {
46
+ if (!isValidId(tmpId)) {
47
+ throw new Error('Invalid id passed to modify');
48
+ }
46
49
  this.tmpId = tmpId;
47
50
  this.#typeId = typeId;
48
51
  this.#ctx = db.modifyCtx.ctx;
@@ -4,6 +4,9 @@ import { DELETE, RANGE_ERR, EDGE_NOINDEX_TMPID, NOEDGE_NOINDEX_TMPID, EDGE_NOIND
4
4
  import { writeEdges } from './edge.js';
5
5
  import { dbUpdateFromUpsert } from './references.js';
6
6
  function writeRef(id, ctx, schema, def, parentId, modifyOp, hasEdges, isTmpId) {
7
+ if (!def.validation(id, def)) {
8
+ return new ModifyError(def, id);
9
+ }
7
10
  if (ctx.len + 16 > ctx.max) {
8
11
  return RANGE_ERR;
9
12
  }
@@ -16,7 +16,7 @@ export declare class QueryBranch<T> {
16
16
  db: DbClient;
17
17
  def: QueryDef;
18
18
  queryCommands: QueryCommand[];
19
- constructor(db: DbClient, def: QueryDef);
19
+ constructor(db: DbClient, def?: QueryDef);
20
20
  sort(field: string, order?: 'asc' | 'desc'): T;
21
21
  filter<O extends Operator>(field: string, operator?: O | boolean, value?: any, opts?: FilterOpts<O>): T;
22
22
  filterBatch(f: FilterAst): this;
@@ -42,7 +42,7 @@ export declare class BasedDbQuery extends QueryBranch<BasedDbQuery> {
42
42
  skipValidation: boolean;
43
43
  target: QueryTarget;
44
44
  constructor(db: DbClient, type: string, rawTarget?: QueryByAliasObj | number | Uint32Array | (QueryByAliasObj | number)[], skipValidation?: boolean);
45
- reBuildQuery(): void;
45
+ reset(): void;
46
46
  id: number;
47
47
  get(): GetPromise;
48
48
  buffer: Uint8Array;
@@ -50,5 +50,4 @@ export declare class BasedDbQuery extends QueryBranch<BasedDbQuery> {
50
50
  locale(locale: LangName): this;
51
51
  subscribe(onData: OnData, onError?: OnError): OnClose;
52
52
  _getSync(dbCtxExternal: any): BasedQueryResponse;
53
- toBuffer(): Uint8Array;
54
53
  }