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/lib/SearchBuilder.js +21 -21
- package/lib/query.js +83 -84
- package/lib/services.js +221 -357
- 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/package.json
CHANGED
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(filter: 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 = (
|
|
64
|
-
delete (
|
|
65
|
-
const x =
|
|
66
|
-
const q2 = this.buildQuery(
|
|
84
|
+
const st = this.sort ? this.sort : "sort"
|
|
85
|
+
const sn = (filter as any)[st] as string
|
|
86
|
+
delete (filter as any)[st]
|
|
87
|
+
const x = this.provider === postgres ? "ilike" : this.param
|
|
88
|
+
const q2 = this.buildQuery(filter, 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
|
}
|