@based/db 0.0.61 → 0.0.63

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
@@ -11,6 +11,7 @@ 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';
14
15
  import { displayTarget } from './display.js';
15
16
  import picocolors from 'picocolors';
16
17
  export class QueryBranch {
@@ -305,9 +306,6 @@ export class BasedDbQuery extends QueryBranch {
305
306
  }
306
307
  }
307
308
  }
308
- // const def = createQueryDef(db, QueryDefType.Root, target, skipValidation)
309
- // def.schemaChecksum = db.schema?.hash || 0
310
- // def: QueryDef
311
309
  super(db);
312
310
  this.db = db;
313
311
  this.skipValidation = skipValidation;
@@ -340,17 +338,13 @@ export class BasedDbQuery extends QueryBranch {
340
338
  const res = await this.db.hooks.getQueryBuf(buf);
341
339
  if (res.byteLength === 1) {
342
340
  if (res[0] === 0) {
343
- if (this.def && this.db.schema?.hash !== this.def.schemaChecksum) {
344
- this.reset();
345
- this.db.emit('info', 'query get schema mismatch - awaiting new schema');
346
- await this.db.once('schema');
347
- return this.#getInternal(resolve, reject);
348
- }
349
- else {
350
- this.db.emit('info', 'query get schema mismatch - got the same already');
351
- this.reset();
352
- return this.#getInternal(resolve, reject);
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'));
353
346
  }
347
+ return this.#getInternal(resolve, reject);
354
348
  }
355
349
  else {
356
350
  reject(new Error('unexpected error'));
@@ -1,6 +1,7 @@
1
1
  import { MessageChannel, Worker } from 'node:worker_threads';
2
2
  import { fileURLToPath } from 'node:url';
3
3
  import { dirname, join } from 'node:path';
4
+ import { readUint64 } from '@saulx/utils';
4
5
  const __filename = fileURLToPath(import.meta.url);
5
6
  const __dirname = dirname(__filename);
6
7
  const workerPath = join(__dirname, 'worker.js');
@@ -60,6 +61,10 @@ export class DbWorker {
60
61
  return new Promise(this.callback);
61
62
  }
62
63
  getQueryBuf(buf) {
64
+ const schemaChecksum = readUint64(buf, buf.byteLength - 8);
65
+ if (schemaChecksum !== this.db.schema?.hash) {
66
+ return Promise.resolve(new Uint8Array(1));
67
+ }
63
68
  this.channel.postMessage(buf);
64
69
  return new Promise(this.callback);
65
70
  }
@@ -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,11 @@ export class DbServer extends DbShared {
334
333
  resolve(new Error('Query queue exceeded'));
335
334
  return;
336
335
  }
336
+ // TODO should we check here as well? Already will check in DbWorker
337
+ const schemaChecksum = readUint64(buf, buf.byteLength - 8);
338
+ if (schemaChecksum !== this.schema?.hash) {
339
+ return Promise.resolve(new Uint8Array(1));
340
+ }
337
341
  this.queryQueue.set(resolve, buf);
338
342
  }
339
343
  getQueryBuf(buf, fromQueue = false) {
@@ -341,10 +345,6 @@ export class DbServer extends DbShared {
341
345
  console.error('Db is stopped - trying to query', buf.byteLength);
342
346
  return Promise.resolve(new Uint8Array(8));
343
347
  }
344
- const schemaChecksum = readUint64(buf, buf.byteLength - 8);
345
- if (schemaChecksum !== this.schema?.hash) {
346
- return Promise.resolve(new Uint8Array(1));
347
- }
348
348
  if (this.modifyQueue.length) {
349
349
  return new Promise((resolve) => {
350
350
  this.addToQueryQueue(resolve, buf);
@@ -393,7 +393,13 @@ export class DbServer extends DbShared {
393
393
  if (this.modifyQueue.length) {
394
394
  const modifyQueue = this.modifyQueue;
395
395
  this.modifyQueue = [];
396
- for (const buf of modifyQueue) {
396
+ for (const bufWithHash of modifyQueue) {
397
+ const schemaHash = readUint64(bufWithHash, 0);
398
+ if (schemaHash !== this.schema?.hash) {
399
+ this.emit('info', 'Schema mismatch in modify');
400
+ return null;
401
+ }
402
+ const buf = bufWithHash.subarray(8);
397
403
  this.#modify(buf);
398
404
  }
399
405
  }
@@ -437,13 +443,15 @@ export class DbServer extends DbShared {
437
443
  }
438
444
  async destroy() {
439
445
  await this.stop(true);
440
- await rm(this.fileSystemPath, { recursive: true }).catch((err) => {
441
- // console.warn(
442
- // 'Error removing dump folder',
443
- // this.fileSystemPath,
444
- // err.message,
445
- // ),
446
- });
446
+ if (this.fileSystemPath) {
447
+ await rm(this.fileSystemPath, { recursive: true }).catch((err) => {
448
+ // console.warn(
449
+ // 'Error removing dump folder',
450
+ // this.fileSystemPath,
451
+ // err.message,
452
+ // ),
453
+ });
454
+ }
447
455
  }
448
456
  }
449
457
  //# sourceMappingURL=index.js.map
@@ -55,8 +55,7 @@ export const migrate = async (server, fromSchema, toSchema, transform) => {
55
55
  return;
56
56
  }
57
57
  setSchemaOnServer(tmpDb.server, toSchema);
58
- // await writeSchemaFile(this, toSchema)
59
- await setNativeSchema(tmpDb.server, toSchema);
58
+ setNativeSchema(tmpDb.server, toSchema);
60
59
  if (abort()) {
61
60
  await tmpDb.destroy();
62
61
  return;
@@ -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) => {
@@ -3,5 +3,5 @@ import { DbSchema } from '../schema.js';
3
3
  import { StrictSchema } from '@based/schema';
4
4
  export declare const setSchemaOnServer: (server: DbServer, schema: DbSchema) => void;
5
5
  export declare const writeSchemaFile: (server: DbServer, schema: DbSchema) => Promise<void>;
6
- export declare const setNativeSchema: (server: DbServer, schema: DbSchema) => Promise<void>;
6
+ export declare const setNativeSchema: (server: DbServer, schema: DbSchema) => void;
7
7
  export declare const strictSchemaToDbSchema: (schema: StrictSchema) => DbSchema;
@@ -22,7 +22,7 @@ export const writeSchemaFile = async (server, schema) => {
22
22
  throw new Error(`Error writing schema to a file path ${schemaFilePath}}`);
23
23
  }
24
24
  };
25
- export const setNativeSchema = async (server, schema) => {
25
+ export const setNativeSchema = (server, schema) => {
26
26
  const types = Object.keys(server.schemaTypesParsed);
27
27
  const s = schemaToSelvaBuffer(server.schemaTypesParsed);
28
28
  for (let i = 0; i < s.length; i++) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@based/db",
3
- "version": "0.0.61",
3
+ "version": "0.0.63",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "./dist/src/index.js",