query-core 0.1.17 → 0.1.20

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.
@@ -54,7 +54,7 @@ var SearchBuilder = (function () {
54
54
  var sn = s[st];
55
55
  delete s[st];
56
56
  var x = (this.provider === exports.postgres ? 'ilike' : this.buildParam);
57
- var q2 = this.buildQuery(s, x, this.table, this.attributes, sn, fields, this.q, this.excluding, this.buildSort);
57
+ var q2 = this.buildQuery(s, x, sn, this.buildSort, this.attributes, this.table, fields, this.q, this.excluding);
58
58
  if (!q2) {
59
59
  throw new Error('Cannot build query');
60
60
  }
package/lib/query.js CHANGED
@@ -56,7 +56,7 @@ function buildDollarParam(i) {
56
56
  return '$' + i;
57
57
  }
58
58
  exports.buildDollarParam = buildDollarParam;
59
- function buildQuery(filter, bparam, table, attrs, sort, fields, sq, strExcluding, buildSort3) {
59
+ function buildQuery(filter, bparam, sort, buildSort3, attrs, table, fields, sq, strExcluding) {
60
60
  if (!table || !attrs) {
61
61
  return undefined;
62
62
  }
package/lib/services.js CHANGED
@@ -14,6 +14,12 @@ var __extends = (this && this.__extends) || (function () {
14
14
  })();
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
16
  var build_1 = require("./build");
17
+ function useGet(table, q, attrs, param, fromDB) {
18
+ var l = new SqlLoader(table, q, attrs, param, fromDB);
19
+ return l.load;
20
+ }
21
+ exports.useGet = useGet;
22
+ exports.useLoad = useGet;
17
23
  var SqlLoader = (function () {
18
24
  function SqlLoader(table, query, attrs, param, fromDB) {
19
25
  this.table = table;
@@ -91,6 +97,252 @@ var SqlSearchLoader = (function (_super) {
91
97
  return SqlSearchLoader;
92
98
  }(SqlLoader));
93
99
  exports.SqlSearchLoader = SqlSearchLoader;
100
+ function log(db, isLog, logger, q, result, r, duration) {
101
+ if (!isLog) {
102
+ return db;
103
+ }
104
+ if (q !== undefined && q != null && q.length > 0) {
105
+ if (!logger.isDebugEnabled()) {
106
+ return db;
107
+ }
108
+ return new LogManager(db, logger.error, logger.debug, q, result, r, duration);
109
+ }
110
+ if (!logger.isInfoEnabled()) {
111
+ return db;
112
+ }
113
+ return new LogManager(db, logger.error, logger.info, q, result, r, duration);
114
+ }
115
+ exports.log = log;
116
+ function useLog(db, isLog, err, lg, q, result, r, duration) {
117
+ if (!isLog) {
118
+ return db;
119
+ }
120
+ if (err) {
121
+ return new LogManager(db, err, lg, q, result, r, duration);
122
+ }
123
+ return db;
124
+ }
125
+ exports.useLog = useLog;
126
+ var LogManager = (function () {
127
+ function LogManager(db, err, lg, q, result, r, duration) {
128
+ this.db = db;
129
+ this.driver = db.driver;
130
+ this.duration = (duration && duration.length > 0 ? duration : 'duration');
131
+ this.sql = (q === undefined ? '' : q);
132
+ this.return = (r !== undefined && r != null ? r : 'count');
133
+ this.result = (result !== undefined && result != null ? result : '');
134
+ this.log = lg;
135
+ this.error = err;
136
+ this.param = this.param.bind(this);
137
+ this.exec = this.exec.bind(this);
138
+ this.execBatch = this.execBatch.bind(this);
139
+ this.query = this.query.bind(this);
140
+ this.queryOne = this.queryOne.bind(this);
141
+ this.execScalar = this.execScalar.bind(this);
142
+ this.count = this.count.bind(this);
143
+ }
144
+ LogManager.prototype.param = function (i) {
145
+ return this.db.param(i);
146
+ };
147
+ LogManager.prototype.exec = function (sql, args, ctx) {
148
+ var t1 = new Date();
149
+ return this.db.exec(sql, args, ctx);
150
+ };
151
+ LogManager.prototype.execBatch = function (statements, firstSuccess, ctx) {
152
+ var _this = this;
153
+ var t1 = new Date();
154
+ return this.db.execBatch(statements, firstSuccess, ctx).then(function (v) {
155
+ setTimeout(function () {
156
+ if (_this.log) {
157
+ var d = diff(t1);
158
+ var obj = {};
159
+ if (_this.sql.length > 0) {
160
+ obj[_this.sql] = JSON.stringify(statements);
161
+ }
162
+ if (_this.return.length > 0) {
163
+ obj[_this.return] = v;
164
+ }
165
+ obj[_this.duration] = d;
166
+ _this.log('exec batch', obj);
167
+ }
168
+ }, 0);
169
+ return v;
170
+ }).catch(function (er) {
171
+ setTimeout(function () {
172
+ var d = diff(t1);
173
+ var obj = {};
174
+ if (_this.sql.length > 0) {
175
+ obj[_this.sql] = JSON.stringify(statements);
176
+ }
177
+ obj[_this.duration] = d;
178
+ _this.error('error exec batch: ' + buildString(er));
179
+ }, 0);
180
+ throw er;
181
+ });
182
+ };
183
+ LogManager.prototype.query = function (sql, args, m, bools, ctx) {
184
+ var _this = this;
185
+ var t1 = new Date();
186
+ return this.db.query(sql, args, m, bools, ctx).then(function (v) {
187
+ setTimeout(function () {
188
+ if (_this.log) {
189
+ var d = diff(t1);
190
+ var obj = {};
191
+ if (_this.sql.length > 0) {
192
+ obj[_this.sql] = getString(sql, args);
193
+ }
194
+ if (_this.result.length > 0) {
195
+ if (v && v.length > 0) {
196
+ obj[_this.result] = JSON.stringify(v);
197
+ }
198
+ }
199
+ if (_this.return.length > 0) {
200
+ obj[_this.return] = v ? v.length : 0;
201
+ }
202
+ obj[_this.duration] = d;
203
+ _this.log('query', obj);
204
+ }
205
+ }, 0);
206
+ return v;
207
+ }).catch(function (er) {
208
+ setTimeout(function () {
209
+ var d = diff(t1);
210
+ var obj = {};
211
+ if (_this.sql.length > 0) {
212
+ obj[_this.sql] = getString(sql, args);
213
+ }
214
+ obj[_this.duration] = d;
215
+ _this.error('error query: ' + buildString(er));
216
+ }, 0);
217
+ throw er;
218
+ });
219
+ };
220
+ LogManager.prototype.queryOne = function (sql, args, m, bools, ctx) {
221
+ var _this = this;
222
+ var t1 = new Date();
223
+ return this.db.queryOne(sql, args, m, bools, ctx).then(function (v) {
224
+ setTimeout(function () {
225
+ if (_this.log) {
226
+ var d = diff(t1);
227
+ var obj = {};
228
+ if (_this.sql.length > 0) {
229
+ obj[_this.sql] = getString(sql, args);
230
+ }
231
+ if (_this.result.length > 0) {
232
+ obj[_this.result] = v ? JSON.stringify(v) : 'null';
233
+ }
234
+ if (_this.return.length > 0) {
235
+ obj[_this.return] = v ? 1 : 0;
236
+ }
237
+ obj[_this.duration] = d;
238
+ _this.log('query one', obj);
239
+ }
240
+ }, 0);
241
+ return v;
242
+ }).catch(function (er) {
243
+ setTimeout(function () {
244
+ var d = diff(t1);
245
+ var obj = {};
246
+ if (_this.sql.length > 0) {
247
+ obj[_this.sql] = getString(sql, args);
248
+ }
249
+ obj[_this.duration] = d;
250
+ _this.error('error query one: ' + buildString(er));
251
+ }, 0);
252
+ throw er;
253
+ });
254
+ };
255
+ LogManager.prototype.execScalar = function (sql, args, ctx) {
256
+ var _this = this;
257
+ var t1 = new Date();
258
+ return this.db.execScalar(sql, args, ctx).then(function (v) {
259
+ setTimeout(function () {
260
+ if (_this.log) {
261
+ var d = diff(t1);
262
+ var obj = {};
263
+ if (_this.sql.length > 0) {
264
+ obj[_this.sql] = getString(sql, args);
265
+ }
266
+ if (_this.result.length > 0) {
267
+ obj[_this.result] = v ? buildString(v) : 'null';
268
+ }
269
+ if (_this.return.length > 0) {
270
+ obj[_this.return] = v ? 1 : 0;
271
+ }
272
+ obj[_this.duration] = d;
273
+ _this.log('exec scalar', obj);
274
+ }
275
+ }, 0);
276
+ return v;
277
+ }).catch(function (er) {
278
+ setTimeout(function () {
279
+ var d = diff(t1);
280
+ var obj = {};
281
+ if (_this.sql.length > 0) {
282
+ obj[_this.sql] = getString(sql, args);
283
+ }
284
+ obj[_this.duration] = d;
285
+ _this.error('error exec scalar: ' + buildString(er));
286
+ }, 0);
287
+ throw er;
288
+ });
289
+ };
290
+ LogManager.prototype.count = function (sql, args, ctx) {
291
+ var _this = this;
292
+ var t1 = new Date();
293
+ return this.db.count(sql, args).then(function (v) {
294
+ setTimeout(function () {
295
+ if (_this.log) {
296
+ var d = diff(t1);
297
+ var obj = {};
298
+ if (_this.sql.length > 0) {
299
+ obj[_this.sql] = getString(sql, args);
300
+ }
301
+ if (_this.return.length > 0) {
302
+ obj[_this.return] = v;
303
+ }
304
+ obj[_this.duration] = d;
305
+ _this.log('count', obj);
306
+ }
307
+ }, 0);
308
+ return v;
309
+ }).catch(function (er) {
310
+ setTimeout(function () {
311
+ var d = diff(t1);
312
+ var obj = {};
313
+ if (_this.sql.length > 0) {
314
+ obj[_this.sql] = getString(sql, args);
315
+ }
316
+ obj[_this.duration] = d;
317
+ _this.error('error count: ' + buildString(er));
318
+ }, 0);
319
+ throw er;
320
+ });
321
+ };
322
+ return LogManager;
323
+ }());
324
+ exports.LogManager = LogManager;
325
+ function buildString(v) {
326
+ if (typeof v === 'string') {
327
+ return v;
328
+ }
329
+ else {
330
+ return JSON.stringify(v);
331
+ }
332
+ }
333
+ function getString(sql, args) {
334
+ if (args && args.length > 0) {
335
+ return sql + ' ' + JSON.stringify(args);
336
+ }
337
+ else {
338
+ return sql;
339
+ }
340
+ }
341
+ function diff(d1) {
342
+ var d2 = new Date();
343
+ return d2.getTime() - d1.getTime();
344
+ }
345
+ exports.diff = diff;
94
346
  var SqlWriter = (function (_super) {
95
347
  __extends(SqlWriter, _super);
96
348
  function SqlWriter(manager, table, attrs, toDB, fromDB) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "query-core",
3
- "version": "0.1.17",
3
+ "version": "0.1.20",
4
4
  "description": "query",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
@@ -10,7 +10,7 @@ export const sqlite = 'sqlite';
10
10
  export class SearchBuilder<T, S> {
11
11
  map?: StringMap;
12
12
  bools?: Attribute[];
13
- buildQuery: (s: S, bparam: LikeType|((i: number ) => string), table?: string, attrs?: Attributes, sort?: string, fields?: string[], sq?: string, strExcluding?: string, buildSort3?: (sort?: string, map?: Attributes|StringMap) => string) => Statement|undefined;
13
+ buildQuery: (s: S, bparam: LikeType|((i: number ) => string), sort?: string, buildSort3?: (sort?: string, map?: Attributes|StringMap) => string, attrs?: Attributes, table?: string, fields?: string[], sq?: string, strExcluding?: string) => Statement|undefined;
14
14
  q?: string;
15
15
  excluding?: string;
16
16
  buildSort?: (sort?: string, map?: Attributes|StringMap) => string;
@@ -19,7 +19,7 @@ export class SearchBuilder<T, S> {
19
19
  constructor(public query: (sql: string, args?: any[], m?: StringMap, bools?: Attribute[]) => Promise<T[]>, public table: string,
20
20
  public attributes?: Attributes,
21
21
  public provider?: string,
22
- buildQ?: (s: S, bparam: LikeType|((i: number ) => string), table?: string, attrs?: Attributes, sort?: string, fields?: string[], sq?: string, strExcluding?: string, buildSort3?: (sort?: string, map?: Attributes|StringMap) => string) => Statement|undefined,
22
+ buildQ?: (s: S, bparam: LikeType|((i: number ) => string), sort?: string, buildSort3?: (sort?: string, map?: Attributes|StringMap) => string, attrs?: Attributes, table?: string, fields?: string[], sq?: string, strExcluding?: string) => Statement|undefined,
23
23
  public fromDB?: (v: T) => T,
24
24
  public sort?: string,
25
25
  q?: string,
@@ -62,7 +62,7 @@ export class SearchBuilder<T, S> {
62
62
  const sn = (s as any)[st] as string;
63
63
  delete (s as any)[st];
64
64
  const x = (this.provider === postgres ? 'ilike' : this.buildParam);
65
- const q2 = this.buildQuery(s, x, this.table, this.attributes, sn, fields, this.q, this.excluding, this.buildSort);
65
+ const q2 = this.buildQuery(s, x, sn, this.buildSort, this.attributes, this.table, fields, this.q, this.excluding);
66
66
  if (!q2) {
67
67
  throw new Error('Cannot build query');
68
68
  }
package/src/batch.ts CHANGED
@@ -26,6 +26,7 @@ export class SqlInserter<T> {
26
26
  }
27
27
  }
28
28
  }
29
+ // tslint:disable-next-line:max-classes-per-file
29
30
  export class SqlUpdater<T> {
30
31
  version?: string;
31
32
  constructor(public exec: (sql: string, args?: any[]) => Promise<number>, public table: string, public attributes: Attributes, public param: (i: number) => string, public map?: (v: T) => T) {
@@ -51,6 +52,7 @@ export class SqlUpdater<T> {
51
52
  }
52
53
  }
53
54
  }
55
+ // tslint:disable-next-line:max-classes-per-file
54
56
  export class SqlBatchInserter<T> {
55
57
  version?: string;
56
58
  constructor(public exec: (sql: string, args?: any[]) => Promise<number>, public table: string, public attributes: Attributes, public param: ((i: number) => string)|boolean, public map?: (v: T) => T) {
@@ -80,6 +82,7 @@ export class SqlBatchInserter<T> {
80
82
  }
81
83
  }
82
84
  }
85
+ // tslint:disable-next-line:max-classes-per-file
83
86
  export class SqlBatchUpdater<T> {
84
87
  version?: string;
85
88
  constructor(public execBatch: (statements: Statement[]) => Promise<number>, public table: string, public attributes: Attributes, public param: (i: number) => string, protected notSkipInvalid?: boolean, public map?: (v: T) => T) {
package/src/index.ts CHANGED
@@ -54,6 +54,7 @@ export interface Config {
54
54
  export class resource {
55
55
  static string?: boolean;
56
56
  }
57
+ // tslint:disable-next-line:max-classes-per-file
57
58
  export class Loader<T> {
58
59
  map?: StringMap;
59
60
  constructor(public query: (sql: string, args?: any[], m?: StringMap, bools?: Attribute[]) => Promise<T[]>, public sql: string, m?: StringMap, public bools?: Attribute[]) {
package/src/query.ts CHANGED
@@ -52,7 +52,7 @@ export function buildOracleParam(i: number): string {
52
52
  export function buildDollarParam(i: number): string {
53
53
  return '$' + i;
54
54
  }
55
- export function buildQuery<S>(filter: S, bparam: LikeType|((i: number ) => string), table?: string, attrs?: Attributes, sort?: string, fields?: string[], sq?: string, strExcluding?: string, buildSort3?: (sort?: string, map?: Attributes|StringMap) => string): Statement|undefined {
55
+ export function buildQuery<S>(filter: S, bparam: LikeType|((i: number ) => string), sort?: string, buildSort3?: (sort?: string, map?: Attributes|StringMap) => string, attrs?: Attributes, table?: string, fields?: string[], sq?: string, strExcluding?: string): Statement|undefined {
56
56
  if (!table || !attrs) {
57
57
  return undefined;
58
58
  }
package/src/services.ts CHANGED
@@ -7,6 +7,17 @@ export interface Filter {
7
7
  sort?: string;
8
8
  q?: string;
9
9
  }
10
+ export type Load<T, ID> = (id: ID, ctx?: any) => Promise<T|null>;
11
+ export type Get<T, ID> = Load<T, ID>;
12
+ export function useGet<T, ID>(table: string,
13
+ q: <K>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any) => Promise<K[]>,
14
+ attrs: Attributes|string[],
15
+ param: (i: number) => string,
16
+ fromDB?: (v: T) => T): Load<T, ID> {
17
+ const l = new SqlLoader<T, ID>(table, q, attrs, param, fromDB);
18
+ return l.load;
19
+ }
20
+ export const useLoad = useGet;
10
21
  export class SqlLoader<T, ID> {
11
22
  primaryKeys: Attribute[];
12
23
  map?: StringMap;
@@ -69,6 +80,7 @@ export class SqlLoader<T, ID> {
69
80
  return this.query(stmt.query, stmt.params, this.map, undefined, ctx).then(res => (!res || res.length === 0) ? false : true);
70
81
  }
71
82
  }
83
+ // tslint:disable-next-line:max-classes-per-file
72
84
  export class SqlSearchLoader<T, ID, S extends Filter> extends SqlLoader<T, ID> {
73
85
  constructor(
74
86
  protected find: (s: S, limit?: number, offset?: number|string, fields?: string[]) => Promise<SearchResult<T>>,
@@ -91,13 +103,280 @@ export interface Manager {
91
103
  execBatch(statements: Statement[], firstSuccess?: boolean, ctx?: any): Promise<number>;
92
104
  query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T[]>;
93
105
  }
94
- export interface DB {
106
+ export type DB = Manager;
107
+ export interface ExtManager {
95
108
  driver: string;
96
109
  param(i: number): string;
97
110
  exec(sql: string, args?: any[], ctx?: any): Promise<number>;
98
111
  execBatch(statements: Statement[], firstSuccess?: boolean, ctx?: any): Promise<number>;
99
112
  query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T[]>;
113
+ queryOne<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T|null>;
114
+ execScalar<T>(sql: string, args?: any[], ctx?: any): Promise<T>;
115
+ count(sql: string, args?: any[], ctx?: any): Promise<number>;
116
+ }
117
+ export interface SimpleMap {
118
+ [key: string]: string|number|boolean|Date;
119
+ }
120
+ export interface Logger {
121
+ level: number;
122
+ debug(msg: string, m?: SimpleMap, ctx?: any): void;
123
+ info(msg: string, m?: SimpleMap, ctx?: any): void;
124
+ error(msg: string, m?: SimpleMap, ctx?: any): void;
125
+ isDebugEnabled(): boolean;
126
+ isInfoEnabled(): boolean;
127
+ }
128
+ export function log(db: ExtManager, isLog: boolean|undefined|null, logger: Logger, q?: string, result?: string, r?: string, duration?: string): ExtManager {
129
+ if (!isLog) {
130
+ return db;
131
+ }
132
+ if (q !== undefined && q != null && q.length > 0) {
133
+ if (!logger.isDebugEnabled()) {
134
+ return db;
135
+ }
136
+ return new LogManager(db, logger.error, logger.debug, q, result, r, duration);
137
+ }
138
+ if (!logger.isInfoEnabled()) {
139
+ return db;
140
+ }
141
+ return new LogManager(db, logger.error, logger.info, q, result, r, duration);
142
+ }
143
+ export function useLog(db: ExtManager, isLog: boolean|undefined|null, err: ((msg: string, m?: SimpleMap) => void)|undefined, lg?: (msg: string, m?: SimpleMap) => void, q?: string, result?: string, r?: string, duration?: string): ExtManager {
144
+ if (!isLog) {
145
+ return db;
146
+ }
147
+ if (err) {
148
+ return new LogManager(db, err, lg, q, result, r, duration);
149
+ }
150
+ return db;
151
+ }
152
+ // tslint:disable-next-line:max-classes-per-file
153
+ export class LogManager implements ExtManager {
154
+ constructor(public db: ExtManager, err: (msg: string, m?: SimpleMap) => void, lg?: (msg: string, m?: SimpleMap) => void, q?: string, result?: string, r?: string, duration?: string) {
155
+ this.driver = db.driver;
156
+ this.duration = (duration && duration.length > 0 ? duration : 'duration');
157
+ this.sql = (q === undefined ? '' : q);
158
+ this.return = (r !== undefined && r != null ? r : 'count');
159
+ this.result = (result !== undefined && result != null ? result : '');
160
+ // this.err = (er ? er : 'error');
161
+ this.log = lg;
162
+ this.error = err;
163
+ this.param = this.param.bind(this);
164
+ this.exec = this.exec.bind(this);
165
+ this.execBatch = this.execBatch.bind(this);
166
+ this.query = this.query.bind(this);
167
+ this.queryOne = this.queryOne.bind(this);
168
+ this.execScalar = this.execScalar.bind(this);
169
+ this.count = this.count.bind(this);
170
+ }
171
+ log?: (msg: string, m?: SimpleMap, ctx?: any) => void;
172
+ error: (msg: string, m?: SimpleMap, ctx?: any) => void;
173
+ driver: string;
174
+ duration: string;
175
+ sql: string;
176
+ return: string;
177
+ result: string;
178
+ // err: string;
179
+ param(i: number): string {
180
+ return this.db.param(i);
181
+ }
182
+ exec(sql: string, args?: any[], ctx?: any): Promise<number> {
183
+ const t1 = new Date();
184
+ return this.db.exec(sql, args, ctx);
185
+ }
186
+ execBatch(statements: Statement[], firstSuccess?: boolean, ctx?: any): Promise<number> {
187
+ const t1 = new Date();
188
+ return this.db.execBatch(statements, firstSuccess, ctx).then(v => {
189
+ setTimeout(() => {
190
+ if (this.log) {
191
+ const d = diff(t1);
192
+ const obj: SimpleMap = {} ;
193
+ if (this.sql.length > 0) {
194
+ obj[this.sql] = JSON.stringify(statements);
195
+ }
196
+ if (this.return.length > 0) {
197
+ obj[this.return] = v;
198
+ }
199
+ obj[this.duration] = d;
200
+ this.log('exec batch', obj);
201
+ }
202
+ }, 0);
203
+ return v;
204
+ }).catch(er => {
205
+ setTimeout(() => {
206
+ const d = diff(t1);
207
+ const obj: SimpleMap = {};
208
+ if (this.sql.length > 0) {
209
+ obj[this.sql] = JSON.stringify(statements);
210
+ }
211
+ obj[this.duration] = d;
212
+ this.error('error exec batch: ' + buildString(er));
213
+ }, 0);
214
+ throw er;
215
+ });
216
+ }
217
+ query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T[]> {
218
+ const t1 = new Date();
219
+ return this.db.query<T>(sql, args, m, bools, ctx).then(v => {
220
+ setTimeout(() => {
221
+ if (this.log) {
222
+ const d = diff(t1);
223
+ const obj: SimpleMap = {} ;
224
+ if (this.sql.length > 0) {
225
+ obj[this.sql] = getString(sql, args);
226
+ }
227
+ if (this.result.length > 0) {
228
+ if (v && v.length > 0) {
229
+ obj[this.result] = JSON.stringify(v);
230
+ }
231
+ }
232
+ if (this.return.length > 0) {
233
+ obj[this.return] = v ? v.length : 0;
234
+ }
235
+ obj[this.duration] = d;
236
+ this.log('query', obj);
237
+ }
238
+ }, 0);
239
+ return v;
240
+ }).catch(er => {
241
+ setTimeout(() => {
242
+ const d = diff(t1);
243
+ const obj: SimpleMap = {};
244
+ if (this.sql.length > 0) {
245
+ obj[this.sql] = getString(sql, args);
246
+ }
247
+ obj[this.duration] = d;
248
+ this.error('error query: ' + buildString(er));
249
+ }, 0);
250
+ throw er;
251
+ });
252
+ }
253
+ queryOne<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T|null> {
254
+ const t1 = new Date();
255
+ return this.db.queryOne<T>(sql, args, m, bools, ctx).then(v => {
256
+ setTimeout(() => {
257
+ if (this.log) {
258
+ const d = diff(t1);
259
+ const obj: SimpleMap = {} ;
260
+ if (this.sql.length > 0) {
261
+ obj[this.sql] = getString(sql, args);
262
+ }
263
+ if (this.result.length > 0) {
264
+ obj[this.result] = v ? JSON.stringify(v) : 'null';
265
+ }
266
+ if (this.return.length > 0) {
267
+ obj[this.return] = v ? 1 : 0;
268
+ }
269
+ obj[this.duration] = d;
270
+ this.log('query one', obj);
271
+ }
272
+ }, 0);
273
+ return v;
274
+ }).catch(er => {
275
+ setTimeout(() => {
276
+ const d = diff(t1);
277
+ const obj: SimpleMap = {};
278
+ if (this.sql.length > 0) {
279
+ obj[this.sql] = getString(sql, args);
280
+ }
281
+ obj[this.duration] = d;
282
+ this.error('error query one: ' + buildString(er));
283
+ }, 0);
284
+ throw er;
285
+ });
286
+ }
287
+ execScalar<T>(sql: string, args?: any[], ctx?: any): Promise<T> {
288
+ const t1 = new Date();
289
+ return this.db.execScalar<T>(sql, args, ctx).then(v => {
290
+ setTimeout(() => {
291
+ if (this.log) {
292
+ const d = diff(t1);
293
+ const obj: SimpleMap = {} ;
294
+ if (this.sql.length > 0) {
295
+ obj[this.sql] = getString(sql, args);
296
+ }
297
+ if (this.result.length > 0) {
298
+ obj[this.result] = v ? buildString(v) : 'null';
299
+ }
300
+ if (this.return.length > 0) {
301
+ obj[this.return] = v ? 1 : 0;
302
+ }
303
+ obj[this.duration] = d;
304
+ this.log('exec scalar', obj);
305
+ }
306
+ }, 0);
307
+ return v;
308
+ }).catch(er => {
309
+ setTimeout(() => {
310
+ const d = diff(t1);
311
+ const obj: SimpleMap = {};
312
+ if (this.sql.length > 0) {
313
+ obj[this.sql] = getString(sql, args);
314
+ }
315
+ obj[this.duration] = d;
316
+ this.error('error exec scalar: ' + buildString(er));
317
+ }, 0);
318
+ throw er;
319
+ });
320
+ }
321
+ count(sql: string, args?: any[], ctx?: any): Promise<number> {
322
+ const t1 = new Date();
323
+ return this.db.count(sql, args).then(v => {
324
+ setTimeout(() => {
325
+ if (this.log) {
326
+ const d = diff(t1);
327
+ const obj: SimpleMap = {} ;
328
+ if (this.sql.length > 0) {
329
+ obj[this.sql] = getString(sql, args);
330
+ }
331
+ if (this.return.length > 0) {
332
+ obj[this.return] = v;
333
+ }
334
+ obj[this.duration] = d;
335
+ this.log('count', obj);
336
+ }
337
+ }, 0);
338
+ return v;
339
+ }).catch(er => {
340
+ setTimeout(() => {
341
+ const d = diff(t1);
342
+ const obj: SimpleMap = {};
343
+ if (this.sql.length > 0) {
344
+ obj[this.sql] = getString(sql, args);
345
+ }
346
+ obj[this.duration] = d;
347
+ this.error('error count: ' + buildString(er));
348
+ }, 0);
349
+ throw er;
350
+ });
351
+ }
352
+ }
353
+ function buildString(v: any): string {
354
+ if (typeof v === 'string') {
355
+ return v;
356
+ } else {
357
+ return JSON.stringify(v);
358
+ }
359
+ }
360
+ function getString(sql: string, args?: any[]): string {
361
+ if (args && args.length > 0) {
362
+ return sql + ' ' + JSON.stringify(args);
363
+ } else {
364
+ return sql;
365
+ }
366
+ }
367
+ export function diff(d1: Date): number {
368
+ const d2 = new Date();
369
+ return d2.getTime() - d1.getTime();
100
370
  }
371
+ /*
372
+ const NS_PER_SEC = 1e9;
373
+ const NS_TO_MS = 1e6;
374
+ const getDurationInMilliseconds = (start: [number, number] | undefined) => {
375
+ const diff = process.hrtime(start);
376
+ return (diff[0] * NS_PER_SEC + diff[1]) / NS_TO_MS;
377
+ };
378
+ */
379
+ // tslint:disable-next-line:max-classes-per-file
101
380
  export class SqlWriter<T, ID> extends SqlLoader<T, ID> {
102
381
  version?: string;
103
382
  exec: (sql: string, args?: any[], ctx?: any) => Promise<number>;
@@ -148,8 +427,8 @@ export class SqlWriter<T, ID> extends SqlLoader<T, ID> {
148
427
  return Promise.resolve(0);
149
428
  }
150
429
  }
151
- patch(obj: T, ctx?: any): Promise<number> {
152
- return this.update(obj, ctx);
430
+ patch(obj: Partial<T>, ctx?: any): Promise<number> {
431
+ return this.update(obj as any, ctx);
153
432
  }
154
433
  delete(id: ID, ctx?: any): Promise<number> {
155
434
  const stmt = buildToDelete<ID>(id, this.table, this.primaryKeys, this.param);
@@ -160,6 +439,7 @@ export class SqlWriter<T, ID> extends SqlLoader<T, ID> {
160
439
  }
161
440
  }
162
441
  }
442
+ // tslint:disable-next-line:max-classes-per-file
163
443
  export class SqlSearchWriter<T, ID, S extends Filter> extends SqlWriter<T, ID> {
164
444
  constructor(
165
445
  protected find: (s: S, limit?: number, offset?: number|string, fields?: string[]) => Promise<SearchResult<T>>,
package/tsconfig.json CHANGED
@@ -13,6 +13,7 @@
13
13
  "experimentalDecorators": false,
14
14
  "removeComments": true,
15
15
  "lib": [
16
+ "DOM",
16
17
  "es7"
17
18
  ]
18
19
  },