query-core 0.2.0 → 0.2.1
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/query.js +83 -84
- package/package.json +1 -1
- package/src/SearchBuilder.ts +134 -101
- package/src/batch.ts +149 -106
- package/src/build.ts +426 -382
- package/src/client.ts +139 -107
- package/src/health.ts +38 -38
- package/src/index.ts +74 -69
- package/src/map.ts +27 -27
- package/src/metadata.ts +74 -57
- package/src/query.ts +198 -189
- package/src/search.ts +89 -72
- package/src/services.ts +474 -423
package/src/client.ts
CHANGED
|
@@ -1,202 +1,234 @@
|
|
|
1
|
-
import {buildToInsert, buildToInsertBatch, buildToUpdate, buildToUpdateBatch} from
|
|
2
|
-
import {handleResults} from
|
|
3
|
-
import {Attribute, Attributes, Statement, StringMap} from
|
|
1
|
+
import { buildToInsert, buildToInsertBatch, buildToUpdate, buildToUpdateBatch } from "./build"
|
|
2
|
+
import { handleResults } from "./map"
|
|
3
|
+
import { Attribute, Attributes, Statement, StringMap } from "./metadata"
|
|
4
4
|
|
|
5
5
|
export interface JStatement {
|
|
6
|
-
query: string
|
|
7
|
-
params?: any[]
|
|
8
|
-
dates?: number[]
|
|
6
|
+
query: string
|
|
7
|
+
params?: any[]
|
|
8
|
+
dates?: number[]
|
|
9
9
|
}
|
|
10
10
|
export interface Headers {
|
|
11
|
-
[key: string]: any
|
|
11
|
+
[key: string]: any
|
|
12
12
|
}
|
|
13
13
|
export interface HttpOptionsService {
|
|
14
|
-
getHttpOptions(): { headers?: Headers }
|
|
14
|
+
getHttpOptions(): { headers?: Headers }
|
|
15
15
|
}
|
|
16
16
|
export interface HttpRequest {
|
|
17
|
-
post<T>(url: string, obj: any, options?: {headers?: Headers}): Promise<T
|
|
17
|
+
post<T>(url: string, obj: any, options?: { headers?: Headers }): Promise<T>
|
|
18
18
|
}
|
|
19
19
|
export interface Proxy {
|
|
20
|
-
query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[]): Promise<T[]
|
|
21
|
-
exec(sql: string, args?: any[]): Promise<number
|
|
22
|
-
execBatch(stmts: Statement[]): Promise<number
|
|
23
|
-
beginTransaction?(timeout?: number): Promise<string
|
|
24
|
-
commitTransaction?(tx: string): Promise<boolean
|
|
25
|
-
rollbackTransaction?(tx: string): Promise<boolean
|
|
26
|
-
queryWithTx?<T>(tx: string, commit: boolean, sql: string, args?: any[], m?: StringMap, bools?: Attribute[]): Promise<T[]
|
|
27
|
-
execWithTx?(tx: string, commit: boolean, sql: string, args?: any[]): Promise<number
|
|
28
|
-
execBatchWithTx?(tx: string, commit: boolean, stmts: Statement[]): Promise<number
|
|
29
|
-
insert?<T>(table: string, attrs: Attributes, obj: T, buildParam: (i: number) => string, ver?: string): Promise<number
|
|
30
|
-
update?<T>(table: string, attrs: Attributes, obj: T, buildParam: (i: number) => string, ver?: string): Promise<number
|
|
31
|
-
insertBatch?<T>(table: string, attrs: Attributes, objs: T[], buildParam: (i: number) => string, driver?: string): Promise<number
|
|
32
|
-
updateBatch?<T>(table: string, attrs: Attributes, objs: T[], buildParam: (i: number) => string, notSkipInvalid?: boolean): Promise<number
|
|
33
|
-
insertWithTx?<T>(tx: string, commit: boolean, table: string, attrs: Attributes, obj: T, buildParam: (i: number) => string, ver?: string): Promise<number
|
|
34
|
-
updateWithTx?<T>(tx: string, commit: boolean, table: string, attrs: Attributes, obj: T, buildParam: (i: number) => string, ver?: string): Promise<number
|
|
35
|
-
insertBatchWithTx?<T>(
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
20
|
+
query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[]): Promise<T[]>
|
|
21
|
+
exec(sql: string, args?: any[]): Promise<number>
|
|
22
|
+
execBatch(stmts: Statement[]): Promise<number>
|
|
23
|
+
beginTransaction?(timeout?: number): Promise<string>
|
|
24
|
+
commitTransaction?(tx: string): Promise<boolean>
|
|
25
|
+
rollbackTransaction?(tx: string): Promise<boolean>
|
|
26
|
+
queryWithTx?<T>(tx: string, commit: boolean, sql: string, args?: any[], m?: StringMap, bools?: Attribute[]): Promise<T[]>
|
|
27
|
+
execWithTx?(tx: string, commit: boolean, sql: string, args?: any[]): Promise<number>
|
|
28
|
+
execBatchWithTx?(tx: string, commit: boolean, stmts: Statement[]): Promise<number>
|
|
29
|
+
insert?<T>(table: string, attrs: Attributes, obj: T, buildParam: (i: number) => string, ver?: string): Promise<number>
|
|
30
|
+
update?<T>(table: string, attrs: Attributes, obj: T, buildParam: (i: number) => string, ver?: string): Promise<number>
|
|
31
|
+
insertBatch?<T>(table: string, attrs: Attributes, objs: T[], buildParam: (i: number) => string, driver?: string): Promise<number>
|
|
32
|
+
updateBatch?<T>(table: string, attrs: Attributes, objs: T[], buildParam: (i: number) => string, notSkipInvalid?: boolean): Promise<number>
|
|
33
|
+
insertWithTx?<T>(tx: string, commit: boolean, table: string, attrs: Attributes, obj: T, buildParam: (i: number) => string, ver?: string): Promise<number>
|
|
34
|
+
updateWithTx?<T>(tx: string, commit: boolean, table: string, attrs: Attributes, obj: T, buildParam: (i: number) => string, ver?: string): Promise<number>
|
|
35
|
+
insertBatchWithTx?<T>(
|
|
36
|
+
tx: string,
|
|
37
|
+
commit: boolean,
|
|
38
|
+
table: string,
|
|
39
|
+
attrs: Attributes,
|
|
40
|
+
objs: T[],
|
|
41
|
+
buildParam: (i: number) => string,
|
|
42
|
+
driver?: string,
|
|
43
|
+
): Promise<number>
|
|
44
|
+
updateBatchWithTx?<T>(
|
|
45
|
+
tx: string,
|
|
46
|
+
commit: boolean,
|
|
47
|
+
table: string,
|
|
48
|
+
attrs: Attributes,
|
|
49
|
+
objs: T[],
|
|
50
|
+
buildParam: (i: number) => string,
|
|
51
|
+
notSkipInvalid?: boolean,
|
|
52
|
+
): Promise<number>
|
|
53
|
+
save?<T>(table: string, attrs: Attributes, obj: T, buildParam: (i: number) => string, ver?: string): Promise<number>
|
|
54
|
+
saveBatch?<T>(table: string, attrs: Attributes, objs: T[], buildParam: (i: number) => string, ver?: string): Promise<number>
|
|
39
55
|
}
|
|
40
56
|
export function buildStatements(s: Statement[]): JStatement[] {
|
|
41
|
-
const d: JStatement[] = []
|
|
57
|
+
const d: JStatement[] = []
|
|
42
58
|
if (!s || s.length === 0) {
|
|
43
|
-
return d
|
|
59
|
+
return d
|
|
44
60
|
}
|
|
45
61
|
for (const t of s) {
|
|
46
|
-
const dates = toDates(t.params)
|
|
47
|
-
const j: JStatement = {query: t.query, params: t.params, dates}
|
|
48
|
-
d.push(j)
|
|
62
|
+
const dates = toDates(t.params)
|
|
63
|
+
const j: JStatement = { query: t.query, params: t.params, dates }
|
|
64
|
+
d.push(j)
|
|
49
65
|
}
|
|
50
|
-
return d
|
|
66
|
+
return d
|
|
51
67
|
}
|
|
52
68
|
export function toDates(args?: any[]): number[] {
|
|
53
|
-
const d: number[] = []
|
|
69
|
+
const d: number[] = []
|
|
54
70
|
if (!args || args.length === 0) {
|
|
55
|
-
return d
|
|
71
|
+
return d
|
|
56
72
|
}
|
|
57
|
-
const l = args.length
|
|
73
|
+
const l = args.length
|
|
58
74
|
for (let i = 0; i < l; i++) {
|
|
59
75
|
if (args[i] && args[i] instanceof Date) {
|
|
60
|
-
d.push(i)
|
|
76
|
+
d.push(i)
|
|
61
77
|
}
|
|
62
78
|
}
|
|
63
|
-
return d
|
|
79
|
+
return d
|
|
64
80
|
}
|
|
65
81
|
export class ProxyClient {
|
|
66
82
|
constructor(protected httpRequest: HttpRequest, protected url: string) {
|
|
67
|
-
this.query = this.query.bind(this)
|
|
68
|
-
this.exec = this.exec.bind(this)
|
|
69
|
-
this.execBatch = this.execBatch.bind(this)
|
|
83
|
+
this.query = this.query.bind(this)
|
|
84
|
+
this.exec = this.exec.bind(this)
|
|
85
|
+
this.execBatch = this.execBatch.bind(this)
|
|
70
86
|
|
|
71
|
-
this.insert = this.insert.bind(this)
|
|
72
|
-
this.update = this.update.bind(this)
|
|
73
|
-
this.insertBatch = this.insertBatch.bind(this)
|
|
74
|
-
this.updateBatch = this.updateBatch.bind(this)
|
|
75
|
-
this.insertWithTx = this.insertWithTx.bind(this)
|
|
76
|
-
this.updateWithTx = this.updateWithTx.bind(this)
|
|
77
|
-
this.insertBatchWithTx = this.insertBatchWithTx.bind(this)
|
|
78
|
-
this.updateBatchWithTx = this.updateBatchWithTx.bind(this)
|
|
87
|
+
this.insert = this.insert.bind(this)
|
|
88
|
+
this.update = this.update.bind(this)
|
|
89
|
+
this.insertBatch = this.insertBatch.bind(this)
|
|
90
|
+
this.updateBatch = this.updateBatch.bind(this)
|
|
91
|
+
this.insertWithTx = this.insertWithTx.bind(this)
|
|
92
|
+
this.updateWithTx = this.updateWithTx.bind(this)
|
|
93
|
+
this.insertBatchWithTx = this.insertBatchWithTx.bind(this)
|
|
94
|
+
this.updateBatchWithTx = this.updateBatchWithTx.bind(this)
|
|
79
95
|
|
|
80
|
-
this.beginTransaction = this.beginTransaction.bind(this)
|
|
81
|
-
this.commitTransaction = this.commitTransaction.bind(this)
|
|
82
|
-
this.rollbackTransaction = this.rollbackTransaction.bind(this)
|
|
83
|
-
this.queryWithTx = this.queryWithTx.bind(this)
|
|
84
|
-
this.execWithTx = this.execWithTx.bind(this)
|
|
85
|
-
this.execBatchWithTx = this.execBatchWithTx.bind(this)
|
|
96
|
+
this.beginTransaction = this.beginTransaction.bind(this)
|
|
97
|
+
this.commitTransaction = this.commitTransaction.bind(this)
|
|
98
|
+
this.rollbackTransaction = this.rollbackTransaction.bind(this)
|
|
99
|
+
this.queryWithTx = this.queryWithTx.bind(this)
|
|
100
|
+
this.execWithTx = this.execWithTx.bind(this)
|
|
101
|
+
this.execBatchWithTx = this.execBatchWithTx.bind(this)
|
|
86
102
|
}
|
|
87
103
|
query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[]): Promise<T[]> {
|
|
88
|
-
const dates = toDates(args)
|
|
89
|
-
const j: JStatement = {query: sql, params: args, dates}
|
|
104
|
+
const dates = toDates(args)
|
|
105
|
+
const j: JStatement = { query: sql, params: args, dates }
|
|
90
106
|
if (m || bools) {
|
|
91
|
-
return this.httpRequest.post<T[]>(this.url +
|
|
107
|
+
return this.httpRequest.post<T[]>(this.url + "/query", j).then((r) => handleResults(r, m, bools))
|
|
92
108
|
} else {
|
|
93
|
-
return this.httpRequest.post<T[]>(this.url +
|
|
109
|
+
return this.httpRequest.post<T[]>(this.url + "/query", j)
|
|
94
110
|
}
|
|
95
111
|
}
|
|
96
112
|
exec(sql: string, args?: any[]): Promise<number> {
|
|
97
|
-
const dates = toDates(args)
|
|
98
|
-
const j: JStatement = {query: sql, params: args, dates}
|
|
99
|
-
return this.httpRequest.post<number>(this.url +
|
|
113
|
+
const dates = toDates(args)
|
|
114
|
+
const j: JStatement = { query: sql, params: args, dates }
|
|
115
|
+
return this.httpRequest.post<number>(this.url + "/exec", j)
|
|
100
116
|
}
|
|
101
117
|
execBatch(stmts: Statement[]): Promise<number> {
|
|
102
|
-
const d = buildStatements(stmts)
|
|
103
|
-
return this.httpRequest.post<number>(this.url +
|
|
118
|
+
const d = buildStatements(stmts)
|
|
119
|
+
return this.httpRequest.post<number>(this.url + "/exec-batch", d)
|
|
104
120
|
}
|
|
105
121
|
|
|
106
122
|
beginTransaction(timeout?: number): Promise<string> {
|
|
107
|
-
const st =
|
|
108
|
-
return this.httpRequest.post<string>(this.url +
|
|
123
|
+
const st = timeout && timeout > 0 ? "?timeout=" + timeout : ""
|
|
124
|
+
return this.httpRequest.post<string>(this.url + "/begin" + st, "")
|
|
109
125
|
}
|
|
110
126
|
commitTransaction(tx: string): Promise<boolean> {
|
|
111
|
-
return this.httpRequest.post<boolean>(this.url +
|
|
127
|
+
return this.httpRequest.post<boolean>(this.url + "/end?tx=" + tx, "")
|
|
112
128
|
}
|
|
113
129
|
rollbackTransaction(tx: string): Promise<boolean> {
|
|
114
|
-
return this.httpRequest.post<boolean>(this.url +
|
|
130
|
+
return this.httpRequest.post<boolean>(this.url + "/end?roleback=true&tx=" + tx, "")
|
|
115
131
|
}
|
|
116
132
|
queryWithTx<T>(tx: string, commit: boolean, sql: string, args?: any[], m?: StringMap, bools?: Attribute[]): Promise<T[]> {
|
|
117
|
-
const dates = toDates(args)
|
|
118
|
-
const j: JStatement = {query: sql, params: args, dates}
|
|
119
|
-
const sc =
|
|
133
|
+
const dates = toDates(args)
|
|
134
|
+
const j: JStatement = { query: sql, params: args, dates }
|
|
135
|
+
const sc = commit ? "&commit=true" : ""
|
|
120
136
|
if (m || bools) {
|
|
121
|
-
return this.httpRequest.post<T[]>(this.url +
|
|
137
|
+
return this.httpRequest.post<T[]>(this.url + "/query?tx=" + tx + sc, j).then((r) => handleResults(r, m, bools))
|
|
122
138
|
} else {
|
|
123
|
-
return this.httpRequest.post<T[]>(this.url +
|
|
139
|
+
return this.httpRequest.post<T[]>(this.url + "/query?tx=" + tx + sc, j)
|
|
124
140
|
}
|
|
125
141
|
}
|
|
126
142
|
execWithTx(tx: string, commit: boolean, sql: string, args?: any[]): Promise<number> {
|
|
127
|
-
const dates = toDates(args)
|
|
128
|
-
const j: JStatement = {query: sql, params: args, dates}
|
|
129
|
-
const sc =
|
|
130
|
-
return this.httpRequest.post<number>(this.url +
|
|
143
|
+
const dates = toDates(args)
|
|
144
|
+
const j: JStatement = { query: sql, params: args, dates }
|
|
145
|
+
const sc = commit ? "&commit=true" : ""
|
|
146
|
+
return this.httpRequest.post<number>(this.url + "/exec?tx=" + tx + sc, j)
|
|
131
147
|
}
|
|
132
148
|
execBatchWithTx(tx: string, commit: boolean, stmts: Statement[]): Promise<number> {
|
|
133
|
-
const d = buildStatements(stmts)
|
|
134
|
-
const sc =
|
|
135
|
-
return this.httpRequest.post<number>(this.url +
|
|
149
|
+
const d = buildStatements(stmts)
|
|
150
|
+
const sc = commit ? "&commit=true" : ""
|
|
151
|
+
return this.httpRequest.post<number>(this.url + "/exec-batch?tx=" + tx + sc, d)
|
|
136
152
|
}
|
|
137
153
|
|
|
138
154
|
insert<T>(table: string, attrs: Attributes, obj: T, buildParam: (i: number) => string, ver?: string): Promise<number> {
|
|
139
|
-
const s = buildToInsert(obj, table, attrs, buildParam, ver)
|
|
155
|
+
const s = buildToInsert(obj, table, attrs, buildParam, ver)
|
|
140
156
|
if (s) {
|
|
141
|
-
return this.exec(s.query, s.params)
|
|
157
|
+
return this.exec(s.query, s.params)
|
|
142
158
|
} else {
|
|
143
|
-
return Promise.resolve(-1)
|
|
159
|
+
return Promise.resolve(-1)
|
|
144
160
|
}
|
|
145
161
|
}
|
|
146
162
|
update<T>(table: string, attrs: Attributes, obj: T, buildParam: (i: number) => string, ver?: string): Promise<number> {
|
|
147
|
-
const s = buildToUpdate(obj, table, attrs, buildParam, ver)
|
|
163
|
+
const s = buildToUpdate(obj, table, attrs, buildParam, ver)
|
|
148
164
|
if (s) {
|
|
149
|
-
return this.exec(s.query, s.params)
|
|
165
|
+
return this.exec(s.query, s.params)
|
|
150
166
|
} else {
|
|
151
|
-
return Promise.resolve(-1)
|
|
167
|
+
return Promise.resolve(-1)
|
|
152
168
|
}
|
|
153
169
|
}
|
|
154
170
|
insertBatch<T>(table: string, attrs: Attributes, objs: T[], buildParam: (i: number) => string, driver?: string): Promise<number> {
|
|
155
|
-
const s = buildToInsertBatch(objs, table, attrs, buildParam)
|
|
171
|
+
const s = buildToInsertBatch(objs, table, attrs, buildParam)
|
|
156
172
|
if (s) {
|
|
157
|
-
return this.exec(s.query, s.params)
|
|
173
|
+
return this.exec(s.query, s.params)
|
|
158
174
|
} else {
|
|
159
|
-
return Promise.resolve(-1)
|
|
175
|
+
return Promise.resolve(-1)
|
|
160
176
|
}
|
|
161
177
|
}
|
|
162
178
|
updateBatch<T>(table: string, attrs: Attributes, objs: T[], buildParam: (i: number) => string, notSkipInvalid?: boolean): Promise<number> {
|
|
163
|
-
const s = buildToUpdateBatch(objs, table, attrs, buildParam, notSkipInvalid)
|
|
179
|
+
const s = buildToUpdateBatch(objs, table, attrs, buildParam, notSkipInvalid)
|
|
164
180
|
if (s && s.length > 0) {
|
|
165
|
-
return this.execBatch(s)
|
|
181
|
+
return this.execBatch(s)
|
|
166
182
|
} else {
|
|
167
|
-
return Promise.resolve(-1)
|
|
183
|
+
return Promise.resolve(-1)
|
|
168
184
|
}
|
|
169
185
|
}
|
|
170
186
|
insertWithTx<T>(tx: string, commit: boolean, table: string, attrs: Attributes, obj: T, buildParam: (i: number) => string, ver?: string): Promise<number> {
|
|
171
|
-
const s = buildToInsert(obj, table, attrs, buildParam, ver)
|
|
187
|
+
const s = buildToInsert(obj, table, attrs, buildParam, ver)
|
|
172
188
|
if (s) {
|
|
173
|
-
return this.execWithTx(tx, commit, s.query, s.params)
|
|
189
|
+
return this.execWithTx(tx, commit, s.query, s.params)
|
|
174
190
|
} else {
|
|
175
|
-
return Promise.resolve(-1)
|
|
191
|
+
return Promise.resolve(-1)
|
|
176
192
|
}
|
|
177
193
|
}
|
|
178
194
|
updateWithTx<T>(tx: string, commit: boolean, table: string, attrs: Attributes, obj: T, buildParam: (i: number) => string, ver?: string): Promise<number> {
|
|
179
|
-
const s = buildToUpdate(obj, table, attrs, buildParam, ver)
|
|
195
|
+
const s = buildToUpdate(obj, table, attrs, buildParam, ver)
|
|
180
196
|
if (s) {
|
|
181
|
-
return this.execWithTx(tx, commit, s.query, s.params)
|
|
197
|
+
return this.execWithTx(tx, commit, s.query, s.params)
|
|
182
198
|
} else {
|
|
183
|
-
return Promise.resolve(-1)
|
|
199
|
+
return Promise.resolve(-1)
|
|
184
200
|
}
|
|
185
201
|
}
|
|
186
|
-
insertBatchWithTx<T>(
|
|
187
|
-
|
|
202
|
+
insertBatchWithTx<T>(
|
|
203
|
+
tx: string,
|
|
204
|
+
commit: boolean,
|
|
205
|
+
table: string,
|
|
206
|
+
attrs: Attributes,
|
|
207
|
+
objs: T[],
|
|
208
|
+
buildParam: (i: number) => string,
|
|
209
|
+
driver?: string,
|
|
210
|
+
): Promise<number> {
|
|
211
|
+
const s = buildToInsertBatch(objs, table, attrs, buildParam)
|
|
188
212
|
if (s) {
|
|
189
|
-
return this.execWithTx(tx, commit, s.query, s.params)
|
|
213
|
+
return this.execWithTx(tx, commit, s.query, s.params)
|
|
190
214
|
} else {
|
|
191
|
-
return Promise.resolve(-1)
|
|
215
|
+
return Promise.resolve(-1)
|
|
192
216
|
}
|
|
193
217
|
}
|
|
194
|
-
updateBatchWithTx<T>(
|
|
195
|
-
|
|
218
|
+
updateBatchWithTx<T>(
|
|
219
|
+
tx: string,
|
|
220
|
+
commit: boolean,
|
|
221
|
+
table: string,
|
|
222
|
+
attrs: Attributes,
|
|
223
|
+
objs: T[],
|
|
224
|
+
buildParam: (i: number) => string,
|
|
225
|
+
notSkipInvalid?: boolean,
|
|
226
|
+
): Promise<number> {
|
|
227
|
+
const s = buildToUpdateBatch(objs, table, attrs, buildParam, notSkipInvalid)
|
|
196
228
|
if (s && s.length > 0) {
|
|
197
|
-
return this.execBatchWithTx(tx, commit, s)
|
|
229
|
+
return this.execBatchWithTx(tx, commit, s)
|
|
198
230
|
} else {
|
|
199
|
-
return Promise.resolve(-1)
|
|
231
|
+
return Promise.resolve(-1)
|
|
200
232
|
}
|
|
201
233
|
}
|
|
202
234
|
}
|
package/src/health.ts
CHANGED
|
@@ -1,42 +1,42 @@
|
|
|
1
|
-
import {StringMap} from
|
|
1
|
+
import { StringMap } from "./metadata"
|
|
2
2
|
|
|
3
3
|
export interface SimpleDB {
|
|
4
|
-
driver: string
|
|
5
|
-
query<T>(sql: string): Promise<T[]
|
|
4
|
+
driver: string
|
|
5
|
+
query<T>(sql: string): Promise<T[]>
|
|
6
6
|
}
|
|
7
7
|
export interface AnyMap {
|
|
8
|
-
[key: string]: any
|
|
8
|
+
[key: string]: any
|
|
9
9
|
}
|
|
10
10
|
export class Checker {
|
|
11
|
-
timeout: number
|
|
12
|
-
service: string
|
|
11
|
+
timeout: number
|
|
12
|
+
service: string
|
|
13
13
|
constructor(public select: (sql: string) => Promise<any[]>, public query: string, service?: string, timeout?: number) {
|
|
14
|
-
this.timeout =
|
|
15
|
-
this.service =
|
|
16
|
-
this.check = this.check.bind(this)
|
|
17
|
-
this.name = this.name.bind(this)
|
|
18
|
-
this.build = this.build.bind(this)
|
|
14
|
+
this.timeout = timeout ? timeout : 4200
|
|
15
|
+
this.service = service ? service : "sql"
|
|
16
|
+
this.check = this.check.bind(this)
|
|
17
|
+
this.name = this.name.bind(this)
|
|
18
|
+
this.build = this.build.bind(this)
|
|
19
19
|
}
|
|
20
20
|
async check(): Promise<AnyMap> {
|
|
21
|
-
const obj = {} as AnyMap
|
|
22
|
-
const promise = this.select(this.query).then(r => obj)
|
|
21
|
+
const obj = {} as AnyMap
|
|
22
|
+
const promise = this.select(this.query).then((r) => obj)
|
|
23
23
|
if (this.timeout > 0) {
|
|
24
|
-
return promiseTimeOut(this.timeout, promise)
|
|
24
|
+
return promiseTimeOut(this.timeout, promise)
|
|
25
25
|
} else {
|
|
26
|
-
return promise
|
|
26
|
+
return promise
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
29
|
name(): string {
|
|
30
|
-
return this.service
|
|
30
|
+
return this.service
|
|
31
31
|
}
|
|
32
32
|
build(data: AnyMap, err: any): AnyMap {
|
|
33
33
|
if (err) {
|
|
34
34
|
if (!data) {
|
|
35
|
-
data = {} as AnyMap
|
|
35
|
+
data = {} as AnyMap
|
|
36
36
|
}
|
|
37
|
-
data[
|
|
37
|
+
data["error"] = err
|
|
38
38
|
}
|
|
39
|
-
return data
|
|
39
|
+
return data
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
42
|
|
|
@@ -45,34 +45,34 @@ function promiseTimeOut(timeoutInMilliseconds: number, promise: Promise<any>): P
|
|
|
45
45
|
promise,
|
|
46
46
|
new Promise((resolve, reject) => {
|
|
47
47
|
setTimeout(() => {
|
|
48
|
-
reject(`Timed out in: ${timeoutInMilliseconds} milliseconds!`)
|
|
49
|
-
}, timeoutInMilliseconds)
|
|
50
|
-
})
|
|
51
|
-
])
|
|
48
|
+
reject(`Timed out in: ${timeoutInMilliseconds} milliseconds!`)
|
|
49
|
+
}, timeoutInMilliseconds)
|
|
50
|
+
}),
|
|
51
|
+
])
|
|
52
52
|
}
|
|
53
53
|
|
|
54
54
|
export const driverMap: StringMap = {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
}
|
|
61
|
-
export function createChecker(query: SimpleDB|((sql: string) => Promise<any[]>), sql?: string, service?: string, timeout?: number): Checker {
|
|
62
|
-
if (typeof query ===
|
|
55
|
+
oracle: "SELECT SYSDATE FROM DUAL",
|
|
56
|
+
postgres: "select now()",
|
|
57
|
+
mssql: "select getdate()",
|
|
58
|
+
mysql: "select current_time",
|
|
59
|
+
sqlite: "select date()",
|
|
60
|
+
}
|
|
61
|
+
export function createChecker(query: SimpleDB | ((sql: string) => Promise<any[]>), sql?: string, service?: string, timeout?: number): Checker {
|
|
62
|
+
if (typeof query === "function") {
|
|
63
63
|
if (!sql) {
|
|
64
|
-
sql =
|
|
64
|
+
sql = "select getdate()"
|
|
65
65
|
}
|
|
66
|
-
return new Checker(query, sql, service, timeout)
|
|
66
|
+
return new Checker(query, sql, service, timeout)
|
|
67
67
|
} else {
|
|
68
|
-
const db = query as SimpleDB
|
|
69
|
-
let s = driverMap[db.driver]
|
|
68
|
+
const db = query as SimpleDB
|
|
69
|
+
let s = driverMap[db.driver]
|
|
70
70
|
if (!s) {
|
|
71
|
-
s =
|
|
71
|
+
s = "select getdate()"
|
|
72
72
|
}
|
|
73
73
|
if (!service) {
|
|
74
|
-
service = db.driver
|
|
74
|
+
service = db.driver
|
|
75
75
|
}
|
|
76
|
-
return new Checker(db.query, s, service, timeout)
|
|
76
|
+
return new Checker(db.query, s, service, timeout)
|
|
77
77
|
}
|
|
78
78
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,112 +1,117 @@
|
|
|
1
|
-
import {Checker} from
|
|
2
|
-
export {Checker as SqlChecker}
|
|
1
|
+
import { Checker } from "./health"
|
|
2
|
+
export { Checker as SqlChecker }
|
|
3
3
|
|
|
4
|
-
import {resource} from
|
|
5
|
-
import {Attribute, StringMap} from
|
|
6
|
-
import {SqlLoader, SqlSearchLoader, SqlSearchWriter, SqlWriter} from
|
|
4
|
+
import { resource } from "./build"
|
|
5
|
+
import { Attribute, StringMap } from "./metadata"
|
|
6
|
+
import { SqlLoader, SqlSearchLoader, SqlSearchWriter, SqlWriter } from "./services"
|
|
7
7
|
// export {SqlLoader as SqlLoadRepository};
|
|
8
|
-
export {SqlLoader as SqlViewRepository}
|
|
9
|
-
export {SqlWriter as SqlGenericRepository};
|
|
10
|
-
export {SqlWriter as Repository};
|
|
8
|
+
export { SqlWriter as Repository, SqlWriter as SqlGenericRepository, SqlLoader as SqlViewRepository }
|
|
11
9
|
|
|
12
|
-
export {SqlLoader as SqlLoadService}
|
|
13
|
-
export {SqlLoader as SqlViewService};
|
|
14
|
-
export {SqlWriter as SqlGenericService};
|
|
10
|
+
export { SqlWriter as SqlGenericService, SqlLoader as SqlLoadService, SqlLoader as SqlViewService }
|
|
15
11
|
|
|
16
|
-
export {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
12
|
+
export {
|
|
13
|
+
SqlSearchLoader as SearchRepository,
|
|
14
|
+
SqlSearchLoader as SearchService,
|
|
15
|
+
SqlSearchLoader as SqlSearchRepository,
|
|
16
|
+
SqlSearchLoader as SqlSearchService,
|
|
17
|
+
SqlSearchLoader as SqlViewSearchRepository,
|
|
18
|
+
SqlSearchLoader as SqlViewSearchService,
|
|
19
|
+
SqlSearchLoader as ViewSearchRepository,
|
|
20
|
+
SqlSearchLoader as ViewSearchService,
|
|
21
|
+
}
|
|
24
22
|
|
|
25
|
-
export {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
23
|
+
export {
|
|
24
|
+
SqlSearchWriter as GenericSearchRepository,
|
|
25
|
+
SqlSearchWriter as GenericSearchService,
|
|
26
|
+
SqlSearchWriter as Service,
|
|
27
|
+
SqlSearchWriter as SqlRepository,
|
|
28
|
+
SqlSearchWriter as SqlService,
|
|
29
|
+
}
|
|
30
30
|
|
|
31
|
-
export * from
|
|
32
|
-
export * from
|
|
33
|
-
export * from
|
|
34
|
-
export * from
|
|
35
|
-
export * from
|
|
36
|
-
export * from
|
|
37
|
-
export * from
|
|
38
|
-
export * from
|
|
39
|
-
export * from
|
|
31
|
+
export * from "./batch"
|
|
32
|
+
export * from "./build"
|
|
33
|
+
export * from "./client"
|
|
34
|
+
export * from "./health"
|
|
35
|
+
export * from "./metadata"
|
|
36
|
+
export * from "./query"
|
|
37
|
+
export * from "./search"
|
|
38
|
+
export * from "./SearchBuilder"
|
|
39
|
+
export * from "./services"
|
|
40
40
|
|
|
41
41
|
export interface Config {
|
|
42
|
-
connectString?: string | undefined
|
|
43
|
-
host?: string | undefined
|
|
44
|
-
port?: number
|
|
45
|
-
server?: string | undefined
|
|
46
|
-
database?: string | undefined
|
|
47
|
-
user?: string | undefined
|
|
48
|
-
password?: string | undefined
|
|
49
|
-
multipleStatements?: boolean | undefined
|
|
50
|
-
max?: number | undefined
|
|
51
|
-
min?: number | undefined
|
|
52
|
-
idleTimeoutMillis?: number | undefined
|
|
42
|
+
connectString?: string | undefined
|
|
43
|
+
host?: string | undefined
|
|
44
|
+
port?: number
|
|
45
|
+
server?: string | undefined
|
|
46
|
+
database?: string | undefined
|
|
47
|
+
user?: string | undefined
|
|
48
|
+
password?: string | undefined
|
|
49
|
+
multipleStatements?: boolean | undefined
|
|
50
|
+
max?: number | undefined
|
|
51
|
+
min?: number | undefined
|
|
52
|
+
idleTimeoutMillis?: number | undefined
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
// tslint:disable-next-line:max-classes-per-file
|
|
56
56
|
export class Loader<T> {
|
|
57
|
-
map?: StringMap
|
|
58
|
-
constructor(
|
|
59
|
-
|
|
60
|
-
|
|
57
|
+
map?: StringMap
|
|
58
|
+
constructor(
|
|
59
|
+
public query: (sql: string, args?: any[], m?: StringMap, bools?: Attribute[]) => Promise<T[]>,
|
|
60
|
+
public sql: string,
|
|
61
|
+
m?: StringMap,
|
|
62
|
+
public bools?: Attribute[],
|
|
63
|
+
) {
|
|
64
|
+
this.map = m
|
|
65
|
+
this.load = this.load.bind(this)
|
|
61
66
|
}
|
|
62
67
|
load(): Promise<T[]> {
|
|
63
|
-
return this.query(this.sql, [], this.map, this.bools)
|
|
68
|
+
return this.query(this.sql, [], this.map, this.bools)
|
|
64
69
|
}
|
|
65
70
|
}
|
|
66
71
|
export function toArray(arr: any[]): any[] {
|
|
67
72
|
if (!arr || arr.length === 0) {
|
|
68
|
-
return []
|
|
73
|
+
return []
|
|
69
74
|
}
|
|
70
|
-
const p: any[] = []
|
|
71
|
-
const l = arr.length
|
|
75
|
+
const p: any[] = []
|
|
76
|
+
const l = arr.length
|
|
72
77
|
for (let i = 0; i < l; i++) {
|
|
73
78
|
if (arr[i] === undefined || arr[i] == null) {
|
|
74
|
-
p.push(null)
|
|
79
|
+
p.push(null)
|
|
75
80
|
} else {
|
|
76
|
-
if (typeof arr[i] ===
|
|
81
|
+
if (typeof arr[i] === "object") {
|
|
77
82
|
if (arr[i] instanceof Date) {
|
|
78
|
-
p.push(arr[i])
|
|
83
|
+
p.push(arr[i])
|
|
79
84
|
} else {
|
|
80
85
|
if (resource.string) {
|
|
81
|
-
const s: string = JSON.stringify(arr[i])
|
|
82
|
-
p.push(s)
|
|
86
|
+
const s: string = JSON.stringify(arr[i])
|
|
87
|
+
p.push(s)
|
|
83
88
|
} else {
|
|
84
|
-
p.push(arr[i])
|
|
89
|
+
p.push(arr[i])
|
|
85
90
|
}
|
|
86
91
|
}
|
|
87
92
|
} else {
|
|
88
|
-
p.push(arr[i])
|
|
93
|
+
p.push(arr[i])
|
|
89
94
|
}
|
|
90
95
|
}
|
|
91
96
|
}
|
|
92
|
-
return p
|
|
97
|
+
return p
|
|
93
98
|
}
|
|
94
99
|
export function map<T>(obj: T, m?: StringMap): any {
|
|
95
100
|
if (!m) {
|
|
96
|
-
return obj
|
|
101
|
+
return obj
|
|
97
102
|
}
|
|
98
|
-
const mkeys = Object.keys(m)
|
|
103
|
+
const mkeys = Object.keys(m)
|
|
99
104
|
if (mkeys.length === 0) {
|
|
100
|
-
return obj
|
|
105
|
+
return obj
|
|
101
106
|
}
|
|
102
|
-
const obj2: any = {}
|
|
103
|
-
const keys = Object.keys(obj as any)
|
|
107
|
+
const obj2: any = {}
|
|
108
|
+
const keys = Object.keys(obj as any)
|
|
104
109
|
for (const key of keys) {
|
|
105
|
-
let k0 = m[key]
|
|
110
|
+
let k0 = m[key]
|
|
106
111
|
if (!k0) {
|
|
107
|
-
k0 = key
|
|
112
|
+
k0 = key
|
|
108
113
|
}
|
|
109
|
-
obj2[k0] = (obj as any)[key]
|
|
114
|
+
obj2[k0] = (obj as any)[key]
|
|
110
115
|
}
|
|
111
|
-
return obj2
|
|
116
|
+
return obj2
|
|
112
117
|
}
|