@axiosleo/orm-mysql 0.8.0 → 0.8.3
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/README.md +2 -2
- package/index.d.ts +18 -6
- package/package.json +1 -1
- package/src/builder.js +8 -2
- package/src/client.js +57 -4
- package/src/operator.js +12 -42
- package/src/utils.js +40 -2
package/README.md
CHANGED
|
@@ -14,7 +14,7 @@ npm install @axiosleo/orm-mysql
|
|
|
14
14
|
|
|
15
15
|
## Usage
|
|
16
16
|
|
|
17
|
-
### Create
|
|
17
|
+
### Create MySQL client
|
|
18
18
|
|
|
19
19
|
```javascript
|
|
20
20
|
const { createClient } = require("@axiosleo/orm-mysql");
|
|
@@ -230,7 +230,7 @@ const hanlder = new QueryHandler(conn, {
|
|
|
230
230
|
|
|
231
231
|
## License
|
|
232
232
|
|
|
233
|
-
This project is open-sourced software licensed under
|
|
233
|
+
This project is open-sourced software licensed under [MIT](LICENSE).
|
|
234
234
|
|
|
235
235
|
|
|
236
236
|
[](https://app.fossa.com/projects/git%2Bgithub.com%2FAxiosLeo%2Fnode-orm-mysql?ref=badge_large)
|
package/index.d.ts
CHANGED
|
@@ -13,7 +13,7 @@ import {
|
|
|
13
13
|
} from 'mysql2/promise';
|
|
14
14
|
|
|
15
15
|
export type Clients = {
|
|
16
|
-
[key: string]: Connection
|
|
16
|
+
[key: string]: Connection | Pool
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
export type ConditionValueType = null | string | number | boolean | Date | Array<string | number | boolean | Date> | Query;
|
|
@@ -108,10 +108,10 @@ export declare class Query {
|
|
|
108
108
|
export type QueryResult = any | undefined | RowDataPacket[] | RowDataPacket | OkPacket;
|
|
109
109
|
|
|
110
110
|
export declare class QueryOperator extends Query {
|
|
111
|
-
conn: Connection;
|
|
111
|
+
conn: Connection | Pool;
|
|
112
112
|
options: QueryOperatorOptions
|
|
113
113
|
|
|
114
|
-
constructor(conn: Connection, opt?: QueryOperatorBaseOptions);
|
|
114
|
+
constructor(conn: Connection | Pool, opt?: QueryOperatorBaseOptions);
|
|
115
115
|
|
|
116
116
|
buildSql(operator: OperatorType): { sql: string, values: any[] };
|
|
117
117
|
|
|
@@ -136,10 +136,10 @@ export declare class QueryOperator extends Query {
|
|
|
136
136
|
}
|
|
137
137
|
|
|
138
138
|
export declare class QueryHandler {
|
|
139
|
-
conn: Connection;
|
|
139
|
+
conn: Connection | Pool;
|
|
140
140
|
options: QueryOperatorBaseOptions;
|
|
141
141
|
|
|
142
|
-
constructor(conn: Connection, options?: QueryOperatorBaseOptions);
|
|
142
|
+
constructor(conn: Connection | Pool, options?: QueryOperatorBaseOptions);
|
|
143
143
|
|
|
144
144
|
/**
|
|
145
145
|
* select table
|
|
@@ -200,7 +200,7 @@ export declare class TransactionHandler {
|
|
|
200
200
|
|
|
201
201
|
export function createClient(options: ConnectionOptions, name?: string | null | undefined): Connection;
|
|
202
202
|
|
|
203
|
-
export function getClient(name: string): Connection;
|
|
203
|
+
export function getClient(name: string): Connection | Pool;
|
|
204
204
|
|
|
205
205
|
export function createPool(options: PoolOptions, name?: string | null | undefined): Pool;
|
|
206
206
|
|
|
@@ -253,3 +253,15 @@ export declare class Builder {
|
|
|
253
253
|
values: any[];
|
|
254
254
|
constructor(options: QueryOperatorOptions);
|
|
255
255
|
}
|
|
256
|
+
|
|
257
|
+
export declare class MySQLClient extends QueryHandler {
|
|
258
|
+
constructor(options?: ConnectionOptions, name?: string | null | undefined);
|
|
259
|
+
|
|
260
|
+
existDatabase(database?: string): Promise<boolean>;
|
|
261
|
+
|
|
262
|
+
existTable(database: string, table: string): Promise<boolean>;
|
|
263
|
+
|
|
264
|
+
exexQuery(query: Query, operator: OperatorType): Promise<QueryResult>;
|
|
265
|
+
|
|
266
|
+
close(): Promise<void>;
|
|
267
|
+
}
|
package/package.json
CHANGED
package/src/builder.js
CHANGED
|
@@ -149,10 +149,16 @@ class Builder {
|
|
|
149
149
|
throw new Error('At least one table is required');
|
|
150
150
|
}
|
|
151
151
|
return tables.map((t) => {
|
|
152
|
+
let name = t.tableName.split('.').map((n) => {
|
|
153
|
+
if (n[0] === '`' && n[n.length - 1] === '`') {
|
|
154
|
+
return n;
|
|
155
|
+
}
|
|
156
|
+
return `\`${n}\``;
|
|
157
|
+
}).join('.');
|
|
152
158
|
if (t.alias) {
|
|
153
|
-
return
|
|
159
|
+
return `${name} AS \`${t.alias}\``;
|
|
154
160
|
}
|
|
155
|
-
return
|
|
161
|
+
return name;
|
|
156
162
|
}).join(' , ');
|
|
157
163
|
}
|
|
158
164
|
|
package/src/client.js
CHANGED
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
const mysql = require('mysql2');
|
|
4
4
|
const mysqlPromise = require('mysql2/promise');
|
|
5
|
-
const {
|
|
5
|
+
const { _validate } = require('./utils');
|
|
6
|
+
const { QueryHandler, Query } = require('./operator');
|
|
7
|
+
const { _query } = require('./utils');
|
|
6
8
|
|
|
7
9
|
const clients = {};
|
|
8
10
|
|
|
@@ -12,7 +14,7 @@ const clients = {};
|
|
|
12
14
|
* @returns {mysql.Connection}
|
|
13
15
|
*/
|
|
14
16
|
const createClient = (options, name = null) => {
|
|
15
|
-
|
|
17
|
+
_validate(options, {
|
|
16
18
|
host: 'required|string',
|
|
17
19
|
user: 'required|string',
|
|
18
20
|
password: 'required|string',
|
|
@@ -35,7 +37,7 @@ const createClient = (options, name = null) => {
|
|
|
35
37
|
* @returns {mysqlPromise.Connection}
|
|
36
38
|
*/
|
|
37
39
|
const createPromiseClient = async (options, name = null) => {
|
|
38
|
-
|
|
40
|
+
_validate(options, {
|
|
39
41
|
host: 'required|string',
|
|
40
42
|
user: 'required|string',
|
|
41
43
|
password: 'required|string',
|
|
@@ -57,7 +59,7 @@ const createPromiseClient = async (options, name = null) => {
|
|
|
57
59
|
* @returns {mysql.Pool}
|
|
58
60
|
*/
|
|
59
61
|
const createPool = (options, name = null) => {
|
|
60
|
-
|
|
62
|
+
_validate(options, {
|
|
61
63
|
host: 'required|string',
|
|
62
64
|
user: 'required|string',
|
|
63
65
|
password: 'required|string',
|
|
@@ -89,7 +91,58 @@ const getClient = (name) => {
|
|
|
89
91
|
return clients[name];
|
|
90
92
|
};
|
|
91
93
|
|
|
94
|
+
class MySQLClient extends QueryHandler {
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* @param {mysql.ConnectionOptions} options
|
|
98
|
+
* @param {*} name
|
|
99
|
+
*/
|
|
100
|
+
constructor(options, name = 'default') {
|
|
101
|
+
const conn = createClient(options, name);
|
|
102
|
+
super(conn);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async existTable(database, table) {
|
|
106
|
+
const c = await this.table('information_schema.TABLES')
|
|
107
|
+
.where('TABLE_SCHEMA', database)
|
|
108
|
+
.where('TABLE_NAME', table)
|
|
109
|
+
.count();
|
|
110
|
+
return !!c;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async existDatabase(database) {
|
|
114
|
+
const c = await this.table('information_schema.SCHEMATA')
|
|
115
|
+
.where('SCHEMA_NAME', database)
|
|
116
|
+
.count();
|
|
117
|
+
return !!c;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* @param {import('./operator').Query} query
|
|
122
|
+
*/
|
|
123
|
+
async exexQuery(query, operator = null) {
|
|
124
|
+
if (query instanceof Query) {
|
|
125
|
+
query.options.operator = operator;
|
|
126
|
+
return await _query(this.conn, query.options);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async close() {
|
|
131
|
+
return new Promise((resolve, reject) => {
|
|
132
|
+
this.conn.end((err) => {
|
|
133
|
+
if (err) {
|
|
134
|
+
reject(err);
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
resolve();
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
92
143
|
module.exports = {
|
|
144
|
+
MySQLClient,
|
|
145
|
+
|
|
93
146
|
getClient,
|
|
94
147
|
createPool,
|
|
95
148
|
createClient,
|
package/src/operator.js
CHANGED
|
@@ -3,42 +3,7 @@
|
|
|
3
3
|
const { Builder } = require('./builder');
|
|
4
4
|
const Query = require('./query');
|
|
5
5
|
const Hook = require('./hook');
|
|
6
|
-
|
|
7
|
-
const query = async (conn, options, opt = null) => {
|
|
8
|
-
switch (options.driver) {
|
|
9
|
-
case 'mysql': {
|
|
10
|
-
if (opt === null) {
|
|
11
|
-
const builder = new Builder(options);
|
|
12
|
-
opt = {
|
|
13
|
-
sql: builder.sql,
|
|
14
|
-
values: builder.values || [],
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
return new Promise((resolve, reject) => {
|
|
18
|
-
if (options.transaction) {
|
|
19
|
-
conn.execute(opt)
|
|
20
|
-
.then((res) => resolve(res))
|
|
21
|
-
.catch((err) => reject(err));
|
|
22
|
-
} else {
|
|
23
|
-
conn.query(opt, (err, result) => {
|
|
24
|
-
if (err) {
|
|
25
|
-
reject(err);
|
|
26
|
-
} else {
|
|
27
|
-
resolve(result);
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
default: {
|
|
34
|
-
const promise = options.query_handler(conn, options, opt);
|
|
35
|
-
if (!(promise instanceof Promise)) {
|
|
36
|
-
throw new Error('query_handler must return a promise');
|
|
37
|
-
}
|
|
38
|
-
return promise;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
};
|
|
6
|
+
const { _query } = require('./utils');
|
|
42
7
|
|
|
43
8
|
class QueryOperator extends Query {
|
|
44
9
|
/**
|
|
@@ -77,17 +42,17 @@ class QueryOperator extends Query {
|
|
|
77
42
|
try {
|
|
78
43
|
switch (this.options.operator) {
|
|
79
44
|
case 'find': {
|
|
80
|
-
const tmp = await
|
|
45
|
+
const tmp = await _query(this.conn, this.options);
|
|
81
46
|
res = tmp[0];
|
|
82
47
|
break;
|
|
83
48
|
}
|
|
84
49
|
case 'count': {
|
|
85
|
-
const [tmp] = await
|
|
50
|
+
const [tmp] = await _query(this.conn, this.options);
|
|
86
51
|
res = tmp.count;
|
|
87
52
|
break;
|
|
88
53
|
}
|
|
89
54
|
default:
|
|
90
|
-
res = await
|
|
55
|
+
res = await _query(this.conn, this.options);
|
|
91
56
|
}
|
|
92
57
|
Hook.listen({ label: 'post', table: from, opt: this.options.operator }, this.options, res);
|
|
93
58
|
} catch (err) {
|
|
@@ -140,18 +105,23 @@ class QueryOperator extends Query {
|
|
|
140
105
|
class QueryHandler {
|
|
141
106
|
constructor(conn, options = {}) {
|
|
142
107
|
this.conn = conn;
|
|
143
|
-
this.
|
|
108
|
+
this.options = options;
|
|
144
109
|
}
|
|
145
110
|
|
|
111
|
+
/**
|
|
112
|
+
*
|
|
113
|
+
* @param {import('mysql2').QueryOptions} opt
|
|
114
|
+
* @returns
|
|
115
|
+
*/
|
|
146
116
|
async query(opt) {
|
|
147
117
|
if (!opt) {
|
|
148
118
|
throw new Error('opt is required');
|
|
149
119
|
}
|
|
150
|
-
return await
|
|
120
|
+
return await _query(this.conn, this.options, opt);
|
|
151
121
|
}
|
|
152
122
|
|
|
153
123
|
table(table, alias = null) {
|
|
154
|
-
return (new QueryOperator(this.conn, this.
|
|
124
|
+
return (new QueryOperator(this.conn, this.options)).table(table, alias);
|
|
155
125
|
}
|
|
156
126
|
|
|
157
127
|
async upsert(tableName, data, condition = {}) {
|
package/src/utils.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const Validator = require('validatorjs');
|
|
4
|
+
const { Builder } = require('./builder');
|
|
4
5
|
|
|
5
|
-
const
|
|
6
|
+
const _validate = (obj, rules) => {
|
|
6
7
|
let validation = new Validator(obj, rules);
|
|
7
8
|
validation.check();
|
|
8
9
|
if (validation.fails()) {
|
|
@@ -12,6 +13,43 @@ const validate = (obj, rules) => {
|
|
|
12
13
|
}
|
|
13
14
|
};
|
|
14
15
|
|
|
16
|
+
const _query = async (conn, options, opt = null) => {
|
|
17
|
+
switch (options.driver) {
|
|
18
|
+
case 'mysql': {
|
|
19
|
+
if (opt === null) {
|
|
20
|
+
const builder = new Builder(options);
|
|
21
|
+
opt = {
|
|
22
|
+
sql: builder.sql,
|
|
23
|
+
values: builder.values || [],
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
return new Promise((resolve, reject) => {
|
|
27
|
+
if (options.transaction) {
|
|
28
|
+
conn.execute(opt)
|
|
29
|
+
.then((res) => resolve(res))
|
|
30
|
+
.catch((err) => reject(err));
|
|
31
|
+
} else {
|
|
32
|
+
conn.query(opt, (err, result) => {
|
|
33
|
+
if (err) {
|
|
34
|
+
reject(err);
|
|
35
|
+
} else {
|
|
36
|
+
resolve(result);
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
default: {
|
|
43
|
+
const promise = options.query_handler(conn, options, opt);
|
|
44
|
+
if (!(promise instanceof Promise)) {
|
|
45
|
+
throw new Error('query_handler must return a promise');
|
|
46
|
+
}
|
|
47
|
+
return promise;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
|
|
15
52
|
module.exports = {
|
|
16
|
-
|
|
53
|
+
_validate,
|
|
54
|
+
_query
|
|
17
55
|
};
|