query-core 0.5.0 → 0.5.2

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.
@@ -38,7 +38,6 @@ var SearchBuilder = (function () {
38
38
  else {
39
39
  this.primaryKeys = [];
40
40
  }
41
- this.deleteSort = buildQ ? undefined : true;
42
41
  this.buildQuery = buildQ ? buildQ : query_1.buildQuery;
43
42
  this.buildSort = buildSort ? buildSort : query_1.buildSort;
44
43
  this.q = q && q.length > 0 ? q : "q";
@@ -70,9 +69,6 @@ var SearchBuilder = (function () {
70
69
  }
71
70
  var st = this.sort ? this.sort : "sort";
72
71
  var sn = filter[st];
73
- if (this.deleteSort) {
74
- delete filter[st];
75
- }
76
72
  var likeType = this.provider === exports.postgres ? "ilike" : "like";
77
73
  var q2 = this.buildQuery(filter, this.param, sn, this.buildSort, this.attrs, this.table, fields, this.q, this.excluding, likeType);
78
74
  if (!q2) {
@@ -99,11 +95,11 @@ var SearchBuilder = (function () {
99
95
  exports.SearchBuilder = SearchBuilder;
100
96
  var SqlSearchWriter = (function (_super) {
101
97
  __extends(SqlSearchWriter, _super);
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;
98
+ function SqlSearchWriter(db, table, attributes, buildQ, toDB, fromDB, sort, q, excluding, buildSort, buildParam, total) {
99
+ var _this = _super.call(this, db.query, table, attributes, db.driver, buildQ, fromDB, sort, q, excluding, buildSort, buildParam, total) || this;
104
100
  _this.attributes = attributes;
105
101
  _this.toDB = toDB;
106
- _this.exec = manager.exec;
102
+ _this.exec = db.exec;
107
103
  var x = build_1.version(attributes);
108
104
  if (x) {
109
105
  _this.version = x.name;
@@ -154,8 +150,8 @@ var SqlSearchWriter = (function (_super) {
154
150
  exports.SqlSearchWriter = SqlSearchWriter;
155
151
  var SqlRepository = (function (_super) {
156
152
  __extends(SqlRepository, _super);
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;
153
+ function SqlRepository(db, table, attributes, buildQ, toDB, fromDB, sort, q, excluding, buildSort, buildParam, total) {
154
+ var _this = _super.call(this, db, table, attributes, buildQ, toDB, fromDB, sort, q, excluding, buildSort, buildParam, total) || this;
159
155
  _this.attributes = attributes;
160
156
  _this.toDB = toDB;
161
157
  _this.metadata = _this.metadata.bind(_this);
package/lib/query.js CHANGED
@@ -68,14 +68,12 @@ function buildQuery(filter, param, sort, buildSort3, attrs, table, fields, sq, s
68
68
  var args = [];
69
69
  if (sq && sq.length > 0) {
70
70
  q = s[sq];
71
- delete s[sq];
72
71
  if (q === "") {
73
72
  q = undefined;
74
73
  }
75
74
  }
76
75
  if (strExcluding && strExcluding.length > 0) {
77
76
  excluding = s[strExcluding];
78
- delete s[strExcluding];
79
77
  if (typeof excluding === "string") {
80
78
  excluding = excluding.split(",");
81
79
  }
package/lib/services.js CHANGED
@@ -461,10 +461,10 @@ var SqlWriter = (function () {
461
461
  exports.SqlWriter = SqlWriter;
462
462
  var CRUDRepository = (function (_super) {
463
463
  __extends(CRUDRepository, _super);
464
- function CRUDRepository(manager, table, attributes, toDB, fromDB) {
465
- var _this = _super.call(this, manager.exec, manager.param, table, attributes, toDB) || this;
464
+ function CRUDRepository(db, table, attributes, toDB, fromDB) {
465
+ var _this = _super.call(this, db.exec, db.param, table, attributes, toDB) || this;
466
466
  _this.fromDB = fromDB;
467
- _this.query = manager.query;
467
+ _this.query = db.query;
468
468
  var m = build_1.metadata(attributes);
469
469
  _this.primaryKeys = m.keys;
470
470
  _this.map = m.map;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "query-core",
3
- "version": "0.5.0",
3
+ "version": "0.5.2",
4
4
  "description": "query",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
@@ -1,4 +1,4 @@
1
- import { Manager } from "./services"
1
+ import { DB, Manager } from "./services"
2
2
  import { buildToDelete, buildToInsert, buildToUpdate, exist, metadata, param, select, version } from "./build"
3
3
  import { Attribute, Attributes, Statement, StringMap } from "./metadata"
4
4
  import { buildSort as bs, buildDollarParam, buildMsSQLParam, buildOracleParam, buildQuery, LikeType } from "./query"
@@ -12,7 +12,6 @@ export class SearchBuilder<T, S> {
12
12
  map?: StringMap
13
13
  bools?: Attribute[]
14
14
  primaryKeys: Attribute[]
15
- protected deleteSort?: boolean
16
15
  buildQuery: (
17
16
  s: S,
18
17
  param: (i: number) => string,
@@ -64,7 +63,6 @@ export class SearchBuilder<T, S> {
64
63
  } else {
65
64
  this.primaryKeys = []
66
65
  }
67
- this.deleteSort = buildQ ? undefined : true
68
66
  this.buildQuery = buildQ ? buildQ : buildQuery
69
67
  this.buildSort = buildSort ? buildSort : bs
70
68
  this.q = q && q.length > 0 ? q : "q"
@@ -92,9 +90,6 @@ export class SearchBuilder<T, S> {
92
90
  }
93
91
  const st = this.sort ? this.sort : "sort"
94
92
  const sn = (filter as any)[st] as string
95
- if (this.deleteSort) {
96
- delete (filter as any)[st]
97
- }
98
93
  const likeType = this.provider === postgres ? "ilike" : "like"
99
94
  const q2 = this.buildQuery(filter, this.param, sn, this.buildSort, this.attrs, this.table, fields, this.q, this.excluding, likeType)
100
95
  if (!q2) {
@@ -119,10 +114,9 @@ export class SqlSearchWriter<T, S> extends SearchBuilder<T, S> {
119
114
  protected version?: string
120
115
  protected exec: (sql: string, args?: any[], ctx?: any) => Promise<number>
121
116
  constructor(
122
- manager: Manager,
117
+ db: DB,
123
118
  table: string,
124
119
  protected attributes: Attributes,
125
- provider?: string,
126
120
  buildQ?: (
127
121
  s: S,
128
122
  param: (i: number) => string,
@@ -144,8 +138,8 @@ export class SqlSearchWriter<T, S> extends SearchBuilder<T, S> {
144
138
  buildParam?: (i: number) => string,
145
139
  total?: string,
146
140
  ) {
147
- super(manager.query, table, attributes, provider, buildQ, fromDB, sort, q, excluding, buildSort, buildParam, total)
148
- this.exec = manager.exec
141
+ super(db.query, table, attributes, db.driver, buildQ, fromDB, sort, q, excluding, buildSort, buildParam, total)
142
+ this.exec = db.exec
149
143
  const x = version(attributes)
150
144
  if (x) {
151
145
  this.version = x.name
@@ -191,10 +185,9 @@ export class SqlSearchWriter<T, S> extends SearchBuilder<T, S> {
191
185
  // tslint:disable-next-line:max-classes-per-file
192
186
  export class SqlRepository<T, ID, S> extends SqlSearchWriter<T, S> {
193
187
  constructor(
194
- manager: Manager,
188
+ db: DB,
195
189
  table: string,
196
190
  protected attributes: Attributes,
197
- provider?: string,
198
191
  buildQ?: (
199
192
  s: S,
200
193
  param: (i: number) => string,
@@ -216,7 +209,7 @@ export class SqlRepository<T, ID, S> extends SqlSearchWriter<T, S> {
216
209
  buildParam?: (i: number) => string,
217
210
  total?: string,
218
211
  ) {
219
- super(manager, table, attributes, provider, buildQ, toDB, fromDB, sort, q, excluding, buildSort, buildParam, total)
212
+ super(db, table, attributes, buildQ, toDB, fromDB, sort, q, excluding, buildSort, buildParam, total)
220
213
  this.metadata = this.metadata.bind(this)
221
214
  this.all = this.all.bind(this)
222
215
  this.load = this.load.bind(this)
package/src/query.ts CHANGED
@@ -76,14 +76,14 @@ export function buildQuery<S>(
76
76
  const args: any[] = []
77
77
  if (sq && sq.length > 0) {
78
78
  q = s[sq]
79
- delete s[sq]
79
+ //delete s[sq]
80
80
  if (q === "") {
81
81
  q = undefined
82
82
  }
83
83
  }
84
84
  if (strExcluding && strExcluding.length > 0) {
85
85
  excluding = s[strExcluding]
86
- delete s[strExcluding]
86
+ //delete s[strExcluding]
87
87
  if (typeof excluding === "string") {
88
88
  excluding = (excluding as string).split(",")
89
89
  }
package/src/services.ts CHANGED
@@ -112,190 +112,16 @@ export class QueryRepository<T, ID> {
112
112
  return this.db.query<T>(sql, ids, this.map, this.bools)
113
113
  }
114
114
  }
115
- /*
116
- // tslint:disable-next-line:max-classes-per-file
117
- export class SqlLoadRepository<T, K1, K2> {
118
- map?: StringMap
119
- attributes: Attributes
120
- bools?: Attribute[]
121
- id1Col: string
122
- id2Col: string
123
- constructor(
124
- public query: <K>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any) => Promise<K[]>,
125
- public table: string,
126
- attrs: Attributes,
127
- public param: (i: number) => string,
128
- public id1Field: string,
129
- public id2Field: string,
130
- public fromDB?: (v: T) => T,
131
- id1Col?: string,
132
- id2Col?: string,
133
- ) {
134
- const m = metadata(attrs)
135
- this.attributes = attrs
136
- this.map = m.map
137
- this.bools = m.bools
138
115
 
139
- if (this.metadata) {
140
- this.metadata = this.metadata.bind(this)
141
- }
142
- this.all = this.all.bind(this)
143
- this.load = this.load.bind(this)
144
- this.exist = this.exist.bind(this)
145
- if (id1Col && id1Col.length > 0) {
146
- this.id1Col = id1Col
147
- } else {
148
- const c = attrs[this.id1Field]
149
- if (c) {
150
- this.id1Col = c.column && c.column.length > 0 ? c.column : this.id1Field
151
- } else {
152
- this.id1Col = this.id1Field
153
- }
154
- }
155
- if (id2Col && id2Col.length > 0) {
156
- this.id2Col = id2Col
157
- } else {
158
- const c = attrs[this.id2Field]
159
- if (c) {
160
- this.id2Col = c.column && c.column.length > 0 ? c.column : this.id2Field
161
- } else {
162
- this.id2Col = this.id2Field
163
- }
164
- }
165
- }
166
- metadata?(): Attributes | undefined {
167
- return this.attributes
168
- }
169
- all(): Promise<T[]> {
170
- const sql = `select * from ${this.table}`
171
- return this.query(sql, [], this.map)
172
- }
173
- load(id1: K1, id2: K2, ctx?: any): Promise<T | null> {
174
- return this.query<T>(
175
- `select * from ${this.table} where ${this.id1Col} = ${this.param(1)} and ${this.id2Col} = ${this.param(2)}`,
176
- [id1, id2],
177
- this.map,
178
- undefined,
179
- ctx,
180
- ).then((objs) => {
181
- if (!objs || objs.length === 0) {
182
- return null
183
- } else {
184
- const fn = this.fromDB
185
- if (fn) {
186
- return fn(objs[0])
187
- } else {
188
- return objs[0]
189
- }
190
- }
191
- })
192
- }
193
- exist(id1: K1, id2: K2, ctx?: any): Promise<boolean> {
194
- return this.query<T>(
195
- `select ${this.id1Col} from ${this.table} where ${this.id1Col} = ${this.param(1)} and ${this.id2Col} = ${this.param(2)}`,
196
- [id1, id2],
197
- undefined,
198
- undefined,
199
- ctx,
200
- ).then((objs) => {
201
- return objs && objs.length > 0 ? true : false
202
- })
203
- }
204
- }
205
- // tslint:disable-next-line:max-classes-per-file
206
- export class GenericRepository<T, K1, K2> extends SqlLoadRepository<T, K1, K2> {
207
- version?: string
208
- exec: (sql: string, args?: any[], ctx?: any) => Promise<number>
209
- // execBatch: (statements: Statement[], firstSuccess?: boolean, ctx?: any) => Promise<number>
210
- constructor(
211
- manager: Manager,
212
- table: string,
213
- attrs: Attributes,
214
- id1Field: string,
215
- id2Field: string,
216
- public toDB?: (v: T) => T,
217
- fromDB?: (v: T) => T,
218
- id1Col?: string,
219
- id2Col?: string,
220
- ) {
221
- super(manager.query, table, attrs, manager.param, id1Field, id2Field, fromDB, id1Col, id2Col)
222
- const x = version(attrs)
223
- this.exec = manager.exec
224
- // this.execBatch = manager.execBatch
225
- if (x) {
226
- this.version = x.name
227
- }
228
- this.create = this.create.bind(this)
229
- this.update = this.update.bind(this)
230
- this.patch = this.patch.bind(this)
231
- this.delete = this.delete.bind(this)
232
- }
233
- create(obj: T, ctx?: any): Promise<number> {
234
- let obj2 = obj
235
- if (this.toDB) {
236
- obj2 = this.toDB(obj)
237
- }
238
- const stmt = buildToInsert(obj2, this.table, this.attributes, this.param, this.version)
239
- if (stmt) {
240
- return this.exec(stmt.query, stmt.params, ctx).catch((err) => {
241
- if (err && err.error === "duplicate") {
242
- return 0
243
- } else {
244
- throw err
245
- }
246
- })
247
- } else {
248
- return Promise.resolve(0)
249
- }
250
- }
251
- update(obj: T, ctx?: any): Promise<number> {
252
- let obj2 = obj
253
- if (this.toDB) {
254
- obj2 = this.toDB(obj)
255
- }
256
- const stmt = buildToUpdate(obj2, this.table, this.attributes, this.param, this.version)
257
- if (stmt) {
258
- return this.exec(stmt.query, stmt.params, ctx)
259
- } else {
260
- return Promise.resolve(0)
261
- }
262
- }
263
- patch(obj: Partial<T>, ctx?: any): Promise<number> {
264
- return this.update(obj as any, ctx)
265
- }
266
- delete(id1: K1, id2: K2, ctx?: any): Promise<number> {
267
- return this.exec(`delete from ${this.table} where ${this.id1Col} = ${this.param(1)} and ${this.id2Col} = ${this.param(2)}`, [id1, id2], ctx)
268
- }
269
- }
270
- */
271
- /*
272
- // tslint:disable-next-line:max-classes-per-file
273
- export class SqlSearchLoader<T, ID, S extends Filter> extends SqlLoader<T, ID> {
274
- constructor(
275
- protected find: (s: S, limit: number, offset?: number | string, fields?: string[]) => Promise<SearchResult<T>>,
276
- query: <K>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any) => Promise<K[]>,
277
- table: string,
278
- attrs: Attributes | string[],
279
- param: (i: number) => string,
280
- fromDB?: (v: T) => T,
281
- ) {
282
- super(query, table, attrs, param, fromDB)
283
- this.search = this.search.bind(this)
284
- }
285
- search(s: S, limit: number, offset?: number | string, fields?: string[]): Promise<SearchResult<T>> {
286
- return this.find(s, limit, offset, fields)
287
- }
288
- }
289
- */
290
- export interface Manager {
116
+ export interface DB {
291
117
  driver: string
292
118
  param(i: number): string
293
119
  exec(sql: string, args?: any[], ctx?: any): Promise<number>
294
120
  execBatch(statements: Statement[], firstSuccess?: boolean, ctx?: any): Promise<number>
295
121
  query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T[]>
296
122
  }
297
- export type DB = Manager
298
- export interface ExtManager {
123
+ export type Manager = DB
124
+ export interface FullDB {
299
125
  driver: string
300
126
  param(i: number): string
301
127
  exec(sql: string, args?: any[], ctx?: any): Promise<number>
@@ -305,6 +131,7 @@ export interface ExtManager {
305
131
  execScalar<T>(sql: string, args?: any[], ctx?: any): Promise<T>
306
132
  count(sql: string, args?: any[], ctx?: any): Promise<number>
307
133
  }
134
+ export type ExtManager = FullDB
308
135
  export interface SimpleMap {
309
136
  [key: string]: string | number | boolean | Date
310
137
  }
@@ -632,18 +459,14 @@ const getDurationInMilliseconds = (start: [number, number] | undefined) => {
632
459
  // tslint:disable-next-line:max-classes-per-file
633
460
  export class SqlWriter<T> {
634
461
  protected version?: string
635
- // execBatch: (statements: Statement[], firstSuccess?: boolean, ctx?: any) => Promise<number>
636
462
  constructor(protected exec: (sql: string, args?: any[], ctx?: any) => Promise<number>, protected param: (i: number) => string, protected table: string, protected attributes: Attributes, public toDB?: (v: T) => T) {
637
- // super(manager.query, table, attrs, manager.param, fromDB)
638
463
  const x = version(attributes)
639
- // this.execBatch = manager.execBatch
640
464
  if (x) {
641
465
  this.version = x.name
642
466
  }
643
467
  this.create = this.create.bind(this)
644
468
  this.update = this.update.bind(this)
645
469
  this.patch = this.patch.bind(this)
646
- // this.delete = this.delete.bind(this)
647
470
  }
648
471
  create(obj: T, ctx?: any): Promise<number> {
649
472
  let obj2 = obj
@@ -678,26 +501,15 @@ export class SqlWriter<T> {
678
501
  patch(obj: Partial<T>, ctx?: any): Promise<number> {
679
502
  return this.update(obj as any, ctx)
680
503
  }
681
- /*
682
- delete(id: ID, ctx?: any): Promise<number> {
683
- const stmt = buildToDelete<ID>(id, this.table, this.primaryKeys, this.param)
684
- if (stmt) {
685
- return this.exec(stmt.query, stmt.params, ctx)
686
- } else {
687
- return Promise.resolve(0)
688
- }
689
- }
690
- */
691
504
  }
692
505
  export class CRUDRepository<T, ID> extends SqlWriter<T> {
693
506
  protected primaryKeys: Attribute[]
694
507
  protected map?: StringMap
695
- // attributes: Attributes;
696
508
  protected bools?: Attribute[]
697
509
  protected query: <K>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any) => Promise<K[]>
698
- constructor(manager: Manager, table: string, attributes: Attributes, toDB?: (v: T) => T, protected fromDB?: (v: T) => T) {
699
- super(manager.exec, manager.param, table, attributes, toDB)
700
- this.query = manager.query
510
+ constructor(db: DB, table: string, attributes: Attributes, toDB?: (v: T) => T, protected fromDB?: (v: T) => T) {
511
+ super(db.exec, db.param, table, attributes, toDB)
512
+ this.query = db.query
701
513
  const m = metadata(attributes)
702
514
  this.primaryKeys = m.keys
703
515
  this.map = m.map
@@ -751,22 +563,3 @@ export class CRUDRepository<T, ID> extends SqlWriter<T> {
751
563
  }
752
564
  }
753
565
  }
754
- /*
755
- // tslint:disable-next-line:max-classes-per-file
756
- export class SqlSearchWriter<T, ID, S extends Filter> extends SqlWriter<T, ID> {
757
- constructor(
758
- protected find: (s: S, limit: number, offset?: number | string, fields?: string[]) => Promise<SearchResult<T>>,
759
- manager: Manager,
760
- table: string,
761
- attrs: Attributes,
762
- toDB?: (v: T) => T,
763
- fromDB?: (v: T) => T,
764
- ) {
765
- super(manager, table, attrs, toDB, fromDB)
766
- this.search = this.search.bind(this)
767
- }
768
- search(s: S, limit: number, offset?: number | string, fields?: string[]): Promise<SearchResult<T>> {
769
- return this.find(s, limit, offset, fields)
770
- }
771
- }
772
- */