From 607230b51ced184a76c3a15d4e27024e0b67bf8e Mon Sep 17 00:00:00 2001 From: Matthew Aaron Raymer Date: Fri, 16 Dec 2022 13:34:01 +0800 Subject: [PATCH] Cleaned up old Dexie class. Added mnemonic and dateCreated columns. --- src/constants/model.ts | 27 ----------------------- src/constants/table.ts | 6 ----- src/db/tables/accounts.ts | 5 ++++- src/models/Account.ts | 9 -------- src/services/DexieWrapper.ts | 34 ----------------------------- src/use/useDBAccounts.ts | 41 ----------------------------------- src/views/AccountViewView.vue | 2 ++ vue.config.js | 2 +- 8 files changed, 7 insertions(+), 119 deletions(-) delete mode 100644 src/constants/model.ts delete mode 100644 src/constants/table.ts delete mode 100644 src/models/Account.ts delete mode 100644 src/services/DexieWrapper.ts delete mode 100644 src/use/useDBAccounts.ts diff --git a/src/constants/model.ts b/src/constants/model.ts deleted file mode 100644 index 782e2f700..000000000 --- a/src/constants/model.ts +++ /dev/null @@ -1,27 +0,0 @@ -/** - * Field names used by the tables for the DB objects. - */ -export enum Field { - // Setting - KEY = "key", - VALUE = "value", - // Log - SEVERITY = "severity", - LABEL = "label", - ERROR = "error", - // Accounts - NAME = "name", - DESCRIPTION = "description", - ID = "id", - IDENTITY = "indentity", - CREATED_TIMESTAMP = "createdTimestamp", - STATUS = "status", -} - -export enum Severity { - DEBUG = "Debug", - INFO = "Info", - WARN = "Warning", - ERROR = "Error", - CRITICAL = "Critical", -} diff --git a/src/constants/table.ts b/src/constants/table.ts deleted file mode 100644 index 915eb80c5..000000000 --- a/src/constants/table.ts +++ /dev/null @@ -1,6 +0,0 @@ -/** - * Dexie table names used by the DexieWrapper service. - */ -export enum AppTable { - ACCOUNTS = "Accounts-Table", -} diff --git a/src/db/tables/accounts.ts b/src/db/tables/accounts.ts index c14ac9567..8ed1bd2b8 100644 --- a/src/db/tables/accounts.ts +++ b/src/db/tables/accounts.ts @@ -3,13 +3,16 @@ import { Table } from "dexie"; export type Account = { id?: number; publicKey: string; + mnemonic: string; identity: string; + dateCreated: number; }; export type AccountsTable = { accounts: Table; }; +// mark encrypted field by starting with a $ character export const accountsSchema = { - accounts: "++id, publicKey, $identity", + accounts: "++id, publicKey, $mnemonic, $identity, dateCreated", }; diff --git a/src/models/Account.ts b/src/models/Account.ts deleted file mode 100644 index 0c5f6d35e..000000000 --- a/src/models/Account.ts +++ /dev/null @@ -1,9 +0,0 @@ -import type { Field } from "@/constants/model"; - -export interface IDBAccount { - [Field.ID]?: string; - [Field.IDENTITY]: string; - [Field.NAME]: string; - [Field.DESCRIPTION]: string; - [Field.CREATED_TIMESTAMP]: number; -} diff --git a/src/services/DexieWrapper.ts b/src/services/DexieWrapper.ts deleted file mode 100644 index fa04a6ae7..000000000 --- a/src/services/DexieWrapper.ts +++ /dev/null @@ -1,34 +0,0 @@ -import Dexie, { type Table } from "dexie"; -import { encrypted, Encryption } from "@pvermeer/dexie-encrypted-addon"; - -import { AppString } from "@/constants/app"; -import { AppTable } from "../constants/table"; -import { Field } from "@/constants/model"; -import { IDBAccount } from "@/models/Account"; - -export class DexieWrapper extends Dexie { - [AppTable.ACCOUNTS]!: Table; - - constructor(name: string, secret: string) { - super(name, { autoOpen: true }); - encrypted(this, { secretKey: secret }); - this.version(1).stores({ - [AppTable.ACCOUNTS]: `#&${Field.ID}, $${Field.IDENTITY}`, - }); - } -} - -const secret = - localStorage.getItem("secret") || Encryption.createRandomEncryptionKey(); - -if (localStorage.getItem("secret") == null) { - localStorage.setItem("secret", secret); -} - -/** - * Preconfigured DexieWrapper - */ -export const dexieWrapper = new DexieWrapper( - `${AppString.APP_NAME} v${AppString.VERSION}`, - secret -); diff --git a/src/use/useDBAccounts.ts b/src/use/useDBAccounts.ts deleted file mode 100644 index 19de0ba9d..000000000 --- a/src/use/useDBAccounts.ts +++ /dev/null @@ -1,41 +0,0 @@ -import type { IndexableType } from "dexie"; -import { AppTable } from "@/constants/table"; -import { Field } from "@/constants/model"; -import { dexieWrapper } from "@/services/DexieWrapper"; -import type { IDBAccount } from "@/models/Account"; - -export default function useDBAccounts() { - /** - * Gets all data from the Logs table. - * @returns IDBAccount[] - */ - async function getAccountsTable(): Promise { - return await dexieWrapper.table(AppTable.ACCOUNTS).toArray(); - } - - /** - * Adds an Account to the database. - * @param name - * @param description - * @param identity - * @returns Id of new Account - */ - async function addAccount( - name: string, - description: string, - identity: string - ): Promise { - const account: IDBAccount = { - [Field.CREATED_TIMESTAMP]: new Date().getTime(), - [Field.NAME]: name, - [Field.DESCRIPTION]: description, - [Field.IDENTITY]: identity, - }; - return await dexieWrapper.table(AppTable.ACCOUNTS).add(account); - } - - return { - getAccountsTable, - addAccount, - }; -} diff --git a/src/views/AccountViewView.vue b/src/views/AccountViewView.vue index 9e8b9f038..bd4e55f34 100644 --- a/src/views/AccountViewView.vue +++ b/src/views/AccountViewView.vue @@ -228,7 +228,9 @@ export default class AccountViewView extends Vue { odexie._allTables.accounts .add({ publicKey: newId.keys[0].publicKeyHex, + mnemonic: mnemonic, identity: JSON.stringify(newId), + dateCreated: new Date().getTime(), }) .then(function () { odexie._allTables.accounts.each(function (account) { diff --git a/vue.config.js b/vue.config.js index 7a47bf41a..c38a5949a 100644 --- a/vue.config.js +++ b/vue.config.js @@ -5,6 +5,6 @@ module.exports = defineConfig({ devtool: "source-map", experiments: { topLevelAwait: true, - } + }, }, });