@goatlab/fluent 0.7.14 → 0.7.17
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/dist/TypeOrmConnector/TypeOrmConnector.js +11 -5
- package/dist/TypeOrmConnector/queryBuilder/mongodb/getMongoFindAggregatedQuery.js +11 -0
- package/dist/TypeOrmConnector/queryBuilder/mongodb/getMongoOrderBy.d.ts +4 -0
- package/dist/TypeOrmConnector/queryBuilder/mongodb/getMongoOrderBy.js +19 -0
- package/dist/TypeOrmConnector/queryBuilder/mongodb/getMongoWhere.js +35 -35
- package/dist/TypeOrmConnector/test/advanced/advancedTestSuite.js +34 -27
- package/dist/TypeOrmConnector/test/basic/basicTestSuite.js +12 -2
- package/dist/TypeOrmConnector/test/relations/relationsTestsSuite.js +0 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -2
|
@@ -34,6 +34,10 @@ class TypeOrmConnector extends BaseConnector_1.BaseConnector {
|
|
|
34
34
|
}
|
|
35
35
|
async insert(data) {
|
|
36
36
|
const validatedData = this.inputSchema.parse(data);
|
|
37
|
+
if (this.isMongoDB && validatedData['id']) {
|
|
38
|
+
validatedData['_id'] = js_utils_1.Ids.objectID(validatedData['id']);
|
|
39
|
+
delete validatedData['id'];
|
|
40
|
+
}
|
|
37
41
|
let datum = await this.repository.save(validatedData);
|
|
38
42
|
if (this.isMongoDB) {
|
|
39
43
|
datum['id'] = datum['id'].toString();
|
|
@@ -54,7 +58,7 @@ class TypeOrmConnector extends BaseConnector_1.BaseConnector {
|
|
|
54
58
|
}
|
|
55
59
|
async findMany(query) {
|
|
56
60
|
const requiresCustomQuery = query?.include && Object.keys(query.include).length;
|
|
57
|
-
if (this.isMongoDB
|
|
61
|
+
if (this.isMongoDB) {
|
|
58
62
|
const results = await this.customMongoRelatedFind(query);
|
|
59
63
|
return results;
|
|
60
64
|
}
|
|
@@ -70,7 +74,7 @@ class TypeOrmConnector extends BaseConnector_1.BaseConnector {
|
|
|
70
74
|
let [found, count] = await this.repository.findAndCount(generatedQuery);
|
|
71
75
|
found.map(d => {
|
|
72
76
|
if (this.isMongoDB) {
|
|
73
|
-
d['id'] = d['
|
|
77
|
+
d['id'] = d['_id'].toString();
|
|
74
78
|
}
|
|
75
79
|
(0, clearEmpties_1.clearEmpties)(js_utils_1.Objects.deleteNulls(d));
|
|
76
80
|
});
|
|
@@ -224,9 +228,6 @@ class TypeOrmConnector extends BaseConnector_1.BaseConnector {
|
|
|
224
228
|
if (query?.select) {
|
|
225
229
|
const selectQuery = js_utils_1.Objects.flatten(query?.select || {});
|
|
226
230
|
filter.select = selectQuery;
|
|
227
|
-
if (this.isMongoDB) {
|
|
228
|
-
filter.select = Object.keys(selectQuery);
|
|
229
|
-
}
|
|
230
231
|
}
|
|
231
232
|
if (query?.orderBy) {
|
|
232
233
|
filter.order = (0, extractOrderBy_1.extractOrderBy)(query.orderBy);
|
|
@@ -391,6 +392,11 @@ class TypeOrmConnector extends BaseConnector_1.BaseConnector {
|
|
|
391
392
|
self: this
|
|
392
393
|
});
|
|
393
394
|
const raw = await this.mongoRaw().aggregate(aggregate).toArray();
|
|
395
|
+
if (query?.select) {
|
|
396
|
+
return this.outputSchema['deepPartial']()
|
|
397
|
+
.array()
|
|
398
|
+
.parse(raw);
|
|
399
|
+
}
|
|
394
400
|
return this.outputSchema?.array().parse(raw);
|
|
395
401
|
}
|
|
396
402
|
}
|
|
@@ -2,10 +2,12 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getMongoFindAggregatedQuery = void 0;
|
|
4
4
|
const getMongoBaseAggregations_1 = require("./getMongoBaseAggregations");
|
|
5
|
+
const getMongoOrderBy_1 = require("./getMongoOrderBy");
|
|
5
6
|
const getMongoSelect_1 = require("./getMongoSelect");
|
|
6
7
|
const getMongoWhere_1 = require("./getMongoWhere");
|
|
7
8
|
const getMongoFindAggregatedQuery = ({ query, self }) => {
|
|
8
9
|
const selected = (0, getMongoSelect_1.getMongoSelect)(query?.select);
|
|
10
|
+
const orderBy = (0, getMongoOrderBy_1.getMongoOrderBy)(query?.orderBy);
|
|
9
11
|
const where = (0, getMongoWhere_1.getMongoWhere)({
|
|
10
12
|
where: query?.where
|
|
11
13
|
});
|
|
@@ -18,6 +20,15 @@ const getMongoFindAggregatedQuery = ({ query, self }) => {
|
|
|
18
20
|
$match: where
|
|
19
21
|
}
|
|
20
22
|
];
|
|
23
|
+
if (orderBy) {
|
|
24
|
+
aggregate.push(orderBy);
|
|
25
|
+
}
|
|
26
|
+
if (!query?.include) {
|
|
27
|
+
aggregate.push({ $addFields: { id: { $toString: '$_id' } } });
|
|
28
|
+
}
|
|
29
|
+
if (query?.offset) {
|
|
30
|
+
aggregate.push({ $skip: query?.offset });
|
|
31
|
+
}
|
|
21
32
|
if (query?.limit) {
|
|
22
33
|
aggregate.push({ $limit: query.limit });
|
|
23
34
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getMongoOrderBy = void 0;
|
|
4
|
+
const extractOrderBy_1 = require("../../util/extractOrderBy");
|
|
5
|
+
const getMongoOrderBy = (orderBy) => {
|
|
6
|
+
if (!orderBy) {
|
|
7
|
+
return undefined;
|
|
8
|
+
}
|
|
9
|
+
const order = (0, extractOrderBy_1.extractOrderBy)(orderBy);
|
|
10
|
+
const sort = {
|
|
11
|
+
$sort: {}
|
|
12
|
+
};
|
|
13
|
+
for (const key of Object.keys(order)) {
|
|
14
|
+
const val = order[key];
|
|
15
|
+
sort.$sort[key] = val === 'asc' ? 1 : -1;
|
|
16
|
+
}
|
|
17
|
+
return sort;
|
|
18
|
+
};
|
|
19
|
+
exports.getMongoOrderBy = getMongoOrderBy;
|
|
@@ -10,7 +10,7 @@ const getMongoWhere = ({ where }) => {
|
|
|
10
10
|
return {};
|
|
11
11
|
}
|
|
12
12
|
const Filters = {
|
|
13
|
-
|
|
13
|
+
filter: { $or: [{ $and: [] }] }
|
|
14
14
|
};
|
|
15
15
|
const orConditions = (0, extractConditions_1.extractConditions)(where['OR']);
|
|
16
16
|
const andConditions = (0, extractConditions_1.extractConditions)(where['AND']);
|
|
@@ -32,39 +32,39 @@ const getMongoWhere = ({ where }) => {
|
|
|
32
32
|
}
|
|
33
33
|
switch (operator) {
|
|
34
34
|
case types_1.LogicOperator.equals:
|
|
35
|
-
Filters.
|
|
35
|
+
Filters.filter.$or[0].$and.push({ [element]: { $eq: value } });
|
|
36
36
|
break;
|
|
37
37
|
case types_1.LogicOperator.isNot:
|
|
38
|
-
Filters.
|
|
38
|
+
Filters.filter.$or[0].$and.push({ [element]: { $neq: value } });
|
|
39
39
|
break;
|
|
40
40
|
case types_1.LogicOperator.greaterThan:
|
|
41
|
-
Filters.
|
|
41
|
+
Filters.filter.$or[0].$and.push({ [element]: { $gt: value } });
|
|
42
42
|
break;
|
|
43
43
|
case types_1.LogicOperator.greaterOrEqualThan:
|
|
44
|
-
Filters.
|
|
44
|
+
Filters.filter.$or[0].$and.push({ [element]: { $gte: value } });
|
|
45
45
|
break;
|
|
46
46
|
case types_1.LogicOperator.lessThan:
|
|
47
|
-
Filters.
|
|
47
|
+
Filters.filter.$or[0].$and.push({ [element]: { $lt: value } });
|
|
48
48
|
break;
|
|
49
49
|
case types_1.LogicOperator.lessOrEqualThan:
|
|
50
|
-
Filters.
|
|
50
|
+
Filters.filter.$or[0].$and.push({ [element]: { $lte: value } });
|
|
51
51
|
break;
|
|
52
52
|
case types_1.LogicOperator.in:
|
|
53
|
-
Filters.
|
|
53
|
+
Filters.filter.$or[0].$and.push({ [element]: { $in: value } });
|
|
54
54
|
break;
|
|
55
55
|
case types_1.LogicOperator.notIn:
|
|
56
|
-
Filters.
|
|
56
|
+
Filters.filter.$or[0].$and.push({
|
|
57
57
|
[element]: { $not: { $in: value } }
|
|
58
58
|
});
|
|
59
59
|
break;
|
|
60
60
|
case types_1.LogicOperator.exists:
|
|
61
|
-
Filters.
|
|
61
|
+
Filters.filter.$or[0].$and.push({ [element]: { $exists: true } });
|
|
62
62
|
break;
|
|
63
63
|
case types_1.LogicOperator.notExists:
|
|
64
|
-
Filters.
|
|
64
|
+
Filters.filter.$or[0].$and.push({ [element]: { $exists: false } });
|
|
65
65
|
break;
|
|
66
66
|
case types_1.LogicOperator.regexp:
|
|
67
|
-
Filters.
|
|
67
|
+
Filters.filter.$or[0].$and.push({ [element]: { $regex: value } });
|
|
68
68
|
break;
|
|
69
69
|
}
|
|
70
70
|
}
|
|
@@ -78,39 +78,39 @@ const getMongoWhere = ({ where }) => {
|
|
|
78
78
|
}
|
|
79
79
|
switch (operator) {
|
|
80
80
|
case types_1.LogicOperator.equals:
|
|
81
|
-
Filters.
|
|
81
|
+
Filters.filter.$or[0].$and.push({ [element]: { $eq: value } });
|
|
82
82
|
break;
|
|
83
83
|
case types_1.LogicOperator.isNot:
|
|
84
|
-
Filters.
|
|
84
|
+
Filters.filter.$or[0].$and.push({ [element]: { $neq: value } });
|
|
85
85
|
break;
|
|
86
86
|
case types_1.LogicOperator.greaterThan:
|
|
87
|
-
Filters.
|
|
87
|
+
Filters.filter.$or[0].$and.push({ [element]: { $gt: value } });
|
|
88
88
|
break;
|
|
89
89
|
case types_1.LogicOperator.greaterOrEqualThan:
|
|
90
|
-
Filters.
|
|
90
|
+
Filters.filter.$or[0].$and.push({ [element]: { $gte: value } });
|
|
91
91
|
break;
|
|
92
92
|
case types_1.LogicOperator.lessThan:
|
|
93
|
-
Filters.
|
|
93
|
+
Filters.filter.$or[0].$and.push({ [element]: { $lt: value } });
|
|
94
94
|
break;
|
|
95
95
|
case types_1.LogicOperator.lessOrEqualThan:
|
|
96
|
-
Filters.
|
|
96
|
+
Filters.filter.$or[0].$and.push({ [element]: { $lte: value } });
|
|
97
97
|
break;
|
|
98
98
|
case types_1.LogicOperator.in:
|
|
99
|
-
Filters.
|
|
99
|
+
Filters.filter.$or[0].$and.push({ [element]: { $in: value } });
|
|
100
100
|
break;
|
|
101
101
|
case types_1.LogicOperator.notIn:
|
|
102
|
-
Filters.
|
|
102
|
+
Filters.filter.$or[0].$and.push({
|
|
103
103
|
[element]: { $not: { $in: value } }
|
|
104
104
|
});
|
|
105
105
|
break;
|
|
106
106
|
case types_1.LogicOperator.exists:
|
|
107
|
-
Filters.
|
|
107
|
+
Filters.filter.$or[0].$and.push({ [element]: { $exists: true } });
|
|
108
108
|
break;
|
|
109
109
|
case types_1.LogicOperator.notExists:
|
|
110
|
-
Filters.
|
|
110
|
+
Filters.filter.$or[0].$and.push({ [element]: { $exists: false } });
|
|
111
111
|
break;
|
|
112
112
|
case types_1.LogicOperator.regexp:
|
|
113
|
-
Filters.
|
|
113
|
+
Filters.filter.$or[0].$and.push({ [element]: { $regex: value } });
|
|
114
114
|
break;
|
|
115
115
|
}
|
|
116
116
|
}
|
|
@@ -124,43 +124,43 @@ const getMongoWhere = ({ where }) => {
|
|
|
124
124
|
}
|
|
125
125
|
switch (operator) {
|
|
126
126
|
case types_1.LogicOperator.equals:
|
|
127
|
-
Filters.
|
|
127
|
+
Filters.filter.$or.push({ [element]: { $eq: value } });
|
|
128
128
|
break;
|
|
129
129
|
case types_1.LogicOperator.isNot:
|
|
130
|
-
Filters.
|
|
130
|
+
Filters.filter.$or.push({ [element]: { $neq: value } });
|
|
131
131
|
break;
|
|
132
132
|
case types_1.LogicOperator.greaterThan:
|
|
133
|
-
Filters.
|
|
133
|
+
Filters.filter.$or.push({ [element]: { $gt: value } });
|
|
134
134
|
break;
|
|
135
135
|
case types_1.LogicOperator.greaterOrEqualThan:
|
|
136
|
-
Filters.
|
|
136
|
+
Filters.filter.$or.push({ [element]: { $gte: value } });
|
|
137
137
|
break;
|
|
138
138
|
case types_1.LogicOperator.lessThan:
|
|
139
|
-
Filters.
|
|
139
|
+
Filters.filter.$or.push({ [element]: { $lt: value } });
|
|
140
140
|
break;
|
|
141
141
|
case types_1.LogicOperator.lessOrEqualThan:
|
|
142
|
-
Filters.
|
|
142
|
+
Filters.filter.$or.push({ [element]: { $lte: value } });
|
|
143
143
|
break;
|
|
144
144
|
case types_1.LogicOperator.in:
|
|
145
|
-
Filters.
|
|
145
|
+
Filters.filter.$or.push({ [element]: { $in: value } });
|
|
146
146
|
break;
|
|
147
147
|
case types_1.LogicOperator.notIn:
|
|
148
|
-
Filters.
|
|
148
|
+
Filters.filter.$or.push({
|
|
149
149
|
[element]: { $not: { $in: value } }
|
|
150
150
|
});
|
|
151
151
|
break;
|
|
152
152
|
case types_1.LogicOperator.exists:
|
|
153
|
-
Filters.
|
|
153
|
+
Filters.filter.$or.push({ [element]: { $exists: true } });
|
|
154
154
|
break;
|
|
155
155
|
case types_1.LogicOperator.notExists:
|
|
156
|
-
Filters.
|
|
156
|
+
Filters.filter.$or.push({ [element]: { $exists: false } });
|
|
157
157
|
break;
|
|
158
158
|
case types_1.LogicOperator.regexp:
|
|
159
|
-
Filters.
|
|
159
|
+
Filters.filter.$or.push({ [element]: { $regex: value } });
|
|
160
160
|
break;
|
|
161
161
|
}
|
|
162
162
|
}
|
|
163
|
-
const filtered = (0, clearEmpties_1.clearEmpties)(Filters.
|
|
163
|
+
const filtered = (0, clearEmpties_1.clearEmpties)(Filters.filter);
|
|
164
164
|
return filtered;
|
|
165
165
|
};
|
|
166
166
|
exports.getMongoWhere = getMongoWhere;
|
|
@@ -45,8 +45,10 @@ const advancedTestSuite = Model => {
|
|
|
45
45
|
const form = await Model.findFirst({
|
|
46
46
|
select: {
|
|
47
47
|
id: true,
|
|
48
|
+
test: true,
|
|
48
49
|
nestedTest: {
|
|
49
|
-
c: true
|
|
50
|
+
c: true,
|
|
51
|
+
a: true
|
|
50
52
|
}
|
|
51
53
|
},
|
|
52
54
|
where: {
|
|
@@ -76,6 +78,12 @@ const advancedTestSuite = Model => {
|
|
|
76
78
|
await insertTestData(Model);
|
|
77
79
|
const forms = await Model.findMany({
|
|
78
80
|
select: {
|
|
81
|
+
id: true,
|
|
82
|
+
test: true,
|
|
83
|
+
nestedTest: {
|
|
84
|
+
c: true,
|
|
85
|
+
a: true
|
|
86
|
+
},
|
|
79
87
|
created: true,
|
|
80
88
|
order: true
|
|
81
89
|
},
|
|
@@ -89,8 +97,12 @@ const advancedTestSuite = Model => {
|
|
|
89
97
|
await insertTestData(Model);
|
|
90
98
|
const forms = await Model.findMany({
|
|
91
99
|
select: {
|
|
92
|
-
|
|
93
|
-
|
|
100
|
+
id: true,
|
|
101
|
+
test: true,
|
|
102
|
+
nestedTest: {
|
|
103
|
+
c: true,
|
|
104
|
+
a: true
|
|
105
|
+
}
|
|
94
106
|
},
|
|
95
107
|
offset: 1,
|
|
96
108
|
limit: 1
|
|
@@ -160,12 +172,15 @@ const advancedTestSuite = Model => {
|
|
|
160
172
|
await insertTestData(Model);
|
|
161
173
|
const forms = await Model.findMany({
|
|
162
174
|
select: {
|
|
175
|
+
id: true,
|
|
163
176
|
test: true,
|
|
164
177
|
order: true,
|
|
165
178
|
nestedTest: {
|
|
179
|
+
c: true,
|
|
166
180
|
a: true,
|
|
167
181
|
b: {
|
|
168
|
-
c: true
|
|
182
|
+
c: true,
|
|
183
|
+
d: true
|
|
169
184
|
}
|
|
170
185
|
}
|
|
171
186
|
},
|
|
@@ -182,12 +197,15 @@ const advancedTestSuite = Model => {
|
|
|
182
197
|
await insertTestData(Model);
|
|
183
198
|
const forms = await Model.findMany({
|
|
184
199
|
select: {
|
|
200
|
+
id: true,
|
|
185
201
|
test: true,
|
|
186
202
|
order: true,
|
|
187
203
|
nestedTest: {
|
|
204
|
+
c: true,
|
|
188
205
|
a: true,
|
|
189
206
|
b: {
|
|
190
|
-
c: true
|
|
207
|
+
c: true,
|
|
208
|
+
d: true
|
|
191
209
|
}
|
|
192
210
|
}
|
|
193
211
|
},
|
|
@@ -203,8 +221,17 @@ const advancedTestSuite = Model => {
|
|
|
203
221
|
await insertTestData(Model);
|
|
204
222
|
const forms = await Model.findMany({
|
|
205
223
|
select: {
|
|
206
|
-
|
|
207
|
-
|
|
224
|
+
id: true,
|
|
225
|
+
test: true,
|
|
226
|
+
order: true,
|
|
227
|
+
nestedTest: {
|
|
228
|
+
c: true,
|
|
229
|
+
a: true,
|
|
230
|
+
b: {
|
|
231
|
+
c: true,
|
|
232
|
+
d: true
|
|
233
|
+
}
|
|
234
|
+
}
|
|
208
235
|
},
|
|
209
236
|
orderBy: [
|
|
210
237
|
{
|
|
@@ -225,25 +252,5 @@ const advancedTestSuite = Model => {
|
|
|
225
252
|
});
|
|
226
253
|
expect(forms[0].order).toBe(3);
|
|
227
254
|
});
|
|
228
|
-
it('Should get paginated data', async () => {
|
|
229
|
-
await insertTestData(Model);
|
|
230
|
-
const result = await Model.findMany({
|
|
231
|
-
paginated: {
|
|
232
|
-
page: 3,
|
|
233
|
-
perPage: 5
|
|
234
|
-
}
|
|
235
|
-
});
|
|
236
|
-
expect(Array.isArray(result.data)).toBe(true);
|
|
237
|
-
expect(result.data.length > 0).toBe(true);
|
|
238
|
-
expect(isNaN(result.total)).toBe(false);
|
|
239
|
-
expect(isNaN(result.perPage)).toBe(false);
|
|
240
|
-
expect(isNaN(result.currentPage)).toBe(false);
|
|
241
|
-
expect(isNaN(result.nextPage)).toBe(false);
|
|
242
|
-
expect(isNaN(result.firstPage)).toBe(false);
|
|
243
|
-
expect(isNaN(result.lastPage)).toBe(false);
|
|
244
|
-
expect(isNaN(result.prevPage)).toBe(false);
|
|
245
|
-
expect(isNaN(result.from)).toBe(false);
|
|
246
|
-
expect(isNaN(result.to)).toBe(false);
|
|
247
|
-
});
|
|
248
255
|
};
|
|
249
256
|
exports.advancedTestSuite = advancedTestSuite;
|
|
@@ -15,6 +15,15 @@ const basicTestSuite = Repo => {
|
|
|
15
15
|
expect(a.name).toBe('myGoat');
|
|
16
16
|
expect(0).toBe(0);
|
|
17
17
|
});
|
|
18
|
+
test('insert - Should insert data with customId', async () => {
|
|
19
|
+
const a = await Repository.insert({
|
|
20
|
+
id: '631ce4304f9183f61ffb613a',
|
|
21
|
+
name: 'myGoat',
|
|
22
|
+
age: 13
|
|
23
|
+
});
|
|
24
|
+
expect(typeof a.id).toBe('string');
|
|
25
|
+
expect(a.id).toBe('631ce4304f9183f61ffb613a');
|
|
26
|
+
});
|
|
18
27
|
it('insertMany - Should insert Multiple elements', async () => {
|
|
19
28
|
const insertedFlock = await Repository.insertMany(flock_1.flock);
|
|
20
29
|
expect(insertedFlock[0].name).toBe('Goatee');
|
|
@@ -118,12 +127,13 @@ const basicTestSuite = Repo => {
|
|
|
118
127
|
name: 'Goatee'
|
|
119
128
|
},
|
|
120
129
|
select: {
|
|
121
|
-
name: true
|
|
130
|
+
name: true,
|
|
131
|
+
age: true,
|
|
122
132
|
}
|
|
123
133
|
});
|
|
124
134
|
expect(Array.isArray(storedGoats)).toBe(false);
|
|
125
135
|
expect(storedGoats.name).toBe('Goatee');
|
|
126
|
-
expect(storedGoats).not.toHaveProperty('
|
|
136
|
+
expect(storedGoats).not.toHaveProperty('id');
|
|
127
137
|
});
|
|
128
138
|
test('requireFirst - Should fail if not found', async () => {
|
|
129
139
|
const insertedUser = await Repository.insert({
|
|
@@ -521,7 +521,6 @@ const relationsTestSuite = (UserRepo, BelongsToModelF, ManyToManyModelF) => {
|
|
|
521
521
|
}
|
|
522
522
|
}
|
|
523
523
|
});
|
|
524
|
-
console.log(searchUserWithRelation[0]);
|
|
525
524
|
expect(Array.isArray(searchUserWithRelation[0].roles)).toBe(true);
|
|
526
525
|
expect(searchUserWithRelation[0].roles.length > 0).toBe(true);
|
|
527
526
|
expect(typeof searchUserWithRelation[0].roles[0].name).toBe('string');
|