@joktec/mysql 0.0.68 → 0.0.70
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/LICENSE +22 -22
- package/README.md +118 -118
- package/bin/generate-entity.sh +189 -189
- package/dist/__tests__/mysql.utils.spec.d.ts +1 -1
- package/dist/__tests__/mysql.utils.spec.js +25 -25
- package/dist/index.d.ts +10 -10
- package/dist/index.js +28 -28
- package/dist/models/index.d.ts +3 -3
- package/dist/models/index.js +19 -19
- package/dist/models/mysql.model.d.ts +6 -6
- package/dist/models/mysql.model.js +35 -35
- package/dist/models/mysql.request.d.ts +10 -10
- package/dist/models/mysql.request.js +2 -2
- package/dist/models/mysql.response.d.ts +3 -3
- package/dist/models/mysql.response.js +2 -2
- package/dist/mysql.client.d.ts +8 -8
- package/dist/mysql.client.js +2 -2
- package/dist/mysql.config.d.ts +34 -34
- package/dist/mysql.config.js +119 -119
- package/dist/mysql.exception.d.ts +5 -5
- package/dist/mysql.exception.js +35 -35
- package/dist/mysql.module.d.ts +2 -2
- package/dist/mysql.module.js +22 -22
- package/dist/mysql.repo.d.ts +22 -22
- package/dist/mysql.repo.js +118 -118
- package/dist/mysql.service.d.ts +12 -12
- package/dist/mysql.service.js +81 -81
- package/dist/mysql.utils.d.ts +6 -6
- package/dist/mysql.utils.js +49 -49
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -3
package/dist/mysql.repo.js
CHANGED
|
@@ -1,119 +1,119 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
-
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;
|
|
6
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
-
};
|
|
8
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.MysqlRepo = void 0;
|
|
13
|
-
const core_1 = require("@joktec/core");
|
|
14
|
-
const mysql_utils_1 = require("./mysql.utils");
|
|
15
|
-
const mysql_exception_1 = require("./mysql.exception");
|
|
16
|
-
class MysqlRepo {
|
|
17
|
-
constructor(mysqlService, model, conId = core_1.DEFAULT_CON_ID) {
|
|
18
|
-
this.mysqlService = mysqlService;
|
|
19
|
-
this.model = model;
|
|
20
|
-
this.conId = conId;
|
|
21
|
-
}
|
|
22
|
-
async onModuleInit() {
|
|
23
|
-
await this.mysqlService.getModelSync(this.model, this.conId);
|
|
24
|
-
}
|
|
25
|
-
async find(query) {
|
|
26
|
-
const options = (0, mysql_utils_1.preHandleQuery)(query);
|
|
27
|
-
if (query.select)
|
|
28
|
-
options.attributes = query.select.split(',');
|
|
29
|
-
if (query.sort)
|
|
30
|
-
options.order = Object.entries(query.sort);
|
|
31
|
-
if (query.limit && query.page) {
|
|
32
|
-
options.limit = query.limit;
|
|
33
|
-
options.offset = (query.page - 1) * query.limit;
|
|
34
|
-
}
|
|
35
|
-
return this.model.findAll(options);
|
|
36
|
-
}
|
|
37
|
-
async count(query) {
|
|
38
|
-
const options = (0, mysql_utils_1.preHandleQuery)(query);
|
|
39
|
-
return this.model.count(options);
|
|
40
|
-
}
|
|
41
|
-
async findOne(query) {
|
|
42
|
-
const options = (0, mysql_utils_1.preHandleQuery)(query);
|
|
43
|
-
if (query.select)
|
|
44
|
-
options.attributes = query.select.split(',');
|
|
45
|
-
return this.model.findOne(options);
|
|
46
|
-
}
|
|
47
|
-
async create(body) {
|
|
48
|
-
return this.model.create(body);
|
|
49
|
-
}
|
|
50
|
-
async update(condition, body) {
|
|
51
|
-
const options = (0, mysql_utils_1.preHandleQuery)({ condition });
|
|
52
|
-
const model = await this.model.findOne(options);
|
|
53
|
-
if (!model)
|
|
54
|
-
return null;
|
|
55
|
-
const fields = Object.keys(body);
|
|
56
|
-
return model.update(body, { fields });
|
|
57
|
-
}
|
|
58
|
-
async delete(condition, opts) {
|
|
59
|
-
const existModel = await this.findOne({ condition });
|
|
60
|
-
if (!existModel)
|
|
61
|
-
return null;
|
|
62
|
-
const options = (0, mysql_utils_1.preHandleQuery)({ condition });
|
|
63
|
-
options.force = (0, core_1.toBool)(opts === null || opts === void 0 ? void 0 : opts.force, false);
|
|
64
|
-
await this.model.destroy(options);
|
|
65
|
-
return existModel;
|
|
66
|
-
}
|
|
67
|
-
async upsert(condition, body) {
|
|
68
|
-
const fields = Object.keys(body);
|
|
69
|
-
const pk = this.model.primaryKeyAttribute;
|
|
70
|
-
const [row, result] = await this.model.upsert(body, { returning: true, fields, conflictFields: [pk] });
|
|
71
|
-
if (!row || !result)
|
|
72
|
-
return null;
|
|
73
|
-
return row;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
__decorate([
|
|
77
|
-
mysql_exception_1.MysqlCatch,
|
|
78
|
-
__metadata("design:type", Function),
|
|
79
|
-
__metadata("design:paramtypes", [Object]),
|
|
80
|
-
__metadata("design:returntype", Promise)
|
|
81
|
-
], MysqlRepo.prototype, "find", null);
|
|
82
|
-
__decorate([
|
|
83
|
-
mysql_exception_1.MysqlCatch,
|
|
84
|
-
__metadata("design:type", Function),
|
|
85
|
-
__metadata("design:paramtypes", [Object]),
|
|
86
|
-
__metadata("design:returntype", Promise)
|
|
87
|
-
], MysqlRepo.prototype, "count", null);
|
|
88
|
-
__decorate([
|
|
89
|
-
mysql_exception_1.MysqlCatch,
|
|
90
|
-
__metadata("design:type", Function),
|
|
91
|
-
__metadata("design:paramtypes", [Object]),
|
|
92
|
-
__metadata("design:returntype", Promise)
|
|
93
|
-
], MysqlRepo.prototype, "findOne", null);
|
|
94
|
-
__decorate([
|
|
95
|
-
mysql_exception_1.MysqlCatch,
|
|
96
|
-
__metadata("design:type", Function),
|
|
97
|
-
__metadata("design:paramtypes", [Object]),
|
|
98
|
-
__metadata("design:returntype", Promise)
|
|
99
|
-
], MysqlRepo.prototype, "create", null);
|
|
100
|
-
__decorate([
|
|
101
|
-
mysql_exception_1.MysqlCatch,
|
|
102
|
-
__metadata("design:type", Function),
|
|
103
|
-
__metadata("design:paramtypes", [Object, Object]),
|
|
104
|
-
__metadata("design:returntype", Promise)
|
|
105
|
-
], MysqlRepo.prototype, "update", null);
|
|
106
|
-
__decorate([
|
|
107
|
-
mysql_exception_1.MysqlCatch,
|
|
108
|
-
__metadata("design:type", Function),
|
|
109
|
-
__metadata("design:paramtypes", [Object, Object]),
|
|
110
|
-
__metadata("design:returntype", Promise)
|
|
111
|
-
], MysqlRepo.prototype, "delete", null);
|
|
112
|
-
__decorate([
|
|
113
|
-
mysql_exception_1.MysqlCatch,
|
|
114
|
-
__metadata("design:type", Function),
|
|
115
|
-
__metadata("design:paramtypes", [Object, Object]),
|
|
116
|
-
__metadata("design:returntype", Promise)
|
|
117
|
-
], MysqlRepo.prototype, "upsert", null);
|
|
118
|
-
exports.MysqlRepo = MysqlRepo;
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
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;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.MysqlRepo = void 0;
|
|
13
|
+
const core_1 = require("@joktec/core");
|
|
14
|
+
const mysql_utils_1 = require("./mysql.utils");
|
|
15
|
+
const mysql_exception_1 = require("./mysql.exception");
|
|
16
|
+
class MysqlRepo {
|
|
17
|
+
constructor(mysqlService, model, conId = core_1.DEFAULT_CON_ID) {
|
|
18
|
+
this.mysqlService = mysqlService;
|
|
19
|
+
this.model = model;
|
|
20
|
+
this.conId = conId;
|
|
21
|
+
}
|
|
22
|
+
async onModuleInit() {
|
|
23
|
+
await this.mysqlService.getModelSync(this.model, this.conId);
|
|
24
|
+
}
|
|
25
|
+
async find(query) {
|
|
26
|
+
const options = (0, mysql_utils_1.preHandleQuery)(query);
|
|
27
|
+
if (query.select)
|
|
28
|
+
options.attributes = query.select.split(',');
|
|
29
|
+
if (query.sort)
|
|
30
|
+
options.order = Object.entries(query.sort);
|
|
31
|
+
if (query.limit && query.page) {
|
|
32
|
+
options.limit = query.limit;
|
|
33
|
+
options.offset = (query.page - 1) * query.limit;
|
|
34
|
+
}
|
|
35
|
+
return this.model.findAll(options);
|
|
36
|
+
}
|
|
37
|
+
async count(query) {
|
|
38
|
+
const options = (0, mysql_utils_1.preHandleQuery)(query);
|
|
39
|
+
return this.model.count(options);
|
|
40
|
+
}
|
|
41
|
+
async findOne(query) {
|
|
42
|
+
const options = (0, mysql_utils_1.preHandleQuery)(query);
|
|
43
|
+
if (query.select)
|
|
44
|
+
options.attributes = query.select.split(',');
|
|
45
|
+
return this.model.findOne(options);
|
|
46
|
+
}
|
|
47
|
+
async create(body) {
|
|
48
|
+
return this.model.create(body);
|
|
49
|
+
}
|
|
50
|
+
async update(condition, body) {
|
|
51
|
+
const options = (0, mysql_utils_1.preHandleQuery)({ condition });
|
|
52
|
+
const model = await this.model.findOne(options);
|
|
53
|
+
if (!model)
|
|
54
|
+
return null;
|
|
55
|
+
const fields = Object.keys(body);
|
|
56
|
+
return model.update(body, { fields });
|
|
57
|
+
}
|
|
58
|
+
async delete(condition, opts) {
|
|
59
|
+
const existModel = await this.findOne({ condition });
|
|
60
|
+
if (!existModel)
|
|
61
|
+
return null;
|
|
62
|
+
const options = (0, mysql_utils_1.preHandleQuery)({ condition });
|
|
63
|
+
options.force = (0, core_1.toBool)(opts === null || opts === void 0 ? void 0 : opts.force, false);
|
|
64
|
+
await this.model.destroy(options);
|
|
65
|
+
return existModel;
|
|
66
|
+
}
|
|
67
|
+
async upsert(condition, body) {
|
|
68
|
+
const fields = Object.keys(body);
|
|
69
|
+
const pk = this.model.primaryKeyAttribute;
|
|
70
|
+
const [row, result] = await this.model.upsert(body, { returning: true, fields, conflictFields: [pk] });
|
|
71
|
+
if (!row || !result)
|
|
72
|
+
return null;
|
|
73
|
+
return row;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
__decorate([
|
|
77
|
+
mysql_exception_1.MysqlCatch,
|
|
78
|
+
__metadata("design:type", Function),
|
|
79
|
+
__metadata("design:paramtypes", [Object]),
|
|
80
|
+
__metadata("design:returntype", Promise)
|
|
81
|
+
], MysqlRepo.prototype, "find", null);
|
|
82
|
+
__decorate([
|
|
83
|
+
mysql_exception_1.MysqlCatch,
|
|
84
|
+
__metadata("design:type", Function),
|
|
85
|
+
__metadata("design:paramtypes", [Object]),
|
|
86
|
+
__metadata("design:returntype", Promise)
|
|
87
|
+
], MysqlRepo.prototype, "count", null);
|
|
88
|
+
__decorate([
|
|
89
|
+
mysql_exception_1.MysqlCatch,
|
|
90
|
+
__metadata("design:type", Function),
|
|
91
|
+
__metadata("design:paramtypes", [Object]),
|
|
92
|
+
__metadata("design:returntype", Promise)
|
|
93
|
+
], MysqlRepo.prototype, "findOne", null);
|
|
94
|
+
__decorate([
|
|
95
|
+
mysql_exception_1.MysqlCatch,
|
|
96
|
+
__metadata("design:type", Function),
|
|
97
|
+
__metadata("design:paramtypes", [Object]),
|
|
98
|
+
__metadata("design:returntype", Promise)
|
|
99
|
+
], MysqlRepo.prototype, "create", null);
|
|
100
|
+
__decorate([
|
|
101
|
+
mysql_exception_1.MysqlCatch,
|
|
102
|
+
__metadata("design:type", Function),
|
|
103
|
+
__metadata("design:paramtypes", [Object, Object]),
|
|
104
|
+
__metadata("design:returntype", Promise)
|
|
105
|
+
], MysqlRepo.prototype, "update", null);
|
|
106
|
+
__decorate([
|
|
107
|
+
mysql_exception_1.MysqlCatch,
|
|
108
|
+
__metadata("design:type", Function),
|
|
109
|
+
__metadata("design:paramtypes", [Object, Object]),
|
|
110
|
+
__metadata("design:returntype", Promise)
|
|
111
|
+
], MysqlRepo.prototype, "delete", null);
|
|
112
|
+
__decorate([
|
|
113
|
+
mysql_exception_1.MysqlCatch,
|
|
114
|
+
__metadata("design:type", Function),
|
|
115
|
+
__metadata("design:paramtypes", [Object, Object]),
|
|
116
|
+
__metadata("design:returntype", Promise)
|
|
117
|
+
], MysqlRepo.prototype, "upsert", null);
|
|
118
|
+
exports.MysqlRepo = MysqlRepo;
|
|
119
119
|
//# sourceMappingURL=mysql.repo.js.map
|
package/dist/mysql.service.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { AbstractClientService } from '@joktec/core';
|
|
2
|
-
import { MysqlConfig } from './mysql.config';
|
|
3
|
-
import { MysqlClient } from './mysql.client';
|
|
4
|
-
import { Model, ModelCtor, Sequelize } from 'sequelize-typescript';
|
|
5
|
-
export declare class MysqlService extends AbstractClientService<MysqlConfig, Sequelize> implements MysqlClient {
|
|
6
|
-
constructor();
|
|
7
|
-
protected init(config: MysqlConfig): Promise<Sequelize>;
|
|
8
|
-
start(client: Sequelize, conId?: string): Promise<void>;
|
|
9
|
-
stop(client: Sequelize, conId?: string): Promise<void>;
|
|
10
|
-
getModel<T extends Model<T>>(model: ModelCtor<T>, conId?: string): ModelCtor<T>;
|
|
11
|
-
getModelSync<T extends Model<T>>(model: ModelCtor<T>, conId?: string): Promise<ModelCtor<T>>;
|
|
12
|
-
}
|
|
1
|
+
import { AbstractClientService } from '@joktec/core';
|
|
2
|
+
import { MysqlConfig } from './mysql.config';
|
|
3
|
+
import { MysqlClient } from './mysql.client';
|
|
4
|
+
import { Model, ModelCtor, Sequelize } from 'sequelize-typescript';
|
|
5
|
+
export declare class MysqlService extends AbstractClientService<MysqlConfig, Sequelize> implements MysqlClient {
|
|
6
|
+
constructor();
|
|
7
|
+
protected init(config: MysqlConfig): Promise<Sequelize>;
|
|
8
|
+
start(client: Sequelize, conId?: string): Promise<void>;
|
|
9
|
+
stop(client: Sequelize, conId?: string): Promise<void>;
|
|
10
|
+
getModel<T extends Model<T>>(model: ModelCtor<T>, conId?: string): ModelCtor<T>;
|
|
11
|
+
getModelSync<T extends Model<T>>(model: ModelCtor<T>, conId?: string): Promise<ModelCtor<T>>;
|
|
12
|
+
}
|
|
13
13
|
//# sourceMappingURL=mysql.service.d.ts.map
|
package/dist/mysql.service.js
CHANGED
|
@@ -1,82 +1,82 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
-
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;
|
|
6
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
-
};
|
|
8
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.MysqlService = void 0;
|
|
13
|
-
const core_1 = require("@joktec/core");
|
|
14
|
-
const mysql_config_1 = require("./mysql.config");
|
|
15
|
-
const sequelize_typescript_1 = require("sequelize-typescript");
|
|
16
|
-
const lodash_1 = require("lodash");
|
|
17
|
-
const RETRY_OPTS = 'mysql.retry';
|
|
18
|
-
let MysqlService = class MysqlService extends core_1.AbstractClientService {
|
|
19
|
-
constructor() {
|
|
20
|
-
super('mysql', mysql_config_1.MysqlConfig);
|
|
21
|
-
}
|
|
22
|
-
async init(config) {
|
|
23
|
-
var _a;
|
|
24
|
-
this.logService.info('MysqlConfig: %j', config);
|
|
25
|
-
const connection = (0, lodash_1.pick)(config, ['host', 'port', 'username', 'password', 'database']);
|
|
26
|
-
const options = Object.assign(Object.assign({}, connection), { dialect: config.dialect, dialectOptions: { charset: config.charset, connectTimeout: config.connectTimeout }, logging: (sql, timing) => {
|
|
27
|
-
this.logService.debug('SQL statement: %s', sql);
|
|
28
|
-
this.logService.trace('SQL execute in %j', timing);
|
|
29
|
-
} });
|
|
30
|
-
if ((_a = config.slaves) === null || _a === void 0 ? void 0 : _a.length) {
|
|
31
|
-
options.replication = { write: Object.assign({}, connection), read: config.slaves };
|
|
32
|
-
}
|
|
33
|
-
const sequelize = new sequelize_typescript_1.Sequelize(options);
|
|
34
|
-
this.logService.info('MySQL Service is configured in `%s` connection', config.conId);
|
|
35
|
-
return sequelize;
|
|
36
|
-
}
|
|
37
|
-
async start(client, conId = core_1.DEFAULT_CON_ID) {
|
|
38
|
-
const config = this.getConfig(conId);
|
|
39
|
-
try {
|
|
40
|
-
await client.authenticate();
|
|
41
|
-
this.logService.info('MySQL Service have been start on host %s', config.host);
|
|
42
|
-
}
|
|
43
|
-
catch (err) {
|
|
44
|
-
this.logService.error(err, 'MySQL Service failed to start on host %s', config.host);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
async stop(client, conId = core_1.DEFAULT_CON_ID) {
|
|
48
|
-
const config = this.getConfig(conId);
|
|
49
|
-
try {
|
|
50
|
-
await client.close();
|
|
51
|
-
this.logService.warn('MySQL Service have been stop on host %s', config.host);
|
|
52
|
-
}
|
|
53
|
-
catch (err) {
|
|
54
|
-
this.logService.error(err, 'MySQL Service failed to stop on host %s', config.host);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
getModel(model, conId = core_1.DEFAULT_CON_ID) {
|
|
58
|
-
if (!this.getClient(conId).isDefined(model.name)) {
|
|
59
|
-
this.getClient(conId).addModels([model]);
|
|
60
|
-
}
|
|
61
|
-
return model;
|
|
62
|
-
}
|
|
63
|
-
async getModelSync(model, conId = core_1.DEFAULT_CON_ID) {
|
|
64
|
-
if (!this.getClient(conId).isDefined(model.name)) {
|
|
65
|
-
this.getClient(conId).addModels([model]);
|
|
66
|
-
await model.sync({ alter: { drop: false } });
|
|
67
|
-
}
|
|
68
|
-
return model;
|
|
69
|
-
}
|
|
70
|
-
};
|
|
71
|
-
__decorate([
|
|
72
|
-
(0, core_1.Retry)(RETRY_OPTS),
|
|
73
|
-
__metadata("design:type", Function),
|
|
74
|
-
__metadata("design:paramtypes", [mysql_config_1.MysqlConfig]),
|
|
75
|
-
__metadata("design:returntype", Promise)
|
|
76
|
-
], MysqlService.prototype, "init", null);
|
|
77
|
-
MysqlService = __decorate([
|
|
78
|
-
(0, core_1.Injectable)(),
|
|
79
|
-
__metadata("design:paramtypes", [])
|
|
80
|
-
], MysqlService);
|
|
81
|
-
exports.MysqlService = MysqlService;
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
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;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.MysqlService = void 0;
|
|
13
|
+
const core_1 = require("@joktec/core");
|
|
14
|
+
const mysql_config_1 = require("./mysql.config");
|
|
15
|
+
const sequelize_typescript_1 = require("sequelize-typescript");
|
|
16
|
+
const lodash_1 = require("lodash");
|
|
17
|
+
const RETRY_OPTS = 'mysql.retry';
|
|
18
|
+
let MysqlService = class MysqlService extends core_1.AbstractClientService {
|
|
19
|
+
constructor() {
|
|
20
|
+
super('mysql', mysql_config_1.MysqlConfig);
|
|
21
|
+
}
|
|
22
|
+
async init(config) {
|
|
23
|
+
var _a;
|
|
24
|
+
this.logService.info('MysqlConfig: %j', config);
|
|
25
|
+
const connection = (0, lodash_1.pick)(config, ['host', 'port', 'username', 'password', 'database']);
|
|
26
|
+
const options = Object.assign(Object.assign({}, connection), { dialect: config.dialect, dialectOptions: { charset: config.charset, connectTimeout: config.connectTimeout }, logging: (sql, timing) => {
|
|
27
|
+
this.logService.debug('SQL statement: %s', sql);
|
|
28
|
+
this.logService.trace('SQL execute in %j', timing);
|
|
29
|
+
} });
|
|
30
|
+
if ((_a = config.slaves) === null || _a === void 0 ? void 0 : _a.length) {
|
|
31
|
+
options.replication = { write: Object.assign({}, connection), read: config.slaves };
|
|
32
|
+
}
|
|
33
|
+
const sequelize = new sequelize_typescript_1.Sequelize(options);
|
|
34
|
+
this.logService.info('MySQL Service is configured in `%s` connection', config.conId);
|
|
35
|
+
return sequelize;
|
|
36
|
+
}
|
|
37
|
+
async start(client, conId = core_1.DEFAULT_CON_ID) {
|
|
38
|
+
const config = this.getConfig(conId);
|
|
39
|
+
try {
|
|
40
|
+
await client.authenticate();
|
|
41
|
+
this.logService.info('MySQL Service have been start on host %s', config.host);
|
|
42
|
+
}
|
|
43
|
+
catch (err) {
|
|
44
|
+
this.logService.error(err, 'MySQL Service failed to start on host %s', config.host);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
async stop(client, conId = core_1.DEFAULT_CON_ID) {
|
|
48
|
+
const config = this.getConfig(conId);
|
|
49
|
+
try {
|
|
50
|
+
await client.close();
|
|
51
|
+
this.logService.warn('MySQL Service have been stop on host %s', config.host);
|
|
52
|
+
}
|
|
53
|
+
catch (err) {
|
|
54
|
+
this.logService.error(err, 'MySQL Service failed to stop on host %s', config.host);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
getModel(model, conId = core_1.DEFAULT_CON_ID) {
|
|
58
|
+
if (!this.getClient(conId).isDefined(model.name)) {
|
|
59
|
+
this.getClient(conId).addModels([model]);
|
|
60
|
+
}
|
|
61
|
+
return model;
|
|
62
|
+
}
|
|
63
|
+
async getModelSync(model, conId = core_1.DEFAULT_CON_ID) {
|
|
64
|
+
if (!this.getClient(conId).isDefined(model.name)) {
|
|
65
|
+
this.getClient(conId).addModels([model]);
|
|
66
|
+
await model.sync({ alter: { drop: false } });
|
|
67
|
+
}
|
|
68
|
+
return model;
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
__decorate([
|
|
72
|
+
(0, core_1.Retry)(RETRY_OPTS),
|
|
73
|
+
__metadata("design:type", Function),
|
|
74
|
+
__metadata("design:paramtypes", [mysql_config_1.MysqlConfig]),
|
|
75
|
+
__metadata("design:returntype", Promise)
|
|
76
|
+
], MysqlService.prototype, "init", null);
|
|
77
|
+
MysqlService = __decorate([
|
|
78
|
+
(0, core_1.Injectable)(),
|
|
79
|
+
__metadata("design:paramtypes", [])
|
|
80
|
+
], MysqlService);
|
|
81
|
+
exports.MysqlService = MysqlService;
|
|
82
82
|
//# sourceMappingURL=mysql.service.js.map
|
package/dist/mysql.utils.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { IOperation } from '@joktec/core';
|
|
2
|
-
import { FindOptions } from 'sequelize';
|
|
3
|
-
import { Model } from 'sequelize-typescript';
|
|
4
|
-
import { IMysqlRequest } from './models';
|
|
5
|
-
export declare const preHandleQuery: <T extends Model<T, T>>(query: IMysqlRequest<T>) => FindOptions;
|
|
6
|
-
export declare const convertOp: (op: IOperation) => symbol;
|
|
1
|
+
import { IOperation } from '@joktec/core';
|
|
2
|
+
import { FindOptions } from 'sequelize';
|
|
3
|
+
import { Model } from 'sequelize-typescript';
|
|
4
|
+
import { IMysqlRequest } from './models';
|
|
5
|
+
export declare const preHandleQuery: <T extends Model<T, T>>(query: IMysqlRequest<T>) => FindOptions;
|
|
6
|
+
export declare const convertOp: (op: IOperation) => symbol;
|
|
7
7
|
//# sourceMappingURL=mysql.utils.d.ts.map
|
package/dist/mysql.utils.js
CHANGED
|
@@ -1,50 +1,50 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.convertOp = exports.preHandleQuery = void 0;
|
|
4
|
-
const sequelize_1 = require("sequelize");
|
|
5
|
-
const preHandleQuery = (query) => {
|
|
6
|
-
const { condition = {}, keyword } = query;
|
|
7
|
-
const where = {};
|
|
8
|
-
for (const [key, value] of Object.entries(condition)) {
|
|
9
|
-
if (key === '$and') {
|
|
10
|
-
where[sequelize_1.Op.and] = value.map(c => (0, exports.preHandleQuery)({ condition: c }));
|
|
11
|
-
}
|
|
12
|
-
else if (key === '$or') {
|
|
13
|
-
where[sequelize_1.Op.or] = value.map(c => (0, exports.preHandleQuery)({ condition: c }));
|
|
14
|
-
}
|
|
15
|
-
else if (typeof value === 'object') {
|
|
16
|
-
const entries = Object.entries(value);
|
|
17
|
-
for (const [op, val] of entries) {
|
|
18
|
-
const sqlOp = (0, exports.convertOp)(op);
|
|
19
|
-
where[key] = Object.assign(Object.assign({}, where[key]), { [sqlOp]: sqlOp === sequelize_1.Op.like || sqlOp === sequelize_1.Op.notLike ? `%${val}%` : val });
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
else {
|
|
23
|
-
where[key] = value;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
if (keyword) {
|
|
27
|
-
if (!where[sequelize_1.Op.and])
|
|
28
|
-
where[sequelize_1.Op.and] = [];
|
|
29
|
-
where[sequelize_1.Op.and].push((0, sequelize_1.literal)(`MATCH(name) AGAINST('${keyword}' IN BOOLEAN MODE)`));
|
|
30
|
-
}
|
|
31
|
-
return { where };
|
|
32
|
-
};
|
|
33
|
-
exports.preHandleQuery = preHandleQuery;
|
|
34
|
-
const convertOp = (op) => {
|
|
35
|
-
const mapping = {
|
|
36
|
-
$ne: sequelize_1.Op.ne,
|
|
37
|
-
$gt: sequelize_1.Op.gt,
|
|
38
|
-
$gte: sequelize_1.Op.gte,
|
|
39
|
-
$lt: sequelize_1.Op.lt,
|
|
40
|
-
$lte: sequelize_1.Op.lte,
|
|
41
|
-
$in: sequelize_1.Op.in,
|
|
42
|
-
$nin: sequelize_1.Op.notIn,
|
|
43
|
-
$eq: sequelize_1.Op.eq,
|
|
44
|
-
$like: sequelize_1.Op.like,
|
|
45
|
-
$unlike: sequelize_1.Op.notLike,
|
|
46
|
-
};
|
|
47
|
-
return mapping[op] || sequelize_1.Op.eq;
|
|
48
|
-
};
|
|
49
|
-
exports.convertOp = convertOp;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.convertOp = exports.preHandleQuery = void 0;
|
|
4
|
+
const sequelize_1 = require("sequelize");
|
|
5
|
+
const preHandleQuery = (query) => {
|
|
6
|
+
const { condition = {}, keyword } = query;
|
|
7
|
+
const where = {};
|
|
8
|
+
for (const [key, value] of Object.entries(condition)) {
|
|
9
|
+
if (key === '$and') {
|
|
10
|
+
where[sequelize_1.Op.and] = value.map(c => (0, exports.preHandleQuery)({ condition: c }));
|
|
11
|
+
}
|
|
12
|
+
else if (key === '$or') {
|
|
13
|
+
where[sequelize_1.Op.or] = value.map(c => (0, exports.preHandleQuery)({ condition: c }));
|
|
14
|
+
}
|
|
15
|
+
else if (typeof value === 'object') {
|
|
16
|
+
const entries = Object.entries(value);
|
|
17
|
+
for (const [op, val] of entries) {
|
|
18
|
+
const sqlOp = (0, exports.convertOp)(op);
|
|
19
|
+
where[key] = Object.assign(Object.assign({}, where[key]), { [sqlOp]: sqlOp === sequelize_1.Op.like || sqlOp === sequelize_1.Op.notLike ? `%${val}%` : val });
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
where[key] = value;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (keyword) {
|
|
27
|
+
if (!where[sequelize_1.Op.and])
|
|
28
|
+
where[sequelize_1.Op.and] = [];
|
|
29
|
+
where[sequelize_1.Op.and].push((0, sequelize_1.literal)(`MATCH(name) AGAINST('${keyword}' IN BOOLEAN MODE)`));
|
|
30
|
+
}
|
|
31
|
+
return { where };
|
|
32
|
+
};
|
|
33
|
+
exports.preHandleQuery = preHandleQuery;
|
|
34
|
+
const convertOp = (op) => {
|
|
35
|
+
const mapping = {
|
|
36
|
+
$ne: sequelize_1.Op.ne,
|
|
37
|
+
$gt: sequelize_1.Op.gt,
|
|
38
|
+
$gte: sequelize_1.Op.gte,
|
|
39
|
+
$lt: sequelize_1.Op.lt,
|
|
40
|
+
$lte: sequelize_1.Op.lte,
|
|
41
|
+
$in: sequelize_1.Op.in,
|
|
42
|
+
$nin: sequelize_1.Op.notIn,
|
|
43
|
+
$eq: sequelize_1.Op.eq,
|
|
44
|
+
$like: sequelize_1.Op.like,
|
|
45
|
+
$unlike: sequelize_1.Op.notLike,
|
|
46
|
+
};
|
|
47
|
+
return mapping[op] || sequelize_1.Op.eq;
|
|
48
|
+
};
|
|
49
|
+
exports.convertOp = convertOp;
|
|
50
50
|
//# sourceMappingURL=mysql.utils.js.map
|