query-core 0.1.6 → 0.1.10

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/client.js ADDED
@@ -0,0 +1,180 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var build_1 = require("./build");
4
+ var map_1 = require("./map");
5
+ function buildStatements(s) {
6
+ var d = [];
7
+ if (!s || s.length === 0) {
8
+ return d;
9
+ }
10
+ for (var _i = 0, s_1 = s; _i < s_1.length; _i++) {
11
+ var t = s_1[_i];
12
+ var dates = toDates(t.params);
13
+ var j = { query: t.query, params: t.params, dates: dates };
14
+ d.push(j);
15
+ }
16
+ return d;
17
+ }
18
+ exports.buildStatements = buildStatements;
19
+ function toDates(args) {
20
+ var d = [];
21
+ if (!args || args.length === 0) {
22
+ return d;
23
+ }
24
+ var l = args.length;
25
+ for (var i = 0; i < l; i++) {
26
+ if (args[i] && args[i] instanceof Date) {
27
+ d.push(i);
28
+ }
29
+ }
30
+ return d;
31
+ }
32
+ exports.toDates = toDates;
33
+ var ProxyClient = (function () {
34
+ function ProxyClient(httpRequest, url) {
35
+ this.httpRequest = httpRequest;
36
+ this.url = url;
37
+ this.query = this.query.bind(this);
38
+ this.exec = this.exec.bind(this);
39
+ this.execBatch = this.execBatch.bind(this);
40
+ this.insert = this.insert.bind(this);
41
+ this.update = this.update.bind(this);
42
+ this.insertBatch = this.insertBatch.bind(this);
43
+ this.updateBatch = this.updateBatch.bind(this);
44
+ this.insertWithTx = this.insertWithTx.bind(this);
45
+ this.updateWithTx = this.updateWithTx.bind(this);
46
+ this.insertBatchWithTx = this.insertBatchWithTx.bind(this);
47
+ this.updateBatchWithTx = this.updateBatchWithTx.bind(this);
48
+ this.beginTransaction = this.beginTransaction.bind(this);
49
+ this.commitTransaction = this.commitTransaction.bind(this);
50
+ this.rollbackTransaction = this.rollbackTransaction.bind(this);
51
+ this.queryWithTx = this.queryWithTx.bind(this);
52
+ this.execWithTx = this.execWithTx.bind(this);
53
+ this.execBatchWithTx = this.execBatchWithTx.bind(this);
54
+ }
55
+ ProxyClient.prototype.query = function (sql, args, m, bools) {
56
+ var dates = toDates(args);
57
+ var j = { query: sql, params: args, dates: dates };
58
+ if (m || bools) {
59
+ return this.httpRequest.post(this.url + '/query', j).then(function (r) { return map_1.handleResults(r, m, bools); });
60
+ }
61
+ else {
62
+ return this.httpRequest.post(this.url + '/query', j);
63
+ }
64
+ };
65
+ ProxyClient.prototype.exec = function (sql, args) {
66
+ var dates = toDates(args);
67
+ var j = { query: sql, params: args, dates: dates };
68
+ return this.httpRequest.post(this.url + '/exec', j);
69
+ };
70
+ ProxyClient.prototype.execBatch = function (stmts) {
71
+ var d = buildStatements(stmts);
72
+ return this.httpRequest.post(this.url + '/exec-batch', d);
73
+ };
74
+ ProxyClient.prototype.beginTransaction = function (timeout) {
75
+ var st = (timeout && timeout > 0 ? '?timeout=' + timeout : '');
76
+ return this.httpRequest.post(this.url + '/begin' + st, '');
77
+ };
78
+ ProxyClient.prototype.commitTransaction = function (tx) {
79
+ return this.httpRequest.post(this.url + '/end?tx=' + tx, '');
80
+ };
81
+ ProxyClient.prototype.rollbackTransaction = function (tx) {
82
+ return this.httpRequest.post(this.url + '/end?roleback=true&tx=' + tx, '');
83
+ };
84
+ ProxyClient.prototype.queryWithTx = function (tx, commit, sql, args, m, bools) {
85
+ var dates = toDates(args);
86
+ var j = { query: sql, params: args, dates: dates };
87
+ var sc = (commit ? '&commit=true' : '');
88
+ if (m || bools) {
89
+ return this.httpRequest.post(this.url + '/query?tx=' + tx + sc, j).then(function (r) { return map_1.handleResults(r, m, bools); });
90
+ }
91
+ else {
92
+ return this.httpRequest.post(this.url + '/query?tx=' + tx + sc, j);
93
+ }
94
+ };
95
+ ProxyClient.prototype.execWithTx = function (tx, commit, sql, args) {
96
+ var dates = toDates(args);
97
+ var j = { query: sql, params: args, dates: dates };
98
+ var sc = (commit ? '&commit=true' : '');
99
+ return this.httpRequest.post(this.url + '/exec?tx=' + tx + sc, j);
100
+ };
101
+ ProxyClient.prototype.execBatchWithTx = function (tx, commit, stmts) {
102
+ var d = buildStatements(stmts);
103
+ var sc = (commit ? '&commit=true' : '');
104
+ return this.httpRequest.post(this.url + '/exec-batch?tx=' + tx + sc, d);
105
+ };
106
+ ProxyClient.prototype.insert = function (table, attrs, obj, buildParam, ver) {
107
+ var s = build_1.buildToInsert(obj, table, attrs, buildParam, ver);
108
+ if (s) {
109
+ return this.exec(s.query, s.params);
110
+ }
111
+ else {
112
+ return Promise.resolve(-1);
113
+ }
114
+ };
115
+ ProxyClient.prototype.update = function (table, attrs, obj, buildParam, ver) {
116
+ var s = build_1.buildToUpdate(obj, table, attrs, buildParam, ver);
117
+ if (s) {
118
+ return this.exec(s.query, s.params);
119
+ }
120
+ else {
121
+ return Promise.resolve(-1);
122
+ }
123
+ };
124
+ ProxyClient.prototype.insertBatch = function (table, attrs, objs, buildParam, driver) {
125
+ var s = build_1.buildToInsertBatch(objs, table, attrs, buildParam);
126
+ if (s) {
127
+ return this.exec(s.query, s.params);
128
+ }
129
+ else {
130
+ return Promise.resolve(-1);
131
+ }
132
+ };
133
+ ProxyClient.prototype.updateBatch = function (table, attrs, objs, buildParam, notSkipInvalid) {
134
+ var s = build_1.buildToUpdateBatch(objs, table, attrs, buildParam, notSkipInvalid);
135
+ if (s && s.length > 0) {
136
+ return this.execBatch(s);
137
+ }
138
+ else {
139
+ return Promise.resolve(-1);
140
+ }
141
+ };
142
+ ProxyClient.prototype.insertWithTx = function (tx, commit, table, attrs, obj, buildParam, ver) {
143
+ var s = build_1.buildToInsert(obj, table, attrs, buildParam, ver);
144
+ if (s) {
145
+ return this.execWithTx(tx, commit, s.query, s.params);
146
+ }
147
+ else {
148
+ return Promise.resolve(-1);
149
+ }
150
+ };
151
+ ProxyClient.prototype.updateWithTx = function (tx, commit, table, attrs, obj, buildParam, ver) {
152
+ var s = build_1.buildToUpdate(obj, table, attrs, buildParam, ver);
153
+ if (s) {
154
+ return this.execWithTx(tx, commit, s.query, s.params);
155
+ }
156
+ else {
157
+ return Promise.resolve(-1);
158
+ }
159
+ };
160
+ ProxyClient.prototype.insertBatchWithTx = function (tx, commit, table, attrs, objs, buildParam, driver) {
161
+ var s = build_1.buildToInsertBatch(objs, table, attrs, buildParam);
162
+ if (s) {
163
+ return this.execWithTx(tx, commit, s.query, s.params);
164
+ }
165
+ else {
166
+ return Promise.resolve(-1);
167
+ }
168
+ };
169
+ ProxyClient.prototype.updateBatchWithTx = function (tx, commit, table, attrs, objs, buildParam, notSkipInvalid) {
170
+ var s = build_1.buildToUpdateBatch(objs, table, attrs, buildParam, notSkipInvalid);
171
+ if (s && s.length > 0) {
172
+ return this.execBatchWithTx(tx, commit, s);
173
+ }
174
+ else {
175
+ return Promise.resolve(-1);
176
+ }
177
+ };
178
+ return ProxyClient;
179
+ }());
180
+ exports.ProxyClient = ProxyClient;
package/lib/index.js CHANGED
@@ -6,14 +6,28 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  var services_1 = require("./services");
7
7
  exports.createGenericSearchRepository = services_1.createSqlSearchWriter;
