Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Look up the model
let Model = actionUtil.parseModel(req);
let countInResponse = sails.config.blueprints.countInResponse;
if (!Model.mapping) {
return fallback(req, res);
}
if (actionUtil.parsePk(req)) {
return require('./findOne')(req, res);
}
let populate = actionUtil.populateRequest(null, req);
let repository = req.getRepository(Model.Entity);
let criteria = actionUtil.parseCriteria(req);
let options = {
limit : actionUtil.parseLimit(req),
offset : actionUtil.parseSkip(req),
orderBy: actionUtil.parseSort(req)
};
if (populate && populate.length && populate[0] !== undefined) {
options.populate = populate;
}
let promises = [repository.find(criteria, options)];
if (countInResponse) {
promises.push(count(repository, criteria, options));
}
module.exports = function activeNodeData(request, response, next) {
sails.log.verbose(__filename + ':' + __line + ' [Policy.activeNodeData() called]');
// sails.config.kong_admin_url = request.headers['kong-admin-url'] || sails.config.kong_admin_url
var c = actionUtil.parseCriteria(request)
if(c.hasOwnProperty('or')){
c['and'] = [{node_id : request.node_id}]
}else{
c['node_id'] = request.node_id
}
request.query.where = JSON.stringify(c)
return next()
};
exports.makeConditionValidProperty = function(validIds, property, request) {
// Parse where criteria
var where = actionUtil.parseCriteria(request);
// Normalize valid id array
validIds = _.map(validIds, function(id) {
return parseInt(id, 10);
});
// Specified property is not yet in where query
if (!where[property]) {
where[property] = validIds;
} else { // We have id condition set so we need to check if that / those are allowed
// Normalize current ids
var currentIds = _.map((!_.isArray(where[property])) ? [where[property]] : where[property], function (id) {
return parseInt(id, 10);
});
// Remove not valid ids
exports.getCollection = function(request, extraCriteria, next) {
// Determine model name and used default criteria object
var model = actionUtil.parseModel(request);
var criteria = actionUtil.parseCriteria(request);
// Merge extra criteria to main criteria object
if (extraCriteria) {
criteria = _.merge(criteria, extraCriteria);
}
// Fetch data from database
model
.find(criteria)
.exec(function found(error, items) {
next(error, items);
});
};
module.exports = (req, res) => {
_.set(req.options, 'criteria.blacklist', ['fields', 'populate', 'limit', 'skip', 'page', 'sort']);
const fields = req.param('fields') ? req.param('fields').replace(/ /g, '').split(',') : [];
const populate = req.param('populate') ? req.param('populate').replace(/ /g, '').split(',') : [];
const Model = actionUtil.parseModel(req);
const where = actionUtil.parseCriteria(req);
const limit = actionUtil.parseLimit(req);
const skip = req.param('page') * limit || actionUtil.parseSkip(req);
const sort = actionUtil.parseSort(req);
const query = Model.find(null, fields.length > 0 ? {select: fields} : null).where(where).limit(limit).skip(skip).sort(sort);
const findQuery = _.reduce(_.intersection(populate, takeAlias(Model.associations)), populateAlias, query);
findQuery
.then(records => [records, {
root: {
criteria: where,
limit: limit,
start: skip,
end: skip + limit,
page: Math.floor(skip / limit)
}
}])
module.exports = function findRecords(req, res) {
let Model = actionUtil.parseModel(req);
let criteria = actionUtil.parseCriteria(req);
if (!Model) {
return res.badRequest('invalid_parameter');
}
let options = {
limit : actionUtil.parseLimit(req),
offset : actionUtil.parseSkip(req),
orderBy: actionUtil.parseSort(req)
};
count(req.getRepository(Model.Entity), criteria, options)
.then(count => res.ok({count}))
.catch(error => res.serverError('database_error', error));
};
count: function(request, response) {
var model = actionUtil.parseModel(request);
model
.count(actionUtil.parseCriteria(request))
.exec(function found(error, count) {
if (error) {
response.json(500, error);
} else {
response.json(200, {count: count});
}
});
},
count: function count(request, response) {
var Model = actionUtil.parseModel(request);
Model
.count(actionUtil.parseCriteria(request))
.exec(function found(error, count) {
if (error) {
response.negotiate(error);
} else {
response.ok({count: count});
}
})
;
}
};