@axiosleo/orm-mysql 0.5.6 → 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/index.d.ts +32 -1
- package/index.js +1 -1
- package/package.json +1 -1
- package/src/hook.js +47 -29
- package/src/operator.js +4 -4
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[];
|
|
@@ -199,13 +199,44 @@ export function createPool(options: PoolOptions, name?: string | null | undefine
|
|
|
199
199
|
export function createPromiseClient(options: ConnectionOptions, name?: string | null | undefined): PromiseConnection;
|
|
200
200
|
|
|
201
201
|
export declare class Hook {
|
|
202
|
+
/**
|
|
203
|
+
* pre hook for query operator
|
|
204
|
+
*/
|
|
202
205
|
static pre: (
|
|
203
206
|
callback: (options: QueryOperatorOptions) => void,
|
|
204
207
|
option: { table?: string, opt?: OperatorType }
|
|
205
208
|
) => string;
|
|
206
209
|
|
|
210
|
+
/**
|
|
211
|
+
* post hook for query operator
|
|
212
|
+
*/
|
|
207
213
|
static post: (
|
|
208
214
|
callback: (options: QueryOperatorOptions, result: QueryResult | Error) => void,
|
|
209
215
|
option: { table?: string, opt?: OperatorType }
|
|
210
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
|
|
211
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) => {
|
|
@@ -49,7 +49,7 @@ class QueryOperator extends Query {
|
|
|
49
49
|
sql, values
|
|
50
50
|
};
|
|
51
51
|
const from = this.options.tables.map(t => t.tableName).join(',');
|
|
52
|
-
|
|
52
|
+
Hook.listen({ label: 'pre', table: from, opt: this.options.operator }, this.options);
|
|
53
53
|
let res;
|
|
54
54
|
try {
|
|
55
55
|
switch (this.options.operator) {
|
|
@@ -66,9 +66,9 @@ class QueryOperator extends Query {
|
|
|
66
66
|
default:
|
|
67
67
|
res = await query(this.conn, options, this.options.transaction);
|
|
68
68
|
}
|
|
69
|
-
|
|
69
|
+
Hook.listen({ label: 'post', table: from, opt: this.options.operator }, this.options, res);
|
|
70
70
|
} catch (err) {
|
|
71
|
-
|
|
71
|
+
Hook.listen({ label: 'post', table: from, opt: this.options.operator }, this.options, err);
|
|
72
72
|
throw err;
|
|
73
73
|
}
|
|
74
74
|
return res;
|