database-builder
Version:
Library to assist in creating and maintaining SQL commands.
92 lines (91 loc) • 4.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Crud = void 0;
var query_1 = require("./query/query");
var insert_1 = require("./insert/insert");
var update_1 = require("./update/update");
var delete_1 = require("./delete/delete");
var errors_1 = require("../core/errors");
var utils_1 = require("../core/utils");
var key_utils_1 = require("../core/key-utils");
var mapper_utils_1 = require("../mapper/mapper-utils");
var Crud = /** @class */ (function () {
function Crud(_config, _a) {
var _b = _a === void 0 ? {} : _a, getMapper = _b.getMapper, database = _b.database, _c = _b.enableLog, enableLog = _c === void 0 ? true : _c;
this._config = _config;
this._getMapper = getMapper;
this._database = database;
this.enableLog = enableLog;
}
Crud.prototype.delete = function (typeT, _a) {
var _b = _a === void 0 ? {} : _a, _c = _b.modelToSave, modelToSave = _c === void 0 ? void 0 : _c, _d = _b.metadata, metadata = _d === void 0 ? this.getMapper(typeT) : _d, _e = _b.database, database = _e === void 0 ? this.getDatabase() : _e;
return new delete_1.Delete(typeT, {
modelToSave: modelToSave,
mapperTable: metadata.mapperTable,
database: database,
enableLog: this.enableLog, config: this._config
});
};
Crud.prototype.deleteByKey = function (typeT, key, _a) {
var _b = _a === void 0 ? {} : _a, _c = _b.metadata, metadata = _c === void 0 ? this.getMapper(typeT) : _c, _d = _b.database, database = _d === void 0 ? this.getDatabase() : _d;
var obj = {};
key_utils_1.KeyUtils.setKey(metadata.mapperTable, obj, key);
return new delete_1.Delete(typeT, {
modelToSave: obj, mapperTable: metadata.mapperTable,
database: database,
enableLog: this.enableLog, config: this._config
});
};
Crud.prototype.update = function (typeT, _a) {
var _b = _a === void 0 ? {} : _a, toSave = _b.toSave, alias = _b.alias, _c = _b.metadata, metadata = _c === void 0 ? this.getMapper(typeT) : _c, _d = _b.database, database = _d === void 0 ? this.getDatabase() : _d;
return new update_1.Update(typeT, {
toSave: toSave, mapperTable: metadata.mapperTable,
alias: alias,
database: database,
enableLog: this.enableLog, config: this._config
});
};
Crud.prototype.insert = function (typeT, _a) {
var _b = _a === void 0 ? {} : _a, toSave = _b.toSave, alias = _b.alias, _c = _b.metadata, metadata = _c === void 0 ? this.getMapper(typeT) : _c, _d = _b.database, database = _d === void 0 ? this.getDatabase() : _d;
return new insert_1.Insert(typeT, {
toSave: toSave, mapperTable: metadata.mapperTable,
alias: alias,
database: database,
enableLog: this.enableLog, config: this._config
});
};
Crud.prototype.query = function (typeT, _a) {
var _this = this;
var _b = _a === void 0 ? {} : _a, alias = _b.alias, metadata = _b.metadata, _c = _b.database, database = _c === void 0 ? this.getDatabase() : _c;
if (typeT && typeT._builder) {
typeT = typeT._builder();
}
if (utils_1.Utils.isNull(metadata)) {
metadata = utils_1.Utils.getMapperTable(typeT, function (tKey) {
return _this._getMapper.get(tKey);
});
}
var that = this;
return new query_1.Query(typeT, {
alias: alias,
getMapper: function (tKey) {
return that.getMapper(tKey);
},
mapperTable: metadata.mapperTable,
database: database,
enableLog: this.enableLog
});
};
Crud.prototype.getDatabase = function () {
return this._database;
};
Crud.prototype.getMapper = function (tKey) {
var metadata = this._getMapper.get(tKey);
if (utils_1.Utils.isNull(metadata)) {
throw new errors_1.DatabaseBuilderError("Mapper for \"".concat(mapper_utils_1.MapperUtils.resolveKey(tKey), "\" not found!\""));
}
return metadata;
};
return Crud;
}());
exports.Crud = Crud;