@verdant-web/store 2.5.8 → 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.
Files changed (74) hide show
  1. package/dist/bundle/index.js +15 -10
  2. package/dist/bundle/index.js.map +4 -4
  3. package/dist/cjs/__tests__/queries.test.js +2 -0
  4. package/dist/cjs/__tests__/queries.test.js.map +1 -1
  5. package/dist/cjs/client/Client.js +23 -11
  6. package/dist/cjs/client/Client.js.map +1 -1
  7. package/dist/cjs/client/ClientDescriptor.d.ts +3 -0
  8. package/dist/cjs/client/ClientDescriptor.js +100 -36
  9. package/dist/cjs/client/ClientDescriptor.js.map +1 -1
  10. package/dist/cjs/entities/DocumentFamiliyCache.d.ts +20 -1
  11. package/dist/cjs/entities/DocumentFamiliyCache.js +33 -18
  12. package/dist/cjs/entities/DocumentFamiliyCache.js.map +1 -1
  13. package/dist/cjs/entities/EntityStore.d.ts +2 -1
  14. package/dist/cjs/entities/EntityStore.js +36 -7
  15. package/dist/cjs/entities/EntityStore.js.map +1 -1
  16. package/dist/cjs/idb.d.ts +2 -0
  17. package/dist/cjs/idb.js +9 -1
  18. package/dist/cjs/idb.js.map +1 -1
  19. package/dist/cjs/metadata/openMetadataDatabase.d.ts +11 -2
  20. package/dist/cjs/metadata/openMetadataDatabase.js +56 -3
  21. package/dist/cjs/metadata/openMetadataDatabase.js.map +1 -1
  22. package/dist/cjs/migration/db.d.ts +1 -1
  23. package/dist/cjs/migration/db.js +5 -2
  24. package/dist/cjs/migration/db.js.map +1 -1
  25. package/dist/cjs/migration/openDatabase.d.ts +8 -0
  26. package/dist/cjs/migration/openDatabase.js +216 -153
  27. package/dist/cjs/migration/openDatabase.js.map +1 -1
  28. package/dist/cjs/queries/BaseQuery.js +12 -1
  29. package/dist/cjs/queries/BaseQuery.js.map +1 -1
  30. package/dist/cjs/sync/WebSocketSync.js +4 -3
  31. package/dist/cjs/sync/WebSocketSync.js.map +1 -1
  32. package/dist/esm/__tests__/queries.test.js +2 -0
  33. package/dist/esm/__tests__/queries.test.js.map +1 -1
  34. package/dist/esm/client/Client.js +23 -11
  35. package/dist/esm/client/Client.js.map +1 -1
  36. package/dist/esm/client/ClientDescriptor.d.ts +3 -0
  37. package/dist/esm/client/ClientDescriptor.js +104 -40
  38. package/dist/esm/client/ClientDescriptor.js.map +1 -1
  39. package/dist/esm/entities/DocumentFamiliyCache.d.ts +20 -1
  40. package/dist/esm/entities/DocumentFamiliyCache.js +33 -18
  41. package/dist/esm/entities/DocumentFamiliyCache.js.map +1 -1
  42. package/dist/esm/entities/EntityStore.d.ts +2 -1
  43. package/dist/esm/entities/EntityStore.js +36 -7
  44. package/dist/esm/entities/EntityStore.js.map +1 -1
  45. package/dist/esm/idb.d.ts +2 -0
  46. package/dist/esm/idb.js +6 -0
  47. package/dist/esm/idb.js.map +1 -1
  48. package/dist/esm/metadata/openMetadataDatabase.d.ts +11 -2
  49. package/dist/esm/metadata/openMetadataDatabase.js +54 -2
  50. package/dist/esm/metadata/openMetadataDatabase.js.map +1 -1
  51. package/dist/esm/migration/db.d.ts +1 -1
  52. package/dist/esm/migration/db.js +5 -2
  53. package/dist/esm/migration/db.js.map +1 -1
  54. package/dist/esm/migration/openDatabase.d.ts +8 -0
  55. package/dist/esm/migration/openDatabase.js +213 -151
  56. package/dist/esm/migration/openDatabase.js.map +1 -1
  57. package/dist/esm/queries/BaseQuery.js +12 -1
  58. package/dist/esm/queries/BaseQuery.js.map +1 -1
  59. package/dist/esm/sync/WebSocketSync.js +4 -3
  60. package/dist/esm/sync/WebSocketSync.js.map +1 -1
  61. package/dist/tsconfig-cjs.tsbuildinfo +1 -1
  62. package/dist/tsconfig.tsbuildinfo +1 -1
  63. package/package.json +2 -2
  64. package/src/__tests__/queries.test.ts +3 -0
  65. package/src/client/Client.ts +26 -12
  66. package/src/client/ClientDescriptor.ts +145 -49
  67. package/src/entities/DocumentFamiliyCache.ts +59 -19
  68. package/src/entities/EntityStore.ts +45 -6
  69. package/src/idb.ts +10 -0
  70. package/src/metadata/openMetadataDatabase.ts +96 -13
  71. package/src/migration/db.ts +14 -1
  72. package/src/migration/openDatabase.ts +344 -203
  73. package/src/queries/BaseQuery.ts +14 -1
  74. 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,220 +76,359 @@ export async function openDocumentDatabase({
74
76
  });
