|
|
|
import BaseDexie, { Table } from "dexie";
|
|
|
|
import { encrypted, Encryption } from "@pvermeer/dexie-encrypted-addon";
|
|
|
|
import {
|
|
|
|
Account,
|
|
|
|
AccountsSchema,
|
|
|
|
MASTER_SETTINGS,
|
|
|
|
Settings,
|
|
|
|
SettingsSchema,
|
|
|
|
} from "./tables";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* In order to make the next line be acceptable, the program needs to have its linter suppress a rule:
|
|
|
|
* https://typescript-eslint.io/rules/no-unnecessary-type-constraint/
|
|
|
|
*
|
|
|
|
* and change *any* to *unknown*
|
|
|
|
*
|
|
|
|
* https://9to5answer.com/how-to-bypass-warning-unexpected-any-specify-a-different-type-typescript-eslint-no-explicit-any
|
|
|
|
*/
|
|
|
|
type DexieTables = {
|
|
|
|
accounts: Table<Account>;
|
|
|
|
settings: Table<Settings>;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type Dexie<T extends unknown = DexieTables> = BaseDexie & T;
|
|
|
|
export const db = new BaseDexie("KickStart") as Dexie;
|
|
|
|
|
|
|
|
const AllSchemas = Object.assign({}, AccountsSchema, SettingsSchema);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Needed to enable a special webpack setting to allow *await* below:
|
|
|
|
* https://stackoverflow.com/questions/72474803/error-the-top-level-await-experiment-is-not-enabled-set-experiments-toplevelaw
|
|
|
|
*/
|
|
|
|
|
|
|
|
// create password and place password in localStorage
|
|
|
|
const secret =
|
|
|
|
localStorage.getItem("secret") || Encryption.createRandomEncryptionKey();
|
|
|
|
|
|
|
|
if (localStorage.getItem("secret") == null) {
|
|
|
|
localStorage.setItem("secret", secret);
|
|
|
|
}
|
|
|
|
console.log("Secret:", secret);
|
|
|
|
encrypted(db, { secretKey: secret });
|
|
|
|
db.version(1).stores(AllSchemas);
|
|
|
|
|
|
|
|
// initialize, a la https://dexie.org/docs/Tutorial/Design#the-populate-event
|
|
|
|
db.on("populate", function () {
|
|
|
|
// ensure there's an initial entry for settings
|
|
|
|
db.settings.add({ id: MASTER_SETTINGS });
|
|
|
|
});
|