UNPKG

@dazejs/framework

Version:

Daze.js - A powerful web framework for Node.js

153 lines 5.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Model = void 0; const date_fns_1 = require("date-fns"); const relations_1 = require("./relations"); const repository_1 = require("./repository"); class Model { constructor(Entity) { var _a, _b, _c, _d, _e; this._originEntity = Entity; this._table = Reflect.getMetadata('table', Entity); this._connection = (_a = Reflect.getMetadata('connection', Entity)) !== null && _a !== void 0 ? _a : 'default'; this._columns = (_b = Reflect.getMetadata('columns', Entity)) !== null && _b !== void 0 ? _b : new Map(); this._customColumns = (_c = Reflect.getMetadata('customColumns', Entity)) !== null && _c !== void 0 ? _c : []; this._primaryKey = (_d = Reflect.getMetadata('primaryKey', Entity)) !== null && _d !== void 0 ? _d : 'id'; this._incrementing = (_e = Reflect.getMetadata('incrementing', Entity)) !== null && _e !== void 0 ? _e : true; this._softDeleteKey = Reflect.getMetadata('softDeleteKey', Entity); this._createTimestampKey = Reflect.getMetadata('createTimestampKey', Entity); this._updateTimestampKey = Reflect.getMetadata('updateTimestampKey', Entity); this._relationMap = Reflect.getMetadata('relations', Entity) || new Map(); } createNewEntity() { return new this._originEntity(); } getOriginEntity() { return this._originEntity; } getTable() { return this._table; } setTable(table) { this._table = table; return this; } getConnectionName() { var _a; return (_a = this._connection) !== null && _a !== void 0 ? _a : 'default'; } getPrimaryKey() { var _a; return (_a = this._primaryKey) !== null && _a !== void 0 ? _a : 'id'; } getColumns() { return this._columns; } getCustomColumns() { return this._customColumns; } isIncrementing() { return !!this._incrementing; } getSoftDeleteKey() { return this._softDeleteKey; } getSoftDeleteDefaultValue() { var _a, _b; if (!this._softDeleteKey) return null; return (_b = (_a = this._columns.get(this._softDeleteKey)) === null || _a === void 0 ? void 0 : _a.defaultValue) !== null && _b !== void 0 ? _b : null; } isForceDelete() { return !this._softDeleteKey; } hasUpdateTimestamp() { return !!this._updateTimestampKey; } getUpdateTimestampKey() { return this._updateTimestampKey; } getFreshDateWithColumnKey(key) { const type = this.getColumnType(key); return this.getFormatedDate(type); } hasCreateTimestamp() { return !!this._createTimestampKey; } getCreateTimestampKey() { return this._createTimestampKey; } createRepository() { const repos = new repository_1.Repository(this); return repos; } getColumnType(key) { var _a; return (_a = this._columns.get(key)) === null || _a === void 0 ? void 0 : _a.type; } getRelationMap() { return this._relationMap; } getFormatedDate(type = 'int') { switch (type.toLowerCase()) { case 'date': return (0, date_fns_1.format)(new Date(), 'yyyy-MM-dd'); case 'time': return (0, date_fns_1.format)(new Date(), 'HH:MM:SS'); case 'year': return (0, date_fns_1.format)(new Date(), 'yyyy'); case 'datetime': return (0, date_fns_1.format)(new Date(), 'yyyy-MM-dd HH:mm:ss'); case 'timestamp': return (0, date_fns_1.format)(new Date(), 'yyyy-MM-dd HH:mm:ss'); case 'bigint': return Date.now(); case 'int': default: return (0, date_fns_1.getUnixTime)(new Date()); } } getRelationImp(relation) { const relationDesc = this.getRelationMap().get(relation.split('.')[0]); if (!relationDesc) return; if (relationDesc) { const RelationEntity = relationDesc.entityFn(); const model = new Model(RelationEntity); switch (relationDesc.type) { case 'hasOne': return new relations_1.HasOne(this, model, relationDesc.foreignKey, relationDesc.localKey); case 'belongsTo': return new relations_1.BelongsTo(this, model, relationDesc.foreignKey, relationDesc.localKey); case 'hasMany': return new relations_1.HasMany(this, model, relationDesc.foreignKey, relationDesc.localKey); case 'belongsToMany': return new relations_1.BelongsToMany(this, model, relationDesc.pivot, relationDesc.foreignPivotKey, relationDesc.relatedPivotKey); default: return; } } return; } async resultToRepository(parentRepos, data, isFromCollection = false) { const repos = this.createRepository() .setExists(true) .fill(data); if (!isFromCollection && parentRepos.getWiths().size > 0) { await repos.eagerly(parentRepos.getWiths(), repos); } return repos; } async resultToRepositories(parentRepos, results) { const data = []; for (const item of results) { data.push(await this.resultToRepository(parentRepos, item, true)); } if (parentRepos.getWiths().size > 0) { await parentRepos.eagerlyCollection(parentRepos.getWiths(), data); } return data; } } exports.Model = Model; //# sourceMappingURL=model.js.map