Make export types more precise

This commit is contained in:
Danila Fedorin 2021-08-31 15:50:57 -07:00
parent 2262e6be30
commit 78fb8fdadf
2 changed files with 9 additions and 7 deletions

View file

@ -16,7 +16,7 @@ limitations under the License.
import {Storage} from "./Storage"; import {Storage} from "./Storage";
import { openDatabase, reqAsPromise } from "./utils"; import { openDatabase, reqAsPromise } from "./utils";
import { exportSession, importSession } from "./export"; import { exportSession, importSession, Export } from "./export";
import { schema } from "./schema"; import { schema } from "./schema";
import { detectWebkitEarlyCloseTxnBug } from "./quirks"; import { detectWebkitEarlyCloseTxnBug } from "./quirks";
@ -80,12 +80,12 @@ export class StorageFactory {
return reqAsPromise(req); return reqAsPromise(req);
} }
async export(sessionId: string): Promise<{ [storeName: string]: any }> { async export(sessionId: string): Promise<Export> {
const db = await openDatabaseWithSessionId(sessionId, this._idbFactory); const db = await openDatabaseWithSessionId(sessionId, this._idbFactory);
return await exportSession(db); return await exportSession(db);
} }
async import(sessionId: string, data: { [storeName: string]: any }): Promise<void> { async import(sessionId: string, data: Export): Promise<void> {
const db = await openDatabaseWithSessionId(sessionId, this._idbFactory); const db = await openDatabaseWithSessionId(sessionId, this._idbFactory);
return await importSession(db, data); return await importSession(db, data);
} }

View file

@ -15,9 +15,11 @@ limitations under the License.
*/ */
import { iterateCursor, NOT_DONE, txnAsPromise } from "./utils"; import { iterateCursor, NOT_DONE, txnAsPromise } from "./utils";
import { STORE_NAMES } from "../common"; import { STORE_NAMES, StoreNames } from "../common";
export async function exportSession(db: IDBDatabase): Promise<{ [storeName : string] : any }> { export type Export = { [storeName in StoreNames] : any[] }
export async function exportSession(db: IDBDatabase): Promise<Export> {
const txn = db.transaction(STORE_NAMES, "readonly"); const txn = db.transaction(STORE_NAMES, "readonly");
const data = {}; const data = {};
await Promise.all(STORE_NAMES.map(async name => { await Promise.all(STORE_NAMES.map(async name => {
@ -28,10 +30,10 @@ export async function exportSession(db: IDBDatabase): Promise<{ [storeName : str
return NOT_DONE; return NOT_DONE;
}); });
})); }));
return data; return data as Export;
} }
export async function importSession(db: IDBDatabase, data: { [storeName: string]: any }): Promise<void> { export async function importSession(db: IDBDatabase, data: Export): Promise<void> {
const txn = db.transaction(STORE_NAMES, "readwrite"); const txn = db.transaction(STORE_NAMES, "readwrite");
for (const name of STORE_NAMES) { for (const name of STORE_NAMES) {
const store = txn.objectStore(name); const store = txn.objectStore(name);