@verdant-web/store 3.8.0 → 3.8.1
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/LICENSE +650 -21
- package/dist/bundle/index.js +12 -12
- package/dist/bundle/index.js.map +4 -4
- package/dist/esm/client/Client.d.ts +3 -3
- package/dist/esm/client/Client.js +5 -5
- package/dist/esm/client/Client.js.map +1 -1
- package/dist/esm/client/ClientDescriptor.d.ts +1 -0
- package/dist/esm/client/ClientDescriptor.js +6 -3
- package/dist/esm/client/ClientDescriptor.js.map +1 -1
- package/dist/esm/context.d.ts +1 -0
- package/dist/esm/entities/EntityStore.d.ts +3 -3
- package/dist/esm/entities/EntityStore.js +5 -0
- package/dist/esm/entities/EntityStore.js.map +1 -1
- package/dist/esm/idb.d.ts +6 -0
- package/dist/esm/idb.js +17 -1
- package/dist/esm/idb.js.map +1 -1
- package/dist/esm/migration/db.d.ts +9 -3
- package/dist/esm/migration/db.js +23 -11
- package/dist/esm/migration/db.js.map +1 -1
- package/dist/esm/migration/engine.d.ts +15 -0
- package/dist/esm/migration/engine.js +159 -0
- package/dist/esm/migration/engine.js.map +1 -0
- package/dist/esm/migration/migrations.d.ts +17 -0
- package/dist/esm/migration/migrations.js +242 -0
- package/dist/esm/migration/migrations.js.map +1 -0
- package/dist/esm/migration/openQueryDatabase.d.ts +10 -0
- package/dist/esm/migration/openQueryDatabase.js +27 -0
- package/dist/esm/migration/openQueryDatabase.js.map +1 -0
- package/dist/esm/migration/openWIPDatabase.d.ts +11 -0
- package/dist/esm/migration/openWIPDatabase.js +65 -0
- package/dist/esm/migration/openWIPDatabase.js.map +1 -0
- package/dist/esm/migration/types.d.ts +3 -0
- package/dist/esm/migration/types.js +2 -0
- package/dist/esm/migration/types.js.map +1 -0
- package/dist/esm/queries/QueryableStorage.js +1 -1
- package/dist/esm/queries/QueryableStorage.js.map +1 -1
- package/dist/esm/sync/PushPullSync.d.ts +3 -2
- package/dist/esm/sync/PushPullSync.js +7 -1
- package/dist/esm/sync/PushPullSync.js.map +1 -1
- package/package.json +2 -2
- package/src/client/Client.ts +6 -8
- package/src/client/ClientDescriptor.ts +7 -6
- package/src/context.ts +1 -0
- package/src/entities/EntityStore.ts +8 -2
- package/src/idb.ts +20 -1
- package/src/migration/db.ts +62 -20
- package/src/migration/engine.ts +248 -0
- package/src/migration/migrations.ts +347 -0
- package/src/migration/openQueryDatabase.ts +63 -0
- package/src/migration/openWIPDatabase.ts +97 -0
- package/src/migration/types.ts +4 -0
- package/src/queries/QueryableStorage.ts +1 -1
- package/src/sync/PushPullSync.ts +10 -0
- package/dist/esm/migration/openDatabase.d.ts +0 -20
- package/dist/esm/migration/openDatabase.js +0 -463
- package/dist/esm/migration/openDatabase.js.map +0 -1
- package/src/migration/openDatabase.ts +0 -749
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Migration,
|
|
3
|
+
MigrationEngine,
|
|
4
|
+
createOid,
|
|
5
|
+
decomposeOid,
|
|
6
|
+
getIndexValues,
|
|
7
|
+
getOidRoot,
|
|
8
|
+
} from '@verdant-web/common';
|
|
9
|
+
import { Metadata } from '../metadata/Metadata.js';
|
|
10
|
+
import { ClientOperation } from '../metadata/OperationsStore.js';
|
|
11
|
+
import { acquireLock, openDatabase, upgradeDatabase } from './db.js';
|
|
12
|
+
import { getInitialMigrationEngine, getMigrationEngine } from './engine.js';
|
|
13
|
+
import { OpenDocumentDbContext } from './types.js';
|
|
14
|
+
import { closeDatabase } from '../idb.js';
|
|
15
|
+
|
|
16
|
+
const globalIDB =
|
|
17
|
+
typeof window !== 'undefined' ? window.indexedDB : (undefined as any);
|
|
18
|
+
|
|
19
|
+
export async function runMigrations({
|
|
20
|
+
context,
|
|
21
|
+
toRun,
|
|
22
|
+
meta,
|
|
23
|
+
indexedDB = globalIDB,
|
|
24
|
+
namespace = context.namespace,
|
|
25
|
+
}: {
|
|
26
|
+
context: OpenDocumentDbContext;
|
|
27
|
+
toRun: Migration<any>[];
|
|
28
|
+
meta: Metadata;
|
|
29
|
+
indexedDB?: IDBFactory;
|
|
30
|
+
/** This namespace value controls where the database being migrated is. */
|
|
31
|
+
namespace?: string;
|
|
32
|
+
}) {
|
|
33
|
+
await acquireLock(namespace, async () => {
|
|
34
|
+
// now the fun part
|
|
35
|
+
for (const migration of toRun) {
|
|
36
|
+
// special case: if this is the version 1 migration, we have no pre-existing database
|
|
37
|
+
// to use for the migration.
|
|
38
|
+
let engine: MigrationEngine;
|
|
39
|
+
// migrations from 0 (i.e. initial migrations) don't attempt to open an existing db
|
|
40
|
+
if (migration.oldSchema.version === 0) {
|
|
41
|
+
engine = getInitialMigrationEngine({
|
|
42
|
+
meta,
|
|
43
|
+
migration,
|
|
44
|
+
context,
|
|
45
|
+
});
|
|
46
|
+
await migration.migrate(engine);
|
|
47
|
+
} else {
|
|
48
|
+
// open the database with the current (old) version for this migration. this should
|
|
49
|
+
// align with the database's current version.
|
|
50
|
+
const originalDatabase = await openDatabase({
|
|
51
|
+
indexedDB,
|
|
52
|
+
namespace,
|
|
53
|
+
version: migration.oldSchema.version,
|
|
54
|
+
context,
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// this will only write to our metadata store via operations!
|
|
58
|
+
engine = getMigrationEngine({
|
|
59
|
+
meta,
|
|
60
|
+
migration,
|
|
61
|
+
context: {
|
|
62
|
+
...context,
|
|
63
|
+
documentDb: originalDatabase,
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
try {
|
|
67
|
+
await migration.migrate(engine);
|
|
68
|
+
// wait on any out-of-band async operations to complete
|
|
69
|
+
await Promise.all(engine.awaitables);
|
|
70
|
+
} catch (err) {
|
|
71
|
+
context.log(
|
|
72
|
+
'critical',
|
|
73
|
+
`Migration failed (${migration.oldSchema.version} -> ${migration.newSchema.version})`,
|
|
74
|
+
err,
|
|
75
|
+
);
|
|
76
|
+
if (err instanceof Error) {
|
|
77
|
+
throw err;
|
|
78
|
+
} else {
|
|
79
|
+
throw new Error('Unknown error during migration');
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// now we have to open the database again with the next version and
|
|
84
|
+
// make the appropriate schema changes during the upgrade.
|
|
85
|
+
await closeDatabase(originalDatabase);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
context.log(
|
|
89
|
+
'debug',
|
|
90
|
+
'Upgrading database',
|
|
91
|
+
namespace,
|
|
92
|
+
'to version',
|
|
93
|
+
migration.newSchema.version,
|
|
94
|
+
);
|
|
95
|
+
const upgradedDatabase = await applySchemaToDatabase({
|
|
96
|
+
migration,
|
|
97
|
+
indexedDB,
|
|
98
|
+
namespace,
|
|
99
|
+
context,
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* In cases where operations from the future have been
|
|
104
|
+
* received by this client, we may have created entire
|
|
105
|
+
* documents in metadata which were not written to storage
|
|
106
|
+
* because all of their operations were in the future (
|
|
107
|
+
* i.e. in the next version). We have to find those documents
|
|
108
|
+
* and also write their snapshots to storage, because they
|
|
109
|
+
* won't be present in storage already to 'refresh,' so
|
|
110
|
+
* if we don't analyze metadata for 'future' operations like
|
|
111
|
+
* this, we won't know they exist.
|
|
112
|
+
*
|
|
113
|
+
* This led to behavior where the metadata would be properly
|
|
114
|
+
* synced, but after upgrading the app and migrating, items
|
|
115
|
+
* would be missing from findAll and findOne queries.
|
|
116
|
+
*/
|
|
117
|
+
const docsWithUnappliedMigrations = await getDocsWithUnappliedMigrations({
|
|
118
|
+
meta,
|
|
119
|
+
currentVersion: migration.oldSchema.version,
|
|
120
|
+
newVersion: migration.newSchema.version,
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
// once the schema is ready, we can write back the migrated documents
|
|
124
|
+
|
|
125
|
+
for (const collection of migration.allCollections) {
|
|
126
|
+
// first step is to read in all the keys we need to rewrite
|
|
127
|
+
const documentReadTransaction = upgradedDatabase.transaction(
|
|
128
|
+
collection,
|
|
129
|
+
'readwrite',
|
|
130
|
+
);
|
|
131
|
+
const readStore = documentReadTransaction.objectStore(collection);
|
|
132
|
+
const keys = await getAllKeys(readStore);
|
|
133
|
+
// map the keys to OIDs
|
|
134
|
+
const oids = keys.map((key) => createOid(collection, `${key}`));
|
|
135
|
+
oids.push(
|
|
136
|
+
...engine.newOids.filter((oid) => {
|
|
137
|
+
return decomposeOid(oid).collection === collection;
|
|
138
|
+
}),
|
|
139
|
+
...docsWithUnappliedMigrations.filter((oid) => {
|
|
140
|
+
return decomposeOid(oid).collection === collection;
|
|
141
|
+
}),
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
// add 'touch' operations to all root OIDs of all documents.
|
|
145
|
+
// this marks documents which have undergone a migration
|
|
146
|
+
// so that other clients know when they're working
|
|
147
|
+
// with unmigrated data - by seeing that there are no
|
|
148
|
+
// existing operations or baselines with a timestamp
|
|
149
|
+
// that matches the current version.
|
|
150
|
+
// UPDATE: no longer necessary now that pruning is a thing.
|
|
151
|
+
// await Promise.all(
|
|
152
|
+
// oids.map((oid) =>
|
|
153
|
+
// meta.insertLocalOperations([
|
|
154
|
+
// {
|
|
155
|
+
// oid,
|
|
156
|
+
// timestamp: meta.time.zero(migration.version),
|
|
157
|
+
// data: { op: 'touch' },
|
|
158
|
+
// },
|
|
159
|
+
// ]),
|
|
160
|
+
// ),
|
|
161
|
+
// );
|
|
162
|
+
|
|
163
|
+
const snapshots = await Promise.all(
|
|
164
|
+
oids.map(async (oid) => {
|
|
165
|
+
try {
|
|
166
|
+
const snap = await meta.getDocumentSnapshot(oid);
|
|
167
|
+
return [oid, snap];
|
|
168
|
+
} catch (e) {
|
|
169
|
+
// this seems to happen with baselines/ops which are not fully
|
|
170
|
+
// cleaned up after deletion?
|
|
171
|
+
context.log(
|
|
172
|
+
'error',
|
|
173
|
+
'Could not regenerate snapshot during migration for oid',
|
|
174
|
+
oid,
|
|
175
|
+
'this document will not be preserved',
|
|
176
|
+
e,
|
|
177
|
+
);
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
180
|
+
}),
|
|
181
|
+
);
|
|
182
|
+
|
|
183
|
+
const views = snapshots
|
|
184
|
+
.filter((s): s is [string, any] => !!s)
|
|
185
|
+
.map(([oid, snapshot]) => {
|
|
186
|
+
if (!snapshot) return [oid, undefined];
|
|
187
|
+
const view = getIndexValues(
|
|
188
|
+
migration.newSchema.collections[collection],
|
|
189
|
+
snapshot,
|
|
190
|
+
);
|
|
191
|
+
return [oid, view];
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
// now we can write the documents back
|
|
195
|
+
const documentWriteTransaction = upgradedDatabase.transaction(
|
|
196
|
+
collection,
|
|
197
|
+
'readwrite',
|
|
198
|
+
);
|
|
199
|
+
const writeStore = documentWriteTransaction.objectStore(collection);
|
|
200
|
+
await Promise.all(
|
|
201
|
+
views.map(([oid, view]) => {
|
|
202
|
+
if (view) {
|
|
203
|
+
return putView(writeStore, view);
|
|
204
|
+
} else {
|
|
205
|
+
const { id } = decomposeOid(oid);
|
|
206
|
+
return deleteView(writeStore, id);
|
|
207
|
+
}
|
|
208
|
+
}),
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
await closeDatabase(upgradedDatabase);
|
|
213
|
+
|
|
214
|
+
context.log('debug', `Migration of ${namespace} complete.`);
|
|
215
|
+
context.log(`
|
|
216
|
+
⬆️ v${migration.newSchema.version} Migration complete. Here's the rundown:
|
|
217
|
+
- Added collections: ${migration.addedCollections.join(', ')}
|
|
218
|
+
- Removed collections: ${migration.removedCollections.join(', ')}
|
|
219
|
+
- Changed collections: ${migration.changedCollections.join(', ')}
|
|
220
|
+
- New indexes: ${Object.keys(migration.addedIndexes)
|
|
221
|
+
.map((col) =>
|
|
222
|
+
migration.addedIndexes[col].map((i) => `${col}.${i.name}`),
|
|
223
|
+
)
|
|
224
|
+
.flatMap((i) => i)
|
|
225
|
+
.join(', ')}
|
|
226
|
+
- Removed indexes: ${Object.keys(migration.removedIndexes)
|
|
227
|
+
.map((col) =>
|
|
228
|
+
migration.removedIndexes[col].map((i) => `${col}.${i.name}`),
|
|
229
|
+
)
|
|
230
|
+
.flatMap((i) => i)
|
|
231
|
+
.join(', ')}
|
|
232
|
+
`);
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
async function getAllKeys(store: IDBObjectStore) {
|
|
238
|
+
return new Promise<IDBValidKey[]>((resolve, reject) => {
|
|
239
|
+
const request = store.getAllKeys();
|
|
240
|
+
request.onsuccess = (event) => {
|
|
241
|
+
resolve(request.result);
|
|
242
|
+
};
|
|
243
|
+
request.onerror = (event) => {
|
|
244
|
+
reject(request.error);
|
|
245
|
+
};
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
async function deleteView(store: IDBObjectStore, id: string) {
|
|
250
|
+
const request = store.delete(id);
|
|
251
|
+
return new Promise<void>((resolve, reject) => {
|
|
252
|
+
request.onsuccess = (event) => {
|
|
253
|
+
resolve();
|
|
254
|
+
};
|
|
255
|
+
request.onerror = (event) => {
|
|
256
|
+
reject(request.error);
|
|
257
|
+
};
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
async function putView(store: IDBObjectStore, view: any) {
|
|
262
|
+
const request = store.put(view);
|
|
263
|
+
return new Promise<void>((resolve, reject) => {
|
|
264
|
+
request.onsuccess = (event) => {
|
|
265
|
+
resolve();
|
|
266
|
+
};
|
|
267
|
+
request.onerror = (event) => {
|
|
268
|
+
reject(request.error);
|
|
269
|
+
};
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Gets a list of root OIDs for all documents which had operations stored already
|
|
275
|
+
* that were not applied to their queryable snapshots because they were in the
|
|
276
|
+
* future. These documents need to be refreshed in storage.
|
|
277
|
+
*/
|
|
278
|
+
async function getDocsWithUnappliedMigrations({
|
|
279
|
+
meta,
|
|
280
|
+
currentVersion,
|
|
281
|
+
newVersion: _,
|
|
282
|
+
}: {
|
|
283
|
+
currentVersion: number;
|
|
284
|
+
newVersion: number;
|
|
285
|
+
meta: Metadata;
|
|
286
|
+
}) {
|
|
287
|
+
// scan for all operations in metadata after the current version.
|
|
288
|
+
// this could be more efficient if also filtering below or equal newVersion but
|
|
289
|
+
// that seems so unlikely in practice...
|
|
290
|
+
const unappliedOperations: ClientOperation[] = [];
|
|
291
|
+
await meta.operations.iterateOverAllOperations(
|
|
292
|
+
(op) => unappliedOperations.push(op),
|
|
293
|
+
{
|
|
294
|
+
from: meta.time.zero(currentVersion + 1),
|
|
295
|
+
},
|
|
296
|
+
);
|
|
297
|
+
return Array.from(
|
|
298
|
+
new Set(unappliedOperations.map((op) => getOidRoot(op.oid))),
|
|
299
|
+
);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
export function applySchemaToDatabase({
|
|
303
|
+
migration,
|
|
304
|
+
indexedDB = globalIDB,
|
|
305
|
+
namespace,
|
|
306
|
+
context,
|
|
307
|
+
}: {
|
|
308
|
+
migration: Migration<any>;
|
|
309
|
+
indexedDB?: IDBFactory;
|
|
310
|
+
namespace: string;
|
|
311
|
+
context: OpenDocumentDbContext;
|
|
312
|
+
}) {
|
|
313
|
+
return upgradeDatabase(
|
|
314
|
+
indexedDB,
|
|
315
|
+
namespace,
|
|
316
|
+
migration.newSchema.version,
|
|
317
|
+
(transaction, db) => {
|
|
318
|
+
for (const newCollection of migration.addedCollections) {
|
|
319
|
+
db.createObjectStore(newCollection, {
|
|
320
|
+
keyPath: migration.newSchema.collections[newCollection].primaryKey,
|
|
321
|
+
autoIncrement: false,
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
for (const collection of migration.allCollections) {
|
|
326
|
+
const store = transaction.objectStore(collection);
|
|
327
|
+
// apply new indexes
|
|
328
|
+
for (const newIndex of migration.addedIndexes[collection] || []) {
|
|
329
|
+
store.createIndex(newIndex.name, newIndex.name, {
|
|
330
|
+
multiEntry: newIndex.multiEntry,
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
// remove old indexes
|
|
334
|
+
for (const oldIndex of migration.removedIndexes[collection] || []) {
|
|
335
|
+
store.deleteIndex(oldIndex.name);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
for (const removedCollection of migration.removedCollections) {
|
|
339
|
+
// !! can't delete the store, because old operations that relate to
|
|
340
|
+
// this store may still exist in history. instead, we can clear it out
|
|
341
|
+
// and leave it in place
|
|
342
|
+
transaction.objectStore(removedCollection).clear();
|
|
343
|
+
}
|
|
344
|
+
},
|
|
345
|
+
context.log,
|
|
346
|
+
);
|
|
347
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { Migration } from '@verdant-web/common';
|
|
2
|
+
import { Metadata } from '../metadata/Metadata.js';
|
|
3
|
+
import { getDatabaseVersion, openDatabase } from './db.js';
|
|
4
|
+
import { runMigrations } from './migrations.js';
|
|
5
|
+
import { getMigrationPath } from './paths.js';
|
|
6
|
+
import { OpenDocumentDbContext } from './types.js';
|
|
7
|
+
|
|
8
|
+
const globalIDB =
|
|
9
|
+
typeof window !== 'undefined' ? window.indexedDB : (undefined as any);
|
|
10
|
+
|
|
11
|
+
export async function openQueryDatabase({
|
|
12
|
+
version,
|
|
13
|
+
indexedDB = globalIDB,
|
|
14
|
+
migrations,
|
|
15
|
+
meta,
|
|
16
|
+
context,
|
|
17
|
+
}: {
|
|
18
|
+
version: number;
|
|
19
|
+
migrations: Migration<any>[];
|
|
20
|
+
indexedDB?: IDBFactory;
|
|
21
|
+
meta: Metadata;
|
|
22
|
+
context: OpenDocumentDbContext;
|
|
23
|
+
}) {
|
|
24
|
+
if (context.schema.wip) {
|
|
25
|
+
throw new Error('Cannot open a production client with a WIP schema!');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const currentVersion = await getDatabaseVersion(
|
|
29
|
+
indexedDB,
|
|
30
|
+
context.namespace,
|
|
31
|
+
version,
|
|
32
|
+
context.log,
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
context.log(
|
|
36
|
+
'debug',
|
|
37
|
+
'Current database version:',
|
|
38
|
+
currentVersion,
|
|
39
|
+
'target version:',
|
|
40
|
+
version,
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
const toRun = getMigrationPath({
|
|
44
|
+
currentVersion,
|
|
45
|
+
targetVersion: version,
|
|
46
|
+
migrations,
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
if (toRun.length > 0) {
|
|
50
|
+
context.log(
|
|
51
|
+
'debug',
|
|
52
|
+
'Migrations to run:',
|
|
53
|
+
toRun.map((m) => m.version),
|
|
54
|
+
);
|
|
55
|
+
await runMigrations({ context, toRun, meta, indexedDB });
|
|
56
|
+
}
|
|
57
|
+
return openDatabase({
|
|
58
|
+
indexedDB,
|
|
59
|
+
namespace: context.namespace,
|
|
60
|
+
version,
|
|
61
|
+
context,
|
|
62
|
+
});
|
|
63
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { Migration } from '@verdant-web/common';
|
|
2
|
+
import { Metadata } from '../metadata/Metadata.js';
|
|
3
|
+
import { copyAll, getDatabaseVersion, openDatabase } from './db.js';
|
|
4
|
+
import { runMigrations } from './migrations.js';
|
|
5
|
+
import { getMigrationPath } from './paths.js';
|
|
6
|
+
import { OpenDocumentDbContext } from './types.js';
|
|
7
|
+
|
|
8
|
+
const globalIDB =
|
|
9
|
+
typeof window !== 'undefined' ? window.indexedDB : (undefined as any);
|
|
10
|
+
|
|
11
|
+
export async function openWIPDatabase({
|
|
12
|
+
version,
|
|
13
|
+
indexedDB = globalIDB,
|
|
14
|
+
migrations,
|
|
15
|
+
meta,
|
|
16
|
+
context,
|
|
17
|
+
wipNamespace,
|
|
18
|
+
}: {
|
|
19
|
+
version: number;
|
|
20
|
+
migrations: Migration<any>[];
|
|
21
|
+
indexedDB?: IDBFactory;
|
|
22
|
+
meta: Metadata;
|
|
23
|
+
context: OpenDocumentDbContext;
|
|
24
|
+
wipNamespace: string;
|
|
25
|
+
}) {
|
|
26
|
+
context.log('debug', 'Opening WIP database', wipNamespace);
|
|
27
|
+
const currentWIPVersion = await getDatabaseVersion(
|
|
28
|
+
indexedDB,
|
|
29
|
+
wipNamespace,
|
|
30
|
+
version,
|
|
31
|
+
context.log,
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
if (currentWIPVersion === version) {
|
|
35
|
+
context.log('info', `WIP schema is up-to-date; not refreshing database`);
|
|
36
|
+
} else {
|
|
37
|
+
context.log('info', `WIP schema is out-of-date; refreshing database`);
|
|
38
|
+
|
|
39
|
+
// first we need to copy the data from the production database to the WIP database
|
|
40
|
+
// at the current (non-wip) version.
|
|
41
|
+
|
|
42
|
+
const initialToRun = getMigrationPath({
|
|
43
|
+
currentVersion: currentWIPVersion,
|
|
44
|
+
targetVersion: version - 1,
|
|
45
|
+
migrations,
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
if (initialToRun.length > 0) {
|
|
49
|
+
await runMigrations({
|
|
50
|
+
context,
|
|
51
|
+
toRun: initialToRun,
|
|
52
|
+
meta,
|
|
53
|
+
indexedDB,
|
|
54
|
+
namespace: wipNamespace,
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// now, we copy the data from the main database.
|
|
58
|
+
const mainDatabase = await openDatabase({
|
|
59
|
+
indexedDB,
|
|
60
|
+
namespace: context.namespace,
|
|
61
|
+
version: version - 1,
|
|
62
|
+
context,
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
const wipDatabase = await openDatabase({
|
|
66
|
+
indexedDB,
|
|
67
|
+
namespace: wipNamespace,
|
|
68
|
+
version: version - 1,
|
|
69
|
+
context,
|
|
70
|
+
});
|
|
71
|
+
await copyAll(mainDatabase, wipDatabase);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const toRun = getMigrationPath({
|
|
75
|
+
currentVersion: version - 1,
|
|
76
|
+
targetVersion: version,
|
|
77
|
+
migrations,
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
if (toRun.length > 0) {
|
|
81
|
+
await runMigrations({
|
|
82
|
+
context,
|
|
83
|
+
toRun,
|
|
84
|
+
meta,
|
|
85
|
+
indexedDB,
|
|
86
|
+
namespace: wipNamespace,
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return openDatabase({
|
|
92
|
+
indexedDB,
|
|
93
|
+
namespace: wipNamespace,
|
|
94
|
+
version,
|
|
95
|
+
context,
|
|
96
|
+
});
|
|
97
|
+
}
|
|
@@ -25,7 +25,7 @@ export class QueryableStorage extends IDBService {
|
|
|
25
25
|
*/
|
|
26
26
|
reset = async () => {
|
|
27
27
|
const allCollections = Object.keys(this.ctx.schema.collections);
|
|
28
|
-
const tx = this.
|
|
28
|
+
const tx = this.createTransaction(allCollections, { mode: 'readwrite' });
|
|
29
29
|
await Promise.all(
|
|
30
30
|
allCollections.map((collection) => {
|
|
31
31
|
const store = tx.objectStore(collection);
|
package/src/sync/PushPullSync.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import {
|
|
2
2
|
ClientMessage,
|
|
3
3
|
EventSubscriber,
|
|
4
|
+
PresenceUpdateMessage,
|
|
4
5
|
ServerMessage,
|
|
5
6
|
VerdantErrorCode,
|
|
7
|
+
debounce,
|
|
6
8
|
isVerdantErrorResponse,
|
|
9
|
+
throttle,
|
|
7
10
|
} from '@verdant-web/common';
|
|
8
11
|
import { Metadata } from '../metadata/Metadata.js';
|
|
9
12
|
import { PresenceManager } from './PresenceManager.js';
|
|
@@ -144,10 +147,17 @@ export class PushPullSync
|
|
|
144
147
|
this.emit('message', message);
|
|
145
148
|
};
|
|
146
149
|
|
|
150
|
+
// reduce rate of presence messages sent; each one would trigger an HTTP
|
|
151
|
+
// request, which is not ideal if presence is updating rapidly.
|
|
152
|
+
throttledPresenceUpdate = throttle((message: PresenceUpdateMessage) => {
|
|
153
|
+
this.sendRequest([message]);
|
|
154
|
+
}, 3000);
|
|
155
|
+
|
|
147
156
|
send = (message: ClientMessage) => {
|
|
148
157
|
// only certain messages are sent for pull-based sync.
|
|
149
158
|
switch (message.type) {
|
|
150
159
|
case 'presence-update':
|
|
160
|
+
return this.throttledPresenceUpdate(message);
|
|
151
161
|
case 'sync':
|
|
152
162
|
case 'heartbeat':
|
|
153
163
|
return this.sendRequest([message]);
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { Migration } from '@verdant-web/common';
|
|
2
|
-
import { Context } from '../context.js';
|
|
3
|
-
import { Metadata } from '../metadata/Metadata.js';
|
|
4
|
-
type OpenDocumentDbContext = Omit<Context, 'documentDb'>;
|
|
5
|
-
export declare function openDocumentDatabase({ version, indexedDB, migrations, meta, context, }: {
|
|
6
|
-
version: number;
|
|
7
|
-
migrations: Migration<any>[];
|
|
8
|
-
indexedDB?: IDBFactory;
|
|
9
|
-
meta: Metadata;
|
|
10
|
-
context: OpenDocumentDbContext;
|
|
11
|
-
}): Promise<IDBDatabase>;
|
|
12
|
-
export declare function openWIPDocumentDatabase({ version, indexedDB, migrations, meta, context, wipNamespace, }: {
|
|
13
|
-
version: number;
|
|
14
|
-
migrations: Migration<any>[];
|
|
15
|
-
indexedDB?: IDBFactory;
|
|
16
|
-
meta: Metadata;
|
|
17
|
-
context: OpenDocumentDbContext;
|
|
18
|
-
wipNamespace: string;
|
|
19
|
-
}): Promise<IDBDatabase>;
|
|
20
|
-
export {};
|