You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
1.2 KiB
35 lines
1.2 KiB
2 years ago
|
import BaseDexie from "dexie";
|
||
|
import { encrypted, Encryption } from "@pvermeer/dexie-encrypted-addon";
|
||
|
import { accountsSchema, AccountsTable } from "./tables/accounts";
|
||
|
type DexieTables = AccountsTable;
|
||
|
|
||
|
/**
|
||
|
* 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
|
||
|
*/
|
||
|
//export type Dexie<T extends any = DexieTables> = BaseDexie & T;
|
||
|
export type Dexie<T extends unknown = DexieTables> = BaseDexie & T;
|
||
|
|
||
|
export const db = new BaseDexie("kickStarter") as Dexie;
|
||
|
const schema = Object.assign({}, accountsSchema);
|
||
|
|
||
|
// if db already made, skip creation
|
||
|
BaseDexie.exists("kickStarter").then(function (exists) {
|
||
|
if (exists == false) {
|
||
|
// create password and place password in localStorage
|
||
|
const secret =
|
||
|
localStorage.getItem("secret") || Encryption.createRandomEncryptionKey();
|
||
|
|
||
|
if (localStorage.getItem("secret") == null) {
|
||
|
localStorage.setItem("secret", secret);
|
||
|
}
|
||
|
|
||
|
encrypted(db, { secretKey: secret });
|
||
|
db.version(1).stores(schema);
|
||
|
}
|
||
|
});
|