@mvx/typeorm-adapter
Version:
@mvx/typeorm-adapter is typeorm adapter orm for mvc, type-mvc is base on koa. Decorator, Ioc, AOP mvc frameworker on server.
141 lines (139 loc) • 6.12 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
require("reflect-metadata");
var typeorm_1 = require("typeorm");
var ioc_1 = require("@tsdi/ioc");
var mvc_1 = require("@mvx/mvc");
var OrmInitService = /** @class */ (function () {
function OrmInitService() {
}
OrmInitService.getClassAnnations = function () {
return { "name": "OrmInitService", "params": { "init": ["connect"] } };
};
OrmInitService = tslib_1.__decorate([
ioc_1.Abstract()
], OrmInitService);
return OrmInitService;
}());
exports.OrmInitService = OrmInitService;
var TypeOrmHelper = /** @class */ (function () {
function TypeOrmHelper(ctx) {
this.ctx = ctx;
this.hasInit = false;
}
TypeOrmHelper.prototype.getRepository = function (type) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var conn;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getConnection()];
case 1:
conn = _a.sent();
return [2 /*return*/, conn.getRepository(type)];
}
});
});
};
TypeOrmHelper.prototype.getMongoRepository = function (type) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var conn;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getConnection()];
case 1:
conn = _a.sent();
return [2 /*return*/, conn.getMongoRepository(type)];
}
});
});
};
TypeOrmHelper.prototype.getConnection = function () {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var options, connect, initService, err_1;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getOptions()];
case 1:
options = _a.sent();
if (!!this.hasInit) return [3 /*break*/, 9];
this.hasInit = true;
_a.label = 2;
case 2:
_a.trys.push([2, 8, , 9]);
return [4 /*yield*/, typeorm_1.createConnection(options)];
case 3:
connect = _a.sent();
if (!options.initDb) return [3 /*break*/, 5];
return [4 /*yield*/, options.initDb(connect)];
case 4:
_a.sent();
_a.label = 5;
case 5:
initService = this.ctx.getService(OrmInitService);
if (!(initService instanceof OrmInitService)) return [3 /*break*/, 7];
return [4 /*yield*/, initService.init(connect)];
case 6:
_a.sent();
_a.label = 7;
case 7: return [2 /*return*/, connect];
case 8:
err_1 = _a.sent();
console.error(err_1);
return [3 /*break*/, 9];
case 9: return [4 /*yield*/, typeorm_1.getConnection(options.name)];
case 10: return [2 /*return*/, _a.sent()];
}
});
});
};
TypeOrmHelper.prototype.getOptions = function () {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var config, options, entities_1, loader, models;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!!this.options) return [3 /*break*/, 5];
config = this.ctx.getConfiguration();
options = config.connections || {};
if (!!options.entities) return [3 /*break*/, 4];
entities_1 = [];
loader = this.ctx.getContainer().getLoader();
if (!config.models.some(function (m) { return ioc_1.isString(m); })) return [3 /*break*/, 2];
return [4 /*yield*/, loader.loadTypes({ files: config.models, basePath: this.ctx.getRootPath() })];
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:
this.options = options;
_a.label = 5;
case 5: return [2 /*return*/, this.options];
}
});
});
};
TypeOrmHelper.getClassAnnations = function () {
return { "name": "TypeOrmHelper", "params": { "constructor": ["ctx"], "getRepository": ["type"], "getMongoRepository": ["type"], "getConnection": [], "getOptions": [] } };
};
TypeOrmHelper = tslib_1.__decorate([
ioc_1.Singleton,
tslib_1.__param(0, ioc_1.Inject(mvc_1.MvcContextToken)),
tslib_1.__metadata("design:paramtypes", [mvc_1.MvcContext])
], TypeOrmHelper);
return TypeOrmHelper;
}());
exports.TypeOrmHelper = TypeOrmHelper;
//# sourceMappingURL=sourcemaps/TypeOrmHelper.js.map