@fabrix/spool-broadcast
Version:
Spool: broadcast for Fabrix to implement CQRS and Event Sourcing
212 lines • 8.8 kB
JavaScript
"use strict";
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
t[p[i]] = s[p[i]];
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
const common_1 = require("@fabrix/fabrix/dist/common");
const lodash_1 = require("lodash");
const errors_1 = require("@fabrix/spool-errors/dist/errors");
function Point({ req = null, body = null, options = null, expects_input = null, expects_response = null, docs = null }) {
return function (target, propertyKey, descriptor) {
return descriptor;
};
}
exports.Point = Point;
function Command({ req = null, body = null, options = null, expects_input = null, expects_response = null, docs = null, story = null }) {
return function (target, propertyKey, descriptor) {
return descriptor;
};
}
exports.Command = Command;
function Action({ req = null, body = null, options = null, expects_input = null, expects_response = null, docs = null }) {
return function (target, propertyKey, descriptor) {
return descriptor;
};
}
exports.Action = Action;
class Entry extends common_1.FabrixGeneric {
get name() {
return this.constructor.name;
}
query(model, method, query) {
if (!this.app.models[model]) {
throw new Error('E_BAD_REQUEST_MODEL');
}
if (!this.app.models[model][method]) {
throw new Error('E_BAD_REQUEST_MODEL_METHOD');
}
return this.app.models[model][method](query)
.then(_data => {
if (_data && _data.rows || _data && lodash_1.isArray(_data)) {
_data.forEach(d => {
d.isReloaded = true;
d.isNewRecord = false;
});
}
else {
_data.isReloaded = true;
_data.isNewRecord = false;
return _data;
}
});
}
prepQuery(req, { query }, options) {
const mapShortToLong = {
'$and': this.app.spools.sequelize._datastore.Op.and,
'$or': this.app.spools.sequelize._datastore.Op.or,
'>': this.app.spools.sequelize._datastore.Op.gt,
'$gt': this.app.spools.sequelize._datastore.Op.gt,
'>=': this.app.spools.sequelize._datastore.Op.gte,
'$gte': this.app.spools.sequelize._datastore.Op.gt,
'<': this.app.spools.sequelize._datastore.Op.lt,
'$lt': this.app.spools.sequelize._datastore.Op.gt,
'<=': this.app.spools.sequelize._datastore.Op.lte,
'$lte': this.app.spools.sequelize._datastore.Op.gt,
'$ne': this.app.spools.sequelize._datastore.Op.ne,
'$eq': this.app.spools.sequelize._datastore.Op.eq,
'$not': this.app.spools.sequelize._datastore.Op.not,
'$between': this.app.spools.sequelize._datastore.Op.between,
'$notBetween': this.app.spools.sequelize._datastore.Op.$notBetween,
'$in': this.app.spools.sequelize._datastore.Op.in,
'$notIn': this.app.spools.sequelize._datastore.Op.notIn,
'$like': this.app.spools.sequelize._datastore.Op.like,
'$notLike': this.app.spools.sequelize._datastore.Op.notLike,
'$iLike': this.app.spools.sequelize._datastore.Op.iLike,
'$notILike': this.app.spools.sequelize._datastore.Op.notIlike,
'$startsWith': this.app.spools.sequelize._datastore.Op.startsWith,
'$endsWith': this.app.spools.sequelize._datastore.Op.endsWith,
'$reqexp': this.app.spools.sequelize._datastore.Op.regex,
'$notRegexp': this.app.spools.sequelize._datastore.Op.notRegexp,
'$iRegex': this.app.spools.sequelize._datastore.Op.iRegexp,
'$notIRegexp': this.app.spools.sequelize._datastore.Op.notIRegexp,
'$overlap': this.app.spools.sequelize._datastore.Op.overlap,
'$contains': this.app.spools.sequelize._datastore.Op.contains,
'$contained': this.app.spools.sequelize._datastore.Op.contained,
'$any': this.app.spools.sequelize._datastore.Op.any,
'$col': this.app.spools.sequelize._datastore.Op.col,
};
function refitKeys(o) {
let key, destKey, ix, value;
for (key in o) {
if (typeof key === 'string' && o.hasOwnProperty(key)) {
destKey = mapShortToLong[key] || key;
value = o[key];
if (!lodash_1.isArray(value) && lodash_1.isObject(value)) {
value = refitKeys(value);
}
else if (lodash_1.isArray(value)) {
}
else {
}
o[destKey] = value;
}
}
return o;
}
query = query || {};
if (query.where) {
query.where = refitKeys(query.where);
}
return query;
}
modelError(...params) {
return new errors_1.GenericError(...params);
}
rowExists(req, { params }, options = {}) {
const { resource } = params, p = __rest(params, ["resource"]);
const model = resource && this.app.models[resource] ? this.app.models[resource].instance : null;
let err, query = [];
Object.keys(p || {}).forEach(param => {
query.push(`${param} = $${param}`);
});
if (!resource) {
err = new Error('E_BAD_REQUEST_RESOURCE_NOT_SET');
return Promise.reject(err);
}
if (!model) {
err = new Error('E_BAD_REQUEST_RESOURCE_NOT_VALID');
return Promise.reject(err);
}
if (lodash_1.isEmpty(p) || query.length === 0) {
err = new Error('E_BAD_REQUEST_RESOURCE_QUERY_INVALID');
return Promise.reject(err);
}
return model.sequelize
.query(`SELECT exists(SELECT 1 FROM ${model.getTableName()} WHERE ${query.join(' AND ')} )`, {
bind: p,
model: model,
mapToModel: false,
transaction: options.transaction,
useMaster: options.useMaster
})
.then(([row]) => {
if (!row || row.get('exists') === false) {
return options.reject === false
? false
: Promise.reject(false);
}
else {
return true;
}
})
.catch(_err => {
this.app.log.error(this.name, '.rowExists', _err);
return Promise.reject(_err);
});
}
models(name) {
if (!this.app.spools.broadcast) {
throw new Error('Spool-broadcast is not loaded!');
}
return this.app.spools.broadcast.models(name);
}
entries(name) {
if (!this.app.spools.broadcast) {
throw new Error('Spool-broadcast is not loaded!');
}
return this.app.spools.broadcast.entries(name);
}
transaction(func, req, body, options) {
if (!this.app.spools.broadcast) {
throw new Error('Spool-broadcast is not loaded!');
}
return this.app.spools.broadcast.transaction(func, req, body, options);
}
Sequelize() {
if (!this.app.spools.sequelize) {
throw new Error('Spool-sequelize is not loaded!');
}
return this.app.spools.sequelize._datastore;
}
combine({ src = {}, tgt = {}, tgtModel, tgtOpts = {}, srcModel, srcOpts }) {
if (lodash_1.isEmpty(src) && lodash_1.isEmpty(tgt)) {
return null;
}
if (!tgtModel) {
throw new Error('Target Model is required!');
}
if (tgtModel
&& (!tgtModel.constructor && !tgtModel.constructor.name)
&& !this.app.models[tgtModel.constructor.name]
&& !tgtModel.stage) {
throw new Error('Not a valid model as combine target model!');
}
if (srcModel
&& (!srcModel.constructor && !srcModel.constructor.name)
&& !this.app.models[srcModel.constructor.name]
&& !srcModel.stage) {
throw new Error('Not a valid model as combine source model!');
}
src = src.toJSON ? src.toJSON() : src;
tgt = tgt.toJSON ? tgt.toJSON() : tgt;
return tgtModel.stage(Object.assign({}, src, tgt), tgtOpts);
}
}
exports.Entry = Entry;
//# sourceMappingURL=Entry.js.map