@ackplus/nest-crud-request 1.1.1 → 1.1.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/package.json +2 -2
- package/src/index.d.ts.map +1 -0
- package/src/lib/query-builder.d.ts.map +1 -0
- package/src/lib/relation-builder.d.ts.map +1 -0
- package/src/lib/types.d.ts +1 -0
- package/src/lib/types.d.ts.map +1 -0
- package/src/lib/utils.d.ts.map +1 -0
- package/src/lib/where-builder.d.ts.map +1 -0
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -3
- package/dist/lib/query-builder.d.ts.map +0 -1
- package/dist/lib/query-builder.js +0 -169
- package/dist/lib/relation-builder.d.ts.map +0 -1
- package/dist/lib/relation-builder.js +0 -58
- package/dist/lib/types.d.ts +0 -53
- package/dist/lib/types.d.ts.map +0 -1
- package/dist/lib/types.js +0 -31
- package/dist/lib/utils.d.ts.map +0 -1
- package/dist/lib/utils.js +0 -11
- package/dist/lib/where-builder.d.ts.map +0 -1
- package/dist/lib/where-builder.js +0 -146
- package/eslint.config.mjs +0 -22
- package/jest.config.ts +0 -10
- package/project.json +0 -46
- package/src/index.ts +0 -3
- package/src/lib/query-builder.ts +0 -189
- package/src/lib/relation-builder.ts +0 -68
- package/src/lib/types.js +0 -35
- package/src/lib/types.js.map +0 -1
- package/src/lib/types.ts +0 -61
- package/src/lib/utils.ts +0 -11
- package/src/lib/where-builder.ts +0 -159
- package/src/test/query-builder-where.spec.ts +0 -173
- package/src/test/query-builder.spec.ts +0 -140
- package/src/test/relation-builder.spec.ts +0 -32
- package/src/test/where-builder-complex.spec.ts +0 -173
- package/tsconfig.json +0 -17
- package/tsconfig.lib.json +0 -10
- package/tsconfig.spec.json +0 -15
- /package/{dist → src}/index.d.ts +0 -0
- /package/{dist → src}/lib/query-builder.d.ts +0 -0
- /package/{dist → src}/lib/relation-builder.d.ts +0 -0
- /package/{dist → src}/lib/utils.d.ts +0 -0
- /package/{dist → src}/lib/where-builder.d.ts +0 -0
package/src/lib/query-builder.ts
DELETED
|
@@ -1,189 +0,0 @@
|
|
|
1
|
-
import { RelationBuilder } from './relation-builder';
|
|
2
|
-
import { QueryBuilderOptions, OrderDirectionEnum } from './types';
|
|
3
|
-
import { deepMerge } from './utils';
|
|
4
|
-
import { WhereBuilder, WhereBuilderCondition } from './where-builder';
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
export class QueryBuilder {
|
|
8
|
-
|
|
9
|
-
private options: QueryBuilderOptions = {};
|
|
10
|
-
|
|
11
|
-
private whereBuilder: WhereBuilder = new WhereBuilder();
|
|
12
|
-
|
|
13
|
-
private relationBuilder: RelationBuilder = new RelationBuilder();
|
|
14
|
-
|
|
15
|
-
constructor(options?: QueryBuilderOptions) {
|
|
16
|
-
if (options) {
|
|
17
|
-
this.setOptions(options);
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
setOptions(options: QueryBuilderOptions): this {
|
|
22
|
-
this.options = options;
|
|
23
|
-
this.whereBuilder = new WhereBuilder(options.where);
|
|
24
|
-
this.relationBuilder = new RelationBuilder(options.relations);
|
|
25
|
-
return this;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
mergeOptions(options: QueryBuilderOptions, deep = false): this {
|
|
29
|
-
let updatedOptions = {};
|
|
30
|
-
if (deep) {
|
|
31
|
-
updatedOptions = deepMerge(this.options, options);
|
|
32
|
-
} else {
|
|
33
|
-
updatedOptions = {
|
|
34
|
-
...this.options,
|
|
35
|
-
...options,
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
this.setOptions(updatedOptions);
|
|
39
|
-
return this;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
addSelect(fields: string | string[]): this {
|
|
43
|
-
// Ensure select is always an array
|
|
44
|
-
if (!this.options.select || typeof this.options.select === 'string') {
|
|
45
|
-
this.options.select = [];
|
|
46
|
-
}
|
|
47
|
-
if (Array.isArray(fields)) {
|
|
48
|
-
this.options.select.push(...fields);
|
|
49
|
-
} else {
|
|
50
|
-
this.options.select.push(fields);
|
|
51
|
-
}
|
|
52
|
-
return this;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
removeSelect(fields: string | string[]): this {
|
|
56
|
-
// Ensure select is an array before filtering
|
|
57
|
-
if (this.options.select && Array.isArray(this.options.select)) {
|
|
58
|
-
if (Array.isArray(fields)) {
|
|
59
|
-
this.options.select = this.options.select.filter(field => !fields.includes(field));
|
|
60
|
-
} else {
|
|
61
|
-
this.options.select = this.options.select.filter(field => field !== fields);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
return this;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
addRelation(relation: string, select?: string[], where?: Record<string, any>): this {
|
|
68
|
-
this.relationBuilder.add(relation, select, where);
|
|
69
|
-
return this;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
removeRelation(relation: string): this {
|
|
73
|
-
this.relationBuilder.remove(relation);
|
|
74
|
-
return this;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
where(...args: WhereBuilderCondition): this {
|
|
78
|
-
this.whereBuilder.where(...args);
|
|
79
|
-
return this;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
andWhere(...args: WhereBuilderCondition): this {
|
|
83
|
-
this.whereBuilder.andWhere(...args);
|
|
84
|
-
return this;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
orWhere(...args: WhereBuilderCondition): this {
|
|
88
|
-
this.whereBuilder.orWhere(...args);
|
|
89
|
-
return this;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
addOrder(orderBy: string, order: OrderDirectionEnum): this {
|
|
93
|
-
if (!this.options.order) {
|
|
94
|
-
this.options.order = {};
|
|
95
|
-
}
|
|
96
|
-
this.options.order[orderBy] = order;
|
|
97
|
-
return this;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
removeOrder(orderBy: string): this {
|
|
101
|
-
if (this.options.order) {
|
|
102
|
-
delete this.options.order[orderBy];
|
|
103
|
-
}
|
|
104
|
-
return this;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
setSkip(skip: number): this {
|
|
108
|
-
this.options.skip = skip;
|
|
109
|
-
return this;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
setTake(take: number): this {
|
|
113
|
-
this.options.take = take;
|
|
114
|
-
return this;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
setWithDeleted(withDeleted: boolean): this {
|
|
118
|
-
this.options.withDeleted = withDeleted;
|
|
119
|
-
return this;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
setOnlyDeleted(onlyDeleted: boolean): this {
|
|
123
|
-
this.options.onlyDeleted = onlyDeleted;
|
|
124
|
-
return this;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
set(key: string, value: any): this {
|
|
128
|
-
this.options[key] = value;
|
|
129
|
-
return this;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
toObject(constrainToNestedObject = false) {
|
|
133
|
-
const options = {
|
|
134
|
-
...this.options,
|
|
135
|
-
};
|
|
136
|
-
|
|
137
|
-
// Convert where conditions to JSON string
|
|
138
|
-
if (this.whereBuilder.hasConditions()) {
|
|
139
|
-
if (constrainToNestedObject) {
|
|
140
|
-
options.where = this.whereBuilder.toObject();
|
|
141
|
-
} else {
|
|
142
|
-
options.where = JSON.stringify(this.whereBuilder.toObject());
|
|
143
|
-
}
|
|
144
|
-
} else {
|
|
145
|
-
delete options.where;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
// Convert relations to JSON string
|
|
149
|
-
if (this.relationBuilder.hasRelations()) {
|
|
150
|
-
if (constrainToNestedObject) {
|
|
151
|
-
options.relations = this.relationBuilder.toObject();
|
|
152
|
-
} else {
|
|
153
|
-
options.relations = JSON.stringify(this.relationBuilder.toObject());
|
|
154
|
-
}
|
|
155
|
-
} else {
|
|
156
|
-
delete options.relations;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// Convert order to JSON string if it exists
|
|
160
|
-
if (options.order && Object.keys(options.order).length > 0) {
|
|
161
|
-
if (constrainToNestedObject) {
|
|
162
|
-
options.order = options.order;
|
|
163
|
-
} else {
|
|
164
|
-
options.order = JSON.stringify(options.order);
|
|
165
|
-
}
|
|
166
|
-
} else {
|
|
167
|
-
delete options.order;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
// Convert select to JSON string if it exists
|
|
171
|
-
if (options.select && options.select.length > 0) {
|
|
172
|
-
if (constrainToNestedObject) {
|
|
173
|
-
options.select = options.select;
|
|
174
|
-
} else {
|
|
175
|
-
options.select = JSON.stringify(options.select);
|
|
176
|
-
}
|
|
177
|
-
} else {
|
|
178
|
-
delete options.select;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
return options;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
toJson() {
|
|
185
|
-
const obj = this.toObject(true);
|
|
186
|
-
return JSON.stringify(obj);
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
}
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import { RelationObject, RelationObjectValue, RelationOptions } from './types';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export class RelationBuilder {
|
|
5
|
-
|
|
6
|
-
private relations: RelationObject = {};
|
|
7
|
-
|
|
8
|
-
constructor(relations?: RelationOptions | string) {
|
|
9
|
-
if (relations) {
|
|
10
|
-
const relationOptions = typeof relations === 'string' ? JSON.parse(relations) : relations;
|
|
11
|
-
this.setRelations(relationOptions);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
setRelations(relations: RelationOptions): this {
|
|
16
|
-
if (relations) {
|
|
17
|
-
if (Array.isArray(relations)) {
|
|
18
|
-
relations.forEach(relation => {
|
|
19
|
-
this.add(relation);
|
|
20
|
-
});
|
|
21
|
-
} else if (typeof relations === 'string') {
|
|
22
|
-
this.add(relations);
|
|
23
|
-
} else {
|
|
24
|
-
this.relations = relations;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
return this;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
clear(): this {
|
|
31
|
-
this.relations = {};
|
|
32
|
-
return this;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
add(relation: string, select?: string[], where?: Record<string, any>): this {
|
|
36
|
-
if (!select && !where) {
|
|
37
|
-
this.relations[relation] = true;
|
|
38
|
-
} else {
|
|
39
|
-
const obj: any = {};
|
|
40
|
-
if (select) {
|
|
41
|
-
obj.select = select;
|
|
42
|
-
}
|
|
43
|
-
if (where) {
|
|
44
|
-
obj.where = where;
|
|
45
|
-
}
|
|
46
|
-
this.relations[relation] = obj;
|
|
47
|
-
}
|
|
48
|
-
return this;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
remove(relation: string): this {
|
|
52
|
-
delete this.relations[relation];
|
|
53
|
-
return this;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
hasRelations(): boolean {
|
|
57
|
-
return Object.keys(this.relations).length > 0;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
toObject(): RelationObject {
|
|
61
|
-
return this.relations;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
toJson(): string {
|
|
65
|
-
return JSON.stringify(this.relations);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
}
|
package/src/lib/types.js
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.OrderDirectionEnum = exports.WhereOperatorEnum = exports.WhereLogicalOperatorEnum = void 0;
|
|
4
|
-
var WhereLogicalOperatorEnum;
|
|
5
|
-
(function (WhereLogicalOperatorEnum) {
|
|
6
|
-
WhereLogicalOperatorEnum["AND"] = "$and";
|
|
7
|
-
WhereLogicalOperatorEnum["OR"] = "$or";
|
|
8
|
-
})(WhereLogicalOperatorEnum || (exports.WhereLogicalOperatorEnum = WhereLogicalOperatorEnum = {}));
|
|
9
|
-
var WhereOperatorEnum;
|
|
10
|
-
(function (WhereOperatorEnum) {
|
|
11
|
-
WhereOperatorEnum["EQ"] = "$eq";
|
|
12
|
-
WhereOperatorEnum["NOT_EQ"] = "$ne";
|
|
13
|
-
WhereOperatorEnum["GT"] = "$gt";
|
|
14
|
-
WhereOperatorEnum["GT_OR_EQ"] = "$gte";
|
|
15
|
-
WhereOperatorEnum["LT"] = "$lt";
|
|
16
|
-
WhereOperatorEnum["LT_OR_EQ"] = "$lte";
|
|
17
|
-
WhereOperatorEnum["IN"] = "$in";
|
|
18
|
-
WhereOperatorEnum["NOT_IN"] = "$notIn";
|
|
19
|
-
WhereOperatorEnum["LIKE"] = "$like";
|
|
20
|
-
WhereOperatorEnum["NOT_LIKE"] = "$notLike";
|
|
21
|
-
WhereOperatorEnum["ILIKE"] = "$iLike";
|
|
22
|
-
WhereOperatorEnum["NOT_ILIKE"] = "$notIlike";
|
|
23
|
-
WhereOperatorEnum["IS_NULL"] = "$isNull";
|
|
24
|
-
WhereOperatorEnum["IS_NOT_NULL"] = "$isNotNull";
|
|
25
|
-
WhereOperatorEnum["BETWEEN"] = "$between";
|
|
26
|
-
WhereOperatorEnum["NOT_BETWEEN"] = "$notBetween";
|
|
27
|
-
WhereOperatorEnum["IS_TRUE"] = "$isTrue";
|
|
28
|
-
WhereOperatorEnum["IS_FALSE"] = "$isFalse";
|
|
29
|
-
})(WhereOperatorEnum || (exports.WhereOperatorEnum = WhereOperatorEnum = {}));
|
|
30
|
-
var OrderDirectionEnum;
|
|
31
|
-
(function (OrderDirectionEnum) {
|
|
32
|
-
OrderDirectionEnum["ASC"] = "ASC";
|
|
33
|
-
OrderDirectionEnum["DESC"] = "DESC";
|
|
34
|
-
})(OrderDirectionEnum || (exports.OrderDirectionEnum = OrderDirectionEnum = {}));
|
|
35
|
-
//# sourceMappingURL=types.js.map
|
package/src/lib/types.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["types.ts"],"names":[],"mappings":";;;AAYA,IAAY,wBAGX;AAHD,WAAY,wBAAwB;IAChC,wCAAY,CAAA;IACZ,sCAAU,CAAA;AACd,CAAC,EAHW,wBAAwB,wCAAxB,wBAAwB,QAGnC;AAED,IAAY,iBAmBX;AAnBD,WAAY,iBAAiB;IACzB,+BAAU,CAAA;IACV,mCAAc,CAAA;IACd,+BAAU,CAAA;IACV,sCAAiB,CAAA;IACjB,+BAAU,CAAA;IACV,sCAAiB,CAAA;IACjB,+BAAU,CAAA;IACV,sCAAiB,CAAA;IACjB,mCAAc,CAAA;IACd,0CAAqB,CAAA;IACrB,qCAAgB,CAAA;IAChB,4CAAuB,CAAA;IACvB,wCAAmB,CAAA;IACnB,+CAA0B,CAAA;IAC1B,yCAAoB,CAAA;IACpB,gDAA2B,CAAA;IAC3B,wCAAmB,CAAA;IACnB,0CAAqB,CAAA;AACzB,CAAC,EAnBW,iBAAiB,iCAAjB,iBAAiB,QAmB5B;AAED,IAAY,kBAGX;AAHD,WAAY,kBAAkB;IAC1B,iCAAW,CAAA;IACX,mCAAa,CAAA;AACjB,CAAC,EAHW,kBAAkB,kCAAlB,kBAAkB,QAG7B"}
|
package/src/lib/types.ts
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
export interface QueryBuilderOptions {
|
|
2
|
-
[key: string]: any;
|
|
3
|
-
select?: string[] | string;
|
|
4
|
-
relations?: RelationOptions | string;
|
|
5
|
-
where?: WhereOptions | string;
|
|
6
|
-
order?: Record<string, OrderDirectionEnum> | string;
|
|
7
|
-
skip?: number;
|
|
8
|
-
take?: number;
|
|
9
|
-
withDeleted?: boolean;
|
|
10
|
-
onlyDeleted?: boolean;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export enum WhereLogicalOperatorEnum {
|
|
14
|
-
AND = '$and',
|
|
15
|
-
OR = '$or',
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export enum WhereOperatorEnum {
|
|
19
|
-
EQ = '$eq',
|
|
20
|
-
NOT_EQ = '$ne',
|
|
21
|
-
GT = '$gt',
|
|
22
|
-
GT_OR_EQ = '$gte',
|
|
23
|
-
LT = '$lt',
|
|
24
|
-
LT_OR_EQ = '$lte',
|
|
25
|
-
IN = '$in',
|
|
26
|
-
NOT_IN = '$notIn',
|
|
27
|
-
LIKE = '$like',
|
|
28
|
-
NOT_LIKE = '$notLike',
|
|
29
|
-
ILIKE = '$iLike',
|
|
30
|
-
NOT_ILIKE = '$notIlike',
|
|
31
|
-
IS_NULL = '$isNull',
|
|
32
|
-
IS_NOT_NULL = '$isNotNull',
|
|
33
|
-
BETWEEN = '$between',
|
|
34
|
-
NOT_BETWEEN = '$notBetween',
|
|
35
|
-
IS_TRUE = '$isTrue',
|
|
36
|
-
IS_FALSE = '$isFalse',
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export enum OrderDirectionEnum {
|
|
40
|
-
ASC = 'ASC',
|
|
41
|
-
DESC = 'DESC',
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export type WhereObject = {
|
|
45
|
-
[key: string]: any;
|
|
46
|
-
$and?: WhereObject | WhereObject[];
|
|
47
|
-
$or?: WhereObject | WhereObject[];
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
export type WhereOptions = WhereObject | WhereObject[];
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
export type RelationObjectValue = {
|
|
54
|
-
select?: string[];
|
|
55
|
-
where?: WhereObject | WhereObject[];
|
|
56
|
-
joinType?: 'left' | 'inner';
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
export type RelationObject = Record<string, RelationObjectValue | boolean>;
|
|
60
|
-
|
|
61
|
-
export type RelationOptions = string | string[] | RelationObject;
|
package/src/lib/utils.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
export function deepMerge(target: any, source: any): any {
|
|
2
|
-
for (const key in source) {
|
|
3
|
-
if (source[key] instanceof Object && key in target) {
|
|
4
|
-
Object.assign(source[key], deepMerge(target[key], source[key]));
|
|
5
|
-
}
|
|
6
|
-
}
|
|
7
|
-
return {
|
|
8
|
-
...target,
|
|
9
|
-
...source,
|
|
10
|
-
};
|
|
11
|
-
}
|
package/src/lib/where-builder.ts
DELETED
|
@@ -1,159 +0,0 @@
|
|
|
1
|
-
import { WhereLogicalOperatorEnum, WhereOperatorEnum } from './types';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export type WhereBuilderCondition = [string, any] | [string, WhereOperatorEnum, any] | [Record<string, any>] | [(builder: WhereBuilder) => void];
|
|
5
|
-
|
|
6
|
-
export class WhereBuilder {
|
|
7
|
-
|
|
8
|
-
private whereObject: Record<string, any> = {};
|
|
9
|
-
|
|
10
|
-
constructor(where?: Record<string, any> | string) {
|
|
11
|
-
this.whereObject = typeof where === 'string' ? JSON.parse(where) : where || {};
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
private isOperator(value: any): boolean {
|
|
15
|
-
return `${value}`.startsWith('$');
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
clear(): this {
|
|
19
|
-
this.whereObject = {};
|
|
20
|
-
return this;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
where(...args: WhereBuilderCondition): this {
|
|
24
|
-
this.parseCondition(null, ...args);
|
|
25
|
-
return this;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
andWhere(...args: WhereBuilderCondition): this {
|
|
29
|
-
this.parseCondition(WhereLogicalOperatorEnum.AND, ...args);
|
|
30
|
-
return this;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
orWhere(...args: WhereBuilderCondition): this {
|
|
34
|
-
this.parseCondition(WhereLogicalOperatorEnum.OR, ...args);
|
|
35
|
-
return this;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
removeWhere(field: string): this {
|
|
39
|
-
const keys = field.split('.');
|
|
40
|
-
let current = this.whereObject;
|
|
41
|
-
|
|
42
|
-
for (let i = 0; i < keys.length - 1; i++) {
|
|
43
|
-
if (!current[keys[i]]) {
|
|
44
|
-
return this; // If the path doesn't exist, do nothing
|
|
45
|
-
}
|
|
46
|
-
current = current[keys[i]];
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
delete current[keys[keys.length - 1]];
|
|
50
|
-
return this;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
hasConditions(): boolean {
|
|
54
|
-
return Object.keys(this.whereObject).length > 0;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
toObject(): Record<string, any> {
|
|
58
|
-
return this.whereObject;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
toJson(): string {
|
|
62
|
-
return JSON.stringify(this.whereObject);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
private parseCondition(type: WhereLogicalOperatorEnum | null, ...args: any[]): Record<string, any> {
|
|
66
|
-
let field: string;
|
|
67
|
-
let operator: WhereOperatorEnum;
|
|
68
|
-
let value: any;
|
|
69
|
-
|
|
70
|
-
if (args.length === 0) {
|
|
71
|
-
// Do nothing
|
|
72
|
-
} else if (args.length === 1) {
|
|
73
|
-
const condition = args[0];
|
|
74
|
-
if (typeof condition === 'function') {
|
|
75
|
-
// If the condition is a function, create a new WhereBuilder and call the function with it
|
|
76
|
-
const builder = new WhereBuilder();
|
|
77
|
-
condition(builder);
|
|
78
|
-
this.updateCondition(builder.toObject(), type);
|
|
79
|
-
} else if (condition instanceof WhereBuilder) {
|
|
80
|
-
// If the condition is a WhereBuilder
|
|
81
|
-
this.updateCondition(condition.toObject(), type);
|
|
82
|
-
} else {
|
|
83
|
-
// If the condition is a simple object
|
|
84
|
-
this.updateCondition(condition, type);
|
|
85
|
-
}
|
|
86
|
-
} else if (args.length === 2 || args.length === 3) {
|
|
87
|
-
if (args.length === 2) {
|
|
88
|
-
// if there are only two arguments, the operator is EQ
|
|
89
|
-
field = args[0];
|
|
90
|
-
value = args[1];
|
|
91
|
-
|
|
92
|
-
if (typeof value === 'object') {
|
|
93
|
-
const firstKey = Object.keys(value)[0];
|
|
94
|
-
// if the first key is a operator, update the value with the operator
|
|
95
|
-
if (firstKey.startsWith('$')) {
|
|
96
|
-
this.updateCondition({ [field]: value }, type);
|
|
97
|
-
} else {
|
|
98
|
-
this.updateCondition({ [field]: { [WhereOperatorEnum.EQ]: value } }, type);
|
|
99
|
-
}
|
|
100
|
-
} else if (this.isOperator(value)) {
|
|
101
|
-
this.updateCondition({ [field]: { [value]: true } }, type);
|
|
102
|
-
} else {
|
|
103
|
-
this.updateCondition({ [field]: { [WhereOperatorEnum.EQ]: value } }, type);
|
|
104
|
-
}
|
|
105
|
-
} else {
|
|
106
|
-
// if there are three arguments, the operator is the second argument
|
|
107
|
-
field = args[0];
|
|
108
|
-
operator = args[1];
|
|
109
|
-
value = args[2];
|
|
110
|
-
this.updateCondition({ [field]: { [operator]: value } }, type);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
return this;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
// private updateCondition(condition: Record<string, any>, type: '$and' | '$or'): void {
|
|
117
|
-
// this.whereObject[type] = [...(this.whereObject[type] || []), condition].filter(Boolean);
|
|
118
|
-
// }
|
|
119
|
-
|
|
120
|
-
private updateCondition(condition: Record<string, any>, type: WhereLogicalOperatorEnum | null): void {
|
|
121
|
-
if (type === null) {
|
|
122
|
-
// For direct where conditions, we need to merge intelligently
|
|
123
|
-
this.mergeConditions(this.whereObject, condition);
|
|
124
|
-
} else {
|
|
125
|
-
// For logical operators ($and, $or), add to the array
|
|
126
|
-
this.whereObject[type] = [...(this.whereObject[type] || []), condition].filter(Boolean);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Intelligently merge two condition objects, handling logical operators properly
|
|
132
|
-
*/
|
|
133
|
-
private mergeConditions(target: Record<string, any>, source: Record<string, any>): void {
|
|
134
|
-
for (const key in source) {
|
|
135
|
-
const sourceValue = source[key];
|
|
136
|
-
|
|
137
|
-
if (key === '$and' || key === '$or') {
|
|
138
|
-
// Handle logical operators - merge arrays
|
|
139
|
-
if (target[key]) {
|
|
140
|
-
// If target already has this logical operator, merge the arrays
|
|
141
|
-
target[key] = [...(target[key] || []), ...(Array.isArray(sourceValue) ? sourceValue : [sourceValue])];
|
|
142
|
-
} else {
|
|
143
|
-
// If target doesn't have this logical operator, set it
|
|
144
|
-
target[key] = Array.isArray(sourceValue) ? sourceValue : [sourceValue];
|
|
145
|
-
}
|
|
146
|
-
} else {
|
|
147
|
-
// Handle regular field conditions
|
|
148
|
-
if (target[key] && typeof target[key] === 'object' && typeof sourceValue === 'object') {
|
|
149
|
-
// If both target and source have the same field with object values, merge them
|
|
150
|
-
target[key] = { ...target[key], ...sourceValue };
|
|
151
|
-
} else {
|
|
152
|
-
// Otherwise, simply assign the value
|
|
153
|
-
target[key] = sourceValue;
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
}
|
|
@@ -1,173 +0,0 @@
|
|
|
1
|
-
import { QueryBuilder } from '../lib/query-builder';
|
|
2
|
-
import { WhereOperatorEnum } from '../lib/types';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
describe('QueryBuilder - Where Conditions', () => {
|
|
6
|
-
let queryBuilder: QueryBuilder;
|
|
7
|
-
|
|
8
|
-
beforeEach(() => {
|
|
9
|
-
queryBuilder = new QueryBuilder({});
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
it('should add where conditions', () => {
|
|
13
|
-
queryBuilder.where('age', 25);
|
|
14
|
-
expect(queryBuilder.toObject(true).where).toEqual({ age: { $eq: 25 } });
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
it('should add or where conditions', () => {
|
|
18
|
-
queryBuilder.orWhere('age', 25);
|
|
19
|
-
expect(queryBuilder.toObject(true).where).toEqual({ $or: [{ age: { $eq: 25 } }] });
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
it('should add where conditions with a function', () => {
|
|
23
|
-
queryBuilder.where((builder) => {
|
|
24
|
-
builder.where('age', 25);
|
|
25
|
-
builder.orWhere('name', 'John');
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
expect(queryBuilder.toObject(true).where).toEqual({
|
|
29
|
-
age: { $eq: 25 },
|
|
30
|
-
$or: [{ name: { $eq: 'John' } }],
|
|
31
|
-
});
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
it('should add or where conditions with a function', () => {
|
|
35
|
-
queryBuilder.orWhere((builder) => {
|
|
36
|
-
builder.where('age', 25);
|
|
37
|
-
builder.orWhere('name', 'John');
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
expect(queryBuilder.toObject(true).where).toEqual({
|
|
41
|
-
$or: [
|
|
42
|
-
{
|
|
43
|
-
age: { $eq: 25 },
|
|
44
|
-
$or: [{ name: { $eq: 'John' } }],
|
|
45
|
-
},
|
|
46
|
-
],
|
|
47
|
-
});
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
it('should handle complex where conditions with multiple nested functions', () => {
|
|
51
|
-
queryBuilder.where((builder) => {
|
|
52
|
-
builder.where('age', 25);
|
|
53
|
-
builder.orWhere((innerBuilder) => {
|
|
54
|
-
innerBuilder.where('name', 'John');
|
|
55
|
-
innerBuilder.where('status', 'active');
|
|
56
|
-
});
|
|
57
|
-
builder.where('country', 'USA');
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
expect(queryBuilder.toObject(true).where).toEqual({
|
|
61
|
-
age: { $eq: 25 },
|
|
62
|
-
$or: [
|
|
63
|
-
{
|
|
64
|
-
name: { $eq: 'John' },
|
|
65
|
-
status: { $eq: 'active' },
|
|
66
|
-
},
|
|
67
|
-
],
|
|
68
|
-
country: { $eq: 'USA' },
|
|
69
|
-
});
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
it('should handle complex or where conditions with multiple nested functions', () => {
|
|
73
|
-
queryBuilder.orWhere((builder) => {
|
|
74
|
-
builder.where('age', 25);
|
|
75
|
-
builder.orWhere((innerBuilder) => {
|
|
76
|
-
innerBuilder.where('name', 'John');
|
|
77
|
-
innerBuilder.orWhere('status', 'active');
|
|
78
|
-
});
|
|
79
|
-
builder.orWhere('country', 'USA');
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
expect(queryBuilder.toObject(true).where).toEqual({
|
|
83
|
-
$or: [
|
|
84
|
-
{
|
|
85
|
-
age: { $eq: 25 },
|
|
86
|
-
$or: [
|
|
87
|
-
{
|
|
88
|
-
name: { $eq: 'John' },
|
|
89
|
-
$or: [{ status: { $eq: 'active' } }],
|
|
90
|
-
},
|
|
91
|
-
{ country: { $eq: 'USA' } },
|
|
92
|
-
],
|
|
93
|
-
},
|
|
94
|
-
],
|
|
95
|
-
});
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
it('should handle empty where conditions', () => {
|
|
99
|
-
queryBuilder.where(() => {
|
|
100
|
-
// Do nothing
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
expect(queryBuilder.toObject(true).where).toEqual(undefined);
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
it('should handle single condition without nesting', () => {
|
|
107
|
-
queryBuilder.where('age', 30);
|
|
108
|
-
|
|
109
|
-
expect(queryBuilder.toObject(true).where).toEqual({
|
|
110
|
-
age: { $eq: 30 },
|
|
111
|
-
});
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
it('should handle multiple conditions with different operators', () => {
|
|
115
|
-
queryBuilder.where((builder) => {
|
|
116
|
-
builder.where('age', 30);
|
|
117
|
-
builder.where('salary', WhereOperatorEnum.GT, 50000);
|
|
118
|
-
builder.orWhere('status', 'employed');
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
expect(queryBuilder.toObject(true).where).toEqual({
|
|
122
|
-
age: { $eq: 30 },
|
|
123
|
-
salary: { $gt: 50000 },
|
|
124
|
-
$or: [{ status: { $eq: 'employed' } }],
|
|
125
|
-
});
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
it('should handle nested orWhere with multiple conditions', () => {
|
|
129
|
-
queryBuilder.orWhere((builder) => {
|
|
130
|
-
builder.where('age', 30);
|
|
131
|
-
builder.orWhere((innerBuilder) => {
|
|
132
|
-
innerBuilder.where('name', 'Alice');
|
|
133
|
-
innerBuilder.orWhere('city', 'New York');
|
|
134
|
-
});
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
expect(queryBuilder.toObject(true).where).toEqual({
|
|
138
|
-
$or: [
|
|
139
|
-
{
|
|
140
|
-
age: { $eq: 30 },
|
|
141
|
-
$or: [
|
|
142
|
-
{
|
|
143
|
-
name: { $eq: 'Alice' },
|
|
144
|
-
$or: [{ city: { $eq: 'New York' } }],
|
|
145
|
-
},
|
|
146
|
-
],
|
|
147
|
-
},
|
|
148
|
-
],
|
|
149
|
-
});
|
|
150
|
-
});
|
|
151
|
-
|
|
152
|
-
it('should handle complex conditions with mixed operators', () => {
|
|
153
|
-
queryBuilder.where((builder) => {
|
|
154
|
-
builder.where('age', { $gte: 18 });
|
|
155
|
-
builder.orWhere((innerBuilder) => {
|
|
156
|
-
innerBuilder.where('name', 'Bob');
|
|
157
|
-
innerBuilder.where('status', { $ne: 'inactive' });
|
|
158
|
-
});
|
|
159
|
-
builder.where('country', { $in: ['USA', 'Canada'] });
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
expect(queryBuilder.toObject(true).where).toEqual({
|
|
163
|
-
age: { $gte: 18 },
|
|
164
|
-
$or: [
|
|
165
|
-
{
|
|
166
|
-
name: { $eq: 'Bob' },
|
|
167
|
-
status: { $ne: 'inactive' },
|
|
168
|
-
},
|
|
169
|
-
],
|
|
170
|
-
country: { $in: ['USA', 'Canada'] },
|
|
171
|
-
});
|
|
172
|
-
});
|
|
173
|
-
});
|