Skip to content

Commit a6101bd

Browse files
committed
make sure we can migrate our old storage to the new rxdb version...
1 parent b205879 commit a6101bd

File tree

4 files changed

+865
-29
lines changed

4 files changed

+865
-29
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
"pyodide": "^0.24.1",
7878
"quasar": "^2.16.0",
7979
"rxdb": "^15.30.2",
80+
"rxdb-old": "npm:[email protected]",
8081
"sqlocal": "^0.6.2",
8182
"stopword": "^2.0.8",
8283
"update-browserslist-db": "^1.0.13",

src/modules/taskyon/init.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export async function initTaskyon(
2929
console.log('initializing taskyondb');
3030
let taskyonDBInstance: TaskyonDatabase | undefined = undefined;
3131
try {
32-
taskyonDBInstance = await createTaskyonDatabase('taskyondb');
32+
taskyonDBInstance = await createTaskyonDatabase();
3333
} catch (err) {
3434
console.log('could not initialize taskyonDB', err);
3535
logError(

src/modules/taskyon/rxdb.ts

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@ import {
66
RxDocument,
77
toTypedRxJsonSchema,
88
ExtractDocumentTypeFromTypedRxJsonSchema,
9-
addRxPlugin
9+
addRxPlugin,
1010
} from 'rxdb';
11-
import { getRxStorageDexie, RxStorageDexie } from 'rxdb/plugins/storage-dexie';
12-
import type { RxStorageMemory } from 'rxdb/plugins/storage-memory';
11+
import { getRxStorageDexie } from 'rxdb/plugins/storage-dexie';
1312
import { RxDBJsonDumpPlugin } from 'rxdb/plugins/json-dump';
1413
import { TaskNode } from './types';
1514
// TOOD: remove at some point in the future...
1615
import { RxDBDevModePlugin } from 'rxdb/plugins/dev-mode';
1716
import { RxDBMigrationSchemaPlugin } from 'rxdb/plugins/migration-schema';
17+
/* This is used so that we can migrate from old rxdb version to new ones (currently from v14.X to v15.X) */
18+
import { migrateStorage } from 'rxdb/plugins/migration-storage';
19+
1820
addRxPlugin(RxDBDevModePlugin);
1921
addRxPlugin(RxDBJsonDumpPlugin);
2022
addRxPlugin(RxDBMigrationSchemaPlugin);
@@ -224,19 +226,37 @@ export const collections = {
224226
},
225227
};
226228

227-
export async function createTaskyonDatabase(
228-
name: string,
229-
storage: RxStorageDexie | RxStorageMemory | undefined = undefined,
230-
): Promise<TaskyonDatabase> {
231-
const newStorage = storage || getRxStorageDexie();
229+
export async function createTaskyonDatabase(): Promise<TaskyonDatabase> {
230+
const newStorage = getRxStorageDexie();
232231
const db: TaskyonDatabase =
233232
await createRxDatabase<TaskyonDatabaseCollections>({
234-
name,
233+
name: 'taskyondb_v15',
235234
storage: newStorage,
236235
});
237236

238237
await db.addCollections(collections);
239238

239+
//here we do te migration from or old storage
240+
import('rxdb-old/plugins/storage-dexie').then(
241+
({ getRxStorageDexie: getRxStorageDexieOld }) => {
242+
migrateStorage({
243+
database: db as unknown as RxDatabase,
244+
/**
245+
* Name of the old database,
246+
* using the storage migration requires that the
247+
* new database has a different name.
248+
*/
249+
oldDatabaseName: 'taskyondb',
250+
oldStorage: getRxStorageDexieOld(), // RxStorage of the old database
251+
batchSize: 500, // batch size
252+
parallel: false, // <- true if it should migrate all collections in parallel. False (default) if should migrate in serial
253+
afterMigrateBatch: (/*input: AfterMigrateBatchHandlerInput*/) => {
254+
console.log('storage migration: batch processed');
255+
},
256+
});
257+
},
258+
);
259+
240260
return db;
241261
}
242262

0 commit comments

Comments
 (0)