UNPKG

@tsdi/typeorm-adapter

Version:

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

152 lines (150 loc) 7.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TypeormConnectionStatupService = void 0; var tslib_1 = require("tslib"); require("reflect-metadata"); var ioc_1 = require("@tsdi/ioc"); var boot_1 = require("@tsdi/boot"); var typeorm_1 = require("typeorm"); var TypeormConnectionStatupService = /** @class */ (function (_super) { tslib_1.__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_1.__awaiter(this, void 0, void 0, function () { var logger, config, injector, loader, options; var _this = this; return tslib_1.__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_1.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_1.isString(r); }), basePath: this.ctx.baseURL })]; case 1: // preload repositories for typeorm. _b.sent(); _b.label = 2; case 2: if (!ioc_1.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_1.__awaiter(this, void 0, void 0, function () { var connection; return tslib_1.__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_1.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_1.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_1.__awaiter(this, void 0, void 0, function () { var entities_1, loader, models; return tslib_1.__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_1.isString(m); }))) return [3 /*break*/, 2]; loader = this.ctx.injector.getLoader(); return [4 /*yield*/, loader.loadTypes({ files: config.models.filter(function (m) { return ioc_1.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_1.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_1.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_1.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_1.__decorate([ ioc_1.Singleton() ], TypeormConnectionStatupService); return TypeormConnectionStatupService; }(boot_1.ConnectionStatupService)); exports.TypeormConnectionStatupService = TypeormConnectionStatupService; //# sourceMappingURL=sourcemaps/TypeormConnectionStatupService.js.map