@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
@@ -1,4 +1,4 @@
1
- import { createQueryDef, QueryDefType, filter, sort, defToBuffer, filterOr, isAlias, includeField, includeFields, addAggregate, groupBy, } from './query.js';
1
+ import { filter, sort, filterOr, isAlias, includeField, includeFields, addAggregate, groupBy, } from './query.js';
2
2
  import { BasedQueryResponse } from './BasedIterable.js';
3
3
  import { createOrGetEdgeRefQueryDef, createOrGetRefQueryDef, } from './include/utils.js';
4
4
  import { FilterBranch } from './filter/FilterBranch.js';
@@ -11,7 +11,6 @@ import { langCodesMap } from '@based/schema';
11
11
  import { convertFilter } from './filter/convertFilter.js';
12
12
  import { validateLocale, validateRange } from './validation.js';
13
13
  import { DEF_RANGE_PROP_LIMIT } from './thresholds.js';
14
- import { concatUint8Arr } from '@saulx/utils';
15
14
  import { displayTarget } from './display.js';
16
15
  import picocolors from 'picocolors';
17
16
  export class QueryBranch {
@@ -25,11 +24,13 @@ export class QueryBranch {
25
24
  sort(field, order = 'asc') {
26
25
  if (this.queryCommands) {
27
26
  this.queryCommands.push({
28
- method: 'filter',
27
+ method: 'sort',
29
28
  args: [field, order],
30
29
  });
31
30
  }
32
- sort(this.def, field, order);
31
+ else {
32
+ sort(this.def, field, order);
33
+ }
33
34
  // @ts-ignore
34
35
  return this;
35
36
  }
@@ -40,12 +41,14 @@ export class QueryBranch {
40
41
  args: [field, operator, value, opts],
41
42
  });
42
43
  }
43
- const f = convertFilter(this.def, field, operator, value, opts);
44
- if (!f) {
45
- // @ts-ignore
46
- return this;
44
+ else {
45
+ const f = convertFilter(this.def, field, operator, value, opts);
46
+ if (!f) {
47
+ // @ts-ignore
48
+ return this;
49
+ }
50
+ filter(this.db, this.def, f, this.def.filter);
47
51
  }
48
- filter(this.db, this.def, f, this.def.filter);
49
52
  // @ts-ignore
50
53
  return this;
51
54
  }
@@ -56,7 +59,9 @@ export class QueryBranch {
56
59
  args: [f],
57
60
  });
58
61
  }
59
- filter(this.db, this.def, f, this.def.filter);
62
+ else {
63
+ filter(this.db, this.def, f, this.def.filter);
64
+ }
60
65
  // @ts-ignore
61
66
  return this;
62
67
  }
@@ -67,56 +72,58 @@ export class QueryBranch {
67
72
  args: [query, field, opts, ...fields],
68
73
  });
69
74
  }
