Skip to content

Commit

Permalink
make sure we can migrate our old storage to the new rxdb version...
Browse files Browse the repository at this point in the history
  • Loading branch information
yeus committed Aug 13, 2024
1 parent b205879 commit a6101bd
Show file tree
Hide file tree
Showing 4 changed files with 865 additions and 29 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"pyodide": "^0.24.1",
"quasar": "^2.16.0",
"rxdb": "^15.30.2",
"rxdb-old": "npm:[email protected]",
"sqlocal": "^0.6.2",
"stopword": "^2.0.8",
"update-browserslist-db": "^1.0.13",
Expand Down
2 changes: 1 addition & 1 deletion src/modules/taskyon/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export async function initTaskyon(
console.log('initializing taskyondb');
let taskyonDBInstance: TaskyonDatabase | undefined = undefined;
try {
taskyonDBInstance = await createTaskyonDatabase('taskyondb');
taskyonDBInstance = await createTaskyonDatabase();
} catch (err) {
console.log('could not initialize taskyonDB', err);
logError(
Expand Down
38 changes: 29 additions & 9 deletions src/modules/taskyon/rxdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ import {
RxDocument,
toTypedRxJsonSchema,
ExtractDocumentTypeFromTypedRxJsonSchema,
addRxPlugin
addRxPlugin,
} from 'rxdb';
import { getRxStorageDexie, RxStorageDexie } from 'rxdb/plugins/storage-dexie';
import type { RxStorageMemory } from 'rxdb/plugins/storage-memory';
import { getRxStorageDexie } from 'rxdb/plugins/storage-dexie';
import { RxDBJsonDumpPlugin } from 'rxdb/plugins/json-dump';
import { TaskNode } from './types';
// TOOD: remove at some point in the future...
import { RxDBDevModePlugin } from 'rxdb/plugins/dev-mode';
import { RxDBMigrationSchemaPlugin } from 'rxdb/plugins/migration-schema';
/* This is used so that we can migrate from old rxdb version to new ones (currently from v14.X to v15.X) */
import { migrateStorage } from 'rxdb/plugins/migration-storage';

addRxPlugin(RxDBDevModePlugin);
addRxPlugin(RxDBJsonDumpPlugin);
addRxPlugin(RxDBMigrationSchemaPlugin);
Expand Down Expand Up @@ -224,19 +226,37 @@ export const collections = {
},
};

export async function createTaskyonDatabase(
name: string,
storage: RxStorageDexie | RxStorageMemory | undefined = undefined,
): Promise<TaskyonDatabase> {
const newStorage = storage || getRxStorageDexie();
export async function createTaskyonDatabase(): Promise<TaskyonDatabase> {
const newStorage = getRxStorageDexie();
const db: TaskyonDatabase =
await createRxDatabase<TaskyonDatabaseCollections>({
name,
name: 'taskyondb_v15',
storage: newStorage,
});

await db.addCollections(collections);

//here we do te migration from or old storage
import('rxdb-old/plugins/storage-dexie').then(
({ getRxStorageDexie: getRxStorageDexieOld }) => {
migrateStorage({
database: db as unknown as RxDatabase,
/**
* Name of the old database,
* using the storage migration requires that the
* new database has a different name.
*/
oldDatabaseName: 'taskyondb',
oldStorage: getRxStorageDexieOld(), // RxStorage of the old database
batchSize: 500, // batch size
parallel: false, // <- true if it should migrate all collections in parallel. False (default) if should migrate in serial
afterMigrateBatch: (/*input: AfterMigrateBatchHandlerInput*/) => {
console.log('storage migration: batch processed');
},
});
},
);

return db;
}

Expand Down
Loading

0 comments on commit a6101bd

Please sign in to comment.