UNPKG

@orchestrated-io/cqrs-domain

Version:

Node-cqrs-domain is a node.js module based on node-eventstore. It can be very useful as domain component if you work with (d)ddd, cqrs, eventdenormalizer, host, etc.

89 lines (71 loc) 2.02 kB
var tolerate = require('tolerance'), _ = require('lodash'), Base = require('./base'); function exists(toCheck) { var _exists = require('fs').existsSync || require('path').existsSync; if (require('fs').accessSync) { _exists = function (toCheck) { try { require('fs').accessSync(toCheck); return true; } catch (e) { return false; } }; } return _exists(toCheck); } function getSpecificDbImplementation(options) { options = options || {}; options.type = options.type || 'inmemory'; if (_.isFunction(options.type)) { return options.type; } options.type = options.type.toLowerCase(); var dbPath = __dirname + "/databases/" + options.type + ".js"; if (!exists(dbPath)) { var errMsg = 'Implementation for db "' + options.type + '" does not exist!'; console.log(errMsg); throw new Error(errMsg); } try { var db = require(dbPath); return db; } catch (err) { if (err.message.indexOf('Cannot find module') >= 0 && err.message.indexOf("'") > 0 && err.message.lastIndexOf("'") !== err.message.indexOf("'")) { var moduleName = err.message.substring(err.message.indexOf("'") + 1, err.message.lastIndexOf("'")); console.log('Please install module "' + moduleName + '" to work with db implementation "' + options.type + '"!'); } throw err; } } module.exports = { Lock: Base, create: function(options, callback) { if (typeof options === 'function') { callback = options; options = {}; } options = options || {}; var Lock; try { Lock = getSpecificDbImplementation(options); } catch (err) { if (callback) callback(err); throw err; } var lock = new Lock(options); if (callback) { process.nextTick(function () { tolerate(function (callback) { lock.connect(callback); }, options.timeout || 0, callback || function () { }); }); } return lock; } };