Skip to content

Commit

Permalink
Merge pull request #8 from exAphex/sqlite
Browse files Browse the repository at this point in the history
Sqlite
  • Loading branch information
exAphex committed Jun 28, 2023
2 parents 91aafcd + 7c60ddd commit b7b7b86
Show file tree
Hide file tree
Showing 11 changed files with 5,502 additions and 34,051 deletions.
29,732 changes: 0 additions & 29,732 deletions package-lock.json

This file was deleted.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "p2pmanagerdev",
"version": "0.3.12",
"version": "0.4.0",
"author": "Aydin Tekin <exaphex@github.com>",
"description": "P2P Manager app",
"email": "exaphex@github.com",
Expand All @@ -16,6 +16,7 @@
"@testing-library/react": "^12.1.3",
"@testing-library/user-event": "^13.5.0",
"async-mutex": "^0.3.2",
"better-sqlite3": "^8.4.0",
"chart.js": "3.2.1",
"chartjs-adapter-moment": "^1.0.0",
"electron-json-storage": "^4.5.0",
Expand Down Expand Up @@ -61,7 +62,8 @@
"electron:start": "concurrently -k \"cross-env BROWSER=none yarn start\" \"wait-on http://localhost:3000 && electronmon .\"",
"electron:package:mac": "yarn build && electron-builder -m -c.extraMetadata.main=build/electron.js",
"electron:package:win": "yarn build && electron-builder -w -c.extraMetadata.main=build/electron.js",
"electron:package:linux": "yarn build && electron-builder -l -c.extraMetadata.main=build/electron.js"
"electron:package:linux": "yarn build && electron-builder -l -c.extraMetadata.main=build/electron.js",
"postinstall": "electron-builder install-app-deps"
},
"eslintConfig": {
"extends": [
Expand All @@ -86,6 +88,7 @@
"electron-fetch": "^1.7.4",
"electronmon": "^2.0.2",
"eslint": "^8.14.0",
"electron-rebuild": "^3.2.9",
"eslint-config-airbnb": "^19.0.4",
"eslint-config-google": "^0.14.0",
"eslint-config-standard": "^17.0.0",
Expand Down
13 changes: 7 additions & 6 deletions public/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,33 @@ const {ipcMain} = require('electron');
const store = require('electron-json-storage');
const fetch = require('node-fetch');
const accountHandler = require('./js/handlers/accounthandler.js');
const db = require('./js/db/database.js');

ipcMain.handle('i_query_account', (event, arg) => {
try {
return accountHandler.queryAccount(arg);
return accountHandler.queryAccount(db.getDatabase(), arg);
} catch (e) {
throw {
throw {
messsage: 'Generic error',
error: e,
};
}
});

ipcMain.on('add-account', (event, arg) => {
accountHandler.addAccount(event, arg);
accountHandler.addAccount(event, db.getDatabase(), arg);
});

ipcMain.on('list-accounts', (event, arg) => {
accountHandler.listAccounts(event, arg);
accountHandler.listAccounts(event, db.getDatabase());
});

ipcMain.on('delete-account', (event, arg) => {
accountHandler.deleteAccount(event, arg);
accountHandler.deleteAccount(event, db.getDatabase(), arg);
});

ipcMain.on('update-account', (event, arg) => {
accountHandler.updateAccount(event, arg);
accountHandler.updateAccount(event, db.getDatabase(), arg);
});

ipcMain.on('get-version', (event, arg) => {
Expand Down
26 changes: 26 additions & 0 deletions public/js/db/balancedao.js
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;
56 changes: 56 additions & 0 deletions public/js/db/database.js
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;
43 changes: 43 additions & 0 deletions public/js/db/userdao.js
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;
Loading

0 comments on commit b7b7b86

Please sign in to comment.