@axiosleo/orm-mysql 0.5.5 → 0.6.0
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 +1 -1
- package/index.d.ts +51 -1
- package/index.js +1 -1
- package/package.json +1 -1
- package/src/hook.js +47 -29
- package/src/operator.js +8 -5
package/README.md
CHANGED
|
@@ -126,7 +126,7 @@ async function deleteExample() {
|
|
|
126
126
|
}
|
|
127
127
|
|
|
128
128
|
async function subqueryExample() {
|
|
129
|
-
const query =
|
|
129
|
+
const query = handler.table("users", "u");
|
|
130
130
|
const subQuery = new Query("select");
|
|
131
131
|
subQuery.table("users").having("COUNT(*)", ">", 1);
|
|
132
132
|
|
package/index.d.ts
CHANGED
|
@@ -48,7 +48,7 @@ export interface TableOption {
|
|
|
48
48
|
alias: string | null;
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
-
export
|
|
51
|
+
export type QueryOperatorOptions = {
|
|
52
52
|
conditions: WhereOptions[];
|
|
53
53
|
attrs?: string[] | null;
|
|
54
54
|
orders: OrderByOptions[];
|
|
@@ -133,14 +133,33 @@ export declare class QueryHandler {
|
|
|
133
133
|
|
|
134
134
|
constructor(conn: Connection);
|
|
135
135
|
|
|
136
|
+
/**
|
|
137
|
+
* select table
|
|
138
|
+
* @param table
|
|
139
|
+
* @param alias
|
|
140
|
+
*/
|
|
136
141
|
table(table: string, alias?: string | null): QueryOperator;
|
|
137
142
|
|
|
143
|
+
/**
|
|
144
|
+
* execute sql
|
|
145
|
+
* @param options
|
|
146
|
+
*/
|
|
138
147
|
query(options: QueryOptions): Promise<any>;
|
|
139
148
|
|
|
149
|
+
/**
|
|
150
|
+
* insert or update
|
|
151
|
+
* @param tableName
|
|
152
|
+
* @param data
|
|
153
|
+
* @param condition
|
|
154
|
+
*/
|
|
140
155
|
upsert(tableName: string, data: any, condition: Record<string, ConditionValueType>): Promise<OkPacket>;
|
|
141
156
|
}
|
|
142
157
|
|
|
143
158
|
export declare class TransactionOperator extends QueryOperator {
|
|
159
|
+
/**
|
|
160
|
+
* @example LOCK IN SHARE MODE
|
|
161
|
+
* @example FOR UPDATE
|
|
162
|
+
*/
|
|
144
163
|
append(suffix: string): this;
|
|
145
164
|
}
|
|
146
165
|
|
|
@@ -180,13 +199,44 @@ export function createPool(options: PoolOptions, name?: string | null | undefine
|
|
|
180
199
|
export function createPromiseClient(options: ConnectionOptions, name?: string | null | undefined): PromiseConnection;
|
|
181
200
|
|
|
182
201
|
export declare class Hook {
|
|
202
|
+
/**
|
|
203
|
+
* pre hook for query operator
|
|
204
|
+
*/
|
|
183
205
|
static pre: (
|
|
184
206
|
callback: (options: QueryOperatorOptions) => void,
|
|
185
207
|
option: { table?: string, opt?: OperatorType }
|
|
186
208
|
) => string;
|
|
187
209
|
|
|
210
|
+
/**
|
|
211
|
+
* post hook for query operator
|
|
212
|
+
*/
|
|
188
213
|
static post: (
|
|
189
214
|
callback: (options: QueryOperatorOptions, result: QueryResult | Error) => void,
|
|
190
215
|
option: { table?: string, opt?: OperatorType }
|
|
191
216
|
) => string;
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* register hook
|
|
220
|
+
*/
|
|
221
|
+
static register: (
|
|
222
|
+
callback: (options: QueryOperatorOptions) => void,
|
|
223
|
+
...paths: string[]
|
|
224
|
+
) => void;
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* listen event
|
|
228
|
+
*/
|
|
229
|
+
static listen: (
|
|
230
|
+
options?: QueryOperatorOptions & { label?: string },
|
|
231
|
+
...args: any[]
|
|
232
|
+
) => void;
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* trigger event
|
|
236
|
+
*/
|
|
237
|
+
static trigger: (
|
|
238
|
+
label: string,
|
|
239
|
+
options?: QueryOperatorOptions,
|
|
240
|
+
...args: any[]
|
|
241
|
+
) => void
|
|
192
242
|
}
|
package/index.js
CHANGED
package/package.json
CHANGED
package/src/hook.js
CHANGED
|
@@ -2,25 +2,35 @@
|
|
|
2
2
|
|
|
3
3
|
const EventEmitter = require('events');
|
|
4
4
|
|
|
5
|
-
const events =
|
|
5
|
+
const events = new Map(); // event tree
|
|
6
6
|
const hook = new EventEmitter();
|
|
7
7
|
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
8
|
+
const push = (callback, trace = []) => {
|
|
9
|
+
let step = 0;
|
|
10
|
+
let curr = events;
|
|
11
|
+
let event_name_items = [];
|
|
12
|
+
while (step < trace.length) {
|
|
13
|
+
let curr_key = trace[step] || '*';
|
|
14
|
+
if (curr_key === '__proto__' || curr_key === 'constructor' || curr_key === 'prototype') {
|
|
15
|
+
curr_key = '*';
|
|
16
|
+
}
|
|
17
|
+
event_name_items.push(curr_key);
|
|
18
|
+
if (!curr.has(curr_key)) {
|
|
19
|
+
curr.set(curr_key, new Map());
|
|
20
|
+
}
|
|
21
|
+
curr = curr.get(curr_key);
|
|
22
|
+
step++;
|
|
20
23
|
}
|
|
21
|
-
|
|
22
|
-
hook.on(
|
|
23
|
-
return
|
|
24
|
+
let event_name = event_name_items.join('::');
|
|
25
|
+
hook.on(event_name, callback);
|
|
26
|
+
return event_name;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const pushEvent = (options = {}) => {
|
|
30
|
+
const { label, table, opt, callback } = options;
|
|
31
|
+
const trace = [label, table, opt];
|
|
32
|
+
const event_name = push(callback, trace);
|
|
33
|
+
return { event_name, label, table, opt, callback };
|
|
24
34
|
};
|
|
25
35
|
|
|
26
36
|
const eventRecur = (curr, trace, step, paths, args) => {
|
|
@@ -40,28 +50,36 @@ const eventRecur = (curr, trace, step, paths, args) => {
|
|
|
40
50
|
return;
|
|
41
51
|
};
|
|
42
52
|
|
|
43
|
-
const handleEvent = (label, table, opt, ...args) => {
|
|
44
|
-
let curr = events;
|
|
45
|
-
let step = 0;
|
|
46
|
-
let trace = [label, table, opt];
|
|
47
|
-
eventRecur(curr, trace, step, [], args);
|
|
48
|
-
};
|
|
49
|
-
|
|
50
53
|
class Hook {
|
|
51
|
-
static pre(callback,
|
|
54
|
+
static pre(callback, options = {}) {
|
|
55
|
+
const { table, opt } = options;
|
|
52
56
|
return pushEvent({
|
|
53
57
|
label: 'pre', table, opt, callback
|
|
54
58
|
});
|
|
55
59
|
}
|
|
56
60
|
|
|
57
|
-
static post(callback,
|
|
61
|
+
static post(callback, options = {}) {
|
|
62
|
+
const { table, opt } = options;
|
|
58
63
|
return pushEvent({
|
|
59
64
|
label: 'post', table, opt, callback
|
|
60
65
|
});
|
|
61
66
|
}
|
|
67
|
+
|
|
68
|
+
static listen(options = {}, ...args) {
|
|
69
|
+
const { label, table, opt } = options;
|
|
70
|
+
Hook.trigger([label, table, opt], ...args);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
static register(callback, ...paths) {
|
|
74
|
+
push(callback, paths);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
static trigger(paths = [], ...args) {
|
|
78
|
+
let curr = events;
|
|
79
|
+
let step = 0;
|
|
80
|
+
let trace = paths;
|
|
81
|
+
eventRecur(curr, trace, step, [], args);
|
|
82
|
+
}
|
|
62
83
|
}
|
|
63
84
|
|
|
64
|
-
module.exports =
|
|
65
|
-
Hook,
|
|
66
|
-
handleEvent
|
|
67
|
-
};
|
|
85
|
+
module.exports = Hook;
|
package/src/operator.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
const { Builder } = require('./builder');
|
|
4
4
|
const Query = require('./query');
|
|
5
|
-
const
|
|
5
|
+
const Hook = require('./hook');
|
|
6
6
|
|
|
7
7
|
const query = async (conn, options, transaction) => {
|
|
8
8
|
return new Promise((resolve, reject) => {
|
|
@@ -26,7 +26,7 @@ class QueryOperator extends Query {
|
|
|
26
26
|
/**
|
|
27
27
|
* @param {*} conn
|
|
28
28
|
*/
|
|
29
|
-
constructor(conn) {
|
|
29
|
+
constructor(conn = null) {
|
|
30
30
|
super(null);
|
|
31
31
|
this.conn = conn;
|
|
32
32
|
}
|
|
@@ -40,13 +40,16 @@ class QueryOperator extends Query {
|
|
|
40
40
|
if (!this.options.operator) {
|
|
41
41
|
throw new Error('Invalid operator: ' + this.options.operator);
|
|
42
42
|
}
|
|
43
|
+
if (this.conn === null) {
|
|
44
|
+
throw new Error('Connection is null');
|
|
45
|
+
}
|
|
43
46
|
const builder = this.buildSql(this.options.operator);
|
|
44
47
|
const { sql, values } = builder;
|
|
45
48
|
const options = {
|
|
46
49
|
sql, values
|
|
47
50
|
};
|
|
48
51
|
const from = this.options.tables.map(t => t.tableName).join(',');
|
|
49
|
-
|
|
52
|
+
Hook.listen({ label: 'pre', table: from, opt: this.options.operator }, this.options);
|
|
50
53
|
let res;
|
|
51
54
|
try {
|
|
52
55
|
switch (this.options.operator) {
|
|
@@ -63,9 +66,9 @@ class QueryOperator extends Query {
|
|
|
63
66
|
default:
|
|
64
67
|
res = await query(this.conn, options, this.options.transaction);
|
|
65
68
|
}
|
|
66
|
-
|
|
69
|
+
Hook.listen({ label: 'post', table: from, opt: this.options.operator }, this.options, res);
|
|
67
70
|
} catch (err) {
|
|
68
|
-
|
|
71
|
+
Hook.listen({ label: 'post', table: from, opt: this.options.operator }, this.options, err);
|
|
69
72
|
throw err;
|
|
70
73
|
}
|
|
71
74
|
return res;
|