8
8
  exports.createGenericSearchService = services_1.createSqlSearchWriter;
9
+ exports.createRepository = services_1.createSqlSearchWriter;
10
+ exports.createService = services_1.createSqlSearchWriter;
9
11
  exports.createGenericRepository = services_1.createSqlWriter;
10
12
  exports.createGenericService = services_1.createSqlWriter;
11
13
  exports.SqlLoadRepository = services_1.SqlLoader;
14
+ exports.SqlViewRepository = services_1.SqlLoader;
12
15
  exports.SqlLoadService = services_1.SqlLoader;
16
+ exports.SqlViewService = services_1.SqlLoader;
13
17
  exports.ViewSearchRepository = services_1.SqlSearchLoader;
14
18
  exports.ViewSearchService = services_1.SqlSearchLoader;
19
+ exports.SqlViewSearchRepository = services_1.SqlSearchLoader;
20
+ exports.SqlViewSearchService = services_1.SqlSearchLoader;
21
+ exports.SqlSearchRepository = services_1.SqlSearchLoader;
22
+ exports.SqlSearchService = services_1.SqlSearchLoader;
23
+ exports.SearchRepository = services_1.SqlSearchLoader;
24
+ exports.SearchService = services_1.SqlSearchLoader;
15
25
  exports.GenericSearchRepository = services_1.SqlSearchWriter;
