Permalink
Browse files

Adding services/data folder

  • Loading branch information...
andrewshell committed May 31, 2017
1 parent 5653ce8 commit 73218c7df73127ff22e67bd8fdba3fbb2a469684
Showing with 65 additions and 1 deletion.
  1. +1 −1 .gitignore
  2. +25 −0 services/data/index.js
  3. +39 −0 services/data/init.js
View
@@ -2,6 +2,6 @@
.env
.node-version
coverage/
data/
/data/
node_modules/
Procfile
View
@@ -0,0 +1,25 @@
(function () {
"use strict";
var db,
init = require('./init.js');
function getDb(callback) {
if (undefined === db) {
init(function cbInitDb(err, myDb) {
if (err) {
return callback(err);
}
db = myDb;
return callback(null, db);
});
} else {
return callback(null, db);
}
}
module.exports = {
'init': init,
'getDb': getDb
};
}());
View
@@ -0,0 +1,39 @@
(function () {
"use strict";
var async = require('async'),
DBMigrate = require('db-migrate'),
sqlite3 = require('sqlite3');
function connectToDatabase(filename, callback) {
var db = new sqlite3.Database(
filename,
sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE,
function completeConnect(err) {
if (err) {
return callback(err);
}
return callback(null, db);
}
);
}
function init(callback) {
var dbmigrate;
async.waterfall([
function asyncDoMigration(callback) {
dbmigrate = DBMigrate.getInstance(true);
dbmigrate.up(callback);
},
function asyncConnectToDatabase(callback) {
if ('sqlite3' === dbmigrate.config.getCurrent().settings.driver) {
connectToDatabase(dbmigrate.config.getCurrent().settings.filename, callback);
} else {
callback('This application currently requires sqlite3 as the driver');
}
}
], callback);
}
module.exports = init;
}());

0 comments on commit 73218c7

Please sign in to comment.