Skip to content

Commit

Permalink
Code refactor, UI improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
saikksub committed Feb 9, 2019
1 parent d2a286d commit dcd9951
Show file tree
Hide file tree
Showing 30 changed files with 898 additions and 290 deletions.
9 changes: 3 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@
"titlebar": "^1.4.0",
"uniqid": "^5.0.3",
"vue": "^2.5.22",
"vue-template-compiler": "^2.5.22",
"vue-electron": "^1.0.6",
"vue-router": "^3.0.1",
"vue-template-compiler": "^2.5.22",
"vuetify": "^1.4.3",
"vuex": "^3.1.0",
"winston": "^3.1.0",
Expand Down
102 changes: 0 additions & 102 deletions src/main/database/commands.js

This file was deleted.

9 changes: 0 additions & 9 deletions src/main/database/helper.js

This file was deleted.

28 changes: 20 additions & 8 deletions src/main/database/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
/**
* SQL database wrapper
*/

'use strict'
import sql from './sql'
import helper from './helper'
import commands from './commands'

/**
* Sql database wrapper class
*/
const NodeSqlite3 = function (props) {
this.config = props
this.db = null
}

/**
* Initialize SQL database
* Creates/ensures necessary tables for SQL database
*/
NodeSqlite3.prototype.init = function (callback) {
const context = this
helper.ensureDatabaseDir(this.config)
sql.ensureDatabaseDir(this.config)
sql.ensureDb(context.config, function (err, db) {
if (err) {
console.log(err)
Expand All @@ -35,10 +44,13 @@ NodeSqlite3.prototype.init = function (callback) {
})
}

NodeSqlite3.prototype.readFullTable = commands.readFullTable
NodeSqlite3.prototype.readTableEntryID = commands.readTableByID
NodeSqlite3.prototype.writeTable = commands.writeTable
NodeSqlite3.prototype.updateTableByID = commands.updateTableByID
NodeSqlite3.prototype.deleteTableById = commands.deleteTableById
/**
* SQL Database commands API
*/
NodeSqlite3.prototype.readFullTable = sql.readFullTable
NodeSqlite3.prototype.readTableEntryID = sql.readTableByID
NodeSqlite3.prototype.writeTable = sql.writeTable
NodeSqlite3.prototype.updateTableByID = sql.updateTableByID
NodeSqlite3.prototype.deleteTableById = sql.deleteTableById

export default NodeSqlite3
130 changes: 130 additions & 0 deletions src/main/database/sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@

const sqlite3 = require('sqlite3')
const path = require('path')
const fsExtra = require('fs-extra')

export default {
/**
* Ensures directory is ready for SQL database
*/
ensureDatabaseDir: function (config) {
fsExtra.ensureDirSync(config.path)
},
/**
* Ensures SQL database is created
*/
ensureDb: function (config, callback) {
const dbFile = path.join(
config.path,
Expand All @@ -21,5 +31,125 @@ export default {
}
}
)
},

/**
* Read entire rows of specified table
*/
readFullTable: function (data, callback) {
const context = this
context.db.serialize(function () {
context.db.all(
`SELECT * FROM ${data.table}`,
function (error, rows) {
if (error) {
callback(error)
} else {
console.log('readFullTable', rows)
callback(null, rows)
}
}
)
})
},

/**
* Read table with entry/row id
*/
readTableByID: function (data, callback) {
const context = this
context.db.serialize(function () {
context.db.all(
`SELECT row FROM ${data.table} WHERE id=${data.id}`,
function (error, row) {
if (error) {
callback(error)
} else {
callback(null, row)
}
}
)
})
},

/**
* Write a new entry/row of specified table
*/
writeTable: function (data, callback) {
console.log(data)
const values = data.values.replace(/ /g, '').split(',')
const context = this
context.db.serialize(function () {
context.db.all(
`SELECT * FROM ${data.table} WHERE key=${values[0]}`,
function (error, rows) {
if (error) {
callback(error)
} else {
if (rows.length > 0) {
context.db.run(
`UPDATE ${data.table} SET value=${values[1]} WHERE key=${values[0]}`,
function (err) {
if (err) {
console.log(err)
} else {
console.log('updated')
}
}
)
} else {
context.db.run(
`INSERT INTO ${data.table}(${data.keys}) VALUES(${data.values})`,
function (err) {
if (err) {
console.log(err)
} else {
console.log('updated')
}
}
)
}
}
}
)
})
},

/**
* Update table row of specified id
*/
updateTableByID: function (data, callback) {
const context = this
context.db.serialize(function () {
context.db.run(
`UPDATE INTO ${data.table} SET ${data.assignments}`,
function (error) {
if (error) {
callback(error)
} else {
callback(null)
}
}
)
})
},

/**
* Delete table row of specified id
*/
deleteTableById: function (data, callback) {
const context = this
context.db.serialize(function () {
context.db.run(
`DELETE FROM ${data.table} WHERE ${data.condition}`,
function (error) {
if (error) {
callback(error)
} else {
callback(null)
}
}
)
})
}
}
Loading

0 comments on commit dcd9951

Please sign in to comment.