@based/db 0.0.62 → 0.0.64

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.
Binary file
Binary file
@@ -169,12 +169,12 @@ export const startDrain = (db) => {
169
169
  db.isDraining = true;
170
170
  if (db.flushTime === 0) {
171
171
  process.nextTick(() => {
172
- flushBuffer(db);
172
+ void flushBuffer(db);
173
173
  });
174
174
  }
175
175
  else {
176
176
  setTimeout(() => {
177
- flushBuffer(db);
177
+ void flushBuffer(db);
178
178
  }, db.flushTime);
179
179
  }
180
180
  };
@@ -145,7 +145,7 @@ export function create(db, type, obj, opts) {
145
145
  if (pos === 8) {
146
146
  throw new Error('!No range available');
147
147
  }
148
- flushBuffer(db);
148
+ void flushBuffer(db);
149
149
  return db.create(type, obj, opts);
150
150
  }
151
151
  res.error = err;
@@ -15,7 +15,7 @@ export const deleteFn = (db, type, id, opts) => {
15
15
  if (separate) {
16
16
  const size = 11 /* SIZE.DEFAULT_CURSOR */ + 2 + separate.length * 12;
17
17
  if (ctx.len + size > ctx.max) {
18
- flushBuffer(db);
18
+ void flushBuffer(db);
19
19
  return deleteFn(db, type, id);
20
20
  }
21
21
  setCursor(ctx, schema, 0, MICRO_BUFFER, id, UPDATE);
@@ -28,7 +28,7 @@ export const deleteFn = (db, type, id, opts) => {
28
28
  }
29
29
  else {
30
30
  if (ctx.len + 11 /* SIZE.DEFAULT_CURSOR */ + 2 > ctx.max) {
31
- flushBuffer(db);
31
+ void flushBuffer(db);
32
32
  return deleteFn(db, type, id);
33
33
  }
34
34
  setCursor(ctx, schema, 0, MICRO_BUFFER, id, UPDATE);
@@ -9,7 +9,7 @@ export function expire(db, type, id, seconds) {
9
9
  }
10
10
  const ctx = db.modifyCtx;
11
11
  if (ctx.len + 11 /* SIZE.DEFAULT_CURSOR */ + 5 > ctx.max) {
12
- flushBuffer(db);
12
+ void flushBuffer(db);
13
13
  return expire(db, type, id, seconds);
14
14
  }
15
15
  setCursor(ctx, def, 0, MICRO_BUFFER, id, EXPIRE);
@@ -70,7 +70,7 @@ export const update = (db, type, id, obj, opts) => {
70
70
  if (pos === 8) {
71
71
  throw new Error('out of range');
72
72
  }
73
- flushBuffer(db);
73
+ void flushBuffer(db);
74
74
  return update(db, type, id, obj, opts);
75
75
  }
76
76
  res.error = err;
@@ -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 { wait } from '@saulx/utils';
15
14
  import { displayTarget } from './display.js';
16
15
  import picocolors from 'picocolors';
17
16
  export class QueryBranch {
@@ -338,13 +337,19 @@ export class BasedDbQuery extends QueryBranch {
338
337
  const res = await this.db.hooks.getQueryBuf(buf);
339
338
  if (res.byteLength === 1) {
340
339
  if (res[0] === 0) {
341
- this.reset();
342
- this.db.emit('info', 'query get schema mismatch - awaiting new schema (max 15s)');
343
- const ok = await Promise.race([wait(15e3), this.db.once('schema')]);
344
- if (!ok) {
345
- reject(new Error('schema mismath'));
340
+ if (this.def && this.def.schemaChecksum === this.db.schema?.hash) {
341
+ // my schema did not change since last time, wait for the schema to change
342
+ this.reset();
343
+ this.db.emit('info', 'query get schema mismatch - awaiting new schema');
344
+ await this.db.once('schema');
345
+ return this.#getInternal(resolve, reject);
346
+ }
347
+ else {
348
+ // its changed so lets send again
349
+ this.db.emit('info', 'query get schema mismatch - got the same already');
350
+ this.reset();
351
+ return this.#getInternal(resolve, reject);
346
352
  }
347
- return this.#getInternal(resolve, reject);
348
353
  }
349
354
  else {
350
355
  reject(new Error('unexpected error'));
@@ -32,7 +32,9 @@ export class SubStore {
32
32
  };
33
33
  let killed = false;
34
34
  if (!q.db.schema) {
35
- q.db.schemaIsSet().then(() => {
35
+ q.db
36
+ .schemaIsSet()
37
+ .then(() => {
36
38
  if (!killed) {
37
39
  try {
38
40
  registerQuery(q);
@@ -42,6 +44,9 @@ export class SubStore {
42
44
  onError(err);
43
45
  }
44
46
  }
47
+ })
48
+ .catch((err) => {
49
+ onError(err);
45
50
  });
46
51
  this.onClose = () => {
47
52
  killed = true;
package/dist/src/hooks.js CHANGED
@@ -25,7 +25,7 @@ export const getDefaultHooks = (server, subInterval = 200) => {
25
25
  }
26
26
  timer = setTimeout(poll, subInterval);
27
27
  };
28
- poll();
28
+ void poll();
29
29
  return () => {
30
30
  clearTimeout(timer);
31
31
  killed = true;
@@ -29,7 +29,9 @@ export class DbWorker {
29
29
  });
30
30
  this.worker.on('error', (err) => {
31
31
  console.error('error in query worker:', err);
32
- this.worker.terminate();
32
+ this.worker.terminate().catch((err) => {
33
+ console.error('error terminating query worker:', err);
34
+ });
33
35
  });
34
36
  this.worker.on('exit', (code) => {
35
37
  if (!this.db.stopped) {
@@ -67,10 +67,10 @@ export declare class DbServer extends DbShared {
67
67
  getSortIndex(typeId: number, field: number, start: number, lang: number): SortIndex;
68
68
  createSortIndexBuffer(typeId: number, field: number, start: number, lang: number): SortIndex;
69
69
  setSchema(strictSchema: StrictSchema, transformFns?: TransformFns): Promise<SchemaChecksum>;
70
- modify(buf: Uint8Array): Record<number, number> | null;
71
- addToQueryQueue(resolve: any, buf: any): void;
70
+ modify(bufWithHash: Uint8Array): Record<number, number> | null;
71
+ addToQueryQueue(resolve: any, buf: any): Promise<Uint8Array>;
72
72
  getQueryBuf(buf: Uint8Array, fromQueue?: boolean): Promise<Uint8Array>;
73
- onQueryEnd(): void;
73
+ onQueryEnd(): any;
74
74
  stop(noSave?: boolean): Promise<void>;
75
75
  destroy(): Promise<void>;
76
76
  }
@@ -242,14 +242,13 @@ export class DbServer extends DbShared {
242
242
  });
243
243
  return schema.hash;
244
244
  }
245
- modify(buf) {
246
- const schemaHash = readUint64(buf, 0);
247
- // if !schema
245
+ modify(bufWithHash) {
246
+ const schemaHash = readUint64(bufWithHash, 0);
248
247
  if (schemaHash !== this.schema?.hash) {
249
248
  this.emit('info', 'Schema mismatch in modify');
250
249
  return null;
251
250
  }
252
- buf = buf.subarray(8);
251
+ const buf = bufWithHash.subarray(8);
253
252
  const offsets = {};
254
253
  const dataLen = readUint32(buf, buf.length - 4);
255
254
  let typesSize = readUint16(buf, dataLen);
@@ -277,7 +276,7 @@ export class DbServer extends DbShared {
277
276
  offsets[typeId] = offset;
278
277
  }
279
278
  if (this.processingQueries) {
280
- this.modifyQueue.push(new Uint8Array(buf));
279
+ this.modifyQueue.push(new Uint8Array(bufWithHash));
281
280
  }
282
281
  else {
283
282
  this.#modify(buf);
@@ -334,6 +333,10 @@ export class DbServer extends DbShared {
334
333
  resolve(new Error('Query queue exceeded'));
335
334
  return;
336
335
  }
336
+ const schemaChecksum = readUint64(buf, buf.byteLength - 8);
337
+ if (schemaChecksum !== this.schema?.hash) {
338
+ return Promise.resolve(new Uint8Array(1));
339
+ }
337
340
  this.queryQueue.set(resolve, buf);
338
341
  }
339
342
  getQueryBuf(buf, fromQueue = false) {
@@ -341,10 +344,6 @@ export class DbServer extends DbShared {
341
344
  console.error('Db is stopped - trying to query', buf.byteLength);
342
345
  return Promise.resolve(new Uint8Array(8));
343
346
  }
344
- const schemaChecksum = readUint64(buf, buf.byteLength - 8);
345
- if (schemaChecksum !== this.schema?.hash) {
346
- return Promise.resolve(new Uint8Array(1));
347
- }
348
347
  if (this.modifyQueue.length) {
349
348
  return new Promise((resolve) => {
350
349
  this.addToQueryQueue(resolve, buf);
@@ -393,7 +392,13 @@ export class DbServer extends DbShared {
393
392
  if (this.modifyQueue.length) {
394
393
  const modifyQueue = this.modifyQueue;
395
394
  this.modifyQueue = [];
396
- for (const buf of modifyQueue) {
395
+ for (const bufWithHash of modifyQueue) {
396
+ const schemaHash = readUint64(bufWithHash, 0);
397
+ if (schemaHash !== this.schema?.hash) {
398
+ this.emit('info', 'Schema mismatch in modify');
399
+ return null;
400
+ }
401
+ const buf = bufWithHash.subarray(8);
397
402
  this.#modify(buf);
398
403
  }
399
404
  }
@@ -113,7 +113,7 @@ export const migrate = async (server, fromSchema, toSchema, transform) => {
113
113
  if (server.dirtyRanges.size) {
114
114
  rangesToMigrate = [];
115
115
  i = 0;
116
- foreachDirtyBlock(server, (_mtKey, typeId, start, end) => {
116
+ void foreachDirtyBlock(server, (_mtKey, typeId, start, end) => {
117
117
  rangesToMigrate.push({
118
118
  typeId,
119
119
  start,
@@ -13,6 +13,7 @@ else if (workerData?.isDbMigrateWorker) {
13
13
  const { from, to, fromSchema, toSchema, channel, workerState, transformFns } = workerData;
14
14
  const fromCtx = native.externalFromInt(from);
15
15
  const toCtx = native.externalFromInt(to);
16
+ native.workerCtxInit();
16
17
  const fromDb = new BasedDb({ path: null });
17
18
  const toDb = new BasedDb({ path: null });
18
19
  const cp = (obj) => {
@@ -69,7 +69,7 @@ export function save(db, sync = false, forceFullDump = false, skipMigrationCheck
69
69
  }
70
70
  }
71
71
  else {
72
- foreachDirtyBlock(db, (mtKey, typeId, start, end) => {
72
+ void foreachDirtyBlock(db, (mtKey, typeId, start, end) => {
73
73
  const hash = new Uint8Array(16);
74
74
  const file = saveRange(db, typeId, start, end, hash);
75
75
  if (file === null) {
@@ -124,7 +124,7 @@ export async function start(db, opts) {
124
124
  // use timeout
125
125
  if (db.saveIntervalInSeconds > 0) {
126
126
  db.saveInterval ??= setInterval(() => {
127
- save(db);
127
+ void save(db);
128
128
  }, db.saveIntervalInSeconds * 1e3);
129
129
  }
130
130
  if (db.schema) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@based/db",
3
- "version": "0.0.62",
3
+ "version": "0.0.64",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "./dist/src/index.js",