Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "main.js",
"scripts": {
"start": "electron .",
"test": "jest",
"test": "jest --runInBand",
Comment thread
Phobez marked this conversation as resolved.
"rebuild": "electron-rebuild -f -w sqlite3",
"lint": "./node_modules/.bin/eslint"
},
Expand Down
51 changes: 33 additions & 18 deletions src/repository/DbContext.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,42 @@
const config = require('../services/config.json');
let dbConfig = config.knexConfig;
const fs = require('fs');
const knex = require('knex');

const getKnexObject = () => {
if (dbConfig.client === 'sqlite3') {
return getSqliteKnexObject();
}
}
const config = require('../services/config.json');

module.exports = {
getKnexObject
}
const dbConfig = config.knexConfig;

/**
* Get knex object with sqlite client
*/
function getSqliteKnexObject() {
const filepaths = config.sqlite_config.filepath;
for (let filepath of filepaths) {
if (fs.existsSync(filepath)) {
dbConfig.connection.filename = filepath;
return knex(dbConfig);
}
function getSqliteKnexObject(ENV) {
const filename = (ENV === config.environment.name.Test)
? config.sqlite_config.test_db
: config.sqlite_config.production_db;
const filepaths = config.sqlite_config.filepath;

filepaths.forEach((filepath) => {
const fullFilePath = filepath + filename;

if (fs.existsSync(fullFilePath)) {
dbConfig.connection.filename = fullFilePath;

return knex(dbConfig);
}
}

return null;
});

return new Error('Cannot get SQLite Knex object.');
}

const getKnexObject = (ENV = config.environment.name.Production) => {
if (dbConfig.client === 'sqlite3') {
return getSqliteKnexObject(ENV);
}

return new Error('Database is not SQLite 3.');
};

module.exports = {
getKnexObject,
};
Loading