2020-08-05 22:08:55 +05:30
|
|
|
/*
|
|
|
|
Copyright 2020 Bruno Windels <bruno@windels.cloud>
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-08-13 01:50:37 +05:30
|
|
|
import { iterateCursor, NOT_DONE, txnAsPromise } from "./utils";
|
2021-09-01 04:20:57 +05:30
|
|
|
import { STORE_NAMES, StoreNames } from "../common";
|
2019-12-14 22:59:35 +05:30
|
|
|
|
2021-09-01 04:20:57 +05:30
|
|
|
export type Export = { [storeName in StoreNames] : any[] }
|
|
|
|
|
|
|
|
export async function exportSession(db: IDBDatabase): Promise<Export> {
|
2019-12-14 22:59:35 +05:30
|
|
|
const txn = db.transaction(STORE_NAMES, "readonly");
|
|
|
|
const data = {};
|
|
|
|
await Promise.all(STORE_NAMES.map(async name => {
|
2021-08-13 01:50:37 +05:30
|
|
|
const results: any[] = data[name] = []; // initialize in deterministic order
|
2019-12-14 22:59:35 +05:30
|
|
|
const store = txn.objectStore(name);
|
2021-08-13 01:50:37 +05:30
|
|
|
await iterateCursor<any>(store.openCursor(), (value) => {
|
2019-12-14 22:59:35 +05:30
|
|
|
results.push(value);
|
|
|
|
return NOT_DONE;
|
|
|
|
});
|
|
|
|
}));
|
2021-09-01 04:20:57 +05:30
|
|
|
return data as Export;
|
2019-12-14 22:59:35 +05:30
|
|
|
}
|
|
|
|
|
2021-09-01 04:20:57 +05:30
|
|
|
export async function importSession(db: IDBDatabase, data: Export): Promise<void> {
|
2019-12-14 22:59:35 +05:30
|
|
|
const txn = db.transaction(STORE_NAMES, "readwrite");
|
|
|
|
for (const name of STORE_NAMES) {
|
|
|
|
const store = txn.objectStore(name);
|
|
|
|
for (const value of data[name]) {
|
|
|
|
store.add(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
await txnAsPromise(txn);
|
|
|
|
}
|