Browse Source

Cleaned up old Dexie class. Added mnemonic and dateCreated columns.

experimental_plugin
Matthew Aaron Raymer 2 years ago
parent
commit
607230b51c
  1. 27
      src/constants/model.ts
  2. 6
      src/constants/table.ts
  3. 5
      src/db/tables/accounts.ts
  4. 9
      src/models/Account.ts
  5. 34
      src/services/DexieWrapper.ts
  6. 41
      src/use/useDBAccounts.ts
  7. 2
      src/views/AccountViewView.vue
  8. 2
      vue.config.js

27
src/constants/model.ts

@ -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",
}

6
src/constants/table.ts

@ -1,6 +0,0 @@
/**
* Dexie table names used by the DexieWrapper service.
*/
export enum AppTable {
ACCOUNTS = "Accounts-Table",
}

5
src/db/tables/accounts.ts

@ -3,13 +3,16 @@ import { Table } from "dexie";
export type Account = { export type Account = {
id?: number; id?: number;
publicKey: string; publicKey: string;
mnemonic: string;
identity: string; identity: string;
dateCreated: number;
}; };
export type AccountsTable = { export type AccountsTable = {
accounts: Table<Account>; accounts: Table<Account>;
}; };
// mark encrypted field by starting with a $ character
export const accountsSchema = { export const accountsSchema = {
accounts: "++id, publicKey, $identity", accounts: "++id, publicKey, $mnemonic, $identity, dateCreated",
}; };

9
src/models/Account.ts

@ -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;
}

34
src/services/DexieWrapper.ts

@ -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<IDBAccount>;
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
);

41
src/use/useDBAccounts.ts

@ -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<IDBAccount[]> {
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<IndexableType> {
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,
};
}

2
src/views/AccountViewView.vue

@ -228,7 +228,9 @@ export default class AccountViewView extends Vue {
odexie._allTables.accounts odexie._allTables.accounts
.add({ .add({
publicKey: newId.keys[0].publicKeyHex, publicKey: newId.keys[0].publicKeyHex,
mnemonic: mnemonic,
identity: JSON.stringify(newId), identity: JSON.stringify(newId),
dateCreated: new Date().getTime(),
}) })
.then(function () { .then(function () {
odexie._allTables.accounts.each(function (account) { odexie._allTables.accounts.each(function (account) {

2
vue.config.js

@ -5,6 +5,6 @@ module.exports = defineConfig({
devtool: "source-map", devtool: "source-map",
experiments: { experiments: {
topLevelAwait: true, topLevelAwait: true,
} },
}, },
}); });

Loading…
Cancel
Save