adminmate-express-mongoose 1.2.7 → 1.3.1
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/index.js
CHANGED
|
@@ -7,6 +7,7 @@ const fnHelper = require('./src/helpers/functions');
|
|
|
7
7
|
const { getAll } = require('./src/controllers/model-getall');
|
|
8
8
|
const { getIn } = require('./src/controllers/model-getin');
|
|
9
9
|
const { getOne } = require('./src/controllers/model-getone');
|
|
10
|
+
const { getRefs } = require('./src/controllers/model-getrefs');
|
|
10
11
|
const { postOne } = require('./src/controllers/model-postone');
|
|
11
12
|
const { putOne } = require('./src/controllers/model-putone');
|
|
12
13
|
const { deleteSome } = require('./src/controllers/model-deletesome');
|
|
@@ -29,6 +30,7 @@ const Adminmate = ({ projectId, secretKey, authKey, masterPassword, models, char
|
|
|
29
30
|
modelGetAll: getAll,
|
|
30
31
|
modelGetIn: getIn,
|
|
31
32
|
modelGetOne: getOne,
|
|
33
|
+
modelGetRefs: getRefs,
|
|
32
34
|
modelPostOne: postOne,
|
|
33
35
|
modelPutOne: putOne,
|
|
34
36
|
modelDeleteSome: deleteSome,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "adminmate-express-mongoose",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.1",
|
|
4
4
|
"description": "Adminmate Express/Mongoose connector",
|
|
5
5
|
"author": "Marc Delalonde",
|
|
6
6
|
"homepage": "http://adminmate.io",
|
|
@@ -23,11 +23,10 @@
|
|
|
23
23
|
"url": "https://github.com/Adminmate/adminmate-express-mongoose.git"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"adminmate-express-core": "^1.
|
|
26
|
+
"adminmate-express-core": "^1.2.0",
|
|
27
27
|
"lodash": "^4.17.21",
|
|
28
28
|
"moment": "^2.29.1",
|
|
29
|
-
"mongoose": "
|
|
30
|
-
"mongoose-legacy-pluralize": "^1.0.2",
|
|
29
|
+
"mongoose": "~5.9.7",
|
|
31
30
|
"serialize-error": "^7.0.1"
|
|
32
31
|
},
|
|
33
32
|
"devDependencies": {
|
|
@@ -1,9 +1,56 @@
|
|
|
1
|
+
const fnHelper = require('../helpers/functions');
|
|
2
|
+
|
|
1
3
|
module.exports = async (currentModel, data) => {
|
|
4
|
+
// Get relationship model
|
|
5
|
+
const relationshipModel = fnHelper.getModelObject(data.relationship_model);
|
|
6
|
+
if (!relationshipModel) {
|
|
7
|
+
return res.status(403).json({ message: 'Invalid request' });
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// Default limit
|
|
11
|
+
let limit = data.limit || 10;
|
|
12
|
+
|
|
13
|
+
let _value = 1;
|
|
14
|
+
if (data.relationship_field && ['sum', 'avg'].includes(data.relationship_operation)) {
|
|
15
|
+
_value = `$${data.relationship_field}`;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const repartitionData = await relationshipModel
|
|
19
|
+
.aggregate([
|
|
20
|
+
{
|
|
21
|
+
$group: {
|
|
22
|
+
_id: `$${data.relationship_model_ref_field}`,
|
|
23
|
+
count: data.operation === 'avg' ? { $avg: _value } : { $sum: _value },
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
$project: {
|
|
28
|
+
key: '$_id',
|
|
29
|
+
value: '$count',
|
|
30
|
+
_id: false
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
])
|
|
34
|
+
.limit(limit)
|
|
35
|
+
.sort({ value: -1 });
|
|
36
|
+
|
|
37
|
+
const parentIds = repartitionData.map(d => d.key);
|
|
38
|
+
const parentData = await currentModel.find({ _id: parentIds }).select(data.field).lean();
|
|
39
|
+
|
|
40
|
+
repartitionData.forEach(d => {
|
|
41
|
+
d.item_model = data.model;
|
|
42
|
+
d.item_id = d.key;
|
|
43
|
+
const parent = parentData.find(p => p._id.toString() === d.key.toString());
|
|
44
|
+
if (parent) {
|
|
45
|
+
d.key = parent[data.field];
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
|
|
2
49
|
return {
|
|
3
|
-
success:
|
|
50
|
+
success: true,
|
|
4
51
|
data: {
|
|
5
52
|
config: null,
|
|
6
|
-
data:
|
|
53
|
+
data: repartitionData
|
|
7
54
|
}
|
|
8
55
|
};
|
|
9
56
|
};
|
|
@@ -8,7 +8,7 @@ module.exports.getAll = async (req, res) => {
|
|
|
8
8
|
const filters = req.query.filters;
|
|
9
9
|
const fieldsToFetch = req.headers['am-model-fields'] || [];
|
|
10
10
|
const refFields = req.headers['am-ref-fields'] || {};
|
|
11
|
-
const fieldsToSearchIn = req.query.
|
|
11
|
+
const fieldsToSearchIn = req.query.search_in_fields || [];
|
|
12
12
|
const page = parseInt(req.query.page || 1);
|
|
13
13
|
const nbItemPerPage = 10;
|
|
14
14
|
const defaultOrdering = [ ['_id', 'DESC'] ];
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
const fnHelper = require('../helpers/functions');
|
|
2
|
+
|
|
3
|
+
module.exports.getRefs = async (req, res) => {
|
|
4
|
+
const modelName = req.params.model;
|
|
5
|
+
const ids = req.query.ids;
|
|
6
|
+
const refFields = req.headers['am-ref-fields'] || {};
|
|
7
|
+
const nbItemPerPage = 20;
|
|
8
|
+
|
|
9
|
+
if (!ids) {
|
|
10
|
+
return res.status(403).json({ message: 'Missing parameter ids' });
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const fieldsToFetchSafe = refFields[modelName];
|
|
14
|
+
|
|
15
|
+
// If no ref fields, return default response
|
|
16
|
+
if (!fieldsToFetchSafe) {
|
|
17
|
+
return res.json({
|
|
18
|
+
data: ids.map(id => ({ value: id, label: id }))
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const currentModel = fnHelper.getModelObject(modelName);
|
|
23
|
+
if (!currentModel) {
|
|
24
|
+
return res.status(403).json({ message: 'Invalid request' });
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Find parameters
|
|
28
|
+
const findParams = { _id: ids };
|
|
29
|
+
|
|
30
|
+
const data = await currentModel
|
|
31
|
+
.find(findParams)
|
|
32
|
+
.select(fieldsToFetchSafe)
|
|
33
|
+
.limit(nbItemPerPage)
|
|
34
|
+
.lean()
|
|
35
|
+
.catch(e => {
|
|
36
|
+
res.status(403).json({ message: e.message });
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
if (!data) {
|
|
40
|
+
return res.status(403).json();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Format the response
|
|
44
|
+
const formattedData = ids.map(_id => {
|
|
45
|
+
const match = data.find(d => d._id.toString() === _id.toString());
|
|
46
|
+
const label = match ? fnHelper.fieldsToValues(fieldsToFetchSafe, match) : _id;
|
|
47
|
+
return { value: _id, label };
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
res.json({
|
|
51
|
+
data: formattedData
|
|
52
|
+
});
|
|
53
|
+
};
|
package/src/helpers/functions.js
CHANGED
|
@@ -119,6 +119,9 @@ const cleanString = string => {
|
|
|
119
119
|
module.exports.cleanString = cleanString;
|
|
120
120
|
|
|
121
121
|
const queryRule = rule => {
|
|
122
|
+
if (rule.type === 'group') {
|
|
123
|
+
return queryRuleSet(rule);
|
|
124
|
+
}
|
|
122
125
|
let q = {};
|
|
123
126
|
if (rule.operator === 'is') {
|
|
124
127
|
q[rule.field] = { $eq: rule.value };
|
|
@@ -224,6 +227,12 @@ module.exports.constructQuery = jsonQuery => {
|
|
|
224
227
|
return null;
|
|
225
228
|
};
|
|
226
229
|
|
|
230
|
+
module.exports.fieldsToValues = (string, values) => {
|
|
231
|
+
return string.replace(/[a-z._]+/gi, word => {
|
|
232
|
+
return _.get(values, word);
|
|
233
|
+
});
|
|
234
|
+
};
|
|
235
|
+
|
|
227
236
|
module.exports.refFields = (item, fieldsToPopulate) => {
|
|
228
237
|
const attributes = Object.keys(item);
|
|
229
238
|
attributes.forEach(attr => {
|
package/docker-compose.yml
DELETED