Genesis commit
This commit is contained in:
128
persistence/persistence.js
Normal file
128
persistence/persistence.js
Normal file
@@ -0,0 +1,128 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function () {
|
||||
var db = {};
|
||||
|
||||
const config = require('config');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const nanoid = require('nanoid');
|
||||
const tunnel = require('tunnel-ssh');
|
||||
|
||||
var dbOptions = {
|
||||
db: db,
|
||||
moment: require('moment'),
|
||||
generateUniqueId: (customIdSize) => {
|
||||
var idSize = customIdSize || 11;
|
||||
return nanoid(idSize); //=> "hsCc0ocrXkc"
|
||||
}
|
||||
};
|
||||
|
||||
dbOptions.Sequelize = require('sequelize');
|
||||
dbOptions.bcrypt = require('bcrypt');
|
||||
|
||||
dbOptions.sequelize = new dbOptions.Sequelize(config.get('database'));
|
||||
|
||||
// Load all the models...
|
||||
var modelDir = path.join(__dirname, './models/');
|
||||
fs.readdirSync(modelDir)
|
||||
.filter(file => {
|
||||
return (file.indexOf('.') !== 0) && (file !== "index.js") && (file.slice(-3) === '.js');
|
||||
})
|
||||
.forEach(file => {
|
||||
// const model = sequelize['import'](path.join(modelDir, file));
|
||||
const model = require(path.join(modelDir, file))(dbOptions.sequelize, dbOptions.Sequelize, dbOptions);
|
||||
|
||||
// Format the Model Name like we have for this project...
|
||||
var modelName = model.name;
|
||||
modelName = modelName.replace(modelName[0], modelName[0].toUpperCase());
|
||||
|
||||
// Add model to the db...
|
||||
db[modelName] = model;
|
||||
});
|
||||
|
||||
// Time for the associations...
|
||||
|
||||
Object.keys(db).forEach(modelName => {
|
||||
if (db[modelName].associate) {
|
||||
console.log("Loading associations for " + modelName);
|
||||
db[modelName].associate(db);
|
||||
}
|
||||
});
|
||||
|
||||
db.LoadDB = () => {
|
||||
db.Sequelize = dbOptions.Sequelize;
|
||||
db.sequelize = dbOptions.sequelize;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
tunneling()
|
||||
.then(() => {
|
||||
return dbOptions.sequelize.sync()
|
||||
}).then(() => {
|
||||
resolve(db);
|
||||
}).catch(error => {
|
||||
// Do the things...
|
||||
reject(error);
|
||||
console.error('Unable to connect to the database:', error);
|
||||
})
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
db.updateDatabaseFromModels = () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
dbOptions.sequelize.sync({
|
||||
alter: true
|
||||
})
|
||||
.then(() => {
|
||||
resolve(true);
|
||||
console.log("DB update successful.");
|
||||
})
|
||||
.catch(error => {
|
||||
// Do the things...
|
||||
reject(error);
|
||||
console.error('Unable to connect to the database:', error);
|
||||
})
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
var tunneling = () => {
|
||||
const tunnelConfigs = config.get("tunnel");
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
if(tunnelConfigs.useTunnel) {
|
||||
const privateKeyFilePath = path.resolve(tunnelConfigs["privateKeyFilePath"]);
|
||||
// Run the tunnel...
|
||||
var sshConfig = {
|
||||
"keepAlive": tunnelConfigs["keepAlive"],
|
||||
"username": tunnelConfigs["username"],
|
||||
// "password": tunnelConfigs["password"],
|
||||
"host": tunnelConfigs["host"],
|
||||
"port": tunnelConfigs["port"],
|
||||
"dstHost": tunnelConfigs["dstHost"],
|
||||
"dstPort": tunnelConfigs["dstPort"],
|
||||
// "localHost": tunnelConfigs["localHost"],
|
||||
"localPort": tunnelConfigs["localPort"],
|
||||
"privateKey": fs.readFileSync(privateKeyFilePath),
|
||||
"passphrase": tunnelConfigs["passphrase"]
|
||||
}
|
||||
console.log("Private Key path: ", privateKeyFilePath);
|
||||
tunnel(sshConfig, (error, server) => {
|
||||
if(error) {
|
||||
console.error("Tunneling: ", error);
|
||||
reject(error);
|
||||
} else {
|
||||
console.log('Tunnel server:', server);
|
||||
resolve(server)
|
||||
}
|
||||
})
|
||||
} else {
|
||||
// Continue without setting up a tunnel...
|
||||
resolve();
|
||||
}
|
||||
})
|
||||
}
|
||||
return db;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user