70
- if (ArrayBuffer.isView(query)) {
71
- // @ts-ignore
72
- vectorSearch(this.def, query, field, opts ?? {});
73
- // @ts-ignore
74
- return this;
75
- }
76
- if (field) {
77
- if (!fields) {
78
- // @ts-ignore
79
- fields = [field];
80
- }
81
- else {
75
+ else {
76
+ if (ArrayBuffer.isView(query)) {
82
77
  // @ts-ignore
83
- fields.unshift(field);
84
- }
85
- }
86
- if (opts) {
87
- if (!fields) {
78
+ vectorSearch(this.def, query, field, opts ?? {});
88
79
  // @ts-ignore
89
- fields = [opts];
80
+ return this;
90
81
  }
91
- else {
92
- // @ts-ignore
93
- fields.unshift(opts);
82
+ if (field) {
83
+ if (!fields) {
84
+ // @ts-ignore
85
+ fields = [field];
86
+ }
87
+ else {
88
+ // @ts-ignore
89
+ fields.unshift(field);
90
+ }
94
91
  }
95
- }
96
- if (fields.length) {
97
- if (fields.length === 1) {
98
- search(this.def, query, fields[0]);
92
+ if (opts) {
93
+ if (!fields) {
94
+ // @ts-ignore
95
+ fields = [opts];
96
+ }
97
+ else {
98
+ // @ts-ignore
99
+ fields.unshift(opts);
100
+ }
99
101
  }
100
- else {
101
- const s = {};
102
- for (const f of fields) {
103
- if (typeof f === 'string') {
104
- s[f] = 0;
105
- }
106
- else if (Array.isArray(f)) {
107
- for (const ff of f) {
108
- s[ff] = 0;
102
+ if (fields.length) {
103
+ if (fields.length === 1) {
104
+ search(this.def, query, fields[0]);
105
+ }
106
+ else {
107
+ const s = {};
108
+ for (const f of fields) {
109
+ if (typeof f === 'string') {
110
+ s[f] = 0;
111
+ }
112
+ else if (Array.isArray(f)) {
113
+ for (const ff of f) {
114
+ s[ff] = 0;
115
+ }
116
+ }
117
+ else if (typeof f === 'object') {
118
+ Object.assign(s, f);
109
119
  }
110
120
  }
111
- else if (typeof f === 'object') {
112
- Object.assign(s, f);
113
- }
121
+ search(this.def, query, s);
114
122
  }
115
- search(this.def, query, s);
116
123
  }
117
- }
118
- else {
119
- search(this.def, query);
124
+ else {
125
+ search(this.def, query);
126
+ }
120
127
  }
121
128
  // @ts-ignore
122
129
  return this;
@@ -128,7 +135,9 @@ export class QueryBranch {
128
135
  args: [field],
129
136
  });
130
137
  }
131
- groupBy(this.def, field);
138
+ else {
139
+ groupBy(this.def, field);
140
+ }
132
141
  // only works with aggregates for now
133
142
  // @ts-ignore
134
143
  return this;
@@ -140,19 +149,26 @@ export class QueryBranch {
140
149
  args: [field],
141
150
  });
142
151
  }
143
- const p = field.split('.');
144
- addAggregate(2 /* AggregateType.COUNT */, this.def, p);
152
+ else {
153
+ const p = field.split('.');
154
+ addAggregate(2 /* AggregateType.COUNT */, this.def, p);
155
+ }
145
156
  // @ts-ignore
146
157
  return this;
147
158
  }
148
159
  sum(...fields) {
160
+ if (fields.length === 0) {
161
+ throw new Error('Empty sum() called');
162
+ }
149
163
  if (this.queryCommands) {
150
164
  this.queryCommands.push({
151
165
  method: 'sum',
152
166
  args: fields,
153
167
  });
154
168
  }
155
- addAggregate(1 /* AggregateType.SUM */, this.def, fields);
169
+ else {
170
+ addAggregate(1 /* AggregateType.SUM */, this.def, fields);
171
+ }
156
172
  // @ts-ignore
157
173
  return this;
158
174
  }
@@ -163,15 +179,17 @@ export class QueryBranch {
163
179
  args: [field, operator, value, opts],
164
180
  });
165
181
  }
166
- if (typeof field === 'function') {
167
- const f = new FilterBranch(this.db, filterOr(this.db, this.def, [], this.def.filter), this.def);
168
- field(f);
169
- this.def.filter.size += f.filterBranch.size;
170
- }
171
182
  else {
172
- const f = convertFilter(this.def, field, operator, value, opts);
173
- if (f) {
174
- filterOr(this.db, this.def, f, this.def.filter);
183
+ if (typeof field === 'function') {
184
+ const f = new FilterBranch(this.db, filterOr(this.db, this.def, [], this.def.filter), this.def);
185
+ field(f);
186
+ this.def.filter.size += f.filterBranch.size;
187
+ }
188
+ else {
189
+ const f = convertFilter(this.def, field, operator, value, opts);
190
+ if (f) {
191
+ filterOr(this.db, this.def, f, this.def.filter);
192
+ }
175
193
  }
176
194
  }
177
195
  // @ts-ignore
@@ -181,16 +199,18 @@ export class QueryBranch {
181
199
  if (this.queryCommands) {
182
200
  this.queryCommands.push({ method: 'range', args: [start, end] });
183
201
  }
184
- const offset = start;
185
- const limit = end - start;
186
- if (validateRange(this.def, offset, limit)) {
187
- this.def.range.offset = 0;
188
- this.def.range.limit = DEF_RANGE_PROP_LIMIT;
189
- // @ts-ignore
190
- return this;
202
+ else {
203
+ const offset = start;
204
+ const limit = end - start;
205
+ if (validateRange(this.def, offset, limit)) {
206
+ this.def.range.offset = 0;
207
+ this.def.range.limit = DEF_RANGE_PROP_LIMIT;
208
+ // @ts-ignore
209
+ return this;
210
+ }
211
+ this.def.range.offset = offset;
212
+ this.def.range.limit = limit;
191
213
  }
192
- this.def.range.offset = offset;
193
- this.def.range.limit = limit;
194
214
  // @ts-ignore
195
215
  return this;
196
216
  }
@@ -198,49 +218,51 @@ export class QueryBranch {
198
218
  if (this.queryCommands) {
199
219
  this.queryCommands.push({ method: 'include', args: fields });
200
220
  }
201
- for (const f of fields) {
202
- if (typeof f === 'string') {
203
- includeField(this.def, f);
204
- }
205
- else if (typeof f === 'function') {
206
- f((field) => {
207
- if (field[0] == '$') {
208
- // @ts-ignore
209
- const prop = this.def.target?.propDef?.edges[field];
210
- if (prop &&
211
- (prop.typeIndex === REFERENCE || prop.typeIndex === REFERENCES)) {
212
- const refDef = createOrGetEdgeRefQueryDef(this.db, this.def, prop);
221
+ else {
222
+ for (const f of fields) {
223
+ if (typeof f === 'string') {
224
+ includeField(this.def, f);
225
+ }
226
+ else if (typeof f === 'function') {
227
+ f((field) => {
228
+ if (field[0] == '$') {
213
229
  // @ts-ignore
214
- return new QueryBranch(this.db, refDef);
230
+ const prop = this.def.target?.propDef?.edges[field];
231
+ if (prop &&
232
+ (prop.typeIndex === REFERENCE || prop.typeIndex === REFERENCES)) {
233
+ const refDef = createOrGetEdgeRefQueryDef(this.db, this.def, prop);
234
+ // @ts-ignore
235
+ return new QueryBranch(this.db, refDef);
236
+ }
237
+ throw new Error(`No edge reference or edge references field named "${field}"`);
215
238
  }
216
- throw new Error(`No edge reference or edge references field named "${field}"`);
239
+ else {
240
+ const prop = field[0] == '$'
241
+ ? // @ts-ignore
242
+ this.def.target?.propDef?.edges[field]
243
+ : this.def.props[field];
244
+ if (prop &&
245
+ (prop.typeIndex === REFERENCE || prop.typeIndex === REFERENCES)) {
246
+ const refDef = createOrGetRefQueryDef(this.db, this.def, prop);
247
+ // @ts-ignore
248
+ return new QueryBranch(this.db, refDef);
249
+ }
250
+ throw new Error(`No reference or references field named "${field}"`);
251
+ }
252
+ });
253
+ }
254
+ else if (Array.isArray(f)) {
255
+ if (f.length === 0) {
256
+ includeFields(this.def, ['id']);
217
257
  }
218
258
  else {
219
- const prop = field[0] == '$'
220
- ? // @ts-ignore
221
- this.def.target?.propDef?.edges[field]
222
- : this.def.props[field];
223
- if (prop &&
224
- (prop.typeIndex === REFERENCE || prop.typeIndex === REFERENCES)) {
225
- const refDef = createOrGetRefQueryDef(this.db, this.def, prop);
226
- // @ts-ignore
227
- return new QueryBranch(this.db, refDef);
228
- }
229
- throw new Error(`No reference or references field named "${field}"`);
259
+ includeFields(this.def, f);
230
260
  }
231
- });
232
- }
233
- else if (Array.isArray(f)) {
234
- if (f.length === 0) {
235
- includeFields(this.def, ['id']);
236
261
  }
237
- else {
238
- includeFields(this.def, f);
262
+ else if (f !== undefined) {
263
+ throw new Error('Invalid include statement: expected props, refs and edges (string or array) or function');
239
264
  }
240
265
  }
241
- else if (f !== undefined) {
242
- throw new Error('Invalid include statement: expected props, refs and edges (string or array) or function');
243
- }
244
266
  }
245
267
  // @ts-ignore
246
268
  return this;
@@ -283,35 +305,26 @@ export class BasedDbQuery extends QueryBranch {
283
305
  }
284
306
  }
285
307
  }
286
- if (!db.schema) {
287
- throw new Error('Query: No schema yet - use await db.schemaIsSet()');
288
- }
289
- const def = createQueryDef(db, QueryDefType.Root, target, skipValidation);
290
- def.schemaChecksum = db.schema?.hash || 0;
291
- super(db, def);
308
+ // const def = createQueryDef(db, QueryDefType.Root, target, skipValidation)
309
+ // def.schemaChecksum = db.schema?.hash || 0
310
+ // def: QueryDef
311
+ super(db);
292
312
  this.db = db;
293
313
  this.skipValidation = skipValidation;
294
314
  this.queryCommands = [];
295
315
  this.target = target;
296
316
  }
297
- reBuildQuery() {
317
+ reset() {
298
318
  this.id = undefined;
299
319
  this.buffer = undefined;
300
- const def = createQueryDef(this.db, QueryDefType.Root, this.target, this.skipValidation);
301
- def.schemaChecksum = this.db.schema?.hash || 0;
302
- this.def = def;
303
- const q = this.queryCommands;
304
- this.queryCommands = [];
305
- for (const command of q) {
306
- this[command.method](...command.args);
307
- }
320
+ this.def = undefined;
308
321
  }
309
322
  #getInternal = async (resolve, reject) => {
310
- if (!this.def.include.stringFields.size && !this.def.references.size) {
311
- includeField(this.def, '*');
312
- }
313
323
  let buf;
314
324
  try {
325
+ if (!this.db.schema) {
326
+ await this.db.once('schema');
327
+ }
315
328
  buf = registerQuery(this);
316
329
  }
317
330
  catch (err) {
@@ -321,14 +334,14 @@ export class BasedDbQuery extends QueryBranch {
321
334
  const d = performance.now();
322
335
  await this.db.isModified();
323
336
  if (this.db.schema?.hash !== this.def.schemaChecksum) {
324
- this.reBuildQuery();
337
+ this.reset();
325
338
  return this.#getInternal(resolve, reject);
326
339
  }
327
340
  const res = await this.db.hooks.getQueryBuf(buf);
328
341
  if (res.byteLength === 1) {
329
342
  if (res[0] === 0) {
330
343
  if (this.db.schema?.hash !== this.def.schemaChecksum) {
331
- this.reBuildQuery();
344
+ this.reset();
332
345
  return this.#getInternal(resolve, reject);
333
346
  }
334
347
  reject(new Error('schema mismatch'));
@@ -354,8 +367,16 @@ export class BasedDbQuery extends QueryBranch {
354
367
  registerQuery(this);
355
368
  }
356
369
  locale(locale) {
357
- validateLocale(this.def, locale);
358
- this.def.lang = langCodesMap.get(locale) ?? 0;
370
+ if (this.queryCommands) {
371
+ this.queryCommands.push({
372
+ method: 'locale',
373
+ args: [locale],
374
+ });
375
+ }
376
+ else {
377
+ validateLocale(this.def, locale);
378
+ this.def.lang = langCodesMap.get(locale) ?? 0;
379
+ }
359
380
  return this;
360
381
  }
361
382
  subscribe(onData, onError) {
@@ -377,20 +398,10 @@ export class BasedDbQuery extends QueryBranch {
377
398
  }));
378
399
  }
379
400
  _getSync(dbCtxExternal) {
380
- if (!this.def.include.stringFields.size && !this.def.references.size) {
381
- includeField(this.def, '*');
382
- }
383
401
  const buf = registerQuery(this);
384
402
  const d = performance.now();
385
403
  const res = native.getQueryBuf(buf, dbCtxExternal);
386
404
  return new BasedQueryResponse(this.id, this.def, new Uint8Array(res), performance.now() - d);
387
405
  }
388
- toBuffer() {
389
- if (!this.def.include.stringFields.size && !this.def.references.size) {
390
- includeField(this.def, '*');
391
- }
392
- const b = defToBuffer(this.db, this.def);
393
- return concatUint8Arr(b);
394
- }
395
406
  }
396
407
  //# sourceMappingURL=BasedDbQuery.js.map
@@ -131,6 +131,9 @@ export const isRootCountOnly = (def, filterSize) => {
131
131
  if (aggs[0].type !== 2 /* AggregateType.COUNT */) {
132
132
  return false;
133
133
  }
134
+ if (def.filter && def.filter.size > 0) {
135
+ return false;
136
+ }
134
137
  return true;
135
138
  };
136
139
  //# sourceMappingURL=aggregation.js.map
@@ -2,8 +2,24 @@ import native from '../../native.js';
2
2
  import { concatUint8Arr } from '@saulx/utils';
3
3
  import { defToBuffer } from './toByteCode/toBuffer.js';
4
4
  import { handleErrors } from './validation.js';
5
+ import { createQueryDef } from './queryDef.js';
6
+ import { QueryDefType } from './types.js';
7
+ import { includeField } from './query.js';
5
8
  export const registerQuery = (q) => {
6
9
  if (!q.id) {
10
+ const commands = q.queryCommands;
11
+ q.queryCommands = null;
12
+ const def = createQueryDef(q.db, QueryDefType.Root, q.target, q.skipValidation);
13
+ def.schemaChecksum = q.db.schema?.hash || 0;
14
+ q.def = def;
15
+ // proposal:
16
+ for (const command of commands) {
17
+ q[command.method](...command.args);
18
+ }
19
+ if (!q.def.include.stringFields.size && !q.def.references.size) {
20
+ includeField(q.def, '*');
21
+ }
22
+ q.queryCommands = commands;
7
23
  const b = defToBuffer(q.db, q.def);
8
24
  const buf = concatUint8Arr(b);
9
25
  let id = native.crc32(buf);
@@ -10,5 +10,5 @@ export declare class SubStore {
10
10
  subscribe(q: BasedDbQuery): void;
11
11
  resubscribe(q: BasedDbQuery): void;
12
12
  }
13
- export declare const subscribe: (q: BasedDbQuery, onData: OnData, onError?: OnError) => OnClose;
13
+ export declare const subscribe: (q: BasedDbQuery, onData: OnData, onError: OnError) => OnClose;
14
14
  export * from './types.js';
@@ -1,5 +1,4 @@
1
1
  import { BasedQueryResponse } from '../BasedIterable.js';
2
- import { includeField } from '../query.js';
3
2
  import { registerQuery } from '../registerQuery.js';
4
3
  export class SubStore {
5
4
  listeners;
@@ -8,11 +7,7 @@ export class SubStore {
8
7
  checksum;
9
8
  len;
10
9
  subscribe(q) {
11
- if (!q.def.include.stringFields.size && !q.def.references.size) {
12
- includeField(q.def, '*');
13
- }
14
- registerQuery(q);
15
- this.onClose = q.db.hooks.subscribe(q, (res) => {
10
+ const onData = (res) => {
16
11
  if (!this.response) {
17
12
  this.response = new BasedQueryResponse(q.id, q.def, res, 0);
18
13
  }
@@ -29,15 +24,43 @@ export class SubStore {
29
24
  this.len = len;
30
25
  this.checksum = checksum;
31
26
  }
32
- }, (err) => {
27
+ };
28
+ const onError = (err) => {
33
29
  for (const [, onError] of this.listeners) {
34
30
  onError(err);
35
31
  }
36
- });
32
+ };
33
+ let killed = false;
34
+ if (!q.db.schema) {
35
+ q.db.schemaIsSet().then(() => {
36
+ if (!killed) {
37
+ try {
38
+ registerQuery(q);
39
+ this.onClose = q.db.hooks.subscribe(q, onData, onError);
40
+ }
41
+ catch (err) {
42
+ onError(err);
43
+ }
44
+ }
45
+ });
46
+ this.onClose = () => {
47
+ killed = true;
48
+ };
49
+ }
50
+ else {
51
+ try {
52
+ registerQuery(q);
53
+ this.onClose = q.db.hooks.subscribe(q, onData, onError);
54
+ }
55
+ catch (err) {
56
+ onError(err);
57
+ this.onClose = () => { };
58
+ }
59
+ }
37
60
  }
38
61
  resubscribe(q) {
39
62
  this.onClose();
40
- q.reBuildQuery();
63
+ q.reset();
41
64
  this.response = null;
42
65
  this.subscribe(q);
43
66
  }
@@ -0,0 +1,3 @@
1
+ import { DbClient } from '../index.js';
2
+ import { DbSchema } from '../schema.js';
3
+ export declare const setLocalClientSchema: (client: DbClient, schema: DbSchema) => DbSchema;
@@ -0,0 +1,30 @@
1
+ import { schemaToSelvaBuffer, updateTypeDefs } from '@based/schema/def';
2
+ import { execCtxQueue } from './flushModify.js';
3
+ export const setLocalClientSchema = (client, schema) => {
4
+ if (client.schema && client.schema.hash === schema.hash) {
5
+ return client.schema;
6
+ }
7
+ const { schemaTypesParsed, schemaTypesParsedById } = updateTypeDefs(schema);
8
+ client.schema = schema;
9
+ client.schemaTypesParsed = schemaTypesParsed;
10
+ client.schemaTypesParsedById = schemaTypesParsedById;
11
+ // Adds bidrectional refs on defs
12
+ schemaToSelvaBuffer(client.schemaTypesParsed);
13
+ // this has to happen before the listeners
14
+ if (client.modifyCtx.len > 8) {
15
+ console.info('Modify cancelled - schema updated');
16
+ }
17
+ // cancel modify queue
18
+ const resCtx = client.modifyCtx.ctx;
19
+ client.modifyCtx.reset();
20
+ execCtxQueue(resCtx, true);
21
+ // resubscribe
22
+ for (const [q, store] of client.subs) {
23
+ store.resubscribe(q);
24
+ }
25
+ process.nextTick(() => {
26
+ client.emit('schema', schema);
27
+ });
28
+ return schema;
29
+ };
30
+ //# sourceMappingURL=setLocalClientSchema.js.map
@@ -1,16 +1,17 @@
1
1
  import { StrictSchema } from '@based/schema';
2
2
  import { BasedDbQuery } from './client/query/BasedDbQuery.js';
3
- import { OnError } from './client/query/subscription/types.js';
3
+ import { OnClose, OnData, OnError } from './client/query/subscription/types.js';
4
4
  import { DbServer } from './server/index.js';
5
- export declare const getDefaultHooks: (server: DbServer, subInterval?: number) => {
6
- subscribe(q: BasedDbQuery, onData: (res: Uint8Array) => void, onError: OnError): () => void;
7
- setSchema(schema: StrictSchema, fromStart: boolean): Promise<StrictSchema & {
8
- lastId: number;
9
- hash?: number;
10
- }>;
5
+ import { TransformFns } from './server/migrate/index.js';
6
+ import { DbSchema, SchemaChecksum } from './schema.js';
7
+ export type DbClientHooks = {
8
+ setSchema(schema: StrictSchema, transformFns?: TransformFns): Promise<SchemaChecksum>;
11
9
  flushModify(buf: Uint8Array): Promise<{
12
10
  offsets: Record<number, number>;
13
- dbWriteTime: number;
11
+ dbWriteTime?: number;
14
12
  }>;
15
13
  getQueryBuf(buf: Uint8Array): Promise<Uint8Array>;
14
+ subscribe(q: BasedDbQuery, onData: (buf: Uint8Array) => ReturnType<OnData>, onError?: OnError): OnClose;
15
+ subscribeSchema(cb: (schema: DbSchema) => void): void;
16
16
  };
17
+ export declare const getDefaultHooks: (server: DbServer, subInterval?: number) => DbClientHooks;
package/dist/src/hooks.js CHANGED
@@ -32,8 +32,16 @@ export const getDefaultHooks = (server, subInterval = 200) => {
32
32
  killed = true;
33
33
  };
34
34
  },
35
- setSchema(schema, fromStart) {
36
- return Promise.resolve(server.setSchema(schema, fromStart));
35
+ setSchema(schema) {
36
+ return server.setSchema(schema);
37
+ },
38
+ subscribeSchema(setSchema) {
39
+ if (server.schema) {
40
+ setSchema(server.schema);
41
+ }
42
+ server.on('schema', (schema) => {
43
+ setSchema(schema);
44
+ });
37
45
  },
38
46
  flushModify(buf) {
39
47
  const d = performance.now();
@@ -3,6 +3,7 @@ import { ModifyCtx } from './client/flushModify.js';
3
3
  import { DbServer } from './server/index.js';
4
4
  import { DbClient } from './client/index.js';
5
5
  import { getDefaultHooks } from './hooks.js';
6
+ import { Emitter } from './shared/Emitter.js';
6
7
  export * from './client/modify/modify.js';
7
8
  export { compress, decompress };
8
9
  export { ModifyCtx };
@@ -16,9 +17,9 @@ export * from './client/query/query.js';
16
17
  export * from './client/query/BasedDbQuery.js';
17
18
  export * from './client/query/BasedIterable.js';
18
19
  export * from './server/save.js';
20
+ export * from './hooks.js';
19
21
  export { getDefaultHooks };
20
- export declare class BasedDb {
21
- #private;
22
+ export declare class BasedDb extends Emitter {
22
23
  client: DbClient;
23
24
  server: DbServer;
24
25
  fileSystemPath: string;
@@ -36,17 +37,15 @@ export declare class BasedDb {
36
37
  delete: DbClient['delete'];
37
38
  expire: DbClient['expire'];
38
39
  query: DbClient['query'];
40
+ schemaIsSet: DbClient['schemaIsSet'];
39
41
  setSchema: DbClient['setSchema'];
40
42
  putSchema: DbClient['setSchema'];
41
43
  drain: DbClient['drain'];
42
44
  start: DbServer['start'];
43
45
  stop: DbServer['stop'];
44
46
  save: DbServer['save'];
45
- migrateSchema: DbServer['migrateSchema'];
46
47
  isModified: DbClient['isModified'];
47
- schemaIsSet: DbClient['schemaIsSet'];
48
48
  destroy(): Promise<void>;
49
- wipe(): Promise<void>;
50
- on: DbClient['on'];
51
- off: DbClient['off'];
49
+ on(): any;
50
+ off(): any;
52
51
  }