@based/db 0.0.69 → 0.0.70

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 { writeString } from './string.js';
11
11
  import { writeText } from './text.js';
12
12
  import { writeJson } from './json.js';
13
13
  import { writeAlias } from './alias.js';
14
+ import { getByPath } from '@saulx/utils';
14
15
  const appendCreate = (ctx, schema, obj, res, unsafe) => {
15
16
  const len = ctx.len;
16
17
  let err = modify(ctx, res, obj, schema, CREATE, schema.tree, true, unsafe);
@@ -26,6 +27,9 @@ const appendCreate = (ctx, schema, obj, res, unsafe) => {
26
27
  if (schema.createTs) {
27
28
  const createTs = Date.now();
28
29
  for (const prop of schema.createTs) {
30
+ if (getByPath(obj, prop.path) !== undefined) {
31
+ continue;
32
+ }
29
33
  if (ctx.lastMain === -1) {
30
34
  let mainLenU32 = schema.mainLen;
31
35
  setCursor(ctx, schema, prop.prop, MICRO_BUFFER, res.tmpId, CREATE);
@@ -5,6 +5,7 @@ import { modify } from './modify.js';
5
5
  import { ModifyState } from './ModifyRes.js';
6
6
  import { RANGE_ERR, UPDATE } from './types.js';
7
7
  import { appendFixedValue } from './fixed.js';
8
+ import { getByPath } from '@saulx/utils';
8
9
  const appendUpdate = (ctx, def, obj, res, overwrite) => {
9
10
  const err = modify(ctx, res, obj, def, UPDATE, def.tree, overwrite);
10
11
  if (err) {
@@ -13,6 +14,9 @@ const appendUpdate = (ctx, def, obj, res, overwrite) => {
13
14
  if (def.updateTs) {
14
15
  const updateTs = Date.now();
15
16
  for (const prop of def.updateTs) {
17
+ if (getByPath(obj, prop.path) !== undefined) {
18
+ continue;
19
+ }
16
20
  if (ctx.mergeMain) {
17
21
  ctx.mergeMain.push(prop, updateTs);
18
22
  ctx.mergeMainSize += prop.len + 4;
@@ -7,6 +7,7 @@ import { foreachDirtyBlock } from '../blocks.js';
7
7
  import { fileURLToPath } from 'url';
8
8
  import { setNativeSchema, setSchemaOnServer, writeSchemaFile, } from '../schema.js';
9
9
  import { setToAwake, waitUntilSleeping } from './utils.js';
10
+ import { serialize } from '@based/schema';
10
11
  const __filename = fileURLToPath(import.meta.url);
11
12
  const __dirname = dirname(__filename);
12
13
  const workerPath = join(__dirname, 'worker.js');
@@ -74,8 +75,14 @@ export const migrate = async (server, fromSchema, toSchema, transform) => {
74
75
  isDbMigrateWorker: true,
75
76
  from: fromAddress,
76
77
  to: toAddress,
77
- fromSchema,
78
- toSchema,
78
+ fromSchema: serialize(fromSchema, {
79
+ stripMetaInformation: true,
80
+ stripTransform: true,
81
+ }),
82
+ toSchema: serialize(toSchema, {
83
+ stripMetaInformation: true,
84
+ stripTransform: true,
85
+ }),
79
86
  channel: port2,
80
87
  workerState,
81
88
  transformFns,
@@ -83,9 +90,12 @@ export const migrate = async (server, fromSchema, toSchema, transform) => {
83
90
  transferList: [port2],
84
91
  });
85
92
  // handle?
86
- worker.on('error', (err) => {
87
- killed = true;
88
- console.error(`Error in migration ${err.message}`);
93
+ const errorPromise = new Promise((resolve) => {
94
+ worker.once('error', (err) => {
95
+ killed = true;
96
+ console.error(`Error in migration ${err.message}, aborting migration`);
97
+ resolve();
98
+ });
89
99
  });
90
100
  // Block handling
91
101
  let i = 0;
@@ -107,7 +117,7 @@ export const migrate = async (server, fromSchema, toSchema, transform) => {
107
117
  const leafData = rangesToMigrate[i++];
108
118
  port1.postMessage(leafData);
109
119
  setToAwake(workerState, true);
110
- await waitUntilSleeping(workerState);
120
+ await Promise.race([errorPromise, waitUntilSleeping(workerState)]);
111
121
  // exec queued modifies
112
122
  server.activeReaders--;
113
123
  server.onQueryEnd();
@@ -156,7 +166,11 @@ export const migrate = async (server, fromSchema, toSchema, transform) => {
156
166
  return;
157
167
  }
158
168
  native.membarSyncRead();
159
- await save(server, { forceFullDump: true, skipDirtyCheck: true, skipMigrationCheck: true });
169
+ await save(server, {
170
+ forceFullDump: true,
171
+ skipDirtyCheck: true,
172
+ skipMigrationCheck: true,
173
+ });
160
174
  await writeSchemaFile(server, toSchema);
161
175
  server.migrating = 0;
162
176
  process.nextTick(() => server.emit('schema', server.schema));
@@ -6,6 +6,7 @@ import { isTypedArray } from 'node:util/types';
6
6
  import { setSchemaOnServer } from '../schema.js';
7
7
  import { setToSleep } from './utils.js';
8
8
  import { setLocalClientSchema } from '../../client/setLocalClientSchema.js';
9
+ import { deSerialize } from '@based/schema';
9
10
  if (isMainThread) {
10
11
  console.warn('running worker.ts in mainthread');
11
12
  }
@@ -41,80 +42,87 @@ else if (workerData?.isDbMigrateWorker) {
41
42
  };
42
43
  fromDb.server.dbCtxExternal = fromCtx;
43
44
  toDb.server.dbCtxExternal = toCtx;
44
- setSchemaOnServer(fromDb.server, fromSchema);
45
- setSchemaOnServer(toDb.server, toSchema);
45
+ setSchemaOnServer(fromDb.server, deSerialize(fromSchema));
46
+ setSchemaOnServer(toDb.server, deSerialize(toSchema));
46
47
  setLocalClientSchema(fromDb.client, fromDb.server.schema);
47
48
  setLocalClientSchema(toDb.client, toDb.server.schema);
48
- const map = {};
49
- for (const type in fromDb.server.schemaTypesParsed) {
50
- const { id, props } = fromDb.server.schemaTypesParsed[type];
51
- const include = Object.keys(props);
52
- let i = include.length;
53
- while (i--) {
54
- const path = include[i];
55
- if (props[path].typeIndex === REFERENCE ||
56
- props[path].typeIndex === REFERENCES) {
57
- include[i] = `${path}.id`;
58
- if (props[path].edges) {
59
- for (const key in props[path].edges) {
60
- const prop = props[path].edges[key];
61
- if (prop.typeIndex === REFERENCE || prop.typeIndex === REFERENCES) {
62
- include.push(`${path}.${key}.id`);
63
- }
64
- else {
65
- include.push(`${path}.${key}`);
49
+ try {
50
+ const map = {};
51
+ for (const type in fromDb.server.schemaTypesParsed) {
52
+ const { id, props } = fromDb.server.schemaTypesParsed[type];
53
+ const include = Object.keys(props);
54
+ let i = include.length;
55
+ while (i--) {
56
+ const path = include[i];
57
+ if (props[path].typeIndex === REFERENCE ||
58
+ props[path].typeIndex === REFERENCES) {
59
+ include[i] = `${path}.id`;
60
+ if (props[path].edges) {
61
+ for (const key in props[path].edges) {
62
+ const prop = props[path].edges[key];
63
+ if (prop.typeIndex === REFERENCE ||
64
+ prop.typeIndex === REFERENCES) {
65
+ include.push(`${path}.${key}.id`);
66
+ }
67
+ else {
68
+ include.push(`${path}.${key}`);
69
+ }
66
70
  }
67
71
  }
68
72
  }
69
73
  }
74
+ map[id] = { type, include };
70
75
  }
71
- map[id] = { type, include };
72
- }
73
- for (const type in transformFns) {
74
- const fnOrNull = transformFns[type];
75
- transformFns[type] = eval(`(${fnOrNull})`);
76
- }
77
- while (true) {
78
- let msg;
79
- while ((msg = receiveMessageOnPort(channel))) {
80
- const leafData = msg.message;
81
- const { type, include } = map[leafData.typeId];
82
- const typeTransformFn = transformFns[type];
83
- if (typeTransformFn) {
84
- const nodes = fromDb
85
- .query(type)
86
- .include(include)
87
- .range(leafData.start - 1, leafData.end)
88
- ._getSync(fromCtx);
89
- for (const node of nodes) {
90
- const res = typeTransformFn(node);
91
- if (res === null) {
92
- continue;
93
- }
94
- if (Array.isArray(res)) {
95
- toDb.create(res[0], res[1] || node, { unsafe: true });
96
- }
97
- else {
98
- toDb.create(type, res || node, { unsafe: true });
76
+ for (const type in transformFns) {
77
+ const fnOrNull = transformFns[type];
78
+ transformFns[type] = eval(`(${fnOrNull})`);
79
+ }
80
+ while (true) {
81
+ let msg;
82
+ while ((msg = receiveMessageOnPort(channel))) {
83
+ const leafData = msg.message;
84
+ const { type, include } = map[leafData.typeId];
85
+ const typeTransformFn = transformFns[type];
86
+ if (typeTransformFn) {
87
+ const nodes = fromDb
88
+ .query(type)
89
+ .include(include)
90
+ .range(leafData.start - 1, leafData.end)
91
+ ._getSync(fromCtx);
92
+ for (const node of nodes) {
93
+ const res = typeTransformFn(node);
94
+ if (res === null) {
95
+ continue;
96
+ }
97
+ if (Array.isArray(res)) {
98
+ toDb.create(res[0], res[1] || node, { unsafe: true });
99
+ }
100
+ else {
101
+ toDb.create(type, res || node, { unsafe: true });
102
+ }
99
103
  }
100
104
  }
101
- }
102
- else if (type in toDb.server.schemaTypesParsed) {
103
- const nodes = fromDb
104
- .query(type)
105
- .include(include)
106
- .range(leafData.start - 1, leafData.end)
107
- ._getSync(fromCtx);
108
- for (const node of nodes) {
109
- toDb.create(type, node, { unsafe: true });
105
+ else if (type in toDb.server.schemaTypesParsed) {
106
+ const nodes = fromDb
107
+ .query(type)
108
+ .include(include)
109
+ .range(leafData.start - 1, leafData.end)
110
+ ._getSync(fromCtx);
111
+ for (const node of nodes) {
112
+ toDb.create(type, node, { unsafe: true });
113
+ }
110
114
  }
111
115
  }
116
+ await toDb.drain();
117
+ native.membarSyncWrite();
118
+ // WE ARE ONLY GOING TO SEND { type: lastNodeId }
119
+ channel.postMessage(cp(toDb.server.schemaTypesParsed));
120
+ setToSleep(workerState);
112
121
  }
113
- await toDb.drain();
114
- native.membarSyncWrite();
115
- // WE ARE ONLY GOING TO SEND { type: lastNodeId }
116
- channel.postMessage(cp(toDb.server.schemaTypesParsed));
117
- setToSleep(workerState);
122
+ }
123
+ catch (e) {
124
+ console.error(e);
125
+ throw e;
118
126
  }
119
127
  }
120
128
  else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@based/db",
3
- "version": "0.0.69",
3
+ "version": "0.0.70",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "./dist/src/index.js",
@@ -39,7 +39,7 @@
39
39
  "basedDbNative.cjs"
40
40
  ],
41
41
  "dependencies": {
42
- "@based/schema": "5.0.0-alpha.25",
42
+ "@based/schema": "5.0.0-alpha.26",
43
43
  "@saulx/hash": "^3.0.0",
44
44
  "@saulx/utils": "^6.7.2",
45
45
  "exit-hook": "^4.0.0",
@@ -56,6 +56,7 @@
56
56
  "axios": "^1.7.9",
57
57
  "rimraf": "^3.0.2",
58
58
  "tar": "^7.4.3",
59
- "typescript": "^5.6.3"
59
+ "typescript": "^5.6.3",
60
+ "fs-extra": "^11.1.1"
60
61
  }
61
62
  }