UNPKG

@tsdi/typeorm-adapter

Version:

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

412 lines (368 loc) 17.7 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('tslib'), require('@tsdi/ioc'), require('reflect-metadata'), require('@tsdi/boot'), require('typeorm')) : typeof define === 'function' && define.amd ? define(['tslib', '@tsdi/ioc', 'reflect-metadata', '@tsdi/boot', 'typeorm'], factory) : (global = global || self, (global['typeorm-adapter'] = global['typeorm-adapter'] || {}, global['typeorm-adapter'].umd = factory(global.tslib, global.ioc, global.reflectMetadata, global.boot, global.typeorm))); }(this, (function (tslib, ioc, reflectMetadata, boot, typeorm) { 'use strict'; tslib = tslib && Object.prototype.hasOwnProperty.call(tslib, 'default') ? tslib['default'] : tslib; ioc = ioc && Object.prototype.hasOwnProperty.call(ioc, 'default') ? ioc['default'] : ioc; reflectMetadata = reflectMetadata && Object.prototype.hasOwnProperty.call(reflectMetadata, 'default') ? reflectMetadata['default'] : reflectMetadata; boot = boot && Object.prototype.hasOwnProperty.call(boot, 'default') ? boot['default'] : boot; typeorm = typeorm && Object.prototype.hasOwnProperty.call(typeorm, 'default') ? typeorm['default'] : typeorm; function unwrapExports (x) { return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x; } function createCommonjsModule(fn, module) { return module = { exports: {} }, fn(module, module.exports), module.exports; } var TypeormConnectionStatupService_1 = createCommonjsModule(function (module, exports) { Object.defineProperty(exports, "__esModule", { value: true }); exports.TypeormConnectionStatupService = void 0; var TypeormConnectionStatupService = /** @class */ (function (_super) { tslib.__extends(TypeormConnectionStatupService, _super); function TypeormConnectionStatupService() { return _super !== null && _super.apply(this, arguments) || this; } /** * configure service. * @param ctx context. */ TypeormConnectionStatupService.prototype.configureService = function (ctx) { var _a; return tslib.__awaiter(this, void 0, void 0, function () { var logger, config, injector, loader, options; var _this = this; return tslib.__generator(this, function (_b) { switch (_b.label) { case 0: this.ctx = ctx; logger = this.logger = (_a = ctx.getLogManager()) === null || _a === void 0 ? void 0 : _a.getLogger(); logger === null || logger === void 0 ? void 0 : logger.info('startup db connections'); config = this.ctx.getConfiguration(); injector = ctx.injector; if (!(config === null || config === void 0 ? void 0 : config.repositories.some(function (r) { return ioc.isString(r); }))) return [3 /*break*/, 2]; loader = this.ctx.injector.getLoader(); // preload repositories for typeorm. return [4 /*yield*/, loader.loadTypes({ files: config.repositories.filter(function (r) { return ioc.isString(r); }), basePath: this.ctx.baseURL })]; case 1: // preload repositories for typeorm. _b.sent(); _b.label = 2; case 2: if (!ioc.isArray(config.connections)) return [3 /*break*/, 4]; return [4 /*yield*/, Promise.all(config.connections.map(function (options) { return _this.statupConnection(injector, options, config); }))]; case 3: _b.sent(); return [3 /*break*/, 6]; case 4: if (!config.connections) return [3 /*break*/, 6]; options = config.connections; options.asDefault = true; return [4 /*yield*/, this.statupConnection(injector, options, config)]; case 5: _b.sent(); _b.label = 6; case 6: return [2 /*return*/]; } }); }); }; TypeormConnectionStatupService.prototype.statupConnection = function (injector, options, config) { var _a; return tslib.__awaiter(this, void 0, void 0, function () { var connection; return tslib.__generator(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, this.createConnection(options, config)]; case 1: connection = _b.sent(); if (!options.initDb) return [3 /*break*/, 3]; return [4 /*yield*/, options.initDb(connection)]; case 2: _b.sent(); _b.label = 3; case 3: (_a = typeorm.getMetadataArgsStorage().entityRepositories) === null || _a === void 0 ? void 0 : _a.forEach(function (meta) { if (options.entities.indexOf(meta.entity) >= 0) { injector.set(meta.target, function () { return typeorm.getCustomRepository(meta.target, options.name); }); } }); return [2 /*return*/]; } }); }); }; /** * create connection. * @param options connenction options. * @param config config */ TypeormConnectionStatupService.prototype.createConnection = function (options, config) { return tslib.__awaiter(this, void 0, void 0, function () { var entities_1, loader, models; return tslib.__generator(this, function (_a) { switch (_a.label) { case 0: if (!(options.asDefault && !options.entities)) return [3 /*break*/, 4]; entities_1 = []; if (!(config === null || config === void 0 ? void 0 : config.models.some(function (m) { return ioc.isString(m); }))) return [3 /*break*/, 2]; loader = this.ctx.injector.getLoader(); return [4 /*yield*/, loader.loadTypes({ files: config.models.filter(function (m) { return ioc.isString(m); }), basePath: this.ctx.baseURL })]; case 1: models = _a.sent(); models.forEach(function (ms) { ms.forEach(function (mdl) { if (mdl && entities_1.indexOf(mdl) < 0) { entities_1.push(mdl); } }); }); return [3 /*break*/, 3]; case 2: entities_1 = config.models; _a.label = 3; case 3: options.entities = entities_1; _a.label = 4; case 4: if (options.asDefault) { this.options = options; } return [4 /*yield*/, typeorm.createConnection(options)]; case 5: return [2 /*return*/, _a.sent()]; } }); }); }; /** * get connection via name. * * @param {string} [connectName] * @returns {Connection} * @memberof TyepOrmStartupService */ TypeormConnectionStatupService.prototype.getConnection = function (connectName) { var _a; return typeorm.getConnection(connectName !== null && connectName !== void 0 ? connectName : (_a = this.options) === null || _a === void 0 ? void 0 : _a.name); }; TypeormConnectionStatupService.prototype.destroying = function () { var _a, _b; (_a = this.logger) === null || _a === void 0 ? void 0 : _a.info('close db connections'); (_b = typeorm.getConnectionManager().connections) === null || _b === void 0 ? void 0 : _b.forEach(function (c) { return c === null || c === void 0 ? void 0 : c.close(); }); }; TypeormConnectionStatupService.ρAnn = function () { return { "name": "TypeormConnectionStatupService", "params": { "configureService": ["ctx"], "statupConnection": ["injector", "options", "config"], "createConnection": ["options", "config"], "getConnection": ["connectName"] } }; }; TypeormConnectionStatupService = tslib.__decorate([ ioc.Singleton() ], TypeormConnectionStatupService); return TypeormConnectionStatupService; }(boot.ConnectionStatupService)); exports.TypeormConnectionStatupService = TypeormConnectionStatupService; }); unwrapExports(TypeormConnectionStatupService_1); var TypeormConnectionStatupService_2 = TypeormConnectionStatupService_1.TypeormConnectionStatupService; var TypeOrmHelper_1 = createCommonjsModule(function (module, exports) { Object.defineProperty(exports, "__esModule", { value: true }); exports.TypeOrmHelper = void 0; var TypeOrmHelper = /** @class */ (function () { function TypeOrmHelper() { } TypeOrmHelper.prototype.getConnection = function (connectName) { if (!this.service) { this.service = this.injector.get(TypeormConnectionStatupService_1.TypeormConnectionStatupService); } return this.service.getConnection(connectName); }; TypeOrmHelper.prototype.getRepository = function (type, connectName) { return this.getConnection(connectName).getRepository(type); }; TypeOrmHelper.prototype.getCustomRepository = function (type, connectName) { return this.getConnection(connectName).getCustomRepository(type); }; TypeOrmHelper.prototype.getMongoRepository = function (type, connectName) { return this.getConnection(connectName).getMongoRepository(type); }; TypeOrmHelper.ρAnn = function () { return { "name": "TypeOrmHelper", "params": { "getConnection": ["connectName"], "getRepository": ["type", "connectName"], "getCustomRepository": ["type", "connectName"], "getMongoRepository": ["type", "connectName"] } }; }; tslib.__decorate([ ioc.Inject(ioc.INJECTOR), tslib.__metadata("design:type", Object) ], TypeOrmHelper.prototype, "injector", void 0); TypeOrmHelper = tslib.__decorate([ ioc.Singleton(), tslib.__metadata("design:paramtypes", []) ], TypeOrmHelper); return TypeOrmHelper; }()); exports.TypeOrmHelper = TypeOrmHelper; }); unwrapExports(TypeOrmHelper_1); var TypeOrmHelper_2 = TypeOrmHelper_1.TypeOrmHelper; var TypeOrmModelParser_1 = createCommonjsModule(function (module, exports) { Object.defineProperty(exports, "__esModule", { value: true }); exports.TypeOrmModelParser = exports.ObjectIDToken = void 0; exports.ObjectIDToken = ioc.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.__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.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.isString(col.options.type) ? col.options.type : '', type: _this.getModeType(col) }); }); typeorm.getMetadataArgsStorage().relations.filter(function (col) { return col.target === type; }) .forEach(function (col) { metas[col.propertyName] = metas[col.propertyName] || []; var relaModel = ioc.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.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; }; TypeOrmModelParser.ρAnn = function () { return { "name": "TypeOrmModelParser", "params": { "isObjectId": ["type"], "getPropertyMeta": ["type"], "isExtendBaseType": ["type", "propmeta"], "resolveExtendType": ["type", "value", "propmeta"], "getModeType": ["col"] } }; }; tslib.__decorate([ ioc.Inject(exports.ObjectIDToken), tslib.__metadata("design:type", Object) ], TypeOrmModelParser.prototype, "_ObjectID", void 0); TypeOrmModelParser = tslib.__decorate([ ioc.Singleton(boot.DefaultModelParserToken), ioc.Autorun('setup') ], TypeOrmModelParser); return TypeOrmModelParser; }(boot.ModelParser)); exports.TypeOrmModelParser = TypeOrmModelParser; }); unwrapExports(TypeOrmModelParser_1); var TypeOrmModelParser_2 = TypeOrmModelParser_1.TypeOrmModelParser; var TypeOrmModelParser_3 = TypeOrmModelParser_1.ObjectIDToken; var TypeOrmModule_1 = createCommonjsModule(function (module, exports) { Object.defineProperty(exports, "__esModule", { value: true }); exports.TypeOrmModule = void 0; var TypeOrmModule = /** @class */ (function () { function TypeOrmModule() { } TypeOrmModule.ρAnn = function () { return { "name": "TypeOrmModule", "params": {} }; }; TypeOrmModule = tslib.__decorate([ boot.DIModule({ regIn: 'root', imports: [ boot.ORMCoreModule ], providers: [ TypeormConnectionStatupService_1.TypeormConnectionStatupService, TypeOrmHelper_1.TypeOrmHelper, TypeOrmModelParser_1.TypeOrmModelParser ] }) ], TypeOrmModule); return TypeOrmModule; }()); exports.TypeOrmModule = TypeOrmModule; }); unwrapExports(TypeOrmModule_1); var TypeOrmModule_2 = TypeOrmModule_1.TypeOrmModule; var src = createCommonjsModule(function (module, exports) { Object.defineProperty(exports, "__esModule", { value: true }); tslib.__exportStar(TypeOrmHelper_1, exports); tslib.__exportStar(TypeOrmModelParser_1, exports); tslib.__exportStar(TypeOrmModule_1, exports); tslib.__exportStar(TypeormConnectionStatupService_1, exports); }); var index = unwrapExports(src); return index; }))); //# sourceMappingURL=typeorm-adapter.umd.js.map