database-builder
Version:
Library to assist in creating and maintaining SQL commands.
145 lines (144 loc) • 7.1 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CrudBase = void 0;
var key_utils_1 = require("./../core/key-utils");
var type_crud_1 = require("./enums/type-crud");
var primary_key_type_1 = require("../core/enums/primary-key-type");
var sql_base_1 = require("./sql-base");
var rxjs_1 = require("rxjs");
var utils_1 = require("../core/utils");
var core_1 = require("../core");
var CrudBase = /** @class */ (function (_super) {
__extends(CrudBase, _super);
function CrudBase(_typeCrud, _a) {
var mapperTable = _a.mapperTable, builder = _a.builder, _b = _a.database, database = _b === void 0 ? void 0 : _b, _c = _a.enableLog, enableLog = _c === void 0 ? true : _c;
var _this = _super.call(this, { mapperTable: mapperTable, database: database, enableLog: enableLog }) || this;
_this._typeCrud = _typeCrud;
// tslint:disable-next-line: variable-name
_this.__allowInTransaction = true;
_this._builder = builder;
return _this;
}
CrudBase.prototype.dependencies = function () {
var _this = this;
/**
* retornar apenas as dependencias que foram solititadas nas colunas especificadas
*/
return this.mapperTable.dependencies.filter(function (dependency) { return _this._builder.specifiedColumns.some(function (x) { return x.name === _this.mapperTable.columns.find(function (column) { return column.tableReference === dependency.tableName; }).column; }); });
};
CrudBase.prototype.model = function () {
return this._builder.getModel();
};
CrudBase.prototype.builderCompiled = function () {
return this._builder.compile();
};
CrudBase.prototype.checkDatabaseResult = function (observable) {
var _this = this;
if (this._typeCrud === type_crud_1.TypeCrud.CREATE) {
return observable.pipe((0, rxjs_1.map)(function (results) {
var models = utils_1.Utils.isArray(_this.model())
? _this.model()
: [_this.model()];
if (_this.mainScriptLength) {
var mainScriptResults = results.slice(0, _this.mainScriptLength);
var remainingScriptResults = results.slice(_this.mainScriptLength);
results = __spreadArray([
mainScriptResults
.reduce(function (p, c) { return ({
insertId: Math.max(p.insertId, c.insertId),
rows: arrayToDatabaseRowList(databaseRowListToArray(p.rows).concat(databaseRowListToArray(c.rows))),
rowsAffected: p.rowsAffected + c.rowsAffected
}); }, { insertId: 0, rows: [], rowsAffected: 0 })
], remainingScriptResults, true);
}
var mainResult = results[0];
if (models.length > 1 && models.length != mainResult.rowsAffected) {
throw new core_1.DatabaseBuilderError("H\u00E1 ".concat(models.length, " models e ").concat(mainResult.rowsAffected, " results afetados, isso parece incoerente, e n\u00E3o \u00E9 possivel trata-lo"));
}
_this.setKeyByResult(models, mainResult);
// como não sei qual será o retorno do insertMultiple, vou continuar considerando apenas o primeiro para pegar o id inserted do head
// for (let index = models.length - 1; index >= 0; index--) {
// const model = models[index];
// }
// for (let index = 0; index < models.length; index++) {
// const result = results[index];
// const model = models[index];
// if (KeyUtils.primaryKeyType(this._builder.getMapper()) === PrimaryKeyType.AutoIncrement) {
// KeyUtils.setKey(this._builder.getMapper(), model, result.insertId);
// } else {
// const keyValue = KeyUtils.getKey(this._builder.getMapper(), model);
// try {
// result.insertId = keyValue;
// } catch (error) {
// // ignore error readonly property
// }
// }
// }
return results;
}));
}
return observable;
};
/**
* rowsAffected: 3
* id: 6
*/
CrudBase.prototype.setKeyByResult = function (models, result) {
for (var index = 0; index < models.length; index++) {
var model = models[index];
if (key_utils_1.KeyUtils.primaryKeyType(this._builder.getMapper()) === primary_key_type_1.PrimaryKeyType.AutoIncrement) {
// calcule id by result
var currentId = result.insertId - (result.rowsAffected - (index + 1));
key_utils_1.KeyUtils.setKey(this._builder.getMapper(), model, currentId);
}
else {
var keyValue = key_utils_1.KeyUtils.getKey(this._builder.getMapper(), model);
try {
result.insertId = keyValue;
}
catch (error) {
// ignore error readonly property
}
}
}
return models;
};
return CrudBase;
}(sql_base_1.SqlBase));
exports.CrudBase = CrudBase;
var arrayToDatabaseRowList = function (array) {
return {
item: function (i) { return array[i]; },
length: array.length
};
};
var databaseRowListToArray = function (value) {
var array = [];
for (var i = 0; i < value.length; i++)
array.push(value.item(i));
return array;
};