refactor: migrate in serial

This commit is contained in:
thecodrr
2021-07-06 12:13:35 +05:00
parent 3709a63aae
commit d3b106094e

View File

@@ -3,31 +3,30 @@ import { migrations } from "../migrations";
class Migrator { class Migrator {
async migrate(collections, get, version) { async migrate(collections, get, version) {
await Promise.all( for (let collection of collections) {
collections.map(async (collection) => { if (!collection.index || !collection.dbCollection) continue;
if (!collection.index || !collection.dbCollection) return; for (var i = 0; i < collection.index.length; ++i) {
for (var i = 0; i < collection.index.length; ++i) { let id = collection.index[i];
let id = collection.index[i]; let item = get(id);
let item = get(id); if (!item) {
if (!item) { continue;
continue;
}
if (item.deleted && !item.type) {
await collection.dbCollection?._collection?.addItem(item);
continue;
}
const migrate = migrations[version][item.type || collection.type];
if (migrate) item = migrate(item);
if (!!collection.dbCollection.merge) {
await collection.dbCollection.merge(item);
} else {
await collection.dbCollection.add(item);
}
} }
})
); if (item.deleted && !item.type) {
await collection.dbCollection?._collection?.addItem(item);
continue;
}
const migrate = migrations[version][item.type || collection.type];
if (migrate) item = migrate(item);
item.migrated = true;
if (!!collection.dbCollection.merge) {
await collection.dbCollection.merge(item);
} else {
await collection.dbCollection.add(item);
}
}
}
return true; return true;
} }
} }