query-core 0.1.19 → 0.1.22

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.
@@ -1,4 +1,17 @@
1
1
  "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ extendStatics(d, b);
11
+ function __() { this.constructor = d; }
12
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13
+ };
14
+ })();
2
15
  Object.defineProperty(exports, "__esModule", { value: true });
3
16
  var build_1 = require("./build");
4
17
  var query_1 = require("./query");
@@ -27,20 +40,20 @@ var SearchBuilder = (function () {
27
40
  this.excluding = (excluding && excluding.length > 0 ? excluding : 'excluding');
28
41
  this.search = this.search.bind(this);
29
42
  if (buildParam) {
30
- this.buildParam = buildParam;
43
+ this.param = buildParam;
31
44
  }
32
45
  else {
33
46
  if (provider === search_1.oracle) {
34
- this.buildParam = query_1.buildOracleParam;
47
+ this.param = query_1.buildOracleParam;
35
48
  }
36
49
  else if (provider === exports.postgres) {
37
- this.buildParam = query_1.buildDollarParam;
50
+ this.param = query_1.buildDollarParam;
38
51
  }
39
52
  else if (provider === exports.mssql) {
40
- this.buildParam = query_1.buildMsSQLParam;
53
+ this.param = query_1.buildMsSQLParam;
41
54
  }
42
55
  else {
43
- this.buildParam = build_1.param;
56
+ this.param = build_1.param;
44
57
  }
45
58
  }
46
59
  this.total = (total && total.length > 0 ? total : 'total');
@@ -53,7 +66,7 @@ var SearchBuilder = (function () {
53
66
  var st = (this.sort ? this.sort : 'sort');
54
67
  var sn = s[st];
55
68
  delete s[st];
56
- var x = (this.provider === exports.postgres ? 'ilike' : this.buildParam);
69
+ var x = (this.provider === exports.postgres ? 'ilike' : this.param);
57
70
  var q2 = this.buildQuery(s, x, sn, this.buildSort, this.attributes, this.table, fields, this.q, this.excluding);
58
71
  if (!q2) {
59
72
  throw new Error('Cannot build query');
@@ -77,3 +90,58 @@ var SearchBuilder = (function () {
77
90
  return SearchBuilder;
78
91
  }());
79
92
  exports.SearchBuilder = SearchBuilder;
93
+ var Query = (function (_super) {
94
+ __extends(Query, _super);
95
+ function Query(query, table, attributes, provider, buildQ, fromDB, sort, q, excluding, buildSort, buildParam, total) {
96
+ var _this = _super.call(this, query, table, attributes, provider, buildQ, fromDB, sort, q, excluding, buildSort, buildParam, total) || this;
97
+ var m = build_1.metadata(attributes);
98
+ _this.primaryKeys = m.keys;
99
+ _this.map = m.map;
100
+ _this.bools = m.bools;
101
+ if (_this.metadata) {
102
+ _this.metadata = _this.metadata.bind(_this);
103
+ }
104
+ _this.all = _this.all.bind(_this);
105
+ _this.load = _this.load.bind(_this);
106
+ _this.exist = _this.exist.bind(_this);
107
+ return _this;
108
+ }
109
+ Query.prototype.metadata = function () {
110
+ return this.attributes;
111
+ };
112
+ Query.prototype.all = function () {
113
+ var sql = "select * from " + this.table;
114
+ return this.query(sql, [], this.map);
115
+ };
116
+ Query.prototype.load = function (id, ctx) {
117
+ var stmt = build_1.select(id, this.table, this.primaryKeys, this.param);
118
+ if (!stmt) {
119
+ throw new Error('cannot build query by id');
120
+ }
121
+ var fn = this.fromDB;
122
+ if (fn) {
123
+ return this.query(stmt.query, stmt.params, this.map, ctx).then(function (res) {
124
+ if (!res || res.length === 0) {
125
+ return null;
126
+ }
127
+ else {
128
+ var obj = res[0];
129
+ return fn(obj);
130
+ }
131
+ });
132
+ }
133
+ else {
134
+ return this.query(stmt.query, stmt.params, this.map).then(function (res) { return (!res || res.length === 0) ? null : res[0]; });
135
+ }
136
+ };
137
+ Query.prototype.exist = function (id, ctx) {
138
+ var field = (this.primaryKeys[0].column ? this.primaryKeys[0].column : this.primaryKeys[0].name);
139
+ var stmt = build_1.exist(id, this.table, this.primaryKeys, this.param, field);
140
+ if (!stmt) {
141
+ throw new Error('cannot build query by id');
142
+ }
143
+ return this.query(stmt.query, stmt.params, this.map, undefined, ctx).then(function (res) { return (!res || res.length === 0) ? false : true; });
144
+ };
145
+ return Query;
146
+ }(SearchBuilder));
147
+ exports.Query = Query;
package/lib/services.js CHANGED
@@ -14,16 +14,16 @@ 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);
17
+ function useGet(q, table, attrs, param, fromDB) {
18
+ var l = new SqlLoader(q, table, attrs, param, fromDB);
19
19
  return l.load;
20
20
  }
21
21
  exports.useGet = useGet;
22
22
  exports.useLoad = useGet;
23
23
  var SqlLoader = (function () {
24
- function SqlLoader(table, query, attrs, param, fromDB) {
25
- this.table = table;
24
+ function SqlLoader(query, table, attrs, param, fromDB) {
26
25
  this.query = query;
26
+ this.table = table;
27
27
  this.param = param;
28
28
  this.fromDB = fromDB;
29
29
  if (Array.isArray(attrs)) {
@@ -85,8 +85,8 @@ var SqlLoader = (function () {
85
85
  exports.SqlLoader = SqlLoader;
86
86
  var SqlSearchLoader = (function (_super) {
87
87
  __extends(SqlSearchLoader, _super);
88
- function SqlSearchLoader(find, table, query, attrs, param, fromDB) {
89
- var _this = _super.call(this, table, query, attrs, param, fromDB) || this;
88
+ function SqlSearchLoader(find, query, table, attrs, param, fromDB) {
89
+ var _this = _super.call(this, query, table, attrs, param, fromDB) || this;
90
90
  _this.find = find;
91
91
  _this.search = _this.search.bind(_this);
92
92
  return _this;
@@ -145,8 +145,36 @@ var LogManager = (function () {
145
145
  return this.db.param(i);
146
146
  };
147
147
  LogManager.prototype.exec = function (sql, args, ctx) {
148
+ var _this = this;
148
149
  var t1 = new Date();
149
- return this.db.exec(sql, args, ctx);
150
+ return this.db.exec(sql, args, ctx).then(function (v) {
151
+ setTimeout(function () {
152
+ if (_this.log) {
153
+ var d = diff(t1);
154
+ var obj = {};
155
+ if (_this.sql.length > 0) {
156
+ obj[_this.sql] = getString(sql, args);
157
+ }
158
+ if (_this.return.length > 0) {
159
+ obj[_this.return] = v;
160
+ }
161
+ obj[_this.duration] = d;
162
+ _this.log('query', obj);
163
+ }
164
+ }, 0);
165
+ return v;
166
+ }).catch(function (er) {
167
+ setTimeout(function () {
168
+ var d = diff(t1);
169
+ var obj = {};
170
+ if (_this.sql.length > 0) {
171
+ obj[_this.sql] = getString(sql, args);
172
+ }
173
+ obj[_this.duration] = d;
174
+ _this.error('error query: ' + buildString(er));
175
+ }, 0);
176
+ throw er;
177
+ });
150
178
  };
151
179
  LogManager.prototype.execBatch = function (statements, firstSuccess, ctx) {
152
180
  var _this = this;
@@ -346,7 +374,7 @@ exports.diff = diff;
346
374
  var SqlWriter = (function (_super) {
347
375
  __extends(SqlWriter, _super);
348
376
  function SqlWriter(manager, table, attrs, toDB, fromDB) {
349
- var _this = _super.call(this, table, manager.query, attrs, manager.param, fromDB) || this;
377
+ var _this = _super.call(this, manager.query, table, attrs, manager.param, fromDB) || this;
350
378
  _this.toDB = toDB;
351
379
  var x = build_1.version(attrs);
352
380
  _this.exec = manager.exec;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "query-core",
3
- "version": "0.1.19",
3
+ "version": "0.1.22",
4
4
  "description": "query",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
@@ -1,4 +1,4 @@
1
- import {metadata, param} from './build';
1
+ import {exist, metadata, param, select} from './build';
2
2
  import {Attribute, Attributes, Statement, StringMap} from './metadata';
3
3
  import {buildDollarParam, buildMsSQLParam, buildOracleParam, buildQuery, buildSort as bs, LikeType} from './query';
4
4
  import {buildFromQuery, oracle, SearchResult} from './search';
@@ -14,9 +14,10 @@ export class SearchBuilder<T, S> {
14
14
  q?: string;
15
15
  excluding?: string;
16
16
  buildSort?: (sort?: string, map?: Attributes|StringMap) => string;
17
- buildParam: (i: number) => string;
17
+ param: (i: number) => string;
18
18
  total?: string;
19
- constructor(public query: (sql: string, args?: any[], m?: StringMap, bools?: Attribute[]) => Promise<T[]>, public table: string,
19
+ constructor(public query: <K>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any) => Promise<K[]>,
20
+ public table: string,
20
21
  public attributes?: Attributes,
21
22
  public provider?: string,
22
23
  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,
@@ -39,16 +40,16 @@ export class SearchBuilder<T, S> {
39
40
  this.excluding = (excluding && excluding.length > 0 ? excluding : 'excluding');
40
41
  this.search = this.search.bind(this);
41
42
  if (buildParam) {
42
- this.buildParam = buildParam;
43
+ this.param = buildParam;
43
44
  } else {
44
45
  if (provider === oracle) {
45
- this.buildParam = buildOracleParam;
46
+ this.param = buildOracleParam;
46
47
  } else if (provider === postgres) {
47
- this.buildParam = buildDollarParam;
48
+ this.param = buildDollarParam;
48
49
  } else if (provider === mssql) {
49
- this.buildParam = buildMsSQLParam;
50
+ this.param = buildMsSQLParam;
50
51
  } else {
51
- this.buildParam = param;
52
+ this.param = param;
52
53
  }
53
54
  }
54
55
  this.total = (total && total.length > 0 ? total : 'total');
@@ -61,14 +62,14 @@ export class SearchBuilder<T, S> {
61
62
  const st = (this.sort ? this.sort : 'sort');
62
63
  const sn = (s as any)[st] as string;
63
64
  delete (s as any)[st];
64
- const x = (this.provider === postgres ? 'ilike' : this.buildParam);
65
+ const x = (this.provider === postgres ? 'ilike' : this.param);
65
66
  const q2 = this.buildQuery(s, x, sn, this.buildSort, this.attributes, this.table, fields, this.q, this.excluding);
66
67
  if (!q2) {
67
68
  throw new Error('Cannot build query');
68
69
  }
69
70
  const fn = this.fromDB;
70
71
  if (fn) {
71
- return buildFromQuery(this.query, q2.query, q2.params, limit, skip, this.map, this.bools, this.provider, this.total).then(r => {
72
+ return buildFromQuery<T>(this.query, q2.query, q2.params, limit, skip, this.map, this.bools, this.provider, this.total).then(r => {
72
73
  if (r.list && r.list.length > 0) {
73
74
  r.list = r.list.map(o => fn(o));
74
75
  return r;
@@ -81,3 +82,68 @@ export class SearchBuilder<T, S> {
81
82
  }
82
83
  }
83
84
  }
85
+ export class Query<T, ID, S> extends SearchBuilder<T, S> {
86
+ primaryKeys: Attribute[];
87
+ map?: StringMap;
88
+ // attributes: Attributes;
89
+ bools?: Attribute[];
90
+ constructor(
91
+ query: <K>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any) => Promise<K[]>,
92
+ table: string,
93
+ attributes: Attributes,
94
+ provider?: string,
95
+ 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,
96
+ fromDB?: (v: T) => T,
97
+ sort?: string,
98
+ q?: string,
99
+ excluding?: string,
100
+ buildSort?: (sort?: string, map?: Attributes|StringMap) => string,
101
+ buildParam?: (i: number) => string,
102
+ total?: string) {
103
+ super(query, table, attributes, provider, buildQ, fromDB, sort, q, excluding, buildSort, buildParam, total);
104
+ const m = metadata(attributes);
105
+ this.primaryKeys = m.keys;
106
+ this.map = m.map;
107
+ this.bools = m.bools;
108
+ if (this.metadata) {
109
+ this.metadata = this.metadata.bind(this);
110
+ }
111
+ this.all = this.all.bind(this);
112
+ this.load = this.load.bind(this);
113
+ this.exist = this.exist.bind(this);
114
+ }
115
+ metadata?(): Attributes|undefined {
116
+ return this.attributes;
117
+ }
118
+ all(): Promise<T[]> {
119
+ const sql = `select * from ${this.table}`;
120
+ return this.query(sql, [], this.map);
121
+ }
122
+ load(id: ID, ctx?: any): Promise<T|null> {
123
+ const stmt = select<ID>(id, this.table, this.primaryKeys, this.param);
124
+ if (!stmt) {
125
+ throw new Error('cannot build query by id');
126
+ }
127
+ const fn = this.fromDB;
128
+ if (fn) {
129
+ return this.query<T>(stmt.query, stmt.params, this.map, ctx).then(res => {
130
+ if (!res || res.length === 0) {
131
+ return null;
132
+ } else {
133
+ const obj = res[0];
134
+ return fn(obj);
135
+ }
136
+ });
137
+ } else {
138
+ return this.query<T>(stmt.query, stmt.params, this.map).then(res => (!res || res.length === 0) ? null : res[0]);
139
+ }
140
+ }
141
+ exist(id: ID, ctx?: any): Promise<boolean> {
142
+ const field = (this.primaryKeys[0].column ? this.primaryKeys[0].column : this.primaryKeys[0].name);
143
+ const stmt = exist<ID>(id, this.table, this.primaryKeys, this.param, field);
144
+ if (!stmt) {
145
+ throw new Error('cannot build query by id');
146
+ }
147
+ return this.query(stmt.query, stmt.params, this.map, undefined, ctx).then(res => (!res || res.length === 0) ? false : true);
148
+ }
149
+ }
package/src/search.ts CHANGED
@@ -4,9 +4,9 @@ export interface SearchResult<T> {
4
4
  list: T[];
5
5
  total?: number;
6
6
  }
7
- export function buildFromQuery<T>(query: (sql: string, args?: any[], m?: StringMap, bools?: Attribute[]) => Promise<T[]>, sql: string, params?: any[], limit?: number, offset?: number, mp?: StringMap, bools?: Attribute[], provider?: string, totalCol?: string): Promise<SearchResult<T>> {
7
+ export function buildFromQuery<T>(query: <K>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[]) => Promise<K[]>, sql: string, params?: any[], limit?: number, offset?: number, mp?: StringMap, bools?: Attribute[], provider?: string, totalCol?: string): Promise<SearchResult<T>> {
8
8
  if (!limit || limit <= 0) {
9
- return query(sql, params, mp, bools).then(list => {
9
+ return query<T>(sql, params, mp, bools).then(list => {
10
10
  const total = (list ? list.length : undefined);
11
11
  return {list, total};
12
12
  });
@@ -20,8 +20,8 @@ export function buildFromQuery<T>(query: (sql: string, args?: any[], m?: StringM
20
20
  } else {
21
21
  const sql2 = buildPagingQuery(sql, limit, offset);
22
22
  const countQuery = buildCountQuery(sql);
23
- const resultPromise = query(sql2, params, mp, bools);
24
- const countPromise = query(countQuery, params).then(r => {
23
+ const resultPromise = query<T>(sql2, params, mp, bools);
24
+ const countPromise = query<T>(countQuery, params).then(r => {
25
25
  if (!r || r.length === 0) {
26
26
  return 0;
27
27
  } else {
@@ -37,11 +37,11 @@ export function buildFromQuery<T>(query: (sql: string, args?: any[], m?: StringM
37
37
  }
38
38
  }
39
39
  }
40
- export function queryAndCount<T>(query: (sql: string, args?: any[], m?: StringMap, bools?: Attribute[]) => Promise<T[]>, sql: string, params: any[]|undefined, total: string, mp?: StringMap, bools?: Attribute[]): Promise<SearchResult<T>> {
40
+ export function queryAndCount<T>(query: <K>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[]) => Promise<K[]>, sql: string, params: any[]|undefined, total: string, mp?: StringMap, bools?: Attribute[]): Promise<SearchResult<T>> {
41
41
  if (!total || total.length === 0) {
42
42
  total = 'total';
43
43
  }
44
- return query(sql, params, mp, bools).then(list => {
44
+ return query<T>(sql, params, mp, bools).then(list => {
45
45
  if (!list || list.length === 0) {
46
46
  return {list: [], total: 0};
47
47
  }
package/src/services.ts CHANGED
@@ -9,12 +9,13 @@ export interface Filter {
9
9
  }
10
10
  export type Load<T, ID> = (id: ID, ctx?: any) => Promise<T|null>;
11
11
  export type Get<T, ID> = Load<T, ID>;
12
- export function useGet<T, ID>(table: string,
12
+ export function useGet<T, ID>(
13
13
  q: <K>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any) => Promise<K[]>,
14
+ table: string,
14
15
  attrs: Attributes|string[],
15
16
  param: (i: number) => string,
16
17
  fromDB?: (v: T) => T): Load<T, ID> {
17
- const l = new SqlLoader<T, ID>(table, q, attrs, param, fromDB);
18
+ const l = new SqlLoader<T, ID>(q, table, attrs, param, fromDB);
18
19
  return l.load;
19
20
  }
20
21
  export const useLoad = useGet;
@@ -23,8 +24,9 @@ export class SqlLoader<T, ID> {
23
24
  map?: StringMap;
24
25
  attributes: Attributes;
25
26
  bools?: Attribute[];
26
- constructor(public table: string,
27
+ constructor(
27
28
  public query: <K>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any) => Promise<K[]>,
29
+ public table: string,
28
30
  attrs: Attributes|string[],
29
31
  public param: (i: number) => string,
30
32
  public fromDB?: (v: T) => T) {
@@ -84,12 +86,12 @@ export class SqlLoader<T, ID> {
84
86
  export class SqlSearchLoader<T, ID, S extends Filter> extends SqlLoader<T, ID> {
85
87
  constructor(
86
88
  protected find: (s: S, limit?: number, offset?: number|string, fields?: string[]) => Promise<SearchResult<T>>,
87
- table: string,
88
89
  query: <K>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any) => Promise<K[]>,
90
+ table: string,
89
91
  attrs: Attributes|string[],
90
92
  param: (i: number) => string,
91
93
  fromDB?: (v: T) => T) {
92
- super(table, query, attrs, param, fromDB);
94
+ super(query, table, attrs, param, fromDB);
93
95
  this.search = this.search.bind(this);
94
96
  }
95
97
  search(s: S, limit?: number, offset?: number|string, fields?: string[]): Promise<SearchResult<T>> {
@@ -181,7 +183,34 @@ export class LogManager implements ExtManager {
181
183
  }
182
184
  exec(sql: string, args?: any[], ctx?: any): Promise<number> {
183
185
  const t1 = new Date();
184
- return this.db.exec(sql, args, ctx);
186
+ return this.db.exec(sql, args, ctx).then(v => {
187
+ setTimeout(() => {
188
+ if (this.log) {
189
+ const d = diff(t1);
190
+ const obj: SimpleMap = {} ;
191
+ if (this.sql.length > 0) {
192
+ obj[this.sql] = getString(sql, args);
193
+ }
194
+ if (this.return.length > 0) {
195
+ obj[this.return] = v;
196
+ }
197
+ obj[this.duration] = d;
198
+ this.log('query', obj);
199
+ }
200
+ }, 0);
201
+ return v;
202
+ }).catch(er => {
203
+ setTimeout(() => {
204
+ const d = diff(t1);
205
+ const obj: SimpleMap = {};
206
+ if (this.sql.length > 0) {
207
+ obj[this.sql] = getString(sql, args);
208
+ }
209
+ obj[this.duration] = d;
210
+ this.error('error query: ' + buildString(er));
211
+ }, 0);
212
+ throw er;
213
+ });
185
214
  }
186
215
  execBatch(statements: Statement[], firstSuccess?: boolean, ctx?: any): Promise<number> {
187
216
  const t1 = new Date();
@@ -385,7 +414,7 @@ export class SqlWriter<T, ID> extends SqlLoader<T, ID> {
385
414
  attrs: Attributes,
386
415
  public toDB?: (v: T) => T,
387
416
  fromDB?: (v: T) => T) {
388
- super(table, manager.query, attrs, manager.param, fromDB);
417
+ super(manager.query, table, attrs, manager.param, fromDB);
389
418
  const x = version(attrs);
390
419
  this.exec = manager.exec;
391
420
  this.execBatch = manager.execBatch;
@@ -427,8 +456,8 @@ export class SqlWriter<T, ID> extends SqlLoader<T, ID> {
427
456
  return Promise.resolve(0);
428
457
  }
429
458
  }
430
- patch(obj: T, ctx?: any): Promise<number> {
431
- return this.update(obj, ctx);
459
+ patch(obj: Partial<T>, ctx?: any): Promise<number> {
460
+ return this.update(obj as any, ctx);
432
461
  }
433
462
  delete(id: ID, ctx?: any): Promise<number> {
434
463
  const stmt = buildToDelete<ID>(id, this.table, this.primaryKeys, this.param);