@joktec/mysql 0.0.173 → 0.0.175
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/helpers/mysql.helper.d.ts +8 -4
- package/dist/helpers/mysql.helper.d.ts.map +1 -1
- package/dist/helpers/mysql.helper.js +82 -37
- package/dist/helpers/mysql.helper.js.map +1 -1
- package/dist/index.d.ts +1 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -4
- package/dist/index.js.map +1 -1
- package/dist/models/index.d.ts +1 -0
- package/dist/models/index.d.ts.map +1 -1
- package/dist/models/index.js +1 -0
- package/dist/models/index.js.map +1 -1
- package/dist/models/mysql.model.d.ts +6 -3
- package/dist/models/mysql.model.d.ts.map +1 -1
- package/dist/models/mysql.model.js +5 -5
- package/dist/models/mysql.model.js.map +1 -1
- package/dist/models/mysql.option.d.ts +4 -0
- package/dist/models/mysql.option.d.ts.map +1 -0
- package/dist/models/mysql.option.js +3 -0
- package/dist/models/mysql.option.js.map +1 -0
- package/dist/models/mysql.request.d.ts +2 -1
- package/dist/models/mysql.request.d.ts.map +1 -1
- package/dist/mysql.client.d.ts +11 -9
- package/dist/mysql.client.d.ts.map +1 -1
- package/dist/mysql.client.js.map +1 -1
- package/dist/mysql.config.d.ts +16 -6
- package/dist/mysql.config.d.ts.map +1 -1
- package/dist/mysql.config.js +15 -4
- package/dist/mysql.config.js.map +1 -1
- package/dist/mysql.exception.d.ts.map +1 -1
- package/dist/mysql.exception.js +4 -11
- package/dist/mysql.exception.js.map +1 -1
- package/dist/mysql.repo.d.ts +21 -19
- package/dist/mysql.repo.d.ts.map +1 -1
- package/dist/mysql.repo.js +99 -75
- package/dist/mysql.repo.js.map +1 -1
- package/dist/mysql.service.d.ts +8 -8
- package/dist/mysql.service.d.ts.map +1 -1
- package/dist/mysql.service.js +14 -23
- package/dist/mysql.service.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +6 -5
package/dist/mysql.repo.js
CHANGED
|
@@ -11,7 +11,7 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.MysqlRepo = void 0;
|
|
13
13
|
const core_1 = require("@joktec/core");
|
|
14
|
-
const
|
|
14
|
+
const lodash_1 = require("lodash");
|
|
15
15
|
const helpers_1 = require("./helpers");
|
|
16
16
|
const mysql_exception_1 = require("./mysql.exception");
|
|
17
17
|
const mysql_service_1 = require("./mysql.service");
|
|
@@ -24,85 +24,109 @@ let MysqlRepo = class MysqlRepo {
|
|
|
24
24
|
async onModuleInit() {
|
|
25
25
|
this.logService.setContext(this.constructor.name);
|
|
26
26
|
}
|
|
27
|
-
onApplicationBootstrap() {
|
|
28
|
-
this.model = this.mysqlService.getModel(this.model, this.conId);
|
|
29
|
-
}
|
|
27
|
+
onApplicationBootstrap() { }
|
|
30
28
|
get repository() {
|
|
31
29
|
return this.mysqlService.getRepository(this.model, this.conId);
|
|
32
30
|
}
|
|
33
|
-
|
|
31
|
+
transform(docs) {
|
|
32
|
+
if ((0, lodash_1.isNil)(docs))
|
|
33
|
+
return null;
|
|
34
|
+
if ((0, lodash_1.isArray)(docs) && !docs.length)
|
|
35
|
+
return [];
|
|
36
|
+
const transformDocs = (0, core_1.plainToInstance)(this.model, (0, core_1.toArray)(docs));
|
|
37
|
+
return ((0, lodash_1.isArray)(docs) ? transformDocs : transformDocs[0]);
|
|
38
|
+
}
|
|
39
|
+
qb(query = {}, opts = {}) {
|
|
34
40
|
const options = helpers_1.MysqlHelper.parseFilter(query);
|
|
35
41
|
if (query.select)
|
|
36
|
-
options.
|
|
42
|
+
options.select = helpers_1.MysqlHelper.parseProjection(query.select);
|
|
37
43
|
if (query.sort)
|
|
38
|
-
options.order =
|
|
44
|
+
options.order = helpers_1.MysqlHelper.parseOrder(query.sort);
|
|
39
45
|
if (query.limit)
|
|
40
|
-
options.
|
|
46
|
+
options.take = query.limit;
|
|
41
47
|
if (query.limit && query.page) {
|
|
42
|
-
options.
|
|
43
|
-
options.
|
|
48
|
+
options.take = query.limit;
|
|
49
|
+
options.skip = (query.page - 1) * query.limit;
|
|
44
50
|
}
|
|
45
|
-
|
|
51
|
+
if (query.populate)
|
|
52
|
+
options.relations = helpers_1.MysqlHelper.parseRelations(query.populate);
|
|
53
|
+
return { ...opts, ...options };
|
|
46
54
|
}
|
|
47
|
-
|
|
48
|
-
const
|
|
49
|
-
return
|
|
55
|
+
whereById(pkValue) {
|
|
56
|
+
const primaryColumns = this.repository.metadata.primaryColumns.map(pk => pk.propertyName);
|
|
57
|
+
return primaryColumns.reduce((curr, acc) => {
|
|
58
|
+
curr[acc] = pkValue;
|
|
59
|
+
return curr;
|
|
60
|
+
}, {});
|
|
50
61
|
}
|
|
51
|
-
async paginate(query) {
|
|
52
|
-
const
|
|
62
|
+
async paginate(query, opts = {}) {
|
|
63
|
+
const findQuery = { ...query };
|
|
64
|
+
const countQuery = (0, lodash_1.omit)(query, ['select', 'page', 'limit', 'offset', 'sort']);
|
|
65
|
+
const [items, total] = await Promise.all([this.find(findQuery, opts), this.count(countQuery, opts)]);
|
|
53
66
|
return { items, total };
|
|
54
67
|
}
|
|
55
|
-
async
|
|
56
|
-
const options =
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
return this.model.findOne(options);
|
|
68
|
+
async find(query, opts = {}) {
|
|
69
|
+
const options = this.qb(query, opts);
|
|
70
|
+
const docs = await this.repository.find(options);
|
|
71
|
+
return this.transform(docs);
|
|
60
72
|
}
|
|
61
|
-
async
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
if (query.select)
|
|
65
|
-
options.attributes = (0, core_1.toArray)(query.select, { split: ',' });
|
|
66
|
-
return this.model.findOne(options);
|
|
73
|
+
async count(query, opts = {}) {
|
|
74
|
+
const options = this.qb(query, opts);
|
|
75
|
+
return this.repository.count(options);
|
|
67
76
|
}
|
|
68
|
-
async
|
|
69
|
-
|
|
77
|
+
async findOne(cond, query = {}, opts = {}) {
|
|
78
|
+
const condition = {};
|
|
79
|
+
if (!(0, lodash_1.isObject)(cond))
|
|
80
|
+
Object.assign(condition, { ...this.whereById(cond) });
|
|
81
|
+
else
|
|
82
|
+
Object.assign(condition, cond);
|
|
83
|
+
const mergeQuery = Object.assign({}, query, { condition });
|
|
84
|
+
const options = this.qb(mergeQuery, opts);
|
|
85
|
+
const doc = await this.repository.findOne(options);
|
|
86
|
+
return this.transform(doc);
|
|
70
87
|
}
|
|
71
|
-
async
|
|
72
|
-
const
|
|
73
|
-
const
|
|
74
|
-
|
|
88
|
+
async create(body, opts = {}) {
|
|
89
|
+
const transformBody = this.transform(body);
|
|
90
|
+
const entity = this.repository.create(transformBody);
|
|
91
|
+
return this.repository.save(entity, opts);
|
|
92
|
+
}
|
|
93
|
+
async update(cond, body, options = {}) {
|
|
94
|
+
const condition = {};
|
|
95
|
+
if (!(0, lodash_1.isObject)(cond))
|
|
96
|
+
Object.assign(condition, { ...this.whereById(cond) });
|
|
97
|
+
else
|
|
98
|
+
Object.assign(condition, cond);
|
|
99
|
+
const entity = await this.findOne(condition, options);
|
|
100
|
+
if (!entity)
|
|
75
101
|
return null;
|
|
76
|
-
const
|
|
77
|
-
|
|
102
|
+
const transformBody = this.transform({ ...entity, ...body });
|
|
103
|
+
const doc = await this.repository.save(transformBody, options);
|
|
104
|
+
return this.transform(doc);
|
|
78
105
|
}
|
|
79
|
-
async delete(
|
|
80
|
-
const
|
|
81
|
-
if (!
|
|
106
|
+
async delete(cond, opts = {}) {
|
|
107
|
+
const entity = await this.findOne(cond, opts);
|
|
108
|
+
if (!entity)
|
|
82
109
|
return null;
|
|
83
|
-
const
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
await this.model.restore(options);
|
|
91
|
-
if (!opts?.userId) {
|
|
92
|
-
return this.findOne({ condition });
|
|
93
|
-
}
|
|
94
|
-
return this.update(condition, { updatedAt: opts?.userId });
|
|
95
|
-
}
|
|
96
|
-
async upsert(body, onConflicts) {
|
|
97
|
-
const fields = Object.keys(body);
|
|
98
|
-
const [row, result] = await this.model.upsert(body, {
|
|
99
|
-
returning: true,
|
|
100
|
-
fields,
|
|
101
|
-
conflictFields: onConflicts,
|
|
102
|
-
});
|
|
103
|
-
if (!row || !result)
|
|
110
|
+
const func = opts?.force ? this.repository.remove : this.repository.softRemove;
|
|
111
|
+
const doc = await func(entity);
|
|
112
|
+
return this.transform(doc);
|
|
113
|
+
}
|
|
114
|
+
async restore(cond, opts = {}) {
|
|
115
|
+
const entity = await this.findOne(cond, opts);
|
|
116
|
+
if (!entity)
|
|
104
117
|
return null;
|
|
105
|
-
|
|
118
|
+
const doc = await this.repository.recover(entity);
|
|
119
|
+
return this.transform(doc);
|
|
120
|
+
}
|
|
121
|
+
async upsert(body, onConflicts, opts = {}) {
|
|
122
|
+
const transformBody = this.repository.create(body);
|
|
123
|
+
const result = await this.repository.upsert(transformBody, { ...opts, conflictPaths: onConflicts });
|
|
124
|
+
return this.transform(result.generatedMaps[0]);
|
|
125
|
+
}
|
|
126
|
+
async bulkUpsert(body, onConflicts, opts = {}) {
|
|
127
|
+
const transformBody = this.repository.create(body);
|
|
128
|
+
const result = await this.repository.upsert(transformBody, { ...opts, conflictPaths: onConflicts });
|
|
129
|
+
return this.transform(result.generatedMaps);
|
|
106
130
|
}
|
|
107
131
|
};
|
|
108
132
|
exports.MysqlRepo = MysqlRepo;
|
|
@@ -117,25 +141,25 @@ __decorate([
|
|
|
117
141
|
__decorate([
|
|
118
142
|
mysql_exception_1.MysqlCatch,
|
|
119
143
|
__metadata("design:type", Function),
|
|
120
|
-
__metadata("design:paramtypes", [Object]),
|
|
144
|
+
__metadata("design:paramtypes", [Object, Object]),
|
|
121
145
|
__metadata("design:returntype", Promise)
|
|
122
|
-
], MysqlRepo.prototype, "
|
|
146
|
+
], MysqlRepo.prototype, "paginate", null);
|
|
123
147
|
__decorate([
|
|
124
148
|
mysql_exception_1.MysqlCatch,
|
|
125
149
|
__metadata("design:type", Function),
|
|
126
|
-
__metadata("design:paramtypes", [Object]),
|
|
150
|
+
__metadata("design:paramtypes", [Object, Object]),
|
|
127
151
|
__metadata("design:returntype", Promise)
|
|
128
|
-
], MysqlRepo.prototype, "
|
|
152
|
+
], MysqlRepo.prototype, "find", null);
|
|
129
153
|
__decorate([
|
|
130
154
|
mysql_exception_1.MysqlCatch,
|
|
131
155
|
__metadata("design:type", Function),
|
|
132
|
-
__metadata("design:paramtypes", [Object]),
|
|
156
|
+
__metadata("design:paramtypes", [Object, Object]),
|
|
133
157
|
__metadata("design:returntype", Promise)
|
|
134
|
-
], MysqlRepo.prototype, "
|
|
158
|
+
], MysqlRepo.prototype, "count", null);
|
|
135
159
|
__decorate([
|
|
136
160
|
mysql_exception_1.MysqlCatch,
|
|
137
161
|
__metadata("design:type", Function),
|
|
138
|
-
__metadata("design:paramtypes", [Object]),
|
|
162
|
+
__metadata("design:paramtypes", [Object, Object, Object]),
|
|
139
163
|
__metadata("design:returntype", Promise)
|
|
140
164
|
], MysqlRepo.prototype, "findOne", null);
|
|
141
165
|
__decorate([
|
|
@@ -143,17 +167,11 @@ __decorate([
|
|
|
143
167
|
__metadata("design:type", Function),
|
|
144
168
|
__metadata("design:paramtypes", [Object, Object]),
|
|
145
169
|
__metadata("design:returntype", Promise)
|
|
146
|
-
], MysqlRepo.prototype, "findById", null);
|
|
147
|
-
__decorate([
|
|
148
|
-
mysql_exception_1.MysqlCatch,
|
|
149
|
-
__metadata("design:type", Function),
|
|
150
|
-
__metadata("design:paramtypes", [sequelize_typescript_1.Model]),
|
|
151
|
-
__metadata("design:returntype", Promise)
|
|
152
170
|
], MysqlRepo.prototype, "create", null);
|
|
153
171
|
__decorate([
|
|
154
172
|
mysql_exception_1.MysqlCatch,
|
|
155
173
|
__metadata("design:type", Function),
|
|
156
|
-
__metadata("design:paramtypes", [Object, Object]),
|
|
174
|
+
__metadata("design:paramtypes", [Object, Object, Object]),
|
|
157
175
|
__metadata("design:returntype", Promise)
|
|
158
176
|
], MysqlRepo.prototype, "update", null);
|
|
159
177
|
__decorate([
|
|
@@ -171,9 +189,15 @@ __decorate([
|
|
|
171
189
|
__decorate([
|
|
172
190
|
mysql_exception_1.MysqlCatch,
|
|
173
191
|
__metadata("design:type", Function),
|
|
174
|
-
__metadata("design:paramtypes", [Object, Array]),
|
|
192
|
+
__metadata("design:paramtypes", [Object, Array, Object]),
|
|
175
193
|
__metadata("design:returntype", Promise)
|
|
176
194
|
], MysqlRepo.prototype, "upsert", null);
|
|
195
|
+
__decorate([
|
|
196
|
+
mysql_exception_1.MysqlCatch,
|
|
197
|
+
__metadata("design:type", Function),
|
|
198
|
+
__metadata("design:paramtypes", [Array, Array, Object]),
|
|
199
|
+
__metadata("design:returntype", Promise)
|
|
200
|
+
], MysqlRepo.prototype, "bulkUpsert", null);
|
|
177
201
|
exports.MysqlRepo = MysqlRepo = __decorate([
|
|
178
202
|
(0, core_1.Injectable)(),
|
|
179
203
|
__metadata("design:paramtypes", [mysql_service_1.MysqlService, Object, String])
|
package/dist/mysql.repo.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mysql.repo.js","sourceRoot":"","sources":["../src/mysql.repo.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"mysql.repo.js","sourceRoot":"","sources":["../src/mysql.repo.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,uCAasB;AACtB,mCAAwD;AAGxD,uCAAwC;AAGxC,uDAA+C;AAC/C,mDAA+C;AAGxC,IAAe,SAAS,GAAxB,MAAe,SAAS;IAM7B,YACY,YAA0B,EAC1B,KAAqB,EACrB,QAAgB,qBAAc;QAF9B,iBAAY,GAAZ,YAAY,CAAc;QAC1B,UAAK,GAAL,KAAK,CAAgB;QACrB,UAAK,GAAL,KAAK,CAAyB;IACvC,CAAC;IAEJ,KAAK,CAAC,YAAY;QAChB,IAAI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O,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,aAAa,CAAQ,CAAC;IACrD,CAAC;CACF,CAAA;AApJqB,8BAAS;AAGT;IAAnB,IAAA,aAAM,GAAE;8BAA0B,oBAAa;gDAAC;AAC7B;IAAnB,IAAA,aAAM,GAAE;8BAAuB,iBAAU;6CAAC;AAiDrC;IADL,4BAAU;;;;yCAMV;AAGK;IADL,4BAAU;;;;qCAKV;AAGK;IADL,4BAAU;;;;sCAIV;AAGK;IADL,4BAAU;;;;wCAcV;AAGK;IADL,4BAAU;;;;uCAKV;AAGK;IADL,4BAAU;;;;uCAYV;AAGK;IADL,4BAAU;;;;uCAOV;AAGK;IADL,4BAAU;;;;wCAMV;AAGK;IADL,4BAAU;;;;uCASV;AAGK;IADL,4BAAU;;;;2CASV;oBAnJmB,SAAS;IAD9B,IAAA,iBAAU,GAAE;qCAQe,4BAAY;GAPlB,SAAS,CAoJ9B"}
|
package/dist/mysql.service.d.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import { AbstractClientService } from '@joktec/core';
|
|
2
|
-
import {
|
|
1
|
+
import { AbstractClientService, Constructor } from '@joktec/core';
|
|
2
|
+
import { DataSource, Repository } from 'typeorm';
|
|
3
|
+
import { MysqlModel } from './models';
|
|
3
4
|
import { MysqlClient, MysqlModelRegistry } from './mysql.client';
|
|
4
5
|
import { MysqlConfig } from './mysql.config';
|
|
5
|
-
export declare class MysqlService extends AbstractClientService<MysqlConfig,
|
|
6
|
+
export declare class MysqlService extends AbstractClientService<MysqlConfig, DataSource> implements MysqlClient {
|
|
6
7
|
private modelRegistry;
|
|
7
8
|
constructor(modelRegistry: MysqlModelRegistry);
|
|
8
|
-
protected init(config: MysqlConfig): Promise<
|
|
9
|
-
start(client:
|
|
10
|
-
stop(client:
|
|
11
|
-
|
|
12
|
-
getRepository<T extends Model<T>>(model: ModelCtor<T>, conId?: string): Repository<T>;
|
|
9
|
+
protected init(config: MysqlConfig): Promise<DataSource>;
|
|
10
|
+
start(client: DataSource, conId?: string): Promise<void>;
|
|
11
|
+
stop(client: DataSource, conId?: string): Promise<void>;
|
|
12
|
+
getRepository<T extends MysqlModel>(entityClass: Constructor<T>, conId?: string): Repository<T>;
|
|
13
13
|
}
|
|
14
14
|
//# sourceMappingURL=mysql.service.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mysql.service.d.ts","sourceRoot":"","sources":["../src/mysql.service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,
|
|
1
|
+
{"version":3,"file":"mysql.service.d.ts","sourceRoot":"","sources":["../src/mysql.service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,WAAW,EAA6C,MAAM,cAAc,CAAC;AAE7G,OAAO,EAAgB,UAAU,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAG/D,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AACtC,OAAO,EAAsB,WAAW,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AACrF,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAI7C,qBACa,YAAa,SAAQ,qBAAqB,CAAC,WAAW,EAAE,UAAU,CAAE,YAAW,WAAW;IAC7D,OAAO,CAAC,aAAa;gBAAb,aAAa,EAAE,kBAAkB;cAKjE,IAAI,CAAC,MAAM,EAAE,WAAW,GAAG,OAAO,CAAC,UAAU,CAAC;IAkBxD,KAAK,CAAC,MAAM,EAAE,UAAU,EAAE,KAAK,GAAE,MAAuB,GAAG,OAAO,CAAC,IAAI,CAAC;IAmBxE,IAAI,CAAC,MAAM,EAAE,UAAU,EAAE,KAAK,GAAE,MAAuB,GAAG,OAAO,CAAC,IAAI,CAAC;IAStE,aAAa,CAAC,CAAC,SAAS,UAAU,EACvC,WAAW,EAAE,WAAW,CAAC,CAAC,CAAC,EAC3B,KAAK,GAAE,MAAuB,GAC7B,UAAU,CAAC,CAAC,CAAC;CAGjB"}
|
package/dist/mysql.service.js
CHANGED
|
@@ -15,7 +15,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
15
15
|
exports.MysqlService = void 0;
|
|
16
16
|
const core_1 = require("@joktec/core");
|
|
17
17
|
const lodash_1 = require("lodash");
|
|
18
|
-
const
|
|
18
|
+
const typeorm_1 = require("typeorm");
|
|
19
|
+
const typeorm_naming_strategies_1 = require("typeorm-naming-strategies");
|
|
19
20
|
const mysql_client_1 = require("./mysql.client");
|
|
20
21
|
const mysql_config_1 = require("./mysql.config");
|
|
21
22
|
const RETRY_OPTS = 'mysql.retry';
|
|
@@ -27,33 +28,26 @@ let MysqlService = class MysqlService extends core_1.AbstractClientService {
|
|
|
27
28
|
async init(config) {
|
|
28
29
|
const connection = (0, lodash_1.pick)(config, ['host', 'port', 'username', 'password', 'database']);
|
|
29
30
|
const options = {
|
|
30
|
-
...
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
benchmark: config.debug,
|
|
35
|
-
logging: (sql, timing) => {
|
|
36
|
-
if (config.debug) {
|
|
37
|
-
this.logService.info('SQL statement (%s): %s', (0, core_1.getTimeString)(timing), sql);
|
|
38
|
-
}
|
|
39
|
-
},
|
|
31
|
+
...config,
|
|
32
|
+
type: config.dialect,
|
|
33
|
+
namingStrategy: new typeorm_naming_strategies_1.SnakeNamingStrategy(),
|
|
34
|
+
entities: [...this.modelRegistry[config.conId]],
|
|
40
35
|
};
|
|
41
36
|
if (config.slaves?.length) {
|
|
42
|
-
options
|
|
37
|
+
options['replication'] = { master: { ...connection }, slaves: [...config.slaves] };
|
|
43
38
|
}
|
|
44
|
-
const
|
|
39
|
+
const AppDataSource = new typeorm_1.DataSource(options);
|
|
45
40
|
this.logService.info('`%s` Connection to MySQL established on host %s', config.conId, config.host);
|
|
46
|
-
return
|
|
41
|
+
return AppDataSource;
|
|
47
42
|
}
|
|
48
43
|
async start(client, conId = core_1.DEFAULT_CON_ID) {
|
|
49
44
|
const config = this.getConfig(conId);
|
|
50
45
|
try {
|
|
51
|
-
await client.
|
|
46
|
+
await client.initialize();
|
|
52
47
|
this.logService.info('`%s` Connected to MySQL successfully', conId);
|
|
53
48
|
if (this.modelRegistry[conId]) {
|
|
54
|
-
client.addModels(this.modelRegistry[conId]);
|
|
55
49
|
if (config.sync) {
|
|
56
|
-
await client.
|
|
50
|
+
await client.synchronize(true);
|
|
57
51
|
this.logService.info('`%s` Sync MySQL schema successfully', conId);
|
|
58
52
|
}
|
|
59
53
|
}
|
|
@@ -64,18 +58,15 @@ let MysqlService = class MysqlService extends core_1.AbstractClientService {
|
|
|
64
58
|
}
|
|
65
59
|
async stop(client, conId = core_1.DEFAULT_CON_ID) {
|
|
66
60
|
try {
|
|
67
|
-
await client.
|
|
61
|
+
await client.destroy();
|
|
68
62
|
this.logService.warn('`%s` Close connection to MySQL successfully', conId);
|
|
69
63
|
}
|
|
70
64
|
catch (err) {
|
|
71
65
|
this.logService.error(err, '`%s` Error when close connection to MySQL', conId);
|
|
72
66
|
}
|
|
73
67
|
}
|
|
74
|
-
|
|
75
|
-
return this.getClient(conId).
|
|
76
|
-
}
|
|
77
|
-
getRepository(model, conId = core_1.DEFAULT_CON_ID) {
|
|
78
|
-
return this.getClient(conId).getRepository(model);
|
|
68
|
+
getRepository(entityClass, conId = core_1.DEFAULT_CON_ID) {
|
|
69
|
+
return this.getClient(conId).getRepository(entityClass);
|
|
79
70
|
}
|
|
80
71
|
};
|
|
81
72
|
exports.MysqlService = MysqlService;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mysql.service.js","sourceRoot":"","sources":["../src/mysql.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"mysql.service.js","sourceRoot":"","sources":["../src/mysql.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,uCAA6G;AAC7G,mCAA8B;AAC9B,qCAA+D;AAE/D,yEAAgE;AAEhE,iDAAqF;AACrF,iDAA6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