query-core 0.6.6 → 0.6.7

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/src/log.ts ADDED
@@ -0,0 +1,360 @@
1
+ import { Attribute, FullDB, FullExecutor, FullTransaction, Statement, StringMap } from "./metadata"
2
+
3
+ export interface SimpleMap {
4
+ [key: string]: string | number | boolean | Date
5
+ }
6
+ export interface Logger {
7
+ level: number
8
+ debug(msg: string, m?: SimpleMap, ctx?: any): void
9
+ info(msg: string, m?: SimpleMap, ctx?: any): void
10
+ error(msg: string, m?: SimpleMap, ctx?: any): void
11
+ isDebugEnabled(): boolean
12
+ isInfoEnabled(): boolean
13
+ }
14
+ export function log(db: FullExecutor, isLog: boolean | undefined | null, logger: Logger, q?: string, result?: string, r?: string, duration?: string): FullExecutor {
15
+ if (!isLog) {
16
+ return db
17
+ }
18
+ if (q !== undefined && q != null && q.length > 0) {
19
+ if (!logger.isDebugEnabled()) {
20
+ return db
21
+ }
22
+ return new LogExecutor(db, logger.error, logger.debug, q, result, r, duration)
23
+ }
24
+ if (!logger.isInfoEnabled()) {
25
+ return db
26
+ }
27
+ return new LogExecutor(db, logger.error, logger.info, q, result, r, duration)
28
+ }
29
+ export function useLog(
30
+ db: FullDB,
31
+ isLog: boolean | undefined | null,
32
+ err: ((msg: string, m?: SimpleMap) => void) | undefined,
33
+ lg?: (msg: string, m?: SimpleMap) => void,
34
+ q?: string,
35
+ result?: string,
36
+ r?: string,
37
+ duration?: string,
38
+ ): FullDB {
39
+ if (!isLog) {
40
+ return db
41
+ }
42
+ if (err) {
43
+ return new LogDB(db, err, lg, q, result, r, duration)
44
+ }
45
+ return db
46
+ }
47
+ // tslint:disable-next-line:max-classes-per-file
48
+ export class LogExecutor implements FullExecutor {
49
+ constructor(
50
+ protected executor: FullExecutor,
51
+ protected error: (msg: string, m?: SimpleMap) => void,
52
+ lg?: (msg: string, m?: SimpleMap) => void,
53
+ q?: string,
54
+ result?: string,
55
+ r?: string,
56
+ duration?: string,
57
+ ) {
58
+ this.driver = executor.driver
59
+ this.duration = duration && duration.length > 0 ? duration : "duration"
60
+ this.sql = q === undefined ? "" : q
61
+ this.result = result !== undefined && result != null ? result : ""
62
+ this.return = r !== undefined && r != null ? r : "count"
63
+ this.log = lg
64
+ this.param = this.param.bind(this)
65
+ this.execute = this.execute.bind(this)
66
+ this.executeBatch = this.executeBatch.bind(this)
67
+ this.query = this.query.bind(this)
68
+ this.queryOne = this.queryOne.bind(this)
69
+ this.executeScalar = this.executeScalar.bind(this)
70
+ this.count = this.count.bind(this)
71
+ }
72
+ log?: (msg: string, m?: SimpleMap, ctx?: any) => void
73
+ driver: string
74
+ duration: string
75
+ sql: string
76
+ return: string
77
+ result: string
78
+ // err: string;
79
+ param(i: number): string {
80
+ return this.executor.param(i)
81
+ }
82
+ execute(sql: string, args?: any[], ctx?: any): Promise<number> {
83
+ const t1 = new Date()
84
+ return this.executor
85
+ .execute(sql, args, ctx)
86
+ .then((v) => {
87
+ setTimeout(() => {
88
+ if (this.log) {
89
+ const d = diff(t1)
90
+ const obj: SimpleMap = {}
91
+ if (this.sql.length > 0) {
92
+ obj[this.sql] = getString(sql, args)
93
+ }
94
+ if (this.return.length > 0) {
95
+ obj[this.return] = v
96
+ }
97
+ obj[this.duration] = d
98
+ this.log("query", obj)
99
+ }
100
+ }, 0)
101
+ return v
102
+ })
103
+ .catch((er) => {
104
+ setTimeout(() => {
105
+ const d = diff(t1)
106
+ const obj: SimpleMap = {}
107
+ if (this.sql.length > 0) {
108
+ obj[this.sql] = getString(sql, args)
109
+ }
110
+ obj[this.duration] = d
111
+ this.error("error query: " + buildString(er))
112
+ }, 0)
113
+ throw er
114
+ })
115
+ }
116
+ executeBatch(statements: Statement[], firstSuccess?: boolean, ctx?: any): Promise<number> {
117
+ const t1 = new Date()
118
+ return this.executor
119
+ .executeBatch(statements, firstSuccess, ctx)
120
+ .then((v) => {
121
+ setTimeout(() => {
122
+ if (this.log) {
123
+ const d = diff(t1)
124
+ const obj: SimpleMap = {}
125
+ if (this.sql.length > 0) {
126
+ obj[this.sql] = JSON.stringify(statements)
127
+ }
128
+ if (this.return.length > 0) {
129
+ obj[this.return] = v
130
+ }
131
+ obj[this.duration] = d
132
+ this.log("exec batch", obj)
133
+ }
134
+ }, 0)
135
+ return v
136
+ })
137
+ .catch((er) => {
138
+ setTimeout(() => {
139
+ const d = diff(t1)
140
+ const obj: SimpleMap = {}
141
+ if (this.sql.length > 0) {
142
+ obj[this.sql] = JSON.stringify(statements)
143
+ }
144
+ obj[this.duration] = d
145
+ this.error("error exec batch: " + buildString(er))
146
+ }, 0)
147
+ throw er
148
+ })
149
+ }
150
+ query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T[]> {
151
+ const t1 = new Date()
152
+ return this.executor
153
+ .query<T>(sql, args, m, bools, ctx)
154
+ .then((v) => {
155
+ setTimeout(() => {
156
+ if (this.log) {
157
+ const d = diff(t1)
158
+ const obj: SimpleMap = {}
159
+ if (this.sql.length > 0) {
160
+ obj[this.sql] = getString(sql, args)
161
+ }
162
+ if (this.result.length > 0) {
163
+ if (v && v.length > 0) {
164
+ obj[this.result] = JSON.stringify(v)
165
+ }
166
+ }
167
+ if (this.return.length > 0) {
168
+ obj[this.return] = v ? v.length : 0
169
+ }
170
+ obj[this.duration] = d
171
+ this.log("query", obj)
172
+ }
173
+ }, 0)
174
+ return v
175
+ })
176
+ .catch((er) => {
177
+ setTimeout(() => {
178
+ const d = diff(t1)
179
+ const obj: SimpleMap = {}
180
+ if (this.sql.length > 0) {
181
+ obj[this.sql] = getString(sql, args)
182
+ }
183
+ obj[this.duration] = d
184
+ this.error("error query: " + buildString(er))
185
+ }, 0)
186
+ throw er
187
+ })
188
+ }
189
+ queryOne<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T | null> {
190
+ const t1 = new Date()
191
+ return this.executor
192
+ .queryOne<T>(sql, args, m, bools, ctx)
193
+ .then((v) => {
194
+ setTimeout(() => {
195
+ if (this.log) {
196
+ const d = diff(t1)
197
+ const obj: SimpleMap = {}
198
+ if (this.sql.length > 0) {
199
+ obj[this.sql] = getString(sql, args)
200
+ }
201
+ if (this.result.length > 0) {
202
+ obj[this.result] = v ? JSON.stringify(v) : "null"
203
+ }
204
+ if (this.return.length > 0) {
205
+ obj[this.return] = v ? 1 : 0
206
+ }
207
+ obj[this.duration] = d
208
+ this.log("query one", obj)
209
+ }
210
+ }, 0)
211
+ return v
212
+ })
213
+ .catch((er) => {
214
+ setTimeout(() => {
215
+ const d = diff(t1)
216
+ const obj: SimpleMap = {}
217
+ if (this.sql.length > 0) {
218
+ obj[this.sql] = getString(sql, args)
219
+ }
220
+ obj[this.duration] = d
221
+ this.error("error query one: " + buildString(er))
222
+ }, 0)
223
+ throw er
224
+ })
225
+ }
226
+ executeScalar<T>(sql: string, args?: any[], ctx?: any): Promise<T> {
227
+ const t1 = new Date()
228
+ return this.executor
229
+ .executeScalar<T>(sql, args, ctx)
230
+ .then((v) => {
231
+ setTimeout(() => {
232
+ if (this.log) {
233
+ const d = diff(t1)
234
+ const obj: SimpleMap = {}
235
+ if (this.sql.length > 0) {
236
+ obj[this.sql] = getString(sql, args)
237
+ }
238
+ if (this.result.length > 0) {
239
+ obj[this.result] = v ? buildString(v) : "null"
240
+ }
241
+ if (this.return.length > 0) {
242
+ obj[this.return] = v ? 1 : 0
243
+ }
244
+ obj[this.duration] = d
245
+ this.log("exec scalar", obj)
246
+ }
247
+ }, 0)
248
+ return v
249
+ })
250
+ .catch((er) => {
251
+ setTimeout(() => {
252
+ const d = diff(t1)
253
+ const obj: SimpleMap = {}
254
+ if (this.sql.length > 0) {
255
+ obj[this.sql] = getString(sql, args)
256
+ }
257
+ obj[this.duration] = d
258
+ this.error("error exec scalar: " + buildString(er))
259
+ }, 0)
260
+ throw er
261
+ })
262
+ }
263
+ count(sql: string, args?: any[], ctx?: any): Promise<number> {
264
+ const t1 = new Date()
265
+ return this.executor
266
+ .count(sql, args)
267
+ .then((v) => {
268
+ setTimeout(() => {
269
+ if (this.log) {
270
+ const d = diff(t1)
271
+ const obj: SimpleMap = {}
272
+ if (this.sql.length > 0) {
273
+ obj[this.sql] = getString(sql, args)
274
+ }
275
+ if (this.return.length > 0) {
276
+ obj[this.return] = v
277
+ }
278
+ obj[this.duration] = d
279
+ this.log("count", obj)
280
+ }
281
+ }, 0)
282
+ return v
283
+ })
284
+ .catch((er) => {
285
+ setTimeout(() => {
286
+ const d = diff(t1)
287
+ const obj: SimpleMap = {}
288
+ if (this.sql.length > 0) {
289
+ obj[this.sql] = getString(sql, args)
290
+ }
291
+ obj[this.duration] = d
292
+ this.error("error count: " + buildString(er))
293
+ }, 0)
294
+ throw er
295
+ })
296
+ }
297
+ }
298
+ function buildString(v: any): string {
299
+ if (typeof v === "string") {
300
+ return v
301
+ } else {
302
+ return JSON.stringify(v)
303
+ }
304
+ }
305
+ function getString(sql: string, args?: any[]): string {
306
+ if (args && args.length > 0) {
307
+ return sql + " " + JSON.stringify(args)
308
+ } else {
309
+ return sql
310
+ }
311
+ }
312
+ export function diff(d1: Date): number {
313
+ const d2 = new Date()
314
+ return d2.getTime() - d1.getTime()
315
+ }
316
+ /*
317
+ const NS_PER_SEC = 1e9;
318
+ const NS_TO_MS = 1e6;
319
+ const getDurationInMilliseconds = (start: [number, number] | undefined) => {
320
+ const diff = process.hrtime(start);
321
+ return (diff[0] * NS_PER_SEC + diff[1]) / NS_TO_MS;
322
+ };
323
+ */
324
+ export class LogTransaction extends LogExecutor implements FullTransaction {
325
+ constructor(
326
+ protected tx: FullTransaction,
327
+ err: (msg: string, m?: SimpleMap) => void,
328
+ lg?: (msg: string, m?: SimpleMap) => void,
329
+ q?: string,
330
+ result?: string,
331
+ r?: string,
332
+ duration?: string,
333
+ ) {
334
+ super(tx, err, lg, q, result, r, duration)
335
+ }
336
+ commit(): Promise<void> {
337
+ return this.tx.commit()
338
+ }
339
+ rollback(): Promise<void> {
340
+ return this.tx.rollback()
341
+ }
342
+ }
343
+ export class LogDB extends LogExecutor implements FullDB {
344
+ constructor(
345
+ protected db: FullDB,
346
+ err: (msg: string, m?: SimpleMap) => void,
347
+ lg?: (msg: string, m?: SimpleMap) => void,
348
+ q?: string,
349
+ result?: string,
350
+ r?: string,
351
+ duration?: string,
352
+ ) {
353
+ super(db, err, lg, q, result, r, duration)
354
+ }
355
+ beginTransaction(): Promise<FullTransaction> {
356
+ return this.db.beginTransaction().then(tx => {
357
+ return new LogTransaction(tx, this.error, this.log, this.sql, this.result, this.return, this.duration)
358
+ })
359
+ }
360
+ }
package/src/metadata.ts CHANGED
@@ -62,3 +62,36 @@ export interface Attribute {
62
62
  export interface Attributes {
63
63
  [key: string]: Attribute
64
64
  }
65
+
66
+ export interface MinDB {
67
+ driver?: string
68
+ param(i: number): string
69
+ query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T[]>
70
+ }
71
+ export interface Executor {
72
+ driver: string
73
+ param(i: number): string
74
+ query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T[]>
75
+ execute(sql: string, args?: any[], ctx?: any): Promise<number>
76
+ executeBatch(statements: Statement[], firstSuccess?: boolean, ctx?: any): Promise<number>
77
+ }
78
+ export interface Transaction extends Executor {
79
+ commit(): Promise<void>
80
+ rollback(): Promise<void>
81
+ }
82
+ export interface DB extends Executor {
83
+ beginTransaction(): Promise<Transaction>
84
+ }
85
+
86
+ export interface FullExecutor extends Executor {
87
+ queryOne<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T | null>
88
+ executeScalar<T>(sql: string, args?: any[], ctx?: any): Promise<T>
89
+ count(sql: string, args?: any[], ctx?: any): Promise<number>
90
+ }
91
+ export interface FullTransaction extends FullExecutor {
92
+ commit(): Promise<void>
93
+ rollback(): Promise<void>
94
+ }
95
+ export interface FullDB extends FullExecutor {
96
+ beginTransaction(): Promise<FullTransaction>
97
+ }