-
Notifications
You must be signed in to change notification settings - Fork 2
/
speed.decorator.ts
175 lines (165 loc) · 4.67 KB
/
speed.decorator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { speedPromisePool, resultTypeMap } from './speed.service';
import { ResultSetHeader } from 'mysql2';
const paramMetadataKey = Symbol('param');
function log(message?: any, ...optionalParams: any[]) {
console.log(message, ...optionalParams);
}
function ResultType(constructorFunction) {
const newConstructorFunction: any = function (...args) {
const func: any = function () {
return new constructorFunction(...args);
};
func.prototype = constructorFunction.prototype;
return new func();
};
newConstructorFunction.prototype = constructorFunction.prototype;
return function (target, propertyKey: string) {
resultTypeMap.set(
[target.constructor.name, propertyKey].toString(),
newConstructorFunction(),
);
//never return
};
}
function Param(name: string) {
return function (
target: any,
propertyKey: string | symbol,
parameterIndex: number,
) {
const existingParameters: [string, number][] =
Reflect.getOwnMetadata(paramMetadataKey, target, propertyKey) || [];
existingParameters.push([name, parameterIndex]);
Reflect.defineMetadata(
paramMetadataKey,
existingParameters,
target,
propertyKey,
);
};
}
function convertSQLParams(
args: any[],
target: any,
propertyKey: string,
decoratorSQL: string,
): [string, any[]] {
const queryValues = [];
let argsVal;
if (typeof args[0] === 'object') {
argsVal = new Map(
Object.getOwnPropertyNames(args[0]).map((valName) => [
valName,
args[0][valName],
]),
);
} else {
const existingParameters: [string, number][] = Reflect.getOwnMetadata(
paramMetadataKey,
target,
propertyKey,
);
argsVal = new Map(
existingParameters.map(([argName, argIdx]) => [argName, args[argIdx]]),
);
}
const regExp = /#{(\w+)}/;
let match;
while(match = regExp.exec(decoratorSQL)){
const [replaceTag, matchName] = match;
decoratorSQL = decoratorSQL.replace(new RegExp(replaceTag, 'g'), '?');
queryValues.push(argsVal.get(matchName));
}
// [...decoratorSQL.matchAll(regExp)].forEach((match) => {
// const [replaceTag, matchName] = match;
// decoratorSQL = decoratorSQL.replace(new RegExp(replaceTag, 'g'), '?');
// queryValues.push(argsVal.get(matchName));
// });
return [decoratorSQL, queryValues];
}
async function queryForExecute(
sql: string,
args: any[],
target,
propertyKey: string,
): Promise<ResultSetHeader> {
let sqlValues = [];
let newSql = sql;
if (args.length > 0) {
[newSql, sqlValues] = convertSQLParams(args, target, propertyKey, newSql);
}
const [result] = await speedPromisePool.query(newSql, sqlValues);
return <ResultSetHeader>result;
}
function Insert(sql: string) {
return function (
target,
propertyKey: string,
descriptor: PropertyDescriptor,
) {
descriptor.value = async (...args: any[]) => {
const result: ResultSetHeader = await queryForExecute(
sql,
args,
target,
propertyKey,
);
return result.insertId;
};
};
}
function Update(sql: string) {
return function (
target,
propertyKey: string,
descriptor: PropertyDescriptor,
) {
descriptor.value = async (...args: any[]) => {
const result: ResultSetHeader = await queryForExecute(
sql,
args,
target,
propertyKey,
);
return result.affectedRows;
};
};
}
function Select(sql: string) {
return function (
target,
propertyKey: string,
descriptor: PropertyDescriptor,
) {
descriptor.value = async (...args: any[]) => {
let newSql = sql;
let sqlValues = [];
if (args.length > 0) {
[newSql, sqlValues] = convertSQLParams(args, target, propertyKey, newSql);
}
const [rows] = await speedPromisePool.query(newSql, sqlValues);
if (Object.keys(rows).length === 0) {
return;
}
const records = [];
const resultType = resultTypeMap.get(
[target.constructor.name, propertyKey].toString(),
);
for (const rowIndex in rows) {
const entity = Object.create(resultType);
Object.getOwnPropertyNames(resultType).forEach(function (propertyRow) {
if (rows[rowIndex].hasOwnProperty(propertyRow)) {
Object.defineProperty(
entity,
propertyRow,
Object.getOwnPropertyDescriptor(rows[rowIndex], propertyRow),
);
}
});
records.push(entity);
}
return records;
};
};
}
export { log, Param, ResultType, Select, Insert, Update, Update as Delete };