|
@ -9,59 +9,39 @@ import { |
|
|
} from "./tables/settings"; |
|
|
} from "./tables/settings"; |
|
|
import { AppString } from "@/constants/app"; |
|
|
import { AppString } from "@/constants/app"; |
|
|
|
|
|
|
|
|
// a separate DB because the seed is super-sensitive data
|
|
|
// Define types for tables that hold sensitive and non-sensitive data
|
|
|
type SensitiveTables = { |
|
|
type SensitiveTables = { accounts: Table<Account> }; |
|
|
accounts: Table<Account>; |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
type NonsensitiveTables = { |
|
|
type NonsensitiveTables = { |
|
|
contacts: Table<Contact>; |
|
|
contacts: Table<Contact>; |
|
|
settings: Table<Settings>; |
|
|
settings: Table<Settings>; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
/** |
|
|
// Using 'unknown' instead of 'any' for stricter typing and to avoid TypeScript warnings
|
|
|
* 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 SensitiveDexie<T extends unknown = SensitiveTables> = BaseDexie & T; |
|
|
export type SensitiveDexie<T extends unknown = SensitiveTables> = BaseDexie & T; |
|
|
export const accountsDB = new BaseDexie("TimeSafariAccounts") as SensitiveDexie; |
|
|
|
|
|
const SensitiveSchemas = Object.assign({}, AccountsSchema); |
|
|
|
|
|
|
|
|
|
|
|
export type NonsensitiveDexie<T extends unknown = NonsensitiveTables> = |
|
|
export type NonsensitiveDexie<T extends unknown = NonsensitiveTables> = |
|
|
BaseDexie & T; |
|
|
BaseDexie & T; |
|
|
export const db = new BaseDexie("TimeSafari") as NonsensitiveDexie; |
|
|
|
|
|
const NonsensitiveSchemas = Object.assign({}, ContactsSchema, SettingsSchema); |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
// Initialize Dexie databases for sensitive and non-sensitive data
|
|
|
* Needed to enable a special webpack setting to allow *await* below: |
|
|
export const accountsDB = new BaseDexie("TimeSafariAccounts") as SensitiveDexie; |
|
|
* https://stackoverflow.com/questions/72474803/error-the-top-level-await-experiment-is-not-enabled-set-experiments-toplevelaw
|
|
|
const SensitiveSchemas = { ...AccountsSchema }; |
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
export const db = new BaseDexie("TimeSafari") as NonsensitiveDexie; |
|
|
* Create password and place password in localStorage. |
|
|
const NonsensitiveSchemas = { ...ContactsSchema, ...SettingsSchema }; |
|
|
* |
|
|
|
|
|
* It's good practice to keep the data encrypted at rest, so we'll do that even |
|
|
// Manage the encryption key. If not present in localStorage, create and store it.
|
|
|
* if the secret is stored right next to the app. |
|
|
|
|
|
*/ |
|
|
|
|
|
const secret = |
|
|
const secret = |
|
|
localStorage.getItem("secret") || Encryption.createRandomEncryptionKey(); |
|
|
localStorage.getItem("secret") || Encryption.createRandomEncryptionKey(); |
|
|
|
|
|
if (!localStorage.getItem("secret")) localStorage.setItem("secret", secret); |
|
|
|
|
|
|
|
|
if (localStorage.getItem("secret") == null) { |
|
|
// Apply encryption to the sensitive database using the secret key
|
|
|
localStorage.setItem("secret", secret); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
encrypted(accountsDB, { secretKey: secret }); |
|
|
encrypted(accountsDB, { secretKey: secret }); |
|
|
accountsDB.version(1).stores(SensitiveSchemas); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Define the schema for our databases
|
|
|
|
|
|
accountsDB.version(1).stores(SensitiveSchemas); |
|
|
db.version(1).stores(NonsensitiveSchemas); |
|
|
db.version(1).stores(NonsensitiveSchemas); |
|
|
|
|
|
|
|
|
// initialize, a la https://dexie.org/docs/Tutorial/Design#the-populate-event
|
|
|
// Event handler to initialize the non-sensitive database with default settings
|
|
|
db.on("populate", function () { |
|
|
db.on("populate", () => { |
|
|
// ensure there's an initial entry for settings
|
|
|
|
|
|
db.settings.add({ |
|
|
db.settings.add({ |
|
|
id: MASTER_SETTINGS_KEY, |
|
|
id: MASTER_SETTINGS_KEY, |
|
|
apiServer: AppString.DEFAULT_ENDORSER_API_SERVER, |
|
|
apiServer: AppString.DEFAULT_ENDORSER_API_SERVER, |
|
|