@ -1,6 +1,6 @@ |
|||||
module.exports = { |
module.exports = { |
||||
|
|
||||
'secret': 'secretfortoken', |
'secret': 'secretfortoken', |
||||
'database': 'mongodb://localhost/thoughts' |
|
||||
|
'database': 'mongodb://userdb:userdb@ds013366.mlab.com:13366/thoughts' |
||||
|
|
||||
}; |
}; |
@ -1,2 +0,0 @@ |
|||||
*.DS_Store |
|
||||
node_modules |
|
@ -1,6 +0,0 @@ |
|||||
.PHONY: test |
|
||||
|
|
||||
MOCHA = ./node_modules/mocha/bin/mocha |
|
||||
|
|
||||
test: |
|
||||
$(MOCHA) -R list |
|
@ -1,95 +0,0 @@ |
|||||
# cb() |
|
||||
|
|
||||
A minimal node.js utility for handling common (but often overlooked) callback scenarios. |
|
||||
|
|
||||
##Features |
|
||||
|
|
||||
* `.timeout()`: Simple callback timeouts |
|
||||
* `.error()`: Explicit error handling |
|
||||
* `.once()`: Once-and-only-once callback semantics |
|
||||
* Guaranteed asynchronous callback execution (protects against code that breaks this assumption) |
|
||||
|
|
||||
## Installation |
|
||||
|
|
||||
$ npm install cb |
|
||||
|
|
||||
## Usage |
|
||||
|
|
||||
### Basic Usage |
|
||||
|
|
||||
The most basic usage of `cb` consists of passing in your own function reference. In this example, `cb` will do nothing other |
|
||||
than insure the once-and-only-once, asynchronous invocation of the callback. |
|
||||
|
|
||||
doAsync(cb(function(err, res) { |
|
||||
console.log(res); |
|
||||
})); |
|
||||
|
|
||||
### Timeout Handling |
|
||||
|
|
||||
Timeouts are specified through the `.timeout()` method, and are specified in milliseconds. If a timeout does occur, the error |
|
||||
passed to the callback will be an instance of `cb.TimeoutError`. |
|
||||
|
|
||||
doReallySlowAsync(cb(function(err, res) { |
|
||||
assert(err instanceof cb.TimeoutError); |
|
||||
}).timeout(50)); |
|
||||
|
|
||||
*Note: once a timeout has occured, any tardy attempts to invoke the callback will be ignored.* |
|
||||
|
|
||||
### Explicit Error Handling |
|
||||
|
|
||||
In situations where it is convenient to separate the code that runs on success or failure, this can easily be accomplished |
|
||||
with `.error()`. If an 'errback' handler has been provided to `.error()`, then it is assumed that the error-first parameter |
|
||||
to the success handler is no longer required. To illustrate, |
|
||||
|
|
||||
doAsync(cb(function(err, res) { |
|
||||
if (err) { |
|
||||
console.error(err); |
|
||||
} else { |
|
||||
console.log(res); |
|
||||
} |
|
||||
})); |
|
||||
|
|
||||
Can be rewritten as: |
|
||||
|
|
||||
doAsync(cb(console.log).error(console.error)); |
|
||||
|
|
||||
### Force Once-and-only-once Callback Execution |
|
||||
|
|
||||
Sometimes it's necessary to ensure that a callback is invoked once, and no more. Once-and-only-once execution semantics can be |
|
||||
enforced by using `.once()`. |
|
||||
|
|
||||
function runTwice(callback) { |
|
||||
process.nextTick(function() { |
|
||||
callback(); |
|
||||
callback(); |
|
||||
}); |
|
||||
} |
|
||||
|
|
||||
runTwice(cb(function() { |
|
||||
console.log('I will only run once'); |
|
||||
}).once()); |
|
||||
|
|
||||
*Note: technically, `.once()` simply enforces at-most-once semantics. However, when combined with `.timeout()`, once-and-only-once |
|
||||
is achieved.* |
|
||||
|
|
||||
### Combining Features |
|
||||
|
|
||||
The `cb` API is fully chainable, and any arrangement of the features is valid. For example: |
|
||||
|
|
||||
doAsync(cb(console.log).error(console.error).timeout(50).once()); |
|
||||
|
|
||||
## Running the Tests |
|
||||
|
|
||||
$ make test |
|
||||
|
|
||||
## License |
|
||||
|
|
||||
The MIT License (MIT) |
|
||||
|
|
||||
Copyright (c) 2012 Jeremy Martin |
|
||||
|
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
|
||||
|
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
|
||||
|
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
@ -1,39 +0,0 @@ |
|||||
module.exports = function(callback) { |
|
||||
|
|
||||
var cb = function() { |
|
||||
if (timedout || (once && count)) return; |
|
||||
count += 1; |
|
||||
tid && clearTimeout(tid); |
|
||||
|
|
||||
var args = Array.prototype.slice.call(arguments); |
|
||||
process.nextTick(function() { |
|
||||
if (!errback) return callback.apply(this, args); |
|
||||
args[0] ? errback(args[0]) : callback.apply(this, args.slice(1)); |
|
||||
}); |
|
||||
|
|
||||
}, count = 0, once = false, timedout = false, errback, tid; |
|
||||
|
|
||||
cb.timeout = function(ms) { |
|
||||
tid && clearTimeout(tid); |
|
||||
tid = setTimeout(function() { |
|
||||
cb(new TimeoutError(ms)); |
|
||||
timedout = true; |
|
||||
}, ms); |
|
||||
return cb; |
|
||||
}; |
|
||||
|
|
||||
cb.error = function(func) { errback = func; return cb; }; |
|
||||
|
|
||||
cb.once = function() { once = true; return cb; }; |
|
||||
|
|
||||
return cb; |
|
||||
|
|
||||
}; |
|
||||
|
|
||||
var TimeoutError = module.exports.TimeoutError = function TimeoutError(ms) { |
|
||||
this.message = 'Specified timeout of ' + ms + 'ms was reached'; |
|
||||
Error.captureStackTrace(this, this.constructor); |
|
||||
}; |
|
||||
TimeoutError.prototype = new Error; |
|
||||
TimeoutError.prototype.constructor = TimeoutError; |
|
||||
TimeoutError.prototype.name = 'TimeoutError'; |
|
@ -1,46 +0,0 @@ |
|||||
{ |
|
||||
"author": { |
|
||||
"name": "Jeremy Martin", |
|
||||
"email": "jmar777@gmail.com", |
|
||||
"url": "http://twitter.com/jmar777" |
|
||||
}, |
|
||||
"name": "cb", |
|
||||
"description": "Super simple callback mechanism with support for timeouts and explicit error handling", |
|
||||
"version": "0.1.0", |
|
||||
"repository": { |
|
||||
"type": "git", |
|
||||
"url": "git://github.com/jmar777/cb.git" |
|
||||
}, |
|
||||
"main": "lib/cb.js", |
|
||||
"devDependencies": { |
|
||||
"mocha": ">=0.3.6" |
|
||||
}, |
|
||||
"engines": { |
|
||||
"node": ">=0.6.0" |
|
||||
}, |
|
||||
"_npmUser": { |
|
||||
"name": "jmar777", |
|
||||
"email": "jmar777@gmail.com" |
|
||||
}, |
|
||||
"_id": "cb@0.1.0", |
|
||||
"dependencies": {}, |
|
||||
"optionalDependencies": {}, |
|
||||
"_engineSupported": true, |
|
||||
"_npmVersion": "1.1.0-2", |
|
||||
"_nodeVersion": "v0.6.8", |
|
||||
"_defaultsLoaded": true, |
|
||||
"dist": { |
|
||||
"shasum": "26f7e740f2808facc83cef7b20392e4d881b5203", |
|
||||
"tarball": "https://registry.npmjs.org/cb/-/cb-0.1.0.tgz" |
|
||||
}, |
|
||||
"maintainers": [ |
|
||||
{ |
|
||||
"name": "jmar777", |
|
||||
"email": "jmar777@gmail.com" |
|
||||
} |
|
||||
], |
|
||||
"directories": {}, |
|
||||
"_shasum": "26f7e740f2808facc83cef7b20392e4d881b5203", |
|
||||
"_resolved": "https://registry.npmjs.org/cb/-/cb-0.1.0.tgz", |
|
||||
"_from": "cb@>=0.1.0 <0.2.0" |
|
||||
} |
|
@ -1,125 +0,0 @@ |
|||||
var assert = require('assert'), |
|
||||
cb = require('../'); |
|
||||
|
|
||||
function invokeAsync(callback) { |
|
||||
setTimeout(function() { |
|
||||
callback(null, 'foo'); |
|
||||
}, 100); |
|
||||
} |
|
||||
|
|
||||
function invokeAsyncError(callback) { |
|
||||
setTimeout(function() { |
|
||||
callback(new Error()); |
|
||||
}, 100); |
|
||||
} |
|
||||
|
|
||||
function invokeAsyncTwice(callback) { |
|
||||
setTimeout(function() { |
|
||||
callback(null, 'foo'); |
|
||||
callback(null, 'foo'); |
|
||||
}, 100); |
|
||||
} |
|
||||
|
|
||||
describe('cb(callback)', function() { |
|
||||
|
|
||||
it('should invoke the provided callback', function(done) { |
|
||||
invokeAsync(cb(function(err, res) { |
|
||||
assert.strictEqual(res, 'foo'); |
|
||||
done(); |
|
||||
})); |
|
||||
}); |
|
||||
|
|
||||
it('shouldn\'t mess with errors', function(done) { |
|
||||
invokeAsyncError(cb(function(err, res) { |
|
||||
assert(err); |
|
||||
done(); |
|
||||
})); |
|
||||
}); |
|
||||
|
|
||||
it('should allow multiple executions', function(done) { |
|
||||
var count = 0; |
|
||||
invokeAsyncTwice(cb(function(err, res) { |
|
||||
count++; |
|
||||
if (count === 2) done(); |
|
||||
})); |
|
||||
}); |
|
||||
|
|
||||
}); |
|
||||
|
|
||||
describe('cb(callback).timeout(ms)', function() { |
|
||||
|
|
||||
it('should complete successfully within timeout period', function(done) { |
|
||||
invokeAsync(cb(function(err, res) { |
|
||||
assert.strictEqual(res, 'foo'); |
|
||||
done(); |
|
||||
}).timeout(200)); |
|
||||
}); |
|
||||
|
|
||||
it('should complete with an error after timeout period', function(done) { |
|
||||
invokeAsync(cb(function(err, res) { |
|
||||
assert(err); |
|
||||
done(); |
|
||||
}).timeout(50)); |
|
||||
}); |
|
||||
|
|
||||
it('error resulting from a timeout should be instanceof cb.TimeoutError', function(done) { |
|
||||
invokeAsync(cb(function(err, res) { |
|
||||
assert(err instanceof cb.TimeoutError); |
|
||||
done(); |
|
||||
}).timeout(50)); |
|
||||
}); |
|
||||
|
|
||||
}); |
|
||||
|
|
||||
describe('cb(callback).error(errback)', function() { |
|
||||
|
|
||||
it('should skip the err argument when invoking callback', function(done) { |
|
||||
invokeAsync(cb(function(res) { |
|
||||
assert.strictEqual(res, 'foo'); |
|
||||
done(); |
|
||||
}).error(assert.ifError)); |
|
||||
}); |
|
||||
|
|
||||
it('should pass errors to provided errback', function(done) { |
|
||||
invokeAsyncError(cb(function(res) { |
|
||||
throw new Error('should not be invoked'); |
|
||||
}).error(function(err) { |
|
||||
assert(err); |
|
||||
done(); |
|
||||
})); |
|
||||
}); |
|
||||
|
|
||||
}); |
|
||||
|
|
||||
describe('cb(callback).error(errback).timeout(ms)', function() { |
|
||||
|
|
||||
it('should skip the err argument when invoking callback', function(done) { |
|
||||
invokeAsync(cb(function(res) { |
|
||||
assert.strictEqual(res, 'foo'); |
|
||||
done(); |
|
||||
}).error(assert.ifError).timeout(200)); |
|
||||
}); |
|
||||
|
|
||||
it('should pass timeout error to provided errback', function(done) { |
|
||||
invokeAsyncError(cb(function(res) { |
|
||||
throw new Error('should not be invoked'); |
|
||||
}).error(function(err) { |
|
||||
assert(err); |
|
||||
done(); |
|
||||
}).timeout(50)); |
|
||||
}); |
|
||||
|
|
||||
}); |
|
||||
|
|
||||
describe('cb(callback).once()', function() { |
|
||||
|
|
||||
it('should allow multiple executions', function(done) { |
|
||||
var count = 0; |
|
||||
invokeAsyncTwice(cb(function(err, res) { |
|
||||
count++; |
|
||||
assert.notEqual(count, 2); |
|
||||
setTimeout(done, 100); |
|
||||
}).once()); |
|
||||
}); |
|
||||
|
|
||||
}); |
|
@ -0,0 +1,18 @@ |
|||||
|
# lodash.once v4.1.1 |
||||
|
|
||||
|
The [lodash](https://lodash.com/) method `_.once` exported as a [Node.js](https://nodejs.org/) module. |
||||
|
|
||||
|
## Installation |
||||
|
|
||||
|
Using npm: |
||||
|
```bash |
||||
|
$ {sudo -H} npm i -g npm |
||||
|
$ npm i --save lodash.once |
||||
|
``` |
||||
|
|
||||
|
In Node.js: |
||||
|
```js |
||||
|
var once = require('lodash.once'); |
||||
|
``` |
||||
|
|
||||
|
See the [documentation](https://lodash.com/docs#once) or [package source](https://github.com/lodash/lodash/blob/4.1.1-npm-packages/lodash.once) for more details. |
@ -0,0 +1,294 @@ |
|||||
|
/** |
||||
|
* lodash (Custom Build) <https://lodash.com/>
|
||||
|
* Build: `lodash modularize exports="npm" -o ./` |
||||
|
* Copyright jQuery Foundation and other contributors <https://jquery.org/>
|
||||
|
* Released under MIT license <https://lodash.com/license>
|
||||
|
* Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
|
||||
|
* Copyright Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors |
||||
|
*/ |
||||
|
|
||||
|
/** Used as the `TypeError` message for "Functions" methods. */ |
||||
|
var FUNC_ERROR_TEXT = 'Expected a function'; |
||||
|
|
||||
|
/** Used as references for various `Number` constants. */ |
||||
|
var INFINITY = 1 / 0, |
||||
|
MAX_INTEGER = 1.7976931348623157e+308, |
||||
|
NAN = 0 / 0; |
||||
|
|
||||
|
/** `Object#toString` result references. */ |
||||
|
var symbolTag = '[object Symbol]'; |
||||
|
|
||||
|
/** Used to match leading and trailing whitespace. */ |
||||
|
var reTrim = /^\s+|\s+$/g; |
||||
|
|
||||
|
/** Used to detect bad signed hexadecimal string values. */ |
||||
|
var reIsBadHex = /^[-+]0x[0-9a-f]+$/i; |
||||
|
|
||||
|
/** Used to detect binary string values. */ |
||||
|
var reIsBinary = /^0b[01]+$/i; |
||||
|
|
||||
|
/** Used to detect octal string values. */ |
||||
|
var reIsOctal = /^0o[0-7]+$/i; |
||||
|
|
||||
|
/** Built-in method references without a dependency on `root`. */ |
||||
|
var freeParseInt = parseInt; |
||||
|
|
||||
|
/** Used for built-in method references. */ |
||||
|
var objectProto = Object.prototype; |
||||
|
|
||||
|
/** |
||||
|
* Used to resolve the |
||||
|
* [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring)
|
||||
|
* of values. |
||||
|
*/ |
||||
|
var objectToString = objectProto.toString; |
||||
|
|
||||
|
/** |
||||
|
* Creates a function that invokes `func`, with the `this` binding and arguments |
||||
|
* of the created function, while it's called less than `n` times. Subsequent |
||||
|
* calls to the created function return the result of the last `func` invocation. |
||||
|
* |
||||
|
* @static |
||||
|
* @memberOf _ |
||||
|
* @since 3.0.0 |
||||
|
* @category Function |
||||
|
* @param {number} n The number of calls at which `func` is no longer invoked. |
||||
|
* @param {Function} func The function to restrict. |
||||
|
* @returns {Function} Returns the new restricted function. |
||||
|
* @example |
||||
|
* |
||||
|
* jQuery(element).on('click', _.before(5, addContactToList)); |
||||
|
* // => Allows adding up to 4 contacts to the list.
|
||||
|
*/ |
||||
|
function before(n, func) { |
||||
|
var result; |
||||
|
if (typeof func != 'function') { |
||||
|
throw new TypeError(FUNC_ERROR_TEXT); |
||||
|
} |
||||
|
n = toInteger(n); |
||||
|
return function() { |
||||
|
if (--n > 0) { |
||||
|
result = func.apply(this, arguments); |
||||
|
} |
||||
|
if (n <= 1) { |
||||
|
func = undefined; |
||||
|
} |
||||
|
return result; |
||||
|
}; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Creates a function that is restricted to invoking `func` once. Repeat calls |
||||
|
* to the function return the value of the first invocation. The `func` is |
||||
|
* invoked with the `this` binding and arguments of the created function. |
||||
|
* |
||||
|
* @static |
||||
|
* @memberOf _ |
||||
|
* @since 0.1.0 |
||||
|
* @category Function |
||||
|
* @param {Function} func The function to restrict. |
||||
|
* @returns {Function} Returns the new restricted function. |
||||
|
* @example |
||||
|
* |
||||
|
* var initialize = _.once(createApplication); |
||||
|
* initialize(); |
||||
|
* initialize(); |
||||
|
* // => `createApplication` is invoked once
|
||||
|
*/ |
||||
|
function once(func) { |
||||
|
return before(2, func); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Checks if `value` is the |
||||
|
* [language type](http://www.ecma-international.org/ecma-262/7.0/#sec-ecmascript-language-types)
|
||||
|
* of `Object`. (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`) |
||||
|
* |
||||
|
* @static |
||||
|
* @memberOf _ |
||||
|
* @since 0.1.0 |
||||
|
* @category Lang |
||||
|
* @param {*} value The value to check. |
||||
|
* @returns {boolean} Returns `true` if `value` is an object, else `false`. |
||||
|
* @example |
||||
|
* |
||||
|
* _.isObject({}); |
||||
|
* // => true
|
||||
|
* |
||||
|
* _.isObject([1, 2, 3]); |
||||
|
* // => true
|
||||
|
* |
||||
|
* _.isObject(_.noop); |
||||
|
* // => true
|
||||
|
* |
||||
|
* _.isObject(null); |
||||
|
* // => false
|
||||
|
*/ |
||||
|
function isObject(value) { |
||||
|
var type = typeof value; |
||||
|
return !!value && (type == 'object' || type == 'function'); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Checks if `value` is object-like. A value is object-like if it's not `null` |
||||
|
* and has a `typeof` result of "object". |
||||
|
* |
||||
|
* @static |
||||
|
* @memberOf _ |
||||
|
* @since 4.0.0 |
||||
|
* @category Lang |
||||
|
* @param {*} value The value to check. |
||||
|
* @returns {boolean} Returns `true` if `value` is object-like, else `false`. |
||||
|
* @example |
||||
|
* |
||||
|
* _.isObjectLike({}); |
||||
|
* // => true
|
||||
|
* |
||||
|
* _.isObjectLike([1, 2, 3]); |
||||
|
* // => true
|
||||
|
* |
||||
|
* _.isObjectLike(_.noop); |
||||
|
* // => false
|
||||
|
* |
||||
|
* _.isObjectLike(null); |
||||
|
* // => false
|
||||
|
*/ |
||||
|
function isObjectLike(value) { |
||||
|
return !!value && typeof value == 'object'; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Checks if `value` is classified as a `Symbol` primitive or object. |
||||
|
* |
||||
|
* @static |
||||
|
* @memberOf _ |
||||
|
* @since 4.0.0 |
||||
|
* @category Lang |
||||
|
* @param {*} value The value to check. |
||||
|
* @returns {boolean} Returns `true` if `value` is a symbol, else `false`. |
||||
|
* @example |
||||
|
* |
||||
|
* _.isSymbol(Symbol.iterator); |
||||
|
* // => true
|
||||
|
* |
||||
|
* _.isSymbol('abc'); |
||||
|
* // => false
|
||||
|
*/ |
||||
|
function isSymbol(value) { |
||||
|
return typeof value == 'symbol' || |
||||
|
(isObjectLike(value) && objectToString.call(value) == symbolTag); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Converts `value` to a finite number. |
||||
|
* |
||||
|
* @static |
||||
|
* @memberOf _ |
||||
|
* @since 4.12.0 |
||||
|
* @category Lang |
||||
|
* @param {*} value The value to convert. |
||||
|
* @returns {number} Returns the converted number. |
||||
|
* @example |
||||
|
* |
||||
|
* _.toFinite(3.2); |
||||
|
* // => 3.2
|
||||
|
* |
||||
|
* _.toFinite(Number.MIN_VALUE); |
||||
|
* // => 5e-324
|
||||
|
* |
||||
|
* _.toFinite(Infinity); |
||||
|
* // => 1.7976931348623157e+308
|
||||
|
* |
||||
|
* _.toFinite('3.2'); |
||||
|
* // => 3.2
|
||||
|
*/ |
||||
|
function toFinite(value) { |
||||
|
if (!value) { |
||||
|
return value === 0 ? value : 0; |
||||
|
} |
||||
|
value = toNumber(value); |
||||
|
if (value === INFINITY || value === -INFINITY) { |
||||
|
var sign = (value < 0 ? -1 : 1); |
||||
|
return sign * MAX_INTEGER; |
||||
|
} |
||||
|
return value === value ? value : 0; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Converts `value` to an integer. |
||||
|
* |
||||
|
* **Note:** This method is loosely based on |
||||
|
* [`ToInteger`](http://www.ecma-international.org/ecma-262/7.0/#sec-tointeger).
|
||||
|
* |
||||
|
* @static |
||||
|
* @memberOf _ |
||||
|
* @since 4.0.0 |
||||
|
* @category Lang |
||||
|
* @param {*} value The value to convert. |
||||
|
* @returns {number} Returns the converted integer. |
||||
|
* @example |
||||
|
* |
||||
|
* _.toInteger(3.2); |
||||
|
* // => 3
|
||||
|
* |
||||
|
* _.toInteger(Number.MIN_VALUE); |
||||
|
* // => 0
|
||||
|
* |
||||
|
* _.toInteger(Infinity); |
||||
|
* // => 1.7976931348623157e+308
|
||||
|
* |
||||
|
* _.toInteger('3.2'); |
||||
|
* // => 3
|
||||
|
*/ |
||||
|
function toInteger(value) { |
||||
|
var result = toFinite(value), |
||||
|
remainder = result % 1; |
||||
|
|
||||
|
return result === result ? (remainder ? result - remainder : result) : 0; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Converts `value` to a number. |
||||
|
* |
||||
|
* @static |
||||
|
* @memberOf _ |
||||
|
* @since 4.0.0 |
||||
|
* @category Lang |
||||
|
* @param {*} value The value to process. |
||||
|
* @returns {number} Returns the number. |
||||
|
* @example |
||||
|
* |
||||
|
* _.toNumber(3.2); |
||||
|
* // => 3.2
|
||||
|
* |
||||
|
* _.toNumber(Number.MIN_VALUE); |
||||
|
* // => 5e-324
|
||||
|
* |
||||
|
* _.toNumber(Infinity); |
||||
|
* // => Infinity
|
||||
|
* |
||||
|
* _.toNumber('3.2'); |
||||
|
* // => 3.2
|
||||
|
*/ |
||||
|
function toNumber(value) { |
||||
|
if (typeof value == 'number') { |
||||
|
return value; |
||||
|
} |
||||
|
if (isSymbol(value)) { |
||||
|
return NAN; |
||||
|
} |
||||
|
if (isObject(value)) { |
||||
|
var other = typeof value.valueOf == 'function' ? value.valueOf() : value; |
||||
|
value = isObject(other) ? (other + '') : other; |
||||
|
} |
||||
|
if (typeof value != 'string') { |
||||
|
return value === 0 ? value : +value; |
||||
|
} |
||||
|
value = value.replace(reTrim, ''); |
||||
|
var isBinary = reIsBinary.test(value); |
||||
|
return (isBinary || reIsOctal.test(value)) |
||||
|
? freeParseInt(value.slice(2), isBinary ? 2 : 8) |
||||
|
: (reIsBadHex.test(value) ? NAN : +value); |
||||
|
} |
||||
|
|
||||
|
module.exports = once; |
@ -0,0 +1,78 @@ |
|||||
|
{ |
||||
|
"name": "lodash.once", |
||||
|
"version": "4.1.1", |
||||
|
"description": "The lodash method `_.once` exported as a module.", |
||||
|
"homepage": "https://lodash.com/", |
||||
|
"icon": "https://lodash.com/icon.svg", |
||||
|
"license": "MIT", |
||||
|
"keywords": [ |
||||
|
"lodash-modularized", |
||||
|
"once" |
||||
|
], |
||||
|
"author": { |
||||
|
"name": "John-David Dalton", |
||||
|
"email": "john.david.dalton@gmail.com", |
||||
|
"url": "http://allyoucanleet.com/" |
||||
|
}, |
||||
|
"contributors": [ |
||||
|
{ |
||||
|
"name": "John-David Dalton", |
||||
|
"email": "john.david.dalton@gmail.com", |
||||
|
"url": "http://allyoucanleet.com/" |
||||
|
}, |
||||
|
{ |
||||
|
"name": "Blaine Bublitz", |
||||
|
"email": "blaine.bublitz@gmail.com", |
||||
|
"url": "https://github.com/phated" |
||||
|
}, |
||||
|
{ |
||||
|
"name": "Mathias Bynens", |
||||
|
"email": "mathias@qiwi.be", |
||||
|
"url": "https://mathiasbynens.be/" |
||||
|
} |
||||
|
], |
||||
|
"repository": { |
||||
|
"type": "git", |
||||
|
"url": "git+https://github.com/lodash/lodash.git" |
||||
|
}, |
||||
|
"scripts": { |
||||
|
"test": "echo \"See https://travis-ci.org/lodash/lodash-cli for testing details.\"" |
||||
|
}, |
||||
|
"bugs": { |
||||
|
"url": "https://github.com/lodash/lodash/issues" |
||||
|
}, |
||||
|
"_id": "lodash.once@4.1.1", |
||||
|
"_shasum": "0dd3971213c7c56df880977d504c88fb471a97ac", |
||||
|
"_from": "lodash.once@>=4.0.0 <5.0.0", |
||||
|
"_npmVersion": "2.15.10", |
||||
|
"_nodeVersion": "4.4.7", |
||||
|
"_npmUser": { |
||||
|
"name": "jdalton", |
||||
|
"email": "john.david.dalton@gmail.com" |
||||
|
}, |
||||
|
"dist": { |
||||
|
"shasum": "0dd3971213c7c56df880977d504c88fb471a97ac", |
||||
|
"tarball": "https://registry.npmjs.org/lodash.once/-/lodash.once-4.1.1.tgz" |
||||
|
}, |
||||
|
"maintainers": [ |
||||
|
{ |
||||
|
"name": "jdalton", |
||||
|
"email": "john.david.dalton@gmail.com" |
||||
|
}, |
||||
|
{ |
||||
|
"name": "mathias", |
||||
|
"email": "mathias@qiwi.be" |
||||
|
}, |
||||
|
{ |
||||
|
"name": "phated", |
||||
|
"email": "blaine@iceddev.com" |
||||
|
} |
||||
|
], |
||||
|
"_npmOperationalInternal": { |
||||
|
"host": "packages-12-west.internal.npmjs.com", |
||||
|
"tmp": "tmp/lodash.once-4.1.1.tgz_1471110166870_0.09848929662257433" |
||||
|
}, |
||||
|
"directories": {}, |
||||
|
"_resolved": "https://registry.npmjs.org/lodash.once/-/lodash.once-4.1.1.tgz", |
||||
|
"readme": "ERROR: No README data found!" |
||||
|
} |
@ -0,0 +1,3 @@ |
|||||
|
docs/ |
||||
|
bin/ |
||||
|
test/triage/ |
@ -0,0 +1,31 @@ |
|||||
|
extends: 'eslint:recommended' |
||||
|
|
||||
|
env: |
||||
|
node: true |
||||
|
mocha: true |
||||
|
|
||||
|
rules: |
||||
|
comma-style: error |
||||
|
consistent-this: |
||||
|
- error |
||||
|
- _this |
||||
|
indent: |
||||
|
- error |
||||
|
- 2 |
||||
|
- SwitchCase: 1 |
||||
|
VariableDeclarator: 2 |
||||
|
keyword-spacing: error |
||||
|
no-console: off |
||||
|
no-multi-spaces: error |
||||
|
no-spaced-func: error |
||||
|
no-trailing-spaces: error |
||||
|
quotes: |
||||
|
- error |
||||
|
- single |
||||
|
semi: error |
||||
|
space-before-blocks: error |
||||
|
space-before-function-paren: |
||||
|
- error |
||||
|
- never |
||||
|
space-infix-ops: error |
||||
|
space-unary-ops: error |
@ -1,7 +1,14 @@ |
|||||
language: node_js |
language: node_js |
||||
|
sudo: false |
||||
node_js: |
node_js: |
||||
- 0.6 |
|
||||
- 0.8 |
|
||||
- 0.10 |
|
||||
services: |
|
||||
- mongodb |
|
||||
|
- "6" |
||||
|
- "5" |
||||
|
- "4" |
||||
|
- "0.12" |
||||
|
- "0.10" |
||||
|
- "iojs" |
||||
|
before_script: |
||||
|
- wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.6.11.tgz |
||||
|
- tar -zxvf mongodb-linux-x86_64-2.6.11.tgz |
||||
|
- mkdir -p ./data/db |
||||
|
- ./mongodb-linux-x86_64-2.6.11/bin/mongod --fork --nopreallocj --dbpath ./data/db --syslog --port 27017 |
@ -1,60 +1,56 @@ |
|||||
## Contributing to Mongoose |
## Contributing to Mongoose |
||||
|
|
||||
### STOP! |
|
||||
|
|
||||
If you have a question about Mongoose (not a bug report) please post it to either [StackOverflow](http://stackoverflow.com/questions/tagged/mongoose), our [Google Group](http://groups.google.com/group/mongoose-orm), or on the #mongoosejs irc channel on freenode. |
|
||||
|
If you have a question about Mongoose (not a bug report) please post it to either [StackOverflow](http://stackoverflow.com/questions/tagged/mongoose), or on [Gitter](https://gitter.im/Automattic/mongoose?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) |
||||
|
|
||||
### Reporting bugs |
### Reporting bugs |
||||
|
|
||||
- Before opening a new issue, look for existing [issues](https://github.com/learnboost/mongoose/issues) to avoid duplication. If the issue does not yet exist, [create one](https://github.com/learnboost/mongoose/issues/new). |
|
||||
- Please describe the issue you are experiencing, along with any associated stack trace. |
|
||||
- Please post code that reproduces the issue, the version of mongoose, node version, and mongodb version. |
|
||||
- _The source of this project is written in javascript, not coffeescript, therefore your bug reports should be written in javascript_. |
|
||||
- In general, adding a "+1" comment to an existing issue does little to help get it resolved. A better way is to submit a well documented pull request with clean code and passing tests. |
|
||||
|
- Before opening a new issue, look for existing [issues](https://github.com/Automattic/mongoose/issues) to avoid duplication. If the issue does not yet exist, [create one](https://github.com/Automattic/mongoose/issues/new). |
||||
|
- Please post any relevant code samples, preferably a standalone script that |
||||
|
reproduces your issue. Do **not** describe your issue in prose, show your |
||||
|
code. |
||||
|
- If the bug involves an error, please post the stack trace. |
||||
|
- Please post the version of mongoose and mongodb that you're using. |
||||
|
- Please write bug reports in JavaScript (ES5 or ES2015), not coffeescript, typescript, etc. |
||||
|
|
||||
### Requesting new features |
### Requesting new features |
||||
|
|
||||
- Before opening a new issue, look for existing [issues](https://github.com/learnboost/mongoose/issues) to avoid duplication. If the issue does not yet exist, [create one](https://github.com/learnboost/mongoose/issues/new). |
- Before opening a new issue, look for existing [issues](https://github.com/learnboost/mongoose/issues) to avoid duplication. If the issue does not yet exist, [create one](https://github.com/learnboost/mongoose/issues/new). |
||||
- Please describe a use case for it |
- Please describe a use case for it |
||||
- it would be ideal to include test cases as well |
- it would be ideal to include test cases as well |
||||
- In general, adding a "+1" comment to an existing issue does little to help get it resolved. A better way is to submit a well documented pull request with clean code and passing tests. |
|
||||
|
|
||||
### Fixing bugs / Adding features |
### Fixing bugs / Adding features |
||||
|
|
||||
- Before starting to write code, look for existing [issues](https://github.com/learnboost/mongoose/issues). That way you avoid working on something that might not be of interest or that has been addressed already in a different branch. You can create a new issue [here](https://github.com/learnboost/mongoose/issues/new). |
- Before starting to write code, look for existing [issues](https://github.com/learnboost/mongoose/issues). That way you avoid working on something that might not be of interest or that has been addressed already in a different branch. You can create a new issue [here](https://github.com/learnboost/mongoose/issues/new). |
||||
- _The source of this project is written in javascript, not coffeescript, therefore your bug reports should be written in javascript_. |
|
||||
- Fork the [repo](https://github.com/learnboost/mongoose) _or_ for small documentation changes, navigate to the source on github and click the [Edit](https://github.com/blog/844-forking-with-the-edit-button) button. |
|
||||
|
- _The source of this project is written in javascript, not coffeescript, therefore your bug reports should be written in javascript_. |
||||
|
- Fork the [repo](https://github.com/Automattic/mongoose) _or_ for small documentation changes, navigate to the source on github and click the [Edit](https://github.com/blog/844-forking-with-the-edit-button) button. |
||||
- Follow the general coding style of the rest of the project: |
- Follow the general coding style of the rest of the project: |
||||
- 2 space tabs |
- 2 space tabs |
||||
- no trailing whitespace |
- no trailing whitespace |
||||
- comma first |
|
||||
- inline documentation for new methods, class members, etc |
|
||||
- 1 space between conditionals/functions, and their parenthesis and curly braces |
|
||||
|
- inline documentation for new methods, class members, etc. |
||||
|
- 1 space between conditionals, no space before function parenthesis |
||||
- `if (..) {` |
- `if (..) {` |
||||
- `for (..) {` |
- `for (..) {` |
||||
- `while (..) {` |
- `while (..) {` |
||||
- `function (err) {` |
|
||||
- Write tests and make sure they pass (tests are in the [test](https://github.com/LearnBoost/mongoose/tree/master/test) directory). |
|
||||
|
- `function(err) {` |
||||
|
- Write tests and make sure they pass (tests are in the [test](https://github.com/Automattic/mongoose/tree/master/test) directory). |
||||
|
|
||||
### Running the tests |
### Running the tests |
||||
- Open a terminal and navigate to the root of the project |
- Open a terminal and navigate to the root of the project |
||||
- execute `npm install` to install the necessary dependencies |
- execute `npm install` to install the necessary dependencies |
||||
- execute `make test` to run the tests (we're using [mocha](http://visionmedia.github.com/mocha/)) |
|
||||
- or to execute a single test `T="-g 'some regexp that matches the test description'" make test` |
|
||||
- any mocha flags can be specified with T="..." |
|
||||
|
- start a mongodb instance on port 27017 if one isn't running already. `mongod --dbpath <path to store data> --port 27017` |
||||
|
- execute `npm test` to run the tests (we're using [mocha](http://mochajs.org/)) |
||||
|
- or to execute a single test `npm test -- -g 'some regexp that matches the test description'` |
||||
|
- any mocha flags can be specified with `-- <mocha flags here>` |
||||
|
- For example, you can use `npm test -- -R spec` to use the spec reporter, rather than the dot reporter (by default, the test output looks like a bunch of dots) |
||||
|
|
||||
### Documentation |
### Documentation |
||||
|
|
||||
To contribute to the [API documentation](http://mongoosejs.com/docs/api.html) just make your changes to the inline documentation of the appropriate [source code](https://github.com/LearnBoost/mongoose/tree/master/lib) in the master branch and submit a [pull request](https://help.github.com/articles/using-pull-requests/). You might also use the github [Edit](https://github.com/blog/844-forking-with-the-edit-button) button. |
|
||||
|
To contribute to the [API documentation](http://mongoosejs.com/docs/api.html) just make your changes to the inline documentation of the appropriate [source code](https://github.com/Automattic/mongoose/tree/master/lib) in the master branch and submit a [pull request](https://help.github.com/articles/using-pull-requests/). You might also use the github [Edit](https://github.com/blog/844-forking-with-the-edit-button) button. |
||||
|
|
||||
To contribute to the [guide](http://mongoosejs.com/docs/guide.html) or [quick start](http://mongoosejs.com/docs/index.html) docs, make your changes to the appropriate `.jade` files in the [docs](https://github.com/LearnBoost/mongoose/tree/master/docs) directory of the master branch and submit a pull request. Again, the [Edit](https://github.com/blog/844-forking-with-the-edit-button) button might work for you here. |
|
||||
|
To contribute to the [guide](http://mongoosejs.com/docs/guide.html) or [quick start](http://mongoosejs.com/docs/index.html) docs, make your changes to the appropriate `.jade` files in the [docs](https://github.com/Automattic/mongoose/tree/master/docs) directory of the master branch and submit a pull request. Again, the [Edit](https://github.com/blog/844-forking-with-the-edit-button) button might work for you here. |
||||
|
|
||||
If you'd like to preview your documentation changes, first commit your changes to your local master branch, then execute `make docs` from the project root, which switches to the gh-pages branch, merges from master, and builds all the static pages for you. Now execute `node server.js` from the project root which will launch a local webserver where you can browse the documentation site locally. If all looks good, submit a [pull request](https://help.github.com/articles/using-pull-requests/) to the master branch with your changes. |
|
||||
|
If you'd like to preview your documentation changes, first commit your changes to your local master branch, then execute `make docs` from the project root, which switches to the gh-pages branch, merges from the master branch and builds all the static pages for you. Now execute `node static.js` from the project root which will launch a local webserver where you can browse the documentation site locally. If all looks good, submit a [pull request](https://help.github.com/articles/using-pull-requests/) to the master branch with your changes. |
||||
|
|
||||
### Plugins website |
### Plugins website |
||||
|
|
||||
The [plugins](http://plugins.mongoosejs.com/) site is also an [open source project](https://github.com/aheckmann/mongooseplugins) that you can get involved with. Feel free to fork and improve it as well! |
|
||||
|
|
||||
### Sharing your projects |
|
||||
|
|
||||
All are welcome to share their creations which use mongoose on our [tumbler](http://mongoosejs.tumblr.com/). Just fill out the [simple submission form](http://mongoosejs.tumblr.com/submit). |
|
||||
|
The [plugins](http://plugins.mongoosejs.io/) site is also an [open source project](https://github.com/vkarpov15/mongooseplugins) that you can get involved with. Feel free to fork and improve it as well! |
@ -0,0 +1,103 @@ |
|||||
|
|
||||
|
// import async to make control flow simplier
|
||||
|
var async = require('async'); |
||||
|
|
||||
|
// import the rest of the normal stuff
|
||||
|
var mongoose = require('../../lib'); |
||||
|
|
||||
|
require('./person.js')(); |
||||
|
|
||||
|
var Person = mongoose.model('Person'); |
||||
|
|
||||
|
// define some dummy data
|
||||
|
var data = [ |
||||
|
{ |
||||
|
name: 'bill', |
||||
|
age: 25, |
||||
|
birthday: new Date().setFullYear((new Date().getFullYear() - 25)), |
||||
|
gender: 'Male', |
||||
|
likes: ['movies', 'games', 'dogs'] |
||||
|
}, |
||||
|
{ |
||||
|
name: 'mary', |
||||
|
age: 30, |
||||
|
birthday: new Date().setFullYear((new Date().getFullYear() - 30)), |
||||
|
gender: 'Female', |
||||
|
likes: ['movies', 'birds', 'cats'] |
||||
|
}, |
||||
|
{ |
||||
|
name: 'bob', |
||||
|
age: 21, |
||||
|
birthday: new Date().setFullYear((new Date().getFullYear() - 21)), |
||||
|
gender: 'Male', |
||||
|
likes: ['tv', 'games', 'rabbits'] |
||||
|
}, |
||||
|
{ |
||||
|
name: 'lilly', |
||||
|
age: 26, |
||||
|
birthday: new Date().setFullYear((new Date().getFullYear() - 26)), |
||||
|
gender: 'Female', |
||||
|
likes: ['books', 'cats', 'dogs'] |
||||
|
}, |
||||
|
{ |
||||
|
name: 'alucard', |
||||
|
age: 1000, |
||||
|
birthday: new Date().setFullYear((new Date().getFullYear() - 1000)), |
||||
|
gender: 'Male', |
||||
|
likes: ['glasses', 'wine', 'the night'] |
||||
|
} |
||||
|
]; |
||||
|
|
||||
|
|
||||
|
mongoose.connect('mongodb://localhost/persons', function(err) { |
||||
|
if (err) throw err; |
||||
|
|
||||
|
// create all of the dummy people
|
||||
|
async.each(data, function(item, cb) { |
||||
|
Person.create(item, cb); |
||||
|
}, function(err) { |
||||
|
if (err) { |
||||
|
// handle error
|
||||
|
} |
||||
|
|
||||
|
// run an aggregate query that will get all of the people who like a given
|
||||
|
// item. To see the full documentation on ways to use the aggregate
|
||||
|
// framework, see http://docs.mongodb.org/manual/core/aggregation/
|
||||
|
Person.aggregate( |
||||
|
// select the fields we want to deal with
|
||||
|
{$project: {name: 1, likes: 1}}, |
||||
|
// unwind 'likes', which will create a document for each like
|
||||
|
{$unwind: '$likes'}, |
||||
|
// group everything by the like and then add each name with that like to
|
||||
|
// the set for the like
|
||||
|
{$group: { |
||||
|
_id: {likes: '$likes'}, |
||||
|
likers: {$addToSet: '$name'} |
||||
|
}}, |
||||
|
function(err, result) { |
||||
|
if (err) throw err; |
||||
|
console.log(result); |
||||
|
/* [ |
||||
|
{ _id: { likes: 'the night' }, likers: [ 'alucard' ] }, |
||||
|
{ _id: { likes: 'wine' }, likers: [ 'alucard' ] }, |
||||
|
{ _id: { likes: 'books' }, likers: [ 'lilly' ] }, |
||||
|
{ _id: { likes: 'glasses' }, likers: [ 'alucard' ] }, |
||||
|
{ _id: { likes: 'birds' }, likers: [ 'mary' ] }, |
||||
|
{ _id: { likes: 'rabbits' }, likers: [ 'bob' ] }, |
||||
|
{ _id: { likes: 'cats' }, likers: [ 'lilly', 'mary' ] }, |
||||
|
{ _id: { likes: 'dogs' }, likers: [ 'lilly', 'bill' ] }, |
||||
|
{ _id: { likes: 'tv' }, likers: [ 'bob' ] }, |
||||
|
{ _id: { likes: 'games' }, likers: [ 'bob', 'bill' ] }, |
||||
|
{ _id: { likes: 'movies' }, likers: [ 'mary', 'bill' ] } |
||||
|
] */ |
||||
|
|
||||
|
cleanup(); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
function cleanup() { |
||||
|
Person.remove(function() { |
||||
|
mongoose.disconnect(); |
||||
|
}); |
||||
|
} |
@ -0,0 +1,14 @@ |
|||||
|
{ |
||||
|
"name": "aggregate-example", |
||||
|
"private": "true", |
||||
|
"version": "0.0.0", |
||||
|
"description": "deps for aggregate example", |
||||
|
"main": "aggregate.js", |
||||
|
"scripts": { |
||||
|
"test": "echo \"Error: no test specified\" && exit 1" |
||||
|
}, |
||||
|
"dependencies": { "async": "*" }, |
||||
|
"repository": "", |
||||
|
"author": "", |
||||
|
"license": "BSD" |
||||
|
} |
@ -0,0 +1,17 @@ |
|||||
|
|
||||
|
// import the necessary modules
|
||||
|
var mongoose = require('../../lib'); |
||||
|
var Schema = mongoose.Schema; |
||||
|
|
||||
|
// create an export function to encapsulate the model creation
|
||||
|
module.exports = function() { |
||||
|
// define schema
|
||||
|
var PersonSchema = new Schema({ |
||||
|
name: String, |
||||
|
age: Number, |
||||
|
birthday: Date, |
||||
|
gender: String, |
||||
|
likes: [String] |
||||
|
}); |
||||
|
mongoose.model('Person', PersonSchema); |
||||
|
}; |
@ -0,0 +1 @@ |
|||||
|
Mongoose + Express examples |
@ -0,0 +1,6 @@ |
|||||
|
|
||||
|
To run: |
||||
|
|
||||
|
- Execute `npm install` from this directory |
||||
|
- Execute `node app.js` |
||||
|
- Navigate to `localhost:8000` |
@ -0,0 +1,17 @@ |
|||||
|
|
||||
|
var express = require('express'); |
||||
|
var mongoose = require('../../../lib'); |
||||
|
|
||||
|
var uri = 'mongodb://localhost/mongoose-shared-connection'; |
||||
|
global.db = mongoose.createConnection(uri); |
||||
|
|
||||
|
var routes = require('./routes'); |
||||
|
|
||||
|
var app = express(); |
||||
|
app.get('/', routes.home); |
||||
|
app.get('/insert', routes.insert); |
||||
|
app.get('/name', routes.modelName); |
||||
|
|
||||
|
app.listen(8000, function() { |
||||
|
console.log('listening on http://localhost:8000'); |
||||
|
}); |
@ -0,0 +1,5 @@ |
|||||
|
var Schema = require('../../../lib').Schema; |
||||
|
var mySchema = Schema({name: String}); |
||||
|
|
||||
|
/* global db */ |
||||
|
module.exports = db.model('MyModel', mySchema); |
@ -0,0 +1,14 @@ |
|||||
|
{ |
||||
|
"name": "connection-sharing", |
||||
|
"private": "true", |
||||
|
"version": "0.0.0", |
||||
|
"description": "ERROR: No README.md file found!", |
||||
|
"main": "app.js", |
||||
|
"scripts": { |
||||
|
"test": "echo \"Error: no test specified\" && exit 1" |
||||
|
}, |
||||
|
"dependencies": { "express": "3.1.1" }, |
||||
|
"repository": "", |
||||
|
"author": "", |
||||
|
"license": "BSD" |
||||
|
} |
@ -0,0 +1,20 @@ |
|||||
|
|
||||
|
var model = require('./modelA'); |
||||
|
|
||||
|
exports.home = function(req, res, next) { |
||||
|
model.find(function(err, docs) { |
||||
|
if (err) return next(err); |
||||
|
res.send(docs); |
||||
|
}); |
||||
|
}; |
||||
|
|
||||
|
exports.modelName = function(req, res) { |
||||
|
res.send('my model name is ' + model.modelName); |
||||
|
}; |
||||
|
|
||||
|
exports.insert = function(req, res, next) { |
||||
|
model.create({name: 'inserting ' + Date.now()}, function(err, doc) { |
||||
|
if (err) return next(err); |
||||
|
res.send(doc); |
||||
|
}); |
||||
|
}; |
@ -0,0 +1,22 @@ |
|||||
|
|
||||
|
// import the necessary modules
|
||||
|
var mongoose = require('../../lib'); |
||||
|
var Schema = mongoose.Schema; |
||||
|
|
||||
|
// create an export function to encapsulate the model creation
|
||||
|
module.exports = function() { |
||||
|
// define schema
|
||||
|
// NOTE : This object must conform *precisely* to the geoJSON specification
|
||||
|
// you cannot embed a geoJSON doc inside a model or anything like that- IT
|
||||
|
// MUST BE VANILLA
|
||||
|
var LocationObject = new Schema({ |
||||
|
loc: { |
||||
|
type: {type: String}, |
||||
|
coordinates: [] |
||||
|
} |
||||
|
}); |
||||
|
// define the index
|
||||
|
LocationObject.index({loc: '2dsphere'}); |
||||
|
|
||||
|
mongoose.model('Location', LocationObject); |
||||
|
}; |
@ -0,0 +1,56 @@ |
|||||
|
// import async to make control flow simplier
|
||||
|
var async = require('async'); |
||||
|
|
||||
|
// import the rest of the normal stuff
|
||||
|
var mongoose = require('../../lib'); |
||||
|
|
||||
|
require('./geoJSONSchema.js')(); |
||||
|
|
||||
|
var Location = mongoose.model('Location'); |
||||
|
|
||||
|
// define some dummy data
|
||||
|
// note: the type can be Point, LineString, or Polygon
|
||||
|
var data = [ |
||||
|
{loc: {type: 'Point', coordinates: [-20.0, 5.0]}}, |
||||
|
{loc: {type: 'Point', coordinates: [6.0, 10.0]}}, |
||||
|
{loc: {type: 'Point', coordinates: [34.0, -50.0]}}, |
||||
|
{loc: {type: 'Point', coordinates: [-100.0, 70.0]}}, |
||||
|
{loc: {type: 'Point', coordinates: [38.0, 38.0]}} |
||||
|
]; |
||||
|
|
||||
|
|
||||
|
mongoose.connect('mongodb://localhost/locations', function(err) { |
||||
|
if (err) { |
||||
|
throw err; |
||||
|
} |
||||
|
|
||||
|
Location.on('index', function(err) { |
||||
|
if (err) { |
||||
|
throw err; |
||||
|
} |
||||
|
// create all of the dummy locations
|
||||
|
async.each(data, function(item, cb) { |
||||
|
Location.create(item, cb); |
||||
|
}, function(err) { |
||||
|
if (err) { |
||||
|
throw err; |
||||
|
} |
||||
|
// create the location we want to search for
|
||||
|
var coords = {type: 'Point', coordinates: [-5, 5]}; |
||||
|
// search for it
|
||||
|
Location.find({loc: {$near: coords}}).limit(1).exec(function(err, res) { |
||||
|
if (err) { |
||||
|
throw err; |
||||
|
} |
||||
|
console.log('Closest to %s is %s', JSON.stringify(coords), res); |
||||
|
cleanup(); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
function cleanup() { |
||||
|
Location.remove(function() { |
||||
|
mongoose.disconnect(); |
||||
|
}); |
||||
|
} |
@ -0,0 +1,100 @@ |
|||||
|
// import async to make control flow simplier
|
||||
|
var async = require('async'); |
||||
|
|
||||
|
// import the rest of the normal stuff
|
||||
|
var mongoose = require('../../lib'); |
||||
|
|
||||
|
require('./person.js')(); |
||||
|
|
||||
|
var Person = mongoose.model('Person'); |
||||
|
|
||||
|
// define some dummy data
|
||||
|
var data = [ |
||||
|
{ |
||||
|
name: 'bill', |
||||
|
age: 25, |
||||
|
birthday: new Date().setFullYear((new Date().getFullYear() - 25)), |
||||
|
gender: 'Male', |
||||
|
likes: ['movies', 'games', 'dogs'], |
||||
|
loc: [0, 0] |
||||
|
}, |
||||
|
{ |
||||
|
name: 'mary', |
||||
|
age: 30, |
||||
|
birthday: new Date().setFullYear((new Date().getFullYear() - 30)), |
||||
|
gender: 'Female', |
||||
|
likes: ['movies', 'birds', 'cats'], |
||||
|
loc: [1, 1] |
||||
|
}, |
||||
|
{ |
||||
|
name: 'bob', |
||||
|
age: 21, |
||||
|
birthday: new Date().setFullYear((new Date().getFullYear() - 21)), |
||||
|
gender: 'Male', |
||||
|
likes: ['tv', 'games', 'rabbits'], |
||||
|
loc: [3, 3] |
||||
|
}, |
||||
|
{ |
||||
|
name: 'lilly', |
||||
|
age: 26, |
||||
|
birthday: new Date().setFullYear((new Date().getFullYear() - 26)), |
||||
|
gender: 'Female', |
||||
|
likes: ['books', 'cats', 'dogs'], |
||||
|
loc: [6, 6] |
||||
|
}, |
||||
|
{ |
||||
|
name: 'alucard', |
||||
|
age: 1000, |
||||
|
birthday: new Date().setFullYear((new Date().getFullYear() - 1000)), |
||||
|
gender: 'Male', |
||||
|
likes: ['glasses', 'wine', 'the night'], |
||||
|
loc: [10, 10] |
||||
|
} |
||||
|
]; |
||||
|
|
||||
|
|
||||
|
mongoose.connect('mongodb://localhost/persons', function(err) { |
||||
|
if (err) { |
||||
|
throw err; |
||||
|
} |
||||
|
|
||||
|
// create all of the dummy people
|
||||
|
async.each(data, function(item, cb) { |
||||
|
Person.create(item, cb); |
||||
|
}, function(err) { |
||||
|
if (err) { |
||||
|
// handler error
|
||||
|
} |
||||
|
|
||||
|
// let's find the closest person to bob
|
||||
|
Person.find({name: 'bob'}, function(err, res) { |
||||
|
if (err) { |
||||
|
throw err; |
||||
|
} |
||||
|
|
||||
|
res[0].findClosest(function(err, closest) { |
||||
|
if (err) { |
||||
|
throw err; |
||||
|
} |
||||
|
|
||||
|
console.log('%s is closest to %s', res[0].name, closest); |
||||
|
|
||||
|
|
||||
|
// we can also just query straight off of the model. For more
|
||||
|
// information about geospatial queries and indexes, see
|
||||
|
// http://docs.mongodb.org/manual/applications/geospatial-indexes/
|
||||
|
var coords = [7, 7]; |
||||
|
Person.find({loc: {$nearSphere: coords}}).limit(1).exec(function(err, res) { |
||||
|
console.log('Closest to %s is %s', coords, res); |
||||
|
cleanup(); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
function cleanup() { |
||||
|
Person.remove(function() { |
||||
|
mongoose.disconnect(); |
||||
|
}); |
||||
|
} |
@ -0,0 +1,14 @@ |
|||||
|
{ |
||||
|
"name": "geospatial-example", |
||||
|
"private": "true", |
||||
|
"version": "0.0.0", |
||||
|
"description": "deps for geospatial example", |
||||
|
"main": "geospatial.js", |
||||
|
"scripts": { |
||||
|
"test": "echo \"Error: no test specified\" && exit 1" |
||||
|
}, |
||||
|
"dependencies": { "async": "*" }, |
||||
|
"repository": "", |
||||
|
"author": "", |
||||
|
"license": "BSD" |
||||
|
} |
@ -0,0 +1,27 @@ |
|||||
|
// import the necessary modules
|
||||
|
var mongoose = require('../../lib'); |
||||
|
var Schema = mongoose.Schema; |
||||
|
|
||||
|
// create an export function to encapsulate the model creation
|
||||
|
module.exports = function() { |
||||
|
// define schema
|
||||
|
var PersonSchema = new Schema({ |
||||
|
name: String, |
||||
|
age: Number, |
||||
|
birthday: Date, |
||||
|
gender: String, |
||||
|
likes: [String], |
||||
|
// define the geospatial field
|
||||
|
loc: {type: [Number], index: '2d'} |
||||
|
}); |
||||
|
|
||||
|
// define a method to find the closest person
|
||||
|
PersonSchema.methods.findClosest = function(cb) { |
||||
|
return this.model('Person').find({ |
||||
|
loc: {$nearSphere: this.loc}, |
||||
|
name: {$ne: this.name} |
||||
|
}).limit(1).exec(cb); |
||||
|
}; |
||||
|
|
||||
|
mongoose.model('Person', PersonSchema); |
||||
|
}; |
@ -0,0 +1,47 @@ |
|||||
|
var mongoose = require('../../lib'); |
||||
|
|
||||
|
|
||||
|
// import the global schema, this can be done in any file that needs the model
|
||||
|
require('./person.js')(); |
||||
|
|
||||
|
// grab the person model object
|
||||
|
var Person = mongoose.model('Person'); |
||||
|
|
||||
|
// connect to a server to do a quick write / read example
|
||||
|
|
||||
|
mongoose.connect('mongodb://localhost/persons', function(err) { |
||||
|
if (err) { |
||||
|
throw err; |
||||
|
} |
||||
|
|
||||
|
Person.create({ |
||||
|
name: 'bill', |
||||
|
age: 25, |
||||
|
birthday: new Date().setFullYear((new Date().getFullYear() - 25)) |
||||
|
}, function(err, bill) { |
||||
|
if (err) { |
||||
|
throw err; |
||||
|
} |
||||
|
console.log('People added to db: %s', bill.toString()); |
||||
|
Person.find({}, function(err, people) { |
||||
|
if (err) { |
||||
|
throw err; |
||||
|
} |
||||
|
|
||||
|
people.forEach(function(person) { |
||||
|
console.log('People in the db: %s', person.toString()); |
||||
|
}); |
||||
|
|
||||
|
// make sure to clean things up after we're done
|
||||
|
setTimeout(function() { |
||||
|
cleanup(); |
||||
|
}, 2000); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
function cleanup() { |
||||
|
Person.remove(function() { |
||||
|
mongoose.disconnect(); |
||||
|
}); |
||||
|
} |
@ -0,0 +1,14 @@ |
|||||
|
// import the necessary modules
|
||||
|
var mongoose = require('../../lib'); |
||||
|
var Schema = mongoose.Schema; |
||||
|
|
||||
|
// create an export function to encapsulate the model creation
|
||||
|
module.exports = function() { |
||||
|
// define schema
|
||||
|
var PersonSchema = new Schema({ |
||||
|
name: String, |
||||
|
age: Number, |
||||
|
birthday: Date |
||||
|
}); |
||||
|
mongoose.model('Person', PersonSchema); |
||||
|
}; |
@ -0,0 +1,84 @@ |
|||||
|
|
||||
|
// import async to make control flow simplier
|
||||
|
var async = require('async'); |
||||
|
|
||||
|
// import the rest of the normal stuff
|
||||
|
var mongoose = require('../../lib'); |
||||
|
|
||||
|
require('./person.js')(); |
||||
|
|
||||
|
var Person = mongoose.model('Person'); |
||||
|
|
||||
|
// define some dummy data
|
||||
|
var data = [ |
||||
|
{ |
||||
|
name: 'bill', |
||||
|
age: 25, |
||||
|
birthday: new Date().setFullYear((new Date().getFullYear() - 25)), |
||||
|
gender: 'Male', |
||||
|
likes: ['movies', 'games', 'dogs'] |
||||
|
}, |
||||
|
{ |
||||
|
name: 'mary', |
||||
|
age: 30, |
||||
|
birthday: new Date().setFullYear((new Date().getFullYear() - 30)), |
||||
|
gender: 'Female', |
||||
|
likes: ['movies', 'birds', 'cats'] |
||||
|
}, |
||||
|
{ |
||||
|
name: 'bob', |
||||
|
age: 21, |
||||
|
birthday: new Date().setFullYear((new Date().getFullYear() - 21)), |
||||
|
gender: 'Male', |
||||
|
likes: ['tv', 'games', 'rabbits'] |
||||
|
}, |
||||
|
{ |
||||
|
name: 'lilly', |
||||
|
age: 26, |
||||
|
birthday: new Date().setFullYear((new Date().getFullYear() - 26)), |
||||
|
gender: 'Female', |
||||
|
likes: ['books', 'cats', 'dogs'] |
||||
|
}, |
||||
|
{ |
||||
|
name: 'alucard', |
||||
|
age: 1000, |
||||
|
birthday: new Date().setFullYear((new Date().getFullYear() - 1000)), |
||||
|
gender: 'Male', |
||||
|
likes: ['glasses', 'wine', 'the night'] |
||||
|
} |
||||
|
]; |
||||
|
|
||||
|
|
||||
|
mongoose.connect('mongodb://localhost/persons', function(err) { |
||||
|
if (err) throw err; |
||||
|
|
||||
|
// create all of the dummy people
|
||||
|
async.each(data, function(item, cb) { |
||||
|
Person.create(item, cb); |
||||
|
}, function(err) { |
||||
|
if (err) { |
||||
|
// handle error
|
||||
|
} |
||||
|
|
||||
|
// lean queries return just plain javascript objects, not
|
||||
|
// MongooseDocuments. This makes them good for high performance read
|
||||
|
// situations
|
||||
|
|
||||
|
// when using .lean() the default is true, but you can explicitly set the
|
||||
|
// value by passing in a boolean value. IE. .lean(false)
|
||||
|
var q = Person.find({age: {$lt: 1000}}).sort('age').limit(2).lean(); |
||||
|
q.exec(function(err, results) { |
||||
|
if (err) throw err; |
||||
|
console.log('Are the results MongooseDocuments?: %s', results[0] instanceof mongoose.Document); |
||||
|
|
||||
|
console.log(results); |
||||
|
cleanup(); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
function cleanup() { |
||||
|
Person.remove(function() { |
||||
|
mongoose.disconnect(); |
||||
|
}); |
||||
|
} |
@ -0,0 +1,14 @@ |
|||||
|
{ |
||||
|
"name": "lean-example", |
||||
|
"private": "true", |
||||
|
"version": "0.0.0", |
||||
|
"description": "deps for lean example", |
||||
|
"main": "lean.js", |
||||
|
"scripts": { |
||||
|
"test": "echo \"Error: no test specified\" && exit 1" |
||||
|
}, |
||||
|
"dependencies": { "async": "*" }, |
||||
|
"repository": "", |
||||
|
"author": "", |
||||
|
"license": "BSD" |
||||
|
} |
@ -0,0 +1,16 @@ |
|||||
|
// import the necessary modules
|
||||
|
var mongoose = require('../../lib'); |
||||
|
var Schema = mongoose.Schema; |
||||
|
|
||||
|
// create an export function to encapsulate the model creation
|
||||
|
module.exports = function() { |
||||
|
// define schema
|
||||
|
var PersonSchema = new Schema({ |
||||
|
name: String, |
||||
|
age: Number, |
||||
|
birthday: Date, |
||||
|
gender: String, |
||||
|
likes: [String] |
||||
|
}); |
||||
|
mongoose.model('Person', PersonSchema); |
||||
|
}; |
@ -0,0 +1,100 @@ |
|||||
|
// import async to make control flow simplier
|
||||
|
var async = require('async'); |
||||
|
|
||||
|
// import the rest of the normal stuff
|
||||
|
var mongoose = require('../../lib'); |
||||
|
|
||||
|
require('./person.js')(); |
||||
|
|
||||
|
var Person = mongoose.model('Person'); |
||||
|
|
||||
|
// define some dummy data
|
||||
|
var data = [ |
||||
|
{ |
||||
|
name: 'bill', |
||||
|
age: 25, |
||||
|
birthday: new Date().setFullYear((new Date().getFullYear() - 25)), |
||||
|
gender: 'Male' |
||||
|
}, |
||||
|
{ |
||||
|
name: 'mary', |
||||
|
age: 30, |
||||
|
birthday: new Date().setFullYear((new Date().getFullYear() - 30)), |
||||
|
gender: 'Female' |
||||
|
}, |
||||
|
{ |
||||
|
name: 'bob', |
||||
|
age: 21, |
||||
|
birthday: new Date().setFullYear((new Date().getFullYear() - 21)), |
||||
|
gender: 'Male' |
||||
|
}, |
||||
|
{ |
||||
|
name: 'lilly', |
||||
|
age: 26, |
||||
|
birthday: new Date().setFullYear((new Date().getFullYear() - 26)), |
||||
|
gender: 'Female' |
||||
|
}, |
||||
|
{ |
||||
|
name: 'alucard', |
||||
|
age: 1000, |
||||
|
birthday: new Date().setFullYear((new Date().getFullYear() - 1000)), |
||||
|
gender: 'Male' |
||||
|
} |
||||
|
]; |
||||
|
|
||||
|
|
||||
|
mongoose.connect('mongodb://localhost/persons', function(err) { |
||||
|
if (err) throw err; |
||||
|
|
||||
|
// create all of the dummy people
|
||||
|
async.each(data, function(item, cb) { |
||||
|
Person.create(item, cb); |
||||
|
}, function(err) { |
||||
|
if (err) { |
||||
|
// handle error
|
||||
|
} |
||||
|
|
||||
|
// alright, simple map reduce example. We will find the total ages of each
|
||||
|
// gender
|
||||
|
|
||||
|
// create the options object
|
||||
|
var o = {}; |
||||
|
|
||||
|
o.map = function() { |
||||
|
// in this function, 'this' refers to the current document being
|
||||
|
// processed. Return the (gender, age) tuple using
|
||||
|
/* global emit */ |
||||
|
emit(this.gender, this.age); |
||||
|
}; |
||||
|
|
||||
|
// the reduce function receives the array of ages that are grouped by the
|
||||
|
// id, which in this case is the gender
|
||||
|
o.reduce = function(id, ages) { |
||||
|
return Array.sum(ages); |
||||
|
}; |
||||
|
|
||||
|
// other options that can be specified
|
||||
|
|
||||
|
// o.query = { age : { $lt : 1000 }}; // the query object
|
||||
|
// o.limit = 3; // max number of documents
|
||||
|
// o.keeptemp = true; // default is false, specifies whether to keep temp data
|
||||
|
// o.finalize = someFunc; // function called after reduce
|
||||
|
// o.scope = {}; // the scope variable exposed to map/reduce/finalize
|
||||
|
// o.jsMode = true; // default is false, force execution to stay in JS
|
||||
|
o.verbose = true; // default is false, provide stats on the job
|
||||
|
// o.out = {}; // objects to specify where output goes, by default is
|
||||
|
// returned, but can also be stored in a new collection
|
||||
|
// see: http://mongoosejs.com/docs/api.html#model_Model.mapReduce
|
||||
|
Person.mapReduce(o, function(err, results, stats) { |
||||
|
console.log('map reduce took %d ms', stats.processtime); |
||||
|
console.log(results); |
||||
|
cleanup(); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
function cleanup() { |
||||
|
Person.remove(function() { |
||||
|
mongoose.disconnect(); |
||||
|
}); |
||||
|
} |
@ -0,0 +1,14 @@ |
|||||
|
{ |
||||
|
"name": "map-reduce-example", |
||||
|
"private": "true", |
||||
|
"version": "0.0.0", |
||||
|
"description": "deps for map reduce example", |
||||
|
"main": "mapreduce.js", |
||||
|
"scripts": { |
||||
|
"test": "echo \"Error: no test specified\" && exit 1" |
||||
|
}, |
||||
|
"dependencies": { "async": "*" }, |
||||
|
"repository": "", |
||||
|
"author": "", |
||||
|
"license": "BSD" |
||||
|
} |
@ -0,0 +1,16 @@ |
|||||
|
|
||||
|
// import the necessary modules
|
||||
|
var mongoose = require('../../lib'); |
||||
|
var Schema = mongoose.Schema; |
||||
|
|
||||
|
// create an export function to encapsulate the model creation
|
||||
|
module.exports = function() { |
||||
|
// define schema
|
||||
|
var PersonSchema = new Schema({ |
||||
|
name: String, |
||||
|
age: Number, |
||||
|
birthday: Date, |
||||
|
gender: String |
||||
|
}); |
||||
|
mongoose.model('Person', PersonSchema); |
||||
|
}; |
@ -1,95 +0,0 @@ |
|||||
|
|
||||
var mongoose = require('mongoose') |
|
||||
var Schema = mongoose.Schema; |
|
||||
|
|
||||
console.log('Running mongoose version %s', mongoose.version); |
|
||||
|
|
||||
/** |
|
||||
* Console schema |
|
||||
*/ |
|
||||
|
|
||||
var consoleSchema = Schema({ |
|
||||
name: String |
|
||||
, manufacturer: String |
|
||||
, released: Date |
|
||||
}) |
|
||||
var Console = mongoose.model('Console', consoleSchema); |
|
||||
|
|
||||
/** |
|
||||
* Game schema |
|
||||
*/ |
|
||||
|
|
||||
var gameSchema = Schema({ |
|
||||
name: String |
|
||||
, developer: String |
|
||||
, released: Date |
|
||||
, consoles: [{ type: Schema.Types.ObjectId, ref: 'Console' }] |
|
||||
}) |
|
||||
var Game = mongoose.model('Game', gameSchema); |
|
||||
|
|
||||
/** |
|
||||
* Connect to the console database on localhost with |
|
||||
* the default port (27017) |
|
||||
*/ |
|
||||
|
|
||||
mongoose.connect('mongodb://localhost/console', function (err) { |
|
||||
// if we failed to connect, abort
|
|
||||
if (err) throw err; |
|
||||
|
|
||||
// we connected ok
|
|
||||
createData(); |
|
||||
}) |
|
||||
|
|
||||
/** |
|
||||
* Data generation |
|
||||
*/ |
|
||||
|
|
||||
function createData () { |
|
||||
Console.create({ |
|
||||
name: 'Nintendo 64' |
|
||||
, manufacturer: 'Nintendo' |
|
||||
, released: 'September 29, 1996' |
|
||||
}, function (err, nintendo64) { |
|
||||
if (err) return done(err); |
|
||||
|
|
||||
Game.create({ |
|
||||
name: 'Legend of Zelda: Ocarina of Time' |
|
||||
, developer: 'Nintendo' |
|
||||
, released: new Date('November 21, 1998') |
|
||||
, consoles: [nintendo64] |
|
||||
}, function (err) { |
|
||||
if (err) return done(err); |
|
||||
example(); |
|
||||
}) |
|
||||
}) |
|
||||
} |
|
||||
|
|
||||
/** |
|
||||
* Population |
|
||||
*/ |
|
||||
|
|
||||
function example () { |
|
||||
Game |
|
||||
.findOne({ name: /^Legend of Zelda/ }) |
|
||||
.populate('consoles') |
|
||||
.exec(function (err, ocinara) { |
|
||||
if (err) return done(err); |
|
||||
|
|
||||
console.log( |
|
||||
'"%s" was released for the %s on %s' |
|
||||
, ocinara.name |
|
||||
, ocinara.consoles[0].name |
|
||||
, ocinara.released.toLocaleDateString()); |
|
||||
|
|
||||
done(); |
|
||||
}) |
|
||||
} |
|
||||
|
|
||||
function done (err) { |
|
||||
if (err) console.error(err); |
|
||||
Console.remove(function () { |
|
||||
Game.remove(function () { |
|
||||
mongoose.disconnect(); |
|
||||
}) |
|
||||
}) |
|
||||
} |
|
@ -1,101 +0,0 @@ |
|||||
|
|
||||
var mongoose = require('mongoose') |
|
||||
var Schema = mongoose.Schema; |
|
||||
|
|
||||
console.log('Running mongoose version %s', mongoose.version); |
|
||||
|
|
||||
/** |
|
||||
* Console schema |
|
||||
*/ |
|
||||
|
|
||||
var consoleSchema = Schema({ |
|
||||
name: String |
|
||||
, manufacturer: String |
|
||||
, released: Date |
|
||||
}) |
|
||||
var Console = mongoose.model('Console', consoleSchema); |
|
||||
|
|
||||
/** |
|
||||
* Game schema |
|
||||
*/ |
|
||||
|
|
||||
var gameSchema = Schema({ |
|
||||
name: String |
|
||||
, developer: String |
|
||||
, released: Date |
|
||||
, consoles: [{ type: Schema.Types.ObjectId, ref: 'Console' }] |
|
||||
}) |
|
||||
var Game = mongoose.model('Game', gameSchema); |
|
||||
|
|
||||
/** |
|
||||
* Connect to the console database on localhost with |
|
||||
* the default port (27017) |
|
||||
*/ |
|
||||
|
|
||||
mongoose.connect('mongodb://localhost/console', function (err) { |
|
||||
// if we failed to connect, abort
|
|
||||
if (err) throw err; |
|
||||
|
|
||||
// we connected ok
|
|
||||
createData(); |
|
||||
}) |
|
||||
|
|
||||
/** |
|
||||
* Data generation |
|
||||
*/ |
|
||||
|
|
||||
function createData () { |
|
||||
Console.create({ |
|
||||
name: 'Nintendo 64' |
|
||||
, manufacturer: 'Nintendo' |
|
||||
, released: 'September 29, 1996' |
|
||||
}, function (err, nintendo64) { |
|
||||
if (err) return done(err); |
|
||||
|
|
||||
Game.create({ |
|
||||
name: 'Legend of Zelda: Ocarina of Time' |
|
||||
, developer: 'Nintendo' |
|
||||
, released: new Date('November 21, 1998') |
|
||||
, consoles: [nintendo64] |
|
||||
}, function (err) { |
|
||||
if (err) return done(err); |
|
||||
example(); |
|
||||
}) |
|
||||
}) |
|
||||
} |
|
||||
|
|
||||
/** |
|
||||
* Population |
|
||||
*/ |
|
||||
|
|
||||
function example () { |
|
||||
Game |
|
||||
.findOne({ name: /^Legend of Zelda/ }) |
|
||||
.exec(function (err, ocinara) { |
|
||||
if (err) return done(err); |
|
||||
|
|
||||
console.log('"%s" console _id: %s', ocinara.name, ocinara.consoles[0]); |
|
||||
|
|
||||
// population of existing document
|
|
||||
ocinara.populate('consoles', function (err) { |
|
||||
if (err) return done(err); |
|
||||
|
|
||||
console.log( |
|
||||
'"%s" was released for the %s on %s' |
|
||||
, ocinara.name |
|
||||
, ocinara.consoles[0].name |
|
||||
, ocinara.released.toLocaleDateString()); |
|
||||
|
|
||||
done(); |
|
||||
}) |
|
||||
}) |
|
||||
} |
|
||||
|
|
||||
function done (err) { |
|
||||
if (err) console.error(err); |
|
||||
Console.remove(function () { |
|
||||
Game.remove(function () { |
|
||||
mongoose.disconnect(); |
|
||||
}) |
|
||||
}) |
|
||||
} |
|
@ -1,112 +0,0 @@ |
|||||
|
|
||||
var mongoose = require('mongoose') |
|
||||
var Schema = mongoose.Schema; |
|
||||
|
|
||||
console.log('Running mongoose version %s', mongoose.version); |
|
||||
|
|
||||
/** |
|
||||
* Console schema |
|
||||
*/ |
|
||||
|
|
||||
var consoleSchema = Schema({ |
|
||||
name: String |
|
||||
, manufacturer: String |
|
||||
, released: Date |
|
||||
}) |
|
||||
var Console = mongoose.model('Console', consoleSchema); |
|
||||
|
|
||||
/** |
|
||||
* Game schema |
|
||||
*/ |
|
||||
|
|
||||
var gameSchema = Schema({ |
|
||||
name: String |
|
||||
, developer: String |
|
||||
, released: Date |
|
||||
, consoles: [{ type: Schema.Types.ObjectId, ref: 'Console' }] |
|
||||
}) |
|
||||
var Game = mongoose.model('Game', gameSchema); |
|
||||
|
|
||||
/** |
|
||||
* Connect to the console database on localhost with |
|
||||
* the default port (27017) |
|
||||
*/ |
|
||||
|
|
||||
mongoose.connect('mongodb://localhost/console', function (err) { |
|
||||
// if we failed to connect, abort
|
|
||||
if (err) throw err; |
|
||||
|
|
||||
// we connected ok
|
|
||||
createData(); |
|
||||
}) |
|
||||
|
|
||||
/** |
|
||||
* Data generation |
|
||||
*/ |
|
||||
|
|
||||
function createData () { |
|
||||
Console.create({ |
|
||||
name: 'Nintendo 64' |
|
||||
, manufacturer: 'Nintendo' |
|
||||
, released: 'September 29, 1996' |
|
||||
}, { |
|
||||
name: 'Super Nintendo' |
|
||||
, manufacturer: 'Nintendo' |
|
||||
, released: 'August 23, 1991' |
|
||||
}, function (err, nintendo64, superNintendo) { |
|
||||
if (err) return done(err); |
|
||||
|
|
||||
Game.create({ |
|
||||
name: 'Legend of Zelda: Ocarina of Time' |
|
||||
, developer: 'Nintendo' |
|
||||
, released: new Date('November 21, 1998') |
|
||||
, consoles: [nintendo64] |
|
||||
}, { |
|
||||
name: 'Mario Kart' |
|
||||
, developer: 'Nintendo' |
|
||||
, released: 'September 1, 1992' |
|
||||
, consoles: [superNintendo] |
|
||||
}, function (err) { |
|
||||
if (err) return done(err); |
|
||||
example(); |
|
||||
}) |
|
||||
}) |
|
||||
} |
|
||||
|
|
||||
/** |
|
||||
* Population |
|
||||
*/ |
|
||||
|
|
||||
function example () { |
|
||||
Game |
|
||||
.find({}) |
|
||||
.exec(function (err, games) { |
|
||||
if (err) return done(err); |
|
||||
|
|
||||
console.log('found %d games', games.length); |
|
||||
|
|
||||
var options = { path: 'consoles', select: 'name released -_id' }; |
|
||||
Game.populate(games, options, function (err, games) { |
|
||||
if (err) return done(err); |
|
||||
|
|
||||
games.forEach(function (game) { |
|
||||
console.log( |
|
||||
'"%s" was released for the %s on %s' |
|
||||
, game.name |
|
||||
, game.consoles[0].name |
|
||||
, game.released.toLocaleDateString()); |
|
||||
}) |
|
||||
|
|
||||
done() |
|
||||
}) |
|
||||
}) |
|
||||
} |
|
||||
|
|
||||
function done (err) { |
|
||||
if (err) console.error(err); |
|
||||
Console.remove(function () { |
|
||||
Game.remove(function () { |
|
||||
mongoose.disconnect(); |
|
||||
}) |
|
||||
}) |
|
||||
} |
|
@ -1,124 +0,0 @@ |
|||||
|
|
||||
var mongoose = require('mongoose') |
|
||||
var Schema = mongoose.Schema; |
|
||||
|
|
||||
console.log('Running mongoose version %s', mongoose.version); |
|
||||
|
|
||||
/** |
|
||||
* Console schema |
|
||||
*/ |
|
||||
|
|
||||
var consoleSchema = Schema({ |
|
||||
name: String |
|
||||
, manufacturer: String |
|
||||
, released: Date |
|
||||
}) |
|
||||
var Console = mongoose.model('Console', consoleSchema); |
|
||||
|
|
||||
/** |
|
||||
* Game schema |
|
||||
*/ |
|
||||
|
|
||||
var gameSchema = Schema({ |
|
||||
name: String |
|
||||
, developer: String |
|
||||
, released: Date |
|
||||
, consoles: [{ type: Schema.Types.ObjectId, ref: 'Console' }] |
|
||||
}) |
|
||||
var Game = mongoose.model('Game', gameSchema); |
|
||||
|
|
||||
/** |
|
||||
* Connect to the console database on localhost with |
|
||||
* the default port (27017) |
|
||||
*/ |
|
||||
|
|
||||
mongoose.connect('mongodb://localhost/console', function (err) { |
|
||||
// if we failed to connect, abort
|
|
||||
if (err) throw err; |
|
||||
|
|
||||
// we connected ok
|
|
||||
createData(); |
|
||||
}) |
|
||||
|
|
||||
/** |
|
||||
* Data generation |
|
||||
*/ |
|
||||
|
|
||||
function createData () { |
|
||||
Console.create({ |
|
||||
name: 'Nintendo 64' |
|
||||
, manufacturer: 'Nintendo' |
|
||||
, released: 'September 29, 1996' |
|
||||
}, { |
|
||||
name: 'Super Nintendo' |
|
||||
, manufacturer: 'Nintendo' |
|
||||
, released: 'August 23, 1991' |
|
||||
}, { |
|
||||
name: 'XBOX 360' |
|
||||
, manufacturer: 'Microsoft' |
|
||||
, released: 'November 22, 2005' |
|
||||
}, function (err, nintendo64, superNintendo, xbox360) { |
|
||||
if (err) return done(err); |
|
||||
|
|
||||
Game.create({ |
|
||||
name: 'Legend of Zelda: Ocarina of Time' |
|
||||
, developer: 'Nintendo' |
|
||||
, released: new Date('November 21, 1998') |
|
||||
, consoles: [nintendo64] |
|
||||
}, { |
|
||||
name: 'Mario Kart' |
|
||||
, developer: 'Nintendo' |
|
||||
, released: 'September 1, 1992' |
|
||||
, consoles: [superNintendo] |
|
||||
}, { |
|
||||
name: 'Perfect Dark Zero' |
|
||||
, developer: 'Rare' |
|
||||
, released: 'November 17, 2005' |
|
||||
, consoles: [xbox360] |
|
||||
}, function (err) { |
|
||||
if (err) return done(err); |
|
||||
example(); |
|
||||
}) |
|
||||
}) |
|
||||
} |
|
||||
|
|
||||
/** |
|
||||
* Population |
|
||||
*/ |
|
||||
|
|
||||
function example () { |
|
||||
Game |
|
||||
.find({}) |
|
||||
.populate({ |
|
||||
path: 'consoles' |
|
||||
, match: { manufacturer: 'Nintendo' } |
|
||||
, select: 'name' |
|
||||
, options: { comment: 'population' } |
|
||||
}) |
|
||||
.exec(function (err, games) { |
|
||||
if (err) return done(err); |
|
||||
|
|
||||
games.forEach(function (game) { |
|
||||
console.log( |
|
||||
'"%s" was released for the %s on %s' |
|
||||
, game.name |
|
||||
, game.consoles.length ? game.consoles[0].name : '??' |
|
||||
, game.released.toLocaleDateString()); |
|
||||
}) |
|
||||
|
|
||||
return done(); |
|
||||
}) |
|
||||
} |
|
||||
|
|
||||
/** |
|
||||
* Clean up |
|
||||
*/ |
|
||||
|
|
||||
function done (err) { |
|
||||
if (err) console.error(err); |
|
||||
Console.remove(function () { |
|
||||
Game.remove(function () { |
|
||||
mongoose.disconnect(); |
|
||||
}) |
|
||||
}) |
|
||||
} |
|
@ -1,96 +0,0 @@ |
|||||
|
|
||||
var mongoose = require('mongoose') |
|
||||
var Schema = mongoose.Schema; |
|
||||
|
|
||||
console.log('Running mongoose version %s', mongoose.version); |
|
||||
|
|
||||
/** |
|
||||
* Console schema |
|
||||
*/ |
|
||||
|
|
||||
var consoleSchema = Schema({ |
|
||||
name: String |
|
||||
, manufacturer: String |
|
||||
, released: Date |
|
||||
}) |
|
||||
var Console = mongoose.model('Console', consoleSchema); |
|
||||
|
|
||||
/** |
|
||||
* Game schema |
|
||||
*/ |
|
||||
|
|
||||
var gameSchema = Schema({ |
|
||||
name: String |
|
||||
, developer: String |
|
||||
, released: Date |
|
||||
, consoles: [{ type: Schema.Types.ObjectId, ref: 'Console' }] |
|
||||
}) |
|
||||
var Game = mongoose.model('Game', gameSchema); |
|
||||
|
|
||||
/** |
|
||||
* Connect to the console database on localhost with |
|
||||
* the default port (27017) |
|
||||
*/ |
|
||||
|
|
||||
mongoose.connect('mongodb://localhost/console', function (err) { |
|
||||
// if we failed to connect, abort
|
|
||||
if (err) throw err; |
|
||||
|
|
||||
// we connected ok
|
|
||||
createData(); |
|
||||
}) |
|
||||
|
|
||||
/** |
|
||||
* Data generation |
|
||||
*/ |
|
||||
|
|
||||
function createData () { |
|
||||
Console.create({ |
|
||||
name: 'Nintendo 64' |
|
||||
, manufacturer: 'Nintendo' |
|
||||
, released: 'September 29, 1996' |
|
||||
}, function (err, nintendo64) { |
|
||||
if (err) return done(err); |
|
||||
|
|
||||
Game.create({ |
|
||||
name: 'Legend of Zelda: Ocarina of Time' |
|
||||
, developer: 'Nintendo' |
|
||||
, released: new Date('November 21, 1998') |
|
||||
, consoles: [nintendo64] |
|
||||
}, function (err) { |
|
||||
if (err) return done(err); |
|
||||
example(); |
|
||||
}) |
|
||||
}) |
|
||||
} |
|
||||
|
|
||||
/** |
|
||||
* Population |
|
||||
*/ |
|
||||
|
|
||||
function example () { |
|
||||
Game |
|
||||
.findOne({ name: /^Legend of Zelda/ }) |
|
||||
.populate('consoles') |
|
||||
.lean() // just return plain objects, not documents wrapped by mongoose
|
|
||||
.exec(function (err, ocinara) { |
|
||||
if (err) return done(err); |
|
||||
|
|
||||
console.log( |
|
||||
'"%s" was released for the %s on %s' |
|
||||
, ocinara.name |
|
||||
, ocinara.consoles[0].name |
|
||||
, ocinara.released.toLocaleDateString()); |
|
||||
|
|
||||
done(); |
|
||||
}) |
|
||||
} |
|
||||
|
|
||||
function done (err) { |
|
||||
if (err) console.error(err); |
|
||||
Console.remove(function () { |
|
||||
Game.remove(function () { |
|
||||
mongoose.disconnect(); |
|
||||
}) |
|
||||
}) |
|
||||
} |
|
@ -0,0 +1,103 @@ |
|||||
|
|
||||
|
var mongoose = require('../../lib'); |
||||
|
var Schema = mongoose.Schema; |
||||
|
|
||||
|
console.log('Running mongoose version %s', mongoose.version); |
||||
|
|
||||
|
/** |
||||
|
* Console schema |
||||
|
*/ |
||||
|
|
||||
|
var consoleSchema = Schema({ |
||||
|
name: String, |
||||
|
manufacturer: String, |
||||
|
released: Date |
||||
|
}); |
||||
|
var Console = mongoose.model('Console', consoleSchema); |
||||
|
|
||||
|
/** |
||||
|
* Game schema |
||||
|
*/ |
||||
|
|
||||
|
var gameSchema = Schema({ |
||||
|
name: String, |
||||
|
developer: String, |
||||
|
released: Date, |
||||
|
consoles: [{ |
||||
|
type: Schema.Types.ObjectId, |
||||
|
ref: 'Console' |
||||
|
}] |
||||
|
}); |
||||
|
var Game = mongoose.model('Game', gameSchema); |
||||
|
|
||||
|
/** |
||||
|
* Connect to the console database on localhost with |
||||
|
* the default port (27017) |
||||
|
*/ |
||||
|
|
||||
|
mongoose.connect('mongodb://localhost/console', function(err) { |
||||
|
// if we failed to connect, abort
|
||||
|
if (err) throw err; |
||||
|
|
||||
|
// we connected ok
|
||||
|
createData(); |
||||
|
}); |
||||
|
|
||||
|
/** |
||||
|
* Data generation |
||||
|
*/ |
||||
|
|
||||
|
function createData() { |
||||
|
Console.create( |
||||
|
{ |
||||
|
name: 'Nintendo 64', |
||||
|
manufacturer: 'Nintendo', |
||||
|
released: 'September 29, 1996' |
||||
|
}, |
||||
|
function(err, nintendo64) { |
||||
|
if (err) return done(err); |
||||
|
|
||||
|
Game.create({ |
||||
|
name: 'Legend of Zelda: Ocarina of Time', |
||||
|
developer: 'Nintendo', |
||||
|
released: new Date('November 21, 1998'), |
||||
|
consoles: [nintendo64] |
||||
|
}, |
||||
|
function(err) { |
||||
|
if (err) return done(err); |
||||
|
example(); |
||||
|
}); |
||||
|
} |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Population |
||||
|
*/ |
||||
|
|
||||
|
function example() { |
||||
|
Game |
||||
|
.findOne({name: /^Legend of Zelda/}) |
||||
|
.populate('consoles') |
||||
|
.exec(function(err, ocinara) { |
||||
|
if (err) return done(err); |
||||
|
|
||||
|
console.log( |
||||
|
'"%s" was released for the %s on %s', |
||||
|
ocinara.name, |
||||
|
ocinara.consoles[0].name, |
||||
|
ocinara.released.toLocaleDateString() |
||||
|
); |
||||
|
|
||||
|
done(); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
function done(err) { |
||||
|
if (err) console.error(err); |
||||
|
Console.remove(function() { |
||||
|
Game.remove(function() { |
||||
|
mongoose.disconnect(); |
||||
|
}); |
||||
|
}); |
||||
|
} |
@ -0,0 +1,109 @@ |
|||||
|
|
||||
|
var mongoose = require('../../lib'); |
||||
|
var Schema = mongoose.Schema; |
||||
|
|
||||
|
console.log('Running mongoose version %s', mongoose.version); |
||||
|
|
||||
|
/** |
||||
|
* Console schema |
||||
|
*/ |
||||
|
|
||||
|
var consoleSchema = Schema({ |
||||
|
name: String, |
||||
|
manufacturer: String, |
||||
|
released: Date |
||||
|
}); |
||||
|
var Console = mongoose.model('Console', consoleSchema); |
||||
|
|
||||
|
/** |
||||
|
* Game schema |
||||
|
*/ |
||||
|
|
||||
|
var gameSchema = Schema({ |
||||
|
name: String, |
||||
|
developer: String, |
||||
|
released: Date, |
||||
|
consoles: [{ |
||||
|
type: Schema.Types.ObjectId, |
||||
|
ref: 'Console' |
||||
|
}] |
||||
|
}); |
||||
|
var Game = mongoose.model('Game', gameSchema); |
||||
|
|
||||
|
/** |
||||
|
* Connect to the console database on localhost with |
||||
|
* the default port (27017) |
||||
|
*/ |
||||
|
|
||||
|
mongoose.connect('mongodb://localhost/console', function(err) { |
||||
|
// if we failed to connect, abort
|
||||
|
if (err) throw err; |
||||
|
|
||||
|
// we connected ok
|
||||
|
createData(); |
||||
|
}); |
||||
|
|
||||
|
/** |
||||
|
* Data generation |
||||
|
*/ |
||||
|
|
||||
|
function createData() { |
||||
|
Console.create( |
||||
|
{ |
||||
|
name: 'Nintendo 64', |
||||
|
manufacturer: 'Nintendo', |
||||
|
released: 'September 29, 1996' |
||||
|
}, |
||||
|
function(err, nintendo64) { |
||||
|
if (err) return done(err); |
||||
|
|
||||
|
Game.create({ |
||||
|
name: 'Legend of Zelda: Ocarina of Time', |
||||
|
developer: 'Nintendo', |
||||
|
released: new Date('November 21, 1998'), |
||||
|
consoles: [nintendo64] |
||||
|
}, |
||||
|
function(err) { |
||||
|
if (err) return done(err); |
||||
|
example(); |
||||
|
}); |
||||
|
} |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Population |
||||
|
*/ |
||||
|
|
||||
|
function example() { |
||||
|
Game |
||||
|
.findOne({name: /^Legend of Zelda/}) |
||||
|
.exec(function(err, ocinara) { |
||||
|
if (err) return done(err); |
||||
|
|
||||
|
console.log('"%s" console _id: %s', ocinara.name, ocinara.consoles[0]); |
||||
|
|
||||
|
// population of existing document
|
||||
|
ocinara.populate('consoles', function(err) { |
||||
|
if (err) return done(err); |
||||
|
|
||||
|
console.log( |
||||
|
'"%s" was released for the %s on %s', |
||||
|
ocinara.name, |
||||
|
ocinara.consoles[0].name, |
||||
|
ocinara.released.toLocaleDateString() |
||||
|
); |
||||
|
|
||||
|
done(); |
||||
|
}); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
function done(err) { |
||||
|
if (err) console.error(err); |
||||
|
Console.remove(function() { |
||||
|
Game.remove(function() { |
||||
|
mongoose.disconnect(); |
||||
|
}); |
||||
|
}); |
||||
|
} |
@ -0,0 +1,124 @@ |
|||||
|
|
||||
|
var mongoose = require('../../lib'); |
||||
|
var Schema = mongoose.Schema; |
||||
|
|
||||
|
console.log('Running mongoose version %s', mongoose.version); |
||||
|
|
||||
|
/** |
||||
|
* Console schema |
||||
|
*/ |
||||
|
|
||||
|
var consoleSchema = Schema({ |
||||
|
name: String, |
||||
|
manufacturer: String, |
||||
|
released: Date |
||||
|
}); |
||||
|
var Console = mongoose.model('Console', consoleSchema); |
||||
|
|
||||
|
/** |
||||
|
* Game schema |
||||
|
*/ |
||||
|
|
||||
|
var gameSchema = Schema({ |
||||
|
name: String, |
||||
|
developer: String, |
||||
|
released: Date, |
||||
|
consoles: [{ |
||||
|
type: Schema.Types.ObjectId, |
||||
|
ref: 'Console' |
||||
|
}] |
||||
|
}); |
||||
|
var Game = mongoose.model('Game', gameSchema); |
||||
|
|
||||
|
/** |
||||
|
* Connect to the console database on localhost with |
||||
|
* the default port (27017) |
||||
|
*/ |
||||
|
|
||||
|
mongoose.connect('mongodb://localhost/console', function(err) { |
||||
|
// if we failed to connect, abort
|
||||
|
if (err) throw err; |
||||
|
|
||||
|
// we connected ok
|
||||
|
createData(); |
||||
|
}); |
||||
|
|
||||
|
/** |
||||
|
* Data generation |
||||
|
*/ |
||||
|
|
||||
|
function createData() { |
||||
|
Console.create( |
||||
|
{ |
||||
|
name: 'Nintendo 64', |
||||
|
manufacturer: 'Nintendo', |
||||
|
released: 'September 29, 1996' |
||||
|
}, |
||||
|
{ |
||||
|
name: 'Super Nintendo', |
||||
|
manufacturer: 'Nintendo', |
||||
|
released: 'August 23, 1991' |
||||
|
}, |
||||
|
function(err, nintendo64, superNintendo) { |
||||
|
if (err) return done(err); |
||||
|
|
||||
|
Game.create( |
||||
|
{ |
||||
|
name: 'Legend of Zelda: Ocarina of Time', |
||||
|
developer: 'Nintendo', |
||||
|
released: new Date('November 21, 1998'), |
||||
|
consoles: [nintendo64] |
||||
|
}, |
||||
|
{ |
||||
|
name: 'Mario Kart', |
||||
|
developer: 'Nintendo', |
||||
|
released: 'September 1, 1992', |
||||
|
consoles: [superNintendo] |
||||
|
}, |
||||
|
function(err) { |
||||
|
if (err) return done(err); |
||||
|
example(); |
||||
|
} |
||||
|
); |
||||
|
} |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Population |
||||
|
*/ |
||||
|
|
||||
|
function example() { |
||||
|
Game |
||||
|
.find({}) |
||||
|
.exec(function(err, games) { |
||||
|
if (err) return done(err); |
||||
|
|
||||
|
console.log('found %d games', games.length); |
||||
|
|
||||
|
var options = {path: 'consoles', select: 'name released -_id'}; |
||||
|
Game.populate(games, options, function(err, games) { |
||||
|
if (err) return done(err); |
||||
|
|
||||
|
games.forEach(function(game) { |
||||
|
console.log( |
||||
|
'"%s" was released for the %s on %s', |
||||
|
game.name, |
||||
|
game.consoles[0].name, |
||||
|
game.released.toLocaleDateString() |
||||
|
); |
||||
|
}); |
||||
|
|
||||
|
done(); |
||||
|
}); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
function done(err) { |
||||
|
if (err) console.error(err); |
||||
|
Console.remove(function() { |
||||
|
Game.remove(function() { |
||||
|
mongoose.disconnect(); |
||||
|
}); |
||||
|
}); |
||||
|
} |
@ -0,0 +1,138 @@ |
|||||
|
|
||||
|
var mongoose = require('../../lib'); |
||||
|
var Schema = mongoose.Schema; |
||||
|
|
||||
|
console.log('Running mongoose version %s', mongoose.version); |
||||
|
|
||||
|
/** |
||||
|
* Console schema |
||||
|
*/ |
||||
|
|
||||
|
var consoleSchema = Schema({ |
||||
|
name: String, |
||||
|
manufacturer: String, |
||||
|
released: Date |
||||
|
}); |
||||
|
var Console = mongoose.model('Console', consoleSchema); |
||||
|
|
||||
|
/** |
||||
|
* Game schema |
||||
|
*/ |
||||
|
|
||||
|
var gameSchema = Schema({ |
||||
|
name: String, |
||||
|
developer: String, |
||||
|
released: Date, |
||||
|
consoles: [{ |
||||
|
type: Schema.Types.ObjectId, |
||||
|
ref: 'Console' |
||||
|
}] |
||||
|
}); |
||||
|
var Game = mongoose.model('Game', gameSchema); |
||||
|
|
||||
|
/** |
||||
|
* Connect to the console database on localhost with |
||||
|
* the default port (27017) |
||||
|
*/ |
||||
|
|
||||
|
mongoose.connect('mongodb://localhost/console', function(err) { |
||||
|
// if we failed to connect, abort
|
||||
|
if (err) throw err; |
||||
|
|
||||
|
// we connected ok
|
||||
|
createData(); |
||||
|
}); |
||||
|
|
||||
|
/** |
||||
|
* Data generation |
||||
|
*/ |
||||
|
|
||||
|
function createData() { |
||||
|
Console.create( |
||||
|
{ |
||||
|
name: 'Nintendo 64', |
||||
|
manufacturer: 'Nintendo', |
||||
|
released: 'September 29, 1996' |
||||
|
}, |
||||
|
{ |
||||
|
name: 'Super Nintendo', |
||||
|
manufacturer: 'Nintendo', |
||||
|
released: 'August 23, 1991' |
||||
|
}, |
||||
|
{ |
||||
|
name: 'XBOX 360', |
||||
|
manufacturer: 'Microsoft', |
||||
|
released: 'November 22, 2005' |
||||
|
}, |
||||
|
function(err, nintendo64, superNintendo, xbox360) { |
||||
|
if (err) return done(err); |
||||
|
|
||||
|
Game.create( |
||||
|
{ |
||||
|
name: 'Legend of Zelda: Ocarina of Time', |
||||
|
developer: 'Nintendo', |
||||
|
released: new Date('November 21, 1998'), |
||||
|
consoles: [nintendo64] |
||||
|
}, |
||||
|
{ |
||||
|
name: 'Mario Kart', |
||||
|
developer: 'Nintendo', |
||||
|
released: 'September 1, 1992', |
||||
|
consoles: [superNintendo] |
||||
|
}, |
||||
|
{ |
||||
|
name: 'Perfect Dark Zero', |
||||
|
developer: 'Rare', |
||||
|
released: 'November 17, 2005', |
||||
|
consoles: [xbox360] |
||||
|
}, |
||||
|
function(err) { |
||||
|
if (err) return done(err); |
||||
|
example(); |
||||
|
} |
||||
|
); |
||||
|
} |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Population |
||||
|
*/ |
||||
|
|
||||
|
function example() { |
||||
|
Game |
||||
|
.find({}) |
||||
|
.populate({ |
||||
|
path: 'consoles', |
||||
|
match: {manufacturer: 'Nintendo'}, |
||||
|
select: 'name', |
||||
|
options: {comment: 'population'} |
||||
|
}) |
||||
|
.exec(function(err, games) { |
||||
|
if (err) return done(err); |
||||
|
|
||||
|
games.forEach(function(game) { |
||||
|
console.log( |
||||
|
'"%s" was released for the %s on %s', |
||||
|
game.name, |
||||
|
game.consoles.length ? game.consoles[0].name : '??', |
||||
|
game.released.toLocaleDateString() |
||||
|
); |
||||
|
}); |
||||
|
|
||||
|
return done(); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Clean up |
||||
|
*/ |
||||
|
|
||||
|
function done(err) { |
||||
|
if (err) console.error(err); |
||||
|
Console.remove(function() { |
||||
|
Game.remove(function() { |
||||
|
mongoose.disconnect(); |
||||
|
}); |
||||
|
}); |
||||
|
} |
@ -0,0 +1,106 @@ |
|||||
|
|
||||
|
var mongoose = require('../../lib'); |
||||
|
var Schema = mongoose.Schema; |
||||
|
|
||||
|
console.log('Running mongoose version %s', mongoose.version); |
||||
|
|
||||
|
/** |
||||
|
* Console schema |
||||
|
*/ |
||||
|
|
||||
|
var consoleSchema = Schema({ |
||||
|
name: String, |
||||
|
manufacturer: String, |
||||
|
released: Date |
||||
|
}); |
||||
|
var Console = mongoose.model('Console', consoleSchema); |
||||
|
|
||||
|
/** |
||||
|
* Game schema |
||||
|
*/ |
||||
|
|
||||
|
var gameSchema = Schema({ |
||||
|
name: String, |
||||
|
developer: String, |
||||
|
released: Date, |
||||
|
consoles: [{ |
||||
|
type: Schema.Types.ObjectId, |
||||
|
ref: 'Console' |
||||
|
}] |
||||
|
}); |
||||
|
var Game = mongoose.model('Game', gameSchema); |
||||
|
|
||||
|
/** |
||||
|
* Connect to the console database on localhost with |
||||
|
* the default port (27017) |
||||
|
*/ |
||||
|
|
||||
|
mongoose.connect('mongodb://localhost/console', function(err) { |
||||
|
// if we failed to connect, abort
|
||||
|
if (err) throw err; |
||||
|
|
||||
|
// we connected ok
|
||||
|
createData(); |
||||
|
}); |
||||
|
|
||||
|
/** |
||||
|
* Data generation |
||||
|
*/ |
||||
|
|
||||
|
function createData() { |
||||
|
Console.create( |
||||
|
{ |
||||
|
name: 'Nintendo 64', |
||||
|
manufacturer: 'Nintendo', |
||||
|
released: 'September 29, 1996' |
||||
|
}, |
||||
|
function(err, nintendo64) { |
||||
|
if (err) return done(err); |
||||
|
|
||||
|
Game.create( |
||||
|
{ |
||||
|
name: 'Legend of Zelda: Ocarina of Time', |
||||
|
developer: 'Nintendo', |
||||
|
released: new Date('November 21, 1998'), |
||||
|
consoles: [nintendo64] |
||||
|
}, |
||||
|
function(err) { |
||||
|
if (err) return done(err); |
||||
|
example(); |
||||
|
} |
||||
|
); |
||||
|
} |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Population |
||||
|
*/ |
||||
|
|
||||
|
function example() { |
||||
|
Game |
||||
|
.findOne({name: /^Legend of Zelda/}) |
||||
|
.populate('consoles') |
||||
|
.lean() // just return plain objects, not documents wrapped by mongoose
|
||||
|
.exec(function(err, ocinara) { |
||||
|
if (err) return done(err); |
||||
|
|
||||
|
console.log( |
||||
|
'"%s" was released for the %s on %s', |
||||
|
ocinara.name, |
||||
|
ocinara.consoles[0].name, |
||||
|
ocinara.released.toLocaleDateString() |
||||
|
); |
||||
|
|
||||
|
done(); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
function done(err) { |
||||
|
if (err) console.error(err); |
||||
|
Console.remove(function() { |
||||
|
Game.remove(function() { |
||||
|
mongoose.disconnect(); |
||||
|
}); |
||||
|
}); |
||||
|
} |
@ -0,0 +1,14 @@ |
|||||
|
{ |
||||
|
"name": "promise-example", |
||||
|
"private": "true", |
||||
|
"version": "0.0.0", |
||||
|
"description": "deps for promise example", |
||||
|
"main": "promise.js", |
||||
|
"scripts": { |
||||
|
"test": "echo \"Error: no test specified\" && exit 1" |
||||
|
}, |
||||
|
"dependencies": { "async": "*" }, |
||||
|
"repository": "", |
||||
|
"author": "", |
||||
|
"license": "BSD" |
||||
|
} |
@ -0,0 +1,15 @@ |
|||||
|
|
||||
|
// import the necessary modules
|
||||
|
var mongoose = require('../../lib'); |
||||
|
var Schema = mongoose.Schema; |
||||
|
|
||||
|
// create an export function to encapsulate the model creation
|
||||
|
module.exports = function() { |
||||
|
// define schema
|
||||
|
var PersonSchema = new Schema({ |
||||
|
name: String, |
||||
|
age: Number, |
||||
|
birthday: Date |
||||
|
}); |
||||
|
mongoose.model('Person', PersonSchema); |
||||
|
}; |
@ -0,0 +1,94 @@ |
|||||
|
// import async to make control flow simplier
|
||||
|
var async = require('async'); |
||||
|
|
||||
|
// import the rest of the normal stuff
|
||||
|
var mongoose = require('../../lib'); |
||||
|
|
||||
|
require('./person.js')(); |
||||
|
|
||||
|
var Person = mongoose.model('Person'); |
||||
|
|
||||
|
// define some dummy data
|
||||
|
var data = [ |
||||
|
{ |
||||
|
name: 'bill', |
||||
|
age: 25, |
||||
|
birthday: new Date().setFullYear((new Date().getFullYear() - 25)) |
||||
|
}, |
||||
|
{ |
||||
|
name: 'mary', |
||||
|
age: 30, |
||||
|
birthday: new Date().setFullYear((new Date().getFullYear() - 30)) |
||||
|
}, |
||||
|
{ |
||||
|
name: 'bob', |
||||
|
age: 21, |
||||
|
birthday: new Date().setFullYear((new Date().getFullYear() - 21)) |
||||
|
}, |
||||
|
{ |
||||
|
name: 'lilly', |
||||
|
age: 26, |
||||
|
birthday: new Date().setFullYear((new Date().getFullYear() - 26)) |
||||
|
}, |
||||
|
{ |
||||
|
name: 'alucard', |
||||
|
age: 1000, |
||||
|
birthday: new Date().setFullYear((new Date().getFullYear() - 1000)) |
||||
|
} |
||||
|
]; |
||||
|
|
||||
|
|
||||
|
mongoose.connect('mongodb://localhost/persons', function(err) { |
||||
|
if (err) { |
||||
|
throw err; |
||||
|
} |
||||
|
|
||||
|
// create all of the dummy people
|
||||
|
async.each(data, function(item, cb) { |
||||
|
Person.create(item, cb); |
||||
|
}, function(err) { |
||||
|
if (err) { |
||||
|
// handle error
|
||||
|
} |
||||
|
|
||||
|
// create a promise (get one from the query builder)
|
||||
|
var prom = Person.find({age: {$lt: 1000}}).exec(); |
||||
|
|
||||
|
// add a callback on the promise. This will be called on both error and
|
||||
|
// complete
|
||||
|
prom.addBack(function() { |
||||
|
console.log('completed'); |
||||
|
}); |
||||
|
|
||||
|
// add a callback that is only called on complete (success) events
|
||||
|
prom.addCallback(function() { |
||||
|
console.log('Successful Completion!'); |
||||
|
}); |
||||
|
|
||||
|
// add a callback that is only called on err (rejected) events
|
||||
|
prom.addErrback(function() { |
||||
|
console.log('Fail Boat'); |
||||
|
}); |
||||
|
|
||||
|
// you can chain things just like in the promise/A+ spec
|
||||
|
// note: each then() is returning a new promise, so the above methods
|
||||
|
// that we defined will all fire after the initial promise is fulfilled
|
||||
|
prom.then(function(people) { |
||||
|
// just getting the stuff for the next query
|
||||
|
var ids = people.map(function(p) { |
||||
|
return p._id; |
||||
|
}); |
||||
|
|
||||
|
// return the next promise
|
||||
|
return Person.find({_id: {$nin: ids}}).exec(); |
||||
|
}).then(function(oldest) { |
||||
|
console.log('Oldest person is: %s', oldest); |
||||
|
}).then(cleanup); |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
function cleanup() { |
||||
|
Person.remove(function() { |
||||
|
mongoose.disconnect(); |
||||
|
}); |
||||
|
} |
@ -0,0 +1,14 @@ |
|||||
|
{ |
||||
|
"name": "query-builder-example", |
||||
|
"private": "true", |
||||
|
"version": "0.0.0", |
||||
|
"description": "deps for query builder example", |
||||
|
"main": "querybuilder.js", |
||||
|
"scripts": { |
||||
|
"test": "echo \"Error: no test specified\" && exit 1" |
||||
|
}, |
||||
|
"dependencies": { "async": "*" }, |
||||
|
"repository": "", |
||||
|
"author": "", |
||||
|
"license": "BSD" |
||||
|
} |
@ -0,0 +1,15 @@ |
|||||
|
|
||||
|
// import the necessary modules
|
||||
|
var mongoose = require('../../lib'); |
||||
|
var Schema = mongoose.Schema; |
||||
|
|
||||
|
// create an export function to encapsulate the model creation
|
||||
|
module.exports = function() { |
||||
|
// define schema
|
||||
|
var PersonSchema = new Schema({ |
||||
|
name: String, |
||||
|
age: Number, |
||||
|
birthday: Date |
||||
|
}); |
||||
|
mongoose.model('Person', PersonSchema); |
||||
|
}; |
@ -0,0 +1,79 @@ |
|||||
|
|
||||
|
// import async to make control flow simplier
|
||||
|
var async = require('async'); |
||||
|
|
||||
|
// import the rest of the normal stuff
|
||||
|
var mongoose = require('../../lib'); |
||||
|
|
||||
|
require('./person.js')(); |
||||
|
|
||||
|
var Person = mongoose.model('Person'); |
||||
|
|
||||
|
// define some dummy data
|
||||
|
var data = [ |
||||
|
{ |
||||
|
name: 'bill', |
||||
|
age: 25, |
||||
|
birthday: new Date().setFullYear((new Date().getFullYear() - 25)) |
||||
|
}, |
||||
|
{ |
||||
|
name: 'mary', |
||||
|
age: 30, |
||||
|
birthday: new Date().setFullYear((new Date().getFullYear() - 30)) |
||||
|
}, |
||||
|
{ |
||||
|
name: 'bob', |
||||
|
age: 21, |
||||
|
birthday: new Date().setFullYear((new Date().getFullYear() - 21)) |
||||
|
}, |
||||
|
{ |
||||
|
name: 'lilly', |
||||
|
age: 26, |
||||
|
birthday: new Date().setFullYear((new Date().getFullYear() - 26)) |
||||
|
}, |
||||
|
{ |
||||
|
name: 'alucard', |
||||
|
age: 1000, |
||||
|
birthday: new Date().setFullYear((new Date().getFullYear() - 1000)) |
||||
|
} |
||||
|
]; |
||||
|
|
||||
|
|
||||
|
mongoose.connect('mongodb://localhost/persons', function(err) { |
||||
|
if (err) throw err; |
||||
|
|
||||
|
// create all of the dummy people
|
||||
|
async.each(data, function(item, cb) { |
||||
|
Person.create(item, cb); |
||||
|
}, function(err) { |
||||
|
if (err) throw err; |
||||
|
|
||||
|
// when querying data, instead of providing a callback, you can instead
|
||||
|
// leave that off and get a query object returned
|
||||
|
var query = Person.find({age: {$lt: 1000}}); |
||||
|
|
||||
|
// this allows you to continue applying modifiers to it
|
||||
|
query.sort('birthday'); |
||||
|
query.select('name'); |
||||
|
|
||||
|
// you can chain them together as well
|
||||
|
// a full list of methods can be found:
|
||||
|
// http://mongoosejs.com/docs/api.html#query-js
|
||||
|
query.where('age').gt(21); |
||||
|
|
||||
|
// finally, when ready to execute the query, call the exec() function
|
||||
|
query.exec(function(err, results) { |
||||
|
if (err) throw err; |
||||
|
|
||||
|
console.log(results); |
||||
|
|
||||
|
cleanup(); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
function cleanup() { |
||||
|
Person.remove(function() { |
||||
|
mongoose.disconnect(); |
||||
|
}); |
||||
|
} |
@ -0,0 +1,14 @@ |
|||||
|
{ |
||||
|
"name": "replica-set-example", |
||||
|
"private": "true", |
||||
|
"version": "0.0.0", |
||||
|
"description": "deps for replica set example", |
||||
|
"main": "querybuilder.js", |
||||
|
"scripts": { |
||||
|
"test": "echo \"Error: no test specified\" && exit 1" |
||||
|
}, |
||||
|
"dependencies": { "async": "*" }, |
||||
|
"repository": "", |
||||
|
"author": "", |
||||
|
"license": "BSD" |
||||
|
} |
@ -0,0 +1,15 @@ |
|||||
|
|
||||
|
// import the necessary modules
|
||||
|
var mongoose = require('../../lib'); |
||||
|
var Schema = mongoose.Schema; |
||||
|
|
||||
|
// create an export function to encapsulate the model creation
|
||||
|
module.exports = function() { |
||||
|
// define schema
|
||||
|
var PersonSchema = new Schema({ |
||||
|
name: String, |
||||
|
age: Number, |
||||
|
birthday: Date |
||||
|
}); |
||||
|
mongoose.model('Person', PersonSchema); |
||||
|
}; |
@ -0,0 +1,71 @@ |
|||||
|
|
||||
|
// import async to make control flow simplier
|
||||
|
var async = require('async'); |
||||
|
|
||||
|
// import the rest of the normal stuff
|
||||
|
var mongoose = require('../../lib'); |
||||
|
|
||||
|
require('./person.js')(); |
||||
|
|
||||
|
var Person = mongoose.model('Person'); |
||||
|
|
||||
|
// define some dummy data
|
||||
|
var data = [ |
||||
|
{ |
||||
|
name: 'bill', |
||||
|
age: 25, |
||||
|
birthday: new Date().setFullYear((new Date().getFullYear() - 25)) |
||||
|
}, |
||||
|
{ |
||||
|
name: 'mary', |
||||
|
age: 30, |
||||
|
birthday: new Date().setFullYear((new Date().getFullYear() - 30)) |
||||
|
}, |
||||
|
{ |
||||
|
name: 'bob', |
||||
|
age: 21, |
||||
|
birthday: new Date().setFullYear((new Date().getFullYear() - 21)) |
||||
|
}, |
||||
|
{ |
||||
|
name: 'lilly', |
||||
|
age: 26, |
||||
|
birthday: new Date().setFullYear((new Date().getFullYear() - 26)) |
||||
|
}, |
||||
|
{ |
||||
|
name: 'alucard', |
||||
|
age: 1000, |
||||
|
birthday: new Date().setFullYear((new Date().getFullYear() - 1000)) |
||||
|
} |
||||
|
]; |
||||
|
|
||||
|
|
||||
|
// to connect to a replica set, pass in the comma delimited uri and optionally
|
||||
|
// any connection options such as the rs_name.
|
||||
|
var opts = { |
||||
|
replSet: {rs_name: 'rs0'} |
||||
|
}; |
||||
|
mongoose.connect('mongodb://localhost:27018/persons,localhost:27019,localhost:27020', opts, function(err) { |
||||
|
if (err) throw err; |
||||
|
|
||||
|
// create all of the dummy people
|
||||
|
async.each(data, function(item, cb) { |
||||
|
Person.create(item, cb); |
||||
|
}, function(err) { |
||||
|
if (err) { |
||||
|
// handle error
|
||||
|
} |
||||
|
|
||||
|
// create and delete some data
|
||||
|
var prom = Person.find({age: {$lt: 1000}}).exec(); |
||||
|
|
||||
|
prom.then(function(people) { |
||||
|
console.log('young people: %s', people); |
||||
|
}).then(cleanup); |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
function cleanup() { |
||||
|
Person.remove(function() { |
||||
|
mongoose.disconnect(); |
||||
|
}); |
||||
|
} |
@ -1,102 +0,0 @@ |
|||||
|
|
||||
/** |
|
||||
* Module dependencies. |
|
||||
*/ |
|
||||
|
|
||||
var mongoose = require('mongoose') |
|
||||
, Schema = mongoose.Schema; |
|
||||
|
|
||||
/** |
|
||||
* Schema definition |
|
||||
*/ |
|
||||
|
|
||||
// recursive embedded-document schema
|
|
||||
|
|
||||
var Comment = new Schema(); |
|
||||
|
|
||||
Comment.add({ |
|
||||
title : { type: String, index: true } |
|
||||
, date : Date |
|
||||
, body : String |
|
||||
, comments : [Comment] |
|
||||
}); |
|
||||
|
|
||||
var BlogPost = new Schema({ |
|
||||
title : { type: String, index: true } |
|
||||
, slug : { type: String, lowercase: true, trim: true } |
|
||||
, date : Date |
|
||||
, buf : Buffer |
|
||||
, comments : [Comment] |
|
||||
, creator : Schema.ObjectId |
|
||||
}); |
|
||||
|
|
||||
var Person = new Schema({ |
|
||||
name: { |
|
||||
first: String |
|
||||
, last : String |
|
||||
} |
|
||||
, email: { type: String, required: true, index: { unique: true, sparse: true } } |
|
||||
, alive: Boolean |
|
||||
}); |
|
||||
|
|
||||
/** |
|
||||
* Accessing a specific schema type by key |
|
||||
*/ |
|
||||
|
|
||||
BlogPost.path('date') |
|
||||
.default(function(){ |
|
||||
return new Date() |
|
||||
}) |
|
||||
.set(function(v){ |
|
||||
return v == 'now' ? new Date() : v; |
|
||||
}); |
|
||||
|
|
||||
/** |
|
||||
* Pre hook. |
|
||||
*/ |
|
||||
|
|
||||
BlogPost.pre('save', function(next, done){ |
|
||||
emailAuthor(done); // some async function
|
|
||||
next(); |
|
||||
}); |
|
||||
|
|
||||
/** |
|
||||
* Methods |
|
||||
*/ |
|
||||
|
|
||||
BlogPost.methods.findCreator = function (callback) { |
|
||||
return this.db.model('Person').findById(this.creator, callback); |
|
||||
} |
|
||||
|
|
||||
BlogPost.statics.findByTitle = function (title, callback) { |
|
||||
return this.find({ title: title }, callback); |
|
||||
} |
|
||||
|
|
||||
BlogPost.methods.expressiveQuery = function (creator, date, callback) { |
|
||||
return this.find('creator', creator).where('date').gte(date).run(callback); |
|
||||
} |
|
||||
|
|
||||
/** |
|
||||
* Plugins |
|
||||
*/ |
|
||||
|
|
||||
function slugGenerator (options){ |
|
||||
options = options || {}; |
|
||||
var key = options.key || 'title'; |
|
||||
|
|
||||
return function slugGenerator(schema){ |
|
||||
schema.path(key).set(function(v){ |
|
||||
this.slug = v.toLowerCase().replace(/[^a-z0-9]/g, '').replace(/-+/g, ''); |
|
||||
return v; |
|
||||
}); |
|
||||
}; |
|
||||
}; |
|
||||
|
|
||||
BlogPost.plugin(slugGenerator()); |
|
||||
|
|
||||
/** |
|
||||
* Define model. |
|
||||
*/ |
|
||||
|
|
||||
mongoose.model('BlogPost', BlogPost); |
|
||||
mongoose.model('Person', Person); |
|
@ -0,0 +1,119 @@ |
|||||
|
/** |
||||
|
* Module dependencies. |
||||
|
*/ |
||||
|
|
||||
|
var mongoose = require('../../lib'), |
||||
|
Schema = mongoose.Schema; |
||||
|
|
||||
|
/** |
||||
|
* Schema definition |
||||
|
*/ |
||||
|
|
||||
|
// recursive embedded-document schema
|
||||
|
|
||||
|
var Comment = new Schema(); |
||||
|
|
||||
|
Comment.add({ |
||||
|
title: { |
||||
|
type: String, |
||||
|
index: true |
||||
|
}, |
||||
|
date: Date, |
||||
|
body: String, |
||||
|
comments: [Comment] |
||||
|
}); |
||||
|
|
||||
|
var BlogPost = new Schema({ |
||||
|
title: { |
||||
|
type: String, |
||||
|
index: true |
||||
|
}, |
||||
|
slug: { |
||||
|
type: String, |
||||
|
lowercase: true, |
||||
|
trim: true |
||||
|
}, |
||||
|
date: Date, |
||||
|
buf: Buffer, |
||||
|
comments: [Comment], |
||||
|
creator: Schema.ObjectId |
||||
|
}); |
||||
|
|
||||
|
var Person = new Schema({ |
||||
|
name: { |
||||
|
first: String, |
||||
|
last: String |
||||
|
}, |
||||
|
email: { |
||||
|
type: String, |
||||
|
required: true, |
||||
|
index: { |
||||
|
unique: true, |
||||
|
sparse: true |
||||
|
} |
||||
|
}, |
||||
|
alive: Boolean |
||||
|
}); |
||||
|
|
||||
|
/** |
||||
|
* Accessing a specific schema type by key |
||||
|
*/ |
||||
|
|
||||
|
BlogPost.path('date') |
||||
|
.default(function() { |
||||
|
return new Date(); |
||||
|
}) |
||||
|
.set(function(v) { |
||||
|
return v === 'now' ? new Date() : v; |
||||
|
}); |
||||
|
|
||||
|
/** |
||||
|
* Pre hook. |
||||
|
*/ |
||||
|
|
||||
|
BlogPost.pre('save', function(next, done) { |
||||
|
/* global emailAuthor */ |
||||
|
emailAuthor(done); // some async function
|
||||
|
next(); |
||||
|
}); |
||||
|
|
||||
|
/** |
||||
|
* Methods |
||||
|
*/ |
||||
|
|
||||
|
BlogPost.methods.findCreator = function(callback) { |
||||
|
return this.db.model('Person').findById(this.creator, callback); |
||||
|
}; |
||||
|
|
||||
|
BlogPost.statics.findByTitle = function(title, callback) { |
||||
|
return this.find({title: title}, callback); |
||||
|
}; |
||||
|
|
||||
|
BlogPost.methods.expressiveQuery = function(creator, date, callback) { |
||||
|
return this.find('creator', creator).where('date').gte(date).run(callback); |
||||
|
}; |
||||
|
|
||||
|
/** |
||||
|
* Plugins |
||||
|
*/ |
||||
|
|
||||
|
function slugGenerator(options) { |
||||
|
options = options || {}; |
||||
|
var key = options.key || 'title'; |
||||
|
|
||||
|
return function slugGenerator(schema) { |
||||
|
schema.path(key).set(function(v) { |
||||
|
this.slug = v.toLowerCase().replace(/[^a-z0-9]/g, '').replace(/-+/g, ''); |
||||
|
return v; |
||||
|
}); |
||||
|
}; |
||||
|
} |
||||
|
|
||||
|
BlogPost.plugin(slugGenerator()); |
||||
|
|
||||
|
/** |
||||
|
* Define model. |
||||
|
*/ |
||||
|
|
||||
|
mongoose.model('BlogPost', BlogPost); |
||||
|
mongoose.model('Person', Person); |
@ -0,0 +1,27 @@ |
|||||
|
|
||||
|
// modules
|
||||
|
var mongoose = require('../../../lib'); |
||||
|
var Schema = mongoose.Schema; |
||||
|
|
||||
|
// parse json
|
||||
|
var raw = require('./schema.json'); |
||||
|
|
||||
|
// create a schema
|
||||
|
var timeSignatureSchema = Schema(raw); |
||||
|
|
||||
|
// compile the model
|
||||
|
var TimeSignature = mongoose.model('TimeSignatures', timeSignatureSchema); |
||||
|
|
||||
|
// create a TimeSignature document
|
||||
|
var threeFour = new TimeSignature({ |
||||
|
count: 3, |
||||
|
unit: 4, |
||||
|
description: '3/4', |
||||
|
additive: false, |
||||
|
created: new Date, |
||||
|
links: ['http://en.wikipedia.org/wiki/Time_signature'], |
||||
|
user_id: '518d31a0ef32bbfa853a9814' |
||||
|
}); |
||||
|
|
||||
|
// print its description
|
||||
|
console.log(threeFour); |
@ -0,0 +1,9 @@ |
|||||
|
{ |
||||
|
"count": "number", |
||||
|
"unit": "number", |
||||
|
"description": "string", |
||||
|
"links": ["string"], |
||||
|
"created": "date", |
||||
|
"additive": "boolean", |
||||
|
"user_id": "ObjectId" |
||||
|
} |
@ -0,0 +1,20 @@ |
|||||
|
// import the necessary modules
|
||||
|
var mongoose = require('../../lib'); |
||||
|
var Schema = mongoose.Schema; |
||||
|
|
||||
|
// create an export function to encapsulate the model creation
|
||||
|
module.exports = function() { |
||||
|
// define schema
|
||||
|
var PersonSchema = new Schema({ |
||||
|
name: String, |
||||
|
age: Number, |
||||
|
birthday: Date |
||||
|
}); |
||||
|
|
||||
|
// define a static
|
||||
|
PersonSchema.statics.findPersonByName = function(name, cb) { |
||||
|
this.find({name: new RegExp(name, 'i')}, cb); |
||||
|
}; |
||||
|
|
||||
|
mongoose.model('Person', PersonSchema); |
||||
|
}; |
@ -0,0 +1,41 @@ |
|||||
|
var mongoose = require('../../lib'); |
||||
|
|
||||
|
|
||||
|
// import the schema
|
||||
|
require('./person.js')(); |
||||
|
|
||||
|
// grab the person model object
|
||||
|
var Person = mongoose.model('Person'); |
||||
|
|
||||
|
// connect to a server to do a quick write / read example
|
||||
|
|
||||
|
mongoose.connect('mongodb://localhost/persons', function(err) { |
||||
|
if (err) { |
||||
|
throw err; |
||||
|
} |
||||
|
|
||||
|
Person.create({name: 'bill', age: 25, birthday: new Date().setFullYear((new Date().getFullYear() - 25))}, |
||||
|
function(err, bill) { |
||||
|
if (err) { |
||||
|
throw err; |
||||
|
} |
||||
|
console.log('People added to db: %s', bill.toString()); |
||||
|
|
||||
|
// using the static
|
||||
|
Person.findPersonByName('bill', function(err, result) { |
||||
|
if (err) { |
||||
|
throw err; |
||||
|
} |
||||
|
|
||||
|
console.log(result); |
||||
|
cleanup(); |
||||
|
}); |
||||
|
} |
||||
|
); |
||||
|
}); |
||||
|
|
||||
|
function cleanup() { |
||||
|
Person.remove(function() { |
||||
|
mongoose.disconnect(); |
||||
|
}); |
||||
|
} |
@ -0,0 +1,26 @@ |
|||||
|
/** |
||||
|
* ES6 Promise wrapper constructor. |
||||
|
* |
||||
|
* Promises are returned from executed queries. Example: |
||||
|
* |
||||
|
* var query = Candy.find({ bar: true }); |
||||
|
* var promise = query.exec(); |
||||
|
* |
||||
|
* DEPRECATED. Mongoose 5.0 will use native promises by default (or bluebird, |
||||
|
* if native promises are not present) but still |
||||
|
* support plugging in your own ES6-compatible promises library. Mongoose 5.0 |
||||
|
* will **not** support mpromise. |
||||
|
* |
||||
|
* @param {Function} fn a function which will be called when the promise is resolved that accepts `fn(err, ...){}` as signature |
||||
|
* @api public |
||||
|
*/ |
||||
|
|
||||
|
function ES6Promise() { |
||||
|
throw new Error('Can\'t use ES6 promise with mpromise style constructor'); |
||||
|
} |
||||
|
|
||||
|
ES6Promise.use = function(Promise) { |
||||
|
ES6Promise.ES6 = Promise; |
||||
|
}; |
||||
|
|
||||
|
module.exports = ES6Promise; |
@ -0,0 +1,685 @@ |
|||||
|
/*! |
||||
|
* Module dependencies |
||||
|
*/ |
||||
|
|
||||
|
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. |
||||
|
* |
||||
|
* ####Example: |
||||
|
* |
||||
|
* new Aggregate(); |
||||
|
* new Aggregate({ $project: { a: 1, b: 1 } }); |
||||
|
* new Aggregate({ $project: { a: 1, b: 1 } }, { $skip: 5 }); |
||||
|
* new Aggregate([{ $project: { a: 1, b: 1 } }, { $skip: 5 }]); |
||||
|
* |
||||
|
* Returned when calling Model.aggregate(). |
||||
|
* |
||||
|
* ####Example: |
||||
|
* |
||||
|
* Model |
||||
|
* .aggregate({ $match: { age: { $gte: 21 }}}) |
||||
|
* .unwind('tags') |
||||
|
* .exec(callback) |
||||
|
* |
||||
|
* ####Note: |
||||
|
* |
||||
|
* - 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
|
||||
|
* @param {Object|Array} [ops] aggregation operator(s) or operator array |
||||
|
* @api public |
||||
|
*/ |
||||
|
|
||||
|
function Aggregate() { |
||||
|
this._pipeline = []; |
||||
|
this._model = undefined; |
||||
|
this.options = undefined; |
||||
|
|
||||
|
if (arguments.length === 1 && util.isArray(arguments[0])) { |
||||
|
this.append.apply(this, arguments[0]); |
||||
|
} else { |
||||
|
this.append.apply(this, arguments); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Binds this aggregate to a model. |
||||
|
* |
||||
|
* @param {Model} model the model to which the aggregate is to be bound |
||||
|
* @return {Aggregate} |
||||
|
* @api public |
||||
|
*/ |
||||
|
|
||||
|
Aggregate.prototype.model = function(model) { |
||||
|
this._model = model; |
||||
|
return this; |
||||
|
}; |
||||
|
|
||||
|
/** |
||||
|
* Appends new operators to this aggregate pipeline |
||||
|
* |
||||
|
* ####Examples: |
||||
|
* |
||||
|
* aggregate.append({ $project: { field: 1 }}, { $limit: 2 }); |
||||
|
* |
||||
|
* // or pass an array
|
||||
|
* var pipeline = [{ $match: { daw: 'Logic Audio X' }} ]; |
||||
|
* aggregate.append(pipeline); |
||||
|
* |
||||
|
* @param {Object} ops operator(s) to append |
||||
|
* @return {Aggregate} |
||||
|
* @api public |
||||
|
*/ |
||||
|
|
||||
|
Aggregate.prototype.append = function() { |
||||
|
var args = (arguments.length === 1 && util.isArray(arguments[0])) |
||||
|
? arguments[0] |
||||
|
: utils.args(arguments); |
||||
|
|
||||
|
if (!args.every(isOperator)) { |
||||
|
throw new Error('Arguments must be aggregate pipeline operators'); |
||||
|
} |
||||
|
|
||||
|
this._pipeline = this._pipeline.concat(args); |
||||
|
|
||||
|
return this; |
||||
|
}; |
||||
|
|
||||
|
/** |
||||
|
* Appends a new $project operator to this aggregate pipeline. |
||||
|
* |
||||
|
* Mongoose query [selection syntax](#query_Query-select) is also supported. |
||||
|
* |
||||
|
* ####Examples: |
||||
|
* |
||||
|
* // include a, include b, exclude _id
|
||||
|
* aggregate.project("a b -_id"); |
||||
|
* |
||||
|
* // or you may use object notation, useful when
|
||||
|
* // you have keys already prefixed with a "-"
|
||||
|
* aggregate.project({a: 1, b: 1, _id: 0}); |
||||
|
* |
||||
|
* // reshaping documents
|
||||
|
* aggregate.project({ |
||||
|
* newField: '$b.nested' |
||||
|
* , plusTen: { $add: ['$val', 10]} |
||||
|
* , sub: { |
||||
|
* name: '$a' |
||||
|
* } |
||||
|
* }) |
||||
|
* |
||||
|
* // etc
|
||||
|
* aggregate.project({ salary_k: { $divide: [ "$salary", 1000 ] } }); |
||||
|
* |
||||
|
* @param {Object|String} arg field specification |
||||
|
* @see projection http://docs.mongodb.org/manual/reference/aggregation/project/
|
||||
|
* @return {Aggregate} |
||||
|
* @api public |
||||
|
*/ |
||||
|
|
||||
|
Aggregate.prototype.project = function(arg) { |
||||
|
var fields = {}; |
||||
|
|
||||
|
if (typeof arg === 'object' && !util.isArray(arg)) { |
||||
|
Object.keys(arg).forEach(function(field) { |
||||
|
fields[field] = arg[field]; |
||||
|
}); |
||||
|
} else if (arguments.length === 1 && typeof arg === 'string') { |
||||
|
arg.split(/\s+/).forEach(function(field) { |
||||
|
if (!field) { |
||||
|
return; |
||||
|
} |
||||
|
var include = field[0] === '-' ? 0 : 1; |
||||
|
if (include === 0) { |
||||
|
field = field.substring(1); |
||||
|
} |
||||
|
fields[field] = include; |
||||
|
}); |
||||
|
} else { |
||||
|
throw new Error('Invalid project() argument. Must be string or object'); |
||||
|
} |
||||
|
|
||||
|
return this.append({$project: fields}); |
||||
|
}; |
||||
|
|
||||
|
/** |
||||
|
* Appends a new custom $group operator to this aggregate pipeline. |
||||
|
* |
||||
|
* ####Examples: |
||||
|
* |
||||
|
* aggregate.group({ _id: "$department" }); |
||||
|
* |
||||
|
* @see $group http://docs.mongodb.org/manual/reference/aggregation/group/
|
||||
|
* @method group |
||||
|
* @memberOf Aggregate |
||||
|
* @param {Object} arg $group operator contents |
||||
|
* @return {Aggregate} |
||||
|
* @api public |
||||
|
*/ |
||||
|
|
||||
|
/** |
||||
|
* Appends a new custom $match operator to this aggregate pipeline. |
||||
|
* |
||||
|
* ####Examples: |
||||
|
* |
||||
|
* aggregate.match({ department: { $in: [ "sales", "engineering" } } }); |
||||
|
* |
||||
|
* @see $match http://docs.mongodb.org/manual/reference/aggregation/match/
|
||||
|
* @method match |
||||
|
* @memberOf Aggregate |
||||
|
* @param {Object} arg $match operator contents |
||||
|
* @return {Aggregate} |
||||
|
* @api public |
||||
|
*/ |
||||
|
|
||||
|
/** |
||||
|
* Appends a new $skip operator to this aggregate pipeline. |
||||
|
* |
||||
|
* ####Examples: |
||||
|
* |
||||
|
* aggregate.skip(10); |
||||
|
* |
||||
|
* @see $skip http://docs.mongodb.org/manual/reference/aggregation/skip/
|
||||
|
* @method skip |
||||
|
* @memberOf Aggregate |
||||
|
* @param {Number} num number of records to skip before next stage |
||||
|
* @return {Aggregate} |
||||
|
* @api public |
||||
|
*/ |
||||
|
|
||||
|
/** |
||||
|
* Appends a new $limit operator to this aggregate pipeline. |
||||
|
* |
||||
|
* ####Examples: |
||||
|
* |
||||
|
* aggregate.limit(10); |
||||
|
* |
||||
|
* @see $limit http://docs.mongodb.org/manual/reference/aggregation/limit/
|
||||
|
* @method limit |
||||
|
* @memberOf Aggregate |
||||
|
* @param {Number} num maximum number of records to pass to the next stage |
||||
|
* @return {Aggregate} |
||||
|
* @api public |
||||
|
*/ |
||||
|
|
||||
|
/** |
||||
|
* Appends a new $geoNear operator to this aggregate pipeline. |
||||
|
* |
||||
|
* ####NOTE: |
||||
|
* |
||||
|
* **MUST** be used as the first operator in the pipeline. |
||||
|
* |
||||
|
* ####Examples: |
||||
|
* |
||||
|
* aggregate.near({ |
||||
|
* near: [40.724, -73.997], |
||||
|
* distanceField: "dist.calculated", // required
|
||||
|
* maxDistance: 0.008, |
||||
|
* query: { type: "public" }, |
||||
|
* includeLocs: "dist.location", |
||||
|
* uniqueDocs: true, |
||||
|
* num: 5 |
||||
|
* }); |
||||
|
* |
||||
|
* @see $geoNear http://docs.mongodb.org/manual/reference/aggregation/geoNear/
|
||||
|
* @method near |
||||
|
* @memberOf Aggregate |
||||
|
* @param {Object} parameters |
||||
|
* @return {Aggregate} |
||||
|
* @api public |
||||
|
*/ |
||||
|
|
||||
|
Aggregate.prototype.near = function(arg) { |
||||
|
var op = {}; |
||||
|
op.$geoNear = arg; |
||||
|
return this.append(op); |
||||
|
}; |
||||
|
|
||||
|
/*! |
||||
|
* define methods |
||||
|
*/ |
||||
|
|
||||
|
'group match skip limit out'.split(' ').forEach(function($operator) { |
||||
|
Aggregate.prototype[$operator] = function(arg) { |
||||
|
var op = {}; |
||||
|
op['$' + $operator] = arg; |
||||
|
return this.append(op); |
||||
|
}; |
||||
|
}); |
||||
|
|
||||
|
/** |
||||
|
* 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"); |
||||
|
* aggregate.unwind("a", "b", "c"); |
||||
|
* |
||||
|
* @see $unwind http://docs.mongodb.org/manual/reference/aggregation/unwind/
|
||||
|
* @param {String} fields the field(s) to unwind |
||||
|
* @return {Aggregate} |
||||
|
* @api public |
||||
|
*/ |
||||
|
|
||||
|
Aggregate.prototype.unwind = function() { |
||||
|
var args = utils.args(arguments); |
||||
|
|
||||
|
var res = []; |
||||
|
for (var i = 0; i < args.length; ++i) { |
||||
|
var arg = args[i]; |
||||
|
if (arg && typeof arg === 'object') { |
||||
|
res.push({ $unwind: arg }); |
||||
|
} else if (typeof arg === 'string') { |
||||
|
res.push({ |
||||
|
$unwind: (arg && arg.charAt(0) === '$') ? arg : '$' + arg |
||||
|
}); |
||||
|
} else { |
||||
|
throw new Error('Invalid arg "' + arg + '" to unwind(), ' + |
||||
|
'must be string or object'); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return this.append.apply(this, res); |
||||
|
}; |
||||
|
|
||||
|
/** |
||||
|
* 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. |
||||
|
* |
||||
|
* If an object is passed, values allowed are `asc`, `desc`, `ascending`, `descending`, `1`, and `-1`. |
||||
|
* |
||||
|
* If a string is passed, it must be a space delimited list of path names. The sort order of each path is ascending unless the path name is prefixed with `-` which will be treated as descending. |
||||
|
* |
||||
|
* ####Examples: |
||||
|
* |
||||
|
* // these are equivalent
|
||||
|
* aggregate.sort({ field: 'asc', test: -1 }); |
||||
|
* aggregate.sort('field -test'); |
||||
|
* |
||||
|
* @see $sort http://docs.mongodb.org/manual/reference/aggregation/sort/
|
||||
|
* @param {Object|String} arg |
||||
|
* @return {Aggregate} this |
||||
|
* @api public |
||||
|
*/ |
||||
|
|
||||
|
Aggregate.prototype.sort = function(arg) { |
||||
|
// TODO refactor to reuse the query builder logic
|
||||
|
|
||||
|
var sort = {}; |
||||
|
|
||||
|
if (arg.constructor.name === 'Object') { |
||||
|
var desc = ['desc', 'descending', -1]; |
||||
|
Object.keys(arg).forEach(function(field) { |
||||
|
sort[field] = desc.indexOf(arg[field]) === -1 ? 1 : -1; |
||||
|
}); |
||||
|
} else if (arguments.length === 1 && typeof arg === 'string') { |
||||
|
arg.split(/\s+/).forEach(function(field) { |
||||
|
if (!field) { |
||||
|
return; |
||||
|
} |
||||
|
var ascend = field[0] === '-' ? -1 : 1; |
||||
|
if (ascend === -1) { |
||||
|
field = field.substring(1); |
||||
|
} |
||||
|
sort[field] = ascend; |
||||
|
}); |
||||
|
} else { |
||||
|
throw new TypeError('Invalid sort() argument. Must be a string or object.'); |
||||
|
} |
||||
|
|
||||
|
return this.append({$sort: sort}); |
||||
|
}; |
||||
|
|
||||
|
/** |
||||
|
* Sets the readPreference option for the aggregation query. |
||||
|
* |
||||
|
* ####Example: |
||||
|
* |
||||
|
* Model.aggregate(..).read('primaryPreferred').exec(callback) |
||||
|
* |
||||
|
* @param {String} pref one of the listed preference options or their aliases |
||||
|
* @param {Array} [tags] optional tags for this query |
||||
|
* @see mongodb http://docs.mongodb.org/manual/applications/replication/#read-preference
|
||||
|
* @see driver http://mongodb.github.com/node-mongodb-native/driver-articles/anintroductionto1_1and2_2.html#read-preferences
|
||||
|
*/ |
||||
|
|
||||
|
Aggregate.prototype.read = function(pref, tags) { |
||||
|
if (!this.options) { |
||||
|
this.options = {}; |
||||
|
} |
||||
|
read.call(this, pref, tags); |
||||
|
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) |
||||
|
* |
||||
|
* ####Example: |
||||
|
* |
||||
|
* Model.aggregate(..).allowDiskUse(true).exec(callback) |
||||
|
* |
||||
|
* @param {Boolean} value Should tell server it can use hard drive to store data during aggregation. |
||||
|
* @param {Array} [tags] optional tags for this query |
||||
|
* @see mongodb http://docs.mongodb.org/manual/reference/command/aggregate/
|
||||
|
*/ |
||||
|
|
||||
|
Aggregate.prototype.allowDiskUse = function(value) { |
||||
|
if (!this.options) { |
||||
|
this.options = {}; |
||||
|
} |
||||
|
this.options.allowDiskUse = value; |
||||
|
return this; |
||||
|
}; |
||||
|
|
||||
|
/** |
||||
|
* Sets the cursor option option for the aggregation query (ignored for < 2.6.0). |
||||
|
* Note the different syntax below: .exec() returns a cursor object, and no callback |
||||
|
* is necessary. |
||||
|
* |
||||
|
* ####Example: |
||||
|
* |
||||
|
* var cursor = Model.aggregate(..).cursor({ batchSize: 1000 }).exec(); |
||||
|
* cursor.each(function(error, doc) { |
||||
|
* // use doc
|
||||
|
* }); |
||||
|
* |
||||
|
* @param {Object} options set the cursor batch size |
||||
|
* @see mongodb http://mongodb.github.io/node-mongodb-native/2.0/api/AggregationCursor.html
|
||||
|
*/ |
||||
|
|
||||
|
Aggregate.prototype.cursor = function(options) { |
||||
|
if (!this.options) { |
||||
|
this.options = {}; |
||||
|
} |
||||
|
this.options.cursor = options || {}; |
||||
|
return this; |
||||
|
}; |
||||
|
|
||||
|
/** |
||||
|
* Adds a [cursor flag](http://mongodb.github.io/node-mongodb-native/2.1/api/Cursor.html#addCursorFlag)
|
||||
|
* |
||||
|
* ####Example: |
||||
|
* |
||||
|
* var cursor = Model.aggregate(..).cursor({ batchSize: 1000 }).exec(); |
||||
|
* cursor.each(function(error, doc) { |
||||
|
* // use doc
|
||||
|
* }); |
||||
|
* |
||||
|
* @param {String} flag |
||||
|
* @param {Boolean} value |
||||
|
* @see mongodb http://mongodb.github.io/node-mongodb-native/2.1/api/Cursor.html#addCursorFlag
|
||||
|
*/ |
||||
|
|
||||
|
Aggregate.prototype.addCursorFlag = function(flag, value) { |
||||
|
if (!this.options) { |
||||
|
this.options = {}; |
||||
|
} |
||||
|
this.options[flag] = value; |
||||
|
return this; |
||||
|
}; |
||||
|
|
||||
|
/** |
||||
|
* Executes the aggregate pipeline on the currently bound Model. |
||||
|
* |
||||
|
* ####Example: |
||||
|
* |
||||
|
* aggregate.exec(callback); |
||||
|
* |
||||
|
* // Because a promise is returned, the `callback` is optional.
|
||||
|
* var promise = aggregate.exec(); |
||||
|
* promise.then(..); |
||||
|
* |
||||
|
* @see Promise #promise_Promise |
||||
|
* @param {Function} [callback] |
||||
|
* @return {Promise} |
||||
|
* @api public |
||||
|
*/ |
||||
|
|
||||
|
Aggregate.prototype.exec = function(callback) { |
||||
|
if (!this._model) { |
||||
|
throw new Error('Aggregate not bound to any Model'); |
||||
|
} |
||||
|
var _this = this; |
||||
|
var Promise = PromiseProvider.get(); |
||||
|
var options = utils.clone(this.options); |
||||
|
|
||||
|
if (options && options.cursor) { |
||||
|
if (options.cursor.async) { |
||||
|
delete options.cursor.async; |
||||
|
return new Promise.ES6(function(resolve) { |
||||
|
if (!_this._model.collection.buffer) { |
||||
|
process.nextTick(function() { |
||||
|
var cursor = _this._model.collection. |
||||
|
aggregate(_this._pipeline, options || {}); |
||||
|
resolve(cursor); |
||||
|
callback && callback(null, cursor); |
||||
|
}); |
||||
|
return; |
||||
|
} |
||||
|
_this._model.collection.emitter.once('queue', function() { |
||||
|
var cursor = _this._model.collection. |
||||
|
aggregate(_this._pipeline, options || {}); |
||||
|
resolve(cursor); |
||||
|
callback && callback(null, cursor); |
||||
|
}); |
||||
|
}); |
||||
|
} |
||||
|
return this._model.collection. |
||||
|
aggregate(this._pipeline, this.options || {}); |
||||
|
} |
||||
|
|
||||
|
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 || {}, function(error, result) { |
||||
|
if (error) { |
||||
|
if (callback) { |
||||
|
callback(error); |
||||
|
} |
||||
|
reject(error); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
if (callback) { |
||||
|
callback(null, result); |
||||
|
} |
||||
|
resolve(result); |
||||
|
}); |
||||
|
}); |
||||
|
}; |
||||
|
|
||||
|
/** |
||||
|
* Provides promise for aggregate. |
||||
|
* |
||||
|
* ####Example: |
||||
|
* |
||||
|
* Model.aggregate(..).then(successCallback, errorCallback); |
||||
|
* |
||||
|
* @see Promise #promise_Promise |
||||
|
* @param {Function} [resolve] successCallback |
||||
|
* @param {Function} [reject] errorCallback |
||||
|
* @return {Promise} |
||||
|
*/ |
||||
|
Aggregate.prototype.then = function(resolve, reject) { |
||||
|
var _this = this; |
||||
|
var Promise = PromiseProvider.get(); |
||||
|
var promise = new Promise.ES6(function(success, error) { |
||||
|
_this.exec(function(err, val) { |
||||
|
if (err) error(err); |
||||
|
else success(val); |
||||
|
}); |
||||
|
}); |
||||
|
return promise.then(resolve, reject); |
||||
|
}; |
||||
|
|
||||
|
/*! |
||||
|
* Helpers |
||||
|
*/ |
||||
|
|
||||
|
/** |
||||
|
* Checks whether an object is likely a pipeline operator |
||||
|
* |
||||
|
* @param {Object} obj object to check |
||||
|
* @return {Boolean} |
||||
|
* @api private |
||||
|
*/ |
||||
|
|
||||
|
function isOperator(obj) { |
||||
|
var k; |
||||
|
|
||||
|
if (typeof obj !== 'object') { |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
k = Object.keys(obj); |
||||
|
|
||||
|
return k.length === 1 && k |
||||
|
.some(function(key) { |
||||
|
return key[0] === '$'; |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
/*! |
||||
|
* Adds the appropriate `$match` pipeline step to the top of an aggregate's |
||||
|
* pipeline, should it's model is a non-root discriminator type. This is |
||||
|
* analogous to the `prepareDiscriminatorCriteria` function in `lib/query.js`. |
||||
|
* |
||||
|
* @param {Aggregate} aggregate Aggregate to prepare |
||||
|
*/ |
||||
|
|
||||
|
function prepareDiscriminatorPipeline(aggregate) { |
||||
|
var schema = aggregate._model.schema, |
||||
|
discriminatorMapping = schema && schema.discriminatorMapping; |
||||
|
|
||||
|
if (discriminatorMapping && !discriminatorMapping.isRoot) { |
||||
|
var originalPipeline = aggregate._pipeline, |
||||
|
discriminatorKey = discriminatorMapping.key, |
||||
|
discriminatorValue = discriminatorMapping.value; |
||||
|
|
||||
|
// If the first pipeline stage is a match and it doesn't specify a `__t`
|
||||
|
// key, add the discriminator key to it. This allows for potential
|
||||
|
// aggregation query optimizations not to be disturbed by this feature.
|
||||
|
if (originalPipeline[0] && originalPipeline[0].$match && !originalPipeline[0].$match[discriminatorKey]) { |
||||
|
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; |
||||
|
aggregate._pipeline = [{$match: match}].concat(originalPipeline); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
/*! |
||||
|
* Exports |
||||
|
*/ |
||||
|
|
||||
|
module.exports = Aggregate; |
@ -0,0 +1,99 @@ |
|||||
|
/* eslint-env browser */ |
||||
|
|
||||
|
/** |
||||
|
* The [MongooseError](#error_MongooseError) constructor. |
||||
|
* |
||||
|
* @method Error |
||||
|
* @api public |
||||
|
*/ |
||||
|
|
||||
|
exports.Error = require('./error'); |
||||
|
|
||||
|
/** |
||||
|
* The Mongoose [Schema](#schema_Schema) constructor |
||||
|
* |
||||
|
* ####Example: |
||||
|
* |
||||
|
* var mongoose = require('mongoose'); |
||||
|
* var Schema = mongoose.Schema; |
||||
|
* var CatSchema = new Schema(..); |
||||
|
* |
||||
|
* @method Schema |
||||
|
* @api public |
||||
|
*/ |
||||
|
|
||||
|
exports.Schema = require('./schema'); |
||||
|
|
||||
|
/** |
||||
|
* The various Mongoose Types. |
||||
|
* |
||||
|
* ####Example: |
||||
|
* |
||||
|
* var mongoose = require('mongoose'); |
||||
|
* var array = mongoose.Types.Array; |
||||
|
* |
||||
|
* ####Types: |
||||
|
* |
||||
|
* - [ObjectId](#types-objectid-js) |
||||
|
* - [Buffer](#types-buffer-js) |
||||
|
* - [SubDocument](#types-embedded-js) |
||||
|
* - [Array](#types-array-js) |
||||
|
* - [DocumentArray](#types-documentarray-js) |
||||
|
* |
||||
|
* Using this exposed access to the `ObjectId` type, we can construct ids on demand. |
||||
|
* |
||||
|
* var ObjectId = mongoose.Types.ObjectId; |
||||
|
* var id1 = new ObjectId; |
||||
|
* |
||||
|
* @property Types |
||||
|
* @api public |
||||
|
*/ |
||||
|
exports.Types = require('./types'); |
||||
|
|
||||
|
/** |
||||
|
* The Mongoose [VirtualType](#virtualtype_VirtualType) constructor |
||||
|
* |
||||
|
* @method VirtualType |
||||
|
* @api public |
||||
|
*/ |
||||
|
exports.VirtualType = require('./virtualtype'); |
||||
|
|
||||
|
/** |
||||
|
* The various Mongoose SchemaTypes. |
||||
|
* |
||||
|
* ####Note: |
||||
|
* |
||||
|
* _Alias of mongoose.Schema.Types for backwards compatibility._ |
||||
|
* |
||||
|
* @property SchemaTypes |
||||
|
* @see Schema.SchemaTypes #schema_Schema.Types |
||||
|
* @api public |
||||
|
*/ |
||||
|
|
||||
|
exports.SchemaType = require('./schematype.js'); |
||||
|
|
||||
|
/** |
||||
|
* Internal utils |
||||
|
* |
||||
|
* @property utils |
||||
|
* @api private |
||||
|
*/ |
||||
|
|
||||
|
exports.utils = require('./utils.js'); |
||||
|
|
||||
|
/** |
||||
|
* The Mongoose browser [Document](#document-js) constructor. |
||||
|
* |
||||
|
* @method Document |
||||
|
* @api public |
||||
|
*/ |
||||
|
exports.Document = require('./document_provider.js')(); |
||||
|
|
||||
|
/*! |
||||
|
* Module exports. |
||||
|
*/ |
||||
|
|
||||
|
if (typeof window !== 'undefined') { |
||||
|
window.mongoose = module.exports; |
||||
|
window.Buffer = Buffer; |
||||
|
} |
@ -0,0 +1,103 @@ |
|||||
|
/*! |
||||
|
* Module dependencies. |
||||
|
*/ |
||||
|
|
||||
|
var NodeJSDocument = require('./document'), |
||||
|
EventEmitter = require('events').EventEmitter, |
||||
|
MongooseError = require('./error'), |
||||
|
Schema = require('./schema'), |
||||
|
ObjectId = require('./types/objectid'), |
||||
|
utils = require('./utils'), |
||||
|
ValidationError = MongooseError.ValidationError, |
||||
|
InternalCache = require('./internal'); |
||||
|
|
||||
|
/** |
||||
|
* Document constructor. |
||||
|
* |
||||
|
* @param {Object} obj the values to set |
||||
|
* @param {Object} [fields] optional object containing the fields which were selected in the query returning this document and any populated paths data |
||||
|
* @param {Boolean} [skipId] bool, should we auto create an ObjectId _id |
||||
|
* @inherits NodeJS EventEmitter http://nodejs.org/api/events.html#events_class_events_eventemitter
|
||||
|
* @event `init`: Emitted on a document after it has was retrieved from the db and fully hydrated by Mongoose. |
||||
|
* @event `save`: Emitted when the document is successfully saved |
||||
|
* @api private |
||||
|
*/ |
||||
|
|
||||
|
function Document(obj, schema, fields, skipId, skipInit) { |
||||
|
if (!(this instanceof Document)) { |
||||
|
return new Document(obj, schema, fields, skipId, skipInit); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
if (utils.isObject(schema) && !schema.instanceOfSchema) { |
||||
|
schema = new Schema(schema); |
||||
|
} |
||||
|
|
||||
|
// When creating EmbeddedDocument, it already has the schema and he doesn't need the _id
|
||||
|
schema = this.schema || schema; |
||||
|
|
||||
|
// Generate ObjectId if it is missing, but it requires a scheme
|
||||
|
if (!this.schema && schema.options._id) { |
||||
|
obj = obj || {}; |
||||
|
|
||||
|
if (obj._id === undefined) { |
||||
|
obj._id = new ObjectId(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
if (!schema) { |
||||
|
throw new MongooseError.MissingSchemaError(); |
||||
|
} |
||||
|
|
||||
|
this.$__setSchema(schema); |
||||
|
|
||||
|
this.$__ = new InternalCache; |
||||
|
this.$__.emitter = new EventEmitter(); |
||||
|
this.isNew = true; |
||||
|
this.errors = undefined; |
||||
|
|
||||
|
// var schema = this.schema;
|
||||
|
|
||||
|
if (typeof fields === 'boolean') { |
||||
|
this.$__.strictMode = fields; |
||||
|
fields = undefined; |
||||
|
} else { |
||||
|
this.$__.strictMode = this.schema.options && this.schema.options.strict; |
||||
|
this.$__.selected = fields; |
||||
|
} |
||||
|
|
||||
|
var required = this.schema.requiredPaths(); |
||||
|
for (var i = 0; i < required.length; ++i) { |
||||
|
this.$__.activePaths.require(required[i]); |
||||
|
} |
||||
|
|
||||
|
this.$__.emitter.setMaxListeners(0); |
||||
|
this._doc = this.$__buildDoc(obj, fields, skipId); |
||||
|
|
||||
|
if (!skipInit && obj) { |
||||
|
this.init(obj); |
||||
|
} |
||||
|
|
||||
|
this.$__registerHooksFromSchema(); |
||||
|
|
||||
|
// apply methods
|
||||
|
for (var m in schema.methods) { |
||||
|
this[m] = schema.methods[m]; |
||||
|
} |
||||
|
// apply statics
|
||||
|
for (var s in schema.statics) { |
||||
|
this[s] = schema.statics[s]; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/*! |
||||
|
* Inherit from the NodeJS document |
||||
|
*/ |
||||
|
Document.prototype = Object.create(NodeJSDocument.prototype); |
||||
|
Document.prototype.constructor = Document; |
||||
|
|
||||
|
/*! |
||||
|
* Module exports. |
||||
|
*/ |
||||
|
Document.ValidationError = ValidationError; |
||||
|
module.exports = exports = Document; |
@ -0,0 +1,238 @@ |
|||||
|
/*! |
||||
|
* Module dependencies. |
||||
|
*/ |
||||
|
|
||||
|
var utils = require('./utils'); |
||||
|
var Types = require('./schema/index'); |
||||
|
|
||||
|
var ALLOWED_GEOWITHIN_GEOJSON_TYPES = ['Polygon', 'MultiPolygon']; |
||||
|
|
||||
|
/** |
||||
|
* Handles internal casting for queries |
||||
|
* |
||||
|
* @param {Schema} schema |
||||
|
* @param {Object} obj Object to cast |
||||
|
* @api private |
||||
|
*/ |
||||
|
module.exports = function cast(schema, obj) { |
||||
|
var paths = Object.keys(obj), |
||||
|
i = paths.length, |
||||
|
any$conditionals, |
||||
|
schematype, |
||||
|
nested, |
||||
|
path, |
||||
|
type, |
||||
|
val; |
||||
|
|
||||
|
while (i--) { |
||||
|
path = paths[i]; |
||||
|
val = obj[path]; |
||||
|
|
||||
|
if (path === '$or' || path === '$nor' || path === '$and') { |
||||
|
var k = val.length; |
||||
|
|
||||
|
while (k--) { |
||||
|
val[k] = cast(schema, val[k]); |
||||
|
} |
||||
|
} else if (path === '$where') { |
||||
|
type = typeof val; |
||||
|
|
||||
|
if (type !== 'string' && type !== 'function') { |
||||
|
throw new Error('Must have a string or function for $where'); |
||||
|
} |
||||
|
|
||||
|
if (type === 'function') { |
||||
|
obj[path] = val.toString(); |
||||
|
} |
||||
|
|
||||
|
continue; |
||||
|
} else if (path === '$elemMatch') { |
||||
|
val = cast(schema, val); |
||||
|
} else { |
||||
|
if (!schema) { |
||||
|
// no casting for Mixed types
|
||||
|
continue; |
||||
|
} |
||||
|
|
||||
|
schematype = schema.path(path); |
||||
|
|
||||
|
if (!schematype) { |
||||
|
// Handle potential embedded array queries
|
||||
|
var split = path.split('.'), |
||||
|
j = split.length, |
||||
|
pathFirstHalf, |
||||
|
pathLastHalf, |
||||
|
remainingConds; |
||||
|
|
||||
|
// Find the part of the var path that is a path of the Schema
|
||||
|
while (j--) { |
||||
|
pathFirstHalf = split.slice(0, j).join('.'); |
||||
|
schematype = schema.path(pathFirstHalf); |
||||
|
if (schematype) { |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// If a substring of the input path resolves to an actual real path...
|
||||
|
if (schematype) { |
||||
|
// Apply the casting; similar code for $elemMatch in schema/array.js
|
||||
|
if (schematype.caster && schematype.caster.schema) { |
||||
|
remainingConds = {}; |
||||
|
pathLastHalf = split.slice(j).join('.'); |
||||
|
remainingConds[pathLastHalf] = val; |
||||
|
obj[path] = cast(schematype.caster.schema, remainingConds)[pathLastHalf]; |
||||
|
} else { |
||||
|
obj[path] = val; |
||||
|
} |
||||
|
continue; |
||||
|
} |
||||
|
|
||||
|
if (utils.isObject(val)) { |
||||
|
// handle geo schemas that use object notation
|
||||
|
// { loc: { long: Number, lat: Number }
|
||||
|
|
||||
|
var geo = ''; |
||||
|
if (val.$near) { |
||||
|
geo = '$near'; |
||||
|
} else if (val.$nearSphere) { |
||||
|
geo = '$nearSphere'; |
||||
|
} else if (val.$within) { |
||||
|
geo = '$within'; |
||||
|
} else if (val.$geoIntersects) { |
||||
|
geo = '$geoIntersects'; |
||||
|
} else if (val.$geoWithin) { |
||||
|
geo = '$geoWithin'; |
||||
|
} |
||||
|
|
||||
|
if (!geo) { |
||||
|
continue; |
||||
|
} |
||||
|
|
||||
|
var numbertype = new Types.Number('__QueryCasting__'); |
||||
|
var value = val[geo]; |
||||
|
|
||||
|
if (val.$maxDistance != null) { |
||||
|
val.$maxDistance = numbertype.castForQuery(val.$maxDistance); |
||||
|
} |
||||
|
if (val.$minDistance != null) { |
||||
|
val.$minDistance = numbertype.castForQuery(val.$minDistance); |
||||
|
} |
||||
|
|
||||
|
if (geo === '$within') { |
||||
|
var withinType = value.$center |
||||
|
|| value.$centerSphere |
||||
|
|| value.$box |
||||
|
|| value.$polygon; |
||||
|
|
||||
|
if (!withinType) { |
||||
|
throw new Error('Bad $within paramater: ' + JSON.stringify(val)); |
||||
|
} |
||||
|
|
||||
|
value = withinType; |
||||
|
} else if (geo === '$near' && |
||||
|
typeof value.type === 'string' && Array.isArray(value.coordinates)) { |
||||
|
// geojson; cast the coordinates
|
||||
|
value = value.coordinates; |
||||
|
} else if ((geo === '$near' || geo === '$nearSphere' || geo === '$geoIntersects') && |
||||
|
value.$geometry && typeof value.$geometry.type === 'string' && |
||||
|
Array.isArray(value.$geometry.coordinates)) { |
||||
|
if (value.$maxDistance != null) { |
||||
|
value.$maxDistance = numbertype.castForQuery(value.$maxDistance); |
||||
|
} |
||||
|
if (value.$minDistance != null) { |
||||
|
value.$minDistance = numbertype.castForQuery(value.$minDistance); |
||||
|
} |
||||
|
if (utils.isMongooseObject(value.$geometry)) { |
||||
|
value.$geometry = value.$geometry.toObject({ virtuals: false }); |
||||
|
} |
||||
|
value = value.$geometry.coordinates; |
||||
|
} else if (geo === '$geoWithin') { |
||||
|
if (!value.$geometry) { |
||||
|
throw new Error('$geoWithin must specify $geometry'); |
||||
|
} |
||||
|
if (utils.isMongooseObject(value.$geometry)) { |
||||
|
value.$geometry = value.$geometry.toObject({ virtuals: false }); |
||||
|
} |
||||
|
var geoWithinType = value.$geometry.type; |
||||
|
if (ALLOWED_GEOWITHIN_GEOJSON_TYPES.indexOf(geoWithinType) === -1) { |
||||
|
throw new Error('Invalid geoJSON type for $geoWithin "' + |
||||
|
geoWithinType + '", must be "Polygon" or "MultiPolygon"'); |
||||
|
} |
||||
|
value = value.$geometry.coordinates; |
||||
|
} |
||||
|
|
||||
|
_cast(value, numbertype); |
||||
|
} |
||||
|
} else if (val === null || val === undefined) { |
||||
|
obj[path] = null; |
||||
|
continue; |
||||
|
} else if (val.constructor.name === 'Object') { |
||||
|
any$conditionals = Object.keys(val).some(function(k) { |
||||
|
return k.charAt(0) === '$' && k !== '$id' && k !== '$ref'; |
||||
|
}); |
||||
|
|
||||
|
if (!any$conditionals) { |
||||
|
obj[path] = schematype.castForQuery(val); |
||||
|
} else { |
||||
|
var ks = Object.keys(val), |
||||
|
$cond; |
||||
|
|
||||
|
k = ks.length; |
||||
|
|
||||
|
while (k--) { |
||||
|
$cond = ks[k]; |
||||
|
nested = val[$cond]; |
||||
|
|
||||
|
if ($cond === '$exists') { |
||||
|
if (typeof nested !== 'boolean') { |
||||
|
throw new Error('$exists parameter must be Boolean'); |
||||
|
} |
||||
|
continue; |
||||
|
} |
||||
|
|
||||
|
if ($cond === '$type') { |
||||
|
if (typeof nested !== 'number' && typeof nested !== 'string') { |
||||
|
throw new Error('$type parameter must be number or string'); |
||||
|
} |
||||
|
continue; |
||||
|
} |
||||
|
|
||||
|
if ($cond === '$not') { |
||||
|
cast(schema, nested); |
||||
|
} else { |
||||
|
val[$cond] = schematype.castForQuery($cond, nested); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} else { |
||||
|
obj[path] = schematype.castForQuery(val); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return obj; |
||||
|
}; |
||||
|
|
||||
|
function _cast(val, numbertype) { |
||||
|
if (Array.isArray(val)) { |
||||
|
val.forEach(function(item, i) { |
||||
|
if (Array.isArray(item) || utils.isObject(item)) { |
||||
|
return _cast(item, numbertype); |
||||
|
} |
||||
|
val[i] = numbertype.castForQuery(item); |
||||
|
}); |
||||
|
} else { |
||||
|
var nearKeys = Object.keys(val); |
||||
|
var nearLen = nearKeys.length; |
||||
|
while (nearLen--) { |
||||
|
var nkey = nearKeys[nearLen]; |
||||
|
var item = val[nkey]; |
||||
|
if (Array.isArray(item) || utils.isObject(item)) { |
||||
|
_cast(item, numbertype); |
||||
|
val[nkey] = item; |
||||
|
} else { |
||||
|
val[nkey] = numbertype.castForQuery(item); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,21 @@ |
|||||
|
'use strict'; |
||||
|
|
||||
|
/* eslint-env browser */ |
||||
|
|
||||
|
/*! |
||||
|
* Module dependencies. |
||||
|
*/ |
||||
|
var Document = require('./document.js'); |
||||
|
var BrowserDocument = require('./browserDocument.js'); |
||||
|
|
||||
|
/** |
||||
|
* Returns the Document constructor for the current context |
||||
|
* |
||||
|
* @api private |
||||
|
*/ |
||||
|
module.exports = function() { |
||||
|
if (typeof window !== 'undefined' && typeof document !== 'undefined' && document === window.document) { |
||||
|
return BrowserDocument; |
||||
|
} |
||||
|
return Document; |
||||
|
}; |
@ -0,0 +1,5 @@ |
|||||
|
/*! |
||||
|
* ignore |
||||
|
*/ |
||||
|
|
||||
|
module.exports = function() {}; |