query-core 0.2.0 → 0.2.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.
package/src/batch.ts CHANGED
@@ -1,233 +1,276 @@
1
- import { buildToInsert, buildToInsertBatch, buildToUpdate, buildToUpdateBatch, version } from './build';
2
- import { Attributes, Statement } from './metadata';
1
+ import { buildToInsert, buildToInsertBatch, buildToUpdate, buildToUpdateBatch, version } from "./build"
2
+ import { Attributes, Statement } from "./metadata"
3
3
 
4
4
  export class SqlInserter<T> {
5
- version?: string;
6
- constructor(public exec: (sql: string, args?: any[]) => Promise<number>, public table: string, public attributes: Attributes, public param: (i: number) => string, public oneIfSuccess?: boolean, public map?: (v: T) => T) {
7
- this.write = this.write.bind(this);
8
- const x = version(attributes);
5
+ version?: string
6
+ constructor(
7
+ public exec: (sql: string, args?: any[]) => Promise<number>,
8
+ public table: string,
9
+ public attributes: Attributes,
10
+ public param: (i: number) => string,
11
+ public oneIfSuccess?: boolean,
12
+ public map?: (v: T) => T,
13
+ ) {
14
+ this.write = this.write.bind(this)
15
+ const x = version(attributes)
9
16
  if (x) {
10
- this.version = x.name;
17
+ this.version = x.name
11
18
  }
12
19
  }
13
20
  write(obj: T): Promise<number> {
14
21
  if (!obj) {
15
- return Promise.resolve(0);
22
+ return Promise.resolve(0)
16
23
  }
17
- let obj2: NonNullable<T> | T = obj;
24
+ let obj2: NonNullable<T> | T = obj
18
25
  if (this.map) {
19
- obj2 = this.map(obj);
26
+ obj2 = this.map(obj)
20
27
  }
21
- const stmt = buildToInsert(obj2, this.table, this.attributes, this.param, this.version);
28
+ const stmt = buildToInsert(obj2, this.table, this.attributes, this.param, this.version)
22
29
  if (stmt) {
23
30
  if (this.oneIfSuccess) {
24
- return this.exec(stmt.query, stmt.params).then(ct => ct > 0 ? 1 : 0);
31
+ return this.exec(stmt.query, stmt.params).then((ct) => (ct > 0 ? 1 : 0))
25
32
  } else {
26
- return this.exec(stmt.query, stmt.params);
33
+ return this.exec(stmt.query, stmt.params)
27
34
  }
28
35
  } else {
29
- return Promise.resolve(0);
36
+ return Promise.resolve(0)
30
37
  }
31
38
  }
32
39
  }
33
40
  // tslint:disable-next-line:max-classes-per-file
34
41
  export class SqlUpdater<T> {
35
- version?: string;
36
- constructor(public exec: (sql: string, args?: any[]) => Promise<number>, public table: string, public attributes: Attributes, public param: (i: number) => string, public oneIfSuccess?: boolean, public map?: (v: T) => T) {
37
- this.write = this.write.bind(this);
38
- const x = version(attributes);
42
+ version?: string
43
+ constructor(
44
+ public exec: (sql: string, args?: any[]) => Promise<number>,
45
+ public table: string,
46
+ public attributes: Attributes,
47
+ public param: (i: number) => string,
48
+ public oneIfSuccess?: boolean,
49
+ public map?: (v: T) => T,
50
+ ) {
51
+ this.write = this.write.bind(this)
52
+ const x = version(attributes)
39
53
  if (x) {
40
- this.version = x.name;
54
+ this.version = x.name
41
55
  }
42
56
  }
43
57
  write(obj: T): Promise<number> {
44
58
  if (!obj) {
45
- return Promise.resolve(0);
59
+ return Promise.resolve(0)
46
60
  }
47
- let obj2: NonNullable<T> | T = obj;
61
+ let obj2: NonNullable<T> | T = obj
48
62
  if (this.map) {
49
- obj2 = this.map(obj);
63
+ obj2 = this.map(obj)
50
64
  }
51
- const stmt = buildToUpdate(obj2, this.table, this.attributes, this.param, this.version);
65
+ const stmt = buildToUpdate(obj2, this.table, this.attributes, this.param, this.version)
52
66
  if (stmt) {
53
67
  if (this.oneIfSuccess) {
54
- return this.exec(stmt.query, stmt.params).then(ct => ct > 0 ? 1 : 0);
68
+ return this.exec(stmt.query, stmt.params).then((ct) => (ct > 0 ? 1 : 0))
55
69
  } else {
56
- return this.exec(stmt.query, stmt.params);
70
+ return this.exec(stmt.query, stmt.params)
57
71
  }
58
72
  } else {
59
- return Promise.resolve(0);
73
+ return Promise.resolve(0)
60
74
  }
61
75
  }
62
76
  }
63
77
  // tslint:disable-next-line:max-classes-per-file
64
78
  export class SqlBatchInserter<T> {
65
- version?: string;
66
- constructor(public exec: (sql: string, args?: any[]) => Promise<number>, public table: string, public attributes: Attributes, public param: ((i: number) => string) | boolean, public oneIfSuccess?: boolean, public map?: (v: T) => T) {
67
- this.write = this.write.bind(this);
68
- const x = version(attributes);
79
+ version?: string
80
+ constructor(
81
+ public exec: (sql: string, args?: any[]) => Promise<number>,
82
+ public table: string,
83
+ public attributes: Attributes,
84
+ public param: ((i: number) => string) | boolean,
85
+ public oneIfSuccess?: boolean,
86
+ public map?: (v: T) => T,
87
+ ) {
88
+ this.write = this.write.bind(this)
89
+ const x = version(attributes)
69
90
  if (x) {
70
- this.version = x.name;
91
+ this.version = x.name
71
92
  }
72
93
  }
73
94
  write(objs: T[]): Promise<number> {
74
95
  if (!objs || objs.length === 0) {
75
- return Promise.resolve(0);
96
+ return Promise.resolve(0)
76
97
  }
77
- let list = objs;
98
+ let list = objs
78
99
  if (this.map) {
79
- list = [];
100
+ list = []
80
101
  for (const obj of objs) {
81
- const obj2 = this.map(obj);
82
- list.push(obj2);
102
+ const obj2 = this.map(obj)
103
+ list.push(obj2)
83
104
  }
84
105
  }
85
- const stmt = buildToInsertBatch(list, this.table, this.attributes, this.param, this.version);
106
+ const stmt = buildToInsertBatch(list, this.table, this.attributes, this.param, this.version)
86
107
  if (stmt) {
87
108
  if (this.oneIfSuccess) {
88
- return this.exec(stmt.query, stmt.params).then(ct => objs.length);
109
+ return this.exec(stmt.query, stmt.params).then((ct) => objs.length)
89
110
  } else {
90
- return this.exec(stmt.query, stmt.params);
111
+ return this.exec(stmt.query, stmt.params)
91
112
  }
92
113
  } else {
93
- return Promise.resolve(0);
114
+ return Promise.resolve(0)
94
115
  }
95
116
  }
96
117
  }
97
118
  // tslint:disable-next-line:max-classes-per-file
98
119
  export class SqlBatchUpdater<T> {
99
- version?: string;
100
- constructor(public execBatch: (statements: Statement[]) => Promise<number>, public table: string, public attributes: Attributes, public param: (i: number) => string, public oneIfSuccess?: boolean, protected notSkipInvalid?: boolean, public map?: (v: T) => T) {
101
- this.write = this.write.bind(this);
102
- const x = version(attributes);
120
+ version?: string
121
+ constructor(
122
+ public execBatch: (statements: Statement[]) => Promise<number>,
123
+ public table: string,
124
+ public attributes: Attributes,
125
+ public param: (i: number) => string,
126
+ public oneIfSuccess?: boolean,
127
+ protected notSkipInvalid?: boolean,
128
+ public map?: (v: T) => T,
129
+ ) {
130
+ this.write = this.write.bind(this)
131
+ const x = version(attributes)
103
132
  if (x) {
104
- this.version = x.name;
133
+ this.version = x.name
105
134
  }
106
135
  }
107
136
  write(objs: T[]): Promise<number> {
108
137
  if (!objs || objs.length === 0) {
109
- return Promise.resolve(0);
138
+ return Promise.resolve(0)
110
139
  }
111
- let list = objs;
140
+ let list = objs
112
141
  if (this.map) {
113
- list = [];
142
+ list = []
114
143
  for (const obj of objs) {
115
- const obj2 = this.map(obj);
116
- list.push(obj2);
144
+ const obj2 = this.map(obj)
145
+ list.push(obj2)
117
146
  }
118
147
  }
119
- const stmts = buildToUpdateBatch(list, this.table, this.attributes, this.param, this.notSkipInvalid);
148
+ const stmts = buildToUpdateBatch(list, this.table, this.attributes, this.param, this.notSkipInvalid)
120
149
  if (stmts && stmts.length > 0) {
121
150
  if (this.oneIfSuccess) {
122
- return this.execBatch(stmts).then(ct => stmts.length);
151
+ return this.execBatch(stmts).then((ct) => stmts.length)
123
152
  } else {
124
- return this.execBatch(stmts);
153
+ return this.execBatch(stmts)
125
154
  }
126
155
  } else {
127
- return Promise.resolve(0);
156
+ return Promise.resolve(0)
128
157
  }
129
158
  }
130
159
  }
131
160
  // tslint:disable-next-line:max-classes-per-file
132
161
  export class StreamInserter<T> {
133
- list: T[] = [];
134
- size = 0;
135
- version?: string;
136
- map?: (v: T) => T;
137
- constructor(public exec: ((sql: string, args?: any[]) => Promise<number>), public table: string, public attributes: Attributes, public param: (i: number) => string, size?: number, toDB?: (v: T) => T) {
138
- this.write = this.write.bind(this);
139
- this.flush = this.flush.bind(this);
140
- this.map = toDB;
141
- const x = version(attributes);
162
+ list: T[] = []
163
+ size = 0
164
+ version?: string
165
+ map?: (v: T) => T
166
+ constructor(
167
+ public exec: (sql: string, args?: any[]) => Promise<number>,
168
+ public table: string,
169
+ public attributes: Attributes,
170
+ public param: (i: number) => string,
171
+ size?: number,
172
+ toDB?: (v: T) => T,
173
+ ) {
174
+ this.write = this.write.bind(this)
175
+ this.flush = this.flush.bind(this)
176
+ this.map = toDB
177
+ const x = version(attributes)
142
178
  if (x) {
143
- this.version = x.name;
179
+ this.version = x.name
144
180
  }
145
181
  if (size !== undefined && size > 0) {
146
- this.size = size;
182
+ this.size = size
147
183
  }
148
184
  }
149
185
  write(obj: T): Promise<number> {
150
186
  if (!obj) {
151
- return Promise.resolve(0);
187
+ return Promise.resolve(0)
152
188
  }
153
- let obj2: NonNullable<T> | T = obj;
189
+ let obj2: NonNullable<T> | T = obj
154
190
  if (this.map) {
155
- obj2 = this.map(obj);
156
- this.list.push(obj2);
191
+ obj2 = this.map(obj)
192
+ this.list.push(obj2)
157
193
  } else {
158
- this.list.push(obj);
194
+ this.list.push(obj)
159
195
  }
160
196
  if (this.list.length < this.size) {
161
- return Promise.resolve(0);
197
+ return Promise.resolve(0)
162
198
  } else {
163
- return this.flush();
199
+ return this.flush()
164
200
  }
165
201
  }
166
202
  flush(): Promise<number> {
167
203
  if (!this.list || this.list.length === 0) {
168
- return Promise.resolve(0);
204
+ return Promise.resolve(0)
169
205
  } else {
170
- const total = this.list.length;
171
- const stmt = buildToInsertBatch(this.list, this.table, this.attributes, this.param, this.version);
206
+ const total = this.list.length
207
+ const stmt = buildToInsertBatch(this.list, this.table, this.attributes, this.param, this.version)
172
208
  if (stmt) {
173
- return this.exec(stmt.query, stmt.params).then(r => {
174
- this.list = [];
175
- return total;
176
- });
209
+ return this.exec(stmt.query, stmt.params).then((r) => {
210
+ this.list = []
211
+ return total
212
+ })
177
213
  } else {
178
- return Promise.resolve(0);
214
+ return Promise.resolve(0)
179
215
  }
180
216
  }
181
217
  }
182
218
  }
183
219
  // tslint:disable-next-line:max-classes-per-file
184
220
  export class StreamUpdater<T> {
185
- list: T[] = [];
186
- size = 0;
187
- version?: string;
188
- map?: (v: T) => T;
189
- constructor(public execBatch: ((statements: Statement[]) => Promise<number>), public table: string, public attributes: Attributes, public param: (i: number) => string, size?: number, toDB?: (v: T) => T) {
190
- this.write = this.write.bind(this);
191
- this.flush = this.flush.bind(this);
192
- this.map = toDB;
193
- const x = version(attributes);
221
+ list: T[] = []
222
+ size = 0
223
+ version?: string
224
+ map?: (v: T) => T
225
+ constructor(
226
+ public execBatch: (statements: Statement[]) => Promise<number>,
227
+ public table: string,
228
+ public attributes: Attributes,
229
+ public param: (i: number) => string,
230
+ size?: number,
231
+ toDB?: (v: T) => T,
232
+ ) {
233
+ this.write = this.write.bind(this)
234
+ this.flush = this.flush.bind(this)
235
+ this.map = toDB
236
+ const x = version(attributes)
194
237
  if (x) {
195
- this.version = x.name;
238
+ this.version = x.name
196
239
  }
197
240
  if (size !== undefined && size > 0) {
198
- this.size = size;
241
+ this.size = size
199
242
  }
200
243
  }
201
244
  write(obj: T): Promise<number> {
202
245
  if (!obj) {
203
- return Promise.resolve(0);
246
+ return Promise.resolve(0)
204
247
  }
205
- let obj2: NonNullable<T> | T = obj;
248
+ let obj2: NonNullable<T> | T = obj
206
249
  if (this.map) {
207
- obj2 = this.map(obj);
208
- this.list.push(obj2);
250
+ obj2 = this.map(obj)
251
+ this.list.push(obj2)
209
252
  } else {
210
- this.list.push(obj);
253
+ this.list.push(obj)
211
254
  }
212
255
  if (this.list.length < this.size) {
213
- return Promise.resolve(0);
256
+ return Promise.resolve(0)
214
257
  } else {
215
- return this.flush();
258
+ return this.flush()
216
259
  }
217
260
  }
218
261
  flush(): Promise<number> {
219
262
  if (!this.list || this.list.length === 0) {
220
- return Promise.resolve(0);
263
+ return Promise.resolve(0)
221
264
  } else {
222
- const total = this.list.length;
223
- const stmt = buildToUpdateBatch(this.list, this.table, this.attributes, this.param);
265
+ const total = this.list.length
266
+ const stmt = buildToUpdateBatch(this.list, this.table, this.attributes, this.param)
224
267
  if (stmt) {
225
- return this.execBatch(stmt).then(r => {
226
- this.list = [];
227
- return total;
228
- });
268
+ return this.execBatch(stmt).then((r) => {
269
+ this.list = []
270
+ return total
271
+ })
229
272
  } else {
230
- return Promise.resolve(0);
273
+ return Promise.resolve(0)
231
274
  }
232
275
  }
233
276
  }