query-core 0.2.8 → 0.5.0

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.
@@ -21,7 +21,7 @@ exports.mssql = "mssql";
21
21
  exports.mysql = "mysql";
22
22
  exports.sqlite = "sqlite";
23
23
  var SearchBuilder = (function () {
24
- function SearchBuilder(query, table, attrs, buildQ, provider, fromDB, sort, q, excluding, buildSort, buildParam, total) {
24
+ function SearchBuilder(query, table, attrs, provider, buildQ, fromDB, sort, q, excluding, buildSort, buildParam, total) {
25
25
  this.query = query;
26
26
  this.table = table;
27
27
  this.attrs = attrs;
@@ -99,8 +99,8 @@ var SearchBuilder = (function () {
99
99
  exports.SearchBuilder = SearchBuilder;
100
100
  var SqlSearchWriter = (function (_super) {
101
101
  __extends(SqlSearchWriter, _super);
102
- function SqlSearchWriter(manager, table, attributes, buildQ, provider, toDB, fromDB, sort, q, excluding, buildSort, buildParam, total) {
103
- var _this = _super.call(this, manager.query, table, attributes, buildQ, provider, fromDB, sort, q, excluding, buildSort, buildParam, total) || this;
102
+ function SqlSearchWriter(manager, table, attributes, provider, buildQ, toDB, fromDB, sort, q, excluding, buildSort, buildParam, total) {
103
+ var _this = _super.call(this, manager.query, table, attributes, provider, buildQ, fromDB, sort, q, excluding, buildSort, buildParam, total) || this;
104
104
  _this.attributes = attributes;
105
105
  _this.toDB = toDB;
106
106
  _this.exec = manager.exec;
@@ -154,8 +154,8 @@ var SqlSearchWriter = (function (_super) {
154
154
  exports.SqlSearchWriter = SqlSearchWriter;
155
155
  var SqlRepository = (function (_super) {
156
156
  __extends(SqlRepository, _super);
157
- function SqlRepository(manager, table, attributes, buildQ, provider, toDB, fromDB, sort, q, excluding, buildSort, buildParam, total) {
158
- var _this = _super.call(this, manager, table, attributes, buildQ, provider, toDB, fromDB, sort, q, excluding, buildSort, buildParam, total) || this;
157
+ function SqlRepository(manager, table, attributes, provider, buildQ, toDB, fromDB, sort, q, excluding, buildSort, buildParam, total) {
158
+ var _this = _super.call(this, manager, table, attributes, provider, buildQ, toDB, fromDB, sort, q, excluding, buildSort, buildParam, total) || this;
159
159
  _this.attributes = attributes;
160
160
  _this.toDB = toDB;
161
161
  _this.metadata = _this.metadata.bind(_this);
@@ -216,7 +216,7 @@ exports.SqlRepository = SqlRepository;
216
216
  var Query = (function (_super) {
217
217
  __extends(Query, _super);
218
218
  function Query(query, table, attributes, buildQ, provider, fromDB, sort, q, excluding, buildSort, buildParam, total) {
219
- var _this = _super.call(this, query, table, attributes, buildQ, provider, fromDB, sort, q, excluding, buildSort, buildParam, total) || this;
219
+ var _this = _super.call(this, query, table, attributes, provider, buildQ, fromDB, sort, q, excluding, buildSort, buildParam, total) || this;
220
220
  var m = build_1.metadata(attributes);
221
221
  _this.primaryKeys = m.keys;
222
222
  _this.map = m.map;
package/lib/query.js CHANGED
@@ -99,37 +99,23 @@ function buildQuery(filter, param, sort, buildSort3, attrs, table, fields, sq, s
99
99
  if (attr.q) {
100
100
  ex.push(key);
101
101
  }
102
- if (attr.match === "equal") {
102
+ if (attr.operator === "=") {
103
103
  filters.push(field + " = " + param(i++));
104
104
  args.push(v);
105
105
  }
106
- else if (attr.match === "prefix") {
106
+ else if (attr.operator === "like") {
107
107
  filters.push(field + " " + like + " " + param(i++));
108
- args.push(v + "%");
108
+ args.push("%" + v + "%");
109
109
  }
110
110
  else {
111
111
  filters.push(field + " " + like + " " + param(i++));
112
- args.push("%" + v + "%");
112
+ args.push(v + "%");
113
113
  }
114
114
  }
115
115
  }
116
- else if (v instanceof Date) {
117
- if (attr.match === "max") {
118
- filters.push(field + " <= " + param(i++));
119
- args.push(v);
120
- }
121
- else {
122
- filters.push(field + " >= " + param(i++));
123
- args.push(v);
124
- }
125
- }
126
- else if (typeof v === "number") {
127
- if (attr.match === "max") {
128
- filters.push(field + " <= " + v);
129
- }
130
- else {
131
- filters.push(field + " >= " + v);
132
- }
116
+ else if (typeof v === "number" || v instanceof Date) {
117
+ var operator = attr.operator ? attr.operator : ">=";
118
+ filters.push(field + " " + operator + " " + param(i++));
133
119
  }
134
120
  else if (attr.type === "ObjectId") {
135
121
  filters.push(field + " = " + param(i++));
@@ -236,17 +222,17 @@ function buildQuery(filter, param, sort, buildSort3, attrs, table, fields, sq, s
236
222
  var attr = attrs[field];
237
223
  if (attr.q && (attr.type === undefined || attr.type === "string") && !ex.includes(field)) {
238
224
  var column = attr.column ? attr.column : field;
239
- if (attr.match === "equal") {
225
+ if (attr.operator === "=") {
240
226
  qfilters.push(column + " = " + param(i++));
241
227
  args.push(q);
242
228
  }
243
- else if (attr.match === "prefix") {
229
+ else if (attr.operator === "like") {
244
230
  qfilters.push(column + " " + like + " " + param(i++));
245
- args.push(q + "%");
231
+ args.push("%" + q + "%");
246
232
  }
247
233
  else {
248
234
  qfilters.push(column + " " + like + " " + param(i++));
249
- args.push("%" + q + "%");
235
+ args.push(q + "%");
250
236
  }
251
237
  }
252
238
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "query-core",
3
- "version": "0.2.8",
3
+ "version": "0.5.0",
4
4
  "description": "query",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
@@ -34,9 +34,10 @@ export class SearchBuilder<T, S> {
34
34
  protected query: <K>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any) => Promise<K[]>,
35
35
  protected table: string,
36
36
  protected attrs?: Attributes,
37
+ protected provider?: string,
37
38
  buildQ?: (
38
39
  s: S,
39
- bparam: (i: number) => string,
40
+ param: (i: number) => string,
40
41
  sort?: string,
41
42
  buildSort3?: (sort?: string, map?: Attributes | StringMap) => string,
42
43
  attrs?: Attributes,
@@ -46,7 +47,6 @@ export class SearchBuilder<T, S> {
46
47
  strExcluding?: string,
47
48
  likeType?: LikeType
48
49
  ) => Statement | undefined,
49
- protected provider?: string,
50
50
  protected fromDB?: (v: T) => T,
51
51
  protected sort?: string,
52
52
  q?: string,
@@ -122,6 +122,7 @@ export class SqlSearchWriter<T, S> extends SearchBuilder<T, S> {
122
122
  manager: Manager,
123
123
  table: string,
124
124
  protected attributes: Attributes,
125
+ provider?: string,
125
126
  buildQ?: (
126
127
  s: S,
127
128
  param: (i: number) => string,
@@ -134,7 +135,6 @@ export class SqlSearchWriter<T, S> extends SearchBuilder<T, S> {
134
135
  strExcluding?: string,
135
136
  likeType?: LikeType
136
137
  ) => Statement | undefined,
137
- provider?: string,
138
138
  protected toDB?: (v: T) => T,
139
139
  fromDB?: (v: T) => T,
140
140
  sort?: string,
@@ -144,7 +144,7 @@ export class SqlSearchWriter<T, S> extends SearchBuilder<T, S> {
144
144
  buildParam?: (i: number) => string,
145
145
  total?: string,
146
146
  ) {
147
- super(manager.query, table, attributes, buildQ, provider, fromDB, sort, q, excluding, buildSort, buildParam, total)
147
+ super(manager.query, table, attributes, provider, buildQ, fromDB, sort, q, excluding, buildSort, buildParam, total)
148
148
  this.exec = manager.exec
149
149
  const x = version(attributes)
150
150
  if (x) {
@@ -194,9 +194,10 @@ export class SqlRepository<T, ID, S> extends SqlSearchWriter<T, S> {
194
194
  manager: Manager,
195
195
  table: string,
196
196
  protected attributes: Attributes,
197
+ provider?: string,
197
198
  buildQ?: (
198
199
  s: S,
199
- bparam: LikeType | ((i: number) => string),
200
+ param: (i: number) => string,
200
201
  sort?: string,
201
202
  buildSort3?: (sort?: string, map?: Attributes | StringMap) => string,
202
203
  attrs?: Attributes,
@@ -204,8 +205,8 @@ export class SqlRepository<T, ID, S> extends SqlSearchWriter<T, S> {
204
205
  fields?: string[],
205
206
  sq?: string,
206
207
  strExcluding?: string,
208
+ likeType?: LikeType
207
209
  ) => Statement | undefined,
208
- provider?: string,
209
210
  protected toDB?: (v: T) => T,
210
211
  fromDB?: (v: T) => T,
211
212
  sort?: string,
@@ -215,7 +216,7 @@ export class SqlRepository<T, ID, S> extends SqlSearchWriter<T, S> {
215
216
  buildParam?: (i: number) => string,
216
217
  total?: string,
217
218
  ) {
218
- super(manager, table, attributes, buildQ, provider, toDB, fromDB, sort, q, excluding, buildSort, buildParam, total)
219
+ super(manager, table, attributes, provider, buildQ, toDB, fromDB, sort, q, excluding, buildSort, buildParam, total)
219
220
  this.metadata = this.metadata.bind(this)
220
221
  this.all = this.all.bind(this)
221
222
  this.load = this.load.bind(this)
@@ -277,7 +278,7 @@ export class Query<T, ID, S> extends SearchBuilder<T, S> {
277
278
  attributes: Attributes,
278
279
  buildQ?: (
279
280
  s: S,
280
- bparam: LikeType | ((i: number) => string),
281
+ param: (i: number) => string,
281
282
  sort?: string,
282
283
  buildSort3?: (sort?: string, map?: Attributes | StringMap) => string,
283
284
  attrs?: Attributes,
@@ -285,6 +286,7 @@ export class Query<T, ID, S> extends SearchBuilder<T, S> {
285
286
  fields?: string[],
286
287
  sq?: string,
287
288
  strExcluding?: string,
289
+ likeType?: LikeType
288
290
  ) => Statement | undefined,
289
291
  provider?: string,
290
292
  fromDB?: (v: T) => T,
@@ -295,7 +297,7 @@ export class Query<T, ID, S> extends SearchBuilder<T, S> {
295
297
  buildParam?: (i: number) => string,
296
298
  total?: string,
297
299
  ) {
298
- super(query, table, attributes, buildQ, provider, fromDB, sort, q, excluding, buildSort, buildParam, total)
300
+ super(query, table, attributes, provider, buildQ, fromDB, sort, q, excluding, buildSort, buildParam, total)
299
301
  const m = metadata(attributes)
300
302
  this.primaryKeys = m.keys
301
303
  this.map = m.map
package/src/metadata.ts CHANGED
@@ -40,7 +40,7 @@ export type DataType =
40
40
  | "datetimes"
41
41
  | "times"
42
42
  export type FormatType = "currency" | "percentage" | "email" | "url" | "phone" | "fax" | "ipv4" | "ipv6"
43
- export type MatchType = "equal" | "prefix" | "contain" | "max" | "min" // contain: default for string, min: default for Date, number
43
+ export type Operator = "=" | "like" | "!=" | "<>" | ">" | ">=" | "<" | "<="
44
44
 
45
45
  export interface Model {
46
46
  name?: string
@@ -55,7 +55,7 @@ export interface Attribute {
55
55
  name?: string
56
56
  column?: string
57
57
  type?: DataType
58
- match?: MatchType
58
+ operator?: Operator
59
59
  default?: string | number | Date | boolean
60
60
  key?: boolean
61
61
  q?: boolean
package/src/query.ts CHANGED
@@ -106,31 +106,20 @@ export function buildQuery<S>(
106
106
  if (attr.q) {
107
107
  ex.push(key)
108
108
  }
109
- if (attr.match === "equal") {
109
+ if (attr.operator === "=") {
110
110
  filters.push(`${field} = ${param(i++)}`)
111
111
  args.push(v)
112
- } else if (attr.match === "prefix") {
112
+ } else if (attr.operator === "like") {
113
113
  filters.push(`${field} ${like} ${param(i++)}`)
114
- args.push(v + "%")
114
+ args.push("%" + v + "%")
115
115
  } else {
116
116
  filters.push(`${field} ${like} ${param(i++)}`)
117
- args.push("%" + v + "%")
117
+ args.push(v + "%")
118
118
  }
119
119
  }
120
- } else if (v instanceof Date) {
121
- if (attr.match === "max") {
122
- filters.push(`${field} <= ${param(i++)}`)
123
- args.push(v)
124
- } else {
125
- filters.push(`${field} >= ${param(i++)}`)
126
- args.push(v)
127
- }
128
- } else if (typeof v === "number") {
129
- if (attr.match === "max") {
130
- filters.push(`${field} <= ${v}`)
131
- } else {
132
- filters.push(`${field} >= ${v}`)
133
- }
120
+ } else if (typeof v === "number" || v instanceof Date) {
121
+ const operator = attr.operator ? attr.operator : ">="
122
+ filters.push(`${field} ${operator} ${param(i++)}`)
134
123
  } else if (attr.type === "ObjectId") {
135
124
  filters.push(`${field} = ${param(i++)}`)
136
125
  args.push(v)
@@ -219,15 +208,15 @@ export function buildQuery<S>(
219
208
  const attr = attrs[field]
220
209
  if (attr.q && (attr.type === undefined || attr.type === "string") && !ex.includes(field)) {
221
210
  const column = attr.column ? attr.column : field
222
- if (attr.match === "equal") {
211
+ if (attr.operator === "=") {
223
212
  qfilters.push(`${column} = ${param(i++)}`)
224
213
  args.push(q)
225
- } else if (attr.match === "prefix") {
214
+ } else if (attr.operator === "like") {
226
215
  qfilters.push(`${column} ${like} ${param(i++)}`)
227
- args.push(q + "%")
216
+ args.push("%" + q + "%")
228
217
  } else {
229
218
  qfilters.push(`${column} ${like} ${param(i++)}`)
230
- args.push("%" + q + "%")
219
+ args.push(q + "%")
231
220
  }
232
221
  }
233
222
  }