sails-mongo-cloud
Version:
Mongo DB adapter for Sails.js/Waterline. Forked from sails-mongo and updated with Mongodb Driver v4.9.1
73 lines (50 loc) • 2.72 kB
JavaScript
module.exports = {
friendlyName: 'Count (records)',
description: 'Return the count of the records matched by the query.',
inputs: {
query: require('../constants/query.input'),
connection: require('../constants/connection.input'),
dryOrm: require('../constants/dry-orm.input'),
},
exits: {
success: {
outputFriendlyName: 'Total (# of records)',
outputDescription: 'The number of matching records.',
outputExample: 59
},
},
fn: function (inputs, exits) {
// Dependencies
var _ = require('@sailshq/lodash');
var buildMongoWhereClause = require('./private/build-mongo-where-clause');
// Local var for the stage 3 query, for easier access.
var s3q = inputs.query;
if (s3q.meta && s3q.meta.logMongoS3Qs) {
console.log('* * * * * *\nADAPTER (COUNT RECORDS):',require('util').inspect(s3q,{depth:5}),'\n');
}
// Local var for the `tableName`, for clarity.
var tableName = s3q.using;
// Grab the model definition
var WLModel = _.find(inputs.dryOrm.models, {tableName: tableName});
if (!WLModel) {
return exits.error(new Error('No model with that tableName (`'+tableName+'`) has been registered with this adapter. Were any unexpected modifications made to the stage 3 query? Could the adapter\'s internal state have been corrupted? (This error is usually due to a bug in this adapter\'s implementation.)'));
}//-•
// ┌┬┐┌─┐┌┐┌┌─┐┌─┐┬┌─┐┬ ┬ ╔═╗╦═╗╦╔╦╗╔═╗╦═╗╦╔═╗
// ││││ │││││ ┬│ ││├┤ └┬┘ ║ ╠╦╝║ ║ ║╣ ╠╦╝║╠═╣
// ┴ ┴└─┘┘└┘└─┘└─┘┴└ ┴ ╚═╝╩╚═╩ ╩ ╚═╝╩╚═╩╩ ╩
// Build a Mongo-style WHERE from the `where` clause.
var mongoWhere;
try {
mongoWhere = buildMongoWhereClause(s3q.criteria.where, WLModel, s3q.meta);
} catch (e) { return exits.error(e); }
// ╔═╗╔═╗╔╦╗╔╦╗╦ ╦╔╗╔╦╔═╗╔═╗╔╦╗╔═╗ ┬ ┬┬┌┬┐┬ ┬ ┌┬┐┌┐
// ║ ║ ║║║║║║║║ ║║║║║║ ╠═╣ ║ ║╣ ││││ │ ├─┤ ││├┴┐
// ╚═╝╚═╝╩ ╩╩ ╩╚═╝╝╚╝╩╚═╝╩ ╩ ╩ ╚═╝ └┴┘┴ ┴ ┴ ┴ ─┴┘└─┘
var db = inputs.connection;
var mongoCollection = db.collection(tableName);
mongoCollection.countDocuments(mongoWhere, function countCb(err, nativeResult) {
if (err) { return exits.error(err); }
return exits.success(nativeResult);
});
}
};