Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions backend/actions/Model/aggregate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
'use strict';

const Archetype = require('archetype');
const { EJSON } = require('mongoose').mongo.BSON;
const authorize = require('../../authorize');

const AggregateParams = new Archetype({
model: {
$type: 'string',
$required: true
},
pipeline: {
$type: Archetype.Any,
$required: true
},
limit: {
$type: 'number',
$default: 20
},
roles: {
$type: ['string']
}
}).compile('AggregateParams');

module.exports = ({ db }) => async function aggregate(params) {
params = new AggregateParams(params);
const { model, roles } = params;
await authorize('Model.aggregate', roles);

const Model = db.models[model];
if (Model == null) {
throw new Error(`Model ${model} not found`);
}

if (!Array.isArray(params.pipeline)) {
throw new Error('`pipeline` must be an array');
}

let pipeline;
try {
pipeline = EJSON.deserialize(params.pipeline);
} catch (err) {
throw new Error(`Invalid pipeline (EJSON): ${err.message}`);
}

if (!Array.isArray(pipeline)) {
throw new Error('`pipeline` must be an array');
}

pipeline = pipeline.map((stage, index) => {
if (stage == null || Array.isArray(stage) || typeof stage !== 'object') {
throw new Error(`Invalid stage at index ${index}`);
}
return stage;
});

const limit = Math.max(1, Math.min(200, Math.floor(params.limit ?? 20)));
pipeline.push({ $limit: limit });

Comment thread
vkarpov15 marked this conversation as resolved.
const docs = await Model.aggregate(pipeline).exec();
return { docs };
};
Comment thread
IslandRhythms marked this conversation as resolved.
1 change: 1 addition & 0 deletions backend/actions/Model/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

exports.addField = require('./addField');
exports.aggregate = require('./aggregate');
exports.analyzeSchema = require('./analyzeSchema');
exports.createChatMessage = require('./createChatMessage');
exports.createDocument = require('./createDocument');
Expand Down
1 change: 1 addition & 0 deletions backend/authorize.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const actionsToRequiredRoles = {
'Model.deleteDocuments': ['owner', 'admin', 'member'],
'Model.dropCollection': ['owner', 'admin'],
'Model.dropIndex': ['owner', 'admin'],
'Model.aggregate': ['owner', 'admin', 'member', 'readonly'],
'Model.executeDocumentScript': ['owner', 'admin', 'member'],
'Model.exportQueryResults': ['owner', 'admin', 'member', 'readonly'],
'Model.getDocument': ['owner', 'admin', 'member', 'readonly'],
Expand Down
301 changes: 301 additions & 0 deletions frontend/src/aggregation-builder/aggregation-builder.html

Large diffs are not rendered by default.

Loading
Loading