UNPKG

mongo-milestone

Version:

*A life-saving little tool to work around the lack of ACID Transactions in MongoDB*

87 lines (66 loc) 2.54 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.PARAMETERS_EXPECTED = exports.ACTION_REQUIRED = exports.TYPE_REQUIRED = undefined; var _q = require('q'); var _q2 = _interopRequireDefault(_q); var _mongodb = require('mongodb'); var _Action = require('./Action'); var _Action2 = _interopRequireDefault(_Action); var _robot = require('../robot'); var _robot2 = _interopRequireDefault(_robot); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } const TYPE_REQUIRED = exports.TYPE_REQUIRED = 'The Milestone type is required'; const ACTION_REQUIRED = exports.ACTION_REQUIRED = 'The Miletone root Action must be an Action'; const PARAMETERS_EXPECTED = exports.PARAMETERS_EXPECTED = 'A configuration object { type, action, parameters } is expected'; class Milestone { constructor(...params) { const setup = ({ type, action, parameters }) => { if (!type) { throw new Error(TYPE_REQUIRED); } if (!(action instanceof _Action2.default)) { throw new Error(ACTION_REQUIRED); } this._id = new _mongodb.ObjectId(); this.type = type; this.action = action; this.beginDate = new Date(); this.endDate = null; this.parameters = parameters; this.state = false; this.report = []; this.output = null; }; switch (params.length) { case 0: throw new Error(PARAMETERS_EXPECTED); case 1: setup.apply(null, params); break; default: const [type, action, parameters] = params; setup({ type, action, parameters }); } } save() { return _robot2.default.createMilestone(this).then(milestone => _robot2.default.startMilestone(milestone._id)); } static spawn(...params) { const run = ({ type, action, parameters }) => { const milestone = new Milestone({ type, action, parameters }); return milestone.save(); }; switch (params.length) { case 0: throw new Error(PARAMETERS_EXPECTED); case 1: return run.apply(null, params); default: const [type, action, parameters] = params; return run({ type, action, parameters }); } } } exports.default = Milestone;