@ackplus/nest-crud-request 1.1.1 → 1.1.5
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/index.js +6 -0
- package/src/lib/query-builder.d.ts.map +1 -0
- package/{dist → src}/lib/query-builder.js +13 -9
- package/src/lib/relation-builder.d.ts.map +1 -0
- package/{dist → src}/lib/relation-builder.js +5 -1
- package/src/lib/types.d.ts +1 -0
- package/src/lib/types.d.ts.map +1 -0
- package/src/lib/types.js +0 -1
- package/src/lib/utils.d.ts.map +1 -0
- package/{dist → src}/lib/utils.js +4 -1
- package/src/lib/where-builder.d.ts.map +1 -0
- package/{dist → src}/lib/where-builder.js +10 -6
- 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/relation-builder.d.ts.map +0 -1
- 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/where-builder.d.ts.map +0 -1
- 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.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/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
|
-
});
|
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
import { QueryBuilder } from '../lib/query-builder';
|
|
2
|
-
import { OrderDirectionEnum } from '../lib/types';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
describe('QueryBuilder', () => {
|
|
6
|
-
let queryBuilder: QueryBuilder;
|
|
7
|
-
|
|
8
|
-
beforeEach(() => {
|
|
9
|
-
queryBuilder = new QueryBuilder({});
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
it('should set options', () => {
|
|
13
|
-
const options = { select: ['name', 'age'] };
|
|
14
|
-
queryBuilder.setOptions(options);
|
|
15
|
-
expect(queryBuilder.toObject(true).select).toEqual(options.select);
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
it('should merge options shallowly', () => {
|
|
19
|
-
queryBuilder.setOptions({ select: ['name'] });
|
|
20
|
-
queryBuilder.mergeOptions({ select: ['age'] });
|
|
21
|
-
expect(queryBuilder.toObject(true).select).toEqual(['age']);
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
it('should merge options deeply', () => {
|
|
25
|
-
queryBuilder.setOptions({ where: { age: { $gt: 18 } } });
|
|
26
|
-
queryBuilder.mergeOptions({ where: { name: { $eq: 'John' } } }, true);
|
|
27
|
-
expect(queryBuilder.toObject(true).where).toEqual({
|
|
28
|
-
age: { $gt: 18 },
|
|
29
|
-
name: { $eq: 'John' },
|
|
30
|
-
});
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
it('should add select fields', () => {
|
|
34
|
-
queryBuilder.addSelect('name');
|
|
35
|
-
queryBuilder.addSelect(['age', 'email']);
|
|
36
|
-
expect(queryBuilder.toObject(true).select).toEqual([
|
|
37
|
-
'name',
|
|
38
|
-
'age',
|
|
39
|
-
'email',
|
|
40
|
-
]);
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
it('should remove select fields', () => {
|
|
44
|
-
queryBuilder.addSelect([
|
|
45
|
-
'name',
|
|
46
|
-
'age',
|
|
47
|
-
'email',
|
|
48
|
-
]);
|
|
49
|
-
queryBuilder.removeSelect('age');
|
|
50
|
-
expect(queryBuilder.toObject(true).select).toEqual(['name', 'email']);
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
it('should add and remove relations as array of objects', () => {
|
|
54
|
-
queryBuilder.addRelation('profile', ['id', 'bio']);
|
|
55
|
-
queryBuilder.addRelation('posts', ['title', 'content']);
|
|
56
|
-
|
|
57
|
-
const relations = queryBuilder.toObject(true).relations;
|
|
58
|
-
expect(relations).toEqual(
|
|
59
|
-
expect.objectContaining({
|
|
60
|
-
profile: { select: ['id', 'bio'] },
|
|
61
|
-
posts: { select: ['title', 'content'] },
|
|
62
|
-
}),
|
|
63
|
-
);
|
|
64
|
-
|
|
65
|
-
queryBuilder.removeRelation('profile');
|
|
66
|
-
const updatedRelations = queryBuilder.toObject(true).relations;
|
|
67
|
-
expect(updatedRelations).toEqual(
|
|
68
|
-
expect.objectContaining({
|
|
69
|
-
posts: { select: ['title', 'content'] },
|
|
70
|
-
}),
|
|
71
|
-
);
|
|
72
|
-
expect(updatedRelations).not.toEqual(
|
|
73
|
-
expect.objectContaining({ profile: true }),
|
|
74
|
-
);
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
it('should handle null or empty relations', () => {
|
|
78
|
-
queryBuilder.addRelation('profile');
|
|
79
|
-
expect(queryBuilder.toObject(true).relations).not.toEqual(undefined);
|
|
80
|
-
|
|
81
|
-
queryBuilder.addRelation('profile');
|
|
82
|
-
queryBuilder.removeRelation('profile');
|
|
83
|
-
expect(queryBuilder.toObject(true).relations).toEqual(undefined);
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
it('should add and remove relations as array of strings', () => {
|
|
87
|
-
queryBuilder.addRelation('profile');
|
|
88
|
-
queryBuilder.addRelation('posts');
|
|
89
|
-
|
|
90
|
-
const relations = queryBuilder.toObject(true).relations;
|
|
91
|
-
expect(relations).toEqual(
|
|
92
|
-
expect.objectContaining({ profile: true, posts: true }),
|
|
93
|
-
);
|
|
94
|
-
|
|
95
|
-
queryBuilder.removeRelation('profile');
|
|
96
|
-
const updatedRelations = queryBuilder.toObject(true).relations;
|
|
97
|
-
expect(updatedRelations).toEqual(
|
|
98
|
-
expect.objectContaining({ posts: true }),
|
|
99
|
-
);
|
|
100
|
-
expect(updatedRelations).not.toEqual(
|
|
101
|
-
expect.objectContaining({ profile: true }),
|
|
102
|
-
);
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
it('should add where conditions', () => {
|
|
106
|
-
queryBuilder.where('age', 25);
|
|
107
|
-
expect(queryBuilder.toObject(true).where).toEqual({ age: { $eq: 25 } });
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
it('should handle empty where conditions', () => {
|
|
111
|
-
queryBuilder.where(() => {
|
|
112
|
-
// Do nothing
|
|
113
|
-
});
|
|
114
|
-
expect(queryBuilder.toObject(true).where).toEqual(undefined);
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
it('should add order', () => {
|
|
118
|
-
queryBuilder.addOrder('name', OrderDirectionEnum.ASC);
|
|
119
|
-
expect(queryBuilder.toObject(true).order).toEqual({ name: OrderDirectionEnum.ASC });
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
it('should remove order', () => {
|
|
123
|
-
queryBuilder.addOrder('name', OrderDirectionEnum.ASC);
|
|
124
|
-
queryBuilder.removeOrder('name');
|
|
125
|
-
expect(queryBuilder.toObject(true).order).toEqual(undefined);
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
it('should set skip and take', () => {
|
|
129
|
-
queryBuilder.setSkip(10);
|
|
130
|
-
queryBuilder.setTake(5);
|
|
131
|
-
expect(queryBuilder.toObject(true).skip).toBe(10);
|
|
132
|
-
expect(queryBuilder.toObject(true).take).toBe(5);
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
it('should convert to JSON', () => {
|
|
136
|
-
queryBuilder.setOptions({ select: ['name'] });
|
|
137
|
-
const json = queryBuilder.toJson();
|
|
138
|
-
expect(json).toBe(JSON.stringify(queryBuilder.toObject(true)));
|
|
139
|
-
});
|
|
140
|
-
});
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { RelationBuilder } from '../lib/relation-builder';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
describe('RelationBuilder', () => {
|
|
5
|
-
let relationBuilder: RelationBuilder;
|
|
6
|
-
|
|
7
|
-
beforeEach(() => {
|
|
8
|
-
relationBuilder = new RelationBuilder();
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
it('should handle empty relations', () => {
|
|
12
|
-
// Initially, the relations should be empty
|
|
13
|
-
expect(relationBuilder.toObject()).toEqual({});
|
|
14
|
-
|
|
15
|
-
// Clear any relations if set
|
|
16
|
-
relationBuilder.clear();
|
|
17
|
-
expect(relationBuilder.toObject()).toEqual({});
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
it('should handle setting empty relations', () => {
|
|
21
|
-
// Set empty relations
|
|
22
|
-
relationBuilder.setRelations([]);
|
|
23
|
-
expect(relationBuilder.toObject()).toEqual({});
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
it('should handle clearing relations', () => {
|
|
27
|
-
// Add some relations and then clear them
|
|
28
|
-
relationBuilder.setRelations(['relation1', 'relation2']);
|
|
29
|
-
relationBuilder.clear();
|
|
30
|
-
expect(relationBuilder.toObject()).toEqual({});
|
|
31
|
-
});
|
|
32
|
-
});
|