2020-11-26 16:29:10 +11:00
|
|
|
import * as Comlink from "comlink";
|
2021-02-13 21:51:31 +11:00
|
|
|
import {
|
|
|
|
|
importInto,
|
|
|
|
|
exportDB,
|
|
|
|
|
peakImportFile,
|
|
|
|
|
} from "@mitchemmc/dexie-export-import";
|
2021-01-27 16:24:13 +11:00
|
|
|
import { encode } from "@msgpack/msgpack";
|
2020-11-26 16:29:10 +11:00
|
|
|
|
|
|
|
|
import { getDatabase } from "../database";
|
|
|
|
|
|
|
|
|
|
// Worker to load large amounts of database data on a separate thread
|
2021-01-27 16:24:13 +11:00
|
|
|
let service = {
|
2021-01-22 14:59:05 +11:00
|
|
|
/**
|
|
|
|
|
* Load either a whole table or individual item from the DB
|
|
|
|
|
* @param {string} table Table to load from
|
2021-02-08 16:53:56 +11:00
|
|
|
* @param {string=} key Optional database key to load, if undefined whole table will be loaded
|
|
|
|
|
* @param {bool} excludeFiles Optional exclude files from loaded data when using whole table loading
|
2021-01-22 14:59:05 +11:00
|
|
|
*/
|
2021-02-08 16:53:56 +11:00
|
|
|
async loadData(table, key, excludeFiles = true) {
|
2020-11-26 17:08:09 +11:00
|
|
|
try {
|
|
|
|
|
let db = getDatabase({});
|
2021-01-22 14:59:05 +11:00
|
|
|
if (key) {
|
|
|
|
|
// Load specific item
|
2021-02-13 21:51:31 +11:00
|
|
|
const data = await db.table(table).get(key);
|
|
|
|
|
db.close();
|
|
|
|
|
return data;
|
2021-01-22 14:59:05 +11:00
|
|
|
} else {
|
|
|
|
|
// Load entire table
|
2021-01-27 16:24:13 +11:00
|
|
|
let items = [];
|
2021-01-22 14:59:05 +11:00
|
|
|
// Use a cursor instead of toArray to prevent IPC max size error
|
2021-02-08 16:53:56 +11:00
|
|
|
await db.table(table).each((item) => {
|
|
|
|
|
if (excludeFiles) {
|
|
|
|
|
const { file, resolutions, ...rest } = item;
|
|
|
|
|
items.push(rest);
|
|
|
|
|
} else {
|
|
|
|
|
items.push(item);
|
|
|
|
|
}
|
|
|
|
|
});
|
2021-01-27 16:24:13 +11:00
|
|
|
|
2021-02-13 21:51:31 +11:00
|
|
|
db.close();
|
|
|
|
|
|
2021-01-27 16:24:13 +11:00
|
|
|
// Pack data with msgpack so we can use transfer to avoid memory issues
|
|
|
|
|
const packed = encode(items);
|
|
|
|
|
return Comlink.transfer(packed, [packed.buffer]);
|
2021-01-22 14:59:05 +11:00
|
|
|
}
|
2020-11-26 17:08:09 +11:00
|
|
|
} catch {}
|
2020-11-26 16:29:10 +11:00
|
|
|
},
|
2021-01-27 11:57:23 +11:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Export current database
|
|
|
|
|
* @param {function} progressCallback
|
2021-02-13 21:51:31 +11:00
|
|
|
* @param {string[]} maps An array of map ids to export
|
|
|
|
|
* @param {string[]} tokens An array of token ids to export
|
2021-01-27 11:57:23 +11:00
|
|
|
*/
|
2021-02-13 21:51:31 +11:00
|
|
|
async exportData(progressCallback, maps, tokens) {
|
2021-02-13 13:21:13 +11:00
|
|
|
let db = getDatabase({});
|
2021-02-13 21:51:31 +11:00
|
|
|
|
|
|
|
|
const filter = (table, value) => {
|
|
|
|
|
if (table === "maps") {
|
|
|
|
|
return maps.includes(value.id);
|
|
|
|
|
}
|
|
|
|
|
if (table === "states") {
|
|
|
|
|
return maps.includes(value.mapId);
|
|
|
|
|
}
|
|
|
|
|
if (table === "tokens") {
|
|
|
|
|
return tokens.includes(value.id);
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const data = await exportDB(db, {
|
2021-02-13 13:21:13 +11:00
|
|
|
progressCallback,
|
2021-02-13 21:51:31 +11:00
|
|
|
filter,
|
2021-02-13 13:21:13 +11:00
|
|
|
numRowsPerChunk: 1,
|
2021-02-14 10:11:52 +11:00
|
|
|
prettyJson: true,
|
2021-02-13 13:21:13 +11:00
|
|
|
});
|
2021-02-13 21:51:31 +11:00
|
|
|
db.close();
|
|
|
|
|
return data;
|
2021-01-27 11:57:23 +11:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Import into current database
|
|
|
|
|
* @param {Blob} data
|
2021-02-13 21:51:31 +11:00
|
|
|
* @param {string} databaseName The name of the database to import into
|
2021-01-27 11:57:23 +11:00
|
|
|
* @param {function} progressCallback
|
|
|
|
|
*/
|
2021-02-13 21:51:31 +11:00
|
|
|
async importData(data, databaseName, progressCallback) {
|
|
|
|
|
const importMeta = await peakImportFile(data);
|
2021-02-13 13:21:13 +11:00
|
|
|
let db = getDatabase({});
|
2021-02-13 21:51:31 +11:00
|
|
|
|
|
|
|
|
if (importMeta.data.databaseName !== db.name) {
|
|
|
|
|
throw new Error("Unable to import database, name mismatch");
|
|
|
|
|
}
|
2021-02-14 13:36:00 +11:00
|
|
|
if (importMeta.data.databaseVersion > db.verno) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
`Database version differs. Current database is in version ${db.verno} but export is ${importMeta.data.databaseVersion}`
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ensure import DB is cleared before importing new data
|
|
|
|
|
let importDB = getDatabase({}, databaseName, 0);
|
|
|
|
|
await importDB.delete();
|
|
|
|
|
importDB.close();
|
2021-02-13 21:51:31 +11:00
|
|
|
|
2021-02-14 13:36:00 +11:00
|
|
|
// Load import database up to it's desired version
|
|
|
|
|
importDB = getDatabase({}, databaseName, importMeta.data.databaseVersion);
|
2021-02-13 21:51:31 +11:00
|
|
|
await importInto(importDB, data, {
|
|
|
|
|
progressCallback,
|
|
|
|
|
acceptNameDiff: true,
|
|
|
|
|
overwriteValues: true,
|
|
|
|
|
filter: (table, value) => {
|
|
|
|
|
// Ensure values are of the correct form
|
|
|
|
|
if (table === "maps" || table === "tokens") {
|
|
|
|
|
return "id" in value && "owner" in value;
|
|
|
|
|
}
|
|
|
|
|
if (table === "states") {
|
|
|
|
|
return "mapId" in value;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
},
|
2021-02-14 13:36:00 +11:00
|
|
|
acceptVersionDiff: true,
|
2021-02-13 21:51:31 +11:00
|
|
|
});
|
|
|
|
|
importDB.close();
|
2021-02-14 13:36:00 +11:00
|
|
|
db.close();
|
2021-01-27 11:57:23 +11:00
|
|
|
},
|
2020-11-26 16:29:10 +11:00
|
|
|
};
|
|
|
|
|
|
2021-01-27 16:24:13 +11:00
|
|
|
Comlink.expose(service);
|