@based/db 0.1.4 → 0.1.5

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
@@ -10,101 +10,104 @@ if (isMainThread) {
10
10
  console.warn('running worker.ts in mainthread');
11
11
  }
12
12
  else if (workerData?.isDbMigrateWorker) {
13
- const { from, to, fromSchema, toSchema, channel, workerState, transformFns } = workerData;
14
- const fromCtx = native.externalFromInt(from);
15
- const toCtx = native.externalFromInt(to);
16
- native.createThreadCtx(fromCtx, native.getThreadId());
17
- native.createThreadCtx(toCtx, native.getThreadId());
18
- const fromDb = new BasedDb({ path: null });
19
- const toDb = new BasedDb({ path: null });
20
- fromDb.server.dbCtxExternal = fromCtx;
21
- toDb.server.dbCtxExternal = toCtx;
22
- setSchemaOnServer(fromDb.server, deSerialize(fromSchema));
23
- setSchemaOnServer(toDb.server, deSerialize(toSchema));
24
- setLocalClientSchema(fromDb.client, fromDb.server.schema);
25
- setLocalClientSchema(toDb.client, toDb.server.schema);
26
- try {
27
- const map = {};
28
- for (const type in fromDb.server.schemaTypesParsed) {
29
- const { id, props } = fromDb.server.schemaTypesParsed[type];
30
- const include = [];
31
- const includeRaw = [];
32
- for (const path in props) {
33
- const prop = props[path];
34
- if (prop.typeIndex === REFERENCE || prop.typeIndex === REFERENCES) {
35
- include.push(`${path}.id`);
36
- if (prop.edges) {
37
- for (const key in prop.edges) {
38
- const edge = prop.edges[key];
39
- if (edge.typeIndex === REFERENCE ||
40
- edge.typeIndex === REFERENCES) {
41
- include.push(`${path}.${key}.id`);
42
- }
43
- else if (edge.typeIndex === CARDINALITY) {
44
- includeRaw.push(`${path}.${key}`);
45
- }
46
- else {
47
- include.push(`${path}.${key}`);
13
+ ;
14
+ (async () => {
15
+ const { from, to, fromSchema, toSchema, channel, workerState, transformFns, } = workerData;
16
+ const fromCtx = native.externalFromInt(from);
17
+ const toCtx = native.externalFromInt(to);
18
+ native.createThreadCtx(fromCtx, native.getThreadId());
19
+ native.createThreadCtx(toCtx, native.getThreadId());
20
+ const fromDb = new BasedDb({ path: null });
21
+ const toDb = new BasedDb({ path: null });
22
+ fromDb.server.dbCtxExternal = fromCtx;
23
+ toDb.server.dbCtxExternal = toCtx;
24
+ setSchemaOnServer(fromDb.server, deSerialize(fromSchema));
25
+ setSchemaOnServer(toDb.server, deSerialize(toSchema));
26
+ setLocalClientSchema(fromDb.client, fromDb.server.schema);
27
+ setLocalClientSchema(toDb.client, toDb.server.schema);
28
+ try {
29
+ const map = {};
30
+ for (const type in fromDb.server.schemaTypesParsed) {
31
+ const { id, props } = fromDb.server.schemaTypesParsed[type];
32
+ const include = [];
33
+ const includeRaw = [];
34
+ for (const path in props) {
35
+ const prop = props[path];
36
+ if (prop.typeIndex === REFERENCE || prop.typeIndex === REFERENCES) {
37
+ include.push(`${path}.id`);
38
+ if (prop.edges) {
39
+ for (const key in prop.edges) {
40
+ const edge = prop.edges[key];
41
+ if (edge.typeIndex === REFERENCE ||
42
+ edge.typeIndex === REFERENCES) {
43
+ include.push(`${path}.${key}.id`);
44
+ }
45
+ else if (edge.typeIndex === CARDINALITY) {
46
+ includeRaw.push(`${path}.${key}`);
47
+ }
48
+ else {
49
+ include.push(`${path}.${key}`);
50
+ }
48
51
  }
49
52
  }
50
53
  }
54
+ else if (prop.typeIndex === CARDINALITY) {
55
+ includeRaw.push(path);
56
+ }
57
+ else {
58
+ include.push(path);
59
+ }
51
60
  }
52
- else if (prop.typeIndex === CARDINALITY) {
53
- includeRaw.push(path);
54
- }
55
- else {
56
- include.push(path);
57
- }
61
+ map[id] = { type, include, includeRaw };
58
62
  }
59
- map[id] = { type, include, includeRaw };
60
- }
61
- for (const type in transformFns) {
62
- const fnOrNull = transformFns[type];
63
- transformFns[type] = eval(`(${fnOrNull})`);
64
- }
65
- while (true) {
66
- let msg;
67
- while ((msg = receiveMessageOnPort(channel))) {
68
- const leafData = msg.message;
69
- const { type, include, includeRaw } = map[leafData.typeId];
70
- const typeTransformFn = transformFns[type];
71
- const query = fromDb
72
- .query(type)
73
- .range(leafData.start - 1, leafData.end)
74
- .include(include);
75
- for (const rawProp of includeRaw) {
76
- query.include(rawProp, { raw: true });
77
- }
78
- const nodes = query._getSync(fromCtx);
79
- if (typeTransformFn) {
80
- for (const node of nodes) {
81
- const res = typeTransformFn(node);
82
- if (res === null) {
83
- continue;
84
- }
85
- if (Array.isArray(res)) {
86
- toDb.create(res[0], res[1] || node, { unsafe: true });
87
- }
88
- else {
89
- toDb.create(type, res || node, { unsafe: true });
63
+ for (const type in transformFns) {
64
+ const fnOrNull = transformFns[type];
65
+ transformFns[type] = eval(`(${fnOrNull})`);
66
+ }
67
+ while (true) {
68
+ let msg;
69
+ while ((msg = receiveMessageOnPort(channel))) {
70
+ const leafData = msg.message;
71
+ const { type, include, includeRaw } = map[leafData.typeId];
72
+ const typeTransformFn = transformFns[type];
73
+ const query = fromDb
74
+ .query(type)
75
+ .range(leafData.start - 1, leafData.end)
76
+ .include(include);
77
+ for (const rawProp of includeRaw) {
78
+ query.include(rawProp, { raw: true });
79
+ }
80
+ const nodes = query._getSync(fromCtx);
81
+ if (typeTransformFn) {
82
+ for (const node of nodes) {
83
+ const res = typeTransformFn(node);
84
+ if (res === null) {
85
+ continue;
86
+ }
87
+ if (Array.isArray(res)) {
88
+ toDb.create(res[0], res[1] || node, { unsafe: true });
89
+ }
90
+ else {
91
+ toDb.create(type, res || node, { unsafe: true });
92
+ }
90
93
  }
91
94
  }
92
- }
93
- else if (type in toDb.server.schemaTypesParsed) {
94
- for (const node of nodes) {
95
- toDb.create(type, node, { unsafe: true });
95
+ else if (type in toDb.server.schemaTypesParsed) {
96
+ for (const node of nodes) {
97
+ toDb.create(type, node, { unsafe: true });
98
+ }
96
99
  }
97
100
  }
101
+ await toDb.drain();
102
+ native.membarSyncWrite();
103
+ setToSleep(workerState);
98
104
  }
99
- await toDb.drain();
100
- native.membarSyncWrite();
101
- setToSleep(workerState);
102
105
  }
103
- }
104
- catch (e) {
105
- console.error(e);
106
- throw e;
107
- }
106
+ catch (e) {
107
+ console.error(e);
108
+ throw e;
109
+ }
110
+ })();
108
111
  }
109
112
  else {
110
113
  console.info('incorrect worker db migrate');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@based/db",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "type": "module",
5
5
  "main": "./dist/src/index.js",
6
6
  "scripts": {