@verdant-web/store 3.7.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.
Files changed (64) hide show
  1. package/LICENSE +650 -21
  2. package/dist/bundle/index.js +12 -12
  3. package/dist/bundle/index.js.map +4 -4
  4. package/dist/esm/DocumentManager.d.ts +1 -0
  5. package/dist/esm/DocumentManager.js +8 -3
  6. package/dist/esm/DocumentManager.js.map +1 -1
  7. package/dist/esm/__tests__/documents.test.js +16 -0
  8. package/dist/esm/__tests__/documents.test.js.map +1 -1
  9. package/dist/esm/client/Client.d.ts +3 -3
  10. package/dist/esm/client/Client.js +5 -5
  11. package/dist/esm/client/Client.js.map +1 -1
  12. package/dist/esm/client/ClientDescriptor.d.ts +1 -0
  13. package/dist/esm/client/ClientDescriptor.js +6 -3
  14. package/dist/esm/client/ClientDescriptor.js.map +1 -1
  15. package/dist/esm/context.d.ts +1 -0
  16. package/dist/esm/entities/EntityStore.d.ts +3 -3
  17. package/dist/esm/entities/EntityStore.js +5 -0
  18. package/dist/esm/entities/EntityStore.js.map +1 -1
  19. package/dist/esm/idb.d.ts +6 -0
  20. package/dist/esm/idb.js +17 -1
  21. package/dist/esm/idb.js.map +1 -1
  22. package/dist/esm/migration/db.d.ts +9 -3
  23. package/dist/esm/migration/db.js +23 -11
  24. package/dist/esm/migration/db.js.map +1 -1
  25. package/dist/esm/migration/engine.d.ts +15 -0
  26. package/dist/esm/migration/engine.js +159 -0
  27. package/dist/esm/migration/engine.js.map +1 -0
  28. package/dist/esm/migration/migrations.d.ts +17 -0
  29. package/dist/esm/migration/migrations.js +242 -0
  30. package/dist/esm/migration/migrations.js.map +1 -0
  31. package/dist/esm/migration/openQueryDatabase.d.ts +10 -0
  32. package/dist/esm/migration/openQueryDatabase.js +27 -0
  33. package/dist/esm/migration/openQueryDatabase.js.map +1 -0
  34. package/dist/esm/migration/openWIPDatabase.d.ts +11 -0
  35. package/dist/esm/migration/openWIPDatabase.js +65 -0
  36. package/dist/esm/migration/openWIPDatabase.js.map +1 -0
  37. package/dist/esm/migration/types.d.ts +3 -0
  38. package/dist/esm/migration/types.js +2 -0
  39. package/dist/esm/migration/types.js.map +1 -0
  40. package/dist/esm/queries/QueryableStorage.js +1 -1
  41. package/dist/esm/queries/QueryableStorage.js.map +1 -1
  42. package/dist/esm/sync/PushPullSync.d.ts +3 -2
  43. package/dist/esm/sync/PushPullSync.js +7 -1
  44. package/dist/esm/sync/PushPullSync.js.map +1 -1
  45. package/package.json +4 -3
  46. package/src/DocumentManager.ts +11 -2
  47. package/src/__tests__/documents.test.ts +19 -0
  48. package/src/client/Client.ts +6 -8
  49. package/src/client/ClientDescriptor.ts +7 -6
  50. package/src/context.ts +1 -0
  51. package/src/entities/EntityStore.ts +8 -2
  52. package/src/idb.ts +20 -1
  53. package/src/migration/db.ts +62 -20
  54. package/src/migration/engine.ts +248 -0
  55. package/src/migration/migrations.ts +347 -0
  56. package/src/migration/openQueryDatabase.ts +63 -0
  57. package/src/migration/openWIPDatabase.ts +97 -0
  58. package/src/migration/types.ts +4 -0
  59. package/src/queries/QueryableStorage.ts +1 -1
  60. package/src/sync/PushPullSync.ts +10 -0
  61. package/dist/esm/migration/openDatabase.d.ts +0 -20
  62. package/dist/esm/migration/openDatabase.js +0 -463
  63. package/dist/esm/migration/openDatabase.js.map +0 -1
  64. package/src/migration/openDatabase.ts +0 -749
