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