chore: refactor

This commit is contained in:
Filipe Medeiros 2021-11-26 12:19:51 +00:00
parent 6b0083e2ca
commit 3e271c68d7
8 changed files with 73 additions and 56 deletions

View file

@ -1,29 +1,23 @@
import Dexie, { Table } from 'dexie'
import db from '.'
import { AccountInfoCache } from '../types'
interface Account extends AccountInfoCache {
interface Account {
index: number
account: AccountInfoCache
}
class Accounts extends Dexie {
public accounts!: Table<Account, number>
export type Key = number
export type Value = Account
public constructor() {
super('Accounts')
this.version(1).stores({
accounts: 'index,account',
})
}
}
const db = new Accounts()
export const schema = 'index,account'
export const addAccount = (index: number, account: AccountInfoCache) =>
db.accounts.add({ ...account, index })
db.accounts.add({ account, index })
export const putAccount = (index: number, account: AccountInfoCache) =>
db.accounts.put({ ...account, index })
db.accounts.put({ account, index })
export const removeAccount = (index: number) => db.accounts.delete(index)
@ -31,9 +25,7 @@ export const getAccount = (index: number) =>
db.accounts
.where({ index })
.first()
.then(res => (res === undefined ? undefined : res))
.then(res => (res === undefined ? undefined : res.account))
export const hasAccount = async (index: number) =>
(await db.accounts.where({ index }).count()) > 0
export default db

View file

@ -1,5 +1,7 @@
import Dexie, { Table } from 'dexie'
import db from '.'
export type CryptoAssetId = 'challenge' | 'credentialId'
interface CryptoAsset {
@ -7,18 +9,10 @@ interface CryptoAsset {
cryptoAsset: Uint8Array
}
class CryptoAssets extends Dexie {
public cryptoAssets!: Table<CryptoAsset, CryptoAssetId>
export type Key = CryptoAssetId
export type Value = CryptoAsset
public constructor() {
super('CryptoAssets')
this.version(1).stores({
cryptoAssets: 'id,cryptoAsset',
})
}
}
const db = new CryptoAssets()
export const schema = 'id,cryptoAsset'
export const addCryptoAsset = (id: CryptoAssetId, cryptoAsset: Uint8Array) =>
db.cryptoAssets.add({ id, cryptoAsset })
@ -34,5 +28,3 @@ export const getCryptoAsset = (id: CryptoAssetId) =>
export const hasCryptoAsset = async (id: CryptoAssetId) =>
(await db.cryptoAssets.where({ id }).count()) > 0
export default db

View file

@ -1,5 +1,7 @@
import Dexie, { Table } from 'dexie'
import db from '.'
export type EncryptedSeedId = 'pin' | 'os'
interface EncryptedSeed {
@ -7,18 +9,10 @@ interface EncryptedSeed {
encryptedSeed: string
}
class EncryptedSeeds extends Dexie {
public encryptedSeeds!: Table<EncryptedSeed, EncryptedSeedId>
export type Key = EncryptedSeedId
export type Value = EncryptedSeed
public constructor() {
super('EncryptedSeeds')
this.version(1).stores({
encryptedSeeds: 'id,encryptedSeed',
})
}
}
const db = new EncryptedSeeds()
export const schema = 'id,encryptedSeed'
export const addEncryptedSeed = (id: EncryptedSeedId, encryptedSeed: string) =>
db.encryptedSeeds.add({ id, encryptedSeed })
@ -34,5 +28,3 @@ export const getEncryptedSeed = (id: EncryptedSeedId) =>
export const hasEncryptedSeed = async (id: EncryptedSeedId) =>
(await db.encryptedSeeds.where({ id }).count()) > 0
export default db

43
lib/db/index.ts Normal file
View file

@ -0,0 +1,43 @@
import Dexie, { Table } from 'dexie'
import {
Key as AccountsKey,
Value as AccountsValue,
schema as accountsSchema,
} from './accounts'
import {
Key as CryptoAssetsKey,
Value as CryptoAssetsValue,
schema as cryptoAssetsSchema,
} from './cryptoAssets'
import {
Key as EncryptedSeedsKey,
Value as EncryptedSeedsValue,
schema as encryptedSeedsSchema,
} from './encryptedSeeds'
import {
Key as PreferencesKey,
Value as PreferencesValue,
schema as preferencesSchema,
} from './preferences'
class Database extends Dexie {
public encryptedSeeds!: Table<EncryptedSeedsValue, EncryptedSeedsKey>
public cryptoAssets!: Table<CryptoAssetsValue, CryptoAssetsKey>
public accounts!: Table<AccountsValue, AccountsKey>
public preferences!: Table<PreferencesValue, PreferencesKey>
public constructor() {
super('Database')
this.version(1).stores({
encryptedSeeds: encryptedSeedsSchema,
cryptoAssets: cryptoAssetsSchema,
preferences: preferencesSchema,
accounts: accountsSchema,
})
}
}
const db = new Database()
export default db

View file

@ -1,5 +1,7 @@
import Dexie, { Table } from 'dexie'
import db from '.'
export enum ShowCurrencyPreference {
Xno = 'xno',
Both = 'both',
@ -20,18 +22,10 @@ interface Preference {
value: string
}
class Preferences extends Dexie {
public preferences!: Table<Preference, PreferenceName>
export type Key = PreferenceName
export type Value = Preference
public constructor() {
super('Preferences')
this.version(1).stores({
preferences: 'name,value',
})
}
}
const db = new Preferences()
export const schema = 'name,value'
export const addPreference = <P extends PreferenceName>(
name: P,
@ -58,5 +52,3 @@ export const getPreference = <P extends PreferenceName>(
export const hasPreference = async (name: PreferenceName) =>
(await db.preferences.where({ name }).count()) > 0
export default db

View file

@ -22,6 +22,7 @@
"clsx": "^1.1.1",
"crypto-js": "^4.1.1",
"dexie": "^3.2.0",
"dexie-react-hooks": "^1.0.7",
"jsqr": "^1.4.0",
"nanocurrency": "^2.5.0",
"nanocurrency-web": "^1.3.5",

View file

@ -1685,6 +1685,11 @@ detective@^5.2.0:
defined "^1.0.0"
minimist "^1.1.1"
dexie-react-hooks@^1.0.7:
version "1.0.7"
resolved "https://registry.yarnpkg.com/dexie-react-hooks/-/dexie-react-hooks-1.0.7.tgz#50316a7829a6dfa013b8471f66b010cc77f00d3e"
integrity sha512-hqXGFbfgu1rdfcGHQUPwW2G0iWyupoNWnk3ODvqr+HdZt2ip3y1e/dcWIOsEnlUWWCWk6a3+ok0fvECU05eE2A==
dexie@^3.2.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/dexie/-/dexie-3.2.0.tgz#a1b0267b111f9422c4126da90d6b121b1deabeab"