JimmyMow
6 years ago
committed by
GitHub
27 changed files with 528 additions and 395 deletions
@ -0,0 +1,72 @@ |
|||
import Dexie from 'dexie' |
|||
|
|||
// Suffex the database name with NODE_ENV so that wer can have per-env databases.
|
|||
export const getDbName = () => { |
|||
let name = `ZapDesktop` |
|||
if (process.env.NODE_ENV) { |
|||
name += `.${process.env.NODE_ENV}` |
|||
} |
|||
return name |
|||
} |
|||
|
|||
// Define the database.
|
|||
const db = new Dexie(getDbName()) |
|||
db.version(1).stores({ |
|||
settings: 'key', |
|||
wallets: '++id, type, chain, network', |
|||
nodes: 'id' |
|||
}) |
|||
|
|||
// Wallet with ID 1 will always be a local bitcoin wallet.
|
|||
// This ensures that useres upgrading from older versions do not loose their initial wallet.
|
|||
db.on('populate', () => { |
|||
db.wallets.add({ type: 'local', currency: 'bitcoin' }) |
|||
}) |
|||
|
|||
/** |
|||
* @class Wallet |
|||
* Wallet helper class. |
|||
*/ |
|||
export const Wallet = db.wallets.defineClass({ |
|||
id: Number, |
|||
type: String, |
|||
currency: String, |
|||
network: String, |
|||
alias: String, |
|||
autopilot: Boolean, |
|||
cert: String, |
|||
host: String, |
|||
macaroon: String |
|||
}) |
|||
|
|||
/** |
|||
* @class Node |
|||
* Node helper class. |
|||
*/ |
|||
export const Node = db.nodes.defineClass({ |
|||
id: String, |
|||
hasSynced: Boolean, |
|||
addresses: Object |
|||
}) |
|||
|
|||
/** |
|||
* Get current address of a given type. |
|||
* @param {String} type type of address to fetch. |
|||
* @return {String} current address of requested type, if one exists. |
|||
*/ |
|||
Node.prototype.getCurrentAddress = function(type) { |
|||
return Dexie.getByKeyPath(this, `addresses.${type}`) |
|||
} |
|||
|
|||
/** |
|||
* Set current address of a given type. |
|||
* @param {String} type type of address to save. |
|||
* @param {String} address address to save. |
|||
* @return {Node} updated node instance. |
|||
*/ |
|||
Node.prototype.setCurrentAddress = function(type, address) { |
|||
Dexie.setByKeyPath(this, `addresses.${type}`, address) |
|||
return db.nodes.put(this) |
|||
} |
|||
|
|||
export default db |
@ -1,4 +1,7 @@ |
|||
import dark from './dark' |
|||
import light from './light' |
|||
|
|||
export { dark, light } |
|||
export { dark } |
|||
export { light } |
|||
|
|||
export default { dark, light } |
|||
|
@ -0,0 +1,6 @@ |
|||
import Dexie from 'dexie' |
|||
|
|||
Dexie.dependencies.indexedDB = require('fake-indexeddb') |
|||
Dexie.dependencies.IDBKeyRange = require('fake-indexeddb/lib/FDBKeyRange') |
|||
|
|||
module.exports = Dexie |
Loading…
Reference in new issue