@verdant-web/store 2.5.7 → 2.6.0
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.
- package/dist/bundle/index.js +15 -10
- package/dist/bundle/index.js.map +4 -4
- package/dist/cjs/__tests__/queries.test.js +2 -0
- package/dist/cjs/__tests__/queries.test.js.map +1 -1
- package/dist/cjs/client/Client.js +23 -11
- package/dist/cjs/client/Client.js.map +1 -1
- package/dist/cjs/client/ClientDescriptor.d.ts +3 -0
- package/dist/cjs/client/ClientDescriptor.js +100 -36
- package/dist/cjs/client/ClientDescriptor.js.map +1 -1
- package/dist/cjs/entities/DocumentFamiliyCache.d.ts +20 -1
- package/dist/cjs/entities/DocumentFamiliyCache.js +33 -18
- package/dist/cjs/entities/DocumentFamiliyCache.js.map +1 -1
- package/dist/cjs/entities/Entity.js +17 -0
- package/dist/cjs/entities/Entity.js.map +1 -1
- package/dist/cjs/entities/EntityStore.d.ts +2 -1
- package/dist/cjs/entities/EntityStore.js +36 -7
- package/dist/cjs/entities/EntityStore.js.map +1 -1
- package/dist/cjs/idb.d.ts +2 -0
- package/dist/cjs/idb.js +9 -1
- package/dist/cjs/idb.js.map +1 -1
- package/dist/cjs/metadata/Metadata.d.ts +3 -1
- package/dist/cjs/metadata/Metadata.js +3 -1
- package/dist/cjs/metadata/Metadata.js.map +1 -1
- package/dist/cjs/metadata/openMetadataDatabase.d.ts +11 -2
- package/dist/cjs/metadata/openMetadataDatabase.js +56 -3
- package/dist/cjs/metadata/openMetadataDatabase.js.map +1 -1
- package/dist/cjs/migration/db.d.ts +1 -1
- package/dist/cjs/migration/db.js +5 -2
- package/dist/cjs/migration/db.js.map +1 -1
- package/dist/cjs/migration/openDatabase.d.ts +8 -0
- package/dist/cjs/migration/openDatabase.js +228 -148
- package/dist/cjs/migration/openDatabase.js.map +1 -1
- package/dist/cjs/queries/BaseQuery.js +12 -1
- package/dist/cjs/queries/BaseQuery.js.map +1 -1
- package/dist/cjs/sync/WebSocketSync.js +4 -3
- package/dist/cjs/sync/WebSocketSync.js.map +1 -1
- package/dist/esm/__tests__/queries.test.js +2 -0
- package/dist/esm/__tests__/queries.test.js.map +1 -1
- package/dist/esm/client/Client.js +23 -11
- package/dist/esm/client/Client.js.map +1 -1
- package/dist/esm/client/ClientDescriptor.d.ts +3 -0
- package/dist/esm/client/ClientDescriptor.js +104 -40
- package/dist/esm/client/ClientDescriptor.js.map +1 -1
- package/dist/esm/entities/DocumentFamiliyCache.d.ts +20 -1
- package/dist/esm/entities/DocumentFamiliyCache.js +33 -18
- package/dist/esm/entities/DocumentFamiliyCache.js.map +1 -1
- package/dist/esm/entities/Entity.js +17 -0
- package/dist/esm/entities/Entity.js.map +1 -1
- package/dist/esm/entities/EntityStore.d.ts +2 -1
- package/dist/esm/entities/EntityStore.js +36 -7
- package/dist/esm/entities/EntityStore.js.map +1 -1
- package/dist/esm/idb.d.ts +2 -0
- package/dist/esm/idb.js +6 -0
- package/dist/esm/idb.js.map +1 -1
- package/dist/esm/metadata/Metadata.d.ts +3 -1
- package/dist/esm/metadata/Metadata.js +3 -1
- package/dist/esm/metadata/Metadata.js.map +1 -1
- package/dist/esm/metadata/openMetadataDatabase.d.ts +11 -2
- package/dist/esm/metadata/openMetadataDatabase.js +54 -2
- package/dist/esm/metadata/openMetadataDatabase.js.map +1 -1
- package/dist/esm/migration/db.d.ts +1 -1
- package/dist/esm/migration/db.js +5 -2
- package/dist/esm/migration/db.js.map +1 -1
- package/dist/esm/migration/openDatabase.d.ts +8 -0
- package/dist/esm/migration/openDatabase.js +225 -146
- package/dist/esm/migration/openDatabase.js.map +1 -1
- package/dist/esm/queries/BaseQuery.js +12 -1
- package/dist/esm/queries/BaseQuery.js.map +1 -1
- package/dist/esm/sync/WebSocketSync.js +4 -3
- package/dist/esm/sync/WebSocketSync.js.map +1 -1
- package/dist/tsconfig-cjs.tsbuildinfo +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -2
- package/src/__tests__/queries.test.ts +3 -0
- package/src/client/Client.ts +26 -12
- package/src/client/ClientDescriptor.ts +145 -49
- package/src/entities/DocumentFamiliyCache.ts +59 -19
- package/src/entities/Entity.ts +17 -0
- package/src/entities/EntityStore.ts +45 -6
- package/src/idb.ts +10 -0
- package/src/metadata/Metadata.ts +6 -1
- package/src/metadata/openMetadataDatabase.ts +96 -13
- package/src/migration/db.ts +14 -1
- package/src/migration/openDatabase.ts +357 -194
- package/src/queries/BaseQuery.ts +14 -1
- package/src/sync/WebSocketSync.ts +1 -0
|
@@ -7,23 +7,21 @@ import {
|
|
|
7
7
|
addFieldDefaults,
|
|
8
8
|
assert,
|
|
9
9
|
assignIndexValues,
|
|
10
|
-
assignOid,
|
|
11
10
|
assignOidPropertiesToAllSubObjects,
|
|
12
11
|
assignOidsToAllSubObjects,
|
|
13
12
|
cloneDeep,
|
|
14
13
|
createOid,
|
|
15
14
|
decomposeOid,
|
|
16
15
|
diffToPatches,
|
|
17
|
-
getOid,
|
|
18
16
|
getOidRoot,
|
|
19
17
|
hasOid,
|
|
20
18
|
initialToPatches,
|
|
21
|
-
migrationRange,
|
|
22
19
|
removeOidPropertiesFromAllSubObjects,
|
|
23
|
-
removeOidsFromAllSubObjects,
|
|
24
20
|
} from '@verdant-web/common';
|
|
25
21
|
import { Context } from '../context.js';
|
|
22
|
+
import { storeRequestPromise } from '../idb.js';
|
|
26
23
|
import { Metadata } from '../metadata/Metadata.js';
|
|
24
|
+
import { ClientOperation } from '../metadata/OperationsStore.js';
|
|
27
25
|
import { findAllOids, findOneOid } from '../queries/dbQueries.js';
|
|
28
26
|
import {
|
|
29
27
|
acquireLock,
|
|
@@ -33,7 +31,6 @@ import {
|
|
|
33
31
|
upgradeDatabase,
|
|
34
32
|
} from './db.js';
|
|
35
33
|
import { getMigrationPath } from './paths.js';
|
|
36
|
-
import { ClientOperation } from '../metadata/OperationsStore.js';
|
|
37
34
|
|
|
38
35
|
const globalIDB =
|
|
39
36
|
typeof window !== 'undefined' ? window.indexedDB : (undefined as any);
|
|
@@ -53,6 +50,10 @@ export async function openDocumentDatabase({
|
|
|
53
50
|
meta: Metadata;
|
|
54
51
|
context: OpenDocumentDbContext;
|
|
55
52
|
}) {
|
|
53
|
+
if (context.schema.wip) {
|
|
54
|
+
throw new Error('Cannot open a production client with a WIP schema!');
|
|
55
|
+
}
|
|
56
|
+
|
|
56
57
|
const currentVersion = await getDatabaseVersion(
|
|
57
58
|
indexedDB,
|
|
58
59
|
context.namespace,
|
|
@@ -61,6 +62,7 @@ export async function openDocumentDatabase({
|
|
|
61
62
|
);
|
|
62
63
|
|
|
63
64
|
context.log(
|
|
65
|
+
'debug',
|
|
64
66
|
'Current database version:',
|
|
65
67
|
currentVersion,
|
|
66
68
|
'target version:',
|
|
@@ -74,209 +76,359 @@ export async function openDocumentDatabase({
|
|
|
74
76
|
});
|
|
75
77
|
|
|
76
78
|
if (toRun.length > 0) {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
meta,
|
|
87
|
-
migration,
|
|
88
|
-
context,
|
|
89
|
-
});
|
|
90
|
-
await migration.migrate(engine);
|
|
91
|
-
} else {
|
|
92
|
-
// open the database with the current (old) version for this migration. this should
|
|
93
|
-
// align with the database's current version.
|
|
94
|
-
const originalDatabase = await openDatabase(
|
|
95
|
-
indexedDB,
|
|
96
|
-
context.namespace,
|
|
97
|
-
migration.oldSchema.version,
|
|
98
|
-
);
|
|
79
|
+
context.log(
|
|
80
|
+
'debug',
|
|
81
|
+
'Migrations to run:',
|
|
82
|
+
toRun.map((m) => m.version),
|
|
83
|
+
);
|
|
84
|
+
await runMigrations({ context, toRun, meta, indexedDB });
|
|
85
|
+
}
|
|
86
|
+
return openDatabase(indexedDB, context.namespace, version, context.log);
|
|
87
|
+
}
|
|
99
88
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
89
|
+
export async function openWIPDocumentDatabase({
|
|
90
|
+
version,
|
|
91
|
+
indexedDB = globalIDB,
|
|
92
|
+
migrations,
|
|
93
|
+
meta,
|
|
94
|
+
context,
|
|
95
|
+
wipNamespace,
|
|
96
|
+
}: {
|
|
97
|
+
version: number;
|
|
98
|
+
migrations: Migration<any>[];
|
|
99
|
+
indexedDB?: IDBFactory;
|
|
100
|
+
meta: Metadata;
|
|
101
|
+
context: OpenDocumentDbContext;
|
|
102
|
+
wipNamespace: string;
|
|
103
|
+
}) {
|
|
104
|
+
context.log('debug', 'Opening WIP database', wipNamespace);
|
|
105
|
+
const currentWIPVersion = await getDatabaseVersion(
|
|
106
|
+
indexedDB,
|
|
107
|
+
wipNamespace,
|
|
108
|
+
version,
|
|
109
|
+
context.log,
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
if (currentWIPVersion === version) {
|
|
113
|
+
context.log('info', `WIP schema is up-to-date; not refreshing database`);
|
|
114
|
+
} else {
|
|
115
|
+
context.log('info', `WIP schema is out-of-date; refreshing database`);
|
|
116
|
+
|
|
117
|
+
// first we need to copy the data from the production database to the WIP database
|
|
118
|
+
// at the current (non-wip) version.
|
|
119
|
+
|
|
120
|
+
const initialToRun = getMigrationPath({
|
|
121
|
+
currentVersion: currentWIPVersion,
|
|
122
|
+
targetVersion: version - 1,
|
|
123
|
+
migrations,
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
if (initialToRun.length > 0) {
|
|
127
|
+
await runMigrations({
|
|
128
|
+
context,
|
|
129
|
+
toRun: initialToRun,
|
|
130
|
+
meta,
|
|
131
|
+
indexedDB,
|
|
132
|
+
namespace: wipNamespace,
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
// now, we copy the data from the main database.
|
|
136
|
+
const mainDatabase = await openDatabase(
|
|
137
|
+
indexedDB,
|
|
138
|
+
context.namespace,
|
|
139
|
+
version - 1,
|
|
140
|
+
context.log,
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
const wipDatabase = await openDatabase(
|
|
144
|
+
indexedDB,
|
|
145
|
+
wipNamespace,
|
|
146
|
+
version - 1,
|
|
147
|
+
context.log,
|
|
148
|
+
);
|
|
149
|
+
|
|
150
|
+
// DOMStringList... doesn't have iterable... why
|
|
151
|
+
const mainDatabaseStoreNames = new Array<string>();
|
|
152
|
+
for (let i = 0; i < mainDatabase.objectStoreNames.length; i++) {
|
|
153
|
+
mainDatabaseStoreNames.push(mainDatabase.objectStoreNames[i]);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const copyFromTransaction = mainDatabase.transaction(
|
|
157
|
+
mainDatabaseStoreNames,
|
|
158
|
+
'readonly',
|
|
159
|
+
);
|
|
160
|
+
const copyFromStores = mainDatabaseStoreNames.map((name) =>
|
|
161
|
+
copyFromTransaction.objectStore(name),
|
|
162
|
+
);
|
|
163
|
+
const allObjects = await Promise.all(
|
|
164
|
+
copyFromStores.map((store) => storeRequestPromise(store.getAll())),
|
|
165
|
+
);
|
|
166
|
+
|
|
167
|
+
const copyToTransaction = wipDatabase.transaction(
|
|
168
|
+
mainDatabaseStoreNames,
|
|
169
|
+
'readwrite',
|
|
170
|
+
);
|
|
171
|
+
const copyToStores = mainDatabaseStoreNames.map((name) =>
|
|
172
|
+
copyToTransaction.objectStore(name),
|
|
173
|
+
);
|
|
174
|
+
|
|
175
|
+
for (let i = 0; i < copyToStores.length; i++) {
|
|
176
|
+
await Promise.all(
|
|
177
|
+
allObjects[i].map((obj) => {
|
|
178
|
+
return storeRequestPromise(copyToStores[i].put(obj));
|
|
179
|
+
}),
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const toRun = getMigrationPath({
|
|
185
|
+
currentVersion: version - 1,
|
|
186
|
+
targetVersion: version,
|
|
187
|
+
migrations,
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
if (toRun.length > 0) {
|
|
191
|
+
await runMigrations({
|
|
192
|
+
context,
|
|
193
|
+
toRun,
|
|
194
|
+
meta,
|
|
195
|
+
indexedDB,
|
|
196
|
+
namespace: wipNamespace,
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return openDatabase(indexedDB, wipNamespace, version, context.log);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
async function runMigrations({
|
|
205
|
+
context,
|
|
206
|
+
toRun,
|
|
207
|
+
meta,
|
|
208
|
+
indexedDB = globalIDB,
|
|
209
|
+
namespace = context.namespace,
|
|
210
|
+
}: {
|
|
211
|
+
context: OpenDocumentDbContext;
|
|
212
|
+
toRun: Migration<any>[];
|
|
213
|
+
meta: Metadata;
|
|
214
|
+
indexedDB?: IDBFactory;
|
|
215
|
+
namespace?: string;
|
|
216
|
+
}) {
|
|
217
|
+
await acquireLock(namespace, async () => {
|
|
218
|
+
// now the fun part
|
|
219
|
+
for (const migration of toRun) {
|
|
220
|
+
// special case: if this is the version 1 migration, we have no pre-existing database
|
|
221
|
+
// to use for the migration.
|
|
222
|
+
let engine: MigrationEngine<any, any>;
|
|
223
|
+
// migrations from 0 (i.e. initial migrations) don't attempt to open an existing db
|
|
224
|
+
if (migration.oldSchema.version === 0) {
|
|
225
|
+
engine = getInitialMigrationEngine({
|
|
226
|
+
meta,
|
|
227
|
+
migration,
|
|
228
|
+
context,
|
|
229
|
+
});
|
|
230
|
+
await migration.migrate(engine);
|
|
231
|
+
} else {
|
|
232
|
+
// open the database with the current (old) version for this migration. this should
|
|
233
|
+
// align with the database's current version.
|
|
234
|
+
const originalDatabase = await openDatabase(
|
|
235
|
+
indexedDB,
|
|
236
|
+
namespace,
|
|
237
|
+
migration.oldSchema.version,
|
|
238
|
+
context.log,
|
|
239
|
+
);
|
|
240
|
+
|
|
241
|
+
// this will only write to our metadata store via operations!
|
|
242
|
+
engine = getMigrationEngine({
|
|
243
|
+
meta,
|
|
244
|
+
migration,
|
|
245
|
+
context: {
|
|
246
|
+
...context,
|
|
247
|
+
documentDb: originalDatabase,
|
|
248
|
+
},
|
|
249
|
+
});
|
|
250
|
+
try {
|
|
109
251
|
await migration.migrate(engine);
|
|
110
252
|
// wait on any out-of-band async operations to complete
|
|
111
253
|
await Promise.all(engine.awaitables);
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
254
|
+
} catch (err) {
|
|
255
|
+
context.log(
|
|
256
|
+
'critical',
|
|
257
|
+
`Migration failed (${migration.oldSchema.version} -> ${migration.newSchema.version})`,
|
|
258
|
+
err,
|
|
259
|
+
);
|
|
260
|
+
throw err;
|
|
116
261
|
}
|
|
117
262
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
263
|
+
// now we have to open the database again with the next version and
|
|
264
|
+
// make the appropriate schema changes during the upgrade.
|
|
265
|
+
await closeDatabase(originalDatabase);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
context.log(
|
|
269
|
+
'debug',
|
|
270
|
+
'Upgrading database',
|
|
271
|
+
namespace,
|
|
272
|
+
'to version',
|
|
273
|
+
migration.newSchema.version,
|
|
274
|
+
);
|
|
275
|
+
await upgradeDatabase(
|
|
276
|
+
indexedDB,
|
|
277
|
+
namespace,
|
|
278
|
+
migration.newSchema.version,
|
|
279
|
+
(transaction, db) => {
|
|
280
|
+
for (const newCollection of migration.addedCollections) {
|
|
281
|
+
db.createObjectStore(newCollection, {
|
|
282
|
+
keyPath:
|
|
283
|
+
migration.newSchema.collections[newCollection].primaryKey,
|
|
284
|
+
autoIncrement: false,
|
|
285
|
+
});
|
|
286
|
+
}
|
|
130
287
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
}
|
|
139
|
-
// remove old indexes
|
|
140
|
-
for (const oldIndex of migration.removedIndexes[collection] ||
|
|
141
|
-
[]) {
|
|
142
|
-
store.deleteIndex(oldIndex.name);
|
|
143
|
-
}
|
|
288
|
+
for (const collection of migration.allCollections) {
|
|
289
|
+
const store = transaction.objectStore(collection);
|
|
290
|
+
// apply new indexes
|
|
291
|
+
for (const newIndex of migration.addedIndexes[collection] || []) {
|
|
292
|
+
store.createIndex(newIndex.name, newIndex.name, {
|
|
293
|
+
multiEntry: newIndex.multiEntry,
|
|
294
|
+
});
|
|
144
295
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
// and leave it in place
|
|
149
|
-
transaction.objectStore(removedCollection).clear();
|
|
296
|
+
// remove old indexes
|
|
297
|
+
for (const oldIndex of migration.removedIndexes[collection] || []) {
|
|
298
|
+
store.deleteIndex(oldIndex.name);
|
|
150
299
|
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
|
|
300
|
+
}
|
|
301
|
+
for (const removedCollection of migration.removedCollections) {
|
|
302
|
+
// !! can't delete the store, because old operations that relate to
|
|
303
|
+
// this store may still exist in history. instead, we can clear it out
|
|
304
|
+
// and leave it in place
|
|
305
|
+
transaction.objectStore(removedCollection).clear();
|
|
306
|
+
}
|
|
307
|
+
},
|
|
308
|
+
context.log,
|
|
309
|
+
);
|
|
154
310
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
});
|
|
311
|
+
/**
|
|
312
|
+
* In cases where operations from the future have been
|
|
313
|
+
* received by this client, we may have created entire
|
|
314
|
+
* documents in metadata which were not written to storage
|
|
315
|
+
* because all of their operations were in the future (
|
|
316
|
+
* i.e. in the next version). We have to find those documents
|
|
317
|
+
* and also write their snapshots to storage, because they
|
|
318
|
+
* won't be present in storage already to 'refresh,' so
|
|
319
|
+
* if we don't analyze metadata for 'future' operations like
|
|
320
|
+
* this, we won't know they exist.
|
|
321
|
+
*
|
|
322
|
+
* This led to behavior where the metadata would be properly
|
|
323
|
+
* synced, but after upgrading the app and migrating, items
|
|
324
|
+
* would be missing from findAll and findOne queries.
|
|
325
|
+
*/
|
|
326
|
+
const docsWithUnappliedMigrations = await getDocsWithUnappliedMigrations({
|
|
327
|
+
meta,
|
|
328
|
+
currentVersion: migration.oldSchema.version,
|
|
329
|
+
newVersion: migration.newSchema.version,
|
|
330
|
+
});
|
|
176
331
|
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
332
|
+
// once the schema is ready, we can write back the migrated documents
|
|
333
|
+
const upgradedDatabase = await openDatabase(
|
|
334
|
+
indexedDB,
|
|
335
|
+
namespace,
|
|
336
|
+
migration.newSchema.version,
|
|
337
|
+
context.log,
|
|
338
|
+
);
|
|
339
|
+
for (const collection of migration.allCollections) {
|
|
340
|
+
// first step is to read in all the keys we need to rewrite
|
|
341
|
+
const documentReadTransaction = upgradedDatabase.transaction(
|
|
342
|
+
collection,
|
|
343
|
+
'readwrite',
|
|
344
|
+
);
|
|
345
|
+
const readStore = documentReadTransaction.objectStore(collection);
|
|
346
|
+
const keys = await getAllKeys(readStore);
|
|
347
|
+
// map the keys to OIDs
|
|
348
|
+
const oids = keys.map((key) => createOid(collection, `${key}`));
|
|
349
|
+
oids.push(
|
|
350
|
+
...engine.newOids.filter((oid) => {
|
|
351
|
+
return decomposeOid(oid).collection === collection;
|
|
352
|
+
}),
|
|
353
|
+
...docsWithUnappliedMigrations.filter((oid) => {
|
|
354
|
+
return decomposeOid(oid).collection === collection;
|
|
355
|
+
}),
|
|
182
356
|
);
|
|
183
|
-
for (const collection of migration.allCollections) {
|
|
184
|
-
// first step is to read in all the keys we need to rewrite
|
|
185
|
-
const documentReadTransaction = upgradedDatabase.transaction(
|
|
186
|
-
collection,
|
|
187
|
-
'readwrite',
|
|
188
|
-
);
|
|
189
|
-
const readStore = documentReadTransaction.objectStore(collection);
|
|
190
|
-
const keys = await getAllKeys(readStore);
|
|
191
|
-
// map the keys to OIDs
|
|
192
|
-
const oids = keys.map((key) => createOid(collection, `${key}`));
|
|
193
|
-
oids.push(
|
|
194
|
-
...engine.newOids.filter((oid) => {
|
|
195
|
-
return decomposeOid(oid).collection === collection;
|
|
196
|
-
}),
|
|
197
|
-
...docsWithUnappliedMigrations.filter((oid) => {
|
|
198
|
-
return decomposeOid(oid).collection === collection;
|
|
199
|
-
}),
|
|
200
|
-
);
|
|
201
|
-
|
|
202
|
-
const snapshots = await Promise.all(
|
|
203
|
-
oids.map(async (oid) => {
|
|
204
|
-
try {
|
|
205
|
-
const snap = await meta.getDocumentSnapshot(oid);
|
|
206
|
-
return [oid, snap];
|
|
207
|
-
} catch (e) {
|
|
208
|
-
// this seems to happen with baselines/ops which are not fully
|
|
209
|
-
// cleaned up after deletion?
|
|
210
|
-
context.log(
|
|
211
|
-
'error',
|
|
212
|
-
'Could not regenerate snapshot during migration for oid',
|
|
213
|
-
oid,
|
|
214
|
-
'this document will not be preserved',
|
|
215
|
-
e,
|
|
216
|
-
);
|
|
217
|
-
return null;
|
|
218
|
-
}
|
|
219
|
-
}),
|
|
220
|
-
);
|
|
221
357
|
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
358
|
+
const snapshots = await Promise.all(
|
|
359
|
+
oids.map(async (oid) => {
|
|
360
|
+
try {
|
|
361
|
+
const snap = await meta.getDocumentSnapshot(oid);
|
|
362
|
+
return [oid, snap];
|
|
363
|
+
} catch (e) {
|
|
364
|
+
// this seems to happen with baselines/ops which are not fully
|
|
365
|
+
// cleaned up after deletion?
|
|
366
|
+
context.log(
|
|
367
|
+
'error',
|
|
368
|
+
'Could not regenerate snapshot during migration for oid',
|
|
369
|
+
oid,
|
|
370
|
+
'this document will not be preserved',
|
|
371
|
+
e,
|
|
229
372
|
);
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
373
|
+
return null;
|
|
374
|
+
}
|
|
375
|
+
}),
|
|
376
|
+
);
|
|
234
377
|
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
return deleteView(writeStore, id);
|
|
248
|
-
}
|
|
249
|
-
}),
|
|
250
|
-
);
|
|
251
|
-
}
|
|
378
|
+
const views = snapshots
|
|
379
|
+
.filter((s): s is [string, any] => !!s)
|
|
380
|
+
.map(([oid, snapshot]) => {
|
|
381
|
+
if (!snapshot) return [oid, undefined];
|
|
382
|
+
const view = assignIndexValues(
|
|
383
|
+
migration.newSchema.collections[collection],
|
|
384
|
+
snapshot,
|
|
385
|
+
);
|
|
386
|
+
// TODO: remove the need for this by only storing index values!
|
|
387
|
+
assignOidPropertiesToAllSubObjects(view);
|
|
388
|
+
return [oid, view];
|
|
389
|
+
});
|
|
252
390
|
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
)
|
|
270
|
-
.flatMap((i) => i)
|
|
271
|
-
.join(', ')}
|
|
272
|
-
`);
|
|
391
|
+
// now we can write the documents back
|
|
392
|
+
const documentWriteTransaction = upgradedDatabase.transaction(
|
|
393
|
+
collection,
|
|
394
|
+
'readwrite',
|
|
395
|
+
);
|
|
396
|
+
const writeStore = documentWriteTransaction.objectStore(collection);
|
|
397
|
+
await Promise.all(
|
|
398
|
+
views.map(([oid, view]) => {
|
|
399
|
+
if (view) {
|
|
400
|
+
return putView(writeStore, view);
|
|
401
|
+
} else {
|
|
402
|
+
const { id } = decomposeOid(oid);
|
|
403
|
+
return deleteView(writeStore, id);
|
|
404
|
+
}
|
|
405
|
+
}),
|
|
406
|
+
);
|
|
273
407
|
}
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
408
|
+
|
|
409
|
+
await closeDatabase(upgradedDatabase);
|
|
410
|
+
|
|
411
|
+
context.log('debug', `Migration of ${namespace} complete.`);
|
|
412
|
+
context.log(`
|
|
413
|
+
⬆️ v${migration.newSchema.version} Migration complete. Here's the rundown:
|
|
414
|
+
- Added collections: ${migration.addedCollections.join(', ')}
|
|
415
|
+
- Removed collections: ${migration.removedCollections.join(', ')}
|
|
416
|
+
- Changed collections: ${migration.changedCollections.join(', ')}
|
|
417
|
+
- New indexes: ${Object.keys(migration.addedIndexes)
|
|
418
|
+
.map((col) =>
|
|
419
|
+
migration.addedIndexes[col].map((i) => `${col}.${i.name}`),
|
|
420
|
+
)
|
|
421
|
+
.flatMap((i) => i)
|
|
422
|
+
.join(', ')}
|
|
423
|
+
- Removed indexes: ${Object.keys(migration.removedIndexes)
|
|
424
|
+
.map((col) =>
|
|
425
|
+
migration.removedIndexes[col].map((i) => `${col}.${i.name}`),
|
|
426
|
+
)
|
|
427
|
+
.flatMap((i) => i)
|
|
428
|
+
.join(', ')}
|
|
429
|
+
`);
|
|
430
|
+
}
|
|
431
|
+
});
|
|
280
432
|
}
|
|
281
433
|
|
|
282
434
|
function getMigrationMutations({
|
|
@@ -332,7 +484,10 @@ function getMigrationQueries({
|
|
|
332
484
|
acc[collectionName] = {
|
|
333
485
|
get: async (id: string) => {
|
|
334
486
|
const oid = createOid(collectionName, id);
|
|
335
|
-
const doc = await meta.getDocumentSnapshot(oid
|
|
487
|
+
const doc = await meta.getDocumentSnapshot(oid, {
|
|
488
|
+
// only get the snapshot up to the previous version (newer operations may have synced)
|
|
489
|
+
to: meta.time.now(migration.oldSchema.version),
|
|
490
|
+
});
|
|
336
491
|
// removeOidsFromAllSubObjects(doc);
|
|
337
492
|
return doc;
|
|
338
493
|
},
|
|
@@ -343,7 +498,10 @@ function getMigrationQueries({
|
|
|
343
498
|
context,
|
|
344
499
|
});
|
|
345
500
|
if (!oid) return null;
|
|
346
|
-
const doc = await meta.getDocumentSnapshot(oid
|
|
501
|
+
const doc = await meta.getDocumentSnapshot(oid, {
|
|
502
|
+
// only get the snapshot up to the previous version (newer operations may have synced)
|
|
503
|
+
to: meta.time.now(migration.oldSchema.version),
|
|
504
|
+
});
|
|
347
505
|
// removeOidsFromAllSubObjects(doc);
|
|
348
506
|
return doc;
|
|
349
507
|
},
|
|
@@ -354,7 +512,12 @@ function getMigrationQueries({
|
|
|
354
512
|
context,
|
|
355
513
|
});
|
|
356
514
|
const docs = await Promise.all(
|
|
357
|
-
oids.map((oid) =>
|
|
515
|
+
oids.map((oid) =>
|
|
516
|
+
meta.getDocumentSnapshot(oid, {
|
|
517
|
+
// only get the snapshot up to the previous version (newer operations may have synced)
|
|
518
|
+
to: meta.time.now(migration.oldSchema.version),
|
|
519
|
+
}),
|
|
520
|
+
),
|
|
358
521
|
);
|
|
359
522
|
// docs.forEach((doc) => removeOidsFromAllSubObjects(doc));
|
|
360
523
|
return docs;
|
package/src/queries/BaseQuery.ts
CHANGED
|
@@ -182,7 +182,20 @@ export abstract class BaseQuery<T> extends Disposable {
|
|
|
182
182
|
}
|
|
183
183
|
// no status change needed if already in a 'running' status.
|
|
184
184
|
|
|
185
|
-
this._executionPromise = this.run()
|
|
185
|
+
this._executionPromise = this.run()
|
|
186
|
+
.then(() => this._value)
|
|
187
|
+
.catch((err) => {
|
|
188
|
+
if (err instanceof Error) {
|
|
189
|
+
if (
|
|
190
|
+
err.name === 'InvalidStateError' ||
|
|
191
|
+
err.name === 'InvalidAccessError'
|
|
192
|
+
) {
|
|
193
|
+
// possibly accessing db while it's closed. not much we can do.
|
|
194
|
+
return this._value;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
throw err;
|
|
198
|
+
});
|
|
186
199
|
return this._executionPromise;
|
|
187
200
|
};
|
|
188
201
|
protected abstract run(): Promise<void>;
|