@based/db 0.0.56 → 0.0.58

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
@@ -158,7 +158,6 @@ export function create(db, type, obj, opts) {
158
158
  }
159
159
  if (id > def.lastId) {
160
160
  def.lastId = id;
161
- def.total++;
162
161
  }
163
162
  // @ts-ignore
164
163
  return res;
@@ -340,11 +340,17 @@ export class BasedDbQuery extends QueryBranch {
340
340
  const res = await this.db.hooks.getQueryBuf(buf);
341
341
  if (res.byteLength === 1) {
342
342
  if (res[0] === 0) {
343
- if (this.db.schema?.hash !== this.def.schemaChecksum) {
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');
344
351
  this.reset();
345
352
  return this.#getInternal(resolve, reject);
346
353
  }
347
- reject(new Error('schema mismatch'));
348
354
  }
349
355
  else {
350
356
  reject(new Error('unexpected error'));
@@ -242,7 +242,9 @@ export class DbServer extends DbShared {
242
242
  }
243
243
  modify(buf) {
244
244
  const schemaHash = readUint64(buf, 0);
245
- if (schemaHash !== this.schema.hash) {
245
+ // if !schema
246
+ if (schemaHash !== this.schema?.hash) {
247
+ this.emit('info', 'Schema mismatch in modify');
246
248
  return null;
247
249
  }
248
250
  buf = buf.subarray(8);
@@ -255,6 +257,10 @@ export class DbServer extends DbShared {
255
257
  i += 2;
256
258
  const startId = readUint32(buf, i);
257
259
  const def = this.schemaTypesParsedById[typeId];
260
+ if (!def) {
261
+ console.error(`Wrong cannot get def in modify ${typeId} ${schemaHash} ${this.schema?.hash}!}`);
262
+ return null;
263
+ }
258
264
  let offset = def.lastId - startId;
259
265
  if (offset < 0) {
260
266
  offset = 0;
@@ -6,7 +6,6 @@ import { destructureCsmtKey, foreachDirtyBlock, specialBlock } from '../tree.js'
6
6
  import { fileURLToPath } from 'url';
7
7
  import { setNativeSchema, setSchemaOnServer, writeSchemaFile, } from '../schema.js';
8
8
  import { setToAwake, waitUntilSleeping } from './utils.js';
9
- import { deepMerge } from '@saulx/utils';
10
9
  const __filename = fileURLToPath(import.meta.url);
11
10
  const __dirname = dirname(__filename);
12
11
  const workerPath = join(__dirname, 'worker.js');
@@ -31,7 +30,16 @@ const parseTransform = (transform) => {
31
30
  export const migrate = async (server, fromSchema, toSchema, transform) => {
32
31
  const migrationId = toSchema.hash;
33
32
  server.migrating = migrationId;
34
- const abort = () => server.migrating !== migrationId;
33
+ server.emit('info', `migrating schema ${migrationId}`);
34
+ let killed = false;
35
+ const abort = () => {
36
+ if (killed) {
37
+ server.emit('info', `migration killed something went wrong ${migrationId}`);
38
+ return true;
39
+ }
40
+ server.emit('info', `abort migration - migrating: ${server.migrating} abort: ${migrationId}`);
41
+ return server.migrating !== migrationId;
42
+ };
35
43
  const tmpDb = new BasedDb({
36
44
  path: null,
37
45
  });
@@ -69,7 +77,10 @@ export const migrate = async (server, fromSchema, toSchema, transform) => {
69
77
  transferList: [port2],
70
78
  });
71
79
  // handle?
72
- worker.on('error', console.error);
80
+ worker.on('error', (err) => {
81
+ killed = true;
82
+ console.error(`Error in migration ${err.message}`);
83
+ });
73
84
  // Block handling
74
85
  let i = 0;
75
86
  let rangesToMigrate = [];
@@ -123,12 +134,9 @@ export const migrate = async (server, fromSchema, toSchema, transform) => {
123
134
  // ----------------MAKE NICE THIS------------------
124
135
  // pass last node IDS { type: lastId }
125
136
  setSchemaOnServer(server, toSchema);
126
- // make schema util for this later
127
- server.schemaTypesParsed = deepMerge(tmpDb.server.schemaTypesParsed, schemaTypesParsed);
128
- server.schemaTypesParsedById = {};
129
- for (const key in server.schemaTypesParsed) {
137
+ for (const key in schemaTypesParsed) {
130
138
  const def = server.schemaTypesParsed[key];
131
- server.schemaTypesParsedById[def.id] = def;
139
+ def.lastId = schemaTypesParsed[key].lastId;
132
140
  }
133
141
  // -----------------------------------------
134
142
  tmpDb.server.dbCtxExternal = fromCtx;
@@ -1,6 +1,7 @@
1
1
  import { DbSchema } from '../schema.js';
2
2
  export type EventMap = {
3
3
  schema: DbSchema;
4
+ info: string;
4
5
  };
5
6
  export type Event = keyof EventMap;
6
7
  export type Listener<T> = (data: T) => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@based/db",
3
- "version": "0.0.56",
3
+ "version": "0.0.58",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "./dist/src/index.js",
@@ -38,7 +38,7 @@
38
38
  "basedDbNative.cjs"
39
39
  ],
40
40
  "dependencies": {
41
- "@based/schema": "5.0.0-alpha.17",
41
+ "@based/schema": "5.0.0-alpha.18",
42
42
  "@saulx/hash": "^3.0.0",
43
43
  "@saulx/utils": "^6.7.0",
44
44
  "exit-hook": "^4.0.0",