1
0
mirror of https://github.com/mgerb/mywebsite synced 2026-01-12 02:42:48 +00:00

updated bunch of file paths and changed the way posts are loaded

This commit is contained in:
2016-01-05 12:28:04 -06:00
parent 4bb8cae81e
commit 6ab45fe935
13249 changed files with 317868 additions and 2101398 deletions

View File

@@ -1,12 +1,14 @@
/* eslint no-unused-vars: 1 */
/*!
* Module dependencies
*/
var Promise = require('./promise')
, util = require('util')
, utils = require('./utils')
, Query = require('./query')
, read = Query.prototype.read
var util = require('util');
var utils = require('./utils');
var PromiseProvider = require('./promise_provider');
var Query = require('./query');
var read = Query.prototype.read;
/**
* Aggregate constructor used for building aggregation pipelines.
@@ -31,6 +33,7 @@ var Promise = require('./promise')
*
* - The documents returned are plain javascript objects, not mongoose documents (since any shape of document can be returned).
* - Requires MongoDB >= 2.1
* - Mongoose does **not** cast pipeline stages. `new Aggregate({ $match: { _id: '00000000000000000000000a' } });` will not work unless `_id` is a string in the database. Use `new Aggregate({ $match: { _id: mongoose.Types.ObjectId('00000000000000000000000a') } });` instead.
*
* @see MongoDB http://docs.mongodb.org/manual/applications/aggregation/
* @see driver http://mongodb.github.com/node-mongodb-native/api-generated/collection.html#aggregate
@@ -38,7 +41,7 @@ var Promise = require('./promise')
* @api public
*/
function Aggregate () {
function Aggregate() {
this._pipeline = [];
this._model = undefined;
this.options = undefined;
@@ -55,13 +58,13 @@ function Aggregate () {
*
* @param {Model} model the model to which the aggregate is to be bound
* @return {Aggregate}
* @api private
* @api public
*/
Aggregate.prototype.bind = function (model) {
Aggregate.prototype.model = function(model) {
this._model = model;
return this;
}
};
/**
* Appends new operators to this aggregate pipeline
@@ -79,9 +82,8 @@ Aggregate.prototype.bind = function (model) {
* @api public
*/
Aggregate.prototype.append = function () {
var args = utils.args(arguments)
, arg;
Aggregate.prototype.append = function() {
var args = utils.args(arguments);
if (!args.every(isOperator)) {
throw new Error("Arguments must be aggregate pipeline operators");
@@ -90,7 +92,7 @@ Aggregate.prototype.append = function () {
this._pipeline = this._pipeline.concat(args);
return this;
}
};
/**
* Appends a new $project operator to this aggregate pipeline.
@@ -124,15 +126,15 @@ Aggregate.prototype.append = function () {
* @api public
*/
Aggregate.prototype.project = function (arg) {
Aggregate.prototype.project = function(arg) {
var fields = {};
if ('object' === typeof arg && !util.isArray(arg)) {
Object.keys(arg).forEach(function (field) {
Object.keys(arg).forEach(function(field) {
fields[field] = arg[field];
});
} else if (1 === arguments.length && 'string' === typeof arg) {
arg.split(/\s+/).forEach(function (field) {
arg.split(/\s+/).forEach(function(field) {
if (!field) return;
var include = '-' == field[0] ? 0 : 1;
if (include === 0) field = field.substring(1);
@@ -143,7 +145,7 @@ Aggregate.prototype.project = function (arg) {
}
return this.append({ $project: fields });
}
};
/**
* Appends a new custom $group operator to this aggregate pipeline.
@@ -232,7 +234,7 @@ Aggregate.prototype.project = function (arg) {
* @api public
*/
Aggregate.prototype.near = function (arg) {
Aggregate.prototype.near = function(arg) {
var op = {};
op.$geoNear = arg;
return this.append(op);
@@ -242,8 +244,8 @@ Aggregate.prototype.near = function (arg) {
* define methods
*/
'group match skip limit out'.split(' ').forEach(function ($operator) {
Aggregate.prototype[$operator] = function (arg) {
'group match skip limit out'.split(' ').forEach(function($operator) {
Aggregate.prototype[$operator] = function(arg) {
var op = {};
op['$' + $operator] = arg;
return this.append(op);
@@ -253,6 +255,9 @@ Aggregate.prototype.near = function (arg) {
/**
* Appends new custom $unwind operator(s) to this aggregate pipeline.
*
* Note that the `$unwind` operator requires the path name to start with '$'.
* Mongoose will prepend '$' if the specified field doesn't start '$'.
*
* ####Examples:
*
* aggregate.unwind("tags");
@@ -264,13 +269,47 @@ Aggregate.prototype.near = function (arg) {
* @api public
*/
Aggregate.prototype.unwind = function () {
Aggregate.prototype.unwind = function() {
var args = utils.args(arguments);
return this.append.apply(this, args.map(function (arg) {
return { $unwind: '$' + arg };
return this.append.apply(this, args.map(function(arg) {
return { $unwind: (arg && arg.charAt(0) === '$') ? arg : '$' + arg };
}));
}
};
/**
* Appends new custom $lookup operator(s) to this aggregate pipeline.
*
* ####Examples:
*
* aggregate.lookup({ from: 'users', localField: 'userId', foreignField: '_id', as: 'users' });
*
* @see $lookup https://docs.mongodb.org/manual/reference/operator/aggregation/lookup/#pipe._S_lookup
* @param {Object} options to $lookup as described in the above link
* @return {Aggregate}
* @api public
*/
Aggregate.prototype.lookup = function(options) {
return this.append({ $lookup: options });
};
/**
* Appends new custom $sample operator(s) to this aggregate pipeline.
*
* ####Examples:
*
* aggregate.sample(3); // Add a pipeline that picks 3 random documents
*
* @see $sample https://docs.mongodb.org/manual/reference/operator/aggregation/sample/#pipe._S_sample
* @param {Number} size number of random documents to pick
* @return {Aggregate}
* @api public
*/
Aggregate.prototype.sample = function(size) {
return this.append({ $sample: { size: size } });
};
/**
* Appends a new $sort operator to this aggregate pipeline.
@@ -291,18 +330,18 @@ Aggregate.prototype.unwind = function () {
* @api public
*/
Aggregate.prototype.sort = function (arg) {
Aggregate.prototype.sort = function(arg) {
// TODO refactor to reuse the query builder logic
var sort = {};
if ('Object' === arg.constructor.name) {
var desc = ['desc', 'descending', -1];
Object.keys(arg).forEach(function (field) {
Object.keys(arg).forEach(function(field) {
sort[field] = desc.indexOf(arg[field]) === -1 ? 1 : -1;
});
} else if (1 === arguments.length && 'string' == typeof arg) {
arg.split(/\s+/).forEach(function (field) {
arg.split(/\s+/).forEach(function(field) {
if (!field) return;
var ascend = '-' == field[0] ? -1 : 1;
if (ascend === -1) field = field.substring(1);
@@ -313,7 +352,7 @@ Aggregate.prototype.sort = function (arg) {
}
return this.append({ $sort: sort });
}
};
/**
* Sets the readPreference option for the aggregation query.
@@ -328,12 +367,58 @@ Aggregate.prototype.sort = function (arg) {
* @see driver http://mongodb.github.com/node-mongodb-native/driver-articles/anintroductionto1_1and2_2.html#read-preferences
*/
Aggregate.prototype.read = function (pref) {
Aggregate.prototype.read = function(pref) {
if (!this.options) this.options = {};
read.apply(this, arguments);
return this;
};
/**
* Execute the aggregation with explain
*
* ####Example:
*
* Model.aggregate(..).explain(callback)
*
* @param {Function} callback
* @return {Promise}
*/
Aggregate.prototype.explain = function(callback) {
var _this = this;
var Promise = PromiseProvider.get();
return new Promise.ES6(function(resolve, reject) {
if (!_this._pipeline.length) {
var err = new Error('Aggregate has empty pipeline');
if (callback) {
callback(err);
}
reject(err);
return;
}
prepareDiscriminatorPipeline(_this);
_this._model
.collection
.aggregate(_this._pipeline, _this.options || {})
.explain(function(error, result) {
if (error) {
if (callback) {
callback(error);
}
reject(error);
return;
}
if (callback) {
callback(null, result);
}
resolve(result);
});
});
};
/**
* Sets the allowDiskUse option for the aggregation query (ignored for < 2.6.0)
*
@@ -391,34 +476,68 @@ Aggregate.prototype.cursor = function(options) {
* @api public
*/
Aggregate.prototype.exec = function (callback) {
var promise = new Promise();
if (callback) {
promise.addBack(callback);
}
if (!this._pipeline.length) {
promise.error(new Error("Aggregate has empty pipeline"));
return promise;
}
Aggregate.prototype.exec = function(callback) {
if (!this._model) {
promise.error(new Error("Aggregate not bound to any Model"));
return promise;
throw new Error("Aggregate not bound to any Model");
}
prepareDiscriminatorPipeline(this);
var _this = this;
var Promise = PromiseProvider.get();
if (this.options && this.options.cursor) {
return this._model.collection.aggregate(this._pipeline, this.options || {});
if (this.options.cursor.async) {
return new Promise.ES6(function(resolve, reject) {
if (!_this._model.collection.buffer) {
process.nextTick(function() {
var cursor = _this._model.collection.
aggregate(_this._pipeline, _this.options || {});
resolve(cursor);
callback && callback(cursor);
});
return;
} else {
_this._model.collection.emitter.once('queue', function() {
var cursor = _this._model.collection.
aggregate(_this._pipeline, _this.options || {});
resolve(cursor);
callback && callback(null, cursor);
});
}
});
} else {
return this._model.collection.
aggregate(this._pipeline, this.options || {});
}
}
this._model
.collection
.aggregate(this._pipeline, this.options || {}, promise.resolve.bind(promise));
return new Promise.ES6(function(resolve, reject) {
if (!_this._pipeline.length) {
var err = new Error('Aggregate has empty pipeline');
if (callback) {
callback(err);
}
reject(err);
return;
}
return promise;
prepareDiscriminatorPipeline(_this);
_this._model
.collection
.aggregate(_this._pipeline, _this.options || {}, function(error, result) {
if (error) {
if (callback) {
callback(error);
}
reject(error);
return;
}
if (callback) {
callback(null, result);
}
resolve(result);
});
});
};
/*!
@@ -433,7 +552,7 @@ Aggregate.prototype.exec = function (callback) {
* @api private
*/
function isOperator (obj) {
function isOperator(obj) {
var k;
if ('object' !== typeof obj) {
@@ -442,7 +561,7 @@ function isOperator (obj) {
k = Object.keys(obj);
return 1 === k.length && k.some(function (key) {
return 1 === k.length && k.some(function(key) {
return '$' === key[0];
});
}
@@ -455,7 +574,7 @@ function isOperator (obj) {
* @param {Aggregate} aggregate Aggregate to prepare
*/
function prepareDiscriminatorPipeline (aggregate) {
function prepareDiscriminatorPipeline(aggregate) {
var schema = aggregate._model.schema,
discriminatorMapping = schema && schema.discriminatorMapping;
@@ -472,6 +591,10 @@ function prepareDiscriminatorPipeline (aggregate) {
originalPipeline[0].$match[discriminatorKey] = discriminatorValue;
// `originalPipeline` is a ref, so there's no need for
// aggregate._pipeline = originalPipeline
} else if (originalPipeline[0] && originalPipeline[0].$geoNear) {
originalPipeline[0].$geoNear.query =
originalPipeline[0].$geoNear.query || {};
originalPipeline[0].$geoNear.query[discriminatorKey] = discriminatorValue;
} else {
var match = {};
match[discriminatorKey] = discriminatorValue;