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/SearchBuilder.ts
CHANGED
|
@@ -1,150 +1,183 @@
|
|
|
1
|
-
import {exist, metadata, param, select} from
|
|
2
|
-
import {Attribute, Attributes, Statement, StringMap} from
|
|
3
|
-
import {buildDollarParam, buildMsSQLParam, buildOracleParam, buildQuery,
|
|
4
|
-
import {buildFromQuery, oracle, SearchResult} from
|
|
1
|
+
import { exist, metadata, param, select } from "./build"
|
|
2
|
+
import { Attribute, Attributes, Statement, StringMap } from "./metadata"
|
|
3
|
+
import { buildSort as bs, buildDollarParam, buildMsSQLParam, buildOracleParam, buildQuery, LikeType } from "./query"
|
|
4
|
+
import { buildFromQuery, oracle, SearchResult } from "./search"
|
|
5
5
|
|
|
6
|
-
export const postgres =
|
|
7
|
-
export const mssql =
|
|
8
|
-
export const mysql =
|
|
9
|
-
export const sqlite =
|
|
6
|
+
export const postgres = "postgres"
|
|
7
|
+
export const mssql = "mssql"
|
|
8
|
+
export const mysql = "mysql"
|
|
9
|
+
export const sqlite = "sqlite"
|
|
10
10
|
export class SearchBuilder<T, S> {
|
|
11
|
-
map?: StringMap
|
|
12
|
-
bools?: Attribute[]
|
|
13
|
-
buildQuery: (
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
11
|
+
map?: StringMap
|
|
12
|
+
bools?: Attribute[]
|
|
13
|
+
buildQuery: (
|
|
14
|
+
s: S,
|
|
15
|
+
bparam: LikeType | ((i: number) => string),
|
|
16
|
+
sort?: string,
|
|
17
|
+
buildSort3?: (sort?: string, map?: Attributes | StringMap) => string,
|
|
18
|
+
attrs?: Attributes,
|
|
19
|
+
table?: string,
|
|
20
|
+
fields?: string[],
|
|
21
|
+
sq?: string,
|
|
22
|
+
strExcluding?: string,
|
|
23
|
+
) => Statement | undefined
|
|
24
|
+
q?: string
|
|
25
|
+
excluding?: string
|
|
26
|
+
buildSort?: (sort?: string, map?: Attributes | StringMap) => string
|
|
27
|
+
param: (i: number) => string
|
|
28
|
+
total?: string
|
|
29
|
+
constructor(
|
|
30
|
+
public query: <K>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any) => Promise<K[]>,
|
|
20
31
|
public table: string,
|
|
21
32
|
public attributes?: Attributes,
|
|
22
33
|
public provider?: string,
|
|
23
|
-
buildQ?: (
|
|
34
|
+
buildQ?: (
|
|
35
|
+
s: S,
|
|
36
|
+
bparam: LikeType | ((i: number) => string),
|
|
37
|
+
sort?: string,
|
|
38
|
+
buildSort3?: (sort?: string, map?: Attributes | StringMap) => string,
|
|
39
|
+
attrs?: Attributes,
|
|
40
|
+
table?: string,
|
|
41
|
+
fields?: string[],
|
|
42
|
+
sq?: string,
|
|
43
|
+
strExcluding?: string,
|
|
44
|
+
) => Statement | undefined,
|
|
24
45
|
public fromDB?: (v: T) => T,
|
|
25
46
|
public sort?: string,
|
|
26
47
|
q?: string,
|
|
27
48
|
excluding?: string,
|
|
28
|
-
buildSort?: (sort?: string, map?: Attributes|StringMap) => string,
|
|
49
|
+
buildSort?: (sort?: string, map?: Attributes | StringMap) => string,
|
|
29
50
|
buildParam?: (i: number) => string,
|
|
30
|
-
total?: string
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
51
|
+
total?: string,
|
|
52
|
+
) {
|
|
53
|
+
if (attributes) {
|
|
54
|
+
this.attributes = attributes
|
|
55
|
+
const meta = metadata(attributes)
|
|
56
|
+
this.map = meta.map
|
|
57
|
+
this.bools = meta.bools
|
|
58
|
+
}
|
|
59
|
+
this.buildQuery = buildQ ? buildQ : buildQuery
|
|
60
|
+
this.buildSort = buildSort ? buildSort : bs
|
|
61
|
+
this.q = q && q.length > 0 ? q : "q"
|
|
62
|
+
this.excluding = excluding && excluding.length > 0 ? excluding : "excluding"
|
|
63
|
+
this.search = this.search.bind(this)
|
|
64
|
+
if (buildParam) {
|
|
65
|
+
this.param = buildParam
|
|
66
|
+
} else {
|
|
67
|
+
if (provider === oracle) {
|
|
68
|
+
this.param = buildOracleParam
|
|
69
|
+
} else if (provider === postgres) {
|
|
70
|
+
this.param = buildDollarParam
|
|
71
|
+
} else if (provider === mssql) {
|
|
72
|
+
this.param = buildMsSQLParam
|
|
44
73
|
} else {
|
|
45
|
-
|
|
46
|
-
this.param = buildOracleParam;
|
|
47
|
-
} else if (provider === postgres) {
|
|
48
|
-
this.param = buildDollarParam;
|
|
49
|
-
} else if (provider === mssql) {
|
|
50
|
-
this.param = buildMsSQLParam;
|
|
51
|
-
} else {
|
|
52
|
-
this.param = param;
|
|
53
|
-
}
|
|
74
|
+
this.param = param
|
|
54
75
|
}
|
|
55
|
-
this.total = (total && total.length > 0 ? total : 'total');
|
|
56
76
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
77
|
+
this.total = total && total.length > 0 ? total : "total"
|
|
78
|
+
}
|
|
79
|
+
search(s: S, limit?: number, offset?: number | string, fields?: string[]): Promise<SearchResult<T>> {
|
|
80
|
+
let skip = 0
|
|
81
|
+
if (typeof offset === "number" && offset > 0) {
|
|
82
|
+
skip = offset
|
|
61
83
|
}
|
|
62
|
-
const st =
|
|
63
|
-
const sn = (s as any)[st] as string
|
|
64
|
-
delete (s as any)[st]
|
|
65
|
-
const x =
|
|
66
|
-
const q2 = this.buildQuery(s, x, sn, this.buildSort, this.attributes, this.table, fields, this.q, this.excluding)
|
|
84
|
+
const st = this.sort ? this.sort : "sort"
|
|
85
|
+
const sn = (s as any)[st] as string
|
|
86
|
+
delete (s as any)[st]
|
|
87
|
+
const x = this.provider === postgres ? "ilike" : this.param
|
|
88
|
+
const q2 = this.buildQuery(s, x, sn, this.buildSort, this.attributes, this.table, fields, this.q, this.excluding)
|
|
67
89
|
if (!q2) {
|
|
68
|
-
throw new Error(
|
|
90
|
+
throw new Error("Cannot build query")
|
|
69
91
|
}
|
|
70
|
-
const fn = this.fromDB
|
|
92
|
+
const fn = this.fromDB
|
|
71
93
|
if (fn) {
|
|
72
|
-
return buildFromQuery<T>(this.query, q2.query, q2.params, limit, skip, this.map, this.bools, this.provider, this.total).then(r => {
|
|
94
|
+
return buildFromQuery<T>(this.query, q2.query, q2.params, limit, skip, this.map, this.bools, this.provider, this.total).then((r) => {
|
|
73
95
|
if (r.list && r.list.length > 0) {
|
|
74
|
-
r.list = r.list.map(o => fn(o))
|
|
75
|
-
return r
|
|
96
|
+
r.list = r.list.map((o) => fn(o))
|
|
97
|
+
return r
|
|
76
98
|
} else {
|
|
77
|
-
return r
|
|
99
|
+
return r
|
|
78
100
|
}
|
|
79
|
-
})
|
|
101
|
+
})
|
|
80
102
|
} else {
|
|
81
|
-
return buildFromQuery(this.query, q2.query, q2.params, limit, skip, this.map, this.bools, this.provider, this.total)
|
|
103
|
+
return buildFromQuery(this.query, q2.query, q2.params, limit, skip, this.map, this.bools, this.provider, this.total)
|
|
82
104
|
}
|
|
83
105
|
}
|
|
84
106
|
}
|
|
85
107
|
// tslint:disable-next-line:max-classes-per-file
|
|
86
108
|
export class Query<T, ID, S> extends SearchBuilder<T, S> {
|
|
87
|
-
primaryKeys: Attribute[]
|
|
88
|
-
map?: StringMap
|
|
109
|
+
primaryKeys: Attribute[]
|
|
110
|
+
map?: StringMap
|
|
89
111
|
// attributes: Attributes;
|
|
90
|
-
bools?: Attribute[]
|
|
112
|
+
bools?: Attribute[]
|
|
91
113
|
constructor(
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
114
|
+
query: <K>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any) => Promise<K[]>,
|
|
115
|
+
table: string,
|
|
116
|
+
attributes: Attributes,
|
|
117
|
+
provider?: string,
|
|
118
|
+
buildQ?: (
|
|
119
|
+
s: S,
|
|
120
|
+
bparam: LikeType | ((i: number) => string),
|
|
98
121
|
sort?: string,
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
122
|
+
buildSort3?: (sort?: string, map?: Attributes | StringMap) => string,
|
|
123
|
+
attrs?: Attributes,
|
|
124
|
+
table?: string,
|
|
125
|
+
fields?: string[],
|
|
126
|
+
sq?: string,
|
|
127
|
+
strExcluding?: string,
|
|
128
|
+
) => Statement | undefined,
|
|
129
|
+
fromDB?: (v: T) => T,
|
|
130
|
+
sort?: string,
|
|
131
|
+
q?: string,
|
|
132
|
+
excluding?: string,
|
|
133
|
+
buildSort?: (sort?: string, map?: Attributes | StringMap) => string,
|
|
134
|
+
buildParam?: (i: number) => string,
|
|
135
|
+
total?: string,
|
|
136
|
+
) {
|
|
137
|
+
super(query, table, attributes, provider, buildQ, fromDB, sort, q, excluding, buildSort, buildParam, total)
|
|
138
|
+
const m = metadata(attributes)
|
|
139
|
+
this.primaryKeys = m.keys
|
|
140
|
+
this.map = m.map
|
|
141
|
+
this.bools = m.bools
|
|
109
142
|
if (this.metadata) {
|
|
110
|
-
this.metadata = this.metadata.bind(this)
|
|
143
|
+
this.metadata = this.metadata.bind(this)
|
|
111
144
|
}
|
|
112
|
-
this.all = this.all.bind(this)
|
|
113
|
-
this.load = this.load.bind(this)
|
|
114
|
-
this.exist = this.exist.bind(this)
|
|
145
|
+
this.all = this.all.bind(this)
|
|
146
|
+
this.load = this.load.bind(this)
|
|
147
|
+
this.exist = this.exist.bind(this)
|
|
115
148
|
}
|
|
116
|
-
metadata?(): Attributes|undefined {
|
|
117
|
-
return this.attributes
|
|
149
|
+
metadata?(): Attributes | undefined {
|
|
150
|
+
return this.attributes
|
|
118
151
|
}
|
|
119
152
|
all(): Promise<T[]> {
|
|
120
|
-
const sql = `select * from ${this.table}
|
|
121
|
-
return this.query(sql, [], this.map)
|
|
153
|
+
const sql = `select * from ${this.table}`
|
|
154
|
+
return this.query(sql, [], this.map)
|
|
122
155
|
}
|
|
123
|
-
load(id: ID, ctx?: any): Promise<T|null> {
|
|
124
|
-
const stmt = select<ID>(id, this.table, this.primaryKeys, this.param)
|
|
156
|
+
load(id: ID, ctx?: any): Promise<T | null> {
|
|
157
|
+
const stmt = select<ID>(id, this.table, this.primaryKeys, this.param)
|
|
125
158
|
if (!stmt) {
|
|
126
|
-
throw new Error(
|
|
159
|
+
throw new Error("cannot build query by id")
|
|
127
160
|
}
|
|
128
|
-
const fn = this.fromDB
|
|
161
|
+
const fn = this.fromDB
|
|
129
162
|
if (fn) {
|
|
130
|
-
return this.query<T>(stmt.query, stmt.params, this.map, ctx).then(res => {
|
|
163
|
+
return this.query<T>(stmt.query, stmt.params, this.map, ctx).then((res) => {
|
|
131
164
|
if (!res || res.length === 0) {
|
|
132
|
-
return null
|
|
165
|
+
return null
|
|
133
166
|
} else {
|
|
134
|
-
const obj = res[0]
|
|
135
|
-
return fn(obj)
|
|
167
|
+
const obj = res[0]
|
|
168
|
+
return fn(obj)
|
|
136
169
|
}
|
|
137
|
-
})
|
|
170
|
+
})
|
|
138
171
|
} else {
|
|
139
|
-
return this.query<T>(stmt.query, stmt.params, this.map).then(res => (!res || res.length === 0
|
|
172
|
+
return this.query<T>(stmt.query, stmt.params, this.map).then((res) => (!res || res.length === 0 ? null : res[0]))
|
|
140
173
|
}
|
|
141
174
|
}
|
|
142
175
|
exist(id: ID, ctx?: any): Promise<boolean> {
|
|
143
|
-
const field =
|
|
144
|
-
const stmt = exist<ID>(id, this.table, this.primaryKeys, this.param, field)
|
|
176
|
+
const field = this.primaryKeys[0].column ? this.primaryKeys[0].column : this.primaryKeys[0].name
|
|
177
|
+
const stmt = exist<ID>(id, this.table, this.primaryKeys, this.param, field)
|
|
145
178
|
if (!stmt) {
|
|
146
|
-
throw new Error(
|
|
179
|
+
throw new Error("cannot build query by id")
|
|
147
180
|
}
|
|
148
|
-
return this.query(stmt.query, stmt.params, this.map, undefined, ctx).then(res => (!res || res.length === 0
|
|
181
|
+
return this.query(stmt.query, stmt.params, this.map, undefined, ctx).then((res) => (!res || res.length === 0 ? false : true))
|
|
149
182
|
}
|
|
150
183
|
}
|
package/src/batch.ts
CHANGED
|
@@ -1,233 +1,276 @@
|
|
|
1
|
-
import { buildToInsert, buildToInsertBatch, buildToUpdate, buildToUpdateBatch, version } from
|
|
2
|
-
import { Attributes, Statement } from
|
|
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(
|
|
7
|
-
|
|
8
|
-
|
|
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(
|
|
37
|
-
|
|
38
|
-
|
|
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(
|
|
67
|
-
|
|
68
|
-
|
|
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(
|
|
101
|
-
|
|
102
|
-
|
|
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(
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
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(
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
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
|
}
|