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/lib/index.js +1 -0
- package/lib/log.js +339 -0
- package/lib/services.js +0 -292
- package/package.json +2 -2
- package/src/SearchBuilder.ts +4 -9
- package/src/index.ts +1 -0
- package/src/log.ts +360 -0
- package/src/metadata.ts +33 -0
- package/src/services.ts +4 -360
package/src/services.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { attributes, buildToDelete, buildToInsert, buildToUpdate, exist, buildMetadata, select
|
|
2
|
-
import { Attribute, Attributes, Statement, StringMap } from "./metadata"
|
|
1
|
+
import { attributes, buildToDelete, buildToInsert, buildToUpdate, exist, buildMetadata, select } from "./build"
|
|
2
|
+
import { Attribute, Attributes, Executor, MinDB, Statement, StringMap, Transaction } from "./metadata"
|
|
3
3
|
import { LikeType } from "./query"
|
|
4
4
|
import { SearchBuilder } from "./SearchBuilder"
|
|
5
|
-
// import { SearchResult } from "./search"
|
|
6
5
|
|
|
7
6
|
export interface Filter {
|
|
8
7
|
fields?: string[]
|
|
@@ -12,7 +11,7 @@ export interface Filter {
|
|
|
12
11
|
export type Load<T, ID> = (id: ID, ctx?: any) => Promise<T | null>
|
|
13
12
|
export type Get<T, ID> = Load<T, ID>
|
|
14
13
|
export function useGet<T, ID>(
|
|
15
|
-
db:
|
|
14
|
+
db: Executor,
|
|
16
15
|
table: string,
|
|
17
16
|
attrs: Attributes | string[],
|
|
18
17
|
fromDB?: (v: T) => T,
|
|
@@ -27,7 +26,7 @@ export class SqlLoader<T, ID> {
|
|
|
27
26
|
attributes: Attributes
|
|
28
27
|
bools?: Attribute[]
|
|
29
28
|
constructor(
|
|
30
|
-
protected db:
|
|
29
|
+
protected db: Executor,
|
|
31
30
|
protected table: string,
|
|
32
31
|
attrs: Attributes | string[],
|
|
33
32
|
protected fromDB?: (v: T) => T,
|
|
@@ -121,356 +120,6 @@ export class QueryRepository<T, ID> {
|
|
|
121
120
|
}
|
|
122
121
|
}
|
|
123
122
|
|
|
124
|
-
export interface Executor {
|
|
125
|
-
driver: string
|
|
126
|
-
param(i: number): string
|
|
127
|
-
execute(sql: string, args?: any[], ctx?: any): Promise<number>
|
|
128
|
-
executeBatch(statements: Statement[], firstSuccess?: boolean, ctx?: any): Promise<number>
|
|
129
|
-
query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T[]>
|
|
130
|
-
}
|
|
131
|
-
export interface Transaction extends Executor {
|
|
132
|
-
commit(): Promise<void>
|
|
133
|
-
rollback(): Promise<void>
|
|
134
|
-
}
|
|
135
|
-
export interface DB extends Executor {
|
|
136
|
-
beginTransaction(): Promise<Transaction>
|
|
137
|
-
}
|
|
138
|
-
// export type Manager = DB
|
|
139
|
-
export interface FullDB {
|
|
140
|
-
driver: string
|
|
141
|
-
param(i: number): string
|
|
142
|
-
execute(sql: string, args?: any[], ctx?: any): Promise<number>
|
|
143
|
-
executeBatch(statements: Statement[], firstSuccess?: boolean, ctx?: any): Promise<number>
|
|
144
|
-
query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T[]>
|
|
145
|
-
queryOne<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T | null>
|
|
146
|
-
executeScalar<T>(sql: string, args?: any[], ctx?: any): Promise<T>
|
|
147
|
-
count(sql: string, args?: any[], ctx?: any): Promise<number>
|
|
148
|
-
}
|
|
149
|
-
// export type ExtManager = FullDB
|
|
150
|
-
export interface SimpleMap {
|
|
151
|
-
[key: string]: string | number | boolean | Date
|
|
152
|
-
}
|
|
153
|
-
export interface Logger {
|
|
154
|
-
level: number
|
|
155
|
-
debug(msg: string, m?: SimpleMap, ctx?: any): void
|
|
156
|
-
info(msg: string, m?: SimpleMap, ctx?: any): void
|
|
157
|
-
error(msg: string, m?: SimpleMap, ctx?: any): void
|
|
158
|
-
isDebugEnabled(): boolean
|
|
159
|
-
isInfoEnabled(): boolean
|
|
160
|
-
}
|
|
161
|
-
export function log(db: FullDB, isLog: boolean | undefined | null, logger: Logger, q?: string, result?: string, r?: string, duration?: string): FullDB {
|
|
162
|
-
if (!isLog) {
|
|
163
|
-
return db
|
|
164
|
-
}
|
|
165
|
-
if (q !== undefined && q != null && q.length > 0) {
|
|
166
|
-
if (!logger.isDebugEnabled()) {
|
|
167
|
-
return db
|
|
168
|
-
}
|
|
169
|
-
return new LogManager(db, logger.error, logger.debug, q, result, r, duration)
|
|
170
|
-
}
|
|
171
|
-
if (!logger.isInfoEnabled()) {
|
|
172
|
-
return db
|
|
173
|
-
}
|
|
174
|
-
return new LogManager(db, logger.error, logger.info, q, result, r, duration)
|
|
175
|
-
}
|
|
176
|
-
export function useLog(
|
|
177
|
-
db: FullDB,
|
|
178
|
-
isLog: boolean | undefined | null,
|
|
179
|
-
err: ((msg: string, m?: SimpleMap) => void) | undefined,
|
|
180
|
-
lg?: (msg: string, m?: SimpleMap) => void,
|
|
181
|
-
q?: string,
|
|
182
|
-
result?: string,
|
|
183
|
-
r?: string,
|
|
184
|
-
duration?: string,
|
|
185
|
-
): FullDB {
|
|
186
|
-
if (!isLog) {
|
|
187
|
-
return db
|
|
188
|
-
}
|
|
189
|
-
if (err) {
|
|
190
|
-
return new LogManager(db, err, lg, q, result, r, duration)
|
|
191
|
-
}
|
|
192
|
-
return db
|
|
193
|
-
}
|
|
194
|
-
// tslint:disable-next-line:max-classes-per-file
|
|
195
|
-
export class LogManager implements FullDB {
|
|
196
|
-
constructor(
|
|
197
|
-
public db: FullDB,
|
|
198
|
-
err: (msg: string, m?: SimpleMap) => void,
|
|
199
|
-
lg?: (msg: string, m?: SimpleMap) => void,
|
|
200
|
-
q?: string,
|
|
201
|
-
result?: string,
|
|
202
|
-
r?: string,
|
|
203
|
-
duration?: string,
|
|
204
|
-
) {
|
|
205
|
-
this.driver = db.driver
|
|
206
|
-
this.duration = duration && duration.length > 0 ? duration : "duration"
|
|
207
|
-
this.sql = q === undefined ? "" : q
|
|
208
|
-
this.return = r !== undefined && r != null ? r : "count"
|
|
209
|
-
this.result = result !== undefined && result != null ? result : ""
|
|
210
|
-
// this.err = (er ? er : 'error');
|
|
211
|
-
this.log = lg
|
|
212
|
-
this.error = err
|
|
213
|
-
this.param = this.param.bind(this)
|
|
214
|
-
this.execute = this.execute.bind(this)
|
|
215
|
-
this.executeBatch = this.executeBatch.bind(this)
|
|
216
|
-
this.query = this.query.bind(this)
|
|
217
|
-
this.queryOne = this.queryOne.bind(this)
|
|
218
|
-
this.executeScalar = this.executeScalar.bind(this)
|
|
219
|
-
this.count = this.count.bind(this)
|
|
220
|
-
}
|
|
221
|
-
log?: (msg: string, m?: SimpleMap, ctx?: any) => void
|
|
222
|
-
error: (msg: string, m?: SimpleMap, ctx?: any) => void
|
|
223
|
-
driver: string
|
|
224
|
-
duration: string
|
|
225
|
-
sql: string
|
|
226
|
-
return: string
|
|
227
|
-
result: string
|
|
228
|
-
// err: string;
|
|
229
|
-
param(i: number): string {
|
|
230
|
-
return this.db.param(i)
|
|
231
|
-
}
|
|
232
|
-
execute(sql: string, args?: any[], ctx?: any): Promise<number> {
|
|
233
|
-
const t1 = new Date()
|
|
234
|
-
return this.db
|
|
235
|
-
.execute(sql, args, ctx)
|
|
236
|
-
.then((v) => {
|
|
237
|
-
setTimeout(() => {
|
|
238
|
-
if (this.log) {
|
|
239
|
-
const d = diff(t1)
|
|
240
|
-
const obj: SimpleMap = {}
|
|
241
|
-
if (this.sql.length > 0) {
|
|
242
|
-
obj[this.sql] = getString(sql, args)
|
|
243
|
-
}
|
|
244
|
-
if (this.return.length > 0) {
|
|
245
|
-
obj[this.return] = v
|
|
246
|
-
}
|
|
247
|
-
obj[this.duration] = d
|
|
248
|
-
this.log("query", obj)
|
|
249
|
-
}
|
|
250
|
-
}, 0)
|
|
251
|
-
return v
|
|
252
|
-
})
|
|
253
|
-
.catch((er) => {
|
|
254
|
-
setTimeout(() => {
|
|
255
|
-
const d = diff(t1)
|
|
256
|
-
const obj: SimpleMap = {}
|
|
257
|
-
if (this.sql.length > 0) {
|
|
258
|
-
obj[this.sql] = getString(sql, args)
|
|
259
|
-
}
|
|
260
|
-
obj[this.duration] = d
|
|
261
|
-
this.error("error query: " + buildString(er))
|
|
262
|
-
}, 0)
|
|
263
|
-
throw er
|
|
264
|
-
})
|
|
265
|
-
}
|
|
266
|
-
executeBatch(statements: Statement[], firstSuccess?: boolean, ctx?: any): Promise<number> {
|
|
267
|
-
const t1 = new Date()
|
|
268
|
-
return this.db
|
|
269
|
-
.executeBatch(statements, firstSuccess, ctx)
|
|
270
|
-
.then((v) => {
|
|
271
|
-
setTimeout(() => {
|
|
272
|
-
if (this.log) {
|
|
273
|
-
const d = diff(t1)
|
|
274
|
-
const obj: SimpleMap = {}
|
|
275
|
-
if (this.sql.length > 0) {
|
|
276
|
-
obj[this.sql] = JSON.stringify(statements)
|
|
277
|
-
}
|
|
278
|
-
if (this.return.length > 0) {
|
|
279
|
-
obj[this.return] = v
|
|
280
|
-
}
|
|
281
|
-
obj[this.duration] = d
|
|
282
|
-
this.log("exec batch", obj)
|
|
283
|
-
}
|
|
284
|
-
}, 0)
|
|
285
|
-
return v
|
|
286
|
-
})
|
|
287
|
-
.catch((er) => {
|
|
288
|
-
setTimeout(() => {
|
|
289
|
-
const d = diff(t1)
|
|
290
|
-
const obj: SimpleMap = {}
|
|
291
|
-
if (this.sql.length > 0) {
|
|
292
|
-
obj[this.sql] = JSON.stringify(statements)
|
|
293
|
-
}
|
|
294
|
-
obj[this.duration] = d
|
|
295
|
-
this.error("error exec batch: " + buildString(er))
|
|
296
|
-
}, 0)
|
|
297
|
-
throw er
|
|
298
|
-
})
|
|
299
|
-
}
|
|
300
|
-
query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T[]> {
|
|
301
|
-
const t1 = new Date()
|
|
302
|
-
return this.db
|
|
303
|
-
.query<T>(sql, args, m, bools, ctx)
|
|
304
|
-
.then((v) => {
|
|
305
|
-
setTimeout(() => {
|
|
306
|
-
if (this.log) {
|
|
307
|
-
const d = diff(t1)
|
|
308
|
-
const obj: SimpleMap = {}
|
|
309
|
-
if (this.sql.length > 0) {
|
|
310
|
-
obj[this.sql] = getString(sql, args)
|
|
311
|
-
}
|
|
312
|
-
if (this.result.length > 0) {
|
|
313
|
-
if (v && v.length > 0) {
|
|
314
|
-
obj[this.result] = JSON.stringify(v)
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
if (this.return.length > 0) {
|
|
318
|
-
obj[this.return] = v ? v.length : 0
|
|
319
|
-
}
|
|
320
|
-
obj[this.duration] = d
|
|
321
|
-
this.log("query", obj)
|
|
322
|
-
}
|
|
323
|
-
}, 0)
|
|
324
|
-
return v
|
|
325
|
-
})
|
|
326
|
-
.catch((er) => {
|
|
327
|
-
setTimeout(() => {
|
|
328
|
-
const d = diff(t1)
|
|
329
|
-
const obj: SimpleMap = {}
|
|
330
|
-
if (this.sql.length > 0) {
|
|
331
|
-
obj[this.sql] = getString(sql, args)
|
|
332
|
-
}
|
|
333
|
-
obj[this.duration] = d
|
|
334
|
-
this.error("error query: " + buildString(er))
|
|
335
|
-
}, 0)
|
|
336
|
-
throw er
|
|
337
|
-
})
|
|
338
|
-
}
|
|
339
|
-
queryOne<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T | null> {
|
|
340
|
-
const t1 = new Date()
|
|
341
|
-
return this.db
|
|
342
|
-
.queryOne<T>(sql, args, m, bools, ctx)
|
|
343
|
-
.then((v) => {
|
|
344
|
-
setTimeout(() => {
|
|
345
|
-
if (this.log) {
|
|
346
|
-
const d = diff(t1)
|
|
347
|
-
const obj: SimpleMap = {}
|
|
348
|
-
if (this.sql.length > 0) {
|
|
349
|
-
obj[this.sql] = getString(sql, args)
|
|
350
|
-
}
|
|
351
|
-
if (this.result.length > 0) {
|
|
352
|
-
obj[this.result] = v ? JSON.stringify(v) : "null"
|
|
353
|
-
}
|
|
354
|
-
if (this.return.length > 0) {
|
|
355
|
-
obj[this.return] = v ? 1 : 0
|
|
356
|
-
}
|
|
357
|
-
obj[this.duration] = d
|
|
358
|
-
this.log("query one", obj)
|
|
359
|
-
}
|
|
360
|
-
}, 0)
|
|
361
|
-
return v
|
|
362
|
-
})
|
|
363
|
-
.catch((er) => {
|
|
364
|
-
setTimeout(() => {
|
|
365
|
-
const d = diff(t1)
|
|
366
|
-
const obj: SimpleMap = {}
|
|
367
|
-
if (this.sql.length > 0) {
|
|
368
|
-
obj[this.sql] = getString(sql, args)
|
|
369
|
-
}
|
|
370
|
-
obj[this.duration] = d
|
|
371
|
-
this.error("error query one: " + buildString(er))
|
|
372
|
-
}, 0)
|
|
373
|
-
throw er
|
|
374
|
-
})
|
|
375
|
-
}
|
|
376
|
-
executeScalar<T>(sql: string, args?: any[], ctx?: any): Promise<T> {
|
|
377
|
-
const t1 = new Date()
|
|
378
|
-
return this.db
|
|
379
|
-
.executeScalar<T>(sql, args, ctx)
|
|
380
|
-
.then((v) => {
|
|
381
|
-
setTimeout(() => {
|
|
382
|
-
if (this.log) {
|
|
383
|
-
const d = diff(t1)
|
|
384
|
-
const obj: SimpleMap = {}
|
|
385
|
-
if (this.sql.length > 0) {
|
|
386
|
-
obj[this.sql] = getString(sql, args)
|
|
387
|
-
}
|
|
388
|
-
if (this.result.length > 0) {
|
|
389
|
-
obj[this.result] = v ? buildString(v) : "null"
|
|
390
|
-
}
|
|
391
|
-
if (this.return.length > 0) {
|
|
392
|
-
obj[this.return] = v ? 1 : 0
|
|
393
|
-
}
|
|
394
|
-
obj[this.duration] = d
|
|
395
|
-
this.log("exec scalar", obj)
|
|
396
|
-
}
|
|
397
|
-
}, 0)
|
|
398
|
-
return v
|
|
399
|
-
})
|
|
400
|
-
.catch((er) => {
|
|
401
|
-
setTimeout(() => {
|
|
402
|
-
const d = diff(t1)
|
|
403
|
-
const obj: SimpleMap = {}
|
|
404
|
-
if (this.sql.length > 0) {
|
|
405
|
-
obj[this.sql] = getString(sql, args)
|
|
406
|
-
}
|
|
407
|
-
obj[this.duration] = d
|
|
408
|
-
this.error("error exec scalar: " + buildString(er))
|
|
409
|
-
}, 0)
|
|
410
|
-
throw er
|
|
411
|
-
})
|
|
412
|
-
}
|
|
413
|
-
count(sql: string, args?: any[], ctx?: any): Promise<number> {
|
|
414
|
-
const t1 = new Date()
|
|
415
|
-
return this.db
|
|
416
|
-
.count(sql, args)
|
|
417
|
-
.then((v) => {
|
|
418
|
-
setTimeout(() => {
|
|
419
|
-
if (this.log) {
|
|
420
|
-
const d = diff(t1)
|
|
421
|
-
const obj: SimpleMap = {}
|
|
422
|
-
if (this.sql.length > 0) {
|
|
423
|
-
obj[this.sql] = getString(sql, args)
|
|
424
|
-
}
|
|
425
|
-
if (this.return.length > 0) {
|
|
426
|
-
obj[this.return] = v
|
|
427
|
-
}
|
|
428
|
-
obj[this.duration] = d
|
|
429
|
-
this.log("count", obj)
|
|
430
|
-
}
|
|
431
|
-
}, 0)
|
|
432
|
-
return v
|
|
433
|
-
})
|
|
434
|
-
.catch((er) => {
|
|
435
|
-
setTimeout(() => {
|
|
436
|
-
const d = diff(t1)
|
|
437
|
-
const obj: SimpleMap = {}
|
|
438
|
-
if (this.sql.length > 0) {
|
|
439
|
-
obj[this.sql] = getString(sql, args)
|
|
440
|
-
}
|
|
441
|
-
obj[this.duration] = d
|
|
442
|
-
this.error("error count: " + buildString(er))
|
|
443
|
-
}, 0)
|
|
444
|
-
throw er
|
|
445
|
-
})
|
|
446
|
-
}
|
|
447
|
-
}
|
|
448
|
-
function buildString(v: any): string {
|
|
449
|
-
if (typeof v === "string") {
|
|
450
|
-
return v
|
|
451
|
-
} else {
|
|
452
|
-
return JSON.stringify(v)
|
|
453
|
-
}
|
|
454
|
-
}
|
|
455
|
-
function getString(sql: string, args?: any[]): string {
|
|
456
|
-
if (args && args.length > 0) {
|
|
457
|
-
return sql + " " + JSON.stringify(args)
|
|
458
|
-
} else {
|
|
459
|
-
return sql
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
export function diff(d1: Date): number {
|
|
463
|
-
const d2 = new Date()
|
|
464
|
-
return d2.getTime() - d1.getTime()
|
|
465
|
-
}
|
|
466
|
-
/*
|
|
467
|
-
const NS_PER_SEC = 1e9;
|
|
468
|
-
const NS_TO_MS = 1e6;
|
|
469
|
-
const getDurationInMilliseconds = (start: [number, number] | undefined) => {
|
|
470
|
-
const diff = process.hrtime(start);
|
|
471
|
-
return (diff[0] * NS_PER_SEC + diff[1]) / NS_TO_MS;
|
|
472
|
-
};
|
|
473
|
-
*/
|
|
474
123
|
// tslint:disable-next-line:max-classes-per-file
|
|
475
124
|
export class SqlWriter<T> {
|
|
476
125
|
protected primaryKeys: Attribute[]
|
|
@@ -757,11 +406,6 @@ export class SqlRepository<T, ID, S> extends SqlSearchWriter<T, S> {
|
|
|
757
406
|
}
|
|
758
407
|
export const Repository = SqlRepository
|
|
759
408
|
|
|
760
|
-
interface MinDB {
|
|
761
|
-
driver?: string
|
|
762
|
-
param(i: number): string
|
|
763
|
-
query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T[]>
|
|
764
|
-
}
|
|
765
409
|
// tslint:disable-next-line:max-classes-per-file
|
|
766
410
|
export class Query<T, ID, S> extends SearchBuilder<T, S> {
|
|
767
411
|
constructor(
|