@joktec/mysql
Version:
JokTec - MySql Service
95 lines • 4.41 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MysqlService = void 0;
const core_1 = require("@joktec/core");
const lodash_1 = require("lodash");
const typeorm_1 = require("typeorm");
const mysql_client_1 = require("./mysql.client");
const mysql_config_1 = require("./mysql.config");
const services_1 = require("./services");
const RETRY_OPTS = 'mysql.retry';
let MysqlService = class MysqlService extends core_1.AbstractClientService {
constructor(modelRegistry, subscriberRegistry) {
super('mysql', mysql_config_1.MysqlConfig);
this.modelRegistry = modelRegistry;
this.subscriberRegistry = subscriberRegistry;
}
async init(config) {
const connection = (0, lodash_1.pick)(config, ['host', 'port', 'username', 'password', 'database']);
const options = {
...config,
type: config.dialect,
namingStrategy: new services_1.MysqlNamingStrategy(),
entities: [...this.modelRegistry[config.conId]],
subscribers: [...this.subscriberRegistry[config.conId]],
logger: new services_1.MysqlBenchmark(config.benchmark, this.logService),
dropSchema: false,
};
if (config.slaves?.length) {
options['replication'] = { master: { ...connection }, slaves: [...config.slaves] };
}
const AppDataSource = new typeorm_1.DataSource(options);
this.logService.info('`%s` Connection to MySQL established on host %s', config.conId, config.host);
return AppDataSource;
}
async start(client, conId = core_1.DEFAULT_CON_ID) {
const config = this.getConfig(conId);
try {
await client.initialize();
this.logService.info('`%s` Connected to MySQL successfully', conId);
if (this.modelRegistry[conId]) {
if (config.sync) {
await client.synchronize(false);
this.logService.info('`%s` Sync MySQL schema successfully', conId);
}
}
}
catch (err) {
this.logService.error(err, '`%s` Error when connecting to MySQL', conId);
}
}
async stop(client, conId = core_1.DEFAULT_CON_ID) {
try {
await client.destroy();
this.logService.warn('`%s` Close connection to MySQL successfully', conId);
}
catch (err) {
this.logService.error(err, '`%s` Error when close connection to MySQL', conId);
}
}
getDataSource(conId = core_1.DEFAULT_CON_ID) {
return this.getClient(conId);
}
getEntityManager(conId = core_1.DEFAULT_CON_ID) {
return this.getClient(conId).createEntityManager();
}
getRepository(entityClass, conId = core_1.DEFAULT_CON_ID) {
return this.getClient(conId).getRepository(entityClass);
}
};
exports.MysqlService = MysqlService;
__decorate([
(0, core_1.Retry)(RETRY_OPTS),
__metadata("design:type", Function),
__metadata("design:paramtypes", [mysql_config_1.MysqlConfig]),
__metadata("design:returntype", Promise)
], MysqlService.prototype, "init", null);
exports.MysqlService = MysqlService = __decorate([
(0, core_1.Injectable)(),
__param(0, (0, core_1.Inject)(mysql_client_1.MODEL_REGISTRY_KEY)),
__param(1, (0, core_1.Inject)(mysql_client_1.SUBSCRIBER_REGISTRY_KEY)),
__metadata("design:paramtypes", [Object, Object])
], MysqlService);
//# sourceMappingURL=mysql.service.js.map