query-core 0.1.18 → 0.1.21
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/services.js +35 -1
- package/package.json +1 -1
- package/src/batch.ts +3 -0
- package/src/index.ts +1 -0
- package/src/services.ts +42 -4
- package/tsconfig.json +1 -0
package/lib/services.js
CHANGED
|
@@ -14,6 +14,12 @@ var __extends = (this && this.__extends) || (function () {
|
|
|
14
14
|
})();
|
|
15
15
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
16
|
var build_1 = require("./build");
|
|
17
|
+
function useGet(table, q, attrs, param, fromDB) {
|
|
18
|
+
var l = new SqlLoader(table, q, attrs, param, fromDB);
|
|
19
|
+
return l.load;
|
|
20
|
+
}
|
|
21
|
+
exports.useGet = useGet;
|
|
22
|
+
exports.useLoad = useGet;
|
|
17
23
|
var SqlLoader = (function () {
|
|
18
24
|
function SqlLoader(table, query, attrs, param, fromDB) {
|
|
19
25
|
this.table = table;
|
|
@@ -139,8 +145,36 @@ var LogManager = (function () {
|
|
|
139
145
|
return this.db.param(i);
|
|
140
146
|
};
|
|
141
147
|
LogManager.prototype.exec = function (sql, args, ctx) {
|
|
148
|
+
var _this = this;
|
|
142
149
|
var t1 = new Date();
|
|
143
|
-
return this.db.exec(sql, args, ctx)
|
|
150
|
+
return this.db.exec(sql, args, ctx).then(function (v) {
|
|
151
|
+
setTimeout(function () {
|
|
152
|
+
if (_this.log) {
|
|
153
|
+
var d = diff(t1);
|
|
154
|
+
var obj = {};
|
|
155
|
+
if (_this.sql.length > 0) {
|
|
156
|
+
obj[_this.sql] = getString(sql, args);
|
|
157
|
+
}
|
|
158
|
+
if (_this.return.length > 0) {
|
|
159
|
+
obj[_this.return] = v;
|
|
160
|
+
}
|
|
161
|
+
obj[_this.duration] = d;
|
|
162
|
+
_this.log('query', obj);
|
|
163
|
+
}
|
|
164
|
+
}, 0);
|
|
165
|
+
return v;
|
|
166
|
+
}).catch(function (er) {
|
|
167
|
+
setTimeout(function () {
|
|
168
|
+
var d = diff(t1);
|
|
169
|
+
var obj = {};
|
|
170
|
+
if (_this.sql.length > 0) {
|
|
171
|
+
obj[_this.sql] = getString(sql, args);
|
|
172
|
+
}
|
|
173
|
+
obj[_this.duration] = d;
|
|
174
|
+
_this.error('error query: ' + buildString(er));
|
|
175
|
+
}, 0);
|
|
176
|
+
throw er;
|
|
177
|
+
});
|
|
144
178
|
};
|
|
145
179
|
LogManager.prototype.execBatch = function (statements, firstSuccess, ctx) {
|
|
146
180
|
var _this = this;
|
package/package.json
CHANGED
package/src/batch.ts
CHANGED
|
@@ -26,6 +26,7 @@ export class SqlInserter<T> {
|
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
|
+
// tslint:disable-next-line:max-classes-per-file
|
|
29
30
|
export class SqlUpdater<T> {
|
|
30
31
|
version?: string;
|
|
31
32
|
constructor(public exec: (sql: string, args?: any[]) => Promise<number>, public table: string, public attributes: Attributes, public param: (i: number) => string, public map?: (v: T) => T) {
|
|
@@ -51,6 +52,7 @@ export class SqlUpdater<T> {
|
|
|
51
52
|
}
|
|
52
53
|
}
|
|
53
54
|
}
|
|
55
|
+
// tslint:disable-next-line:max-classes-per-file
|
|
54
56
|
export class SqlBatchInserter<T> {
|
|
55
57
|
version?: string;
|
|
56
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) {
|
|
@@ -80,6 +82,7 @@ export class SqlBatchInserter<T> {
|
|
|
80
82
|
}
|
|
81
83
|
}
|
|
82
84
|
}
|
|
85
|
+
// tslint:disable-next-line:max-classes-per-file
|
|
83
86
|
export class SqlBatchUpdater<T> {
|
|
84
87
|
version?: string;
|
|
85
88
|
constructor(public execBatch: (statements: Statement[]) => Promise<number>, public table: string, public attributes: Attributes, public param: (i: number) => string, protected notSkipInvalid?: boolean, public map?: (v: T) => T) {
|
package/src/index.ts
CHANGED
|
@@ -54,6 +54,7 @@ export interface Config {
|
|
|
54
54
|
export class resource {
|
|
55
55
|
static string?: boolean;
|
|
56
56
|
}
|
|
57
|
+
// tslint:disable-next-line:max-classes-per-file
|
|
57
58
|
export class Loader<T> {
|
|
58
59
|
map?: StringMap;
|
|
59
60
|
constructor(public query: (sql: string, args?: any[], m?: StringMap, bools?: Attribute[]) => Promise<T[]>, public sql: string, m?: StringMap, public bools?: Attribute[]) {
|
package/src/services.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {attributes, buildToDelete, buildToInsert, buildToUpdate, exist, metadata, select,
|
|
1
|
+
import {attributes, buildToDelete, buildToInsert, buildToUpdate, exist, metadata, select, version} from './build';
|
|
2
2
|
import {Attribute, Attributes, Statement, StringMap} from './metadata';
|
|
3
3
|
import {SearchResult} from './search';
|
|
4
4
|
|
|
@@ -7,6 +7,17 @@ export interface Filter {
|
|
|
7
7
|
sort?: string;
|
|
8
8
|
q?: string;
|
|
9
9
|
}
|
|
10
|
+
export type Load<T, ID> = (id: ID, ctx?: any) => Promise<T|null>;
|
|
11
|
+
export type Get<T, ID> = Load<T, ID>;
|
|
12
|
+
export function useGet<T, ID>(table: string,
|
|
13
|
+
q: <K>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any) => Promise<K[]>,
|
|
14
|
+
attrs: Attributes|string[],
|
|
15
|
+
param: (i: number) => string,
|
|
16
|
+
fromDB?: (v: T) => T): Load<T, ID> {
|
|
17
|
+
const l = new SqlLoader<T, ID>(table, q, attrs, param, fromDB);
|
|
18
|
+
return l.load;
|
|
19
|
+
}
|
|
20
|
+
export const useLoad = useGet;
|
|
10
21
|
export class SqlLoader<T, ID> {
|
|
11
22
|
primaryKeys: Attribute[];
|
|
12
23
|
map?: StringMap;
|
|
@@ -170,7 +181,34 @@ export class LogManager implements ExtManager {
|
|
|
170
181
|
}
|
|
171
182
|
exec(sql: string, args?: any[], ctx?: any): Promise<number> {
|
|
172
183
|
const t1 = new Date();
|
|
173
|
-
return this.db.exec(sql, args, ctx)
|
|
184
|
+
return this.db.exec(sql, args, ctx).then(v => {
|
|
185
|
+
setTimeout(() => {
|
|
186
|
+
if (this.log) {
|
|
187
|
+
const d = diff(t1);
|
|
188
|
+
const obj: SimpleMap = {} ;
|
|
189
|
+
if (this.sql.length > 0) {
|
|
190
|
+
obj[this.sql] = getString(sql, args);
|
|
191
|
+
}
|
|
192
|
+
if (this.return.length > 0) {
|
|
193
|
+
obj[this.return] = v;
|
|
194
|
+
}
|
|
195
|
+
obj[this.duration] = d;
|
|
196
|
+
this.log('query', obj);
|
|
197
|
+
}
|
|
198
|
+
}, 0);
|
|
199
|
+
return v;
|
|
200
|
+
}).catch(er => {
|
|
201
|
+
setTimeout(() => {
|
|
202
|
+
const d = diff(t1);
|
|
203
|
+
const obj: SimpleMap = {};
|
|
204
|
+
if (this.sql.length > 0) {
|
|
205
|
+
obj[this.sql] = getString(sql, args);
|
|
206
|
+
}
|
|
207
|
+
obj[this.duration] = d;
|
|
208
|
+
this.error('error query: ' + buildString(er));
|
|
209
|
+
}, 0);
|
|
210
|
+
throw er;
|
|
211
|
+
});
|
|
174
212
|
}
|
|
175
213
|
execBatch(statements: Statement[], firstSuccess?: boolean, ctx?: any): Promise<number> {
|
|
176
214
|
const t1 = new Date();
|
|
@@ -416,8 +454,8 @@ export class SqlWriter<T, ID> extends SqlLoader<T, ID> {
|
|
|
416
454
|
return Promise.resolve(0);
|
|
417
455
|
}
|
|
418
456
|
}
|
|
419
|
-
patch(obj: T
|
|
420
|
-
return this.update(obj, ctx);
|
|
457
|
+
patch(obj: Partial<T>, ctx?: any): Promise<number> {
|
|
458
|
+
return this.update(obj as any, ctx);
|
|
421
459
|
}
|
|
422
460
|
delete(id: ID, ctx?: any): Promise<number> {
|
|
423
461
|
const stmt = buildToDelete<ID>(id, this.table, this.primaryKeys, this.param);
|