2020-12-06 10:52:00 +05:00
|
|
|
import { CURRENT_DATABASE_VERSION } from "../common";
|
|
|
|
|
import { migrations } from "../migrations";
|
|
|
|
|
|
|
|
|
|
class Migrator {
|
|
|
|
|
async migrate(collections, get, version) {
|
|
|
|
|
await Promise.all(
|
|
|
|
|
collections.map(async (collection) => {
|
|
|
|
|
if (!collection.index || !collection.dbCollection) return;
|
2021-02-18 19:46:44 +05:00
|
|
|
for (var i = 0; i < collection.index.length; ++i) {
|
|
|
|
|
let id = collection.index[i];
|
|
|
|
|
let item = get(id);
|
|
|
|
|
if (!item) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2020-12-06 10:52:00 +05:00
|
|
|
|
2021-02-18 19:46:44 +05:00
|
|
|
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);
|
2020-12-29 14:08:48 +05:00
|
|
|
|
2021-02-18 19:46:44 +05:00
|
|
|
if (!!collection.dbCollection.merge) {
|
|
|
|
|
await collection.dbCollection.merge(item);
|
|
|
|
|
} else {
|
|
|
|
|
await collection.dbCollection.add(item);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-06 10:52:00 +05:00
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
export default Migrator;
|