@@ -0,0 +1,248 @@
1
+ import {
2
+ CollectionFilter,
3
+ Migration,
4
+ MigrationEngine,
5
+ ObjectIdentifier,
6
+ addFieldDefaults,
7
+ assert,
8
+ assignOidsToAllSubObjects,
9
+ cloneDeep,
10
+ createOid,
11
+ diffToPatches,
12
+ getOid,
13
+ initialToPatches,
14
+ removeOidPropertiesFromAllSubObjects,
15
+ } from '@verdant-web/common';
16
+ import { Context } from '../context.js';
17
+ import { Metadata } from '../metadata/Metadata.js';
18
+ import { findAllOids, findOneOid } from '../queries/dbQueries.js';
19
+ import { OpenDocumentDbContext } from './types.js';
20
+
21
+ function getMigrationMutations({
22
+ migration,
23
+ meta,
24
+ getMigrationNow,
25
+ newOids,
26
+ }: {
27
+ migration: Migration<any>;
28
+ newOids: string[];
29
+ getMigrationNow: () => string;
30
+ meta: Metadata;
31
+ }) {
32
+ return migration.allCollections.reduce((acc, collectionName) => {
33
+ acc[collectionName] = {
34
+ put: async (doc: any) => {
35
+ // add defaults
36
+ addFieldDefaults(migration.newSchema.collections[collectionName], doc);
37
+ const primaryKey =
38
+ doc[migration.newSchema.collections[collectionName].primaryKey];
39
+ const oid = createOid(collectionName, primaryKey);
40
+ newOids.push(oid);
41
+ await meta.insertLocalOperations(
42
+ initialToPatches(doc, oid, getMigrationNow),
43
+ );
44
+ return doc;
45
+ },
46
+ delete: async (id: string) => {
47
+ const rootOid = createOid(collectionName, id);
48
+ const allOids = await meta.getAllDocumentRelatedOids(rootOid);
49
+ return meta.insertLocalOperations(
50
+ allOids.map((oid) => ({
51
+ oid,
52
+ timestamp: getMigrationNow(),
53
+ data: { op: 'delete' },
54
+ })),
55
+ );
56
+ },
57
+ };
58
+ return acc;
59
+ }, {} as any);
60
+ }
61
+
62
+ function getMigrationQueries({
63
+ migration,
64
+ context,
65
+ meta,
66
+ }: {
67
+ migration: Migration<any>;
68
+ context: Context;
69
+ meta: Metadata;
70
+ }) {
71
+ return migration.oldCollections.reduce((acc, collectionName) => {
72
+ acc[collectionName] = {
73
+ get: async (id: string) => {
74
+ const oid = createOid(collectionName, id);
75
+ const doc = await meta.getDocumentSnapshot(oid, {
76
+ // only get the snapshot up to the previous version (newer operations may have synced)
77
+ to: meta.time.now(migration.oldSchema.version),
78
+ });
79
+ return doc;
80
+ },
81
+ findOne: async (filter: CollectionFilter) => {
82
+ const oid = await findOneOid({
83
+ collection: collectionName,
84
+ index: filter,
85
+ context,
86
+ });
87
+ if (!oid) return null;
88
+ const doc = await meta.getDocumentSnapshot(oid, {
89
+ // only get the snapshot up to the previous version (newer operations may have synced)
90
+ to: meta.time.now(migration.oldSchema.version),
91
+ });
92
+ return doc;
93
+ },
94
+ findAll: async (filter: CollectionFilter) => {
95
+ const oids = await findAllOids({
96
+ collection: collectionName,
97
+ index: filter,
98
+ context,
99
+ });
100
+ const docs = await Promise.all(
101
+ oids.map((oid) =>
102
+ meta.getDocumentSnapshot(oid, {
103
+ // only get the snapshot up to the previous version (newer operations may have synced)
104
+ to: meta.time.now(migration.oldSchema.version),
105
+ }),
106
+ ),
107
+ );
108
+ return docs;
109
+ },
110
+ };
111
+ return acc;
112
+ }, {} as any);
113
+ }
114
+
115
+ export function getMigrationEngine({
116
+ meta,
117
+ migration,
118
+ context,
119
+ }: {
120
+ log?: (...args: any[]) => void;
121
+ migration: Migration;
122
+ meta: Metadata;
123
+ context: Context;
124
+ }): MigrationEngine {
125
+ function getMigrationNow() {
126
+ return meta.time.zero(migration.version);
127
+ }
128
+
129
+ const newOids = new Array<ObjectIdentifier>();
130
+
131
+ const queries = getMigrationQueries({
132
+ migration,
133
+ context,
134
+ meta,
135
+ });
136
+ const mutations = getMigrationMutations({
137
+ migration,
138
+ getMigrationNow,
139
+ newOids,
140
+ meta,
141
+ });
142
+ const deleteCollection = async (collection: string) => {
143
+ const allOids = await meta.getAllCollectionRelatedOids(collection);
144
+ return meta.insertLocalOperations(
145
+ allOids.map((oid) => ({
146
+ oid,
147
+ timestamp: getMigrationNow(),
148
+ data: { op: 'delete' },
149
+ })),
150
+ );
151
+ };
152
+ const awaitables = new Array<Promise<any>>();
153
+ const engine: MigrationEngine = {
154
+ log: context.log,
155
+ newOids,
156
+ deleteCollection,
157
+ migrate: async (collection, strategy) => {
158
+ const docs = await queries[collection].findAll();
159
+
160
+ await Promise.all(
161
+ docs.filter(Boolean).map(async (doc: any) => {
162
+ const rootOid = getOid(doc);
163
+ assert(
164
+ !!rootOid,
165
+ `Document is missing an OID: ${JSON.stringify(doc)}`,
166
+ );
167
+ const original = cloneDeep(doc);
168
+ // @ts-ignore - excessive type resolution
169
+ const newValue = await strategy(doc);
170
+ if (newValue) {
171
+ // the migration has altered the shape of our document. we need
172
+ // to create the operation from the diff and write it to meta as
173
+ // a migration patch
174
+ removeOidPropertiesFromAllSubObjects(original);
175
+ removeOidPropertiesFromAllSubObjects(newValue);
176
+ assignOidsToAllSubObjects(newValue);
177
+ const patches = diffToPatches(
178
+ original,
179
+ newValue,
180
+ getMigrationNow,
181
+ undefined,
182
+ [],
183
+ {
184
+ mergeUnknownObjects: true,
185
+ },
186
+ );
187
+ if (patches.length > 0) {
188
+ await meta.insertLocalOperations(patches);
189
+ }
190
+ }
191
+ }),
192
+ );
193
+ },
194
+ queries,
195
+ mutations,
196
+ awaitables,
197
+ };
198
+ return engine;
199
+ }
200
+
201
+ export function getInitialMigrationEngine({
202
+ meta,
203
+ migration,
204
+ context,
205
+ }: {
206
+ context: OpenDocumentDbContext;
207
+ migration: Migration;
208
+ meta: Metadata;
209
+ }): MigrationEngine {
210
+ function getMigrationNow() {
211
+ return meta.time.zero(migration.version);
212
+ }
213
+
214
+ const newOids = new Array<ObjectIdentifier>();
215
+
216
+ const queries = new Proxy({} as any, {
217
+ get() {
218
+ throw new Error(
219
+ 'Queries are not available in initial migrations; there is no database yet!',
220
+ );
221
+ },
222
+ }) as any;
223
+
224
+ const mutations = getMigrationMutations({
225
+ migration,
226
+ getMigrationNow,
227
+ newOids,
228
+ meta,
229
+ });
230
+ const engine: MigrationEngine = {
231
+ log: context.log,
232
+ newOids,
233
+ deleteCollection: () => {
234
+ throw new Error(
235
+ 'Calling deleteCollection() in initial migrations is not supported! Use initial migrations to seed initial data using mutations.',
236
+ );
237
+ },
238
+ migrate: () => {
239
+ throw new Error(
240
+ 'Calling migrate() in initial migrations is not supported! Use initial migrations to seed initial data using mutations.',
241
+ );
242
+ },
243
+ queries,
244
+ mutations,
245
+ awaitables: [],
246
+ };
247
+ return engine;
248
+ }
@@ -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
+ }