@ackplus/nest-crud-request 1.1.2 → 1.1.6

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ackplus/nest-crud-request",
3
- "version": "1.1.2",
3
+ "version": "1.1.6",
4
4
  "type": "commonjs",
5
5
  "main": "./src/index.js",
6
6
  "types": "./src/index.d.ts",
package/src/index.js ADDED
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./lib/query-builder"), exports);
5
+ tslib_1.__exportStar(require("./lib/where-builder"), exports);
6
+ tslib_1.__exportStar(require("./lib/types"), exports);
@@ -0,0 +1,173 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.QueryBuilder = void 0;
4
+ const relation_builder_1 = require("./relation-builder");
5
+ const utils_1 = require("./utils");
6
+ const where_builder_1 = require("./where-builder");
7
+ class QueryBuilder {
8
+ options = {};
9
+ whereBuilder = new where_builder_1.WhereBuilder();
10
+ relationBuilder = new relation_builder_1.RelationBuilder();
11
+ constructor(options) {
12
+ if (options) {
13
+ this.setOptions(options);
14
+ }
15
+ }
16
+ setOptions(options) {
17
+ this.options = options;
18
+ this.whereBuilder = new where_builder_1.WhereBuilder(options.where);
19
+ this.relationBuilder = new relation_builder_1.RelationBuilder(options.relations);
20
+ return this;
21
+ }
22
+ mergeOptions(options, deep = false) {
23
+ let updatedOptions = {};
24
+ if (deep) {
25
+ updatedOptions = (0, utils_1.deepMerge)(this.options, options);
26
+ }
27
+ else {
28
+ updatedOptions = {
29
+ ...this.options,
30
+ ...options,
31
+ };
32
+ }
33
+ this.setOptions(updatedOptions);
34
+ return this;
35
+ }
36
+ addSelect(fields) {
37
+ // Ensure select is always an array
38
+ if (!this.options.select || typeof this.options.select === 'string') {
39
+ this.options.select = [];
40
+ }
41
+ if (Array.isArray(fields)) {
42
+ this.options.select.push(...fields);
43
+ }
44
+ else {
45
+ this.options.select.push(fields);
46
+ }
47
+ return this;
48
+ }
49
+ removeSelect(fields) {
50
+ // Ensure select is an array before filtering
51
+ if (this.options.select && Array.isArray(this.options.select)) {
52
+ if (Array.isArray(fields)) {
53
+ this.options.select = this.options.select.filter(field => !fields.includes(field));
54
+ }
55
+ else {
56
+ this.options.select = this.options.select.filter(field => field !== fields);
57
+ }
58
+ }
59
+ return this;
60
+ }
61
+ addRelation(relation, select, where) {
62
+ this.relationBuilder.add(relation, select, where);
63
+ return this;
64
+ }
65
+ removeRelation(relation) {
66
+ this.relationBuilder.remove(relation);
67
+ return this;
68
+ }
69
+ where(...args) {
70
+ this.whereBuilder.where(...args);
71
+ return this;
72
+ }
73
+ andWhere(...args) {
74
+ this.whereBuilder.andWhere(...args);
75
+ return this;
76
+ }
77
+ orWhere(...args) {
78
+ this.whereBuilder.orWhere(...args);
79
+ return this;
80
+ }
81
+ addOrder(orderBy, order) {
82
+ if (!this.options.order) {
83
+ this.options.order = {};
84
+ }
85
+ this.options.order[orderBy] = order;
86
+ return this;
87
+ }
88
+ removeOrder(orderBy) {
89
+ if (this.options.order) {
90
+ delete this.options.order[orderBy];
91
+ }
92
+ return this;
93
+ }
94
+ setSkip(skip) {
95
+ this.options.skip = skip;
96
+ return this;
97
+ }
98
+ setTake(take) {
99
+ this.options.take = take;
100
+ return this;
101
+ }
102
+ setWithDeleted(withDeleted) {
103
+ this.options.withDeleted = withDeleted;
104
+ return this;
105
+ }
106
+ setOnlyDeleted(onlyDeleted) {
107
+ this.options.onlyDeleted = onlyDeleted;
108
+ return this;
109
+ }
110
+ set(key, value) {
111
+ this.options[key] = value;
112
+ return this;
113
+ }
114
+ toObject(constrainToNestedObject = false) {
115
+ const options = {
116
+ ...this.options,
117
+ };
118
+ // Convert where conditions to JSON string
119
+ if (this.whereBuilder.hasConditions()) {
120
+ if (constrainToNestedObject) {
121
+ options.where = this.whereBuilder.toObject();
122
+ }
123
+ else {
124
+ options.where = JSON.stringify(this.whereBuilder.toObject());
125
+ }
126
+ }
127
+ else {
128
+ delete options.where;
129
+ }
130
+ // Convert relations to JSON string
131
+ if (this.relationBuilder.hasRelations()) {
132
+ if (constrainToNestedObject) {
133
+ options.relations = this.relationBuilder.toObject();
134
+ }
135
+ else {
136
+ options.relations = JSON.stringify(this.relationBuilder.toObject());
137
+ }
138
+ }
139
+ else {
140
+ delete options.relations;
141
+ }
142
+ // Convert order to JSON string if it exists
143
+ if (options.order && Object.keys(options.order).length > 0) {
144
+ if (constrainToNestedObject) {
145
+ options.order = options.order;
146
+ }
147
+ else {
148
+ options.order = JSON.stringify(options.order);
149
+ }
150
+ }
151
+ else {
152
+ delete options.order;
153
+ }
154
+ // Convert select to JSON string if it exists
155
+ if (options.select && options.select.length > 0) {
156
+ if (constrainToNestedObject) {
157
+ options.select = options.select;
158
+ }
159
+ else {
160
+ options.select = JSON.stringify(options.select);
161
+ }
162
+ }
163
+ else {
164
+ delete options.select;
165
+ }
166
+ return options;
167
+ }
168
+ toJson() {
169
+ const obj = this.toObject(true);
170
+ return JSON.stringify(obj);
171
+ }
172
+ }
173
+ exports.QueryBuilder = QueryBuilder;
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RelationBuilder = void 0;
4
+ class RelationBuilder {
5
+ relations = {};
6
+ constructor(relations) {
7
+ if (relations) {
8
+ const relationOptions = typeof relations === 'string' ? JSON.parse(relations) : relations;
9
+ this.setRelations(relationOptions);
10
+ }
11
+ }
12
+ setRelations(relations) {
13
+ if (relations) {
14
+ if (Array.isArray(relations)) {
15
+ relations.forEach(relation => {
16
+ this.add(relation);
17
+ });
18
+ }
19
+ else if (typeof relations === 'string') {
20
+ this.add(relations);
21
+ }
22
+ else {
23
+ this.relations = relations;
24
+ }
25
+ }
26
+ return this;
27
+ }
28
+ clear() {
29
+ this.relations = {};
30
+ return this;
31
+ }
32
+ add(relation, select, where) {
33
+ if (!select && !where) {
34
+ this.relations[relation] = true;
35
+ }
36
+ else {
37
+ const obj = {};
38
+ if (select) {
39
+ obj.select = select;
40
+ }
41
+ if (where) {
42
+ obj.where = where;
43
+ }
44
+ this.relations[relation] = obj;
45
+ }
46
+ return this;
47
+ }
48
+ remove(relation) {
49
+ delete this.relations[relation];
50
+ return this;
51
+ }
52
+ hasRelations() {
53
+ return Object.keys(this.relations).length > 0;
54
+ }
55
+ toObject() {
56
+ return this.relations;
57
+ }
58
+ toJson() {
59
+ return JSON.stringify(this.relations);
60
+ }
61
+ }
62
+ exports.RelationBuilder = RelationBuilder;
@@ -0,0 +1,34 @@
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 = {}));
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.deepMerge = deepMerge;
4
+ function deepMerge(target, source) {
5
+ for (const key in source) {
6
+ if (source[key] instanceof Object && key in target) {
7
+ Object.assign(source[key], deepMerge(target[key], source[key]));
8
+ }
9
+ }
10
+ return {
11
+ ...target,
12
+ ...source,
13
+ };
14
+ }
@@ -0,0 +1,150 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WhereBuilder = void 0;
4
+ const types_1 = require("./types");
5
+ class WhereBuilder {
6
+ whereObject = {};
7
+ constructor(where) {
8
+ this.whereObject = typeof where === 'string' ? JSON.parse(where) : where || {};
9
+ }
10
+ isOperator(value) {
11
+ return `${value}`.startsWith('$');
12
+ }
13
+ clear() {
14
+ this.whereObject = {};
15
+ return this;
16
+ }
17
+ where(...args) {
18
+ this.parseCondition(null, ...args);
19
+ return this;
20
+ }
21
+ andWhere(...args) {
22
+ this.parseCondition(types_1.WhereLogicalOperatorEnum.AND, ...args);
23
+ return this;
24
+ }
25
+ orWhere(...args) {
26
+ this.parseCondition(types_1.WhereLogicalOperatorEnum.OR, ...args);
27
+ return this;
28
+ }
29
+ removeWhere(field) {
30
+ const keys = field.split('.');
31
+ let current = this.whereObject;
32
+ for (let i = 0; i < keys.length - 1; i++) {
33
+ if (!current[keys[i]]) {
34
+ return this; // If the path doesn't exist, do nothing
35
+ }
36
+ current = current[keys[i]];
37
+ }
38
+ delete current[keys[keys.length - 1]];
39
+ return this;
40
+ }
41
+ hasConditions() {
42
+ return Object.keys(this.whereObject).length > 0;
43
+ }
44
+ toObject() {
45
+ return this.whereObject;
46
+ }
47
+ toJson() {
48
+ return JSON.stringify(this.whereObject);
49
+ }
50
+ parseCondition(type, ...args) {
51
+ let field;
52
+ let operator;
53
+ let value;
54
+ if (args.length === 0) {
55
+ // Do nothing
56
+ }
57
+ else if (args.length === 1) {
58
+ const condition = args[0];
59
+ if (typeof condition === 'function') {
60
+ // If the condition is a function, create a new WhereBuilder and call the function with it
61
+ const builder = new WhereBuilder();
62
+ condition(builder);
63
+ this.updateCondition(builder.toObject(), type);
64
+ }
65
+ else if (condition instanceof WhereBuilder) {
66
+ // If the condition is a WhereBuilder
67
+ this.updateCondition(condition.toObject(), type);
68
+ }
69
+ else {
70
+ // If the condition is a simple object
71
+ this.updateCondition(condition, type);
72
+ }
73
+ }
74
+ else if (args.length === 2 || args.length === 3) {
75
+ if (args.length === 2) {
76
+ // if there are only two arguments, the operator is EQ
77
+ field = args[0];
78
+ value = args[1];
79
+ if (typeof value === 'object') {
80
+ const firstKey = Object.keys(value)[0];
81
+ // if the first key is a operator, update the value with the operator
82
+ if (firstKey.startsWith('$')) {
83
+ this.updateCondition({ [field]: value }, type);
84
+ }
85
+ else {
86
+ this.updateCondition({ [field]: { [types_1.WhereOperatorEnum.EQ]: value } }, type);
87
+ }
88
+ }
89
+ else if (this.isOperator(value)) {
90
+ this.updateCondition({ [field]: { [value]: true } }, type);
91
+ }
92
+ else {
93
+ this.updateCondition({ [field]: { [types_1.WhereOperatorEnum.EQ]: value } }, type);
94
+ }
95
+ }
96
+ else {
97
+ // if there are three arguments, the operator is the second argument
98
+ field = args[0];
99
+ operator = args[1];
100
+ value = args[2];
101
+ this.updateCondition({ [field]: { [operator]: value } }, type);
102
+ }
103
+ }
104
+ return this;
105
+ }
106
+ // private updateCondition(condition: Record<string, any>, type: '$and' | '$or'): void {
107
+ // this.whereObject[type] = [...(this.whereObject[type] || []), condition].filter(Boolean);
108
+ // }
109
+ updateCondition(condition, type) {
110
+ if (type === null) {
111
+ // For direct where conditions, we need to merge intelligently
112
+ this.mergeConditions(this.whereObject, condition);
113
+ }
114
+ else {
115
+ // For logical operators ($and, $or), add to the array
116
+ this.whereObject[type] = [...(this.whereObject[type] || []), condition].filter(Boolean);
117
+ }
118
+ }
119
+ /**
120
+ * Intelligently merge two condition objects, handling logical operators properly
121
+ */
122
+ mergeConditions(target, source) {
123
+ for (const key in source) {
124
+ const sourceValue = source[key];
125
+ if (key === '$and' || key === '$or') {
126
+ // Handle logical operators - merge arrays
127
+ if (target[key]) {
128
+ // If target already has this logical operator, merge the arrays
129
+ target[key] = [...(target[key] || []), ...(Array.isArray(sourceValue) ? sourceValue : [sourceValue])];
130
+ }
131
+ else {
132
+ // If target doesn't have this logical operator, set it
133
+ target[key] = Array.isArray(sourceValue) ? sourceValue : [sourceValue];
134
+ }
135
+ }
136
+ else {
137
+ // Handle regular field conditions
138
+ if (target[key] && typeof target[key] === 'object' && typeof sourceValue === 'object') {
139
+ // If both target and source have the same field with object values, merge them
140
+ target[key] = { ...target[key], ...sourceValue };
141
+ }
142
+ else {
143
+ // Otherwise, simply assign the value
144
+ target[key] = sourceValue;
145
+ }
146
+ }
147
+ }
148
+ }
149
+ }
150
+ exports.WhereBuilder = WhereBuilder;