Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

core: migrate in bulk for more performance

Abdullah Atta41beee45ba8e
diff --git a/packages/core/__mocks__/node-storage.mock.js b/packages/core/__mocks__/node-storage.mock.js
index 3f939b25e..39d2de9c2 100644
--- a/packages/core/__mocks__/node-storage.mock.js
+++ b/packages/core/__mocks__/node-storage.mock.js
@@ -40,8 +40,15 @@ export class NodeStorageInterface {
}
async write(key, data) {
- return new Promise((resolve) => resolve((this.storage[key] = data)));
+ this.storage[key] = data;
}
+
+ async writeMulti(entries) {
+ for (const [key, value] of entries) {
+ this.storage[key] = value;
+ }
+ }
+
remove(key) {
delete this.storage[key];
}
diff --git a/packages/core/src/database/migrator.js b/packages/core/src/database/migrator.js
index f44196f68..2f04ad3dc 100644
--- a/packages/core/src/database/migrator.js
+++ b/packages/core/src/database/migrator.js
@@ -36,6 +36,7 @@ class Migrator {
await migrateCollection(collection.dbCollection, version);
const index = (await collection.index()) || [];
+ const toAdd = [];
for (var i = 0; i < index.length; ++i) {
let id = index[i];
let item = get(id, collection.dbCollection.collectionName);
@@ -43,14 +44,6 @@ class Migrator {
continue;
}
- if (collection.dbCollection.collectionName)
- sendMigrationProgressEvent(
- db.eventManager,
- collection.dbCollection.collectionName,
- index.length,
- i + 1
- );
-
// check if item is permanently deleted or just a soft delete
if (item.deleted && !item.type) {
await collection.dbCollection?._collection?.addItem(item);
@@ -68,8 +61,10 @@ class Migrator {
if (migrated || restore) {
if (collection.type === "settings") {
await collection.dbCollection.merge(item);
+ } else if (item.type === "note") {
+ toAdd.push(await db.notes.merge(null, item));
} else if (collection.dbCollection._collection) {
- await collection.dbCollection._collection?.addItem(item);
+ toAdd.push(item);
} else {
throw new Error(
`No idea how to handle this kind of item: ${item.type}.`
@@ -78,10 +73,21 @@ class Migrator {
// if id changed after migration, we need to delete the old one.
if (item.id !== itemId) {
- await collection.dbCollection?._collection?.deleteItem(itemId);
+ await collection.dbCollection._collection.deleteItem(itemId);
}
}
}
+
+ if (toAdd.length > 0) {
+ await collection.dbCollection._collection.setItems(toAdd);
+ if (collection.dbCollection.collectionName)
+ sendMigrationProgressEvent(
+ db.eventManager,
+ collection.dbCollection.collectionName,
+ toAdd.length,
+ toAdd.length
+ );
+ }
}
return true;
}