16
26
  exports.GenericSearchService = services_1.SqlSearchWriter;
27
+ exports.SqlRepository = services_1.SqlSearchWriter;
28
+ exports.SqlService = services_1.SqlSearchWriter;
29
+ exports.Repository = services_1.SqlSearchWriter;
30
+ exports.Service = services_1.SqlSearchWriter;
17
31
  exports.SqlGenericRepository = services_1.SqlWriter;
18
32
  exports.SqlGenericService = services_1.SqlWriter;
19
33
  __export(require("./build"));
@@ -22,6 +36,7 @@ __export(require("./batch"));
22
36
  __export(require("./query"));
23
37
  __export(require("./search"));
24
38
  __export(require("./SearchBuilder"));
39
+ __export(require("./client"));
25
40
  var resource = (function () {
26
41
  function resource() {
27
42
  }
@@ -75,49 +90,6 @@ function toArray(arr) {
75
90
  return p;
76
91
  }
77
92
  exports.toArray = toArray;
78
- function handleResults(r, m, bools) {
79
- if (m) {
80
- var res = mapArray(r, m);
81
- if (bools && bools.length > 0) {
82
- return handleBool(res, bools);
83
- }
84
- else {
85
- return res;
86
- }
87
- }
88
- else {
89
- if (bools && bools.length > 0) {
90
- return handleBool(r, bools);
91
- }
92
- else {
93
- return r;
94
- }
95
- }
96
- }
97
- exports.handleResults = handleResults;
98
- function handleBool(objs, bools) {
99
- if (!bools || bools.length === 0 || !objs) {
100
- return objs;
101
- }
102
- for (var _i = 0, objs_1 = objs; _i < objs_1.length; _i++) {
103
- var obj = objs_1[_i];
104
- for (var _a = 0, bools_1 = bools; _a < bools_1.length; _a++) {
105
- var field = bools_1[_a];
106
- var value = obj[field.name];
107
- if (value != null && value !== undefined) {
108
- var b = field.true;
109
- if (b == null || b === undefined) {
110
- obj[field.name] = ('1' == value || 'T' == value || 'Y' == value);
111
- }
112
- else {
113
- obj[field.name] = (value == b ? true : false);
114
- }
115
- }
116
- }
117
- }
118
- return objs;
119
- }
120
- exports.handleBool = handleBool;
121
93
  function map(obj, m) {
122
94
  if (!m) {
123
95
  return obj;
@@ -139,30 +111,3 @@ function map(obj, m) {
139
111
  return obj2;
140
112
  }
141
113
  exports.map = map;
142
- function mapArray(results, m) {
143
- if (!m) {
144
- return results;
145
- }
146
- var mkeys = Object.keys(m);
147
- if (mkeys.length === 0) {
148
- return results;
149
- }
150
- var objs = [];
151
- var length = results.length;
152
- for (var i = 0; i < length; i++) {
153
- var obj = results[i];
154
- var obj2 = {};
155
- var keys = Object.keys(obj);
156
- for (var _i = 0, keys_2 = keys; _i < keys_2.length; _i++) {
157
- var key = keys_2[_i];
158
- var k0 = m[key];
159
- if (!k0) {
160
- k0 = key;
161
- }
162
- obj2[k0] = obj[key];
163
- }
164
- objs.push(obj2);
165
- }
166
- return objs;
167
- }
168
- exports.mapArray = mapArray;
package/lib/map.js ADDED
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ function mapArray(results, m) {
4
+ if (!m) {
5
+ return results;
6
+ }
7
+ var mkeys = Object.keys(m);
8
+ if (mkeys.length === 0) {
9
+ return results;
10
+ }
11
+ var objs = [];
12
+ var length = results.length;
13
+ for (var i = 0; i < length; i++) {
14
+ var obj = results[i];
15
+ var obj2 = {};
16
+ var keys = Object.keys(obj);
17
+ for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
18
+ var key = keys_1[_i];
19
+ var k0 = m[key];
20
+ if (!k0) {
21
+ k0 = key;
22
+ }
23
+ obj2[k0] = obj[key];
24
+ }
25
+ objs.push(obj2);
26
+ }
27
+ return objs;
28
+ }
29
+ exports.mapArray = mapArray;
30
+ function handleResults(r, m, bools) {
31
+ if (m) {
32
+ var res = mapArray(r, m);
33
+ if (bools && bools.length > 0) {
34
+ return handleBool(res, bools);
35
+ }
36
+ else {
37
+ return res;
38
+ }
39
+ }
40
+ else {
41
+ if (bools && bools.length > 0) {
42
+ return handleBool(r, bools);
43
+ }
44
+ else {
45
+ return r;
46
+ }
47
+ }
48
+ }
49
+ exports.handleResults = handleResults;
50
+ function handleBool(objs, bools) {
51
+ if (!bools || bools.length === 0 || !objs) {
52
+ return objs;
53
+ }
54
+ for (var _i = 0, objs_1 = objs; _i < objs_1.length; _i++) {
55
+ var obj = objs_1[_i];
56
+ var o = obj;
57
+ for (var _a = 0, bools_1 = bools; _a < bools_1.length; _a++) {
58
+ var field = bools_1[_a];
59
+ if (field.name) {
60
+ var v = o[field.name];
61
+ if (typeof v !== 'boolean' && v != null && v !== undefined) {
62
+ var b = field.true;
63
+ if (b == null || b === undefined) {
64
+ o[field.name] = ('true' == v || '1' == v || 't' == v || 'y' == v || 'on' == v);
65
+ }
66
+ else {
67
+ o[field.name] = (v == b ? true : false);
68
+ }
69
+ }
70
+ }
71
+ }
72
+ }
73
+ return objs;
74
+ }
75
+ exports.handleBool = handleBool;
package/lib/query.js CHANGED
@@ -56,7 +56,11 @@ function buildDollarParam(i) {
56
56
  return '$' + i;
57
57
  }
58
58
  exports.buildDollarParam = buildDollarParam;
59
- function buildQuery(s, bparam, table, attrs, sort, fields, sq, strExcluding, buildSort3) {
59
+ function buildQuery(filter, bparam, table, attrs, sort, fields, sq, strExcluding, buildSort3) {
60
+ if (!table || !attrs) {
61
+ return undefined;
62
+ }
63
+ var s = filter;
60
64
  var like = 'like';
61
65
  var param;
62
66
  if (typeof bparam === 'string') {
@@ -250,7 +254,7 @@ function buildQuery(s, bparam, table, attrs, sort, fields, sq, strExcluding, bui
250
254
  qfilters.push(field + " " + like + " " + param(i++));
251
255
  args.push('%' + q + '%');
252
256
  }
253
- c.push(buildQ(field, attr.match, q));
257
+ c.push(buildQ(field, q, attr.match));
254
258
  }
255
259
  }
256
260
  if (qfilters.length > 0) {
@@ -308,7 +312,7 @@ function isEmpty(s) {
308
312
  return !(s && s.length > 0);
309
313
  }
310
314
  exports.isEmpty = isEmpty;
311
- function buildQ(field, match, q) {
315
+ function buildQ(field, q, match) {
312
316
  var o = {};
313
317
  if (match === 'equal') {
314
318
  o[field] = q;
package/lib/search.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  function buildFromQuery(query, sql, params, limit, offset, mp, bools, provider, totalCol) {
4
- if (limit <= 0) {
4
+ if (!limit || limit <= 0) {
5
5
  return query(sql, params, mp, bools).then(function (list) {
6
6
  var total = (list ? list.length : undefined);
7
7
  return { list: list, total: total };
@@ -78,6 +78,9 @@ function buildPagingQueryForOracle(sql, limit, offset, total) {
78
78
  if (!total || total.length === 0) {
79
79
  total = 'total';
80
80
  }
81
+ if (!offset) {
82
+ offset = 0;
83
+ }
81
84
  var l = d.length;
82
85
  var i = sql.indexOf(d);
83
86
  if (i < 0) {
package/lib/services.js CHANGED
@@ -21,16 +21,19 @@ var SqlLoader = (function () {
21
21
  this.param = param;
22
22
  this.fromDB = fromDB;
23
23
  if (Array.isArray(attrs)) {
24
- this.keys = build_1.attributes(attrs);
24
+ this.primaryKeys = build_1.attributes(attrs);
25
+ this.attributes = {};
25
26
  }
26
27
  else {
27
28
  var m = build_1.metadata(attrs);
28
29
  this.attributes = attrs;
29
- this.keys = m.keys;
30
+ this.primaryKeys = m.keys;
30
31
  this.map = m.map;
31
32
  this.bools = m.bools;
32
33
  }
33
- this.metadata = this.metadata.bind(this);
34
+ if (this.metadata) {
35
+ this.metadata = this.metadata.bind(this);
36
+ }
34
37
  this.all = this.all.bind(this);
35
38
  this.load = this.load.bind(this);
36
39
  this.exist = this.exist.bind(this);
@@ -43,16 +46,19 @@ var SqlLoader = (function () {
43
46
  return this.query(sql, [], this.map);
44
47
  };
45
48
  SqlLoader.prototype.load = function (id, ctx) {
46
- var _this = this;
47
- var stmt = build_1.select(id, this.table, this.keys, this.param);
48
- if (this.fromDB) {
49
+ var stmt = build_1.select(id, this.table, this.primaryKeys, this.param);
50
+ if (!stmt) {
51
+ throw new Error('cannot build query by id');
52
+ }
53
+ var fn = this.fromDB;
54
+ if (fn) {
49
55
  return this.query(stmt.query, stmt.params, this.map, ctx).then(function (res) {
50
56
  if (!res || res.length === 0) {
51
57
  return null;
52
58
  }
53
59
  else {
54
60
  var obj = res[0];
55
- return _this.fromDB(obj);
61
+ return fn(obj);
56
62
  }
57
63
  });
58
64
  }
@@ -61,8 +67,11 @@ var SqlLoader = (function () {
61
67
  }
62
68
  };
63
69
  SqlLoader.prototype.exist = function (id, ctx) {
64
- var field = (this.keys[0].field ? this.keys[0].field : this.keys[0].name);
65
- var stmt = build_1.exist(id, this.table, this.keys, this.param, field);
70
+ var field = (this.primaryKeys[0].field ? this.primaryKeys[0].field : this.primaryKeys[0].name);
71
+ var stmt = build_1.exist(id, this.table, this.primaryKeys, this.param, field);
72
+ if (!stmt) {
73
+ throw new Error('cannot build query by id');
74
+ }
66
75
  return this.query(stmt.query, stmt.params, this.map, undefined, ctx).then(function (res) { return (!res || res.length === 0) ? false : true; });
67
76
  };
68
77
  return SqlLoader;
@@ -73,6 +82,7 @@ var SqlSearchLoader = (function (_super) {
73
82
  function SqlSearchLoader(find, table, query, attrs, param, fromDB) {
74
83
  var _this = _super.call(this, table, query, attrs, param, fromDB) || this;
75
84
  _this.find = find;
85
+ _this.search = _this.search.bind(_this);
76
86
  return _this;
77
87
  }
78
88
  SqlSearchLoader.prototype.search = function (s, limit, offset, fields) {
@@ -82,17 +92,16 @@ var SqlSearchLoader = (function (_super) {
82
92
  }(SqlLoader));
83
93
  exports.SqlSearchLoader = SqlSearchLoader;
84
94
  function createSqlWriter(table, manager, attrs, buildParam, toDB, fromDB) {
85
- var writer = new SqlWriter(table, manager.query, manager.exec, attrs, buildParam, toDB, fromDB, manager.execBatch);
95
+ var writer = new SqlWriter(table, manager.query, manager.exec, attrs, buildParam, toDB, fromDB);
86
96
  return writer;
87
97
  }
88
98
  exports.createSqlWriter = createSqlWriter;
89
99
  var SqlWriter = (function (_super) {
90
100
  __extends(SqlWriter, _super);
91
- function SqlWriter(table, query, exec, attrs, buildParam, toDB, fromDB, execBatch) {
101
+ function SqlWriter(table, query, exec, attrs, buildParam, toDB, fromDB) {
92
102
  var _this = _super.call(this, table, query, attrs, buildParam, fromDB) || this;
93
103
  _this.exec = exec;
94
104
  _this.toDB = toDB;
95
- _this.execBatch = execBatch;
96
105
  var x = build_1.version(attrs);
97
106
  if (x) {
98
107
  _this.version = x.name;
@@ -140,7 +149,7 @@ var SqlWriter = (function (_super) {
140
149
  return this.update(obj, ctx);
141
150
  };
142
151
  SqlWriter.prototype.delete = function (id, ctx) {
143
- var stmt = build_1.buildToDelete(id, this.table, this.keys, this.param);
152
+ var stmt = build_1.buildToDelete(id, this.table, this.primaryKeys, this.param);
144
153
  if (stmt) {
145
154
  return this.exec(stmt.query, stmt.params, ctx);
146
155
  }
@@ -153,9 +162,10 @@ var SqlWriter = (function (_super) {
153
162
  exports.SqlWriter = SqlWriter;
154
163
  var SqlSearchWriter = (function (_super) {
155
164
  __extends(SqlSearchWriter, _super);
156
- function SqlSearchWriter(find, table, query, exec, attrs, buildParam, toDB, fromDB, execBatch) {
157
- var _this = _super.call(this, table, query, exec, attrs, buildParam, toDB, fromDB, execBatch) || this;
165
+ function SqlSearchWriter(find, table, query, exec, attrs, buildParam, toDB, fromDB) {
166
+ var _this = _super.call(this, table, query, exec, attrs, buildParam, toDB, fromDB) || this;
158
167
  _this.find = find;
168
+ _this.search = _this.search.bind(_this);
159
169
  return _this;
160
170
  }
161
171
  SqlSearchWriter.prototype.search = function (s, limit, offset, fields) {
@@ -165,7 +175,7 @@ var SqlSearchWriter = (function (_super) {
165
175
  }(SqlWriter));
166
176
  exports.SqlSearchWriter = SqlSearchWriter;
167
177
  function createSqlSearchWriter(find, table, manager, attrs, buildParam, toDB, fromDB) {
168
- var writer = new SqlSearchWriter(find, table, manager.query, manager.exec, attrs, buildParam, toDB, fromDB, manager.execBatch);
178
+ var writer = new SqlSearchWriter(find, table, manager.query, manager.exec, attrs, buildParam, toDB, fromDB);
169
179
  return writer;
170
180
  }
171
181
  exports.createSqlSearchWriter = createSqlSearchWriter;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "query-core",
3
- "version": "0.1.6",
3
+ "version": "0.1.10",
4
4
  "description": "query",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
@@ -3,28 +3,28 @@ 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';
5
5
 
6
- export const postgre = 'postgre';
6
+ export const postgres = 'postgres';
7
7
  export const mssql = 'mssql';
8
8
  export const mysql = 'mysql';
9
9
  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;
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;
14
14
  q?: string;
15
15
  excluding?: string;
16
- buildSort?: (sort: string, map?: Attributes|StringMap) => string;
17
- buildParam?: (i: number) => string;
16
+ buildSort?: (sort?: string, map?: Attributes|StringMap) => string;
17
+ buildParam: (i: number) => string;
18
18
  total?: string;
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,
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,
23
23
  public fromDB?: (v: T) => T,
24
24
  public sort?: string,
25
25
  q?: string,
26
26
  excluding?: string,
27
- buildSort?: (sort: string, map?: Attributes|StringMap) => string,
27
+ buildSort?: (sort?: string, map?: Attributes|StringMap) => string,
28
28
  buildParam?: (i: number) => string,
29
29
  total?: string) {
30
30
  if (attributes) {
@@ -43,7 +43,7 @@ export class SearchBuilder<T, S> {
43
43
  } else {
44
44
  if (provider === oracle) {
45
45
  this.buildParam = buildOracleParam;
46
- } else if (provider === postgre) {
46
+ } else if (provider === postgres) {
47
47
  this.buildParam = buildDollarParam;
48
48
  } else if (provider === mssql) {
49
49
  this.buildParam = buildMsSQLParam;
@@ -53,16 +53,25 @@ export class SearchBuilder<T, S> {
53
53
  }
54
54
  this.total = (total && total.length > 0 ? total : 'total');
55
55
  }
56
- search(s: S, limit?: number, skip?: number, fields?: string[]): Promise<SearchResult<T>> {
56
+ search(s: S, limit?: number, offset?: number|string, fields?: string[]): Promise<SearchResult<T>> {
57
+ let skip = 0;
58
+ if (typeof offset === 'number' && offset > 0) {
59
+ skip = offset;
60
+ }
57
61
  const st = (this.sort ? this.sort : 'sort');
58
- const sn = s[st] as string;
59
- delete s[st];
60
- const x = (this.provider === postgre ? 'ilike' : this.buildParam);
61
- const q2 = buildQuery(s, x, this.table, this.attributes, sn, fields, this.q, this.excluding, this.buildSort);
62
- if (this.fromDB) {
62
+ const sn = (s as any)[st] as string;
63
+ delete (s as any)[st];
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);
66
+ if (!q2) {
67
+ throw new Error('Cannot build query');
68
+ }
69
+ const fn = this.fromDB;
70
+ if (fn) {
63
71
  return buildFromQuery(this.query, q2.query, q2.params, limit, skip, this.map, this.bools, this.provider, this.total).then(r => {
64
72
  if (r.list && r.list.length > 0) {
65
- r.list = r.list.map(o => this.fromDB(o));
73
+ r.list = r.list.map(o => fn(o));
74
+ return r;
66
75
  } else {
67
76
  return r;
68
77
  }
package/src/batch.ts CHANGED
@@ -53,7 +53,7 @@ export class SqlUpdater<T> {
53
53
  }
54
54
  export class SqlBatchInserter<T> {
55
55
  version?: string;
56
- 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) {
56
+ 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) {
57
57
  this.write = this.write.bind(this);
58
58
  const x = version(attributes);
59
59
  if (x) {
@@ -72,7 +72,7 @@ export class SqlBatchInserter<T> {
72
72
  list.push(obj2);
73
73
  }
74
74
  }
75
- const stmt = buildToInsertBatch(list, this.table, this.attributes, this.param);
75
+ const stmt = buildToInsertBatch(list, this.table, this.attributes, this.param, this.version);
76
76
  if (stmt) {
77
77
  return this.exec(stmt.query, stmt.params);
78
78
  } else {