adminmate-express-mongoose 1.3.3 → 1.3.4
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 +41 -28
- package/package.json +2 -2
- package/src/controllers/chart-pie.js +78 -56
- package/src/controllers/chart-ranking.js +99 -78
- package/src/controllers/chart-time.js +143 -127
- package/src/controllers/chart-value.js +104 -69
- package/src/controllers/model-autocomplete.js +52 -47
- package/src/controllers/model-deletesome.js +21 -18
- package/src/controllers/model-getall.js +109 -96
- package/src/controllers/model-getin.js +16 -12
- package/src/controllers/model-getone.js +41 -38
- package/src/controllers/model-getrefs.js +54 -50
- package/src/controllers/model-postone.js +25 -22
- package/src/controllers/model-putone.js +43 -38
- package/src/controllers/model-query.js +53 -49
- package/src/helpers/functions.js +443 -420
package/index.js
CHANGED
|
@@ -1,23 +1,39 @@
|
|
|
1
|
-
const {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
1
|
+
const Adminmate = ({ projectId, secretKey, authKey, masterPassword, models, charts, authorizedIps, devMode = false, testMode = false }) => {
|
|
2
|
+
const _conf = {};
|
|
3
|
+
_conf.projectId = projectId;
|
|
4
|
+
_conf.secretKey = secretKey;
|
|
5
|
+
_conf.authKey = authKey;
|
|
6
|
+
_conf.masterPassword = masterPassword;
|
|
7
|
+
_conf.models = models || [];
|
|
8
|
+
_conf.charts = charts || [];
|
|
9
|
+
_conf.authorizedIps = authorizedIps || null;
|
|
10
|
+
_conf.devMode = devMode;
|
|
11
|
+
_conf.testMode = testMode;
|
|
12
|
+
|
|
13
|
+
const amCore = require(_conf.devMode ? '../adminmate-express-core' : 'adminmate-express-core');
|
|
14
|
+
|
|
15
|
+
// Helpers
|
|
16
|
+
const fnHelper = require('./src/helpers/functions')(_conf);
|
|
17
|
+
|
|
18
|
+
// CRUD
|
|
19
|
+
const getAll = require('./src/controllers/model-getall')(_conf);
|
|
20
|
+
const getIn = require('./src/controllers/model-getin')(_conf);
|
|
21
|
+
const getOne = require('./src/controllers/model-getone')(_conf);
|
|
22
|
+
const getRefs = require('./src/controllers/model-getrefs')(_conf);
|
|
23
|
+
const postOne = require('./src/controllers/model-postone')(_conf);
|
|
24
|
+
const putOne = require('./src/controllers/model-putone')(_conf);
|
|
25
|
+
const deleteSome = require('./src/controllers/model-deletesome')(_conf);
|
|
26
|
+
const getAutocomplete = require('./src/controllers/model-autocomplete')(_conf);
|
|
27
|
+
const customQuery = require('./src/controllers/model-query')(_conf);
|
|
28
|
+
|
|
18
29
|
const api = {
|
|
19
30
|
// App config
|
|
20
31
|
getAppConfig: fnHelper.getAppConfig,
|
|
32
|
+
getAvailableFeatures: () => {
|
|
33
|
+
return {
|
|
34
|
+
charts: ['single_value', 'pie', 'bar', 'line', 'objective', 'ranking']
|
|
35
|
+
};
|
|
36
|
+
},
|
|
21
37
|
|
|
22
38
|
// General
|
|
23
39
|
getModelProperties: fnHelper.getModelProperties,
|
|
@@ -38,19 +54,16 @@ const Adminmate = ({ projectId, secretKey, authKey, masterPassword, models, char
|
|
|
38
54
|
modelCustomQuery: customQuery
|
|
39
55
|
};
|
|
40
56
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
charts,
|
|
48
|
-
authorizedIps,
|
|
57
|
+
if (testMode === true) {
|
|
58
|
+
return api;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return amCore.init({
|
|
62
|
+
config: _conf,
|
|
49
63
|
api
|
|
50
64
|
});
|
|
51
65
|
};
|
|
52
66
|
|
|
53
67
|
module.exports = {
|
|
54
|
-
init: Adminmate
|
|
55
|
-
|
|
56
|
-
};
|
|
68
|
+
init: Adminmate
|
|
69
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "adminmate-express-mongoose",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.4",
|
|
4
4
|
"description": "Adminmate Express/Mongoose connector",
|
|
5
5
|
"author": "Marc Delalonde",
|
|
6
6
|
"homepage": "http://adminmate.io",
|
|
@@ -23,8 +23,8 @@
|
|
|
23
23
|
"url": "https://github.com/Adminmate/adminmate-express-mongoose.git"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
+
"adminmate-express-core": "~1.2.1",
|
|
26
27
|
"joi": "^17.6.0",
|
|
27
|
-
"adminmate-express-core": "~1.2.0",
|
|
28
28
|
"lodash": "^4.17.21",
|
|
29
29
|
"moment": "^2.29.1",
|
|
30
30
|
"mongoose": "~5.9.7",
|
|
@@ -1,64 +1,86 @@
|
|
|
1
1
|
const Joi = require('joi');
|
|
2
2
|
|
|
3
|
-
module.exports =
|
|
4
|
-
const
|
|
5
|
-
type: Joi.string().required(),
|
|
6
|
-
model: Joi.string().required(),
|
|
7
|
-
operation: Joi.string().required(),
|
|
8
|
-
group_by: Joi.string().required(),
|
|
9
|
-
field: Joi.alternatives().conditional('operation', {
|
|
10
|
-
not: 'count',
|
|
11
|
-
then: Joi.string().required(),
|
|
12
|
-
otherwise: Joi.string().optional()
|
|
13
|
-
}),
|
|
14
|
-
limit: Joi.number().optional()
|
|
15
|
-
});
|
|
3
|
+
module.exports = _conf => {
|
|
4
|
+
const fnHelper = require('../helpers/functions')(_conf);
|
|
16
5
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
6
|
+
const chartPie = async (currentModel, data) => {
|
|
7
|
+
const paramsSchema = Joi.object({
|
|
8
|
+
type: Joi.string().required(),
|
|
9
|
+
model: Joi.string().required(),
|
|
10
|
+
operation: Joi.string().required(),
|
|
11
|
+
group_by: Joi.string().required(),
|
|
12
|
+
field: Joi.alternatives().conditional('operation', {
|
|
13
|
+
not: 'count',
|
|
14
|
+
then: Joi.string().required(),
|
|
15
|
+
otherwise: Joi.string().optional()
|
|
16
|
+
}),
|
|
17
|
+
limit: Joi.number().optional(),
|
|
18
|
+
filters: Joi.object({
|
|
19
|
+
operator: Joi.string().valid('and', 'or').required(),
|
|
20
|
+
list: Joi.array().required()
|
|
21
|
+
})
|
|
22
|
+
});
|
|
25
23
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
24
|
+
// Validate params
|
|
25
|
+
const { error } = paramsSchema.validate(data);
|
|
26
|
+
if (error) {
|
|
27
|
+
return {
|
|
28
|
+
success: false,
|
|
29
|
+
message: error.details[0].message
|
|
30
|
+
};
|
|
31
|
+
}
|
|
30
32
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
.
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
33
|
+
let _value = 1;
|
|
34
|
+
if (data.field && ['sum', 'avg'].includes(data.operation)) {
|
|
35
|
+
_value = `$${data.field}`;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Filters
|
|
39
|
+
let findParams = {};
|
|
40
|
+
if (data.filters) {
|
|
41
|
+
const filtersQuery = fnHelper.constructQuery(data.filters);
|
|
42
|
+
if (filtersQuery) {
|
|
43
|
+
findParams = filtersQuery;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
try {
|
|
48
|
+
const repartitionData = await currentModel
|
|
49
|
+
.aggregate([
|
|
50
|
+
{
|
|
51
|
+
$match: findParams
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
$group: {
|
|
55
|
+
_id: `$${data.group_by}`,
|
|
56
|
+
count: data.operation === 'avg' ? { $avg: _value } : { $sum: _value },
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
$project: {
|
|
61
|
+
key: '$_id',
|
|
62
|
+
value: '$count',
|
|
63
|
+
_id: false
|
|
64
|
+
}
|
|
45
65
|
}
|
|
66
|
+
])
|
|
67
|
+
.sort({ key: 1 });
|
|
68
|
+
|
|
69
|
+
return {
|
|
70
|
+
success: true,
|
|
71
|
+
data: {
|
|
72
|
+
config: null,
|
|
73
|
+
data: repartitionData
|
|
46
74
|
}
|
|
47
|
-
|
|
48
|
-
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
catch(e) {
|
|
78
|
+
return {
|
|
79
|
+
success: false,
|
|
80
|
+
message: e.message
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
};
|
|
49
84
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
data: {
|
|
53
|
-
config: null,
|
|
54
|
-
data: repartitionData
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
catch(e) {
|
|
59
|
-
return {
|
|
60
|
-
success: false,
|
|
61
|
-
message: e.message
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
};
|
|
85
|
+
return chartPie;
|
|
86
|
+
};
|
|
@@ -1,89 +1,110 @@
|
|
|
1
1
|
const Joi = require('joi');
|
|
2
|
-
const fnHelper = require('../helpers/functions');
|
|
3
2
|
|
|
4
|
-
module.exports =
|
|
5
|
-
const
|
|
6
|
-
type: Joi.string().required(),
|
|
7
|
-
model: Joi.string().required(),
|
|
8
|
-
field: Joi.string().required(),
|
|
9
|
-
relationship_model: Joi.string().required(),
|
|
10
|
-
relationship_model_ref_field: Joi.string().required(),
|
|
11
|
-
relationship_operation: Joi.string().required(),
|
|
12
|
-
relationship_field: Joi.alternatives().conditional('relationship_operation', {
|
|
13
|
-
not: 'count',
|
|
14
|
-
then: Joi.string().required(),
|
|
15
|
-
otherwise: Joi.string()
|
|
16
|
-
}),
|
|
17
|
-
limit: Joi.number().optional(),
|
|
18
|
-
});
|
|
3
|
+
module.exports = _conf => {
|
|
4
|
+
const fnHelper = require('../helpers/functions')(_conf);
|
|
19
5
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
6
|
+
const chartRanking = async (currentModel, data) => {
|
|
7
|
+
const paramsSchema = Joi.object({
|
|
8
|
+
type: Joi.string().required(),
|
|
9
|
+
model: Joi.string().required(),
|
|
10
|
+
field: Joi.string().required(),
|
|
11
|
+
relationship_model: Joi.string().required(),
|
|
12
|
+
relationship_model_ref_field: Joi.string().required(),
|
|
13
|
+
relationship_operation: Joi.string().required(),
|
|
14
|
+
relationship_field: Joi.alternatives().conditional('relationship_operation', {
|
|
15
|
+
not: 'count',
|
|
16
|
+
then: Joi.string().required(),
|
|
17
|
+
otherwise: Joi.string()
|
|
18
|
+
}),
|
|
19
|
+
limit: Joi.number().optional(),
|
|
20
|
+
filters: Joi.object({
|
|
21
|
+
operator: Joi.string().valid('and', 'or').required(),
|
|
22
|
+
list: Joi.array().required()
|
|
23
|
+
})
|
|
24
|
+
});
|
|
28
25
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
26
|
+
// Validate params
|
|
27
|
+
const { error } = paramsSchema.validate(data);
|
|
28
|
+
if (error) {
|
|
29
|
+
return {
|
|
30
|
+
success: false,
|
|
31
|
+
message: error.details[0].message
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
34
|
|
|
35
|
-
|
|
36
|
-
|
|
35
|
+
// Get relationship model
|
|
36
|
+
const relationshipModel = fnHelper.getModelObject(data.relationship_model);
|
|
37
|
+
if (!relationshipModel) {
|
|
38
|
+
return res.status(403).json({ message: 'Invalid request' });
|
|
39
|
+
}
|
|
37
40
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
_value = `$${data.relationship_field}`;
|
|
41
|
-
}
|
|
41
|
+
// Default limit
|
|
42
|
+
let limit = data.limit || 10;
|
|
42
43
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
.
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
44
|
+
let _value = 1;
|
|
45
|
+
if (data.relationship_field && ['sum', 'avg'].includes(data.relationship_operation)) {
|
|
46
|
+
_value = `$${data.relationship_field}`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Filters
|
|
50
|
+
let findParams = {};
|
|
51
|
+
if (data.filters) {
|
|
52
|
+
const filtersQuery = fnHelper.constructQuery(data.filters);
|
|
53
|
+
if (filtersQuery) {
|
|
54
|
+
findParams = filtersQuery;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
try {
|
|
59
|
+
const repartitionData = await relationshipModel
|
|
60
|
+
.aggregate([
|
|
61
|
+
{
|
|
62
|
+
$match: findParams
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
$group: {
|
|
66
|
+
_id: `$${data.relationship_model_ref_field}`,
|
|
67
|
+
count: data.operation === 'avg' ? { $avg: _value } : { $sum: _value },
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
$project: {
|
|
72
|
+
key: '$_id',
|
|
73
|
+
value: '$count',
|
|
74
|
+
_id: false
|
|
75
|
+
}
|
|
57
76
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
.sort({ value: -1 });
|
|
77
|
+
])
|
|
78
|
+
.limit(limit)
|
|
79
|
+
.sort({ value: -1 });
|
|
62
80
|
|
|
63
|
-
|
|
64
|
-
|
|
81
|
+
const parentIds = repartitionData.map(d => d.key);
|
|
82
|
+
const parentData = await currentModel.find({ _id: parentIds }).select(data.field).lean();
|
|
65
83
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
84
|
+
repartitionData.forEach(d => {
|
|
85
|
+
d.item_model = data.model;
|
|
86
|
+
d.item_id = d.key;
|
|
87
|
+
const parent = parentData.find(p => p._id.toString() === d.key.toString());
|
|
88
|
+
if (parent) {
|
|
89
|
+
d.key = parent[data.field];
|
|
90
|
+
}
|
|
91
|
+
});
|
|
74
92
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
};
|
|
93
|
+
return {
|
|
94
|
+
success: true,
|
|
95
|
+
data: {
|
|
96
|
+
config: null,
|
|
97
|
+
data: repartitionData
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
catch(e) {
|
|
102
|
+
return {
|
|
103
|
+
success: false,
|
|
104
|
+
message: e.message
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
return chartRanking;
|
|
110
|
+
};
|