mission.core
Version:
mission core
129 lines (128 loc) • 5.01 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const util_1 = require("../util");
const bo_registry_decorator_1 = require("./bo-registry.decorator");
const repository_1 = require("./repository");
class BaseBo {
constructor(request) {
this.request = request;
this.dal = repository_1.Repository.Instance.Dal;
this.log = repository_1.Repository.Instance.Logger;
}
get models() {
return repository_1.Repository.Instance.Models;
}
get items() {
return this.getModel();
}
deleteById(entity, scope) {
return __awaiter(this, void 0, void 0, function* () {
this.checkId(entity);
return yield this.getScope(scope).destroy({ where: { id: entity.id }, limit: 1 });
});
}
executeSQLQuery(queryText, param) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.dal.query(queryText, param);
});
}
executeStoredProcedure(procName, param) {
return __awaiter(this, void 0, void 0, function* () {
return this.dal.query('CALL ' + procName, param);
});
}
getById(id, scope) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.getScope(scope).findByPk(id);
});
}
findOne(options, scope) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.getScope(scope).findOne(options);
});
}
findAll(options, scope) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.getScope(scope).findAll(options);
});
}
findAndCount(options, scope) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this.getScope(scope).findAndCountAll(options);
if (this.request && this.request.body) {
this.request.body.pageContext = this.request.body.pageContext || {};
this.request.body.pageContext.totalRecords = result.count;
}
return result;
});
}
save(entity, options, scope) {
return __awaiter(this, void 0, void 0, function* () {
const model = this.getScope(scope);
options = options || { isNewRecord: true };
options.transaction = options.transaction || this.transaction;
const result = yield model.create(entity, options);
return result;
});
}
// tslint:disable-next-line: max-line-length
saveOrUpdate(entity, options, scope) {
return __awaiter(this, void 0, void 0, function* () {
const model = this.getScope(scope);
options.transaction = options.transaction || this.transaction;
return yield model.upsert(entity, options);
});
}
update(entity, options, scope) {
return __awaiter(this, void 0, void 0, function* () {
this.checkId(entity);
options = options || { where: { id: entity.id }, limit: 1 };
options.transaction = options.transaction || this.transaction;
const res = yield this.getScope(scope).update(entity, options);
return res;
});
}
getScope(scope) {
let mdl = this.items;
if (scope === null) {
mdl = this.items.scope(null);
}
else if (scope) {
mdl = typeof scope === 'string' || scope instanceof Array
? this.items.scope(scope)
: scope;
}
return mdl;
}
getQoBuilder(reqBody) {
reqBody = reqBody ? reqBody : this.request ? this.request.body : null;
return new util_1.QueryOptionBuilder(reqBody);
}
getResponse(data, pageContext, error) {
return { data, error, pageContext };
}
get queryGenerator() {
return this.dal.getQueryInterface().QueryGenerator;
}
getBo(type, req) {
return bo_registry_decorator_1.BoFactory.getBo(type, this.request);
}
checkId(entity) {
if (!entity || entity.id <= 0) {
throw new Error('Invalid enityt or Id. Operation Faild.');
}
}
get transaction() {
return this.request ? this.request.transaction : null;
}
}
exports.BaseBo = BaseBo;