Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return (function() {
var Loki = require('lokijs');
function LokiFsSyncAdapter() {
this.fs = require('fs');
}
Object.assign(LokiFsSyncAdapter.prototype, Loki.persistenceAdapters
.fs.prototype, {
loadDatabase: function(dbname, callback) {
var err = null;
var stats;
stats = this.fs.statSync(dbname);
if (!err && stats && stats.isFile()) {
// readFileSync() is the synchronous alternative to async readFile() which loki normally uses
var data = this.fs.readFileSync(dbname, {
encoding: 'utf8'
});
callback(data);
} else if (!stats.isFile()) {
throw new Error(dbname + ' is not a file!');
} else {
throw new Error('Error loading db');
}
var DB = function(path) {
// create a Loki.js instance with filesystem storega in particular path
const db = new ExtendedLoki(path, {
adapter: new Loki.persistenceAdapters.fs()
})
return new Promise((resolve, reject) => {
db.loadDatabase({}, (err, _) => {
if(err) {
logger.warn('Unable to load database, ignoring', {path, err})
return resolve(db)
}
logger.debug('Database loaded', {path})
return resolve(db)
})
})
}