query-core 0.2.0 → 0.2.1

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/src/query.ts CHANGED
@@ -1,194 +1,204 @@
1
- import {params} from './build';
2
- import {Attribute, Attributes, Statement, StringMap} from './metadata';
1
+ import { params } from "./build"
2
+ import { Attribute, Attributes, Statement, StringMap } from "./metadata"
3
3
 
4
- export type LikeType = 'like' | 'ilike';
4
+ export type LikeType = "like" | "ilike"
5
5
 
6
- export function buildSort(sort?: string, map?: Attributes|StringMap): string {
6
+ export function buildSort(sort?: string, map?: Attributes | StringMap): string {
7
7
  if (!sort || sort.length === 0) {
8
- return '';
8
+ return ""
9
9
  }
10
- const sort2: string[] = [];
10
+ const sort2: string[] = []
11
11
  if (sort && sort.length > 0) {
12
- const sorts = sort.split(',');
12
+ const sorts = sort.split(",")
13
13
  for (const st of sorts) {
14
14
  if (st.length > 0) {
15
- let field = st;
16
- const tp = st.charAt(0);
17
- if (tp === '-' || tp === '+') {
18
- field = st.substring(1);
15
+ let field = st
16
+ const tp = st.charAt(0)
17
+ if (tp === "-" || tp === "+") {
18
+ field = st.substring(1)
19
19
  }
20
- const sortType = (tp === '-' ? ' desc' : '');
21
- sort2.push(getField(field.trim(), map) + sortType);
20
+ const sortType = tp === "-" ? " desc" : ""
21
+ sort2.push(getField(field.trim(), map) + sortType)
22
22
  }
23
23
  }
24
24
  }
25
25
  if (sort2.length === 0) {
26
- return '';
26
+ return ""
27
27
  }
28
- return sort2.join(',');
28
+ return sort2.join(",")
29
29
  }
30
- export function getField(name: string, map?: Attributes|StringMap): string {
30
+ export function getField(name: string, map?: Attributes | StringMap): string {
31
31
  if (!map) {
32
- return name;
32
+ return name
33
33
  }
34
- const x = map[name];
34
+ const x = map[name]
35
35
  if (!x) {
36
- return name;
36
+ return name
37
37
  }
38
- if (typeof x === 'string') {
39
- return x;
38
+ if (typeof x === "string") {
39
+ return x
40
40
  }
41
41
  if (x.column) {
42
- return x.column;
42
+ return x.column
43
43
  }
44
- return name;
44
+ return name
45
45
  }
46
46
  export function buildMsSQLParam(i: number): string {
47
- return '@' + i;
47
+ return "@" + i
48
48
  }
49
49
  export function buildOracleParam(i: number): string {
50
- return ':' + i;
50
+ return ":" + i
51
51
  }
52
52
  export function buildDollarParam(i: number): string {
53
- return '$' + i;
53
+ return "$" + i
54
54
  }
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 {
55
+ export function buildQuery<S>(
56
+ filter: S,
57
+ bparam: LikeType | ((i: number) => string),
58
+ sort?: string,
59
+ buildSort3?: (sort?: string, map?: Attributes | StringMap) => string,
60
+ attrs?: Attributes,
61
+ table?: string,
62
+ fields?: string[],
63
+ sq?: string,
64
+ strExcluding?: string,
65
+ ): Statement | undefined {
56
66
  if (!table || !attrs) {
57
- return undefined;
67
+ return undefined
58
68
  }
59
- const s: any = filter;
60
- let like = 'like';
61
- let param: (i: number ) => string;
62
- if (typeof bparam === 'string') {
63
- if (bparam === 'ilike') {
64
- like = bparam;
69
+ const s: any = filter
70
+ let like = "like"
71
+ let param: (i: number) => string
72
+ if (typeof bparam === "string") {
73
+ if (bparam === "ilike") {
74
+ like = bparam
65
75
  }
66
- param = buildDollarParam;
76
+ param = buildDollarParam
67
77
  } else {
68
- param = bparam;
78
+ param = bparam
69
79
  }
70
80
  if (!like) {
71
- like = 'like';
81
+ like = "like"
72
82
  }
73
- const filters: string[] = [];
74
- let q: string|undefined;
75
- let excluding: string[]|number[]|undefined;
76
- const args: any[] = [];
83
+ const filters: string[] = []
84
+ let q: string | undefined
85
+ let excluding: string[] | number[] | undefined
86
+ const args: any[] = []
77
87
  if (sq && sq.length > 0) {
78
- q = s[sq];
79
- delete s[sq];
80
- if (q === '') {
81
- q = undefined;
88
+ q = s[sq]
89
+ delete s[sq]
90
+ if (q === "") {
91
+ q = undefined
82
92
  }
83
93
  }
84
94
  if (strExcluding && strExcluding.length > 0) {
85
- excluding = s[strExcluding];
86
- delete s[strExcluding];
87
- if (typeof excluding === 'string') {
88
- excluding = (excluding as string).split(',');
95
+ excluding = s[strExcluding]
96
+ delete s[strExcluding]
97
+ if (typeof excluding === "string") {
98
+ excluding = (excluding as string).split(",")
89
99
  }
90
100
  if (excluding && excluding.length === 0) {
91
- excluding = undefined;
101
+ excluding = undefined
92
102
  }
93
103
  }
94
- const ex: string[] = [];
95
- const keys = Object.keys(s);
96
- let i = 1;
104
+ const ex: string[] = []
105
+ const keys = Object.keys(s)
106
+ let i = 1
97
107
  for (const key of keys) {
98
- const v = s[key];
99
- let field = key;
108
+ const v = s[key]
109
+ let field = key
100
110
  if (v !== undefined && v != null) {
101
- const attr: Attribute = attrs[key];
111
+ const attr: Attribute = attrs[key]
102
112
  if (attr) {
103
- field = (attr.column ? attr.column : key);
104
- if (typeof v === 'string') {
113
+ field = attr.column ? attr.column : key
114
+ if (typeof v === "string") {
105
115
  if (v.length !== 0) {
106
116
  if (attr.q) {
107
- ex.push(key);
117
+ ex.push(key)
108
118
  }
109
- if (attr.match === 'equal') {
110
- filters.push(`${field} = ${param(i++)}`);
111
- args.push(v);
112
- } else if (attr.match === 'prefix') {
113
- filters.push(`${field} ${like} ${param(i++)}`);
114
- args.push(v + '%');
119
+ if (attr.match === "equal") {
120
+ filters.push(`${field} = ${param(i++)}`)
121
+ args.push(v)
122
+ } else if (attr.match === "prefix") {
123
+ filters.push(`${field} ${like} ${param(i++)}`)
124
+ args.push(v + "%")
115
125
  } else {
116
- filters.push(`${field} ${like} ${param(i++)}`);
117
- args.push('%' + v + '%');
126
+ filters.push(`${field} ${like} ${param(i++)}`)
127
+ args.push("%" + v + "%")
118
128
  }
119
129
  }
120
130
  } else if (v instanceof Date) {
121
- if (attr.match === 'max') {
122
- filters.push(`${field} <= ${param(i++)}`);
123
- args.push(v);
131
+ if (attr.match === "max") {
132
+ filters.push(`${field} <= ${param(i++)}`)
133
+ args.push(v)
124
134
  } else {
125
- filters.push(`${field} >= ${param(i++)}`);
126
- args.push(v);
135
+ filters.push(`${field} >= ${param(i++)}`)
136
+ args.push(v)
127
137
  }
128
- } else if (typeof v === 'number') {
129
- if (attr.match === 'max') {
130
- filters.push(`${field} <= ${v}`);
138
+ } else if (typeof v === "number") {
139
+ if (attr.match === "max") {
140
+ filters.push(`${field} <= ${v}`)
131
141
  } else {
132
- filters.push(`${field} >= ${v}`);
142
+ filters.push(`${field} >= ${v}`)
133
143
  }
134
- } else if (attr.type === 'ObjectId') {
135
- filters.push(`${field} = ${param(i++)}`);
136
- args.push(v);
137
- } else if (typeof v === 'object') {
144
+ } else if (attr.type === "ObjectId") {
145
+ filters.push(`${field} = ${param(i++)}`)
146
+ args.push(v)
147
+ } else if (typeof v === "object") {
138
148
  if (Array.isArray(v)) {
139
149
  if (v.length > 0) {
140
- const ps = params(v.length, param, i - 1);
141
- i = i + v.length;
150
+ const ps = params(v.length, param, i - 1)
151
+ i = i + v.length
142
152
  for (const sv of v) {
143
- args.push(sv);
153
+ args.push(sv)
144
154
  }
145
- filters.push(`${field} in (${ps.join(',')})`);
155
+ filters.push(`${field} in (${ps.join(",")})`)
146
156
  }
147
- } else if (attr.type === 'date' || attr.type === 'datetime') {
157
+ } else if (attr.type === "date" || attr.type === "datetime") {
148
158
  if (isDateRange(v)) {
149
- if (v['max']) {
150
- filters.push(`${field} <= ${param(i++)}`);
151
- args.push(v['max']);
152
- } else if (v['top']) {
153
- filters.push(`${field} < ${param(i++)}`);
154
- args.push(v['top']);
155
- } else if (v['endDate']) {
156
- filters.push(`${field} <= ${param(i++)}`);
157
- args.push(v['endDate']);
158
- } else if (v['upper']) {
159
- filters.push(`${field} < ${param(i++)}`);
160
- args.push(v['upper']);
161
- } else if (v['endTime']) {
162
- filters.push(`${field} < ${param(i++)}`);
163
- args.push(v['endTime']);
159
+ if (v["max"]) {
160
+ filters.push(`${field} <= ${param(i++)}`)
161
+ args.push(v["max"])
162
+ } else if (v["top"]) {
163
+ filters.push(`${field} < ${param(i++)}`)
164
+ args.push(v["top"])
165
+ } else if (v["endDate"]) {
166
+ filters.push(`${field} <= ${param(i++)}`)
167
+ args.push(v["endDate"])
168
+ } else if (v["upper"]) {
169
+ filters.push(`${field} < ${param(i++)}`)
170
+ args.push(v["upper"])
171
+ } else if (v["endTime"]) {
172
+ filters.push(`${field} < ${param(i++)}`)
173
+ args.push(v["endTime"])
164
174
  }
165
- if (v['min']) {
166
- filters.push(`${field} >= ${param(i++)}`);
167
- args.push(v['min']);
168
- } else if (v['startTime']) {
169
- filters.push(`${field} >= ${param(i++)}`);
170
- args.push(v['startTime']);
171
- } else if (v['startDate']) {
172
- filters.push(`${field} >= ${param(i++)}`);
173
- args.push(v['startDate']);
174
- } else if (v['lower']) {
175
- filters.push(`${field} > ${param(i++)}`);
176
- args.push(v['lower']);
175
+ if (v["min"]) {
176
+ filters.push(`${field} >= ${param(i++)}`)
177
+ args.push(v["min"])
178
+ } else if (v["startTime"]) {
179
+ filters.push(`${field} >= ${param(i++)}`)
180
+ args.push(v["startTime"])
181
+ } else if (v["startDate"]) {
182
+ filters.push(`${field} >= ${param(i++)}`)
183
+ args.push(v["startDate"])
184
+ } else if (v["lower"]) {
185
+ filters.push(`${field} > ${param(i++)}`)
186
+ args.push(v["lower"])
177
187
  }
178
188
  }
179
- } else if (attr.type === 'number' || attr.type === 'integer') {
189
+ } else if (attr.type === "number" || attr.type === "integer") {
180
190
  if (isNumberRange(v)) {
181
- if (v['max']) {
182
- filters.push(`${field} <= ${v['max']}`);
183
- } else if (v['top']) {
184
- filters.push(`${field} < ${v['top']}`);
185
- } else if (v['upper']) {
186
- filters.push(`${field} < ${v['upper']}`);
191
+ if (v["max"]) {
192
+ filters.push(`${field} <= ${v["max"]}`)
193
+ } else if (v["top"]) {
194
+ filters.push(`${field} < ${v["top"]}`)
195
+ } else if (v["upper"]) {
196
+ filters.push(`${field} < ${v["upper"]}`)
187
197
  }
188
- if (v['min']) {
189
- filters.push(`${field} >= ${v['min']}`);
190
- } else if (v['lower']) {
191
- filters.push(`${field} > ${v['lower']}`);
198
+ if (v["min"]) {
199
+ filters.push(`${field} >= ${v["min"]}`)
200
+ } else if (v["lower"]) {
201
+ filters.push(`${field} > ${v["lower"]}`)
192
202
  }
193
203
  }
194
204
  }
@@ -196,126 +206,125 @@ export function buildQuery<S>(filter: S, bparam: LikeType|((i: number ) => strin
196
206
  }
197
207
  }
198
208
  }
199
- const idField = getId(attrs);
200
- const c = [];
209
+ const idField = getId(attrs)
201
210
  if (idField && excluding && excluding.length > 0) {
202
- const l = excluding.length;
203
- const ps: string[] = [];
211
+ const l = excluding.length
212
+ const ps: string[] = []
204
213
  for (const k of excluding) {
205
214
  if (k != null && k !== undefined) {
206
- if (typeof k === 'number') {
207
- ps.push(k.toString());
215
+ if (typeof k === "number") {
216
+ ps.push(k.toString())
208
217
  } else {
209
- ps.push(param(i++));
210
- args.push(k);
218
+ ps.push(param(i++))
219
+ args.push(k)
211
220
  }
212
221
  }
213
222
  }
214
- filters.push(`${idField} not in (${ps.join(',')})`);
223
+ filters.push(`${idField} not in (${ps.join(",")})`)
215
224
  }
216
225
  if (q && attrs) {
217
- const qkeys = Object.keys(attrs);
218
- const qfilters: string[] = [];
226
+ const qkeys = Object.keys(attrs)
227
+ const qfilters: string[] = []
219
228
  for (const field of qkeys) {
220
- const attr = attrs[field];
221
- if (attr.q && (attr.type === undefined || attr.type === 'string') && !ex.includes(field)) {
222
- if (attr.match === 'equal') {
223
- qfilters.push(`${field} = ${param(i++)}`);
224
- args.push(q);
225
- } else if (attr.match === 'prefix') {
226
- qfilters.push(`${field} ${like} ${param(i++)}`);
227
- args.push(q + '%');
229
+ const attr = attrs[field]
230
+ if (attr.q && (attr.type === undefined || attr.type === "string") && !ex.includes(field)) {
231
+ const column = attr.column ? attr.column : field
232
+ if (attr.match === "equal") {
233
+ qfilters.push(`${column} = ${param(i++)}`)
234
+ args.push(q)
235
+ } else if (attr.match === "prefix") {
236
+ qfilters.push(`${column} ${like} ${param(i++)}`)
237
+ args.push(q + "%")
228
238
  } else {
229
- qfilters.push(`${field} ${like} ${param(i++)}`);
230
- args.push('%' + q + '%');
239
+ qfilters.push(`${column} ${like} ${param(i++)}`)
240
+ args.push("%" + q + "%")
231
241
  }
232
- c.push(buildQ(field, q, attr.match));
233
242
  }
234
243
  }
235
244
  if (qfilters.length > 0) {
236
- filters.push(`(${qfilters.join(' or ')})`);
245
+ filters.push(`(${qfilters.join(" or ")})`)
237
246
  }
238
247
  }
239
- const buildS = buildSort3 ? buildSort3 : buildSort;
240
- const sSort = buildS(sort, attrs);
241
- const sOrderBy = (sSort.length > 0 ? ` order by ${sSort}` : '');
248
+ const buildS = buildSort3 ? buildSort3 : buildSort
249
+ const sSort = buildS(sort, attrs)
250
+ const sOrderBy = sSort.length > 0 ? ` order by ${sSort}` : ""
242
251
  if (filters.length === 0) {
243
- const sql = `select ${buildFieldsByAttributes(attrs, fields)} from ${table}${sOrderBy}`;
244
- return { query: sql, params: args };
252
+ const sql = `select ${buildFieldsByAttributes(attrs, fields)} from ${table}${sOrderBy}`
253
+ return { query: sql, params: args }
245
254
  } else {
246
- const sql = `select ${buildFieldsByAttributes(attrs, fields)} from ${table} where ${filters.join(' and ')}${sOrderBy}`;
247
- return { query: sql, params: args };
255
+ const sql = `select ${buildFieldsByAttributes(attrs, fields)} from ${table} where ${filters.join(" and ")}${sOrderBy}`
256
+ return { query: sql, params: args }
248
257
  }
249
258
  }
250
- export function getId(attrs: Attributes): string|undefined {
251
- const qkeys = Object.keys(attrs);
259
+ export function getId(attrs: Attributes): string | undefined {
260
+ const qkeys = Object.keys(attrs)
252
261
  for (const key of qkeys) {
253
- const attr = attrs[key];
262
+ const attr = attrs[key]
254
263
  if (attr.key) {
255
- const field = (attr.column ? attr.column : key);
256
- return field;
264
+ const field = attr.column ? attr.column : key
265
+ return field
257
266
  }
258
267
  }
259
- return undefined;
268
+ return undefined
260
269
  }
261
270
  export function buildFieldsByAttributes(attrs: Attributes, fields?: string[]): string {
262
271
  if (!fields || fields.length === 0) {
263
- return '*';
272
+ return "*"
264
273
  }
265
- const cols: string[] = [];
274
+ const cols: string[] = []
266
275
  for (const f of fields) {
267
- const attr = attrs[f];
276
+ const attr = attrs[f]
268
277
  if (attr) {
269
- const field = (attr.column ? attr.column : f);
270
- cols.push(field);
278
+ const field = attr.column ? attr.column : f
279
+ cols.push(field)
271
280
  }
272
281
  }
273
282
  if (cols.length === 0) {
274
- return '*';
283
+ return "*"
275
284
  } else {
276
- return cols.join(',');
285
+ return cols.join(",")
277
286
  }
278
287
  }
279
288
  export function isEmpty(s: string): boolean {
280
- return !(s && s.length > 0);
289
+ return !(s && s.length > 0)
281
290
  }
282
291
  export function buildQ(field: string, q: string, match?: string): any {
283
- const o: any = {};
284
- if (match === 'equal') {
285
- o[field] = q;
286
- } else if (match === 'prefix') {
287
- o[field] = new RegExp(`^${q}`);
292
+ const o: any = {}
293
+ if (match === "equal") {
294
+ o[field] = q
295
+ } else if (match === "prefix") {
296
+ o[field] = new RegExp(`^${q}`)
288
297
  } else {
289
- o[field] = new RegExp(`\\w*${q}\\w*`);
298
+ o[field] = new RegExp(`\\w*${q}\\w*`)
290
299
  }
291
- return o;
300
+ return o
292
301
  }
293
- export function buildMatch(v: string, match: string): string|RegExp {
294
- if (match === 'equal') {
295
- return v;
296
- } else if (match === 'prefix') {
297
- return new RegExp(`^${v}`);
302
+ export function buildMatch(v: string, match: string): string | RegExp {
303
+ if (match === "equal") {
304
+ return v
305
+ } else if (match === "prefix") {
306
+ return new RegExp(`^${v}`)
298
307
  } else {
299
- return new RegExp(`\\w*${v}\\w*`);
308
+ return new RegExp(`\\w*${v}\\w*`)
300
309
  }
301
310
  }
302
311
  export function isDateRange<T>(obj: T): boolean {
303
- const keys: string[] = Object.keys(obj as any);
312
+ const keys: string[] = Object.keys(obj as any)
304
313
  for (const key of keys) {
305
- const v = (obj as any)[key];
314
+ const v = (obj as any)[key]
306
315
  if (!(v instanceof Date)) {
307
- return false;
316
+ return false
308
317
  }
309
318
  }
310
- return true;
319
+ return true
311
320
  }
312
321
  export function isNumberRange<T>(obj: T): boolean {
313
- const keys: string[] = Object.keys(obj as any);
322
+ const keys: string[] = Object.keys(obj as any)
314
323
  for (const key of keys) {
315
- const v = (obj as any)[key];
316
- if (typeof v !== 'number') {
317
- return false;
324
+ const v = (obj as any)[key]
325
+ if (typeof v !== "number") {
326
+ return false
318
327
  }
319
328
  }
320
- return true;
329
+ return true
321
330
  }