aggregate.js 2.61 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
/*jshint node:true*/
"use strict";

var queryHelpers = require("./query"),

	mongodb = require("mongodb"),
	ObjectId = mongodb.ObjectID;


/*!
 * Misc helpers
 */


/* Create an aggregate pipeline from the base pipeline, the request query if
   any, and contextual additions */
function createPipeline(req, pipeline, additions) {
	pipeline = pipeline.slice(0);

	if (req && req.query["query"]) {
		pipeline.push({ $match: queryHelpers.create(req.query["query"]) });
	}

	for (var i = 0, len = additions.length; i < len; i++) {
		pipeline.push(additions[i]);
	}

	return pipeline;
}



/*!
 * Aggregate collection helpers
 */


function aggregateCollCount(req, cb) {
	var model = req.mongoose.model;
	var pipeline = req.mongoose.pipeline;

	var args = createPipeline(req, pipeline, [
		{ $group: { _id: 0, count: { $sum: 1 } } }
	]);

	args.push(function(err, result) {
		if (err) {
			cb(err);
		} else {
			cb(null, result.length ? result[0].count : 0);
		}
	});

	model.aggregate.apply(model, args);
}


function aggregateCollList(req, offset, limit, cb) {
	var model = req.mongoose.model;
	var pipeline = req.mongoose.pipeline;

	var additions = [{ $skip: offset }];

	if (limit > 0) {
		additions.push({ $limit: limit });
	}

	var args = createPipeline(req, pipeline, additions);

	args.push(function(err, items) {
		if (err) {
			cb(err);
		} else {
			cb(null, items);
		}
	});
	model.aggregate.apply(model, args);
}



/*!
 * Aggregated document helpers
 */


function aggregateDocHook(req, next) {
	var oid, match;

	var id = req.params.projectedId;
	var pipeline = req.mongoose.pipeline;
	var model = req.mongoose.model;

	try {
		oid = new ObjectId(id);
		match = { $or: [{ "_id": id }, { "_id": oid }] };
	} catch(e) {
		// Invalid ObjectID
		match = { "_id": id };
	}

	var args = createPipeline(null, pipeline, [
		{ $match: match },
		{ $limit: 1 }
	]);

	args.push(function(err, result) {
		if (err) {
			next(err);
		} else if (result.length) {
			req.mongoose.item = result[0];
			next();
		} else {
			next();
		}
	});

	model.aggregate.apply(model, args);
}


function aggregateDocGet(req, cb) {
	if (!("item" in req.mongoose)) {
		return cb.notFound();
	}

	cb(null, req.mongoose.item);
}



/*!
 * Aggregate resource definition helper
 */


function aggregateResource(name, Model, pipeline) {
	var collResource = this.sub(name)
		.hook(function aggregateHook(req, next) {
			req.mongoose = { model: Model, pipeline: pipeline };
			next();
		})
		.count(aggregateCollCount)
		.list(aggregateCollList);

	collResource.sub(":projectedId")
		.hook(aggregateDocHook)
		.get(aggregateDocGet);

	return collResource;
}

module.exports = aggregateResource;