mirror of https://github.com/requarks/wiki
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
925 B
52 lines
925 B
8 years ago
|
"use strict";
|
||
|
|
||
|
var loki = require('lokijs'),
|
||
|
fs = require("fs"),
|
||
|
path = require("path"),
|
||
|
Promise = require('bluebird'),
|
||
|
_ = require('lodash');
|
||
|
|
||
8 years ago
|
var cols = ['User','Entry'];
|
||
|
|
||
8 years ago
|
/**
|
||
|
* Loki.js module
|
||
|
*
|
||
|
* @param {Object} appconfig Application config
|
||
|
* @return {Object} LokiJS instance
|
||
|
*/
|
||
|
module.exports = function(appconfig) {
|
||
|
|
||
|
let dbReadyResolve;
|
||
|
let dbReady = new Promise((resolve, reject) => {
|
||
|
dbReadyResolve = resolve;
|
||
|
});
|
||
|
|
||
|
// Initialize Loki.js
|
||
|
|
||
|
let dbModel = {
|
||
|
Store: new loki(path.join(appconfig.datadir.db, 'app.db'), {
|
||
|
env: 'NODEJS',
|
||
|
autosave: true,
|
||
|
autosaveInterval: 5000
|
||
|
}),
|
||
|
onReady: dbReady
|
||
|
};
|
||
|
|
||
|
// Load Models
|
||
|
|
||
|
dbModel.Store.loadDatabase({}, () => {
|
||
|
|
||
8 years ago
|
_.forEach(cols, (col) => {
|
||
|
dbModel[col] = dbModel.Store.getCollection(col);
|
||
|
if(!dbModel[col]) {
|
||
|
dbModel[col] = dbModel.Store.addCollection(col);
|
||
8 years ago
|
}
|
||
|
});
|
||
|
|
||
|
dbReadyResolve();
|
||
|
|
||
|
});
|
||
|
|
||
|
return dbModel;
|
||
|
|
||
|
};
|