UNPKG

@tsdi/typeorm-adapter

Version:

@tsdi/typeorm-adapter is typeorm adapter orm for boot application, mvc frameworks on server.

131 lines (129 loc) 5.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TypeOrmModelParser = exports.ObjectIDToken = void 0; var tslib_1 = require("tslib"); var boot_1 = require("@tsdi/boot"); var ioc_1 = require("@tsdi/ioc"); var typeorm_1 = require("typeorm"); exports.ObjectIDToken = ioc_1.tokenId('ObjectID'); var numbExp = /(int|float|double|dec|numeric|number)/; var intExp = /int/; var strExp = /(char|var|string|text)/; var boolExp = /(bool|bit)/; var timeExp = /(time|date)/; var arrayExp = /array/; var bytesExp = /(bytes|bytea)/; var TypeOrmModelParser = /** @class */ (function (_super) { tslib_1.__extends(TypeOrmModelParser, _super); function TypeOrmModelParser() { return _super !== null && _super.apply(this, arguments) || this; } TypeOrmModelParser.prototype.isObjectId = function (type) { return this._ObjectID && this._ObjectID === type; }; TypeOrmModelParser.prototype.setup = function () { var ObjectID = this._ObjectID; if (ObjectID) { this.getTypeMap() .register(ObjectID, function (id) { return new ObjectID(id); }); } }; TypeOrmModelParser.prototype.getPropertyMeta = function (type) { var _this = this; var metas = {}; typeorm_1.getMetadataArgsStorage().columns.filter(function (col) { return col.target === type; }) .forEach(function (col) { metas[col.propertyName] = metas[col.propertyName] || []; metas[col.propertyName].push({ propertyKey: col.propertyName, dbtype: ioc_1.isString(col.options.type) ? col.options.type : '', type: _this.getModeType(col) }); }); typeorm_1.getMetadataArgsStorage().relations.filter(function (col) { return col.target === type; }) .forEach(function (col) { metas[col.propertyName] = metas[col.propertyName] || []; var relaModel = ioc_1.isFunction(col.type) ? col.type() : undefined; metas[col.propertyName].push({ propertyKey: col.propertyName, provider: relaModel, type: (col.relationType === 'one-to-many' || col.relationType === 'many-to-many') ? Array : relaModel }); }); return metas; }; TypeOrmModelParser.prototype.isExtendBaseType = function (type, propmeta) { if (propmeta.dbtype) { if (intExp.test(propmeta.dbtype)) { return true; } } if (this.isObjectId(type)) { return true; } return _super.prototype.isExtendBaseType.call(this, type, propmeta); }; TypeOrmModelParser.prototype.resolveExtendType = function (type, value, propmeta) { if (propmeta.dbtype) { if (intExp.test(propmeta.dbtype)) { return parseInt(value); } } if (this.isObjectId(type)) { return new this._ObjectID(value); } return _super.prototype.resolveExtendType.call(this, type, propmeta); }; TypeOrmModelParser.prototype.getModeType = function (col) { var type = col.options.type; if (type) { if (ioc_1.isString(type)) { if (type === 'uuid') { return String; } else if (numbExp.test(type)) { return Number; } else if (boolExp.test(type)) { return Boolean; } else if (strExp.test(type)) { return String; } else if (timeExp.test(type)) { return Date; } else if (arrayExp.test(type)) { return Array; } else if (bytesExp.test(type)) { return Buffer; } else { return Object; } } return type; } switch (col.mode) { case 'objectId': type = this._ObjectID; break; } return type; }; TypeOrmModelParserAnn = function () { return { "name": "TypeOrmModelParser", "params": { "isObjectId": ["type"], "getPropertyMeta": ["type"], "isExtendBaseType": ["type", "propmeta"], "resolveExtendType": ["type", "value", "propmeta"], "getModeType": ["col"] } }; }; tslib_1.__decorate([ ioc_1.Inject(exports.ObjectIDToken), tslib_1.__metadata("design:type", Object) ], TypeOrmModelParser.prototype, "_ObjectID", void 0); TypeOrmModelParser = tslib_1.__decorate([ ioc_1.Singleton(boot_1.DefaultModelParserToken), ioc_1.Autorun('setup') ], TypeOrmModelParser); return TypeOrmModelParser; }(boot_1.ModelParser)); exports.TypeOrmModelParser = TypeOrmModelParser; //# sourceMappingURL=sourcemaps/TypeOrmModelParser.js.map