UNPKG

dazjsx

Version:

参考nestjs,基于KOA2的一款轻量级的后端开发框架

54 lines (53 loc) 1.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TypeOrmFactory = void 0; const __1 = require(".."); const typeorm_1 = require("typeorm"); const util_1 = require("../util"); const service_1 = require("../service"); class TypeOrmFactory { static isNullOption(options) { const { type, host, port, database, username, password } = options || {}; return type && host && port && database && username && password; } static setOrm(orm) { if (util_1.Tool.isNotEmptyObject(orm)) { if (util_1.Tool.isIoc(orm)) { const { inject, useFactory } = orm; const arg = inject.map((r) => new r()); this.options = useFactory(...arg); } this.options = orm; } } static set options(options) { this.ormOptions = options; } static get options() { return this.ormOptions; } static async createTypeOrmConnect(entities, cb) { if (!this.isNullOption(this.options)) { return cb && cb(null); } this.options = util_1.R.defineData(this.options, 'entities', entities); let con = null; try { con = await (0, typeorm_1.createConnection)(this.options); service_1.LogService.info(__1.CONNECTION_MESSAGE, this.options.type); } catch (error) { service_1.LogService.info('TypeOrm 连接数据库', this.options.type, '失败:', error.message); if (--this.reTry > 0) { await util_1.Tool.waiting(1); return this.createTypeOrmConnect(entities, cb); } else { return process.exit(0); } } return cb && cb(con); } } exports.TypeOrmFactory = TypeOrmFactory; TypeOrmFactory.reTry = 10;