query-core 0.1.8 → 0.1.9
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/SearchBuilder.js +16 -8
- package/lib/batch.js +1 -1
- package/lib/build.js +202 -100
- package/lib/client.js +180 -0
- package/lib/index.js +9 -70
- package/lib/map.js +75 -0
- package/lib/query.js +7 -3
- package/lib/search.js +4 -1
- package/lib/services.js +13 -4
- package/package.json +1 -1
- package/src/SearchBuilder.ts +23 -14
- package/src/batch.ts +2 -2
- package/src/build.ts +177 -86
- package/src/client.ts +202 -0
- package/src/index.ts +23 -63
- package/src/map.ts +67 -0
- package/src/query.ts +14 -10
- package/src/search.ts +11 -8
- package/src/services.ts +20 -10
- package/tsconfig.json +1 -0
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,22 @@ 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;
|
|
12
14
|
exports.SqlLoadService = services_1.SqlLoader;
|
|
13
15
|
exports.ViewSearchRepository = services_1.SqlSearchLoader;
|
|
14
16
|
exports.ViewSearchService = services_1.SqlSearchLoader;
|
|
17
|
+
exports.SearchRepository = services_1.SqlSearchLoader;
|
|
18
|
+
exports.SearchService = services_1.SqlSearchLoader;
|
|
15
19
|
exports.GenericSearchRepository = services_1.SqlSearchWriter;
|
|
16
20
|
exports.GenericSearchService = services_1.SqlSearchWriter;
|
|
21
|
+
exports.SqlRepository = services_1.SqlSearchWriter;
|
|
22
|
+
exports.SqlService = services_1.SqlSearchWriter;
|
|
23
|
+
exports.Repository = services_1.SqlSearchWriter;
|
|
24
|
+
exports.Service = services_1.SqlSearchWriter;
|
|
17
25
|
exports.SqlGenericRepository = services_1.SqlWriter;
|
|
18
26
|
exports.SqlGenericService = services_1.SqlWriter;
|
|
19
27
|
__export(require("./build"));
|
|
@@ -22,6 +30,7 @@ __export(require("./batch"));
|
|
|
22
30
|
__export(require("./query"));
|
|
23
31
|
__export(require("./search"));
|
|
24
32
|
__export(require("./SearchBuilder"));
|
|
33
|
+
__export(require("./client"));
|
|
25
34
|
var resource = (function () {
|
|
26
35
|
function resource() {
|
|
27
36
|
}
|
|
@@ -75,49 +84,6 @@ function toArray(arr) {
|
|
|
75
84
|
return p;
|
|
76
85
|
}
|
|
77
86
|
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
87
|
function map(obj, m) {
|
|
122
88
|
if (!m) {
|
|
123
89
|
return obj;
|
|
@@ -139,30 +105,3 @@ function map(obj, m) {
|
|
|
139
105
|
return obj2;
|
|
140
106
|
}
|
|
141
107
|
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(
|
|
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
|
|
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,
|
|
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
|
@@ -22,6 +22,7 @@ var SqlLoader = (function () {
|
|
|
22
22
|
this.fromDB = fromDB;
|
|
23
23
|
if (Array.isArray(attrs)) {
|
|
24
24
|
this.primaryKeys = build_1.attributes(attrs);
|
|
25
|
+
this.attributes = {};
|
|
25
26
|
}
|
|
26
27
|
else {
|
|
27
28
|
var m = build_1.metadata(attrs);
|
|
@@ -30,7 +31,9 @@ var SqlLoader = (function () {
|
|
|
30
31
|
this.map = m.map;
|
|
31
32
|
this.bools = m.bools;
|
|
32
33
|
}
|
|
33
|
-
|
|
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
49
|
var stmt = build_1.select(id, this.table, this.primaryKeys, this.param);
|
|
48
|
-
if (
|
|
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
|
|
61
|
+
return fn(obj);
|
|
56
62
|
}
|
|
57
63
|
});
|
|
58
64
|
}
|
|
@@ -63,6 +69,9 @@ var SqlLoader = (function () {
|
|
|
63
69
|
SqlLoader.prototype.exist = function (id, ctx) {
|
|
64
70
|
var field = (this.primaryKeys[0].field ? this.primaryKeys[0].field : this.primaryKeys[0].name);
|
|
65
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;
|
package/package.json
CHANGED
package/src/SearchBuilder.ts
CHANGED
|
@@ -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
|
|
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
|
|
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
|
|
17
|
-
buildParam
|
|
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
|
|
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
|
|
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 ===
|
|
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,
|
|
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 ===
|
|
61
|
-
const q2 = buildQuery(s, x, this.table, this.attributes, sn, fields, this.q, this.excluding, this.buildSort);
|
|
62
|
-
if (
|
|
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 =>
|
|
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 {
|