75
77
 
76
78
  if (toRun.length > 0) {
77
- await acquireLock(context.namespace, async () => {
78
- // now the fun part
79
- for (const migration of toRun as Migration<any>[]) {
80
- // special case: if this is the version 1 migration, we have no pre-existing database
81
- // to use for the migration.
82
- let engine: MigrationEngine<any, any>;
83
- // migrations from 0 (i.e. initial migrations) don't attempt to open an existing db
84
- if (migration.oldSchema.version === 0) {
85
- engine = getInitialMigrationEngine({
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
- // this will only write to our metadata store via operations!
101
- engine = getMigrationEngine({
102
- meta,
103
- migration,
104
- context: {
105
- ...context,
106
- documentDb: originalDatabase,
107
- },
108
- });
109
- try {
110
- await migration.migrate(engine);
111
- // wait on any out-of-band async operations to complete
112
- await Promise.all(engine.awaitables);
113
- } catch (err) {
114
- context.log(
115
- 'critical',
116
- `Migration failed (${migration.oldSchema.version} -> ${migration.newSchema.version})`,
117
- err,
118
- );
119
- throw err;
120
- }
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
+ );
121
111
 
122
- // now we have to open the database again with the next version and
123
- // make the appropriate schema changes during the upgrade.
124
- await closeDatabase(originalDatabase);
125
- }
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`);
126
116
 
127
- await upgradeDatabase(
128
- indexedDB,
129
- context.namespace,
130
- migration.newSchema.version,
131
- (transaction, db) => {
132
- for (const newCollection of migration.addedCollections) {
133
- db.createObjectStore(newCollection, {
134
- keyPath:
135
- migration.newSchema.collections[newCollection].primaryKey,
136
- autoIncrement: false,
137
- });
138
- }
117
+ // first we need to copy the data from the production database to the WIP database
118
+ // at the current (non-wip) version.
139
119
 
140
- for (const collection of migration.allCollections) {
141
- const store = transaction.objectStore(collection);
142
- // apply new indexes
143
- for (const newIndex of migration.addedIndexes[collection] || []) {
144
- store.createIndex(newIndex.name, newIndex.name, {
145
- multiEntry: newIndex.multiEntry,
146
- });
147
- }
148
- // remove old indexes
149
- for (const oldIndex of migration.removedIndexes[collection] ||
150
- []) {
151
- store.deleteIndex(oldIndex.name);
152
- }
153
- }
154
- for (const removedCollection of migration.removedCollections) {
155
- // !! can't delete the store, because old operations that relate to
156
- // this store may still exist in history. instead, we can clear it out
157
- // and leave it in place
158
- transaction.objectStore(removedCollection).clear();
159
- }
160
- },
161
- context.log,
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
+ }),
162
180
  );
181
+ }
182
+ }
163
183
 
164
- /**
165
- * In cases where operations from the future have been
166
- * received by this client, we may have created entire
167
- * documents in metadata which were not written to storage
168
- * because all of their operations were in the future (
169
- * i.e. in the next version). We have to find those documents
170
- * and also write their snapshots to storage, because they
171
- * won't be present in storage already to 'refresh,' so
172
- * if we don't analyze metadata for 'future' operations like
173
- * this, we won't know they exist.
174
- *
175
- * This led to behavior where the metadata would be properly
176
- * synced, but after upgrading the app and migrating, items
177
- * would be missing from findAll and findOne queries.
178
- */
179
- const docsWithUnappliedMigrations =
180
- await getDocsWithUnappliedMigrations({
181
- meta,
182
- currentVersion: migration.oldSchema.version,
183
- newVersion: migration.newSchema.version,
184
- });
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
+ }
185
200
 
186
- // once the schema is ready, we can write back the migrated documents
187
- const upgradedDatabase = await openDatabase(
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(
188
235
  indexedDB,
189
- context.namespace,
190
- migration.newSchema.version,
236
+ namespace,
237
+ migration.oldSchema.version,
238
+ context.log,
191
239
  );
192
- for (const collection of migration.allCollections) {
193
- // first step is to read in all the keys we need to rewrite
194
- const documentReadTransaction = upgradedDatabase.transaction(
195
- collection,
196
- 'readwrite',
197
- );
198
- const readStore = documentReadTransaction.objectStore(collection);
199
- const keys = await getAllKeys(readStore);
200
- // map the keys to OIDs
201
- const oids = keys.map((key) => createOid(collection, `${key}`));
202
- oids.push(
203
- ...engine.newOids.filter((oid) => {
204
- return decomposeOid(oid).collection === collection;
205
- }),
206
- ...docsWithUnappliedMigrations.filter((oid) => {
207
- return decomposeOid(oid).collection === collection;
208
- }),
209
- );
210
240
 
211
- const snapshots = await Promise.all(
212
- oids.map(async (oid) => {
213
- try {
214
- const snap = await meta.getDocumentSnapshot(oid, {
215
- to: meta.time.now(migration.newSchema.version),
216
- });
217
- return [oid, snap];
218
- } catch (e) {
219
- // this seems to happen with baselines/ops which are not fully
220
- // cleaned up after deletion?
221
- context.log(
222
- 'error',
223
- 'Could not regenerate snapshot during migration for oid',
224
- oid,
225
- 'this document will not be preserved',
226
- e,
227
- );
228
- return null;
229
- }
230
- }),
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 {
251
+ await migration.migrate(engine);
252
+ // wait on any out-of-band async operations to complete
253
+ await Promise.all(engine.awaitables);
254
+ } catch (err) {
255
+ context.log(
256
+ 'critical',
257
+ `Migration failed (${migration.oldSchema.version} -> ${migration.newSchema.version})`,
258
+ err,
231
259
  );
260
+ throw err;
261
+ }
232
262
 
233
- const views = snapshots
234
- .filter((s): s is [string, any] => !!s)
235
- .map(([oid, snapshot]) => {
236
- if (!snapshot) return [oid, undefined];
237
- const view = assignIndexValues(
238
- migration.newSchema.collections[collection],
239
- snapshot,
240
- );
241
- // TODO: remove the need for this by only storing index values!
242
- assignOidPropertiesToAllSubObjects(view);
243
- return [oid, view];
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,
244
285
  });
286
+ }
245
287
 
246
- // now we can write the documents back
247
- const documentWriteTransaction = upgradedDatabase.transaction(
248
- collection,
249
- 'readwrite',
250
- );
251
- const writeStore = documentWriteTransaction.objectStore(collection);
252
- await Promise.all(
253
- views.map(([oid, view]) => {
254
- if (view) {
255
- return putView(writeStore, view);
256
- } else {
257
- const { id } = decomposeOid(oid);
258
- return deleteView(writeStore, id);
259
- }
260
- }),
261
- );
262
- }
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
+ });
295
+ }
296
+ // remove old indexes
297
+ for (const oldIndex of migration.removedIndexes[collection] || []) {
298
+ store.deleteIndex(oldIndex.name);
299
+ }
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
+ );
263
310
 
264
- await closeDatabase(upgradedDatabase);
265
-
266
- context.log(`
267
- ⬆️ v${migration.newSchema.version} Migration complete. Here's the rundown:
268
- - Added collections: ${migration.addedCollections.join(', ')}
269
- - Removed collections: ${migration.removedCollections.join(', ')}
270
- - Changed collections: ${migration.changedCollections.join(', ')}
271
- - New indexes: ${Object.keys(migration.addedIndexes)
272
- .map((col) =>
273
- migration.addedIndexes[col].map((i) => `${col}.${i.name}`),
274
- )
275
- .flatMap((i) => i)
276
- .join(', ')}
277
- - Removed indexes: ${Object.keys(migration.removedIndexes)
278
- .map((col) =>
279
- migration.removedIndexes[col].map((i) => `${col}.${i.name}`),
280
- )
281
- .flatMap((i) => i)
282
- .join(', ')}
283
- `);
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
+ });
331
+
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
+ }),
356
+ );
357
+
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,
372
+ );
373
+ return null;
374
+ }
375
+ }),
376
+ );
377
+
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
+ });
390
+
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
+ );
284
407
  }
285
- });
286
- return openDatabase(indexedDB, context.namespace, version);
287
- } else {
288
- // just open the database
289
- return openDatabase(indexedDB, context.namespace, version);
290
- }
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
+ });
291
432
  }
292
433
 
293
434
  function getMigrationMutations({
@@ -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().then(() => this._value);
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>;
@@ -216,6 +216,7 @@ export class WebSocketSync
216
216
  };
217
217
 
218
218
  dispose = () => {
219
+ this.socket?.removeEventListener('message', this.onMessage);
219
220
  this.socket?.removeEventListener('close', this.onClose);
220
221
  this.socket?.close();
221
222
  };