@koralium/base-client 1.0.5 → 1.3.0-alpha10
Sign up to get free protection for your applications and to get access to all the features.
- package/jest.config.js +10 -10
- package/lib/filterBuilder.d.ts +23 -23
- package/lib/filterBuilder.js +155 -155
- package/lib/index.d.ts +5 -5
- package/lib/index.js +30 -30
- package/lib/koraliumClient.d.ts +5 -5
- package/lib/koraliumClient.js +2 -2
- package/lib/objectToSelectMapper.d.ts +9 -9
- package/lib/objectToSelectMapper.js +31 -31
- package/lib/parameterBuilder.d.ts +8 -8
- package/lib/parameterBuilder.js +40 -40
- package/lib/queryBuilder.d.ts +24 -24
- package/lib/queryBuilder.js +134 -134
- package/lib/queryOptions.d.ts +5 -5
- package/lib/queryOptions.js +2 -2
- package/lib/queryResult.d.ts +16 -9
- package/lib/queryResult.js +29 -29
- package/lib/queryResult.js.map +1 -1
- package/package.json +32 -31
- package/src/filterBuilder.ts +199 -199
- package/src/index.ts +17 -17
- package/src/koraliumClient.ts +18 -18
- package/src/objectToSelectMapper.ts +45 -45
- package/src/parameterBuilder.ts +35 -35
- package/src/queryBuilder.ts +160 -160
- package/src/queryOptions.ts +17 -17
- package/src/queryResult.ts +35 -29
- package/test-report.xml +23 -23
- package/tests/queryBuilder.test.ts +308 -308
- package/tsconfig.json +21 -21
package/src/index.ts
CHANGED
@@ -1,18 +1,18 @@
|
|
1
|
-
/*
|
2
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
3
|
-
* you may not use this file except in compliance with the License.
|
4
|
-
* You may obtain a copy of the License at
|
5
|
-
*
|
6
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
7
|
-
*
|
8
|
-
* Unless required by applicable law or agreed to in writing, software
|
9
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
10
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
11
|
-
* See the License for the specific language governing permissions and
|
12
|
-
* limitations under the License.
|
13
|
-
*/
|
14
|
-
export * from "./queryBuilder"
|
15
|
-
export * from "./objectToSelectMapper"
|
16
|
-
export * from "./queryOptions"
|
17
|
-
export * from "./queryResult"
|
1
|
+
/*
|
2
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
3
|
+
* you may not use this file except in compliance with the License.
|
4
|
+
* You may obtain a copy of the License at
|
5
|
+
*
|
6
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
7
|
+
*
|
8
|
+
* Unless required by applicable law or agreed to in writing, software
|
9
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
10
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
11
|
+
* See the License for the specific language governing permissions and
|
12
|
+
* limitations under the License.
|
13
|
+
*/
|
14
|
+
export * from "./queryBuilder"
|
15
|
+
export * from "./objectToSelectMapper"
|
16
|
+
export * from "./queryOptions"
|
17
|
+
export * from "./queryResult"
|
18
18
|
export * from "./koraliumClient"
|
package/src/koraliumClient.ts
CHANGED
@@ -1,19 +1,19 @@
|
|
1
|
-
/*
|
2
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
3
|
-
* you may not use this file except in compliance with the License.
|
4
|
-
* You may obtain a copy of the License at
|
5
|
-
*
|
6
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
7
|
-
*
|
8
|
-
* Unless required by applicable law or agreed to in writing, software
|
9
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
10
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
11
|
-
* See the License for the specific language governing permissions and
|
12
|
-
* limitations under the License.
|
13
|
-
*/
|
14
|
-
import { QueryOptions, QueryResult } from ".";
|
15
|
-
|
16
|
-
export interface KoraliumClient {
|
17
|
-
queryScalar(sql: string,
|
18
|
-
query(sql: string, queryOptions?: QueryOptions): Promise<QueryResult>;
|
1
|
+
/*
|
2
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
3
|
+
* you may not use this file except in compliance with the License.
|
4
|
+
* You may obtain a copy of the License at
|
5
|
+
*
|
6
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
7
|
+
*
|
8
|
+
* Unless required by applicable law or agreed to in writing, software
|
9
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
10
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
11
|
+
* See the License for the specific language governing permissions and
|
12
|
+
* limitations under the License.
|
13
|
+
*/
|
14
|
+
import { QueryOptions, QueryResult } from ".";
|
15
|
+
|
16
|
+
export interface KoraliumClient {
|
17
|
+
queryScalar(sql: string, queryOptions?: QueryOptions): Promise<any>
|
18
|
+
query(sql: string, queryOptions?: QueryOptions): Promise<QueryResult>;
|
19
19
|
}
|
@@ -1,46 +1,46 @@
|
|
1
|
-
/*
|
2
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
3
|
-
* you may not use this file except in compliance with the License.
|
4
|
-
* You may obtain a copy of the License at
|
5
|
-
*
|
6
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
7
|
-
*
|
8
|
-
* Unless required by applicable law or agreed to in writing, software
|
9
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
10
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
11
|
-
* See the License for the specific language governing permissions and
|
12
|
-
* limitations under the License.
|
13
|
-
*/
|
14
|
-
import { QueryBuilder } from "./queryBuilder";
|
15
|
-
|
16
|
-
//This class is mostly useful in GraphQL or similar areas
|
17
|
-
//Where you want to map an object into the query builder.
|
18
|
-
export class ObjectToSelectMapper {
|
19
|
-
|
20
|
-
mappingTable: {[key: string]: string | Array<string>;} = {};
|
21
|
-
|
22
|
-
addMapping(key: string, select: string | Array<string>) : ObjectToSelectMapper {
|
23
|
-
this.mappingTable[key] = select;
|
24
|
-
return this;
|
25
|
-
}
|
26
|
-
|
27
|
-
private isArray(value: string | Array<string>): value is Array<string> {
|
28
|
-
return Array.isArray(value);
|
29
|
-
}
|
30
|
-
|
31
|
-
addSelectsToQuery(queryBuilder: QueryBuilder, inputObject: {}) {
|
32
|
-
for (let [key] of Object.entries(inputObject)) {
|
33
|
-
|
34
|
-
if (this.mappingTable[key] !== undefined) {
|
35
|
-
const mappingTableValue = this.mappingTable[key];
|
36
|
-
|
37
|
-
if(this.isArray(mappingTableValue)) {
|
38
|
-
mappingTableValue.forEach(x => queryBuilder.addSelectElement(x));
|
39
|
-
}
|
40
|
-
else {
|
41
|
-
queryBuilder.addSelectElement(mappingTableValue);
|
42
|
-
}
|
43
|
-
}
|
44
|
-
}
|
45
|
-
}
|
1
|
+
/*
|
2
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
3
|
+
* you may not use this file except in compliance with the License.
|
4
|
+
* You may obtain a copy of the License at
|
5
|
+
*
|
6
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
7
|
+
*
|
8
|
+
* Unless required by applicable law or agreed to in writing, software
|
9
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
10
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
11
|
+
* See the License for the specific language governing permissions and
|
12
|
+
* limitations under the License.
|
13
|
+
*/
|
14
|
+
import { QueryBuilder } from "./queryBuilder";
|
15
|
+
|
16
|
+
//This class is mostly useful in GraphQL or similar areas
|
17
|
+
//Where you want to map an object into the query builder.
|
18
|
+
export class ObjectToSelectMapper {
|
19
|
+
|
20
|
+
mappingTable: {[key: string]: string | Array<string>;} = {};
|
21
|
+
|
22
|
+
addMapping(key: string, select: string | Array<string>) : ObjectToSelectMapper {
|
23
|
+
this.mappingTable[key] = select;
|
24
|
+
return this;
|
25
|
+
}
|
26
|
+
|
27
|
+
private isArray(value: string | Array<string>): value is Array<string> {
|
28
|
+
return Array.isArray(value);
|
29
|
+
}
|
30
|
+
|
31
|
+
addSelectsToQuery(queryBuilder: QueryBuilder, inputObject: {}) {
|
32
|
+
for (let [key] of Object.entries(inputObject)) {
|
33
|
+
|
34
|
+
if (this.mappingTable[key] !== undefined) {
|
35
|
+
const mappingTableValue = this.mappingTable[key];
|
36
|
+
|
37
|
+
if(this.isArray(mappingTableValue)) {
|
38
|
+
mappingTableValue.forEach(x => queryBuilder.addSelectElement(x));
|
39
|
+
}
|
40
|
+
else {
|
41
|
+
queryBuilder.addSelectElement(mappingTableValue);
|
42
|
+
}
|
43
|
+
}
|
44
|
+
}
|
45
|
+
}
|
46
46
|
}
|
package/src/parameterBuilder.ts
CHANGED
@@ -1,36 +1,36 @@
|
|
1
|
-
/*
|
2
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
3
|
-
* you may not use this file except in compliance with the License.
|
4
|
-
* You may obtain a copy of the License at
|
5
|
-
*
|
6
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
7
|
-
*
|
8
|
-
* Unless required by applicable law or agreed to in writing, software
|
9
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
10
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
11
|
-
* See the License for the specific language governing permissions and
|
12
|
-
* limitations under the License.
|
13
|
-
*/
|
14
|
-
export class ParameterBuilder {
|
15
|
-
valuesToParameters: {[key: string]: string;} = {}
|
16
|
-
counter: number = 0;
|
17
|
-
|
18
|
-
getParameterName(value: string | number) {
|
19
|
-
if (this.valuesToParameters[value] !== undefined) {
|
20
|
-
return this.valuesToParameters[value];
|
21
|
-
}
|
22
|
-
else {
|
23
|
-
const newParameter = `P${this.counter++}`;
|
24
|
-
this.valuesToParameters[value] = newParameter;
|
25
|
-
return newParameter;
|
26
|
-
}
|
27
|
-
}
|
28
|
-
|
29
|
-
getParameters(): {} {
|
30
|
-
const output: {[key: string]: string;} = {};
|
31
|
-
for (let [key, value] of Object.entries(this.valuesToParameters)) {
|
32
|
-
output[value] = key;
|
33
|
-
}
|
34
|
-
return output;
|
35
|
-
}
|
1
|
+
/*
|
2
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
3
|
+
* you may not use this file except in compliance with the License.
|
4
|
+
* You may obtain a copy of the License at
|
5
|
+
*
|
6
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
7
|
+
*
|
8
|
+
* Unless required by applicable law or agreed to in writing, software
|
9
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
10
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
11
|
+
* See the License for the specific language governing permissions and
|
12
|
+
* limitations under the License.
|
13
|
+
*/
|
14
|
+
export class ParameterBuilder {
|
15
|
+
valuesToParameters: {[key: string]: string;} = {}
|
16
|
+
counter: number = 0;
|
17
|
+
|
18
|
+
getParameterName(value: string | number) {
|
19
|
+
if (this.valuesToParameters[value] !== undefined) {
|
20
|
+
return this.valuesToParameters[value];
|
21
|
+
}
|
22
|
+
else {
|
23
|
+
const newParameter = `P${this.counter++}`;
|
24
|
+
this.valuesToParameters[value] = newParameter;
|
25
|
+
return newParameter;
|
26
|
+
}
|
27
|
+
}
|
28
|
+
|
29
|
+
getParameters(): {} {
|
30
|
+
const output: {[key: string]: string;} = {};
|
31
|
+
for (let [key, value] of Object.entries(this.valuesToParameters)) {
|
32
|
+
output[value] = key;
|
33
|
+
}
|
34
|
+
return output;
|
35
|
+
}
|
36
36
|
}
|
package/src/queryBuilder.ts
CHANGED
@@ -1,161 +1,161 @@
|
|
1
|
-
/*
|
2
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
3
|
-
* you may not use this file except in compliance with the License.
|
4
|
-
* You may obtain a copy of the License at
|
5
|
-
*
|
6
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
7
|
-
*
|
8
|
-
* Unless required by applicable law or agreed to in writing, software
|
9
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
10
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
11
|
-
* See the License for the specific language governing permissions and
|
12
|
-
* limitations under the License.
|
13
|
-
*/
|
14
|
-
import { writeFilter, writeFilterWithParameterBuilder } from "./filterBuilder";
|
15
|
-
import { ParameterBuilder } from "./parameterBuilder";
|
16
|
-
import { ObjectToSelectMapper } from "./objectToSelectMapper";
|
17
|
-
|
18
|
-
export class QueryBuilder {
|
19
|
-
|
20
|
-
private table: string = "";
|
21
|
-
private selects: Array<string> = [];
|
22
|
-
|
23
|
-
private limit?: number | null = null;
|
24
|
-
private offset?: number | null = null;
|
25
|
-
|
26
|
-
private filters: Array<string> = [];
|
27
|
-
private parameterBuilder: ParameterBuilder = new ParameterBuilder();
|
28
|
-
|
29
|
-
private orderby?: string | null = null;
|
30
|
-
|
31
|
-
private mapper?: ObjectToSelectMapper;
|
32
|
-
|
33
|
-
constructor(table: string, mapper?: ObjectToSelectMapper) {
|
34
|
-
this.table = table;
|
35
|
-
this.mapper = mapper;
|
36
|
-
}
|
37
|
-
|
38
|
-
addSelectElement(expression: string): QueryBuilder {
|
39
|
-
if(!this.selects.includes(expression)) {
|
40
|
-
this.selects.push(expression);
|
41
|
-
}
|
42
|
-
return this;
|
43
|
-
}
|
44
|
-
|
45
|
-
addSelectsWithMapper(value: {}): QueryBuilder {
|
46
|
-
if(this.mapper === undefined) {
|
47
|
-
throw new Error("No mapper was entered in the constructor");
|
48
|
-
}
|
49
|
-
this.mapper.addSelectsToQuery(this, value);
|
50
|
-
return this;
|
51
|
-
}
|
52
|
-
|
53
|
-
setLimit(limit: number): QueryBuilder {
|
54
|
-
this.limit = limit;
|
55
|
-
return this;
|
56
|
-
}
|
57
|
-
|
58
|
-
setOffset(offset: number): QueryBuilder {
|
59
|
-
this.offset = offset;
|
60
|
-
return this;
|
61
|
-
}
|
62
|
-
|
63
|
-
addFilter(filter: string | {} | null | undefined): QueryBuilder {
|
64
|
-
if(filter === undefined || filter === null) {
|
65
|
-
return this;
|
66
|
-
}
|
67
|
-
if(typeof filter === "string") {
|
68
|
-
this.filters.push( filter);
|
69
|
-
}
|
70
|
-
else {
|
71
|
-
const result = writeFilterWithParameterBuilder(this.parameterBuilder, filter);
|
72
|
-
this.filters.push(result);
|
73
|
-
}
|
74
|
-
|
75
|
-
return this;
|
76
|
-
}
|
77
|
-
|
78
|
-
addInFilter(columnName: string, array: Array<any>): QueryBuilder {
|
79
|
-
|
80
|
-
if(array.length == 0) {
|
81
|
-
return this;
|
82
|
-
}
|
83
|
-
|
84
|
-
const firstValue = array[0];
|
85
|
-
if (typeof firstValue === "string") {
|
86
|
-
const filter = `${columnName} IN (${array.map(x => `'${x}'`).join(", ")})`;
|
87
|
-
this.addFilter(filter);
|
88
|
-
}
|
89
|
-
else if (typeof firstValue === "number") {
|
90
|
-
const filter = `${columnName} IN (${array.join(", ")})`;
|
91
|
-
this.addFilter(filter);
|
92
|
-
}
|
93
|
-
else {
|
94
|
-
throw new Error("Only strings and numbers can be used in IN");
|
95
|
-
}
|
96
|
-
return this;
|
97
|
-
}
|
98
|
-
|
99
|
-
orderBy(columnName: string, descending: boolean) {
|
100
|
-
if(descending) {
|
101
|
-
this.orderby = `${columnName} desc`;
|
102
|
-
}
|
103
|
-
else {
|
104
|
-
this.orderby = `${columnName}`;
|
105
|
-
}
|
106
|
-
}
|
107
|
-
|
108
|
-
//This is used in graphQL and similar scenarios
|
109
|
-
//Example: { asc: Name} or { desc: Name }
|
110
|
-
orderByWithObject(obj: {[key: string]: string;} | undefined | null): QueryBuilder {
|
111
|
-
|
112
|
-
if(obj === undefined || obj === null){
|
113
|
-
return this;
|
114
|
-
}
|
115
|
-
|
116
|
-
if(obj.asc !== undefined) {
|
117
|
-
this.orderby = `${obj.asc}`
|
118
|
-
}
|
119
|
-
else if (obj.desc !== undefined) {
|
120
|
-
this.orderby = `${obj.desc} desc`
|
121
|
-
}
|
122
|
-
else {
|
123
|
-
throw new Error("Could not find either asc or desc field");
|
124
|
-
}
|
125
|
-
return this;
|
126
|
-
}
|
127
|
-
|
128
|
-
getParameters(): {} {
|
129
|
-
return this.parameterBuilder.getParameters();
|
130
|
-
}
|
131
|
-
|
132
|
-
buildQuery(): string {
|
133
|
-
var selectElements = this.selects.join(", ");
|
134
|
-
|
135
|
-
let sql = `SELECT ${selectElements} FROM ${this.table}`;
|
136
|
-
|
137
|
-
if(this.filters.length > 0) {
|
138
|
-
//Try and filter out any empty filters
|
139
|
-
this.filters = this.filters.filter(x => x.trim() != "");
|
140
|
-
//Check again if there still are filters
|
141
|
-
if(this.filters.length > 0) {
|
142
|
-
sql += ` WHERE ${this.filters.join(" AND ")}`;
|
143
|
-
}
|
144
|
-
}
|
145
|
-
|
146
|
-
if(this.orderby != null) {
|
147
|
-
sql += ` ORDER BY ${this.orderby}`
|
148
|
-
}
|
149
|
-
|
150
|
-
if(this.limit != null) {
|
151
|
-
sql += ` LIMIT ${this.limit}`;
|
152
|
-
}
|
153
|
-
|
154
|
-
if(this.offset != null) {
|
155
|
-
sql += ` OFFSET ${this.offset}`;
|
156
|
-
}
|
157
|
-
|
158
|
-
return sql;
|
159
|
-
}
|
160
|
-
|
1
|
+
/*
|
2
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
3
|
+
* you may not use this file except in compliance with the License.
|
4
|
+
* You may obtain a copy of the License at
|
5
|
+
*
|
6
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
7
|
+
*
|
8
|
+
* Unless required by applicable law or agreed to in writing, software
|
9
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
10
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
11
|
+
* See the License for the specific language governing permissions and
|
12
|
+
* limitations under the License.
|
13
|
+
*/
|
14
|
+
import { writeFilter, writeFilterWithParameterBuilder } from "./filterBuilder";
|
15
|
+
import { ParameterBuilder } from "./parameterBuilder";
|
16
|
+
import { ObjectToSelectMapper } from "./objectToSelectMapper";
|
17
|
+
|
18
|
+
export class QueryBuilder {
|
19
|
+
|
20
|
+
private table: string = "";
|
21
|
+
private selects: Array<string> = [];
|
22
|
+
|
23
|
+
private limit?: number | null = null;
|
24
|
+
private offset?: number | null = null;
|
25
|
+
|
26
|
+
private filters: Array<string> = [];
|
27
|
+
private parameterBuilder: ParameterBuilder = new ParameterBuilder();
|
28
|
+
|
29
|
+
private orderby?: string | null = null;
|
30
|
+
|
31
|
+
private mapper?: ObjectToSelectMapper;
|
32
|
+
|
33
|
+
constructor(table: string, mapper?: ObjectToSelectMapper) {
|
34
|
+
this.table = table;
|
35
|
+
this.mapper = mapper;
|
36
|
+
}
|
37
|
+
|
38
|
+
addSelectElement(expression: string): QueryBuilder {
|
39
|
+
if(!this.selects.includes(expression)) {
|
40
|
+
this.selects.push(expression);
|
41
|
+
}
|
42
|
+
return this;
|
43
|
+
}
|
44
|
+
|
45
|
+
addSelectsWithMapper(value: {}): QueryBuilder {
|
46
|
+
if(this.mapper === undefined) {
|
47
|
+
throw new Error("No mapper was entered in the constructor");
|
48
|
+
}
|
49
|
+
this.mapper.addSelectsToQuery(this, value);
|
50
|
+
return this;
|
51
|
+
}
|
52
|
+
|
53
|
+
setLimit(limit: number): QueryBuilder {
|
54
|
+
this.limit = limit;
|
55
|
+
return this;
|
56
|
+
}
|
57
|
+
|
58
|
+
setOffset(offset: number): QueryBuilder {
|
59
|
+
this.offset = offset;
|
60
|
+
return this;
|
61
|
+
}
|
62
|
+
|
63
|
+
addFilter(filter: string | {} | null | undefined): QueryBuilder {
|
64
|
+
if(filter === undefined || filter === null) {
|
65
|
+
return this;
|
66
|
+
}
|
67
|
+
if(typeof filter === "string") {
|
68
|
+
this.filters.push( filter);
|
69
|
+
}
|
70
|
+
else {
|
71
|
+
const result = writeFilterWithParameterBuilder(this.parameterBuilder, filter);
|
72
|
+
this.filters.push(result);
|
73
|
+
}
|
74
|
+
|
75
|
+
return this;
|
76
|
+
}
|
77
|
+
|
78
|
+
addInFilter(columnName: string, array: Array<any>): QueryBuilder {
|
79
|
+
|
80
|
+
if(array.length == 0) {
|
81
|
+
return this;
|
82
|
+
}
|
83
|
+
|
84
|
+
const firstValue = array[0];
|
85
|
+
if (typeof firstValue === "string") {
|
86
|
+
const filter = `${columnName} IN (${array.map(x => `'${x}'`).join(", ")})`;
|
87
|
+
this.addFilter(filter);
|
88
|
+
}
|
89
|
+
else if (typeof firstValue === "number") {
|
90
|
+
const filter = `${columnName} IN (${array.join(", ")})`;
|
91
|
+
this.addFilter(filter);
|
92
|
+
}
|
93
|
+
else {
|
94
|
+
throw new Error("Only strings and numbers can be used in IN");
|
95
|
+
}
|
96
|
+
return this;
|
97
|
+
}
|
98
|
+
|
99
|
+
orderBy(columnName: string, descending: boolean) {
|
100
|
+
if(descending) {
|
101
|
+
this.orderby = `${columnName} desc`;
|
102
|
+
}
|
103
|
+
else {
|
104
|
+
this.orderby = `${columnName}`;
|
105
|
+
}
|
106
|
+
}
|
107
|
+
|
108
|
+
//This is used in graphQL and similar scenarios
|
109
|
+
//Example: { asc: Name} or { desc: Name }
|
110
|
+
orderByWithObject(obj: {[key: string]: string;} | undefined | null): QueryBuilder {
|
111
|
+
|
112
|
+
if(obj === undefined || obj === null){
|
113
|
+
return this;
|
114
|
+
}
|
115
|
+
|
116
|
+
if(obj.asc !== undefined) {
|
117
|
+
this.orderby = `${obj.asc}`
|
118
|
+
}
|
119
|
+
else if (obj.desc !== undefined) {
|
120
|
+
this.orderby = `${obj.desc} desc`
|
121
|
+
}
|
122
|
+
else {
|
123
|
+
throw new Error("Could not find either asc or desc field");
|
124
|
+
}
|
125
|
+
return this;
|
126
|
+
}
|
127
|
+
|
128
|
+
getParameters(): {} {
|
129
|
+
return this.parameterBuilder.getParameters();
|
130
|
+
}
|
131
|
+
|
132
|
+
buildQuery(): string {
|
133
|
+
var selectElements = this.selects.join(", ");
|
134
|
+
|
135
|
+
let sql = `SELECT ${selectElements} FROM ${this.table}`;
|
136
|
+
|
137
|
+
if(this.filters.length > 0) {
|
138
|
+
//Try and filter out any empty filters
|
139
|
+
this.filters = this.filters.filter(x => x.trim() != "");
|
140
|
+
//Check again if there still are filters
|
141
|
+
if(this.filters.length > 0) {
|
142
|
+
sql += ` WHERE ${this.filters.join(" AND ")}`;
|
143
|
+
}
|
144
|
+
}
|
145
|
+
|
146
|
+
if(this.orderby != null) {
|
147
|
+
sql += ` ORDER BY ${this.orderby}`
|
148
|
+
}
|
149
|
+
|
150
|
+
if(this.limit != null) {
|
151
|
+
sql += ` LIMIT ${this.limit}`;
|
152
|
+
}
|
153
|
+
|
154
|
+
if(this.offset != null) {
|
155
|
+
sql += ` OFFSET ${this.offset}`;
|
156
|
+
}
|
157
|
+
|
158
|
+
return sql;
|
159
|
+
}
|
160
|
+
|
161
161
|
}
|