@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.
- 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/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/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 +216 -153
- 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/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/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 +213 -151
- 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/EntityStore.ts +45 -6
- package/src/idb.ts +10 -0
- package/src/metadata/openMetadataDatabase.ts +96 -13
- package/src/migration/db.ts +14 -1
- package/src/migration/openDatabase.ts +344 -203
- 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,220 +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
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
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
|
-
|
|
123
|
-
|
|
124
|
-
|
|
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
|
-
|
|
128
|
-
|
|
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
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
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
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
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
|
-
|
|
187
|
-
|
|
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
|
-
|
|
190
|
-
migration.
|
|
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
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
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
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
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
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
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
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
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
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
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({
|
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>;
|