query-core 0.1.24 → 0.1.26
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/batch.js +120 -0
- package/lib/services.js +1 -0
- package/package.json +1 -1
- package/src/batch.ts +109 -5
- package/src/index.ts +1 -1
- package/src/map.ts +1 -1
- package/src/query.ts +2 -2
- package/src/search.ts +1 -1
- package/src/services.ts +1 -0
package/lib/batch.js
CHANGED
|
@@ -140,3 +140,123 @@ var SqlBatchUpdater = (function () {
|
|
|
140
140
|
return SqlBatchUpdater;
|
|
141
141
|
}());
|
|
142
142
|
exports.SqlBatchUpdater = SqlBatchUpdater;
|
|
143
|
+
var StreamInserter = (function () {
|
|
144
|
+
function StreamInserter(exec, table, attributes, param, size, toDB) {
|
|
145
|
+
this.exec = exec;
|
|
146
|
+
this.table = table;
|
|
147
|
+
this.attributes = attributes;
|
|
148
|
+
this.param = param;
|
|
149
|
+
this.list = [];
|
|
150
|
+
this.size = 0;
|
|
151
|
+
this.write = this.write.bind(this);
|
|
152
|
+
this.flush = this.flush.bind(this);
|
|
153
|
+
this.map = toDB;
|
|
154
|
+
var x = build_1.version(attributes);
|
|
155
|
+
if (x) {
|
|
156
|
+
this.version = x.name;
|
|
157
|
+
}
|
|
158
|
+
if (size !== undefined && size > 0) {
|
|
159
|
+
this.size = size;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
StreamInserter.prototype.write = function (obj) {
|
|
163
|
+
if (!obj) {
|
|
164
|
+
return Promise.resolve(0);
|
|
165
|
+
}
|
|
166
|
+
var obj2 = obj;
|
|
167
|
+
if (this.map) {
|
|
168
|
+
obj2 = this.map(obj);
|
|
169
|
+
this.list.push(obj2);
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
this.list.push(obj);
|
|
173
|
+
}
|
|
174
|
+
if (this.list.length < this.size) {
|
|
175
|
+
return Promise.resolve(0);
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
return this.flush();
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
StreamInserter.prototype.flush = function () {
|
|
182
|
+
var _this = this;
|
|
183
|
+
if (!this.list || this.list.length === 0) {
|
|
184
|
+
return Promise.resolve(0);
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
var total_1 = this.list.length;
|
|
188
|
+
var stmt = build_1.buildToInsertBatch(this.list, this.table, this.attributes, this.param, this.version);
|
|
189
|
+
if (stmt) {
|
|
190
|
+
return this.exec(stmt.query, stmt.params).then(function (r) {
|
|
191
|
+
_this.list = [];
|
|
192
|
+
return total_1;
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
return Promise.resolve(0);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
return StreamInserter;
|
|
201
|
+
}());
|
|
202
|
+
exports.StreamInserter = StreamInserter;
|
|
203
|
+
var StreamUpdater = (function () {
|
|
204
|
+
function StreamUpdater(execBatch, table, attributes, param, size, toDB) {
|
|
205
|
+
this.execBatch = execBatch;
|
|
206
|
+
this.table = table;
|
|
207
|
+
this.attributes = attributes;
|
|
208
|
+
this.param = param;
|
|
209
|
+
this.list = [];
|
|
210
|
+
this.size = 0;
|
|
211
|
+
this.write = this.write.bind(this);
|
|
212
|
+
this.flush = this.flush.bind(this);
|
|
213
|
+
this.map = toDB;
|
|
214
|
+
var x = build_1.version(attributes);
|
|
215
|
+
if (x) {
|
|
216
|
+
this.version = x.name;
|
|
217
|
+
}
|
|
218
|
+
if (size !== undefined && size > 0) {
|
|
219
|
+
this.size = size;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
StreamUpdater.prototype.write = function (obj) {
|
|
223
|
+
if (!obj) {
|
|
224
|
+
return Promise.resolve(0);
|
|
225
|
+
}
|
|
226
|
+
var obj2 = obj;
|
|
227
|
+
if (this.map) {
|
|
228
|
+
obj2 = this.map(obj);
|
|
229
|
+
this.list.push(obj2);
|
|
230
|
+
}
|
|
231
|
+
else {
|
|
232
|
+
this.list.push(obj);
|
|
233
|
+
}
|
|
234
|
+
if (this.list.length < this.size) {
|
|
235
|
+
return Promise.resolve(0);
|
|
236
|
+
}
|
|
237
|
+
else {
|
|
238
|
+
return this.flush();
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
StreamUpdater.prototype.flush = function () {
|
|
242
|
+
var _this = this;
|
|
243
|
+
if (!this.list || this.list.length === 0) {
|
|
244
|
+
return Promise.resolve(0);
|
|
245
|
+
}
|
|
246
|
+
else {
|
|
247
|
+
var total_2 = this.list.length;
|
|
248
|
+
var stmt = build_1.buildToUpdateBatch(this.list, this.table, this.attributes, this.param);
|
|
249
|
+
if (stmt) {
|
|
250
|
+
return this.execBatch(stmt).then(function (r) {
|
|
251
|
+
_this.list = [];
|
|
252
|
+
return total_2;
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
else {
|
|
256
|
+
return Promise.resolve(0);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
return StreamUpdater;
|
|
261
|
+
}());
|
|
262
|
+
exports.StreamUpdater = StreamUpdater;
|
package/lib/services.js
CHANGED
package/package.json
CHANGED
package/src/batch.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
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
5
|
version?: string;
|
|
@@ -14,7 +14,7 @@ export class SqlInserter<T> {
|
|
|
14
14
|
if (!obj) {
|
|
15
15
|
return Promise.resolve(0);
|
|
16
16
|
}
|
|
17
|
-
let obj2 = obj;
|
|
17
|
+
let obj2: NonNullable<T> | T = obj;
|
|
18
18
|
if (this.map) {
|
|
19
19
|
obj2 = this.map(obj);
|
|
20
20
|
}
|
|
@@ -40,7 +40,7 @@ export class SqlUpdater<T> {
|
|
|
40
40
|
if (!obj) {
|
|
41
41
|
return Promise.resolve(0);
|
|
42
42
|
}
|
|
43
|
-
let obj2 = obj;
|
|
43
|
+
let obj2: NonNullable<T> | T = obj;
|
|
44
44
|
if (this.map) {
|
|
45
45
|
obj2 = this.map(obj);
|
|
46
46
|
}
|
|
@@ -55,7 +55,7 @@ export class SqlUpdater<T> {
|
|
|
55
55
|
// tslint:disable-next-line:max-classes-per-file
|
|
56
56
|
export class SqlBatchInserter<T> {
|
|
57
57
|
version?: string;
|
|
58
|
-
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) {
|
|
58
|
+
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) {
|
|
59
59
|
this.write = this.write.bind(this);
|
|
60
60
|
const x = version(attributes);
|
|
61
61
|
if (x) {
|
|
@@ -112,3 +112,107 @@ export class SqlBatchUpdater<T> {
|
|
|
112
112
|
}
|
|
113
113
|
}
|
|
114
114
|
}
|
|
115
|
+
// tslint:disable-next-line:max-classes-per-file
|
|
116
|
+
export class StreamInserter<T> {
|
|
117
|
+
list: T[] = [];
|
|
118
|
+
size: number = 0;
|
|
119
|
+
version?: string;
|
|
120
|
+
map?: (v: T) => T;
|
|
121
|
+
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) {
|
|
122
|
+
this.write = this.write.bind(this);
|
|
123
|
+
this.flush = this.flush.bind(this);
|
|
124
|
+
this.map = toDB;
|
|
125
|
+
const x = version(attributes);
|
|
126
|
+
if (x) {
|
|
127
|
+
this.version = x.name;
|
|
128
|
+
}
|
|
129
|
+
if (size !== undefined && size > 0) {
|
|
130
|
+
this.size = size;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
write(obj: T): Promise<number> {
|
|
134
|
+
if (!obj) {
|
|
135
|
+
return Promise.resolve(0);
|
|
136
|
+
}
|
|
137
|
+
let obj2: NonNullable<T> | T = obj;
|
|
138
|
+
if (this.map) {
|
|
139
|
+
obj2 = this.map(obj);
|
|
140
|
+
this.list.push(obj2);
|
|
141
|
+
} else {
|
|
142
|
+
this.list.push(obj);
|
|
143
|
+
}
|
|
144
|
+
if (this.list.length < this.size) {
|
|
145
|
+
return Promise.resolve(0);
|
|
146
|
+
} else {
|
|
147
|
+
return this.flush();
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
flush(): Promise<number> {
|
|
151
|
+
if (!this.list || this.list.length === 0) {
|
|
152
|
+
return Promise.resolve(0);
|
|
153
|
+
} else {
|
|
154
|
+
const total = this.list.length;
|
|
155
|
+
const stmt = buildToInsertBatch(this.list, this.table, this.attributes, this.param, this.version);
|
|
156
|
+
if (stmt) {
|
|
157
|
+
return this.exec(stmt.query, stmt.params).then(r => {
|
|
158
|
+
this.list = [];
|
|
159
|
+
return total;
|
|
160
|
+
});
|
|
161
|
+
} else {
|
|
162
|
+
return Promise.resolve(0);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
// tslint:disable-next-line:max-classes-per-file
|
|
168
|
+
export class StreamUpdater<T> {
|
|
169
|
+
list: T[] = [];
|
|
170
|
+
size: number = 0;
|
|
171
|
+
version?: string;
|
|
172
|
+
map?: (v: T) => T;
|
|
173
|
+
constructor(public execBatch: ((statements: Statement[]) => Promise<number>), public table: string, public attributes: Attributes, public param: (i: number) => string, size?: number, toDB?: (v: T) => T) {
|
|
174
|
+
this.write = this.write.bind(this);
|
|
175
|
+
this.flush = this.flush.bind(this);
|
|
176
|
+
this.map = toDB;
|
|
177
|
+
const x = version(attributes);
|
|
178
|
+
if (x) {
|
|
179
|
+
this.version = x.name;
|
|
180
|
+
}
|
|
181
|
+
if (size !== undefined && size > 0) {
|
|
182
|
+
this.size = size;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
write(obj: T): Promise<number> {
|
|
186
|
+
if (!obj) {
|
|
187
|
+
return Promise.resolve(0);
|
|
188
|
+
}
|
|
189
|
+
let obj2: NonNullable<T> | T = obj;
|
|
190
|
+
if (this.map) {
|
|
191
|
+
obj2 = this.map(obj);
|
|
192
|
+
this.list.push(obj2);
|
|
193
|
+
} else {
|
|
194
|
+
this.list.push(obj);
|
|
195
|
+
}
|
|
196
|
+
if (this.list.length < this.size) {
|
|
197
|
+
return Promise.resolve(0);
|
|
198
|
+
} else {
|
|
199
|
+
return this.flush();
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
flush(): Promise<number> {
|
|
203
|
+
if (!this.list || this.list.length === 0) {
|
|
204
|
+
return Promise.resolve(0);
|
|
205
|
+
} else {
|
|
206
|
+
const total = this.list.length;
|
|
207
|
+
const stmt = buildToUpdateBatch(this.list, this.table, this.attributes, this.param);
|
|
208
|
+
if (stmt) {
|
|
209
|
+
return this.execBatch(stmt).then(r => {
|
|
210
|
+
this.list = [];
|
|
211
|
+
return total;
|
|
212
|
+
});
|
|
213
|
+
} else {
|
|
214
|
+
return Promise.resolve(0);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
package/src/index.ts
CHANGED
package/src/map.ts
CHANGED
|
@@ -13,7 +13,7 @@ export function mapArray<T>(results: T[], m?: StringMap): T[] {
|
|
|
13
13
|
for (let i = 0; i < length; i++) {
|
|
14
14
|
const obj = results[i];
|
|
15
15
|
const obj2: any = {};
|
|
16
|
-
const keys = Object.keys(obj);
|
|
16
|
+
const keys = Object.keys(obj as any);
|
|
17
17
|
for (const key of keys) {
|
|
18
18
|
let k0 = m[key];
|
|
19
19
|
if (!k0) {
|
package/src/query.ts
CHANGED
|
@@ -300,7 +300,7 @@ export function buildMatch(v: string, match: string): string|RegExp {
|
|
|
300
300
|
}
|
|
301
301
|
}
|
|
302
302
|
export function isDateRange<T>(obj: T): boolean {
|
|
303
|
-
const keys: string[] = Object.keys(obj);
|
|
303
|
+
const keys: string[] = Object.keys(obj as any);
|
|
304
304
|
for (const key of keys) {
|
|
305
305
|
const v = (obj as any)[key];
|
|
306
306
|
if (!(v instanceof Date)) {
|
|
@@ -310,7 +310,7 @@ export function isDateRange<T>(obj: T): boolean {
|
|
|
310
310
|
return true;
|
|
311
311
|
}
|
|
312
312
|
export function isNumberRange<T>(obj: T): boolean {
|
|
313
|
-
const keys: string[] = Object.keys(obj);
|
|
313
|
+
const keys: string[] = Object.keys(obj as any);
|
|
314
314
|
for (const key of keys) {
|
|
315
315
|
const v = (obj as any)[key];
|
|
316
316
|
if (typeof v !== 'number') {
|
package/src/search.ts
CHANGED