UNPKG

ts-db-helper

Version:
120 lines 4.46 kB
/** * @private * @class ModelResult * * @description * This class is private part of the API. * A specific wrapper to convert QueryResult to typed QueryResult on demand * * @param T @extends {@link DbHelperModel}, a model declared with table and * column annotations * * @author Olivier Margarit * @since 0.1 */ var ModelResult = /** @class */ (function () { /** * @public * @constructor this is a private API and should not be available for integrators * * @param {QueryResult<any>} result the real QueryResult converted to return typed models * @param {{new(): T}} model the target model to convert * @param {Array<string>} projection the optional projection */ function ModelResult(result, model, projection) { this.result = result; this.model = model; this.projection = projection; this.cache = new Array(result.rows.length).fill(null); } Object.defineProperty(ModelResult.prototype, "rowsAffected", { /** * @public * @property {number} rowsAffected serve real ResultQuery rowsAffected; */ get: function () { return this.result.rowsAffected; }, enumerable: true, configurable: true }); Object.defineProperty(ModelResult.prototype, "insertId", { /** * @public * @property {number} insertId serve real ResultQuery insertId; */ get: function () { return this.result.insertId; }, enumerable: true, configurable: true }); Object.defineProperty(ModelResult.prototype, "rows", { /** * @public * @property {Object} rows serve customized type ResultQuery rows; */ get: function () { var _this = this; return { /** * @public * @property {number} length serve real ResultQuery rows.length; */ length: this.result.rows.length, /** * @public * @method item get typed item from ResultQuery and keep it in cache * to avoid doing the job two times * * @param {number} i the index of the item * * @return {T} the typed item */ item: function (i) { if (!_this.cache[i]) { var entity = new _this.model(); var item = _this.result.rows.item(i); for (var key in entity.$$shadow) { if (entity.$$shadow.hasOwnProperty(key)) { var shadow = entity.$$shadow[key]; if (_this.projection && _this.projection.indexOf(key) < 0) { continue; } if (item.hasOwnProperty(key)) { shadow.val = item[key]; } } } entity.$$partialWithProjection = _this.projection; entity.$$rowid = item.hasOwnProperty('rowid') ? item.rowid : null; entity.$$inserted = true; entity.$$isModified = false; _this.cache[i] = entity; } return _this.cache[i]; }, /** * @public * @method toArray convert rows to an array of instanciated models * * @return {Array<T>} the array of models */ toArray: function () { for (var i = 0; i < _this.cache.length; i++) { if (!_this.cache[i]) { _this.rows.item(i); } } return _this.cache; } }; }, enumerable: true, configurable: true }); ; return ModelResult; }()); export { ModelResult }; //# sourceMappingURL=model-result.model.js.map