-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from exAphex/sqlite
Sqlite
- Loading branch information
Showing
11 changed files
with
5,502 additions
and
34,051 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
|
||
const getBalances = (db, id) => { | ||
const stmt = db.prepare("SELECT * FROM balances where id = ?"); | ||
let balances = stmt.all(id); | ||
let retData = {}; | ||
for (let i = 0; i < balances.length; i++) { | ||
retData[balances[i].date] = {total: balances[i].total, invested: balances[i].invested, uninvested: balances[i].uninvested, loss: balances[i].loss, profit: balances[i].profit}; | ||
} | ||
return retData; | ||
}; | ||
|
||
const upsertBalance = (db, id, date, balanceData) => { | ||
const stmt = db.prepare("SELECT * FROM balances where id = ? AND date = ?"); | ||
let balances = stmt.all(id, date); | ||
if (balances.length > 0) { | ||
const updateStmt = db.prepare("UPDATE balances SET total = ?, uninvested = ?, invested = ?, profit = ?, loss = ? WHERE id = ? and date = ?"); | ||
updateStmt.run(balanceData.total, balanceData.uninvested, balanceData.invested, balanceData.profit, balanceData.loss, id, date); | ||
} else { | ||
const insertStmt = db.prepare("INSERT INTO balances VALUES (?,?,?,?,?,?,?)"); | ||
insertStmt.run(id, date, balanceData.total, balanceData.uninvested, balanceData.invested, balanceData.profit, balanceData.loss); | ||
} | ||
return getBalances(db, id); | ||
}; | ||
|
||
exports.getBalances = getBalances; | ||
exports.upsertBalance = upsertBalance; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
const {app} = require('electron'); | ||
console.log(app.getPath('userData')); | ||
const db = require('better-sqlite3')(app.getPath('userData')+ '/accounts.db'); | ||
const { listAccountsStore } = require('../handlers/accounthandler.js'); | ||
const schemaVersion = 1; | ||
|
||
|
||
const insertVersion = (ver) => { | ||
const insertStmt = db.prepare("INSERT INTO version VALUES (?)"); | ||
insertStmt.run(ver); | ||
}; | ||
|
||
const migrate = (version) => { | ||
if (version < 1) { | ||
db.exec("CREATE TABLE accounts (id TEXT NOT NULL,name TEXT NOT NULL,type TEXT NOT NULL,user TEXT,password TEXT,description TEXT, lastUpdated INTEGER)"); | ||
let accounts = listAccountsStore(); | ||
for (let i = 0; i < accounts.length; i++) { | ||
const insertStmt = db.prepare("INSERT INTO accounts VALUES (?,?,?,?,?,?,?)"); | ||
insertStmt.run(accounts[i].id, accounts[i].name, accounts[i].type, accounts[i].user, accounts[i].password, accounts[i].description, accounts[i].lastUpdated); | ||
} | ||
insertVersion(1); | ||
} else if (version < 2) { | ||
db.exec("CREATE TABLE balances (id TEXT NOT NULL,date TEXT NOT NULL, total REAL, uninvested REAL, invested REAL, profit REAL, loss REAL)"); | ||
db.exec("CREATE UNIQUE INDEX idx_balance_user_date ON balances (id, date)"); | ||
let accounts = listAccountsStore(); | ||
for (let i = 0; i < accounts.length; i++) { | ||
for (var key in accounts[i].balances) { | ||
const insertStmt = db.prepare("INSERT INTO balances VALUES (?,?,?,?,?,?,?)"); | ||
insertStmt.run(accounts[i].id, key, accounts[i].balances[key].total, accounts[i].balances[key].uninvested, accounts[i].balances[key].invested, accounts[i].balances[key].profit, accounts[i].balances[key].loss); | ||
} | ||
} | ||
insertVersion(2); | ||
} | ||
return (schemaVersion <= version); | ||
}; | ||
|
||
const getDatabase = () => { | ||
return db; | ||
}; | ||
|
||
const prepareDB = () => { | ||
db.exec(` | ||
CREATE TABLE IF NOT EXISTS version (version INT); | ||
`); | ||
const stmt = db.prepare("SELECT * FROM version ORDER BY version DESC LIMIT 1"); | ||
const retVersion = stmt.all(); | ||
let dbVersion = (retVersion.length > 0) ? retVersion[0].version : 0; | ||
while(!migrate(dbVersion)) { | ||
dbVersion++; | ||
} | ||
}; | ||
|
||
db.pragma("journal_mode = WAL"); | ||
prepareDB(); | ||
|
||
exports.getDatabase = getDatabase; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
|
||
const listAccounts = (db) => { | ||
const stmt = db.prepare("SELECT * FROM accounts"); | ||
return stmt.all(); | ||
}; | ||
|
||
|
||
const getAccount = (db, id) => { | ||
const stmt = db.prepare("SELECT * FROM accounts where id = ?"); | ||
return stmt.all(id); | ||
}; | ||
|
||
const createAccount = (db, account) => { | ||
const insertStmt = db.prepare("INSERT INTO accounts VALUES (?,?,?,?,?,?,?)"); | ||
insertStmt.run(account.id, account.name, account.type, account.user, account.password, account.description, account.lastUpdated); | ||
return listAccounts(db); | ||
}; | ||
|
||
const deleteAccount = (db, accountID) => { | ||
const insertStmt = db.prepare("DELETE FROM accounts where id = ?"); | ||
insertStmt.run(accountID); | ||
return listAccounts(db); | ||
} | ||
|
||
const updateAccount = (db, account) => { | ||
const today = new Date(); | ||
const insertStmt = db.prepare("UPDATE accounts SET name = ?, user = ?, password = ?, description = ?, lastUpdated = ? where ID = ?"); | ||
insertStmt.run(account.name, account.user, account.password, account.description, today.getTime() - today.getTimezoneOffset() * 60000, account.id); | ||
return listAccounts(db); | ||
} | ||
|
||
const updateAccountLastUpdated = (db, id, lastUpdated) => { | ||
const insertStmt = db.prepare("UPDATE accounts SET lastUpdated = ? where ID = ?"); | ||
insertStmt.run(lastUpdated, id); | ||
return listAccounts(db); | ||
} | ||
|
||
exports.updateAccount = updateAccount; | ||
exports.updateAccountLastUpdated = updateAccountLastUpdated; | ||
exports.listAccounts = listAccounts; | ||
exports.createAccount = createAccount; | ||
exports.deleteAccount = deleteAccount; | ||
exports.getAccount = getAccount; |
Oops, something went wrong.