database and ajax test added
This commit is contained in:
+21
@@ -0,0 +1,21 @@
|
||||
language: node_js
|
||||
sudo: false
|
||||
node_js: [10, 9, 8, 7, 6, 5, 4]
|
||||
install:
|
||||
- travis_retry npm install
|
||||
before_script:
|
||||
- wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.6.6.tgz
|
||||
- tar -zxvf mongodb-linux-x86_64-3.6.6.tgz
|
||||
- mkdir -p ./data/db/27017
|
||||
- mkdir -p ./data/db/27000
|
||||
- printf "\n--timeout 8000" >> ./test/mocha.opts
|
||||
- ./mongodb-linux-x86_64-3.6.6/bin/mongod --fork --dbpath ./data/db/27017 --syslog --port 27017
|
||||
- sleep 2
|
||||
matrix:
|
||||
include:
|
||||
- name: "👕Linter"
|
||||
node_js: 10
|
||||
before_script: skip
|
||||
script: npm run lint
|
||||
notifications:
|
||||
email: false
|
||||
+4828
File diff suppressed because it is too large
Load Diff
+332
@@ -0,0 +1,332 @@
|
||||
# Mongoose
|
||||
|
||||
Mongoose is a [MongoDB](https://www.mongodb.org/) object modeling tool designed to work in an asynchronous environment.
|
||||
|
||||
[](http://slack.mongoosejs.io)
|
||||
[](https://travis-ci.org/Automattic/mongoose)
|
||||
[](http://badge.fury.io/js/mongoose)
|
||||
|
||||
[](https://www.npmjs.com/package/mongoose)
|
||||
|
||||
## Documentation
|
||||
|
||||
[mongoosejs.com](http://mongoosejs.com/)
|
||||
|
||||
[Mongoose 5.0.0](https://github.com/Automattic/mongoose/blob/master/History.md#500--2018-01-17) was released on January 17, 2018. You can find more details on backwards breaking changes in 5.0.0 on [GitHub](https://github.com/Automattic/mongoose/blob/master/migrating_to_5.md).
|
||||
|
||||
## Support
|
||||
|
||||
- [Stack Overflow](http://stackoverflow.com/questions/tagged/mongoose)
|
||||
- [Bug Reports](https://github.com/Automattic/mongoose/issues/)
|
||||
- [Mongoose Slack Channel](http://slack.mongoosejs.io/)
|
||||
- [Help Forum](http://groups.google.com/group/mongoose-orm)
|
||||
- [MongoDB Support](https://docs.mongodb.org/manual/support/)
|
||||
|
||||
## Importing
|
||||
|
||||
```javascript
|
||||
// Using Node.js `require()`
|
||||
const mongoose = require('mongoose');
|
||||
|
||||
// Using ES6 imports
|
||||
import mongoose from 'mongoose';
|
||||
```
|
||||
|
||||
## Plugins
|
||||
|
||||
Check out the [plugins search site](http://plugins.mongoosejs.io/) to see hundreds of related modules from the community. Next, learn how to write your own plugin from the [docs](http://mongoosejs.com/docs/plugins.html) or [this blog post](http://thecodebarbarian.com/2015/03/06/guide-to-mongoose-plugins).
|
||||
|
||||
## Contributors
|
||||
|
||||
Pull requests are always welcome! Please base pull requests against the `master`
|
||||
branch and follow the [contributing guide](https://github.com/Automattic/mongoose/blob/master/CONTRIBUTING.md).
|
||||
|
||||
If your pull requests makes documentation changes, please do **not**
|
||||
modify any `.html` files. The `.html` files are compiled code, so please make
|
||||
your changes in `docs/*.jade`, `lib/*.js`, or `test/docs/*.js`.
|
||||
|
||||
View all 300+ [contributors](https://github.com/Automattic/mongoose/graphs/contributors).
|
||||
|
||||
## Installation
|
||||
|
||||
First install [node.js](http://nodejs.org/) and [mongodb](https://www.mongodb.org/downloads). Then:
|
||||
|
||||
```sh
|
||||
$ npm install mongoose
|
||||
```
|
||||
|
||||
## Overview
|
||||
|
||||
### Connecting to MongoDB
|
||||
|
||||
First, we need to define a connection. If your app uses only one database, you should use `mongoose.connect`. If you need to create additional connections, use `mongoose.createConnection`.
|
||||
|
||||
Both `connect` and `createConnection` take a `mongodb://` URI, or the parameters `host, database, port, options`.
|
||||
|
||||
```js
|
||||
const mongoose = require('mongoose');
|
||||
|
||||
mongoose.connect('mongodb://localhost/my_database');
|
||||
```
|
||||
|
||||
Once connected, the `open` event is fired on the `Connection` instance. If you're using `mongoose.connect`, the `Connection` is `mongoose.connection`. Otherwise, `mongoose.createConnection` return value is a `Connection`.
|
||||
|
||||
**Note:** _If the local connection fails then try using 127.0.0.1 instead of localhost. Sometimes issues may arise when the local hostname has been changed._
|
||||
|
||||
**Important!** Mongoose buffers all the commands until it's connected to the database. This means that you don't have to wait until it connects to MongoDB in order to define models, run queries, etc.
|
||||
|
||||
### Defining a Model
|
||||
|
||||
Models are defined through the `Schema` interface.
|
||||
|
||||
```js
|
||||
const Schema = mongoose.Schema;
|
||||
const ObjectId = Schema.ObjectId;
|
||||
|
||||
const BlogPost = new Schema({
|
||||
author: ObjectId,
|
||||
title: String,
|
||||
body: String,
|
||||
date: Date
|
||||
});
|
||||
```
|
||||
|
||||
Aside from defining the structure of your documents and the types of data you're storing, a Schema handles the definition of:
|
||||
|
||||
* [Validators](http://mongoosejs.com/docs/validation.html) (async and sync)
|
||||
* [Defaults](http://mongoosejs.com/docs/api.html#schematype_SchemaType-default)
|
||||
* [Getters](http://mongoosejs.com/docs/api.html#schematype_SchemaType-get)
|
||||
* [Setters](http://mongoosejs.com/docs/api.html#schematype_SchemaType-set)
|
||||
* [Indexes](http://mongoosejs.com/docs/guide.html#indexes)
|
||||
* [Middleware](http://mongoosejs.com/docs/middleware.html)
|
||||
* [Methods](http://mongoosejs.com/docs/guide.html#methods) definition
|
||||
* [Statics](http://mongoosejs.com/docs/guide.html#statics) definition
|
||||
* [Plugins](http://mongoosejs.com/docs/plugins.html)
|
||||
* [pseudo-JOINs](http://mongoosejs.com/docs/populate.html)
|
||||
|
||||
The following example shows some of these features:
|
||||
|
||||
```js
|
||||
const Comment = new Schema({
|
||||
name: { type: String, default: 'hahaha' },
|
||||
age: { type: Number, min: 18, index: true },
|
||||
bio: { type: String, match: /[a-z]/ },
|
||||
date: { type: Date, default: Date.now },
|
||||
buff: Buffer
|
||||
});
|
||||
|
||||
// a setter
|
||||
Comment.path('name').set(function (v) {
|
||||
return capitalize(v);
|
||||
});
|
||||
|
||||
// middleware
|
||||
Comment.pre('save', function (next) {
|
||||
notify(this.get('email'));
|
||||
next();
|
||||
});
|
||||
```
|
||||
|
||||
Take a look at the example in `examples/schema.js` for an end-to-end example of a typical setup.
|
||||
|
||||
### Accessing a Model
|
||||
|
||||
Once we define a model through `mongoose.model('ModelName', mySchema)`, we can access it through the same function
|
||||
|
||||
```js
|
||||
const myModel = mongoose.model('ModelName');
|
||||
```
|
||||
|
||||
Or just do it all at once
|
||||
|
||||
```js
|
||||
const MyModel = mongoose.model('ModelName', mySchema);
|
||||
```
|
||||
|
||||
The first argument is the _singular_ name of the collection your model is for. **Mongoose automatically looks for the _plural_ version of your model name.** For example, if you use
|
||||
|
||||
```js
|
||||
const MyModel = mongoose.model('Ticket', mySchema);
|
||||
```
|
||||
|
||||
Then Mongoose will create the model for your __tickets__ collection, not your __ticket__ collection.
|
||||
|
||||
Once we have our model, we can then instantiate it, and save it:
|
||||
|
||||
```js
|
||||
const instance = new MyModel();
|
||||
instance.my.key = 'hello';
|
||||
instance.save(function (err) {
|
||||
//
|
||||
});
|
||||
```
|
||||
|
||||
Or we can find documents from the same collection
|
||||
|
||||
```js
|
||||
MyModel.find({}, function (err, docs) {
|
||||
// docs.forEach
|
||||
});
|
||||
```
|
||||
|
||||
You can also `findOne`, `findById`, `update`, etc. For more details check out [the docs](http://mongoosejs.com/docs/queries.html).
|
||||
|
||||
**Important!** If you opened a separate connection using `mongoose.createConnection()` but attempt to access the model through `mongoose.model('ModelName')` it will not work as expected since it is not hooked up to an active db connection. In this case access your model through the connection you created:
|
||||
|
||||
```js
|
||||
const conn = mongoose.createConnection('your connection string');
|
||||
const MyModel = conn.model('ModelName', schema);
|
||||
const m = new MyModel;
|
||||
m.save(); // works
|
||||
```
|
||||
|
||||
vs
|
||||
|
||||
```js
|
||||
const conn = mongoose.createConnection('your connection string');
|
||||
const MyModel = mongoose.model('ModelName', schema);
|
||||
const m = new MyModel;
|
||||
m.save(); // does not work b/c the default connection object was never connected
|
||||
```
|
||||
|
||||
### Embedded Documents
|
||||
|
||||
In the first example snippet, we defined a key in the Schema that looks like:
|
||||
|
||||
```
|
||||
comments: [Comment]
|
||||
```
|
||||
|
||||
Where `Comment` is a `Schema` we created. This means that creating embedded documents is as simple as:
|
||||
|
||||
```js
|
||||
// retrieve my model
|
||||
var BlogPost = mongoose.model('BlogPost');
|
||||
|
||||
// create a blog post
|
||||
var post = new BlogPost();
|
||||
|
||||
// create a comment
|
||||
post.comments.push({ title: 'My comment' });
|
||||
|
||||
post.save(function (err) {
|
||||
if (!err) console.log('Success!');
|
||||
});
|
||||
```
|
||||
|
||||
The same goes for removing them:
|
||||
|
||||
```js
|
||||
BlogPost.findById(myId, function (err, post) {
|
||||
if (!err) {
|
||||
post.comments[0].remove();
|
||||
post.save(function (err) {
|
||||
// do something
|
||||
});
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
Embedded documents enjoy all the same features as your models. Defaults, validators, middleware. Whenever an error occurs, it's bubbled to the `save()` error callback, so error handling is a snap!
|
||||
|
||||
|
||||
### Middleware
|
||||
|
||||
See the [docs](http://mongoosejs.com/docs/middleware.html) page.
|
||||
|
||||
#### Intercepting and mutating method arguments
|
||||
|
||||
You can intercept method arguments via middleware.
|
||||
|
||||
For example, this would allow you to broadcast changes about your Documents every time someone `set`s a path in your Document to a new value:
|
||||
|
||||
```js
|
||||
schema.pre('set', function (next, path, val, typel) {
|
||||
// `this` is the current Document
|
||||
this.emit('set', path, val);
|
||||
|
||||
// Pass control to the next pre
|
||||
next();
|
||||
});
|
||||
```
|
||||
|
||||
Moreover, you can mutate the incoming `method` arguments so that subsequent middleware see different values for those arguments. To do so, just pass the new values to `next`:
|
||||
|
||||
```js
|
||||
.pre(method, function firstPre (next, methodArg1, methodArg2) {
|
||||
// Mutate methodArg1
|
||||
next("altered-" + methodArg1.toString(), methodArg2);
|
||||
});
|
||||
|
||||
// pre declaration is chainable
|
||||
.pre(method, function secondPre (next, methodArg1, methodArg2) {
|
||||
console.log(methodArg1);
|
||||
// => 'altered-originalValOfMethodArg1'
|
||||
|
||||
console.log(methodArg2);
|
||||
// => 'originalValOfMethodArg2'
|
||||
|
||||
// Passing no arguments to `next` automatically passes along the current argument values
|
||||
// i.e., the following `next()` is equivalent to `next(methodArg1, methodArg2)`
|
||||
// and also equivalent to, with the example method arg
|
||||
// values, `next('altered-originalValOfMethodArg1', 'originalValOfMethodArg2')`
|
||||
next();
|
||||
});
|
||||
```
|
||||
|
||||
#### Schema gotcha
|
||||
|
||||
`type`, when used in a schema has special meaning within Mongoose. If your schema requires using `type` as a nested property you must use object notation:
|
||||
|
||||
```js
|
||||
new Schema({
|
||||
broken: { type: Boolean },
|
||||
asset: {
|
||||
name: String,
|
||||
type: String // uh oh, it broke. asset will be interpreted as String
|
||||
}
|
||||
});
|
||||
|
||||
new Schema({
|
||||
works: { type: Boolean },
|
||||
asset: {
|
||||
name: String,
|
||||
type: { type: String } // works. asset is an object with a type property
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
### Driver Access
|
||||
|
||||
Mongoose is built on top of the [official MongoDB Node.js driver](https://github.com/mongodb/node-mongodb-native). Each mongoose model keeps a reference to a [native MongoDB driver collection](http://mongodb.github.io/node-mongodb-native/2.1/api/Collection.html). The collection object can be accessed using `YourModel.collection`. However, using the collection object directly bypasses all mongoose features, including hooks, validation, etc. The one
|
||||
notable exception that `YourModel.collection` still buffers
|
||||
commands. As such, `YourModel.collection.find()` will **not**
|
||||
return a cursor.
|
||||
|
||||
## API Docs
|
||||
|
||||
Find the API docs [here](http://mongoosejs.com/docs/api.html), generated using [dox](https://github.com/tj/dox)
|
||||
and [acquit](https://github.com/vkarpov15/acquit).
|
||||
|
||||
## License
|
||||
|
||||
Copyright (c) 2010 LearnBoost <dev@learnboost.com>
|
||||
|
||||
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.
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
/**
|
||||
* Export lib/mongoose
|
||||
*
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
module.exports = require('./lib/browser');
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
|
||||
/**
|
||||
* Export lib/mongoose
|
||||
*
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
module.exports = require('./lib/');
|
||||
+1099
File diff suppressed because it is too large
Load Diff
+133
@@ -0,0 +1,133 @@
|
||||
/* eslint-env browser */
|
||||
|
||||
'use strict';
|
||||
|
||||
require('./driver').set(require('./drivers/browser'));
|
||||
|
||||
const DocumentProvider = require('./document_provider.js');
|
||||
const PromiseProvider = require('./promise_provider');
|
||||
|
||||
DocumentProvider.setBrowser(true);
|
||||
|
||||
/**
|
||||
* The Mongoose [Promise](#promise_Promise) constructor.
|
||||
*
|
||||
* @method Promise
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Object.defineProperty(exports, 'Promise', {
|
||||
get: function() {
|
||||
return PromiseProvider.get();
|
||||
},
|
||||
set: function(lib) {
|
||||
PromiseProvider.set(lib);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Storage layer for mongoose promises
|
||||
*
|
||||
* @method PromiseProvider
|
||||
* @api public
|
||||
*/
|
||||
|
||||
exports.PromiseProvider = PromiseProvider;
|
||||
|
||||
/**
|
||||
* 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 = DocumentProvider();
|
||||
|
||||
/*!
|
||||
* Module exports.
|
||||
*/
|
||||
|
||||
if (typeof window !== 'undefined') {
|
||||
window.mongoose = module.exports;
|
||||
window.Buffer = Buffer;
|
||||
}
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const NodeJSDocument = require('./document');
|
||||
const EventEmitter = require('events').EventEmitter;
|
||||
const MongooseError = require('./error');
|
||||
const Schema = require('./schema');
|
||||
const ObjectId = require('./types/objectid');
|
||||
const ValidationError = MongooseError.ValidationError;
|
||||
const applyHooks = require('./helpers/model/applyHooks');
|
||||
const utils = require('./utils');
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
NodeJSDocument.call(this, obj, fields, skipId, skipInit);
|
||||
|
||||
applyHooks(this, schema, { decorateDoc: true });
|
||||
|
||||
// apply methods
|
||||
for (const m in schema.methods) {
|
||||
this[m] = schema.methods[m];
|
||||
}
|
||||
// apply statics
|
||||
for (const s in schema.statics) {
|
||||
this[s] = schema.statics[s];
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* Inherit from the NodeJS document
|
||||
*/
|
||||
|
||||
Document.prototype = Object.create(NodeJSDocument.prototype);
|
||||
Document.prototype.constructor = Document;
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
Document.events = new EventEmitter();
|
||||
|
||||
/*!
|
||||
* Browser doc exposes the event emitter API
|
||||
*/
|
||||
|
||||
Document.$emitter = new EventEmitter();
|
||||
|
||||
utils.each(
|
||||
['on', 'once', 'emit', 'listeners', 'removeListener', 'setMaxListeners',
|
||||
'removeAllListeners', 'addListener'],
|
||||
function(emitterFn) {
|
||||
Document[emitterFn] = function() {
|
||||
return Document.$emitter[emitterFn].apply(Document.$emitter, arguments);
|
||||
};
|
||||
});
|
||||
|
||||
/*!
|
||||
* Module exports.
|
||||
*/
|
||||
|
||||
Document.ValidationError = ValidationError;
|
||||
module.exports = exports = Document;
|
||||
+340
@@ -0,0 +1,340 @@
|
||||
'use strict';
|
||||
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
const StrictModeError = require('./error/strict');
|
||||
const Types = require('./schema/index');
|
||||
const castTextSearch = require('./schema/operators/text');
|
||||
const get = require('./helpers/get');
|
||||
const util = require('util');
|
||||
const utils = require('./utils');
|
||||
|
||||
const ALLOWED_GEOWITHIN_GEOJSON_TYPES = ['Polygon', 'MultiPolygon'];
|
||||
|
||||
/**
|
||||
* Handles internal casting for query filters.
|
||||
*
|
||||
* @param {Schema} schema
|
||||
* @param {Object} obj Object to cast
|
||||
* @param {Object} options the query options
|
||||
* @param {Query} context passed to setters
|
||||
* @api private
|
||||
*/
|
||||
module.exports = function cast(schema, obj, options, context) {
|
||||
if (Array.isArray(obj)) {
|
||||
throw new Error('Query filter must be an object, got an array ', util.inspect(obj));
|
||||
}
|
||||
|
||||
const paths = Object.keys(obj);
|
||||
let i = paths.length;
|
||||
let _keys;
|
||||
let any$conditionals;
|
||||
let schematype;
|
||||
let nested;
|
||||
let path;
|
||||
let type;
|
||||
let val;
|
||||
|
||||
options = options || {};
|
||||
|
||||
while (i--) {
|
||||
path = paths[i];
|
||||
val = obj[path];
|
||||
|
||||
if (path === '$or' || path === '$nor' || path === '$and') {
|
||||
let k = val.length;
|
||||
|
||||
while (k--) {
|
||||
val[k] = cast(schema, val[k], options, context);
|
||||
}
|
||||
} 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, options, context);
|
||||
} else if (path === '$text') {
|
||||
val = castTextSearch(val, path);
|
||||
} else {
|
||||
if (!schema) {
|
||||
// no casting for Mixed types
|
||||
continue;
|
||||
}
|
||||
|
||||
schematype = schema.path(path);
|
||||
|
||||
// Check for embedded discriminator paths
|
||||
if (!schematype) {
|
||||
const split = path.split('.');
|
||||
let j = split.length;
|
||||
while (j--) {
|
||||
const pathFirstHalf = split.slice(0, j).join('.');
|
||||
const pathLastHalf = split.slice(j).join('.');
|
||||
const _schematype = schema.path(pathFirstHalf);
|
||||
const discriminatorKey = get(_schematype, 'schema.options.discriminatorKey');
|
||||
|
||||
// gh-6027: if we haven't found the schematype but this path is
|
||||
// underneath an embedded discriminator and the embedded discriminator
|
||||
// key is in the query, use the embedded discriminator schema
|
||||
if (_schematype != null &&
|
||||
get(_schematype, 'schema.discriminators') != null &&
|
||||
discriminatorKey != null &&
|
||||
pathLastHalf !== discriminatorKey) {
|
||||
const discriminatorVal = get(obj, pathFirstHalf + '.' + discriminatorKey);
|
||||
if (discriminatorVal != null) {
|
||||
schematype = _schematype.schema.discriminators[discriminatorVal].
|
||||
path(pathLastHalf);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!schematype) {
|
||||
// Handle potential embedded array queries
|
||||
const split = path.split('.');
|
||||
let j = split.length;
|
||||
let pathFirstHalf;
|
||||
let pathLastHalf;
|
||||
let 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, options, context)[pathLastHalf];
|
||||
} else {
|
||||
obj[path] = val;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (utils.isObject(val)) {
|
||||
// handle geo schemas that use object notation
|
||||
// { loc: { long: Number, lat: Number }
|
||||
|
||||
let 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) {
|
||||
const numbertype = new Types.Number('__QueryCasting__');
|
||||
let value = val[geo];
|
||||
|
||||
if (val.$maxDistance != null) {
|
||||
val.$maxDistance = numbertype.castForQueryWrapper({
|
||||
val: val.$maxDistance,
|
||||
context: context
|
||||
});
|
||||
}
|
||||
if (val.$minDistance != null) {
|
||||
val.$minDistance = numbertype.castForQueryWrapper({
|
||||
val: val.$minDistance,
|
||||
context: context
|
||||
});
|
||||
}
|
||||
|
||||
if (geo === '$within') {
|
||||
const withinType = value.$center
|
||||
|| value.$centerSphere
|
||||
|| value.$box
|
||||
|| value.$polygon;
|
||||
|
||||
if (!withinType) {
|
||||
throw new Error('Bad $within parameter: ' + 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.castForQueryWrapper({
|
||||
val: value.$maxDistance,
|
||||
context: context
|
||||
});
|
||||
}
|
||||
if (value.$minDistance != null) {
|
||||
value.$minDistance = numbertype.castForQueryWrapper({
|
||||
val: value.$minDistance,
|
||||
context: context
|
||||
});
|
||||
}
|
||||
if (utils.isMongooseObject(value.$geometry)) {
|
||||
value.$geometry = value.$geometry.toObject({
|
||||
transform: false,
|
||||
virtuals: false
|
||||
});
|
||||
}
|
||||
value = value.$geometry.coordinates;
|
||||
} else if (geo === '$geoWithin') {
|
||||
if (value.$geometry) {
|
||||
if (utils.isMongooseObject(value.$geometry)) {
|
||||
value.$geometry = value.$geometry.toObject({ virtuals: false });
|
||||
}
|
||||
const 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;
|
||||
} else {
|
||||
value = value.$box || value.$polygon || value.$center ||
|
||||
value.$centerSphere;
|
||||
if (utils.isMongooseObject(value)) {
|
||||
value = value.toObject({ virtuals: false });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_cast(value, numbertype, context);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (schema.nested[path]) {
|
||||
continue;
|
||||
}
|
||||
if (options.upsert && options.strict) {
|
||||
if (options.strict === 'throw') {
|
||||
throw new StrictModeError(path);
|
||||
}
|
||||
throw new StrictModeError(path, 'Path "' + path + '" is not in ' +
|
||||
'schema, strict mode is `true`, and upsert is `true`.');
|
||||
} else if (options.strictQuery === 'throw') {
|
||||
throw new StrictModeError(path, 'Path "' + path + '" is not in ' +
|
||||
'schema and strictQuery is true.');
|
||||
} else if (options.strictQuery) {
|
||||
delete obj[path];
|
||||
}
|
||||
} else if (val == 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.castForQueryWrapper({
|
||||
val: val,
|
||||
context: context
|
||||
});
|
||||
} else {
|
||||
const ks = Object.keys(val);
|
||||
let $cond;
|
||||
|
||||
let k = ks.length;
|
||||
|
||||
while (k--) {
|
||||
$cond = ks[k];
|
||||
nested = val[$cond];
|
||||
|
||||
if ($cond === '$not') {
|
||||
if (nested && schematype && !schematype.caster) {
|
||||
_keys = Object.keys(nested);
|
||||
if (_keys.length && _keys[0].charAt(0) === '$') {
|
||||
for (const key in nested) {
|
||||
nested[key] = schematype.castForQueryWrapper({
|
||||
$conditional: key,
|
||||
val: nested[key],
|
||||
context: context
|
||||
});
|
||||
}
|
||||
} else {
|
||||
val[$cond] = schematype.castForQueryWrapper({
|
||||
$conditional: $cond,
|
||||
val: nested,
|
||||
context: context
|
||||
});
|
||||
}
|
||||
continue;
|
||||
}
|
||||
cast(schematype.caster ? schematype.caster.schema : schema, nested, options, context);
|
||||
} else {
|
||||
val[$cond] = schematype.castForQueryWrapper({
|
||||
$conditional: $cond,
|
||||
val: nested,
|
||||
context: context
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (Array.isArray(val) && ['Buffer', 'Array'].indexOf(schematype.instance) === -1) {
|
||||
const casted = [];
|
||||
for (let valIndex = 0; valIndex < val.length; valIndex++) {
|
||||
casted.push(schematype.castForQueryWrapper({
|
||||
val: val[valIndex],
|
||||
context: context
|
||||
}));
|
||||
}
|
||||
|
||||
obj[path] = { $in: casted };
|
||||
} else {
|
||||
obj[path] = schematype.castForQueryWrapper({
|
||||
val: val,
|
||||
context: context
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return obj;
|
||||
};
|
||||
|
||||
function _cast(val, numbertype, context) {
|
||||
if (Array.isArray(val)) {
|
||||
val.forEach(function(item, i) {
|
||||
if (Array.isArray(item) || utils.isObject(item)) {
|
||||
return _cast(item, numbertype, context);
|
||||
}
|
||||
val[i] = numbertype.castForQueryWrapper({ val: item, context: context });
|
||||
});
|
||||
} else {
|
||||
const nearKeys = Object.keys(val);
|
||||
let nearLen = nearKeys.length;
|
||||
while (nearLen--) {
|
||||
const nkey = nearKeys[nearLen];
|
||||
const item = val[nkey];
|
||||
if (Array.isArray(item) || utils.isObject(item)) {
|
||||
_cast(item, numbertype, context);
|
||||
val[nkey] = item;
|
||||
} else {
|
||||
val[nkey] = numbertype.castForQuery({ val: item, context: context });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
'use strict';
|
||||
|
||||
const CastError = require('../error/cast');
|
||||
|
||||
/*!
|
||||
* Given a value, cast it to a boolean, or throw a `CastError` if the value
|
||||
* cannot be casted. `null` and `undefined` are considered valid.
|
||||
*
|
||||
* @param {Any} value
|
||||
* @param {String} [path] optional the path to set on the CastError
|
||||
* @return {Boolean|null|undefined}
|
||||
* @throws {CastError} if `value` is not one of the allowed values
|
||||
* @api private
|
||||
*/
|
||||
|
||||
module.exports = function castBoolean(value, path) {
|
||||
if (value == null) {
|
||||
return value;
|
||||
}
|
||||
|
||||
if (module.exports.convertToTrue.has(value)) {
|
||||
return true;
|
||||
}
|
||||
if (module.exports.convertToFalse.has(value)) {
|
||||
return false;
|
||||
}
|
||||
throw new CastError('boolean', value, path);
|
||||
};
|
||||
|
||||
module.exports.convertToTrue = new Set([true, 'true', 1, '1', 'yes']);
|
||||
module.exports.convertToFalse = new Set([false, 'false', 0, '0', 'no']);
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
'use strict';
|
||||
|
||||
const assert = require('assert');
|
||||
|
||||
module.exports = function castDate(value) {
|
||||
// Support empty string because of empty form values. Originally introduced
|
||||
// in https://github.com/Automattic/mongoose/commit/efc72a1898fc3c33a319d915b8c5463a22938dfe
|
||||
if (value == null || value === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (value instanceof Date) {
|
||||
assert.ok(!isNaN(value.valueOf()));
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
let date;
|
||||
|
||||
assert.ok(typeof value !== 'boolean');
|
||||
|
||||
if (value instanceof Number || typeof value === 'number') {
|
||||
date = new Date(value);
|
||||
} else if (typeof value === 'string' && !isNaN(Number(value)) && (Number(value) >= 275761 || Number(value) < -271820)) {
|
||||
// string representation of milliseconds take this path
|
||||
date = new Date(Number(value));
|
||||
} else if (typeof value.valueOf === 'function') {
|
||||
// support for moment.js. This is also the path strings will take because
|
||||
// strings have a `valueOf()`
|
||||
date = new Date(value.valueOf());
|
||||
} else {
|
||||
// fallback
|
||||
date = new Date(value);
|
||||
}
|
||||
|
||||
if (!isNaN(date.valueOf())) {
|
||||
return date;
|
||||
}
|
||||
|
||||
assert.ok(false);
|
||||
};
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
'use strict';
|
||||
|
||||
const Decimal128Type = require('../types/decimal128');
|
||||
const assert = require('assert');
|
||||
|
||||
module.exports = function castDecimal128(value) {
|
||||
if (value == null) {
|
||||
return value;
|
||||
}
|
||||
|
||||
if (typeof value === 'object' && typeof value.$numberDecimal === 'string') {
|
||||
return Decimal128Type.fromString(value.$numberDecimal);
|
||||
}
|
||||
|
||||
if (value instanceof Decimal128Type) {
|
||||
return value;
|
||||
}
|
||||
|
||||
if (typeof value === 'string') {
|
||||
return Decimal128Type.fromString(value);
|
||||
}
|
||||
|
||||
if (Buffer.isBuffer(value)) {
|
||||
return new Decimal128Type(value);
|
||||
}
|
||||
|
||||
if (typeof value === 'number') {
|
||||
return Decimal128Type.fromString(String(value));
|
||||
}
|
||||
|
||||
if (typeof value.valueOf === 'function' && typeof value.valueOf() === 'string') {
|
||||
return Decimal128Type.fromString(value.valueOf());
|
||||
}
|
||||
|
||||
assert.ok(false);
|
||||
};
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
'use strict';
|
||||
|
||||
const assert = require('assert');
|
||||
|
||||
/*!
|
||||
* Given a value, cast it to a number, or throw a `CastError` if the value
|
||||
* cannot be casted. `null` and `undefined` are considered valid.
|
||||
*
|
||||
* @param {Any} value
|
||||
* @param {String} [path] optional the path to set on the CastError
|
||||
* @return {Boolean|null|undefined}
|
||||
* @throws {Error} if `value` is not one of the allowed values
|
||||
* @api private
|
||||
*/
|
||||
|
||||
module.exports = function castNumber(val) {
|
||||
assert.ok(!isNaN(val));
|
||||
|
||||
if (val == null) {
|
||||
return val;
|
||||
}
|
||||
if (val === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (typeof val === 'string' || typeof val === 'boolean') {
|
||||
val = Number(val);
|
||||
}
|
||||
|
||||
assert.ok(!isNaN(val));
|
||||
if (val instanceof Number) {
|
||||
return val;
|
||||
}
|
||||
if (typeof val === 'number') {
|
||||
return val;
|
||||
}
|
||||
if (!Array.isArray(val) && typeof val.valueOf === 'function') {
|
||||
return Number(val.valueOf());
|
||||
}
|
||||
if (val.toString && !Array.isArray(val) && val.toString() == Number(val)) {
|
||||
return new Number(val);
|
||||
}
|
||||
|
||||
assert.ok(false);
|
||||
};
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
'use strict';
|
||||
|
||||
const ObjectId = require('../driver').get().ObjectId;
|
||||
const assert = require('assert');
|
||||
|
||||
module.exports = function castObjectId(value) {
|
||||
if (value == null) {
|
||||
return value;
|
||||
}
|
||||
|
||||
if (value instanceof ObjectId) {
|
||||
return value;
|
||||
}
|
||||
|
||||
if (value._id) {
|
||||
if (value._id instanceof ObjectId) {
|
||||
return value._id;
|
||||
}
|
||||
if (value._id.toString instanceof Function) {
|
||||
return new ObjectId(value._id.toString());
|
||||
}
|
||||
}
|
||||
|
||||
if (value.toString instanceof Function) {
|
||||
return new ObjectId(value.toString());
|
||||
}
|
||||
|
||||
assert.ok(false);
|
||||
};
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
'use strict';
|
||||
|
||||
const CastError = require('../error/cast');
|
||||
|
||||
/*!
|
||||
* Given a value, cast it to a string, or throw a `CastError` if the value
|
||||
* cannot be casted. `null` and `undefined` are considered valid.
|
||||
*
|
||||
* @param {Any} value
|
||||
* @param {String} [path] optional the path to set on the CastError
|
||||
* @return {string|null|undefined}
|
||||
* @throws {CastError}
|
||||
* @api private
|
||||
*/
|
||||
|
||||
module.exports = function castString(value, path) {
|
||||
// If null or undefined
|
||||
if (value == null) {
|
||||
return value;
|
||||
}
|
||||
|
||||
// handle documents being passed
|
||||
if (value._id && typeof value._id === 'string') {
|
||||
return value._id;
|
||||
}
|
||||
|
||||
// Re: gh-647 and gh-3030, we're ok with casting using `toString()`
|
||||
// **unless** its the default Object.toString, because "[object Object]"
|
||||
// doesn't really qualify as useful data
|
||||
if (value.toString && value.toString !== Object.prototype.toString) {
|
||||
return value.toString();
|
||||
}
|
||||
|
||||
throw new CastError('string', value, path);
|
||||
};
|
||||
+269
@@ -0,0 +1,269 @@
|
||||
'use strict';
|
||||
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
const EventEmitter = require('events').EventEmitter;
|
||||
const STATES = require('./connectionstate');
|
||||
const immediate = require('./helpers/immediate');
|
||||
|
||||
/**
|
||||
* Abstract Collection constructor
|
||||
*
|
||||
* This is the base class that drivers inherit from and implement.
|
||||
*
|
||||
* @param {String} name name of the collection
|
||||
* @param {Connection} conn A MongooseConnection instance
|
||||
* @param {Object} opts optional collection options
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function Collection(name, conn, opts) {
|
||||
if (opts === void 0) {
|
||||
opts = {};
|
||||
}
|
||||
if (opts.capped === void 0) {
|
||||
opts.capped = {};
|
||||
}
|
||||
|
||||
opts.bufferCommands = undefined === opts.bufferCommands
|
||||
? true
|
||||
: opts.bufferCommands;
|
||||
|
||||
if (typeof opts.capped === 'number') {
|
||||
opts.capped = {size: opts.capped};
|
||||
}
|
||||
|
||||
this.opts = opts;
|
||||
this.name = name;
|
||||
this.collectionName = name;
|
||||
this.conn = conn;
|
||||
this.queue = [];
|
||||
this.buffer = this.opts.bufferCommands;
|
||||
this.emitter = new EventEmitter();
|
||||
|
||||
if (STATES.connected === this.conn.readyState) {
|
||||
this.onOpen();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The collection name
|
||||
*
|
||||
* @api public
|
||||
* @property name
|
||||
*/
|
||||
|
||||
Collection.prototype.name;
|
||||
|
||||
/**
|
||||
* The collection name
|
||||
*
|
||||
* @api public
|
||||
* @property collectionName
|
||||
*/
|
||||
|
||||
Collection.prototype.collectionName;
|
||||
|
||||
/**
|
||||
* The Connection instance
|
||||
*
|
||||
* @api public
|
||||
* @property conn
|
||||
*/
|
||||
|
||||
Collection.prototype.conn;
|
||||
|
||||
/**
|
||||
* Called when the database connects
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Collection.prototype.onOpen = function() {
|
||||
this.buffer = false;
|
||||
immediate(() => this.doQueue());
|
||||
};
|
||||
|
||||
/**
|
||||
* Called when the database disconnects
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Collection.prototype.onClose = function(force) {
|
||||
if (this.opts.bufferCommands && !force) {
|
||||
this.buffer = true;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Queues a method for later execution when its
|
||||
* database connection opens.
|
||||
*
|
||||
* @param {String} name name of the method to queue
|
||||
* @param {Array} args arguments to pass to the method when executed
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Collection.prototype.addQueue = function(name, args) {
|
||||
this.queue.push([name, args]);
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Executes all queued methods and clears the queue.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Collection.prototype.doQueue = function() {
|
||||
for (let i = 0, l = this.queue.length; i < l; i++) {
|
||||
if (typeof this.queue[i][0] === 'function') {
|
||||
this.queue[i][0].apply(this, this.queue[i][1]);
|
||||
} else {
|
||||
this[this.queue[i][0]].apply(this, this.queue[i][1]);
|
||||
}
|
||||
}
|
||||
this.queue = [];
|
||||
const _this = this;
|
||||
process.nextTick(function() {
|
||||
_this.emitter.emit('queue');
|
||||
});
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Abstract method that drivers must implement.
|
||||
*/
|
||||
|
||||
Collection.prototype.ensureIndex = function() {
|
||||
throw new Error('Collection#ensureIndex unimplemented by driver');
|
||||
};
|
||||
|
||||
/**
|
||||
* Abstract method that drivers must implement.
|
||||
*/
|
||||
|
||||
Collection.prototype.createIndex = function() {
|
||||
throw new Error('Collection#ensureIndex unimplemented by driver');
|
||||
};
|
||||
|
||||
/**
|
||||
* Abstract method that drivers must implement.
|
||||
*/
|
||||
|
||||
Collection.prototype.findAndModify = function() {
|
||||
throw new Error('Collection#findAndModify unimplemented by driver');
|
||||
};
|
||||
|
||||
/**
|
||||
* Abstract method that drivers must implement.
|
||||
*/
|
||||
|
||||
Collection.prototype.findOneAndUpdate = function() {
|
||||
throw new Error('Collection#findOneAndUpdate unimplemented by driver');
|
||||
};
|
||||
|
||||
/**
|
||||
* Abstract method that drivers must implement.
|
||||
*/
|
||||
|
||||
Collection.prototype.findOneAndDelete = function() {
|
||||
throw new Error('Collection#findOneAndDelete unimplemented by driver');
|
||||
};
|
||||
|
||||
/**
|
||||
* Abstract method that drivers must implement.
|
||||
*/
|
||||
|
||||
Collection.prototype.findOneAndReplace = function() {
|
||||
throw new Error('Collection#findOneAndReplace unimplemented by driver');
|
||||
};
|
||||
|
||||
/**
|
||||
* Abstract method that drivers must implement.
|
||||
*/
|
||||
|
||||
Collection.prototype.findOne = function() {
|
||||
throw new Error('Collection#findOne unimplemented by driver');
|
||||
};
|
||||
|
||||
/**
|
||||
* Abstract method that drivers must implement.
|
||||
*/
|
||||
|
||||
Collection.prototype.find = function() {
|
||||
throw new Error('Collection#find unimplemented by driver');
|
||||
};
|
||||
|
||||
/**
|
||||
* Abstract method that drivers must implement.
|
||||
*/
|
||||
|
||||
Collection.prototype.insert = function() {
|
||||
throw new Error('Collection#insert unimplemented by driver');
|
||||
};
|
||||
|
||||
/**
|
||||
* Abstract method that drivers must implement.
|
||||
*/
|
||||
|
||||
Collection.prototype.insertOne = function() {
|
||||
throw new Error('Collection#insertOne unimplemented by driver');
|
||||
};
|
||||
|
||||
/**
|
||||
* Abstract method that drivers must implement.
|
||||
*/
|
||||
|
||||
Collection.prototype.insertMany = function() {
|
||||
throw new Error('Collection#insertMany unimplemented by driver');
|
||||
};
|
||||
|
||||
/**
|
||||
* Abstract method that drivers must implement.
|
||||
*/
|
||||
|
||||
Collection.prototype.save = function() {
|
||||
throw new Error('Collection#save unimplemented by driver');
|
||||
};
|
||||
|
||||
/**
|
||||
* Abstract method that drivers must implement.
|
||||
*/
|
||||
|
||||
Collection.prototype.update = function() {
|
||||
throw new Error('Collection#update unimplemented by driver');
|
||||
};
|
||||
|
||||
/**
|
||||
* Abstract method that drivers must implement.
|
||||
*/
|
||||
|
||||
Collection.prototype.getIndexes = function() {
|
||||
throw new Error('Collection#getIndexes unimplemented by driver');
|
||||
};
|
||||
|
||||
/**
|
||||
* Abstract method that drivers must implement.
|
||||
*/
|
||||
|
||||
Collection.prototype.mapReduce = function() {
|
||||
throw new Error('Collection#mapReduce unimplemented by driver');
|
||||
};
|
||||
|
||||
/**
|
||||
* Abstract method that drivers must implement.
|
||||
*/
|
||||
|
||||
Collection.prototype.watch = function() {
|
||||
throw new Error('Collection#watch unimplemented by driver');
|
||||
};
|
||||
|
||||
/*!
|
||||
* Module exports.
|
||||
*/
|
||||
|
||||
module.exports = Collection;
|
||||
+954
@@ -0,0 +1,954 @@
|
||||
'use strict';
|
||||
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
const EventEmitter = require('events').EventEmitter;
|
||||
const Schema = require('./schema');
|
||||
const Collection = require('./driver').get().Collection;
|
||||
const STATES = require('./connectionstate');
|
||||
const MongooseError = require('./error');
|
||||
const PromiseProvider = require('./promise_provider');
|
||||
const get = require('./helpers/get');
|
||||
const mongodb = require('mongodb');
|
||||
const utils = require('./utils');
|
||||
|
||||
const parseConnectionString = require('mongodb-core').parseConnectionString;
|
||||
|
||||
/*!
|
||||
* A list of authentication mechanisms that don't require a password for authentication.
|
||||
* This is used by the authMechanismDoesNotRequirePassword method.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
const noPasswordAuthMechanisms = [
|
||||
'MONGODB-X509'
|
||||
];
|
||||
|
||||
/**
|
||||
* Connection constructor
|
||||
*
|
||||
* For practical reasons, a Connection equals a Db.
|
||||
*
|
||||
* @param {Mongoose} base a mongoose instance
|
||||
* @inherits NodeJS EventEmitter http://nodejs.org/api/events.html#events_class_events_eventemitter
|
||||
* @event `connecting`: Emitted when `connection.openUri()` is executed on this connection.
|
||||
* @event `connected`: Emitted when this connection successfully connects to the db. May be emitted _multiple_ times in `reconnected` scenarios.
|
||||
* @event `open`: Emitted after we `connected` and `onOpen` is executed on all of this connections models.
|
||||
* @event `disconnecting`: Emitted when `connection.close()` was executed.
|
||||
* @event `disconnected`: Emitted after getting disconnected from the db.
|
||||
* @event `close`: Emitted after we `disconnected` and `onClose` executed on all of this connections models.
|
||||
* @event `reconnected`: Emitted after we `connected` and subsequently `disconnected`, followed by successfully another successfull connection.
|
||||
* @event `error`: Emitted when an error occurs on this connection.
|
||||
* @event `fullsetup`: Emitted in a replica-set scenario, when primary and at least one seconaries specified in the connection string are connected.
|
||||
* @event `all`: Emitted in a replica-set scenario, when all nodes specified in the connection string are connected.
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function Connection(base) {
|
||||
this.base = base;
|
||||
this.collections = {};
|
||||
this.models = {};
|
||||
this.config = {autoIndex: true};
|
||||
this.replica = false;
|
||||
this.options = null;
|
||||
this.otherDbs = []; // FIXME: To be replaced with relatedDbs
|
||||
this.relatedDbs = {}; // Hashmap of other dbs that share underlying connection
|
||||
this.states = STATES;
|
||||
this._readyState = STATES.disconnected;
|
||||
this._closeCalled = false;
|
||||
this._hasOpened = false;
|
||||
|
||||
this.$internalEmitter = new EventEmitter();
|
||||
this.$internalEmitter.setMaxListeners(0);
|
||||
}
|
||||
|
||||
/*!
|
||||
* Inherit from EventEmitter
|
||||
*/
|
||||
|
||||
Connection.prototype.__proto__ = EventEmitter.prototype;
|
||||
|
||||
/**
|
||||
* Connection ready state
|
||||
*
|
||||
* - 0 = disconnected
|
||||
* - 1 = connected
|
||||
* - 2 = connecting
|
||||
* - 3 = disconnecting
|
||||
*
|
||||
* Each state change emits its associated event name.
|
||||
*
|
||||
* ####Example
|
||||
*
|
||||
* conn.on('connected', callback);
|
||||
* conn.on('disconnected', callback);
|
||||
*
|
||||
* @property readyState
|
||||
* @memberOf Connection
|
||||
* @instance
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Object.defineProperty(Connection.prototype, 'readyState', {
|
||||
get: function() {
|
||||
return this._readyState;
|
||||
},
|
||||
set: function(val) {
|
||||
if (!(val in STATES)) {
|
||||
throw new Error('Invalid connection state: ' + val);
|
||||
}
|
||||
|
||||
if (this._readyState !== val) {
|
||||
this._readyState = val;
|
||||
// [legacy] loop over the otherDbs on this connection and change their state
|
||||
for (let i = 0; i < this.otherDbs.length; i++) {
|
||||
this.otherDbs[i].readyState = val;
|
||||
}
|
||||
|
||||
// loop over relatedDbs on this connection and change their state
|
||||
for (const k in this.relatedDbs) {
|
||||
this.relatedDbs[k].readyState = val;
|
||||
}
|
||||
|
||||
if (STATES.connected === val) {
|
||||
this._hasOpened = true;
|
||||
}
|
||||
|
||||
this.emit(STATES[val]);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* A hash of the collections associated with this connection
|
||||
*
|
||||
* @property collections
|
||||
* @memberOf Connection
|
||||
* @instance
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Connection.prototype.collections;
|
||||
|
||||
/**
|
||||
* The name of the database this connection points to.
|
||||
*
|
||||
* ####Example
|
||||
*
|
||||
* mongoose.createConnection('mongodb://localhost:27017/mydb').name; // "mydb"
|
||||
*
|
||||
* @property name
|
||||
* @memberOf Connection
|
||||
* @instance
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Connection.prototype.name;
|
||||
|
||||
/**
|
||||
* The host name portion of the URI. If multiple hosts, such as a replica set,
|
||||
* this will contain the first host name in the URI
|
||||
*
|
||||
* ####Example
|
||||
*
|
||||
* mongoose.createConnection('mongodb://localhost:27017/mydb').host; // "localhost"
|
||||
*
|
||||
* @property host
|
||||
* @memberOf Connection
|
||||
* @instance
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Object.defineProperty(Connection.prototype, 'host', {
|
||||
configurable: true,
|
||||
enumerable: true,
|
||||
writable: true
|
||||
});
|
||||
|
||||
/**
|
||||
* The port portion of the URI. If multiple hosts, such as a replica set,
|
||||
* this will contain the port from the first host name in the URI.
|
||||
*
|
||||
* ####Example
|
||||
*
|
||||
* mongoose.createConnection('mongodb://localhost:27017/mydb').port; // 27017
|
||||
*
|
||||
* @property port
|
||||
* @memberOf Connection
|
||||
* @instance
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Object.defineProperty(Connection.prototype, 'port', {
|
||||
configurable: true,
|
||||
enumerable: true,
|
||||
writable: true
|
||||
});
|
||||
|
||||
/**
|
||||
* The username specified in the URI
|
||||
*
|
||||
* ####Example
|
||||
*
|
||||
* mongoose.createConnection('mongodb://val:psw@localhost:27017/mydb').user; // "val"
|
||||
*
|
||||
* @property user
|
||||
* @memberOf Connection
|
||||
* @instance
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Object.defineProperty(Connection.prototype, 'user', {
|
||||
configurable: true,
|
||||
enumerable: true,
|
||||
writable: true
|
||||
});
|
||||
|
||||
/**
|
||||
* The password specified in the URI
|
||||
*
|
||||
* ####Example
|
||||
*
|
||||
* mongoose.createConnection('mongodb://val:psw@localhost:27017/mydb').pass; // "psw"
|
||||
*
|
||||
* @property pass
|
||||
* @memberOf Connection
|
||||
* @instance
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Object.defineProperty(Connection.prototype, 'pass', {
|
||||
configurable: true,
|
||||
enumerable: true,
|
||||
writable: true
|
||||
});
|
||||
|
||||
/**
|
||||
* The mongodb.Db instance, set when the connection is opened
|
||||
*
|
||||
* @property db
|
||||
* @memberOf Connection
|
||||
* @instance
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Connection.prototype.db;
|
||||
|
||||
/**
|
||||
* A hash of the global options that are associated with this connection
|
||||
*
|
||||
* @property config
|
||||
* @memberOf Connection
|
||||
* @instance
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Connection.prototype.config;
|
||||
|
||||
/**
|
||||
* Helper for `createCollection()`. Will explicitly create the given collection
|
||||
* with specified options. Used to create [capped collections](https://docs.mongodb.com/manual/core/capped-collections/)
|
||||
* and [views](https://docs.mongodb.com/manual/core/views/) from mongoose.
|
||||
*
|
||||
* Options are passed down without modification to the [MongoDB driver's `createCollection()` function](http://mongodb.github.io/node-mongodb-native/2.2/api/Db.html#createCollection)
|
||||
*
|
||||
* @method createCollection
|
||||
* @param {string} collection The collection to create
|
||||
* @param {Object} [options] see [MongoDB driver docs](http://mongodb.github.io/node-mongodb-native/2.2/api/Db.html#createCollection)
|
||||
* @param {Function} [callback]
|
||||
* @return {Promise}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Connection.prototype.createCollection = _wrapConnHelper(function createCollection(collection, options, cb) {
|
||||
if (typeof options === 'function') {
|
||||
cb = options;
|
||||
options = {};
|
||||
}
|
||||
this.db.createCollection(collection, options, cb);
|
||||
});
|
||||
|
||||
/**
|
||||
* _Requires MongoDB >= 3.6.0._ Starts a [MongoDB session](https://docs.mongodb.com/manual/release-notes/3.6/#client-sessions)
|
||||
* for benefits like causal consistency, [retryable writes](https://docs.mongodb.com/manual/core/retryable-writes/),
|
||||
* and [transactions](http://thecodebarbarian.com/a-node-js-perspective-on-mongodb-4-transactions.html).
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* const session = await conn.startSession();
|
||||
* let doc = await Person.findOne({ name: 'Ned Stark' }, null, { session });
|
||||
* await doc.remove();
|
||||
* // `doc` will always be null, even if reading from a replica set
|
||||
* // secondary. Without causal consistency, it is possible to
|
||||
* // get a doc back from the below query if the query reads from a
|
||||
* // secondary that is experiencing replication lag.
|
||||
* doc = await Person.findOne({ name: 'Ned Stark' }, null, { session, readPreference: 'secondary' });
|
||||
*
|
||||
*
|
||||
* @method startSession
|
||||
* @param {Object} [options] see the [mongodb driver options](http://mongodb.github.io/node-mongodb-native/3.0/api/MongoClient.html#startSession)
|
||||
* @param {Boolean} [options.causalConsistency=true] set to false to disable causal consistency
|
||||
* @param {Function} [callback]
|
||||
* @return {Promise<ClientSession>} promise that resolves to a MongoDB driver `ClientSession`
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Connection.prototype.startSession = _wrapConnHelper(function startSession(options, cb) {
|
||||
if (typeof options === 'function') {
|
||||
cb = options;
|
||||
options = null;
|
||||
}
|
||||
const session = this.client.startSession(options);
|
||||
cb(null, session);
|
||||
});
|
||||
|
||||
/**
|
||||
* Helper for `dropCollection()`. Will delete the given collection, including
|
||||
* all documents and indexes.
|
||||
*
|
||||
* @method dropCollection
|
||||
* @param {string} collection The collection to delete
|
||||
* @param {Function} [callback]
|
||||
* @return {Promise}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Connection.prototype.dropCollection = _wrapConnHelper(function dropCollection(collection, cb) {
|
||||
this.db.dropCollection(collection, cb);
|
||||
});
|
||||
|
||||
/**
|
||||
* Helper for `dropDatabase()`. Deletes the given database, including all
|
||||
* collections, documents, and indexes.
|
||||
*
|
||||
* @method dropDatabase
|
||||
* @param {Function} [callback]
|
||||
* @return {Promise}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Connection.prototype.dropDatabase = _wrapConnHelper(function dropDatabase(cb) {
|
||||
this.$internalEmitter.emit('dropDatabase');
|
||||
this.db.dropDatabase(cb);
|
||||
});
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function _wrapConnHelper(fn) {
|
||||
return function() {
|
||||
const cb = arguments.length > 0 ? arguments[arguments.length - 1] : null;
|
||||
const argsWithoutCb = typeof cb === 'function' ?
|
||||
Array.prototype.slice.call(arguments, 0, arguments.length - 1) :
|
||||
Array.prototype.slice.call(arguments);
|
||||
return utils.promiseOrCallback(cb, cb => {
|
||||
if (this.readyState !== STATES.connected) {
|
||||
this.once('open', function() {
|
||||
fn.apply(this, argsWithoutCb.concat([cb]));
|
||||
});
|
||||
} else {
|
||||
fn.apply(this, argsWithoutCb.concat([cb]));
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* error
|
||||
*
|
||||
* Graceful error handling, passes error to callback
|
||||
* if available, else emits error on the connection.
|
||||
*
|
||||
* @param {Error} err
|
||||
* @param {Function} callback optional
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Connection.prototype.error = function(err, callback) {
|
||||
if (callback) {
|
||||
callback(err);
|
||||
return null;
|
||||
}
|
||||
if (this.listeners('error').length > 0) {
|
||||
this.emit('error', err);
|
||||
}
|
||||
return Promise.reject(err);
|
||||
};
|
||||
|
||||
/**
|
||||
* Called when the connection is opened
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Connection.prototype.onOpen = function() {
|
||||
this.readyState = STATES.connected;
|
||||
|
||||
// avoid having the collection subscribe to our event emitter
|
||||
// to prevent 0.3 warning
|
||||
for (const i in this.collections) {
|
||||
if (utils.object.hasOwnProperty(this.collections, i)) {
|
||||
this.collections[i].onOpen();
|
||||
}
|
||||
}
|
||||
|
||||
this.emit('open');
|
||||
};
|
||||
|
||||
/**
|
||||
* Opens the connection with a URI using `MongoClient.connect()`.
|
||||
*
|
||||
* @param {String} uri The URI to connect with.
|
||||
* @param {Object} [options] Passed on to http://mongodb.github.io/node-mongodb-native/2.2/api/MongoClient.html#connect
|
||||
* @param {Function} [callback]
|
||||
* @returns {Connection} this
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Connection.prototype.openUri = function(uri, options, callback) {
|
||||
this.readyState = STATES.connecting;
|
||||
this._closeCalled = false;
|
||||
|
||||
if (typeof options === 'function') {
|
||||
callback = options;
|
||||
options = null;
|
||||
}
|
||||
|
||||
if (['string', 'number'].indexOf(typeof options) !== -1) {
|
||||
throw new MongooseError('Mongoose 5.x no longer supports ' +
|
||||
'`mongoose.connect(host, dbname, port)` or ' +
|
||||
'`mongoose.createConnection(host, dbname, port)`. See ' +
|
||||
'http://mongoosejs.com/docs/connections.html for supported connection syntax');
|
||||
}
|
||||
|
||||
if (typeof uri !== 'string') {
|
||||
throw new MongooseError('The `uri` parameter to `openUri()` must be a ' +
|
||||
`string, got "${typeof uri}". Make sure the first parameter to ` +
|
||||
'`mongoose.connect()` or `mongoose.createConnection()` is a string.');
|
||||
}
|
||||
|
||||
const Promise = PromiseProvider.get();
|
||||
const _this = this;
|
||||
|
||||
if (options) {
|
||||
options = utils.clone(options);
|
||||
const autoIndex = options.config && options.config.autoIndex != null ?
|
||||
options.config.autoIndex :
|
||||
options.autoIndex;
|
||||
if (autoIndex != null) {
|
||||
this.config.autoIndex = autoIndex !== false;
|
||||
delete options.config;
|
||||
delete options.autoIndex;
|
||||
}
|
||||
|
||||
if ('autoCreate' in options) {
|
||||
this.config.autoCreate = !!options.autoCreate;
|
||||
delete options.autoCreate;
|
||||
}
|
||||
if ('useCreateIndex' in options) {
|
||||
this.config.useCreateIndex = !!options.useCreateIndex;
|
||||
delete options.useCreateIndex;
|
||||
}
|
||||
|
||||
if ('useFindAndModify' in options) {
|
||||
this.config.useFindAndModify = !!options.useFindAndModify;
|
||||
delete options.useFindAndModify;
|
||||
}
|
||||
|
||||
// Backwards compat
|
||||
if (options.user || options.pass) {
|
||||
options.auth = options.auth || {};
|
||||
options.auth.user = options.user;
|
||||
options.auth.password = options.pass;
|
||||
|
||||
this.user = options.user;
|
||||
this.pass = options.pass;
|
||||
}
|
||||
delete options.user;
|
||||
delete options.pass;
|
||||
|
||||
if (options.bufferCommands != null) {
|
||||
options.bufferMaxEntries = 0;
|
||||
this.config.bufferCommands = options.bufferCommands;
|
||||
delete options.bufferCommands;
|
||||
}
|
||||
|
||||
if (options.useMongoClient != null) {
|
||||
handleUseMongoClient(options);
|
||||
}
|
||||
} else {
|
||||
options = {};
|
||||
}
|
||||
|
||||
this._connectionOptions = options;
|
||||
const dbName = options.dbName;
|
||||
if (dbName != null) {
|
||||
this.$dbName = dbName;
|
||||
}
|
||||
delete options.dbName;
|
||||
|
||||
if (!('promiseLibrary' in options)) {
|
||||
options.promiseLibrary = PromiseProvider.get();
|
||||
}
|
||||
if (!('useNewUrlParser' in options)) {
|
||||
if ('useNewUrlParser' in this.base.options) {
|
||||
options.useNewUrlParser = this.base.options.useNewUrlParser;
|
||||
} else {
|
||||
options.useNewUrlParser = false;
|
||||
}
|
||||
}
|
||||
|
||||
const parsePromise = new Promise((resolve, reject) => {
|
||||
parseConnectionString(uri, options, (err, parsed) => {
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
this.name = dbName != null ? dbName : get(parsed, 'auth.db', null);
|
||||
this.host = get(parsed, 'hosts.0.host', 'localhost');
|
||||
this.port = get(parsed, 'hosts.0.port', 27017);
|
||||
this.user = this.user || get(parsed, 'auth.username');
|
||||
this.pass = this.pass || get(parsed, 'auth.password');
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
const client = new mongodb.MongoClient(uri, options);
|
||||
_this.client = client;
|
||||
client.connect(function(error) {
|
||||
if (error) {
|
||||
_this.readyState = STATES.disconnected;
|
||||
return reject(error);
|
||||
}
|
||||
|
||||
const db = dbName != null ? client.db(dbName) : client.db();
|
||||
_this.db = db;
|
||||
|
||||
// Backwards compat for mongoose 4.x
|
||||
db.on('reconnect', function() {
|
||||
_this.readyState = STATES.connected;
|
||||
_this.emit('reconnect');
|
||||
_this.emit('reconnected');
|
||||
});
|
||||
db.s.topology.on('reconnectFailed', function() {
|
||||
_this.emit('reconnectFailed');
|
||||
});
|
||||
db.s.topology.on('left', function(data) {
|
||||
_this.emit('left', data);
|
||||
});
|
||||
db.s.topology.on('joined', function(data) {
|
||||
_this.emit('joined', data);
|
||||
});
|
||||
db.s.topology.on('fullsetup', function(data) {
|
||||
_this.emit('fullsetup', data);
|
||||
});
|
||||
db.on('close', function() {
|
||||
// Implicitly emits 'disconnected'
|
||||
_this.readyState = STATES.disconnected;
|
||||
});
|
||||
client.on('left', function() {
|
||||
if (_this.readyState === STATES.connected &&
|
||||
get(db, 's.topology.s.coreTopology.s.replicaSetState.topologyType') === 'ReplicaSetNoPrimary') {
|
||||
_this.readyState = STATES.disconnected;
|
||||
}
|
||||
});
|
||||
db.on('timeout', function() {
|
||||
_this.emit('timeout');
|
||||
});
|
||||
|
||||
delete _this.then;
|
||||
delete _this.catch;
|
||||
_this.readyState = STATES.connected;
|
||||
|
||||
for (const i in _this.collections) {
|
||||
if (utils.object.hasOwnProperty(_this.collections, i)) {
|
||||
_this.collections[i].onOpen();
|
||||
}
|
||||
}
|
||||
|
||||
resolve(_this);
|
||||
_this.emit('open');
|
||||
});
|
||||
});
|
||||
|
||||
this.$initialConnection = Promise.all([promise, parsePromise]).
|
||||
then(res => res[0]).
|
||||
catch(err => {
|
||||
if (this.listeners('error').length > 0) {
|
||||
process.nextTick(() => this.emit('error', err));
|
||||
return;
|
||||
}
|
||||
throw err;
|
||||
});
|
||||
this.then = function(resolve, reject) {
|
||||
return this.$initialConnection.then(resolve, reject);
|
||||
};
|
||||
this.catch = function(reject) {
|
||||
return this.$initialConnection.catch(reject);
|
||||
};
|
||||
|
||||
if (callback != null) {
|
||||
this.$initialConnection = this.$initialConnection.then(
|
||||
() => callback(null, this),
|
||||
err => callback(err)
|
||||
);
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
const handleUseMongoClient = function handleUseMongoClient(options) {
|
||||
console.warn('WARNING: The `useMongoClient` option is no longer ' +
|
||||
'necessary in mongoose 5.x, please remove it.');
|
||||
const stack = new Error().stack;
|
||||
console.warn(stack.substr(stack.indexOf('\n') + 1));
|
||||
delete options.useMongoClient;
|
||||
};
|
||||
|
||||
/**
|
||||
* Closes the connection
|
||||
*
|
||||
* @param {Boolean} [force] optional
|
||||
* @param {Function} [callback] optional
|
||||
* @return {Connection} self
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Connection.prototype.close = function(force, callback) {
|
||||
if (typeof force === 'function') {
|
||||
callback = force;
|
||||
force = false;
|
||||
}
|
||||
|
||||
this.$wasForceClosed = !!force;
|
||||
|
||||
return utils.promiseOrCallback(callback, cb => {
|
||||
this._close(force, cb);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Handles closing the connection
|
||||
*
|
||||
* @param {Boolean} force
|
||||
* @param {Function} callback
|
||||
* @api private
|
||||
*/
|
||||
Connection.prototype._close = function(force, callback) {
|
||||
const _this = this;
|
||||
this._closeCalled = true;
|
||||
|
||||
switch (this.readyState) {
|
||||
case 0: // disconnected
|
||||
callback();
|
||||
break;
|
||||
|
||||
case 1: // connected
|
||||
this.readyState = STATES.disconnecting;
|
||||
this.doClose(force, function(err) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
_this.onClose(force);
|
||||
callback(null);
|
||||
});
|
||||
|
||||
break;
|
||||
case 2: // connecting
|
||||
this.once('open', function() {
|
||||
_this.close(callback);
|
||||
});
|
||||
break;
|
||||
|
||||
case 3: // disconnecting
|
||||
this.once('close', function() {
|
||||
callback();
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Called when the connection closes
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Connection.prototype.onClose = function(force) {
|
||||
this.readyState = STATES.disconnected;
|
||||
|
||||
// avoid having the collection subscribe to our event emitter
|
||||
// to prevent 0.3 warning
|
||||
for (const i in this.collections) {
|
||||
if (utils.object.hasOwnProperty(this.collections, i)) {
|
||||
this.collections[i].onClose(force);
|
||||
}
|
||||
}
|
||||
|
||||
this.emit('close', force);
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieves a collection, creating it if not cached.
|
||||
*
|
||||
* Not typically needed by applications. Just talk to your collection through your model.
|
||||
*
|
||||
* @param {String} name of the collection
|
||||
* @param {Object} [options] optional collection options
|
||||
* @return {Collection} collection instance
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Connection.prototype.collection = function(name, options) {
|
||||
options = options ? utils.clone(options) : {};
|
||||
options.$wasForceClosed = this.$wasForceClosed;
|
||||
if (!(name in this.collections)) {
|
||||
this.collections[name] = new Collection(name, this, options);
|
||||
}
|
||||
return this.collections[name];
|
||||
};
|
||||
|
||||
/**
|
||||
* Defines or retrieves a model.
|
||||
*
|
||||
* var mongoose = require('mongoose');
|
||||
* var db = mongoose.createConnection(..);
|
||||
* db.model('Venue', new Schema(..));
|
||||
* var Ticket = db.model('Ticket', new Schema(..));
|
||||
* var Venue = db.model('Venue');
|
||||
*
|
||||
* _When no `collection` argument is passed, Mongoose produces a collection name by passing the model `name` to the [utils.toCollectionName](#utils_exports.toCollectionName) method. This method pluralizes the name. If you don't like this behavior, either pass a collection name or set your schemas collection name option._
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* var schema = new Schema({ name: String }, { collection: 'actor' });
|
||||
*
|
||||
* // or
|
||||
*
|
||||
* schema.set('collection', 'actor');
|
||||
*
|
||||
* // or
|
||||
*
|
||||
* var collectionName = 'actor'
|
||||
* var M = conn.model('Actor', schema, collectionName)
|
||||
*
|
||||
* @param {String|Function} name the model name or class extending Model
|
||||
* @param {Schema} [schema] a schema. necessary when defining a model
|
||||
* @param {String} [collection] name of mongodb collection (optional) if not given it will be induced from model name
|
||||
* @see Mongoose#model #index_Mongoose-model
|
||||
* @return {Model} The compiled model
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Connection.prototype.model = function(name, schema, collection) {
|
||||
if (!(this instanceof Connection)) {
|
||||
throw new MongooseError('`connection.model()` should not be run with ' +
|
||||
'`new`. If you are doing `new db.model(foo)(bar)`, use ' +
|
||||
'`db.model(foo)(bar)` instead');
|
||||
}
|
||||
|
||||
let fn;
|
||||
if (typeof name === 'function') {
|
||||
fn = name;
|
||||
name = fn.name;
|
||||
}
|
||||
|
||||
// collection name discovery
|
||||
if (typeof schema === 'string') {
|
||||
collection = schema;
|
||||
schema = false;
|
||||
}
|
||||
|
||||
if (utils.isObject(schema) && !schema.instanceOfSchema) {
|
||||
schema = new Schema(schema);
|
||||
}
|
||||
if (schema && !schema.instanceOfSchema) {
|
||||
throw new Error('The 2nd parameter to `mongoose.model()` should be a ' +
|
||||
'schema or a POJO');
|
||||
}
|
||||
|
||||
if (this.models[name] && !collection) {
|
||||
// model exists but we are not subclassing with custom collection
|
||||
if (schema && schema.instanceOfSchema && schema !== this.models[name].schema) {
|
||||
throw new MongooseError.OverwriteModelError(name);
|
||||
}
|
||||
return this.models[name];
|
||||
}
|
||||
|
||||
const opts = {cache: false, connection: this};
|
||||
let model;
|
||||
|
||||
if (schema && schema.instanceOfSchema) {
|
||||
// compile a model
|
||||
model = this.base.model(fn || name, schema, collection, opts);
|
||||
|
||||
// only the first model with this name is cached to allow
|
||||
// for one-offs with custom collection names etc.
|
||||
if (!this.models[name]) {
|
||||
this.models[name] = model;
|
||||
}
|
||||
|
||||
// Errors handled internally, so safe to ignore error
|
||||
model.init(function $modelInitNoop() {});
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
if (this.models[name] && collection) {
|
||||
// subclassing current model with alternate collection
|
||||
model = this.models[name];
|
||||
schema = model.prototype.schema;
|
||||
const sub = model.__subclass(this, schema, collection);
|
||||
// do not cache the sub model
|
||||
return sub;
|
||||
}
|
||||
|
||||
// lookup model in mongoose module
|
||||
model = this.base.models[name];
|
||||
|
||||
if (!model) {
|
||||
throw new MongooseError.MissingSchemaError(name);
|
||||
}
|
||||
|
||||
if (this === model.prototype.db
|
||||
&& (!collection || collection === model.collection.name)) {
|
||||
// model already uses this connection.
|
||||
|
||||
// only the first model with this name is cached to allow
|
||||
// for one-offs with custom collection names etc.
|
||||
if (!this.models[name]) {
|
||||
this.models[name] = model;
|
||||
}
|
||||
|
||||
return model;
|
||||
}
|
||||
this.models[name] = model.__subclass(this, schema, collection);
|
||||
return this.models[name];
|
||||
};
|
||||
|
||||
/**
|
||||
* Removes the model named `name` from this connection, if it exists. You can
|
||||
* use this function to clean up any models you created in your tests to
|
||||
* prevent OverwriteModelErrors.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* conn.model('User', new Schema({ name: String }));
|
||||
* console.log(conn.model('User')); // Model object
|
||||
* conn.deleteModel('User');
|
||||
* console.log(conn.model('User')); // undefined
|
||||
*
|
||||
* // Usually useful in a Mocha `afterEach()` hook
|
||||
* afterEach(function() {
|
||||
* conn.deleteModel(/.+/); // Delete every model
|
||||
* });
|
||||
*
|
||||
* @api public
|
||||
* @param {String|RegExp} name if string, the name of the model to remove. If regexp, removes all models whose name matches the regexp.
|
||||
* @return {Connection} this
|
||||
*/
|
||||
|
||||
Connection.prototype.deleteModel = function(name) {
|
||||
if (typeof name === 'string') {
|
||||
const model = this.model(name);
|
||||
if (model == null) {
|
||||
return this;
|
||||
}
|
||||
delete this.models[name];
|
||||
delete this.collections[model.collection.name];
|
||||
delete this.base.modelSchemas[name];
|
||||
} else if (name instanceof RegExp) {
|
||||
const pattern = name;
|
||||
const names = this.modelNames();
|
||||
for (const name of names) {
|
||||
if (pattern.test(name)) {
|
||||
this.deleteModel(name);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw new Error('First parameter to `deleteModel()` must be a string ' +
|
||||
'or regexp, got "' + name + '"');
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns an array of model names created on this connection.
|
||||
* @api public
|
||||
* @return {Array}
|
||||
*/
|
||||
|
||||
Connection.prototype.modelNames = function() {
|
||||
return Object.keys(this.models);
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Returns if the connection requires authentication after it is opened. Generally if a
|
||||
* username and password are both provided than authentication is needed, but in some cases a
|
||||
* password is not required.
|
||||
* @api private
|
||||
* @return {Boolean} true if the connection should be authenticated after it is opened, otherwise false.
|
||||
*/
|
||||
Connection.prototype.shouldAuthenticate = function() {
|
||||
return this.user != null &&
|
||||
(this.pass != null || this.authMechanismDoesNotRequirePassword());
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Returns a boolean value that specifies if the current authentication mechanism needs a
|
||||
* password to authenticate according to the auth objects passed into the openUri methods.
|
||||
* @api private
|
||||
* @return {Boolean} true if the authentication mechanism specified in the options object requires
|
||||
* a password, otherwise false.
|
||||
*/
|
||||
Connection.prototype.authMechanismDoesNotRequirePassword = function() {
|
||||
if (this.options && this.options.auth) {
|
||||
return noPasswordAuthMechanisms.indexOf(this.options.auth.authMechanism) >= 0;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Returns a boolean value that specifies if the provided objects object provides enough
|
||||
* data to authenticate with. Generally this is true if the username and password are both specified
|
||||
* but in some authentication methods, a password is not required for authentication so only a username
|
||||
* is required.
|
||||
* @param {Object} [options] the options object passed into the openUri methods.
|
||||
* @api private
|
||||
* @return {Boolean} true if the provided options object provides enough data to authenticate with,
|
||||
* otherwise false.
|
||||
*/
|
||||
Connection.prototype.optionsProvideAuthenticationData = function(options) {
|
||||
return (options) &&
|
||||
(options.user) &&
|
||||
((options.pass) || this.authMechanismDoesNotRequirePassword());
|
||||
};
|
||||
|
||||
/**
|
||||
* Switches to a different database using the same connection pool.
|
||||
*
|
||||
* Returns a new connection object, with the new db.
|
||||
*
|
||||
* @method useDb
|
||||
* @memberOf Connection
|
||||
* @param {String} name The database name
|
||||
* @return {Connection} New Connection Object
|
||||
* @api public
|
||||
*/
|
||||
|
||||
/*!
|
||||
* Module exports.
|
||||
*/
|
||||
|
||||
Connection.STATES = STATES;
|
||||
module.exports = Connection;
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
|
||||
/*!
|
||||
* Connection states
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const STATES = module.exports = exports = Object.create(null);
|
||||
|
||||
const disconnected = 'disconnected';
|
||||
const connected = 'connected';
|
||||
const connecting = 'connecting';
|
||||
const disconnecting = 'disconnecting';
|
||||
const uninitialized = 'uninitialized';
|
||||
|
||||
STATES[0] = disconnected;
|
||||
STATES[1] = connected;
|
||||
STATES[2] = connecting;
|
||||
STATES[3] = disconnecting;
|
||||
STATES[99] = uninitialized;
|
||||
|
||||
STATES[disconnected] = 0;
|
||||
STATES[connected] = 1;
|
||||
STATES[connecting] = 2;
|
||||
STATES[disconnecting] = 3;
|
||||
STATES[uninitialized] = 99;
|
||||
+296
@@ -0,0 +1,296 @@
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const Readable = require('stream').Readable;
|
||||
const eachAsync = require('../helpers/cursor/eachAsync');
|
||||
const util = require('util');
|
||||
const utils = require('../utils');
|
||||
|
||||
/**
|
||||
* An AggregationCursor is a concurrency primitive for processing aggregation
|
||||
* results one document at a time. It is analogous to QueryCursor.
|
||||
*
|
||||
* An AggregationCursor fulfills the Node.js streams3 API,
|
||||
* in addition to several other mechanisms for loading documents from MongoDB
|
||||
* one at a time.
|
||||
*
|
||||
* Creating an AggregationCursor executes the model's pre aggregate hooks,
|
||||
* but **not** the model's post aggregate hooks.
|
||||
*
|
||||
* Unless you're an advanced user, do **not** instantiate this class directly.
|
||||
* Use [`Aggregate#cursor()`](/docs/api.html#aggregate_Aggregate-cursor) instead.
|
||||
*
|
||||
* @param {Aggregate} agg
|
||||
* @param {Object} options
|
||||
* @inherits Readable
|
||||
* @event `cursor`: Emitted when the cursor is created
|
||||
* @event `error`: Emitted when an error occurred
|
||||
* @event `data`: Emitted when the stream is flowing and the next doc is ready
|
||||
* @event `end`: Emitted when the stream is exhausted
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function AggregationCursor(agg) {
|
||||
Readable.call(this, { objectMode: true });
|
||||
|
||||
this.cursor = null;
|
||||
this.agg = agg;
|
||||
this._transforms = [];
|
||||
const model = agg._model;
|
||||
delete agg.options.cursor.useMongooseAggCursor;
|
||||
this._mongooseOptions = {};
|
||||
|
||||
_init(model, this, agg);
|
||||
}
|
||||
|
||||
util.inherits(AggregationCursor, Readable);
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function _init(model, c, agg) {
|
||||
if (!model.collection.buffer) {
|
||||
model.hooks.execPre('aggregate', agg, function() {
|
||||
c.cursor = model.collection.aggregate(agg._pipeline, agg.options || {});
|
||||
c.emit('cursor', c.cursor);
|
||||
});
|
||||
} else {
|
||||
model.collection.emitter.once('queue', function() {
|
||||
model.hooks.execPre('aggregate', agg, function() {
|
||||
c.cursor = model.collection.aggregate(agg._pipeline, agg.options || {});
|
||||
c.emit('cursor', c.cursor);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* Necessary to satisfy the Readable API
|
||||
*/
|
||||
|
||||
AggregationCursor.prototype._read = function() {
|
||||
const _this = this;
|
||||
_next(this, function(error, doc) {
|
||||
if (error) {
|
||||
return _this.emit('error', error);
|
||||
}
|
||||
if (!doc) {
|
||||
_this.push(null);
|
||||
_this.cursor.close(function(error) {
|
||||
if (error) {
|
||||
return _this.emit('error', error);
|
||||
}
|
||||
setTimeout(function() {
|
||||
_this.emit('close');
|
||||
}, 0);
|
||||
});
|
||||
return;
|
||||
}
|
||||
_this.push(doc);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Registers a transform function which subsequently maps documents retrieved
|
||||
* via the streams interface or `.next()`
|
||||
*
|
||||
* ####Example
|
||||
*
|
||||
* // Map documents returned by `data` events
|
||||
* Thing.
|
||||
* find({ name: /^hello/ }).
|
||||
* cursor().
|
||||
* map(function (doc) {
|
||||
* doc.foo = "bar";
|
||||
* return doc;
|
||||
* })
|
||||
* on('data', function(doc) { console.log(doc.foo); });
|
||||
*
|
||||
* // Or map documents returned by `.next()`
|
||||
* var cursor = Thing.find({ name: /^hello/ }).
|
||||
* cursor().
|
||||
* map(function (doc) {
|
||||
* doc.foo = "bar";
|
||||
* return doc;
|
||||
* });
|
||||
* cursor.next(function(error, doc) {
|
||||
* console.log(doc.foo);
|
||||
* });
|
||||
*
|
||||
* @param {Function} fn
|
||||
* @return {AggregationCursor}
|
||||
* @api public
|
||||
* @method map
|
||||
*/
|
||||
|
||||
AggregationCursor.prototype.map = function(fn) {
|
||||
this._transforms.push(fn);
|
||||
return this;
|
||||
};
|
||||
|
||||
/*!
|
||||
* Marks this cursor as errored
|
||||
*/
|
||||
|
||||
AggregationCursor.prototype._markError = function(error) {
|
||||
this._error = error;
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Marks this cursor as closed. Will stop streaming and subsequent calls to
|
||||
* `next()` will error.
|
||||
*
|
||||
* @param {Function} callback
|
||||
* @return {Promise}
|
||||
* @api public
|
||||
* @method close
|
||||
* @emits close
|
||||
* @see MongoDB driver cursor#close http://mongodb.github.io/node-mongodb-native/2.1/api/Cursor.html#close
|
||||
*/
|
||||
|
||||
AggregationCursor.prototype.close = function(callback) {
|
||||
return utils.promiseOrCallback(callback, cb => {
|
||||
this.cursor.close(error => {
|
||||
if (error) {
|
||||
cb(error);
|
||||
return this.listeners('error').length > 0 && this.emit('error', error);
|
||||
}
|
||||
this.emit('close');
|
||||
cb(null);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the next document from this cursor. Will return `null` when there are
|
||||
* no documents left.
|
||||
*
|
||||
* @param {Function} callback
|
||||
* @return {Promise}
|
||||
* @api public
|
||||
* @method next
|
||||
*/
|
||||
|
||||
AggregationCursor.prototype.next = function(callback) {
|
||||
return utils.promiseOrCallback(callback, cb => {
|
||||
_next(this, cb);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Execute `fn` for every document in the cursor. If `fn` returns a promise,
|
||||
* will wait for the promise to resolve before iterating on to the next one.
|
||||
* Returns a promise that resolves when done.
|
||||
*
|
||||
* @param {Function} fn
|
||||
* @param {Object} [options]
|
||||
* @param {Number} [options.parallel] the number of promises to execute in parallel. Defaults to 1.
|
||||
* @param {Function} [callback] executed when all docs have been processed
|
||||
* @return {Promise}
|
||||
* @api public
|
||||
* @method eachAsync
|
||||
*/
|
||||
|
||||
AggregationCursor.prototype.eachAsync = function(fn, opts, callback) {
|
||||
const _this = this;
|
||||
if (typeof opts === 'function') {
|
||||
callback = opts;
|
||||
opts = {};
|
||||
}
|
||||
opts = opts || {};
|
||||
|
||||
return eachAsync(function(cb) { return _next(_this, cb); }, fn, opts, callback);
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
AggregationCursor.prototype.transformNull = function(val) {
|
||||
if (arguments.length === 0) {
|
||||
val = true;
|
||||
}
|
||||
this._mongooseOptions.transformNull = val;
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Adds a [cursor flag](http://mongodb.github.io/node-mongodb-native/2.2/api/Cursor.html#addCursorFlag).
|
||||
* Useful for setting the `noCursorTimeout` and `tailable` flags.
|
||||
*
|
||||
* @param {String} flag
|
||||
* @param {Boolean} value
|
||||
* @return {AggregationCursor} this
|
||||
* @api public
|
||||
* @method addCursorFlag
|
||||
*/
|
||||
|
||||
AggregationCursor.prototype.addCursorFlag = function(flag, value) {
|
||||
const _this = this;
|
||||
_waitForCursor(this, function() {
|
||||
_this.cursor.addCursorFlag(flag, value);
|
||||
});
|
||||
return this;
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function _waitForCursor(ctx, cb) {
|
||||
if (ctx.cursor) {
|
||||
return cb();
|
||||
}
|
||||
ctx.once('cursor', function() {
|
||||
cb();
|
||||
});
|
||||
}
|
||||
|
||||
/*!
|
||||
* Get the next doc from the underlying cursor and mongooseify it
|
||||
* (populate, etc.)
|
||||
*/
|
||||
|
||||
function _next(ctx, cb) {
|
||||
let callback = cb;
|
||||
if (ctx._transforms.length) {
|
||||
callback = function(err, doc) {
|
||||
if (err || (doc === null && !ctx._mongooseOptions.transformNull)) {
|
||||
return cb(err, doc);
|
||||
}
|
||||
cb(err, ctx._transforms.reduce(function(doc, fn) {
|
||||
return fn(doc);
|
||||
}, doc));
|
||||
};
|
||||
}
|
||||
|
||||
if (ctx._error) {
|
||||
return process.nextTick(function() {
|
||||
callback(ctx._error);
|
||||
});
|
||||
}
|
||||
|
||||
if (ctx.cursor) {
|
||||
return ctx.cursor.next(function(error, doc) {
|
||||
if (error) {
|
||||
return callback(error);
|
||||
}
|
||||
if (!doc) {
|
||||
return callback(null, null);
|
||||
}
|
||||
|
||||
callback(null, doc);
|
||||
});
|
||||
} else {
|
||||
ctx.once('cursor', function() {
|
||||
_next(ctx, cb);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = AggregationCursor;
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
'use strict';
|
||||
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
const EventEmitter = require('events').EventEmitter;
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
class ChangeStream extends EventEmitter {
|
||||
constructor(model, pipeline, options) {
|
||||
super();
|
||||
|
||||
this.driverChangeStream = null;
|
||||
this.closed = false;
|
||||
// This wrapper is necessary because of buffering.
|
||||
if (model.collection.buffer) {
|
||||
model.collection.addQueue(() => {
|
||||
if (this.closed) {
|
||||
return;
|
||||
}
|
||||
this.driverChangeStream = model.collection.watch(pipeline, options);
|
||||
this._bindEvents();
|
||||
this.emit('ready');
|
||||
});
|
||||
} else {
|
||||
this.driverChangeStream = model.collection.watch(pipeline, options);
|
||||
this._bindEvents();
|
||||
this.emit('ready');
|
||||
}
|
||||
}
|
||||
|
||||
_bindEvents() {
|
||||
['close', 'change', 'end', 'error'].forEach(ev => {
|
||||
this.driverChangeStream.on(ev, data => this.emit(ev, data));
|
||||
});
|
||||
}
|
||||
|
||||
_queue(cb) {
|
||||
this.once('ready', () => cb());
|
||||
}
|
||||
|
||||
close() {
|
||||
this.closed = true;
|
||||
if (this.driverChangeStream) {
|
||||
this.driverChangeStream.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
module.exports = ChangeStream;
|
||||
+331
@@ -0,0 +1,331 @@
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const Readable = require('stream').Readable;
|
||||
const eachAsync = require('../helpers/cursor/eachAsync');
|
||||
const helpers = require('../queryhelpers');
|
||||
const util = require('util');
|
||||
const utils = require('../utils');
|
||||
|
||||
/**
|
||||
* A QueryCursor is a concurrency primitive for processing query results
|
||||
* one document at a time. A QueryCursor fulfills the Node.js streams3 API,
|
||||
* in addition to several other mechanisms for loading documents from MongoDB
|
||||
* one at a time.
|
||||
*
|
||||
* QueryCursors execute the model's pre find hooks, but **not** the model's
|
||||
* post find hooks.
|
||||
*
|
||||
* Unless you're an advanced user, do **not** instantiate this class directly.
|
||||
* Use [`Query#cursor()`](/docs/api.html#query_Query-cursor) instead.
|
||||
*
|
||||
* @param {Query} query
|
||||
* @param {Object} options query options passed to `.find()`
|
||||
* @inherits Readable
|
||||
* @event `cursor`: Emitted when the cursor is created
|
||||
* @event `error`: Emitted when an error occurred
|
||||
* @event `data`: Emitted when the stream is flowing and the next doc is ready
|
||||
* @event `end`: Emitted when the stream is exhausted
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function QueryCursor(query, options) {
|
||||
Readable.call(this, { objectMode: true });
|
||||
|
||||
this.cursor = null;
|
||||
this.query = query;
|
||||
const _this = this;
|
||||
const model = query.model;
|
||||
this._mongooseOptions = {};
|
||||
this._transforms = [];
|
||||
this.model = model;
|
||||
model.hooks.execPre('find', query, () => {
|
||||
this._transforms = this._transforms.concat(query._transforms.slice());
|
||||
if (options.transform) {
|
||||
this._transforms.push(options.transform);
|
||||
}
|
||||
model.collection.find(query._conditions, options, function(err, cursor) {
|
||||
if (_this._error) {
|
||||
cursor.close(function() {});
|
||||
_this.listeners('error').length > 0 && _this.emit('error', _this._error);
|
||||
}
|
||||
if (err) {
|
||||
return _this.emit('error', err);
|
||||
}
|
||||
_this.cursor = cursor;
|
||||
_this.emit('cursor', cursor);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
util.inherits(QueryCursor, Readable);
|
||||
|
||||
/*!
|
||||
* Necessary to satisfy the Readable API
|
||||
*/
|
||||
|
||||
QueryCursor.prototype._read = function() {
|
||||
const _this = this;
|
||||
_next(this, function(error, doc) {
|
||||
if (error) {
|
||||
return _this.emit('error', error);
|
||||
}
|
||||
if (!doc) {
|
||||
_this.push(null);
|
||||
_this.cursor.close(function(error) {
|
||||
if (error) {
|
||||
return _this.emit('error', error);
|
||||
}
|
||||
setTimeout(function() {
|
||||
_this.emit('close');
|
||||
}, 0);
|
||||
});
|
||||
return;
|
||||
}
|
||||
_this.push(doc);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Registers a transform function which subsequently maps documents retrieved
|
||||
* via the streams interface or `.next()`
|
||||
*
|
||||
* ####Example
|
||||
*
|
||||
* // Map documents returned by `data` events
|
||||
* Thing.
|
||||
* find({ name: /^hello/ }).
|
||||
* cursor().
|
||||
* map(function (doc) {
|
||||
* doc.foo = "bar";
|
||||
* return doc;
|
||||
* })
|
||||
* on('data', function(doc) { console.log(doc.foo); });
|
||||
*
|
||||
* // Or map documents returned by `.next()`
|
||||
* var cursor = Thing.find({ name: /^hello/ }).
|
||||
* cursor().
|
||||
* map(function (doc) {
|
||||
* doc.foo = "bar";
|
||||
* return doc;
|
||||
* });
|
||||
* cursor.next(function(error, doc) {
|
||||
* console.log(doc.foo);
|
||||
* });
|
||||
*
|
||||
* @param {Function} fn
|
||||
* @return {QueryCursor}
|
||||
* @api public
|
||||
* @method map
|
||||
*/
|
||||
|
||||
QueryCursor.prototype.map = function(fn) {
|
||||
this._transforms.push(fn);
|
||||
return this;
|
||||
};
|
||||
|
||||
/*!
|
||||
* Marks this cursor as errored
|
||||
*/
|
||||
|
||||
QueryCursor.prototype._markError = function(error) {
|
||||
this._error = error;
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Marks this cursor as closed. Will stop streaming and subsequent calls to
|
||||
* `next()` will error.
|
||||
*
|
||||
* @param {Function} callback
|
||||
* @return {Promise}
|
||||
* @api public
|
||||
* @method close
|
||||
* @emits close
|
||||
* @see MongoDB driver cursor#close http://mongodb.github.io/node-mongodb-native/2.1/api/Cursor.html#close
|
||||
*/
|
||||
|
||||
QueryCursor.prototype.close = function(callback) {
|
||||
return utils.promiseOrCallback(callback, cb => {
|
||||
this.cursor.close(error => {
|
||||
if (error) {
|
||||
cb(error);
|
||||
return this.listeners('error').length > 0 && this.emit('error', error);
|
||||
}
|
||||
this.emit('close');
|
||||
cb(null);
|
||||
});
|
||||
}, this.model.events);
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the next document from this cursor. Will return `null` when there are
|
||||
* no documents left.
|
||||
*
|
||||
* @param {Function} callback
|
||||
* @return {Promise}
|
||||
* @api public
|
||||
* @method next
|
||||
*/
|
||||
|
||||
QueryCursor.prototype.next = function(callback) {
|
||||
return utils.promiseOrCallback(callback, cb => {
|
||||
_next(this, function(error, doc) {
|
||||
if (error) {
|
||||
return cb(error);
|
||||
}
|
||||
cb(null, doc);
|
||||
});
|
||||
}, this.model.events);
|
||||
};
|
||||
|
||||
/**
|
||||
* Execute `fn` for every document in the cursor. If `fn` returns a promise,
|
||||
* will wait for the promise to resolve before iterating on to the next one.
|
||||
* Returns a promise that resolves when done.
|
||||
*
|
||||
* @param {Function} fn
|
||||
* @param {Object} [options]
|
||||
* @param {Number} [options.parallel] the number of promises to execute in parallel. Defaults to 1.
|
||||
* @param {Function} [callback] executed when all docs have been processed
|
||||
* @return {Promise}
|
||||
* @api public
|
||||
* @method eachAsync
|
||||
*/
|
||||
|
||||
QueryCursor.prototype.eachAsync = function(fn, opts, callback) {
|
||||
const _this = this;
|
||||
if (typeof opts === 'function') {
|
||||
callback = opts;
|
||||
opts = {};
|
||||
}
|
||||
opts = opts || {};
|
||||
|
||||
return eachAsync(function(cb) { return _next(_this, cb); }, fn, opts, callback);
|
||||
};
|
||||
|
||||
/**
|
||||
* Adds a [cursor flag](http://mongodb.github.io/node-mongodb-native/2.2/api/Cursor.html#addCursorFlag).
|
||||
* Useful for setting the `noCursorTimeout` and `tailable` flags.
|
||||
*
|
||||
* @param {String} flag
|
||||
* @param {Boolean} value
|
||||
* @return {AggregationCursor} this
|
||||
* @api public
|
||||
* @method addCursorFlag
|
||||
*/
|
||||
|
||||
QueryCursor.prototype.addCursorFlag = function(flag, value) {
|
||||
const _this = this;
|
||||
_waitForCursor(this, function() {
|
||||
_this.cursor.addCursorFlag(flag, value);
|
||||
});
|
||||
return this;
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
QueryCursor.prototype.transformNull = function(val) {
|
||||
if (arguments.length === 0) {
|
||||
val = true;
|
||||
}
|
||||
this._mongooseOptions.transformNull = val;
|
||||
return this;
|
||||
};
|
||||
|
||||
/*!
|
||||
* Get the next doc from the underlying cursor and mongooseify it
|
||||
* (populate, etc.)
|
||||
*/
|
||||
|
||||
function _next(ctx, cb) {
|
||||
let callback = cb;
|
||||
if (ctx._transforms.length) {
|
||||
callback = function(err, doc) {
|
||||
if (err || (doc === null && !ctx._mongooseOptions.transformNull)) {
|
||||
return cb(err, doc);
|
||||
}
|
||||
cb(err, ctx._transforms.reduce(function(doc, fn) {
|
||||
return fn.call(ctx, doc);
|
||||
}, doc));
|
||||
};
|
||||
}
|
||||
|
||||
if (ctx._error) {
|
||||
return process.nextTick(function() {
|
||||
callback(ctx._error);
|
||||
});
|
||||
}
|
||||
|
||||
if (ctx.cursor) {
|
||||
return ctx.cursor.next(function(error, doc) {
|
||||
if (error) {
|
||||
return callback(error);
|
||||
}
|
||||
if (!doc) {
|
||||
return callback(null, null);
|
||||
}
|
||||
|
||||
const opts = ctx.query._mongooseOptions;
|
||||
if (!opts.populate) {
|
||||
return opts.lean ?
|
||||
callback(null, doc) :
|
||||
_create(ctx, doc, null, callback);
|
||||
}
|
||||
|
||||
const pop = helpers.preparePopulationOptionsMQ(ctx.query,
|
||||
ctx.query._mongooseOptions);
|
||||
pop.__noPromise = true;
|
||||
ctx.query.model.populate(doc, pop, function(err, doc) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
return opts.lean ?
|
||||
callback(null, doc) :
|
||||
_create(ctx, doc, pop, callback);
|
||||
});
|
||||
});
|
||||
} else {
|
||||
ctx.once('cursor', function() {
|
||||
_next(ctx, cb);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function _waitForCursor(ctx, cb) {
|
||||
if (ctx.cursor) {
|
||||
return cb();
|
||||
}
|
||||
ctx.once('cursor', function() {
|
||||
cb();
|
||||
});
|
||||
}
|
||||
|
||||
/*!
|
||||
* Convert a raw doc into a full mongoose doc.
|
||||
*/
|
||||
|
||||
function _create(ctx, doc, populatedIds, cb) {
|
||||
const instance = helpers.createModel(ctx.query.model, doc, ctx.query._fields);
|
||||
const opts = populatedIds ?
|
||||
{ populated: populatedIds } :
|
||||
undefined;
|
||||
|
||||
instance.init(doc, opts, function(err) {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
cb(null, instance);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = QueryCursor;
|
||||
+3170
File diff suppressed because it is too large
Load Diff
+30
@@ -0,0 +1,30 @@
|
||||
'use strict';
|
||||
|
||||
/* eslint-env browser */
|
||||
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
const Document = require('./document.js');
|
||||
const BrowserDocument = require('./browserDocument.js');
|
||||
|
||||
let isBrowser = false;
|
||||
|
||||
/**
|
||||
* Returns the Document constructor for the current context
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
module.exports = function() {
|
||||
if (isBrowser) {
|
||||
return BrowserDocument;
|
||||
}
|
||||
return Document;
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
module.exports.setBrowser = function(flag) {
|
||||
isBrowser = flag;
|
||||
};
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
'use strict';
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
let driver = null;
|
||||
|
||||
module.exports.get = function() {
|
||||
return driver;
|
||||
};
|
||||
|
||||
module.exports.set = function(v) {
|
||||
driver = v;
|
||||
};
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
|
||||
# Driver Spec
|
||||
|
||||
TODO
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
module.exports = function() {};
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const Binary = require('bson').Binary;
|
||||
|
||||
/*!
|
||||
* Module exports.
|
||||
*/
|
||||
|
||||
module.exports = exports = Binary;
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
module.exports = require('bson').Decimal128;
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
/*!
|
||||
* Module exports.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
exports.Binary = require('./binary');
|
||||
exports.Collection = function() {
|
||||
throw new Error('Cannot create a collection from browser library');
|
||||
};
|
||||
exports.Decimal128 = require('./decimal128');
|
||||
exports.ObjectId = require('./objectid');
|
||||
exports.ReadPreference = require('./ReadPreference');
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
|
||||
/*!
|
||||
* [node-mongodb-native](https://github.com/mongodb/node-mongodb-native) ObjectId
|
||||
* @constructor NodeMongoDbObjectId
|
||||
* @see ObjectId
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const ObjectId = require('bson').ObjectID;
|
||||
|
||||
/*!
|
||||
* Getter for convenience with populate, see gh-6115
|
||||
*/
|
||||
|
||||
Object.defineProperty(ObjectId.prototype, '_id', {
|
||||
enumerable: false,
|
||||
configurable: true,
|
||||
get: function() {
|
||||
return this;
|
||||
}
|
||||
});
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
module.exports = exports = ObjectId;
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const mongodb = require('mongodb');
|
||||
const ReadPref = mongodb.ReadPreference;
|
||||
|
||||
/*!
|
||||
* Converts arguments to ReadPrefs the driver
|
||||
* can understand.
|
||||
*
|
||||
* @param {String|Array} pref
|
||||
* @param {Array} [tags]
|
||||
*/
|
||||
|
||||
module.exports = function readPref(pref, tags) {
|
||||
if (Array.isArray(pref)) {
|
||||
tags = pref[1];
|
||||
pref = pref[0];
|
||||
}
|
||||
|
||||
if (pref instanceof ReadPref) {
|
||||
return pref;
|
||||
}
|
||||
|
||||
switch (pref) {
|
||||
case 'p':
|
||||
pref = 'primary';
|
||||
break;
|
||||
case 'pp':
|
||||
pref = 'primaryPreferred';
|
||||
break;
|
||||
case 's':
|
||||
pref = 'secondary';
|
||||
break;
|
||||
case 'sp':
|
||||
pref = 'secondaryPreferred';
|
||||
break;
|
||||
case 'n':
|
||||
pref = 'nearest';
|
||||
break;
|
||||
}
|
||||
|
||||
return new ReadPref(pref, tags);
|
||||
};
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const Binary = require('mongodb').Binary;
|
||||
|
||||
module.exports = exports = Binary;
|
||||
+332
@@ -0,0 +1,332 @@
|
||||
'use strict';
|
||||
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
const MongooseCollection = require('../../collection');
|
||||
const Collection = require('mongodb').Collection;
|
||||
const get = require('../../helpers/get');
|
||||
const sliced = require('sliced');
|
||||
const stream = require('stream');
|
||||
const util = require('util');
|
||||
|
||||
/**
|
||||
* A [node-mongodb-native](https://github.com/mongodb/node-mongodb-native) collection implementation.
|
||||
*
|
||||
* All methods methods from the [node-mongodb-native](https://github.com/mongodb/node-mongodb-native) driver are copied and wrapped in queue management.
|
||||
*
|
||||
* @inherits Collection
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function NativeCollection() {
|
||||
this.collection = null;
|
||||
MongooseCollection.apply(this, arguments);
|
||||
}
|
||||
|
||||
/*!
|
||||
* Inherit from abstract Collection.
|
||||
*/
|
||||
|
||||
NativeCollection.prototype.__proto__ = MongooseCollection.prototype;
|
||||
|
||||
/**
|
||||
* Called when the connection opens.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
NativeCollection.prototype.onOpen = function() {
|
||||
const _this = this;
|
||||
|
||||
// always get a new collection in case the user changed host:port
|
||||
// of parent db instance when re-opening the connection.
|
||||
|
||||
if (!_this.opts.capped.size) {
|
||||
// non-capped
|
||||
callback(null, _this.conn.db.collection(_this.name));
|
||||
return _this.collection;
|
||||
}
|
||||
|
||||
// capped
|
||||
return _this.conn.db.collection(_this.name, function(err, c) {
|
||||
if (err) return callback(err);
|
||||
|
||||
// discover if this collection exists and if it is capped
|
||||
_this.conn.db.listCollections({name: _this.name}).toArray(function(err, docs) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
const doc = docs[0];
|
||||
const exists = !!doc;
|
||||
|
||||
if (exists) {
|
||||
if (doc.options && doc.options.capped) {
|
||||
callback(null, c);
|
||||
} else {
|
||||
const msg = 'A non-capped collection exists with the name: ' + _this.name + '\n\n'
|
||||
+ ' To use this collection as a capped collection, please '
|
||||
+ 'first convert it.\n'
|
||||
+ ' http://www.mongodb.org/display/DOCS/Capped+Collections#CappedCollections-Convertingacollectiontocapped';
|
||||
err = new Error(msg);
|
||||
callback(err);
|
||||
}
|
||||
} else {
|
||||
// create
|
||||
const opts = Object.assign({}, _this.opts.capped);
|
||||
opts.capped = true;
|
||||
_this.conn.db.createCollection(_this.name, opts, callback);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
function callback(err, collection) {
|
||||
if (err) {
|
||||
// likely a strict mode error
|
||||
_this.conn.emit('error', err);
|
||||
} else {
|
||||
_this.collection = collection;
|
||||
MongooseCollection.prototype.onOpen.call(_this);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Called when the connection closes
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
NativeCollection.prototype.onClose = function(force) {
|
||||
MongooseCollection.prototype.onClose.call(this, force);
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
const syncCollectionMethods = { watch: true };
|
||||
|
||||
/*!
|
||||
* Copy the collection methods and make them subject to queues
|
||||
*/
|
||||
|
||||
function iter(i) {
|
||||
NativeCollection.prototype[i] = function() {
|
||||
const collection = this.collection;
|
||||
const args = arguments;
|
||||
const _this = this;
|
||||
const debug = _this.conn.base.options.debug;
|
||||
|
||||
// If user force closed, queueing will hang forever. See #5664
|
||||
if (this.opts.$wasForceClosed) {
|
||||
return this.conn.db.collection(this.name)[i].apply(collection, args);
|
||||
}
|
||||
if (this.buffer) {
|
||||
if (syncCollectionMethods[i]) {
|
||||
throw new Error('Collection method ' + i + ' is synchronous');
|
||||
}
|
||||
this.addQueue(i, arguments);
|
||||
return;
|
||||
}
|
||||
|
||||
if (debug) {
|
||||
if (typeof debug === 'function') {
|
||||
debug.apply(_this,
|
||||
[_this.name, i].concat(sliced(args, 0, args.length - 1)));
|
||||
} else if (debug instanceof stream.Writable) {
|
||||
this.$printToStream(_this.name, i, args, debug);
|
||||
} else {
|
||||
this.$print(_this.name, i, args);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
return collection[i].apply(collection, args);
|
||||
} catch (error) {
|
||||
// Collection operation may throw because of max bson size, catch it here
|
||||
// See gh-3906
|
||||
if (args.length > 0 &&
|
||||
typeof args[args.length - 1] === 'function') {
|
||||
args[args.length - 1](error);
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
for (const i in Collection.prototype) {
|
||||
// Janky hack to work around gh-3005 until we can get rid of the mongoose
|
||||
// collection abstraction
|
||||
try {
|
||||
if (typeof Collection.prototype[i] !== 'function') {
|
||||
continue;
|
||||
}
|
||||
} catch (e) {
|
||||
continue;
|
||||
}
|
||||
|
||||
iter(i);
|
||||
}
|
||||
|
||||
/**
|
||||
* Debug print helper
|
||||
*
|
||||
* @api public
|
||||
* @method $print
|
||||
*/
|
||||
|
||||
NativeCollection.prototype.$print = function(name, i, args) {
|
||||
const moduleName = '\x1B[0;36mMongoose:\x1B[0m ';
|
||||
const functionCall = [name, i].join('.');
|
||||
const _args = [];
|
||||
for (let j = args.length - 1; j >= 0; --j) {
|
||||
if (this.$format(args[j]) || _args.length) {
|
||||
_args.unshift(this.$format(args[j]));
|
||||
}
|
||||
}
|
||||
const params = '(' + _args.join(', ') + ')';
|
||||
|
||||
console.info(moduleName + functionCall + params);
|
||||
};
|
||||
|
||||
/**
|
||||
* Debug print helper
|
||||
*
|
||||
* @api public
|
||||
* @method $print
|
||||
*/
|
||||
|
||||
NativeCollection.prototype.$printToStream = function(name, i, args, stream) {
|
||||
const functionCall = [name, i].join('.');
|
||||
const _args = [];
|
||||
for (let j = args.length - 1; j >= 0; --j) {
|
||||
if (this.$format(args[j]) || _args.length) {
|
||||
_args.unshift(this.$format(args[j]));
|
||||
}
|
||||
}
|
||||
const params = '(' + _args.join(', ') + ')';
|
||||
|
||||
stream.write(functionCall + params, 'utf8');
|
||||
};
|
||||
|
||||
/**
|
||||
* Formatter for debug print args
|
||||
*
|
||||
* @api public
|
||||
* @method $format
|
||||
*/
|
||||
|
||||
NativeCollection.prototype.$format = function(arg) {
|
||||
const type = typeof arg;
|
||||
if (type === 'function' || type === 'undefined') return '';
|
||||
return format(arg);
|
||||
};
|
||||
|
||||
/*!
|
||||
* Debug print helper
|
||||
*/
|
||||
|
||||
function inspectable(representation) {
|
||||
const ret = {
|
||||
inspect: function() { return representation; },
|
||||
};
|
||||
if (util.inspect.custom) {
|
||||
ret[util.inspect.custom] = ret.inspect;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
function map(o) {
|
||||
return format(o, true);
|
||||
}
|
||||
function formatObjectId(x, key) {
|
||||
x[key] = inspectable('ObjectId("' + x[key].toHexString() + '")');
|
||||
}
|
||||
function formatDate(x, key) {
|
||||
x[key] = inspectable('new Date("' + x[key].toUTCString() + '")');
|
||||
}
|
||||
function format(obj, sub) {
|
||||
if (obj && typeof obj.toBSON === 'function') {
|
||||
obj = obj.toBSON();
|
||||
}
|
||||
if (obj == null) {
|
||||
return obj;
|
||||
}
|
||||
|
||||
let x = require('../../utils').clone(obj, {transform: false});
|
||||
|
||||
if (x.constructor.name === 'Binary') {
|
||||
x = 'BinData(' + x.sub_type + ', "' + x.toString('base64') + '")';
|
||||
} else if (x.constructor.name === 'ObjectID') {
|
||||
x = inspectable('ObjectId("' + x.toHexString() + '")');
|
||||
} else if (x.constructor.name === 'Date') {
|
||||
x = inspectable('new Date("' + x.toUTCString() + '")');
|
||||
} else if (x.constructor.name === 'Object') {
|
||||
const keys = Object.keys(x);
|
||||
const numKeys = keys.length;
|
||||
let key;
|
||||
for (let i = 0; i < numKeys; ++i) {
|
||||
key = keys[i];
|
||||
if (x[key]) {
|
||||
let error;
|
||||
if (typeof x[key].toBSON === 'function') {
|
||||
try {
|
||||
// `session.toBSON()` throws an error. This means we throw errors
|
||||
// in debug mode when using transactions, see gh-6712. As a
|
||||
// workaround, catch `toBSON()` errors, try to serialize without
|
||||
// `toBSON()`, and rethrow if serialization still fails.
|
||||
x[key] = x[key].toBSON();
|
||||
} catch (_error) {
|
||||
error = _error;
|
||||
}
|
||||
}
|
||||
if (x[key].constructor.name === 'Binary') {
|
||||
x[key] = 'BinData(' + x[key].sub_type + ', "' +
|
||||
x[key].buffer.toString('base64') + '")';
|
||||
} else if (x[key].constructor.name === 'Object') {
|
||||
x[key] = format(x[key], true);
|
||||
} else if (x[key].constructor.name === 'ObjectID') {
|
||||
formatObjectId(x, key);
|
||||
} else if (x[key].constructor.name === 'Date') {
|
||||
formatDate(x, key);
|
||||
} else if (x[key].constructor.name === 'ClientSession') {
|
||||
x[key] = inspectable('ClientSession("' +
|
||||
get(x[key], 'id.id.buffer', '').toString('hex') + '")');
|
||||
} else if (Array.isArray(x[key])) {
|
||||
x[key] = x[key].map(map);
|
||||
} else if (error != null) {
|
||||
// If there was an error with `toBSON()` and the object wasn't
|
||||
// already converted to a string representation, rethrow it.
|
||||
// Open to better ideas on how to handle this.
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (sub) {
|
||||
return x;
|
||||
}
|
||||
|
||||
return util.
|
||||
inspect(x, false, 10, true).
|
||||
replace(/\n/g, '').
|
||||
replace(/\s{2,}/g, ' ');
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves information about this collections indexes.
|
||||
*
|
||||
* @param {Function} callback
|
||||
* @method getIndexes
|
||||
* @api public
|
||||
*/
|
||||
|
||||
NativeCollection.prototype.getIndexes = NativeCollection.prototype.indexInformation;
|
||||
|
||||
/*!
|
||||
* Module exports.
|
||||
*/
|
||||
|
||||
module.exports = NativeCollection;
|
||||
+181
@@ -0,0 +1,181 @@
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const MongooseConnection = require('../../connection');
|
||||
const STATES = require('../../connectionstate');
|
||||
|
||||
/**
|
||||
* A [node-mongodb-native](https://github.com/mongodb/node-mongodb-native) connection implementation.
|
||||
*
|
||||
* @inherits Connection
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function NativeConnection() {
|
||||
MongooseConnection.apply(this, arguments);
|
||||
this._listening = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Expose the possible connection states.
|
||||
* @api public
|
||||
*/
|
||||
|
||||
NativeConnection.STATES = STATES;
|
||||
|
||||
/*!
|
||||
* Inherits from Connection.
|
||||
*/
|
||||
|
||||
NativeConnection.prototype.__proto__ = MongooseConnection.prototype;
|
||||
|
||||
/**
|
||||
* Switches to a different database using the same connection pool.
|
||||
*
|
||||
* Returns a new connection object, with the new db.
|
||||
*
|
||||
* @param {String} name The database name
|
||||
* @return {Connection} New Connection Object
|
||||
* @api public
|
||||
*/
|
||||
|
||||
NativeConnection.prototype.useDb = function(name, options) {
|
||||
// Return immediately if cached
|
||||
if (options && options.useCache && this.relatedDbs[name]) {
|
||||
return this.relatedDbs[name];
|
||||
}
|
||||
|
||||
// we have to manually copy all of the attributes...
|
||||
const newConn = new this.constructor();
|
||||
newConn.name = name;
|
||||
newConn.base = this.base;
|
||||
newConn.collections = {};
|
||||
newConn.models = {};
|
||||
newConn.replica = this.replica;
|
||||
newConn.name = this.name;
|
||||
newConn.options = this.options;
|
||||
newConn._readyState = this._readyState;
|
||||
newConn._closeCalled = this._closeCalled;
|
||||
newConn._hasOpened = this._hasOpened;
|
||||
newConn._listening = false;
|
||||
|
||||
newConn.host = this.host;
|
||||
newConn.port = this.port;
|
||||
newConn.user = this.user;
|
||||
newConn.pass = this.pass;
|
||||
|
||||
// First, when we create another db object, we are not guaranteed to have a
|
||||
// db object to work with. So, in the case where we have a db object and it
|
||||
// is connected, we can just proceed with setting everything up. However, if
|
||||
// we do not have a db or the state is not connected, then we need to wait on
|
||||
// the 'open' event of the connection before doing the rest of the setup
|
||||
// the 'connected' event is the first time we'll have access to the db object
|
||||
|
||||
const _this = this;
|
||||
|
||||
newConn.client = _this.client;
|
||||
|
||||
if (this.db && this._readyState === STATES.connected) {
|
||||
wireup();
|
||||
} else {
|
||||
this.once('connected', wireup);
|
||||
}
|
||||
|
||||
function wireup() {
|
||||
newConn.client = _this.client;
|
||||
newConn.db = _this.client.db(name);
|
||||
newConn.onOpen();
|
||||
// setup the events appropriately
|
||||
listen(newConn);
|
||||
}
|
||||
|
||||
newConn.name = name;
|
||||
|
||||
// push onto the otherDbs stack, this is used when state changes
|
||||
this.otherDbs.push(newConn);
|
||||
newConn.otherDbs.push(this);
|
||||
|
||||
// push onto the relatedDbs cache, this is used when state changes
|
||||
if (options && options.useCache) {
|
||||
this.relatedDbs[newConn.name] = newConn;
|
||||
newConn.relatedDbs = this.relatedDbs;
|
||||
}
|
||||
|
||||
return newConn;
|
||||
};
|
||||
|
||||
/*!
|
||||
* Register listeners for important events and bubble appropriately.
|
||||
*/
|
||||
|
||||
function listen(conn) {
|
||||
if (conn.db._listening) {
|
||||
return;
|
||||
}
|
||||
conn.db._listening = true;
|
||||
|
||||
conn.db.on('close', function(force) {
|
||||
if (conn._closeCalled) return;
|
||||
|
||||
// the driver never emits an `open` event. auto_reconnect still
|
||||
// emits a `close` event but since we never get another
|
||||
// `open` we can't emit close
|
||||
if (conn.db.serverConfig.autoReconnect) {
|
||||
conn.readyState = STATES.disconnected;
|
||||
conn.emit('close');
|
||||
return;
|
||||
}
|
||||
conn.onClose(force);
|
||||
});
|
||||
conn.db.on('error', function(err) {
|
||||
conn.emit('error', err);
|
||||
});
|
||||
conn.db.on('reconnect', function() {
|
||||
conn.readyState = STATES.connected;
|
||||
conn.emit('reconnect');
|
||||
conn.emit('reconnected');
|
||||
conn.onOpen();
|
||||
});
|
||||
conn.db.on('timeout', function(err) {
|
||||
conn.emit('timeout', err);
|
||||
});
|
||||
conn.db.on('open', function(err, db) {
|
||||
if (STATES.disconnected === conn.readyState && db && db.databaseName) {
|
||||
conn.readyState = STATES.connected;
|
||||
conn.emit('reconnect');
|
||||
conn.emit('reconnected');
|
||||
}
|
||||
});
|
||||
conn.db.on('parseError', function(err) {
|
||||
conn.emit('parseError', err);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the connection
|
||||
*
|
||||
* @param {Boolean} [force]
|
||||
* @param {Function} [fn]
|
||||
* @return {Connection} this
|
||||
* @api private
|
||||
*/
|
||||
|
||||
NativeConnection.prototype.doClose = function(force, fn) {
|
||||
this.client.close(force, (err, res) => {
|
||||
// Defer because the driver will wait at least 1ms before finishing closing
|
||||
// the pool, see https://github.com/mongodb-js/mongodb-core/blob/a8f8e4ce41936babc3b9112bf42d609779f03b39/lib/connection/pool.js#L1026-L1030.
|
||||
// If there's queued operations, you may still get some background work
|
||||
// after the callback is called.
|
||||
setTimeout(() => fn(err, res), 1);
|
||||
});
|
||||
return this;
|
||||
};
|
||||
|
||||
/*!
|
||||
* Module exports.
|
||||
*/
|
||||
|
||||
module.exports = NativeConnection;
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
module.exports = require('mongodb').Decimal128;
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
/*!
|
||||
* Module exports.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
exports.Binary = require('./binary');
|
||||
exports.Collection = require('./collection');
|
||||
exports.Decimal128 = require('./decimal128');
|
||||
exports.ObjectId = require('./objectid');
|
||||
exports.ReadPreference = require('./ReadPreference');
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
|
||||
/*!
|
||||
* [node-mongodb-native](https://github.com/mongodb/node-mongodb-native) ObjectId
|
||||
* @constructor NodeMongoDbObjectId
|
||||
* @see ObjectId
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const ObjectId = require('mongodb').ObjectId;
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
module.exports = exports = ObjectId;
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const MongooseError = require('./');
|
||||
|
||||
/*!
|
||||
* MissingSchema Error constructor.
|
||||
*
|
||||
* @inherits MongooseError
|
||||
*/
|
||||
|
||||
function MissingSchemaError() {
|
||||
const msg = 'Schema hasn\'t been registered for document.\n'
|
||||
+ 'Use mongoose.Document(name, schema)';
|
||||
MongooseError.call(this, msg);
|
||||
this.name = 'MissingSchemaError';
|
||||
if (Error.captureStackTrace) {
|
||||
Error.captureStackTrace(this);
|
||||
} else {
|
||||
this.stack = new Error().stack;
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* Inherits from MongooseError.
|
||||
*/
|
||||
|
||||
MissingSchemaError.prototype = Object.create(MongooseError.prototype);
|
||||
MissingSchemaError.prototype.constructor = MongooseError;
|
||||
|
||||
/*!
|
||||
* exports
|
||||
*/
|
||||
|
||||
module.exports = MissingSchemaError;
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
'use strict';
|
||||
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
const MongooseError = require('./mongooseError');
|
||||
const util = require('util');
|
||||
|
||||
/**
|
||||
* Casting Error constructor.
|
||||
*
|
||||
* @param {String} type
|
||||
* @param {String} value
|
||||
* @inherits MongooseError
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function CastError(type, value, path, reason) {
|
||||
let stringValue = util.inspect(value);
|
||||
stringValue = stringValue.replace(/^'/, '"').replace(/'$/, '"');
|
||||
if (stringValue.charAt(0) !== '"') {
|
||||
stringValue = '"' + stringValue + '"';
|
||||
}
|
||||
MongooseError.call(this, 'Cast to ' + type + ' failed for value ' +
|
||||
stringValue + ' at path "' + path + '"');
|
||||
this.name = 'CastError';
|
||||
if (Error.captureStackTrace) {
|
||||
Error.captureStackTrace(this);
|
||||
} else {
|
||||
this.stack = new Error().stack;
|
||||
}
|
||||
this.stringValue = stringValue;
|
||||
this.kind = type;
|
||||
this.value = value;
|
||||
this.path = path;
|
||||
this.reason = reason;
|
||||
}
|
||||
|
||||
/*!
|
||||
* Inherits from MongooseError.
|
||||
*/
|
||||
|
||||
CastError.prototype = Object.create(MongooseError.prototype);
|
||||
CastError.prototype.constructor = MongooseError;
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
CastError.prototype.setModel = function(model) {
|
||||
this.model = model;
|
||||
this.message = 'Cast to ' + this.kind + ' failed for value ' +
|
||||
this.stringValue + ' at path "' + this.path + '"' + ' for model "' +
|
||||
model.modelName + '"';
|
||||
};
|
||||
|
||||
/*!
|
||||
* exports
|
||||
*/
|
||||
|
||||
module.exports = CastError;
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const MongooseError = require('./');
|
||||
|
||||
/**
|
||||
* Casting Error constructor.
|
||||
*
|
||||
* @param {String} type
|
||||
* @param {String} value
|
||||
* @inherits MongooseError
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function DisconnectedError(connectionString) {
|
||||
MongooseError.call(this, 'Ran out of retries trying to reconnect to "' +
|
||||
connectionString + '". Try setting `server.reconnectTries` and ' +
|
||||
'`server.reconnectInterval` to something higher.');
|
||||
this.name = 'DisconnectedError';
|
||||
if (Error.captureStackTrace) {
|
||||
Error.captureStackTrace(this);
|
||||
} else {
|
||||
this.stack = new Error().stack;
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* Inherits from MongooseError.
|
||||
*/
|
||||
|
||||
DisconnectedError.prototype = Object.create(MongooseError.prototype);
|
||||
DisconnectedError.prototype.constructor = MongooseError;
|
||||
|
||||
|
||||
/*!
|
||||
* exports
|
||||
*/
|
||||
|
||||
module.exports = DisconnectedError;
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const MongooseError = require('./');
|
||||
|
||||
/*!
|
||||
* DivergentArrayError constructor.
|
||||
*
|
||||
* @inherits MongooseError
|
||||
*/
|
||||
|
||||
function DivergentArrayError(paths) {
|
||||
const msg = 'For your own good, using `document.save()` to update an array '
|
||||
+ 'which was selected using an $elemMatch projection OR '
|
||||
+ 'populated using skip, limit, query conditions, or exclusion of '
|
||||
+ 'the _id field when the operation results in a $pop or $set of '
|
||||
+ 'the entire array is not supported. The following '
|
||||
+ 'path(s) would have been modified unsafely:\n'
|
||||
+ ' ' + paths.join('\n ') + '\n'
|
||||
+ 'Use Model.update() to update these arrays instead.';
|
||||
// TODO write up a docs page (FAQ) and link to it
|
||||
|
||||
MongooseError.call(this, msg);
|
||||
this.name = 'DivergentArrayError';
|
||||
if (Error.captureStackTrace) {
|
||||
Error.captureStackTrace(this);
|
||||
} else {
|
||||
this.stack = new Error().stack;
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* Inherits from MongooseError.
|
||||
*/
|
||||
|
||||
DivergentArrayError.prototype = Object.create(MongooseError.prototype);
|
||||
DivergentArrayError.prototype.constructor = MongooseError;
|
||||
|
||||
|
||||
/*!
|
||||
* exports
|
||||
*/
|
||||
|
||||
module.exports = DivergentArrayError;
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
'use strict';
|
||||
|
||||
const MongooseError = require('./mongooseError');
|
||||
|
||||
/*!
|
||||
* Module exports.
|
||||
*/
|
||||
|
||||
module.exports = exports = MongooseError;
|
||||
|
||||
/**
|
||||
* The default built-in validator error messages.
|
||||
*
|
||||
* @see Error.messages #error_messages_MongooseError-messages
|
||||
* @api public
|
||||
*/
|
||||
|
||||
MongooseError.messages = require('./messages');
|
||||
|
||||
// backward compat
|
||||
MongooseError.Messages = MongooseError.messages;
|
||||
|
||||
/**
|
||||
* An instance of this error class will be returned when `save()` fails
|
||||
* because the underlying
|
||||
* document was not found. The constructor takes one parameter, the
|
||||
* conditions that mongoose passed to `update()` when trying to update
|
||||
* the document.
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
|
||||
MongooseError.DocumentNotFoundError = require('./notFound');
|
||||
|
||||
/**
|
||||
* An instance of this error class will be returned when mongoose failed to
|
||||
* cast a value.
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
|
||||
MongooseError.CastError = require('./cast');
|
||||
|
||||
/**
|
||||
* An instance of this error class will be returned when [validation](/docs/validation.html) failed.
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
|
||||
MongooseError.ValidationError = require('./validation');
|
||||
|
||||
/**
|
||||
* A `ValidationError` has a hash of `errors` that contain individual `ValidatorError` instances
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
|
||||
MongooseError.ValidatorError = require('./validator');
|
||||
|
||||
/**
|
||||
* An instance of this error class will be returned when you call `save()` after
|
||||
* the document in the database was changed in a potentially unsafe way. See
|
||||
* the [`versionKey` option](/docs/guide.html#versionKey) for more information.
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
|
||||
MongooseError.VersionError = require('./version');
|
||||
|
||||
/**
|
||||
* An instance of this error class will be returned when you call `save()` multiple
|
||||
* times on the same document in parallel. See the [FAQ](/docs/faq.html) for more
|
||||
* information.
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
|
||||
MongooseError.ParallelSaveError = require('./parallelSave');
|
||||
|
||||
/**
|
||||
* Thrown when a model with the given name was already registered on the connection.
|
||||
* See [the FAQ about `OverwriteModelError`](/docs/faq.html#overwrite-model-error).
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
|
||||
MongooseError.OverwriteModelError = require('./overwriteModel');
|
||||
|
||||
/**
|
||||
* Thrown when you try to access a model that has not been registered yet
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
|
||||
MongooseError.MissingSchemaError = require('./missingSchema');
|
||||
|
||||
/**
|
||||
* An instance of this error will be returned if you used an array projection
|
||||
* and then modified the array in an unsafe way.
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
|
||||
MongooseError.DivergentArrayError = require('./divergentArray');
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
|
||||
/**
|
||||
* The default built-in validator error messages. These may be customized.
|
||||
*
|
||||
* // customize within each schema or globally like so
|
||||
* var mongoose = require('mongoose');
|
||||
* mongoose.Error.messages.String.enum = "Your custom message for {PATH}.";
|
||||
*
|
||||
* As you might have noticed, error messages support basic templating
|
||||
*
|
||||
* - `{PATH}` is replaced with the invalid document path
|
||||
* - `{VALUE}` is replaced with the invalid value
|
||||
* - `{TYPE}` is replaced with the validator type such as "regexp", "min", or "user defined"
|
||||
* - `{MIN}` is replaced with the declared min value for the Number.min validator
|
||||
* - `{MAX}` is replaced with the declared max value for the Number.max validator
|
||||
*
|
||||
* Click the "show code" link below to see all defaults.
|
||||
*
|
||||
* @static messages
|
||||
* @receiver MongooseError
|
||||
* @api public
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const msg = module.exports = exports = {};
|
||||
|
||||
msg.DocumentNotFoundError = null;
|
||||
|
||||
msg.general = {};
|
||||
msg.general.default = 'Validator failed for path `{PATH}` with value `{VALUE}`';
|
||||
msg.general.required = 'Path `{PATH}` is required.';
|
||||
|
||||
msg.Number = {};
|
||||
msg.Number.min = 'Path `{PATH}` ({VALUE}) is less than minimum allowed value ({MIN}).';
|
||||
msg.Number.max = 'Path `{PATH}` ({VALUE}) is more than maximum allowed value ({MAX}).';
|
||||
|
||||
msg.Date = {};
|
||||
msg.Date.min = 'Path `{PATH}` ({VALUE}) is before minimum allowed value ({MIN}).';
|
||||
msg.Date.max = 'Path `{PATH}` ({VALUE}) is after maximum allowed value ({MAX}).';
|
||||
|
||||
msg.String = {};
|
||||
msg.String.enum = '`{VALUE}` is not a valid enum value for path `{PATH}`.';
|
||||
msg.String.match = 'Path `{PATH}` is invalid ({VALUE}).';
|
||||
msg.String.minlength = 'Path `{PATH}` (`{VALUE}`) is shorter than the minimum allowed length ({MINLENGTH}).';
|
||||
msg.String.maxlength = 'Path `{PATH}` (`{VALUE}`) is longer than the maximum allowed length ({MAXLENGTH}).';
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const MongooseError = require('./');
|
||||
|
||||
/*!
|
||||
* MissingSchema Error constructor.
|
||||
*
|
||||
* @inherits MongooseError
|
||||
*/
|
||||
|
||||
function MissingSchemaError(name) {
|
||||
const msg = 'Schema hasn\'t been registered for model "' + name + '".\n'
|
||||
+ 'Use mongoose.model(name, schema)';
|
||||
MongooseError.call(this, msg);
|
||||
this.name = 'MissingSchemaError';
|
||||
if (Error.captureStackTrace) {
|
||||
Error.captureStackTrace(this);
|
||||
} else {
|
||||
this.stack = new Error().stack;
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* Inherits from MongooseError.
|
||||
*/
|
||||
|
||||
MissingSchemaError.prototype = Object.create(MongooseError.prototype);
|
||||
MissingSchemaError.prototype.constructor = MongooseError;
|
||||
|
||||
/*!
|
||||
* exports
|
||||
*/
|
||||
|
||||
module.exports = MissingSchemaError;
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* MongooseError constructor
|
||||
*
|
||||
* @param {String} msg Error message
|
||||
* @inherits Error https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
function MongooseError(msg) {
|
||||
Error.call(this);
|
||||
if (Error.captureStackTrace) {
|
||||
Error.captureStackTrace(this);
|
||||
} else {
|
||||
this.stack = new Error().stack;
|
||||
}
|
||||
this.message = msg;
|
||||
this.name = 'MongooseError';
|
||||
}
|
||||
|
||||
/*!
|
||||
* Inherits from Error.
|
||||
*/
|
||||
|
||||
MongooseError.prototype = Object.create(Error.prototype);
|
||||
MongooseError.prototype.constructor = Error;
|
||||
|
||||
module.exports = MongooseError;
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
'use strict';
|
||||
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
const MongooseError = require('./');
|
||||
const util = require('util');
|
||||
|
||||
/*!
|
||||
* OverwriteModel Error constructor.
|
||||
*
|
||||
* @inherits MongooseError
|
||||
*/
|
||||
|
||||
function DocumentNotFoundError(query) {
|
||||
let msg;
|
||||
const messages = MongooseError.messages;
|
||||
if (messages.DocumentNotFoundError != null) {
|
||||
msg = typeof messages.DocumentNotFoundError === 'function' ?
|
||||
messages.DocumentNotFoundError(query) :
|
||||
messages.DocumentNotFoundError;
|
||||
} else {
|
||||
msg = 'No document found for query "' + util.inspect(query) + '"';
|
||||
}
|
||||
|
||||
MongooseError.call(this, msg);
|
||||
|
||||
this.name = 'DocumentNotFoundError';
|
||||
if (Error.captureStackTrace) {
|
||||
Error.captureStackTrace(this);
|
||||
} else {
|
||||
this.stack = new Error().stack;
|
||||
}
|
||||
|
||||
this.query = query;
|
||||
}
|
||||
|
||||
/*!
|
||||
* Inherits from MongooseError.
|
||||
*/
|
||||
|
||||
DocumentNotFoundError.prototype = Object.create(MongooseError.prototype);
|
||||
DocumentNotFoundError.prototype.constructor = MongooseError;
|
||||
|
||||
/*!
|
||||
* exports
|
||||
*/
|
||||
|
||||
module.exports = DocumentNotFoundError;
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const MongooseError = require('./');
|
||||
|
||||
/**
|
||||
* Strict mode error constructor
|
||||
*
|
||||
* @param {String} type
|
||||
* @param {String} value
|
||||
* @inherits MongooseError
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function ObjectExpectedError(path, val) {
|
||||
MongooseError.call(this, 'Tried to set nested object field `' + path +
|
||||
'` to primitive value `' + val + '` and strict mode is set to throw.');
|
||||
this.name = 'ObjectExpectedError';
|
||||
if (Error.captureStackTrace) {
|
||||
Error.captureStackTrace(this);
|
||||
} else {
|
||||
this.stack = new Error().stack;
|
||||
}
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
/*!
|
||||
* Inherits from MongooseError.
|
||||
*/
|
||||
|
||||
ObjectExpectedError.prototype = Object.create(MongooseError.prototype);
|
||||
ObjectExpectedError.prototype.constructor = MongooseError;
|
||||
|
||||
module.exports = ObjectExpectedError;
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const MongooseError = require('./');
|
||||
|
||||
/**
|
||||
* Constructor for errors that happen when a parameter that's expected to be
|
||||
* an object isn't an object
|
||||
*
|
||||
* @param {Any} value
|
||||
* @param {String} paramName
|
||||
* @param {String} fnName
|
||||
* @inherits MongooseError
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function ObjectParameterError(value, paramName, fnName) {
|
||||
MongooseError.call(this, 'Parameter "' + paramName + '" to ' + fnName +
|
||||
'() must be an object, got ' + value.toString());
|
||||
this.name = 'ObjectParameterError';
|
||||
if (Error.captureStackTrace) {
|
||||
Error.captureStackTrace(this);
|
||||
} else {
|
||||
this.stack = new Error().stack;
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* Inherits from MongooseError.
|
||||
*/
|
||||
|
||||
ObjectParameterError.prototype = Object.create(MongooseError.prototype);
|
||||
ObjectParameterError.prototype.constructor = MongooseError;
|
||||
|
||||
module.exports = ObjectParameterError;
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const MongooseError = require('./');
|
||||
|
||||
/*!
|
||||
* OverwriteModel Error constructor.
|
||||
*
|
||||
* @inherits MongooseError
|
||||
*/
|
||||
|
||||
function OverwriteModelError(name) {
|
||||
MongooseError.call(this, 'Cannot overwrite `' + name + '` model once compiled.');
|
||||
this.name = 'OverwriteModelError';
|
||||
if (Error.captureStackTrace) {
|
||||
Error.captureStackTrace(this);
|
||||
} else {
|
||||
this.stack = new Error().stack;
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* Inherits from MongooseError.
|
||||
*/
|
||||
|
||||
OverwriteModelError.prototype = Object.create(MongooseError.prototype);
|
||||
OverwriteModelError.prototype.constructor = MongooseError;
|
||||
|
||||
/*!
|
||||
* exports
|
||||
*/
|
||||
|
||||
module.exports = OverwriteModelError;
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
'use strict';
|
||||
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
const MongooseError = require('./');
|
||||
|
||||
/**
|
||||
* ParallelSave Error constructor.
|
||||
*
|
||||
* @inherits MongooseError
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function ParallelSaveError(doc) {
|
||||
const msg = 'Can\'t save() the same doc multiple times in parallel. Document: ';
|
||||
MongooseError.call(this, msg + doc.id);
|
||||
this.name = 'ParallelSaveError';
|
||||
}
|
||||
|
||||
/*!
|
||||
* Inherits from MongooseError.
|
||||
*/
|
||||
|
||||
ParallelSaveError.prototype = Object.create(MongooseError.prototype);
|
||||
ParallelSaveError.prototype.constructor = MongooseError;
|
||||
|
||||
/*!
|
||||
* exports
|
||||
*/
|
||||
|
||||
module.exports = ParallelSaveError;
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const MongooseError = require('./');
|
||||
|
||||
/**
|
||||
* Strict mode error constructor
|
||||
*
|
||||
* @param {String} type
|
||||
* @param {String} value
|
||||
* @inherits MongooseError
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function StrictModeError(path, msg) {
|
||||
msg = msg || 'Field `' + path + '` is not in schema and strict ' +
|
||||
'mode is set to throw.';
|
||||
MongooseError.call(this, msg);
|
||||
this.name = 'StrictModeError';
|
||||
if (Error.captureStackTrace) {
|
||||
Error.captureStackTrace(this);
|
||||
} else {
|
||||
this.stack = new Error().stack;
|
||||
}
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
/*!
|
||||
* Inherits from MongooseError.
|
||||
*/
|
||||
|
||||
StrictModeError.prototype = Object.create(MongooseError.prototype);
|
||||
StrictModeError.prototype.constructor = MongooseError;
|
||||
|
||||
module.exports = StrictModeError;
|
||||
+112
@@ -0,0 +1,112 @@
|
||||
/*!
|
||||
* Module requirements
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const MongooseError = require('./');
|
||||
const util = require('util');
|
||||
|
||||
/**
|
||||
* Document Validation Error
|
||||
*
|
||||
* @api private
|
||||
* @param {Document} instance
|
||||
* @inherits MongooseError
|
||||
*/
|
||||
|
||||
function ValidationError(instance) {
|
||||
this.errors = {};
|
||||
this._message = '';
|
||||
if (instance && instance.constructor.name === 'model') {
|
||||
this._message = instance.constructor.modelName + ' validation failed';
|
||||
MongooseError.call(this, this._message);
|
||||
} else {
|
||||
this._message = 'Validation failed';
|
||||
MongooseError.call(this, this._message);
|
||||
}
|
||||
this.name = 'ValidationError';
|
||||
if (Error.captureStackTrace) {
|
||||
Error.captureStackTrace(this);
|
||||
} else {
|
||||
this.stack = new Error().stack;
|
||||
}
|
||||
if (instance) {
|
||||
instance.errors = this.errors;
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* Inherits from MongooseError.
|
||||
*/
|
||||
|
||||
ValidationError.prototype = Object.create(MongooseError.prototype);
|
||||
ValidationError.prototype.constructor = MongooseError;
|
||||
|
||||
/**
|
||||
* Console.log helper
|
||||
*/
|
||||
|
||||
ValidationError.prototype.toString = function() {
|
||||
return this.name + ': ' + _generateMessage(this);
|
||||
};
|
||||
|
||||
/*!
|
||||
* inspect helper
|
||||
*/
|
||||
|
||||
ValidationError.prototype.inspect = function() {
|
||||
return Object.assign(new Error(this.message), this);
|
||||
};
|
||||
|
||||
if (util.inspect.custom) {
|
||||
/*!
|
||||
* Avoid Node deprecation warning DEP0079
|
||||
*/
|
||||
|
||||
ValidationError.prototype[util.inspect.custom] = ValidationError.prototype.inspect;
|
||||
}
|
||||
|
||||
/*!
|
||||
* Helper for JSON.stringify
|
||||
*/
|
||||
|
||||
ValidationError.prototype.toJSON = function() {
|
||||
return Object.assign({}, this, { message: this.message });
|
||||
};
|
||||
|
||||
/*!
|
||||
* add message
|
||||
*/
|
||||
|
||||
ValidationError.prototype.addError = function(path, error) {
|
||||
this.errors[path] = error;
|
||||
this.message = this._message + ': ' + _generateMessage(this);
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function _generateMessage(err) {
|
||||
const keys = Object.keys(err.errors || {});
|
||||
const len = keys.length;
|
||||
const msgs = [];
|
||||
let key;
|
||||
|
||||
for (let i = 0; i < len; ++i) {
|
||||
key = keys[i];
|
||||
if (err === err.errors[key]) {
|
||||
continue;
|
||||
}
|
||||
msgs.push(key + ': ' + err.errors[key].message);
|
||||
}
|
||||
|
||||
return msgs.join(', ');
|
||||
}
|
||||
|
||||
/*!
|
||||
* Module exports
|
||||
*/
|
||||
|
||||
module.exports = exports = ValidationError;
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const MongooseError = require('./');
|
||||
|
||||
/**
|
||||
* Schema validator error
|
||||
*
|
||||
* @param {Object} properties
|
||||
* @inherits MongooseError
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function ValidatorError(properties) {
|
||||
let msg = properties.message;
|
||||
if (!msg) {
|
||||
msg = MongooseError.messages.general.default;
|
||||
}
|
||||
|
||||
const message = this.formatMessage(msg, properties);
|
||||
MongooseError.call(this, message);
|
||||
|
||||
properties = Object.assign({}, properties, { message: message });
|
||||
this.name = 'ValidatorError';
|
||||
if (Error.captureStackTrace) {
|
||||
Error.captureStackTrace(this);
|
||||
} else {
|
||||
this.stack = new Error().stack;
|
||||
}
|
||||
this.properties = properties;
|
||||
this.kind = properties.type;
|
||||
this.path = properties.path;
|
||||
this.value = properties.value;
|
||||
this.reason = properties.reason;
|
||||
}
|
||||
|
||||
/*!
|
||||
* Inherits from MongooseError
|
||||
*/
|
||||
|
||||
ValidatorError.prototype = Object.create(MongooseError.prototype);
|
||||
ValidatorError.prototype.constructor = MongooseError;
|
||||
|
||||
/*!
|
||||
* The object used to define this validator. Not enumerable to hide
|
||||
* it from `require('util').inspect()` output re: gh-3925
|
||||
*/
|
||||
|
||||
Object.defineProperty(ValidatorError.prototype, 'properties', {
|
||||
enumerable: false,
|
||||
writable: true,
|
||||
value: null
|
||||
});
|
||||
|
||||
/*!
|
||||
* Formats error messages
|
||||
*/
|
||||
|
||||
ValidatorError.prototype.formatMessage = function(msg, properties) {
|
||||
if (typeof msg === 'function') {
|
||||
return msg(properties);
|
||||
}
|
||||
const propertyNames = Object.keys(properties);
|
||||
for (let i = 0; i < propertyNames.length; ++i) {
|
||||
const propertyName = propertyNames[i];
|
||||
if (propertyName === 'message') {
|
||||
continue;
|
||||
}
|
||||
msg = msg.replace('{' + propertyName.toUpperCase() + '}', properties[propertyName]);
|
||||
}
|
||||
return msg;
|
||||
};
|
||||
|
||||
/*!
|
||||
* toString helper
|
||||
*/
|
||||
|
||||
ValidatorError.prototype.toString = function() {
|
||||
return this.message;
|
||||
};
|
||||
|
||||
/*!
|
||||
* exports
|
||||
*/
|
||||
|
||||
module.exports = ValidatorError;
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
'use strict';
|
||||
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
const MongooseError = require('./');
|
||||
|
||||
/**
|
||||
* Version Error constructor.
|
||||
*
|
||||
* @inherits MongooseError
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function VersionError(doc, currentVersion, modifiedPaths) {
|
||||
const modifiedPathsStr = modifiedPaths.join(', ');
|
||||
MongooseError.call(this, 'No matching document found for id "' + doc._id +
|
||||
'" version ' + currentVersion + ' modifiedPaths "' + modifiedPathsStr + '"');
|
||||
this.name = 'VersionError';
|
||||
this.version = currentVersion;
|
||||
this.modifiedPaths = modifiedPaths;
|
||||
}
|
||||
|
||||
/*!
|
||||
* Inherits from MongooseError.
|
||||
*/
|
||||
|
||||
VersionError.prototype = Object.create(MongooseError.prototype);
|
||||
VersionError.prototype.constructor = MongooseError;
|
||||
|
||||
/*!
|
||||
* exports
|
||||
*/
|
||||
|
||||
module.exports = VersionError;
|
||||
+87
@@ -0,0 +1,87 @@
|
||||
'use strict';
|
||||
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
const ObjectId = require('../types/objectid');
|
||||
const utils = require('../utils');
|
||||
|
||||
exports.flatten = flatten;
|
||||
exports.modifiedPaths = modifiedPaths;
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function flatten(update, path, options) {
|
||||
let keys;
|
||||
if (update && utils.isMongooseObject(update) && !Buffer.isBuffer(update)) {
|
||||
keys = Object.keys(update.toObject({ transform: false, virtuals: false }));
|
||||
} else {
|
||||
keys = Object.keys(update || {});
|
||||
}
|
||||
|
||||
const numKeys = keys.length;
|
||||
const result = {};
|
||||
path = path ? path + '.' : '';
|
||||
|
||||
for (let i = 0; i < numKeys; ++i) {
|
||||
const key = keys[i];
|
||||
const val = update[key];
|
||||
result[path + key] = val;
|
||||
if (shouldFlatten(val)) {
|
||||
if (options && options.skipArrays && Array.isArray(val)) {
|
||||
continue;
|
||||
}
|
||||
const flat = flatten(val, path + key, options);
|
||||
for (const k in flat) {
|
||||
result[k] = flat[k];
|
||||
}
|
||||
if (Array.isArray(val)) {
|
||||
result[path + key] = val;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function modifiedPaths(update, path, result) {
|
||||
const keys = Object.keys(update || {});
|
||||
const numKeys = keys.length;
|
||||
result = result || {};
|
||||
path = path ? path + '.' : '';
|
||||
|
||||
for (let i = 0; i < numKeys; ++i) {
|
||||
const key = keys[i];
|
||||
let val = update[key];
|
||||
|
||||
result[path + key] = true;
|
||||
if (utils.isMongooseObject(val) && !Buffer.isBuffer(val)) {
|
||||
val = val.toObject({ transform: false, virtuals: false });
|
||||
}
|
||||
if (shouldFlatten(val)) {
|
||||
modifiedPaths(val, path + key, result);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function shouldFlatten(val) {
|
||||
return val &&
|
||||
typeof val === 'object' &&
|
||||
!(val instanceof Date) &&
|
||||
!(val instanceof ObjectId) &&
|
||||
(!Array.isArray(val) || val.length > 0) &&
|
||||
!(val instanceof Buffer);
|
||||
}
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
'use strict';
|
||||
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
const async = require('async');
|
||||
const utils = require('../../utils');
|
||||
|
||||
/**
|
||||
* Execute `fn` for every document in the cursor. If `fn` returns a promise,
|
||||
* will wait for the promise to resolve before iterating on to the next one.
|
||||
* Returns a promise that resolves when done.
|
||||
*
|
||||
* @param {Function} next the thunk to call to get the next document
|
||||
* @param {Function} fn
|
||||
* @param {Object} options
|
||||
* @param {Function} [callback] executed when all docs have been processed
|
||||
* @return {Promise}
|
||||
* @api public
|
||||
* @method eachAsync
|
||||
*/
|
||||
|
||||
module.exports = function eachAsync(next, fn, options, callback) {
|
||||
const parallel = options.parallel || 1;
|
||||
|
||||
const handleNextResult = function(doc, callback) {
|
||||
const promise = fn(doc);
|
||||
if (promise && typeof promise.then === 'function') {
|
||||
promise.then(
|
||||
function() { callback(null); },
|
||||
function(error) { callback(error || new Error('`eachAsync()` promise rejected without error')); });
|
||||
} else {
|
||||
callback(null);
|
||||
}
|
||||
};
|
||||
|
||||
const iterate = function(callback) {
|
||||
let drained = false;
|
||||
const nextQueue = async.queue(function(task, cb) {
|
||||
if (drained) return cb();
|
||||
next(function(err, doc) {
|
||||
if (err) return cb(err);
|
||||
if (!doc) drained = true;
|
||||
cb(null, doc);
|
||||
});
|
||||
}, 1);
|
||||
|
||||
const getAndRun = function(cb) {
|
||||
nextQueue.push({}, function(err, doc) {
|
||||
if (err) return cb(err);
|
||||
if (!doc) return cb();
|
||||
handleNextResult(doc, function(err) {
|
||||
if (err) return cb(err);
|
||||
// Make sure to clear the stack re: gh-4697
|
||||
setTimeout(function() {
|
||||
getAndRun(cb);
|
||||
}, 0);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
async.times(parallel, function(n, cb) {
|
||||
getAndRun(cb);
|
||||
}, callback);
|
||||
};
|
||||
|
||||
return utils.promiseOrCallback(callback, cb => {
|
||||
iterate(cb);
|
||||
});
|
||||
};
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
'use strict';
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
module.exports = function cleanModifiedSubpaths(doc, path, options) {
|
||||
options = options || {};
|
||||
const skipDocArrays = options.skipDocArrays;
|
||||
|
||||
let deleted = 0;
|
||||
for (const modifiedPath of Object.keys(doc.$__.activePaths.states.modify)) {
|
||||
if (skipDocArrays) {
|
||||
const schemaType = doc.schema.path(modifiedPath);
|
||||
if (schemaType && schemaType.$isMongooseDocumentArray) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (modifiedPath.indexOf(path + '.') === 0) {
|
||||
delete doc.$__.activePaths.states.modify[modifiedPath];
|
||||
++deleted;
|
||||
}
|
||||
}
|
||||
return deleted;
|
||||
};
|
||||
+148
@@ -0,0 +1,148 @@
|
||||
'use strict';
|
||||
|
||||
const get = require('../../helpers/get');
|
||||
const getSymbol = require('../../helpers/symbols').getSymbol;
|
||||
const utils = require('../../utils');
|
||||
|
||||
let Document;
|
||||
|
||||
/*!
|
||||
* exports
|
||||
*/
|
||||
|
||||
exports.compile = compile;
|
||||
exports.defineKey = defineKey;
|
||||
|
||||
/*!
|
||||
* Compiles schemas.
|
||||
*/
|
||||
|
||||
function compile(tree, proto, prefix, options) {
|
||||
Document = Document || require('../../document');
|
||||
const keys = Object.keys(tree);
|
||||
const len = keys.length;
|
||||
let limb;
|
||||
let key;
|
||||
|
||||
for (let i = 0; i < len; ++i) {
|
||||
key = keys[i];
|
||||
limb = tree[key];
|
||||
|
||||
const hasSubprops = utils.getFunctionName(limb.constructor) === 'Object' &&
|
||||
Object.keys(limb).length &&
|
||||
(!limb[options.typeKey] || (options.typeKey === 'type' && limb.type.type));
|
||||
const subprops = hasSubprops ? limb : null;
|
||||
|
||||
defineKey(key, subprops, proto, prefix, keys, options);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* Defines the accessor named prop on the incoming prototype.
|
||||
*/
|
||||
|
||||
function defineKey(prop, subprops, prototype, prefix, keys, options) {
|
||||
Document = Document || require('../../document');
|
||||
const path = (prefix ? prefix + '.' : '') + prop;
|
||||
prefix = prefix || '';
|
||||
|
||||
if (subprops) {
|
||||
Object.defineProperty(prototype, prop, {
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
get: function() {
|
||||
const _this = this;
|
||||
if (!this.$__.getters) {
|
||||
this.$__.getters = {};
|
||||
}
|
||||
|
||||
if (!this.$__.getters[path]) {
|
||||
const nested = Object.create(Document.prototype, getOwnPropertyDescriptors(this));
|
||||
|
||||
// save scope for nested getters/setters
|
||||
if (!prefix) {
|
||||
nested.$__.scope = this;
|
||||
}
|
||||
nested.$__.nestedPath = path;
|
||||
|
||||
Object.defineProperty(nested, 'schema', {
|
||||
enumerable: false,
|
||||
configurable: true,
|
||||
writable: false,
|
||||
value: prototype.schema
|
||||
});
|
||||
|
||||
Object.defineProperty(nested, 'toObject', {
|
||||
enumerable: false,
|
||||
configurable: true,
|
||||
writable: false,
|
||||
value: function() {
|
||||
return utils.clone(_this.get(path, null, {
|
||||
virtuals: get(this, 'schema.options.toObject.virtuals', null)
|
||||
}));
|
||||
}
|
||||
});
|
||||
|
||||
Object.defineProperty(nested, 'toJSON', {
|
||||
enumerable: false,
|
||||
configurable: true,
|
||||
writable: false,
|
||||
value: function() {
|
||||
return _this.get(path, null, {
|
||||
virtuals: get(_this, 'schema.options.toJSON.virtuals', null)
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
Object.defineProperty(nested, '$__isNested', {
|
||||
enumerable: false,
|
||||
configurable: true,
|
||||
writable: false,
|
||||
value: true
|
||||
});
|
||||
|
||||
compile(subprops, nested, path, options);
|
||||
this.$__.getters[path] = nested;
|
||||
}
|
||||
|
||||
return this.$__.getters[path];
|
||||
},
|
||||
set: function(v) {
|
||||
if (v instanceof Document) {
|
||||
v = v.toObject({ transform: false });
|
||||
}
|
||||
const doc = this.$__.scope || this;
|
||||
return doc.$set(path, v);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
Object.defineProperty(prototype, prop, {
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
get: function() {
|
||||
return this[getSymbol].call(this.$__.scope || this, path);
|
||||
},
|
||||
set: function(v) {
|
||||
return this.$set.call(this.$__.scope || this, path, v);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// gets descriptors for all properties of `object`
|
||||
// makes all properties non-enumerable to match previous behavior to #2211
|
||||
function getOwnPropertyDescriptors(object) {
|
||||
const result = {};
|
||||
|
||||
Object.getOwnPropertyNames(object).forEach(function(key) {
|
||||
result[key] = Object.getOwnPropertyDescriptor(object, key);
|
||||
// Assume these are schema paths, ignore them re: #5470
|
||||
if (result[key].get) {
|
||||
delete result[key];
|
||||
return;
|
||||
}
|
||||
result[key].enumerable = ['isNew', '$__', 'errors', '_doc'].indexOf(key) === -1;
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
'use strict';
|
||||
|
||||
const get = require('../get');
|
||||
|
||||
/*!
|
||||
* Like `schema.path()`, except with a document, because impossible to
|
||||
* determine path type without knowing the embedded discriminator key.
|
||||
*/
|
||||
|
||||
module.exports = function getEmbeddedDiscriminatorPath(doc, path, options) {
|
||||
options = options || {};
|
||||
const typeOnly = options.typeOnly;
|
||||
const parts = path.split('.');
|
||||
let schema = null;
|
||||
let type = 'adhocOrUndefined';
|
||||
|
||||
for (let i = 0; i < parts.length; ++i) {
|
||||
const subpath = parts.slice(0, i + 1).join('.');
|
||||
schema = doc.schema.path(subpath);
|
||||
if (schema == null) {
|
||||
continue;
|
||||
}
|
||||
type = doc.schema.pathType(subpath);
|
||||
if ((schema.$isSingleNested || schema.$isMongooseDocumentArrayElement) &&
|
||||
schema.schema.discriminators != null) {
|
||||
const discriminators = schema.schema.discriminators;
|
||||
const discriminatorKey = doc.get(subpath + '.' +
|
||||
get(schema, 'schema.options.discriminatorKey'));
|
||||
if (discriminatorKey == null || discriminators[discriminatorKey] == null) {
|
||||
continue;
|
||||
}
|
||||
const rest = parts.slice(i + 1).join('.');
|
||||
schema = discriminators[discriminatorKey].path(rest);
|
||||
if (schema != null) {
|
||||
type = discriminators[discriminatorKey].pathType(rest);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Are we getting the whole schema or just the type, 'real', 'nested', etc.
|
||||
return typeOnly ? type : schema;
|
||||
};
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
'use strict';
|
||||
|
||||
/*!
|
||||
* Simplified lodash.get to work around the annoying null quirk. See:
|
||||
* https://github.com/lodash/lodash/issues/3659
|
||||
*/
|
||||
|
||||
module.exports = function get(obj, path, def) {
|
||||
const parts = path.split('.');
|
||||
let rest = path;
|
||||
let cur = obj;
|
||||
for (const part of parts) {
|
||||
if (cur == null) {
|
||||
return def;
|
||||
}
|
||||
|
||||
// `lib/cast.js` depends on being able to get dotted paths in updates,
|
||||
// like `{ $set: { 'a.b': 42 } }`
|
||||
if (cur[rest] != null) {
|
||||
return cur[rest];
|
||||
}
|
||||
|
||||
cur = getProperty(cur, part);
|
||||
|
||||
rest = rest.substr(part.length + 1);
|
||||
}
|
||||
|
||||
return cur == null ? def : cur;
|
||||
};
|
||||
|
||||
function getProperty(obj, prop) {
|
||||
if (obj == null) {
|
||||
return obj;
|
||||
}
|
||||
if (obj instanceof Map) {
|
||||
return obj.get(prop);
|
||||
}
|
||||
return obj[prop];
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
/*!
|
||||
* Centralize this so we can more easily work around issues with people
|
||||
* stubbing out `process.nextTick()` in tests using sinon:
|
||||
* https://github.com/sinonjs/lolex#automatically-incrementing-mocked-time
|
||||
* See gh-6074
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
module.exports = function immediate(cb) {
|
||||
return process.nextTick(cb);
|
||||
};
|
||||
+122
@@ -0,0 +1,122 @@
|
||||
'use strict';
|
||||
|
||||
const symbols = require('../../schema/symbols');
|
||||
const utils = require('../../utils');
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
module.exports = applyHooks;
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
applyHooks.middlewareFunctions = [
|
||||
'save',
|
||||
'validate',
|
||||
'remove',
|
||||
'updateOne',
|
||||
'init'
|
||||
];
|
||||
|
||||
/*!
|
||||
* Register hooks for this model
|
||||
*
|
||||
* @param {Model} model
|
||||
* @param {Schema} schema
|
||||
*/
|
||||
|
||||
function applyHooks(model, schema, options) {
|
||||
options = options || {};
|
||||
|
||||
const kareemOptions = {
|
||||
useErrorHandlers: true,
|
||||
numCallbackParams: 1,
|
||||
nullResultByDefault: true,
|
||||
contextParameter: true
|
||||
};
|
||||
const objToDecorate = options.decorateDoc ? model : model.prototype;
|
||||
|
||||
model.$appliedHooks = true;
|
||||
for (let i = 0; i < schema.childSchemas.length; ++i) {
|
||||
const childModel = schema.childSchemas[i].model;
|
||||
if (childModel.$appliedHooks) {
|
||||
continue;
|
||||
}
|
||||
applyHooks(childModel, schema.childSchemas[i].schema, options);
|
||||
if (childModel.discriminators != null) {
|
||||
const keys = Object.keys(childModel.discriminators);
|
||||
for (let j = 0; j < keys.length; ++j) {
|
||||
applyHooks(childModel.discriminators[keys[j]],
|
||||
childModel.discriminators[keys[j]].schema, options);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Built-in hooks rely on hooking internal functions in order to support
|
||||
// promises and make it so that `doc.save.toString()` provides meaningful
|
||||
// information.
|
||||
|
||||
const middleware = schema.s.hooks.
|
||||
filter(hook => {
|
||||
if (hook.name === 'updateOne') {
|
||||
return !!hook['document'];
|
||||
}
|
||||
if (hook.name === 'remove') {
|
||||
return hook['document'] == null || !!hook['document'];
|
||||
}
|
||||
return true;
|
||||
}).
|
||||
filter(hook => {
|
||||
// If user has overwritten the method, don't apply built-in middleware
|
||||
if (schema.methods[hook.name]) {
|
||||
return !hook.fn[symbols.builtInMiddleware];
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
model._middleware = middleware;
|
||||
|
||||
objToDecorate.$__save = middleware.
|
||||
createWrapper('save', objToDecorate.$__save, null, kareemOptions);
|
||||
objToDecorate.$__validate = middleware.
|
||||
createWrapper('validate', objToDecorate.$__validate, null, kareemOptions);
|
||||
objToDecorate.$__remove = middleware.
|
||||
createWrapper('remove', objToDecorate.$__remove, null, kareemOptions);
|
||||
objToDecorate.$__init = middleware.
|
||||
createWrapperSync('init', objToDecorate.$__init, null, kareemOptions);
|
||||
|
||||
// Support hooks for custom methods
|
||||
const customMethods = Object.keys(schema.methods);
|
||||
const customMethodOptions = Object.assign({}, kareemOptions, {
|
||||
// Only use `checkForPromise` for custom methods, because mongoose
|
||||
// query thunks are not as consistent as I would like about returning
|
||||
// a nullish value rather than the query. If a query thunk returns
|
||||
// a query, `checkForPromise` causes infinite recursion
|
||||
checkForPromise: true
|
||||
});
|
||||
for (const method of customMethods) {
|
||||
if (!middleware.hasHooks(method)) {
|
||||
// Don't wrap if there are no hooks for the custom method to avoid
|
||||
// surprises. Also, `createWrapper()` enforces consistent async,
|
||||
// so wrapping a sync method would break it.
|
||||
continue;
|
||||
}
|
||||
const originalMethod = objToDecorate[method];
|
||||
objToDecorate[method] = function() {
|
||||
const args = Array.prototype.slice.call(arguments);
|
||||
const cb = utils.last(args);
|
||||
const argsWithoutCallback = cb == null ? args :
|
||||
args.slice(0, args.length - 1);
|
||||
return utils.promiseOrCallback(cb, callback => {
|
||||
this[`$__${method}`].apply(this,
|
||||
argsWithoutCallback.concat([callback]));
|
||||
}, model.events);
|
||||
};
|
||||
objToDecorate[`$__${method}`] = middleware.
|
||||
createWrapper(method, originalMethod, null, customMethodOptions);
|
||||
}
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
'use strict';
|
||||
|
||||
const get = require('../get');
|
||||
|
||||
/*!
|
||||
* Register methods for this model
|
||||
*
|
||||
* @param {Model} model
|
||||
* @param {Schema} schema
|
||||
*/
|
||||
|
||||
module.exports = function applyMethods(model, schema) {
|
||||
function apply(method, schema) {
|
||||
Object.defineProperty(model.prototype, method, {
|
||||
get: function() {
|
||||
const h = {};
|
||||
for (const k in schema.methods[method]) {
|
||||
h[k] = schema.methods[method][k].bind(this);
|
||||
}
|
||||
return h;
|
||||
},
|
||||
configurable: true
|
||||
});
|
||||
}
|
||||
for (const method of Object.keys(schema.methods)) {
|
||||
const fn = schema.methods[method];
|
||||
if (schema.tree.hasOwnProperty(method)) {
|
||||
throw new Error('You have a method and a property in your schema both ' +
|
||||
'named "' + method + '"');
|
||||
}
|
||||
if (schema.reserved[method] &&
|
||||
!get(schema, `methodOptions.${method}.suppressWarning`, false)) {
|
||||
console.warn(`mongoose: the method name "${method}" is used by mongoose ` +
|
||||
'internally, overwriting it may cause bugs. If you\'re sure you know ' +
|
||||
'what you\'re doing, you can suppress this error by using ' +
|
||||
`\`schema.method('${method}', fn, { suppressWarning: true })\`.`);
|
||||
}
|
||||
if (typeof fn === 'function') {
|
||||
model.prototype[method] = fn;
|
||||
} else {
|
||||
apply(method, schema);
|
||||
}
|
||||
}
|
||||
|
||||
// Recursively call `applyMethods()` on child schemas
|
||||
model.$appliedMethods = true;
|
||||
for (let i = 0; i < schema.childSchemas.length; ++i) {
|
||||
if (schema.childSchemas[i].model.$appliedMethods) {
|
||||
continue;
|
||||
}
|
||||
applyMethods(schema.childSchemas[i].model, schema.childSchemas[i].schema);
|
||||
}
|
||||
};
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
'use strict';
|
||||
|
||||
/*!
|
||||
* Register statics for this model
|
||||
* @param {Model} model
|
||||
* @param {Schema} schema
|
||||
*/
|
||||
module.exports = function applyStatics(model, schema) {
|
||||
for (const i in schema.statics) {
|
||||
model[i] = schema.statics[i];
|
||||
}
|
||||
};
|
||||
+136
@@ -0,0 +1,136 @@
|
||||
'use strict';
|
||||
|
||||
const applyTimestampsToChildren = require('../update/applyTimestampsToChildren');
|
||||
const applyTimestampsToUpdate = require('../update/applyTimestampsToUpdate');
|
||||
const cast = require('../../cast');
|
||||
const castUpdate = require('../query/castUpdate');
|
||||
const setDefaultsOnInsert = require('../setDefaultsOnInsert');
|
||||
|
||||
/*!
|
||||
* Given a model and a bulkWrite op, return a thunk that handles casting and
|
||||
* validating the individual op.
|
||||
*/
|
||||
|
||||
module.exports = function castBulkWrite(model, op) {
|
||||
const now = model.base.now();
|
||||
|
||||
if (op['insertOne']) {
|
||||
return (callback) => {
|
||||
const doc = new model(op['insertOne']['document']);
|
||||
if (model.schema.options.timestamps != null) {
|
||||
doc.initializeTimestamps();
|
||||
}
|
||||
|
||||
op['insertOne']['document'] = doc;
|
||||
op['insertOne']['document'].validate({ __noPromise: true }, function(error) {
|
||||
if (error) {
|
||||
return callback(error, null);
|
||||
}
|
||||
callback(null);
|
||||
});
|
||||
};
|
||||
} else if (op['updateOne']) {
|
||||
op = op['updateOne'];
|
||||
return (callback) => {
|
||||
try {
|
||||
op['filter'] = cast(model.schema, op['filter']);
|
||||
op['update'] = castUpdate(model.schema, op['update'], {
|
||||
strict: model.schema.options.strict,
|
||||
overwrite: false
|
||||
});
|
||||
if (op.setDefaultsOnInsert) {
|
||||
setDefaultsOnInsert(op['filter'], model.schema, op['update'], {
|
||||
setDefaultsOnInsert: true,
|
||||
upsert: op.upsert
|
||||
});
|
||||
}
|
||||
if (model.schema.$timestamps != null) {
|
||||
const createdAt = model.schema.$timestamps.createdAt;
|
||||
const updatedAt = model.schema.$timestamps.updatedAt;
|
||||
applyTimestampsToUpdate(now, createdAt, updatedAt, op['update'], {});
|
||||
}
|
||||
applyTimestampsToChildren(now, op['update'], model.schema);
|
||||
} catch (error) {
|
||||
return callback(error, null);
|
||||
}
|
||||
|
||||
callback(null);
|
||||
};
|
||||
} else if (op['updateMany']) {
|
||||
op = op['updateMany'];
|
||||
return (callback) => {
|
||||
try {
|
||||
op['filter'] = cast(model.schema, op['filter']);
|
||||
op['update'] = castUpdate(model.schema, op['update'], {
|
||||
strict: model.schema.options.strict,
|
||||
overwrite: false
|
||||
});
|
||||
if (op.setDefaultsOnInsert) {
|
||||
setDefaultsOnInsert(op['filter'], model.schema, op['update'], {
|
||||
setDefaultsOnInsert: true,
|
||||
upsert: op.upsert
|
||||
});
|
||||
}
|
||||
if (model.schema.$timestamps != null) {
|
||||
const createdAt = model.schema.$timestamps.createdAt;
|
||||
const updatedAt = model.schema.$timestamps.updatedAt;
|
||||
applyTimestampsToUpdate(now, createdAt, updatedAt, op['update'], {});
|
||||
}
|
||||
applyTimestampsToChildren(now, op['update'], model.schema);
|
||||
} catch (error) {
|
||||
return callback(error, null);
|
||||
}
|
||||
|
||||
callback(null);
|
||||
};
|
||||
} else if (op['replaceOne']) {
|
||||
return (callback) => {
|
||||
try {
|
||||
op['replaceOne']['filter'] = cast(model.schema,
|
||||
op['replaceOne']['filter']);
|
||||
} catch (error) {
|
||||
return callback(error, null);
|
||||
}
|
||||
|
||||
// set `skipId`, otherwise we get "_id field cannot be changed"
|
||||
const doc = new model(op['replaceOne']['replacement'], null, true);
|
||||
if (model.schema.options.timestamps != null) {
|
||||
doc.initializeTimestamps();
|
||||
}
|
||||
op['replaceOne']['replacement'] = doc;
|
||||
|
||||
op['replaceOne']['replacement'].validate({ __noPromise: true }, function(error) {
|
||||
if (error) {
|
||||
return callback(error, null);
|
||||
}
|
||||
callback(null);
|
||||
});
|
||||
};
|
||||
} else if (op['deleteOne']) {
|
||||
return (callback) => {
|
||||
try {
|
||||
op['deleteOne']['filter'] = cast(model.schema,
|
||||
op['deleteOne']['filter']);
|
||||
} catch (error) {
|
||||
return callback(error, null);
|
||||
}
|
||||
|
||||
callback(null);
|
||||
};
|
||||
} else if (op['deleteMany']) {
|
||||
return (callback) => {
|
||||
try {
|
||||
op['deleteMany']['filter'] = cast(model.schema,
|
||||
op['deleteMany']['filter']);
|
||||
} catch (error) {
|
||||
return callback(error, null);
|
||||
}
|
||||
|
||||
callback(null);
|
||||
};
|
||||
} else {
|
||||
return (callback) => {
|
||||
callback(new Error('Invalid op passed to `bulkWrite()`'), null);
|
||||
};
|
||||
}
|
||||
};
|
||||
+168
@@ -0,0 +1,168 @@
|
||||
'use strict';
|
||||
|
||||
const defineKey = require('../document/compile').defineKey;
|
||||
const get = require('../../helpers/get');
|
||||
const utils = require('../../utils');
|
||||
|
||||
const CUSTOMIZABLE_DISCRIMINATOR_OPTIONS = {
|
||||
toJSON: true,
|
||||
toObject: true,
|
||||
_id: true,
|
||||
id: true
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
module.exports = function discriminator(model, name, schema, tiedValue) {
|
||||
if (!(schema && schema.instanceOfSchema)) {
|
||||
throw new Error('You must pass a valid discriminator Schema');
|
||||
}
|
||||
|
||||
const applyPluginsToDiscriminators = get(model.base,
|
||||
'options.applyPluginsToDiscriminators', false);
|
||||
// Even if `applyPluginsToDiscriminators` isn't set, we should still apply
|
||||
// global plugins to schemas embedded in the discriminator schema (gh-7370)
|
||||
model.base._applyPlugins(schema, { skipTopLevel: !applyPluginsToDiscriminators });
|
||||
|
||||
if (model.schema.discriminatorMapping &&
|
||||
!model.schema.discriminatorMapping.isRoot) {
|
||||
throw new Error('Discriminator "' + name +
|
||||
'" can only be a discriminator of the root model');
|
||||
}
|
||||
|
||||
const key = model.schema.options.discriminatorKey;
|
||||
|
||||
const baseSchemaAddition = {};
|
||||
baseSchemaAddition[key] = {
|
||||
default: void 0,
|
||||
select: true,
|
||||
$skipDiscriminatorCheck: true
|
||||
};
|
||||
baseSchemaAddition[key][model.schema.options.typeKey] = String;
|
||||
model.schema.add(baseSchemaAddition);
|
||||
defineKey(key, null, model.prototype, null, [key], model.schema.options);
|
||||
|
||||
if (schema.path(key) && schema.path(key).options.$skipDiscriminatorCheck !== true) {
|
||||
throw new Error('Discriminator "' + name +
|
||||
'" cannot have field with name "' + key + '"');
|
||||
}
|
||||
|
||||
let value = name;
|
||||
if (typeof tiedValue == 'string' && tiedValue.length) {
|
||||
value = tiedValue;
|
||||
}
|
||||
|
||||
function merge(schema, baseSchema) {
|
||||
// Retain original schema before merging base schema
|
||||
schema._originalSchema = schema.clone();
|
||||
if (baseSchema.paths._id &&
|
||||
baseSchema.paths._id.options &&
|
||||
!baseSchema.paths._id.options.auto) {
|
||||
const originalSchema = schema;
|
||||
utils.merge(schema, originalSchema);
|
||||
delete schema.paths._id;
|
||||
delete schema.tree._id;
|
||||
}
|
||||
|
||||
// Find conflicting paths: if something is a path in the base schema
|
||||
// and a nested path in the child schema, overwrite the base schema path.
|
||||
// See gh-6076
|
||||
const baseSchemaPaths = Object.keys(baseSchema.paths);
|
||||
const conflictingPaths = [];
|
||||
for (let i = 0; i < baseSchemaPaths.length; ++i) {
|
||||
if (schema.nested[baseSchemaPaths[i]]) {
|
||||
conflictingPaths.push(baseSchemaPaths[i]);
|
||||
}
|
||||
}
|
||||
|
||||
utils.merge(schema, baseSchema, {
|
||||
omit: { discriminators: true, $parentSchema: true },
|
||||
omitNested: conflictingPaths.reduce((cur, path) => {
|
||||
cur['tree.' + path] = true;
|
||||
return cur;
|
||||
}, {})
|
||||
});
|
||||
|
||||
// Clean up conflicting paths _after_ merging re: gh-6076
|
||||
for (let i = 0; i < conflictingPaths.length; ++i) {
|
||||
delete schema.paths[conflictingPaths[i]];
|
||||
}
|
||||
|
||||
const obj = {};
|
||||
obj[key] = {
|
||||
default: value,
|
||||
select: true,
|
||||
set: function(newName) {
|
||||
if (newName === value) {
|
||||
return value;
|
||||
}
|
||||
throw new Error('Can\'t set discriminator key "' + key + '"');
|
||||
},
|
||||
$skipDiscriminatorCheck: true
|
||||
};
|
||||
obj[key][schema.options.typeKey] = String;
|
||||
schema.add(obj);
|
||||
schema.discriminatorMapping = {key: key, value: value, isRoot: false};
|
||||
|
||||
if (baseSchema.options.collection) {
|
||||
schema.options.collection = baseSchema.options.collection;
|
||||
}
|
||||
|
||||
const toJSON = schema.options.toJSON;
|
||||
const toObject = schema.options.toObject;
|
||||
const _id = schema.options._id;
|
||||
const id = schema.options.id;
|
||||
|
||||
const keys = Object.keys(schema.options);
|
||||
schema.options.discriminatorKey = baseSchema.options.discriminatorKey;
|
||||
|
||||
for (let i = 0; i < keys.length; ++i) {
|
||||
const _key = keys[i];
|
||||
if (!CUSTOMIZABLE_DISCRIMINATOR_OPTIONS[_key]) {
|
||||
if (!utils.deepEqual(schema.options[_key], baseSchema.options[_key])) {
|
||||
throw new Error('Can\'t customize discriminator option ' + _key +
|
||||
' (can only modify ' +
|
||||
Object.keys(CUSTOMIZABLE_DISCRIMINATOR_OPTIONS).join(', ') +
|
||||
')');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
schema.options = utils.clone(baseSchema.options);
|
||||
if (toJSON) schema.options.toJSON = toJSON;
|
||||
if (toObject) schema.options.toObject = toObject;
|
||||
if (typeof _id !== 'undefined') {
|
||||
schema.options._id = _id;
|
||||
}
|
||||
schema.options.id = id;
|
||||
schema.s.hooks = model.schema.s.hooks.merge(schema.s.hooks);
|
||||
|
||||
schema.plugins = Array.prototype.slice(baseSchema.plugins);
|
||||
schema.callQueue = baseSchema.callQueue.concat(schema.callQueue);
|
||||
delete schema._requiredpaths; // reset just in case Schema#requiredPaths() was called on either schema
|
||||
}
|
||||
|
||||
// merges base schema into new discriminator schema and sets new type field.
|
||||
merge(schema, model.schema);
|
||||
|
||||
if (!model.discriminators) {
|
||||
model.discriminators = {};
|
||||
}
|
||||
|
||||
if (!model.schema.discriminatorMapping) {
|
||||
model.schema.discriminatorMapping = {key: key, value: null, isRoot: true};
|
||||
}
|
||||
if (!model.schema.discriminators) {
|
||||
model.schema.discriminators = {};
|
||||
}
|
||||
|
||||
model.schema.discriminators[name] = schema;
|
||||
|
||||
if (model.discriminators[name]) {
|
||||
throw new Error('Discriminator with name "' + name + '" already exists');
|
||||
}
|
||||
|
||||
return schema;
|
||||
};
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function once(fn) {
|
||||
let called = false;
|
||||
return function() {
|
||||
if (called) {
|
||||
return;
|
||||
}
|
||||
called = true;
|
||||
return fn.apply(null, arguments);
|
||||
};
|
||||
};
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = assignRawDocsToIdStructure;
|
||||
|
||||
/*!
|
||||
* Assign `vals` returned by mongo query to the `rawIds`
|
||||
* structure returned from utils.getVals() honoring
|
||||
* query sort order if specified by user.
|
||||
*
|
||||
* This can be optimized.
|
||||
*
|
||||
* Rules:
|
||||
*
|
||||
* if the value of the path is not an array, use findOne rules, else find.
|
||||
* for findOne the results are assigned directly to doc path (including null results).
|
||||
* for find, if user specified sort order, results are assigned directly
|
||||
* else documents are put back in original order of array if found in results
|
||||
*
|
||||
* @param {Array} rawIds
|
||||
* @param {Array} vals
|
||||
* @param {Boolean} sort
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function assignRawDocsToIdStructure(rawIds, resultDocs, resultOrder, options, recursed) {
|
||||
// honor user specified sort order
|
||||
const newOrder = [];
|
||||
const sorting = options.sort && rawIds.length > 1;
|
||||
let doc;
|
||||
let sid;
|
||||
let id;
|
||||
|
||||
for (let i = 0; i < rawIds.length; ++i) {
|
||||
id = rawIds[i];
|
||||
|
||||
if (Array.isArray(id)) {
|
||||
// handle [ [id0, id2], [id3] ]
|
||||
assignRawDocsToIdStructure(id, resultDocs, resultOrder, options, true);
|
||||
newOrder.push(id);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (id === null && !sorting) {
|
||||
// keep nulls for findOne unless sorting, which always
|
||||
// removes them (backward compat)
|
||||
newOrder.push(id);
|
||||
continue;
|
||||
}
|
||||
|
||||
sid = String(id);
|
||||
|
||||
doc = resultDocs[sid];
|
||||
// If user wants separate copies of same doc, use this option
|
||||
if (options.clone) {
|
||||
doc = doc.constructor.hydrate(doc._doc);
|
||||
}
|
||||
|
||||
if (recursed) {
|
||||
if (doc) {
|
||||
if (sorting) {
|
||||
newOrder[resultOrder[sid]] = doc;
|
||||
} else {
|
||||
newOrder.push(doc);
|
||||
}
|
||||
} else {
|
||||
newOrder.push(id);
|
||||
}
|
||||
} else {
|
||||
// apply findOne behavior - if document in results, assign, else assign null
|
||||
newOrder[i] = doc || null;
|
||||
}
|
||||
}
|
||||
|
||||
rawIds.length = 0;
|
||||
if (newOrder.length) {
|
||||
// reassign the documents based on corrected order
|
||||
|
||||
// forEach skips over sparse entries in arrays so we
|
||||
// can safely use this to our advantage dealing with sorted
|
||||
// result sets too.
|
||||
newOrder.forEach(function(doc, i) {
|
||||
rawIds[i] = doc;
|
||||
});
|
||||
}
|
||||
}
|
||||
+181
@@ -0,0 +1,181 @@
|
||||
'use strict';
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
const Mixed = require('../../schema/mixed');
|
||||
const get = require('../get');
|
||||
const leanPopulateMap = require('./leanPopulateMap');
|
||||
const mpath = require('mpath');
|
||||
|
||||
/*!
|
||||
* @param {Schema} schema
|
||||
* @param {Object} doc POJO
|
||||
* @param {string} path
|
||||
*/
|
||||
|
||||
module.exports = function getSchemaTypes(schema, doc, path) {
|
||||
const pathschema = schema.path(path);
|
||||
const topLevelDoc = doc;
|
||||
|
||||
if (pathschema) {
|
||||
return pathschema;
|
||||
}
|
||||
|
||||
function search(parts, schema, subdoc, nestedPath) {
|
||||
let p = parts.length + 1;
|
||||
let foundschema;
|
||||
let trypath;
|
||||
|
||||
while (p--) {
|
||||
trypath = parts.slice(0, p).join('.');
|
||||
foundschema = schema.path(trypath);
|
||||
|
||||
if (foundschema == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (foundschema.caster) {
|
||||
// array of Mixed?
|
||||
if (foundschema.caster instanceof Mixed) {
|
||||
return foundschema.caster;
|
||||
}
|
||||
|
||||
let schemas = null;
|
||||
if (doc != null && foundschema.schema != null && foundschema.schema.discriminators != null) {
|
||||
const discriminators = foundschema.schema.discriminators;
|
||||
const discriminatorKeyPath = trypath + '.' +
|
||||
foundschema.schema.options.discriminatorKey;
|
||||
const keys = subdoc ? mpath.get(discriminatorKeyPath, subdoc) || [] : [];
|
||||
schemas = Object.keys(discriminators).
|
||||
reduce(function(cur, discriminator) {
|
||||
if (keys.indexOf(discriminator) !== -1) {
|
||||
cur.push(discriminators[discriminator]);
|
||||
}
|
||||
return cur;
|
||||
}, []);
|
||||
}
|
||||
|
||||
// Now that we found the array, we need to check if there
|
||||
// are remaining document paths to look up for casting.
|
||||
// Also we need to handle array.$.path since schema.path
|
||||
// doesn't work for that.
|
||||
// If there is no foundschema.schema we are dealing with
|
||||
// a path like array.$
|
||||
if (p !== parts.length && foundschema.schema) {
|
||||
let ret;
|
||||
if (parts[p] === '$') {
|
||||
if (p + 1 === parts.length) {
|
||||
// comments.$
|
||||
return foundschema;
|
||||
}
|
||||
// comments.$.comments.$.title
|
||||
ret = search(
|
||||
parts.slice(p + 1),
|
||||
schema,
|
||||
subdoc ? mpath.get(trypath, subdoc) : null,
|
||||
nestedPath.concat(parts.slice(0, p))
|
||||
);
|
||||
if (ret) {
|
||||
ret.$isUnderneathDocArray = ret.$isUnderneathDocArray ||
|
||||
!foundschema.schema.$isSingleNested;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (schemas != null && schemas.length > 0) {
|
||||
ret = [];
|
||||
for (let i = 0; i < schemas.length; ++i) {
|
||||
const _ret = search(
|
||||
parts.slice(p),
|
||||
schemas[i],
|
||||
subdoc ? mpath.get(trypath, subdoc) : null,
|
||||
nestedPath.concat(parts.slice(0, p))
|
||||
);
|
||||
if (_ret != null) {
|
||||
_ret.$isUnderneathDocArray = _ret.$isUnderneathDocArray ||
|
||||
!foundschema.schema.$isSingleNested;
|
||||
if (_ret.$isUnderneathDocArray) {
|
||||
ret.$isUnderneathDocArray = true;
|
||||
}
|
||||
ret.push(_ret);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
} else {
|
||||
ret = search(
|
||||
parts.slice(p),
|
||||
foundschema.schema,
|
||||
subdoc ? mpath.get(trypath, subdoc) : null,
|
||||
nestedPath.concat(parts.slice(0, p))
|
||||
);
|
||||
|
||||
if (ret) {
|
||||
ret.$isUnderneathDocArray = ret.$isUnderneathDocArray ||
|
||||
!foundschema.schema.$isSingleNested;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const fullPath = nestedPath.concat([trypath]).join('.');
|
||||
if (topLevelDoc.$__ && topLevelDoc.populated(fullPath) && p < parts.length) {
|
||||
const schema = get(doc.$__.populated[fullPath], 'options.model.schema');
|
||||
if (schema != null) {
|
||||
const ret = search(
|
||||
parts.slice(p),
|
||||
schema,
|
||||
subdoc ? mpath.get(trypath, subdoc) : null,
|
||||
nestedPath.concat(parts.slice(0, p))
|
||||
);
|
||||
|
||||
if (ret) {
|
||||
ret.$isUnderneathDocArray = ret.$isUnderneathDocArray ||
|
||||
!schema.$isSingleNested;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
const _val = get(topLevelDoc, trypath);
|
||||
if (_val != null) {
|
||||
const model = Array.isArray(_val) && _val.length > 0 ?
|
||||
leanPopulateMap.get(_val[0]) :
|
||||
leanPopulateMap.get(_val);
|
||||
// Populated using lean, `leanPopulateMap` value is the foreign model
|
||||
const schema = model != null ? model.schema : null;
|
||||
if (schema != null) {
|
||||
const ret = search(
|
||||
parts.slice(p),
|
||||
schema,
|
||||
subdoc ? mpath.get(trypath, subdoc) : null,
|
||||
nestedPath.concat(parts.slice(0, p))
|
||||
);
|
||||
|
||||
if (ret) {
|
||||
ret.$isUnderneathDocArray = ret.$isUnderneathDocArray ||
|
||||
!schema.$isSingleNested;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
return foundschema;
|
||||
}
|
||||
}
|
||||
|
||||
// look for arrays
|
||||
const parts = path.split('.');
|
||||
for (let i = 0; i < parts.length; ++i) {
|
||||
if (parts[i] === '$') {
|
||||
// Re: gh-5628, because `schema.path()` doesn't take $ into account.
|
||||
parts[i] = '0';
|
||||
}
|
||||
}
|
||||
return search(parts, schema, doc, []);
|
||||
};
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = getVirtual;
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function getVirtual(schema, name) {
|
||||
if (schema.virtuals[name]) {
|
||||
return schema.virtuals[name];
|
||||
}
|
||||
const parts = name.split('.');
|
||||
let cur = '';
|
||||
let nestedSchemaPath = '';
|
||||
for (let i = 0; i < parts.length; ++i) {
|
||||
cur += (cur.length > 0 ? '.' : '') + parts[i];
|
||||
if (schema.virtuals[cur]) {
|
||||
if (i === parts.length - 1) {
|
||||
schema.virtuals[cur].$nestedSchemaPath = nestedSchemaPath;
|
||||
return schema.virtuals[cur];
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (schema.nested[cur]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (schema.paths[cur] && schema.paths[cur].schema) {
|
||||
schema = schema.paths[cur].schema;
|
||||
const rest = parts.slice(i + 1).join('.');
|
||||
|
||||
if (schema.virtuals[rest]) {
|
||||
if (i === parts.length - 2) {
|
||||
schema.virtuals[rest].$nestedSchemaPath =
|
||||
[nestedSchemaPath, cur].filter(v => !!v).join('.');
|
||||
return schema.virtuals[rest];
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (i + 1 < parts.length && schema.discriminators) {
|
||||
for (const key of Object.keys(schema.discriminators)) {
|
||||
const _virtual = getVirtual(schema.discriminators[key], rest);
|
||||
if (_virtual != null) {
|
||||
_virtual.$nestedSchemaPath = [nestedSchemaPath, cur].
|
||||
filter(v => !!v).join('.');
|
||||
return _virtual;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
nestedSchemaPath += (nestedSchemaPath.length > 0 ? '.' : '') + cur;
|
||||
cur = '';
|
||||
continue;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
module.exports = new WeakMap();
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function normalizeRefPath(refPath, doc, populatedPath) {
|
||||
if (refPath == null) {
|
||||
return refPath;
|
||||
}
|
||||
|
||||
if (typeof refPath === 'function') {
|
||||
refPath = refPath.call(doc, doc, populatedPath);
|
||||
}
|
||||
|
||||
// If populated path has numerics, the end `refPath` should too. For example,
|
||||
// if populating `a.0.b` instead of `a.b` and `b` has `refPath = a.c`, we
|
||||
// should return `a.0.c` for the refPath.
|
||||
const hasNumericProp = /(\.\d+$|\.\d+\.)/g;
|
||||
|
||||
if (hasNumericProp.test(populatedPath)) {
|
||||
const chunks = populatedPath.split(hasNumericProp);
|
||||
|
||||
if (chunks[chunks.length - 1] === '') {
|
||||
throw new Error('Can\'t populate individual element in an array');
|
||||
}
|
||||
|
||||
let _refPath = '';
|
||||
let _remaining = refPath;
|
||||
// 2nd, 4th, etc. will be numeric props. For example: `[ 'a', '.0.', 'b' ]`
|
||||
for (let i = 0; i < chunks.length; i += 2) {
|
||||
const chunk = chunks[i];
|
||||
if (_remaining.startsWith(chunk + '.')) {
|
||||
_refPath += _remaining.substr(0, chunk.length) + chunks[i + 1];
|
||||
_remaining = _remaining.substr(chunk.length + 1);
|
||||
} else if (i === chunks.length - 1) {
|
||||
_refPath += _remaining;
|
||||
_remaining = '';
|
||||
break;
|
||||
} else {
|
||||
throw new Error('Could not normalize ref path, chunk ' + chunk + ' not in populated path');
|
||||
}
|
||||
}
|
||||
|
||||
return _refPath;
|
||||
}
|
||||
|
||||
return refPath;
|
||||
};
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
'use strict';
|
||||
|
||||
const MongooseError = require('../../error/mongooseError');
|
||||
const util = require('util');
|
||||
|
||||
module.exports = validateRef;
|
||||
|
||||
function validateRef(ref, path) {
|
||||
if (typeof ref === 'string') {
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof ref === 'function') {
|
||||
return;
|
||||
}
|
||||
|
||||
throw new MongooseError('Invalid ref at path "' + path + '". Got ' +
|
||||
util.inspect(ref, { depth: 0 }));
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
'use strict';
|
||||
|
||||
if (typeof jest !== 'undefined' && typeof window !== 'undefined') {
|
||||
console.warn('Mongoose: looks like you\'re trying to test a Mongoose app ' +
|
||||
'with Jest\'s default jsdom test environment. Please make sure you read ' +
|
||||
'Mongoose\'s docs on configuring Jest to test Node.js apps: ' +
|
||||
'http://mongoosejs.com/docs/jest.html');
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
'use strict';
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
module.exports = function isDefiningProjection(val) {
|
||||
if (val == null) {
|
||||
// `undefined` or `null` become exclusive projections
|
||||
return true;
|
||||
}
|
||||
if (typeof val === 'object') {
|
||||
// Only cases where a value does **not** define whether the whole projection
|
||||
// is inclusive or exclusive are `$meta` and `$slice`.
|
||||
return !('$meta' in val) && !('$slice' in val);
|
||||
}
|
||||
return true;
|
||||
};
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
'use strict';
|
||||
|
||||
const isDefiningProjection = require('./isDefiningProjection');
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
module.exports = function isExclusive(projection) {
|
||||
const keys = Object.keys(projection);
|
||||
let ki = keys.length;
|
||||
let exclude = null;
|
||||
|
||||
if (ki === 1 && keys[0] === '_id') {
|
||||
exclude = !!projection[keys[ki]];
|
||||
} else {
|
||||
while (ki--) {
|
||||
// Does this projection explicitly define inclusion/exclusion?
|
||||
// Explicitly avoid `$meta` and `$slice`
|
||||
if (keys[ki] !== '_id' && isDefiningProjection(projection[keys[ki]])) {
|
||||
exclude = !projection[keys[ki]];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return exclude;
|
||||
};
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
'use strict';
|
||||
|
||||
const isDefiningProjection = require('./isDefiningProjection');
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
module.exports = function isInclusive(projection) {
|
||||
if (projection == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const props = Object.keys(projection);
|
||||
const numProps = props.length;
|
||||
if (numProps === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (let i = 0; i < numProps; ++i) {
|
||||
const prop = props[i];
|
||||
// Plus paths can't define the projection (see gh-7050)
|
||||
if (prop.charAt(0) === '+') {
|
||||
continue;
|
||||
}
|
||||
// If field is truthy (1, true, etc.) and not an object, then this
|
||||
// projection must be inclusive. If object, assume its $meta, $slice, etc.
|
||||
if (isDefiningProjection(projection[prop]) && !!projection[prop]) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
'use strict';
|
||||
|
||||
const isDefiningProjection = require('./isDefiningProjection');
|
||||
|
||||
/*!
|
||||
* Determines if `path` is excluded by `projection`
|
||||
*
|
||||
* @param {Object} projection
|
||||
* @param {string} path
|
||||
* @return {Boolean}
|
||||
*/
|
||||
|
||||
module.exports = function isPathExcluded(projection, path) {
|
||||
if (path === '_id') {
|
||||
return projection._id === 0;
|
||||
}
|
||||
|
||||
const paths = Object.keys(projection);
|
||||
let type = null;
|
||||
|
||||
for (const _path of paths) {
|
||||
if (isDefiningProjection(projection[_path])) {
|
||||
type = projection[path] === 1 ? 'inclusive' : 'exclusive';
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (type === 'inclusive') {
|
||||
return projection[path] !== 1;
|
||||
}
|
||||
if (type === 'exclusive') {
|
||||
return projection[path] === 0;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
'use strict';
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
module.exports = function isPathSelectedInclusive(fields, path) {
|
||||
const chunks = path.split('.');
|
||||
let cur = '';
|
||||
let j;
|
||||
let keys;
|
||||
let numKeys;
|
||||
for (let i = 0; i < chunks.length; ++i) {
|
||||
cur += cur.length ? '.' : '' + chunks[i];
|
||||
if (fields[cur]) {
|
||||
keys = Object.keys(fields);
|
||||
numKeys = keys.length;
|
||||
for (j = 0; j < numKeys; ++j) {
|
||||
if (keys[i].indexOf(cur + '.') === 0 && keys[i].indexOf(path) !== 0) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
'use strict';
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
module.exports = applyQueryMiddleware;
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
applyQueryMiddleware.middlewareFunctions = [
|
||||
'count',
|
||||
'countDocuments',
|
||||
'deleteMany',
|
||||
'deleteOne',
|
||||
'estimatedDocumentCount',
|
||||
'find',
|
||||
'findOne',
|
||||
'findOneAndDelete',
|
||||
'findOneAndRemove',
|
||||
'findOneAndReplace',
|
||||
'findOneAndUpdate',
|
||||
'remove',
|
||||
'replaceOne',
|
||||
'update',
|
||||
'updateMany',
|
||||
'updateOne'
|
||||
];
|
||||
|
||||
/*!
|
||||
* Apply query middleware
|
||||
*
|
||||
* @param {Query} query constructor
|
||||
* @param {Model} model
|
||||
*/
|
||||
|
||||
function applyQueryMiddleware(Query, model) {
|
||||
const kareemOptions = {
|
||||
useErrorHandlers: true,
|
||||
numCallbackParams: 1,
|
||||
nullResultByDefault: true
|
||||
};
|
||||
|
||||
const middleware = model.hooks.filter(hook => {
|
||||
if (hook.name === 'updateOne') {
|
||||
return hook.query == null || !!hook.query;
|
||||
}
|
||||
if (hook.name === 'remove') {
|
||||
return !!hook.query;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
|
||||
// `update()` thunk has a different name because `_update` was already taken
|
||||
Query.prototype._execUpdate = middleware.createWrapper('update',
|
||||
Query.prototype._execUpdate, null, kareemOptions);
|
||||
|
||||
applyQueryMiddleware.middlewareFunctions.
|
||||
filter(v => v !== 'update').
|
||||
forEach(fn => {
|
||||
Query.prototype[`_${fn}`] = middleware.createWrapper(fn,
|
||||
Query.prototype[`_${fn}`], null, kareemOptions);
|
||||
});
|
||||
}
|
||||
+428
@@ -0,0 +1,428 @@
|
||||
'use strict';
|
||||
|
||||
const CastError = require('../../error/cast');
|
||||
const StrictModeError = require('../../error/strict');
|
||||
const ValidationError = require('../../error/validation');
|
||||
const castNumber = require('../../cast/number');
|
||||
const getEmbeddedDiscriminatorPath = require('./getEmbeddedDiscriminatorPath');
|
||||
const utils = require('../../utils');
|
||||
|
||||
/*!
|
||||
* Casts an update op based on the given schema
|
||||
*
|
||||
* @param {Schema} schema
|
||||
* @param {Object} obj
|
||||
* @param {Object} options
|
||||
* @param {Boolean} [options.overwrite] defaults to false
|
||||
* @param {Boolean|String} [options.strict] defaults to true
|
||||
* @param {Query} context passed to setters
|
||||
* @return {Boolean} true iff the update is non-empty
|
||||
*/
|
||||
|
||||
module.exports = function castUpdate(schema, obj, options, context, filter) {
|
||||
if (!obj) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const ops = Object.keys(obj);
|
||||
let i = ops.length;
|
||||
const ret = {};
|
||||
let hasKeys;
|
||||
let val;
|
||||
let hasDollarKey = false;
|
||||
const overwrite = options.overwrite;
|
||||
|
||||
filter = filter || {};
|
||||
|
||||
while (i--) {
|
||||
const op = ops[i];
|
||||
// if overwrite is set, don't do any of the special $set stuff
|
||||
if (op[0] !== '$' && !overwrite) {
|
||||
// fix up $set sugar
|
||||
if (!ret.$set) {
|
||||
if (obj.$set) {
|
||||
ret.$set = obj.$set;
|
||||
} else {
|
||||
ret.$set = {};
|
||||
}
|
||||
}
|
||||
ret.$set[op] = obj[op];
|
||||
ops.splice(i, 1);
|
||||
if (!~ops.indexOf('$set')) ops.push('$set');
|
||||
} else if (op === '$set') {
|
||||
if (!ret.$set) {
|
||||
ret[op] = obj[op];
|
||||
}
|
||||
} else {
|
||||
ret[op] = obj[op];
|
||||
}
|
||||
}
|
||||
|
||||
// cast each value
|
||||
i = ops.length;
|
||||
|
||||
// if we get passed {} for the update, we still need to respect that when it
|
||||
// is an overwrite scenario
|
||||
if (overwrite) {
|
||||
hasKeys = true;
|
||||
}
|
||||
|
||||
while (i--) {
|
||||
const op = ops[i];
|
||||
val = ret[op];
|
||||
hasDollarKey = hasDollarKey || op.charAt(0) === '$';
|
||||
|
||||
if (val &&
|
||||
typeof val === 'object' &&
|
||||
!Buffer.isBuffer(val) &&
|
||||
(!overwrite || hasDollarKey)) {
|
||||
hasKeys |= walkUpdatePath(schema, val, op, options, context, filter);
|
||||
} else if (overwrite && ret && typeof ret === 'object') {
|
||||
// if we are just using overwrite, cast the query and then we will
|
||||
// *always* return the value, even if it is an empty object. We need to
|
||||
// set hasKeys above because we need to account for the case where the
|
||||
// user passes {} and wants to clobber the whole document
|
||||
// Also, _walkUpdatePath expects an operation, so give it $set since that
|
||||
// is basically what we're doing
|
||||
walkUpdatePath(schema, ret, '$set', options, context, filter);
|
||||
} else {
|
||||
const msg = 'Invalid atomic update value for ' + op + '. '
|
||||
+ 'Expected an object, received ' + typeof val;
|
||||
throw new Error(msg);
|
||||
}
|
||||
}
|
||||
|
||||
return hasKeys && ret;
|
||||
};
|
||||
|
||||
/*!
|
||||
* Walk each path of obj and cast its values
|
||||
* according to its schema.
|
||||
*
|
||||
* @param {Schema} schema
|
||||
* @param {Object} obj - part of a query
|
||||
* @param {String} op - the atomic operator ($pull, $set, etc)
|
||||
* @param {Object} options
|
||||
* @param {Boolean|String} [options.strict]
|
||||
* @param {Boolean} [options.omitUndefined]
|
||||
* @param {Query} context
|
||||
* @param {String} pref - path prefix (internal only)
|
||||
* @return {Bool} true if this path has keys to update
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function walkUpdatePath(schema, obj, op, options, context, filter, pref) {
|
||||
const strict = options.strict;
|
||||
const prefix = pref ? pref + '.' : '';
|
||||
const keys = Object.keys(obj);
|
||||
let i = keys.length;
|
||||
let hasKeys = false;
|
||||
let schematype;
|
||||
let key;
|
||||
let val;
|
||||
|
||||
let aggregatedError = null;
|
||||
|
||||
let useNestedStrict;
|
||||
if (options.useNestedStrict === undefined) {
|
||||
useNestedStrict = schema.options.useNestedStrict;
|
||||
} else {
|
||||
useNestedStrict = options.useNestedStrict;
|
||||
}
|
||||
|
||||
while (i--) {
|
||||
key = keys[i];
|
||||
val = obj[key];
|
||||
|
||||
if (val && val.constructor.name === 'Object') {
|
||||
// watch for embedded doc schemas
|
||||
schematype = schema._getSchema(prefix + key);
|
||||
if (schematype && schematype.caster && op in castOps) {
|
||||
// embedded doc schema
|
||||
if ('$each' in val) {
|
||||
hasKeys = true;
|
||||
try {
|
||||
obj[key] = {
|
||||
$each: castUpdateVal(schematype, val.$each, op, key, context, prefix + key)
|
||||
};
|
||||
} catch (error) {
|
||||
aggregatedError = _handleCastError(error, context, key, aggregatedError);
|
||||
}
|
||||
|
||||
if (val.$slice != null) {
|
||||
obj[key].$slice = val.$slice | 0;
|
||||
}
|
||||
|
||||
if (val.$sort) {
|
||||
obj[key].$sort = val.$sort;
|
||||
}
|
||||
|
||||
if (!!val.$position || val.$position === 0) {
|
||||
obj[key].$position = val.$position;
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
obj[key] = castUpdateVal(schematype, val, op, key, context, prefix + key);
|
||||
} catch (error) {
|
||||
aggregatedError = _handleCastError(error, context, key, aggregatedError);
|
||||
}
|
||||
|
||||
if (options.omitUndefined && obj[key] === void 0) {
|
||||
delete obj[key];
|
||||
continue;
|
||||
}
|
||||
|
||||
hasKeys = true;
|
||||
}
|
||||
} else if ((op === '$currentDate') || (op in castOps && schematype)) {
|
||||
// $currentDate can take an object
|
||||
try {
|
||||
obj[key] = castUpdateVal(schematype, val, op, key, context, prefix + key);
|
||||
} catch (error) {
|
||||
aggregatedError = _handleCastError(error, context, key, aggregatedError);
|
||||
}
|
||||
|
||||
if (options.omitUndefined && obj[key] === void 0) {
|
||||
delete obj[key];
|
||||
continue;
|
||||
}
|
||||
|
||||
hasKeys = true;
|
||||
} else {
|
||||
const pathToCheck = (prefix + key);
|
||||
const v = schema._getPathType(pathToCheck);
|
||||
let _strict = strict;
|
||||
if (useNestedStrict &&
|
||||
v &&
|
||||
v.schema &&
|
||||
'strict' in v.schema.options) {
|
||||
_strict = v.schema.options.strict;
|
||||
}
|
||||
|
||||
if (v.pathType === 'undefined') {
|
||||
if (_strict === 'throw') {
|
||||
throw new StrictModeError(pathToCheck);
|
||||
} else if (_strict) {
|
||||
delete obj[key];
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// gh-2314
|
||||
// we should be able to set a schema-less field
|
||||
// to an empty object literal
|
||||
hasKeys |= walkUpdatePath(schema, val, op, options, context, filter, prefix + key) ||
|
||||
(utils.isObject(val) && Object.keys(val).length === 0);
|
||||
}
|
||||
} else {
|
||||
const checkPath = (key === '$each' || key === '$or' || key === '$and' || key === '$in') ?
|
||||
pref : prefix + key;
|
||||
schematype = schema._getSchema(checkPath);
|
||||
let pathDetails = schema._getPathType(checkPath);
|
||||
|
||||
// If no schema type, check for embedded discriminators
|
||||
if (schematype == null) {
|
||||
const _res = getEmbeddedDiscriminatorPath(schema, obj, filter, checkPath);
|
||||
if (_res.schematype != null) {
|
||||
schematype = _res.schematype;
|
||||
pathDetails = _res.type;
|
||||
}
|
||||
}
|
||||
|
||||
let isStrict = strict;
|
||||
if (useNestedStrict &&
|
||||
pathDetails &&
|
||||
pathDetails.schema &&
|
||||
'strict' in pathDetails.schema.options) {
|
||||
isStrict = pathDetails.schema.options.strict;
|
||||
}
|
||||
|
||||
const skip = isStrict &&
|
||||
!schematype &&
|
||||
!/real|nested/.test(pathDetails.pathType);
|
||||
|
||||
if (skip) {
|
||||
// Even if strict is `throw`, avoid throwing an error because of
|
||||
// virtuals because of #6731
|
||||
if (isStrict === 'throw' && schema.virtuals[checkPath] == null) {
|
||||
throw new StrictModeError(prefix + key);
|
||||
} else {
|
||||
delete obj[key];
|
||||
}
|
||||
} else {
|
||||
// gh-1845 temporary fix: ignore $rename. See gh-3027 for tracking
|
||||
// improving this.
|
||||
if (op === '$rename') {
|
||||
hasKeys = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
obj[key] = castUpdateVal(schematype, val, op, key, context, prefix + key);
|
||||
} catch (error) {
|
||||
aggregatedError = _handleCastError(error, context, key, aggregatedError);
|
||||
}
|
||||
|
||||
if (Array.isArray(obj[key]) && (op === '$addToSet' || op === '$push') && key !== '$each') {
|
||||
if (schematype && schematype.caster && !schematype.caster.$isMongooseArray) {
|
||||
obj[key] = { $each: obj[key] };
|
||||
}
|
||||
}
|
||||
|
||||
if (options.omitUndefined && obj[key] === void 0) {
|
||||
delete obj[key];
|
||||
continue;
|
||||
}
|
||||
|
||||
hasKeys = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (aggregatedError != null) {
|
||||
throw aggregatedError;
|
||||
}
|
||||
|
||||
return hasKeys;
|
||||
}
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function _handleCastError(error, query, key, aggregatedError) {
|
||||
if (typeof query !== 'object' || !query.options.multipleCastError) {
|
||||
throw error;
|
||||
}
|
||||
aggregatedError = aggregatedError || new ValidationError();
|
||||
aggregatedError.addError(key, error);
|
||||
return aggregatedError;
|
||||
}
|
||||
|
||||
/*!
|
||||
* These operators should be cast to numbers instead
|
||||
* of their path schema type.
|
||||
*/
|
||||
|
||||
const numberOps = {
|
||||
$pop: 1,
|
||||
$inc: 1
|
||||
};
|
||||
|
||||
/*!
|
||||
* These ops require no casting because the RHS doesn't do anything.
|
||||
*/
|
||||
|
||||
const noCastOps = {
|
||||
$unset: 1
|
||||
};
|
||||
|
||||
/*!
|
||||
* These operators require casting docs
|
||||
* to real Documents for Update operations.
|
||||
*/
|
||||
|
||||
const castOps = {
|
||||
$push: 1,
|
||||
$addToSet: 1,
|
||||
$set: 1,
|
||||
$setOnInsert: 1
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
const overwriteOps = {
|
||||
$set: 1,
|
||||
$setOnInsert: 1
|
||||
};
|
||||
|
||||
/*!
|
||||
* Casts `val` according to `schema` and atomic `op`.
|
||||
*
|
||||
* @param {SchemaType} schema
|
||||
* @param {Object} val
|
||||
* @param {String} op - the atomic operator ($pull, $set, etc)
|
||||
* @param {String} $conditional
|
||||
* @param {Query} context
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function castUpdateVal(schema, val, op, $conditional, context, path) {
|
||||
if (!schema) {
|
||||
// non-existing schema path
|
||||
if (op in numberOps) {
|
||||
try {
|
||||
return castNumber(val);
|
||||
} catch (err) {
|
||||
throw new CastError('number', val, path);
|
||||
}
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
const cond = schema.caster && op in castOps &&
|
||||
(utils.isObject(val) || Array.isArray(val));
|
||||
if (cond && op !== '$set') {
|
||||
// Cast values for ops that add data to MongoDB.
|
||||
// Ensures embedded documents get ObjectIds etc.
|
||||
const tmp = schema.cast(val);
|
||||
if (Array.isArray(val)) {
|
||||
val = tmp;
|
||||
} else if (Array.isArray(tmp)) {
|
||||
val = tmp[0];
|
||||
} else {
|
||||
val = tmp;
|
||||
}
|
||||
return val;
|
||||
} else if (cond && op === '$set') {
|
||||
return schema.cast(val);
|
||||
}
|
||||
|
||||
if (op in noCastOps) {
|
||||
return val;
|
||||
}
|
||||
if (op in numberOps) {
|
||||
// Null and undefined not allowed for $pop, $inc
|
||||
if (val == null) {
|
||||
throw new CastError('number', val, schema.path);
|
||||
}
|
||||
if (op === '$inc') {
|
||||
// Support `$inc` with long, int32, etc. (gh-4283)
|
||||
return schema.castForQueryWrapper({
|
||||
val: val,
|
||||
context: context
|
||||
});
|
||||
}
|
||||
try {
|
||||
return castNumber(val);
|
||||
} catch (error) {
|
||||
throw new CastError('number', val, schema.path);
|
||||
}
|
||||
}
|
||||
if (op === '$currentDate') {
|
||||
if (typeof val === 'object') {
|
||||
return {$type: val.$type};
|
||||
}
|
||||
return Boolean(val);
|
||||
}
|
||||
|
||||
if (/^\$/.test($conditional)) {
|
||||
return schema.castForQueryWrapper({
|
||||
$conditional: $conditional,
|
||||
val: val,
|
||||
context: context
|
||||
});
|
||||
}
|
||||
|
||||
if (overwriteOps[op]) {
|
||||
return schema.castForQueryWrapper({
|
||||
val: val,
|
||||
context: context,
|
||||
$skipQueryCastForUpdate: val != null && schema.$isMongooseArray && schema.$parentSchema
|
||||
});
|
||||
}
|
||||
|
||||
return schema.castForQueryWrapper({ val: val, context: context });
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
'use strict';
|
||||
|
||||
const helpers = require('../../queryhelpers');
|
||||
|
||||
module.exports = completeMany;
|
||||
|
||||
/*!
|
||||
* Given a model and an array of docs, hydrates all the docs to be instances
|
||||
* of the model. Used to initialize docs returned from the db from `find()`
|
||||
*
|
||||
* @param {Model} model
|
||||
* @param {Array} docs
|
||||
* @param {Object} fields the projection used, including `select` from schemas
|
||||
* @param {Object} userProvidedFields the user-specified projection
|
||||
* @param {Object} opts
|
||||
* @param {Array} [opts.populated]
|
||||
* @param {ClientSession} [opts.session]
|
||||
* @param {Function} callback
|
||||
*/
|
||||
|
||||
function completeMany(model, docs, fields, userProvidedFields, opts, callback) {
|
||||
const arr = [];
|
||||
let count = docs.length;
|
||||
const len = count;
|
||||
let error = null;
|
||||
|
||||
function init(_error) {
|
||||
if (_error != null) {
|
||||
error = error || _error;
|
||||
}
|
||||
if (error != null) {
|
||||
--count || process.nextTick(() => callback(error));
|
||||
return;
|
||||
}
|
||||
--count || process.nextTick(() => callback(error, arr));
|
||||
}
|
||||
|
||||
for (let i = 0; i < len; ++i) {
|
||||
arr[i] = helpers.createModel(model, docs[i], fields, userProvidedFields);
|
||||
try {
|
||||
arr[i].init(docs[i], opts, init);
|
||||
} catch (error) {
|
||||
init(error);
|
||||
}
|
||||
arr[i].$session(opts.session);
|
||||
}
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
'use strict';
|
||||
|
||||
const get = require('../get');
|
||||
|
||||
/*!
|
||||
* Like `schema.path()`, except with a document, because impossible to
|
||||
* determine path type without knowing the embedded discriminator key.
|
||||
*/
|
||||
|
||||
module.exports = function getEmbeddedDiscriminatorPath(schema, update, filter, path) {
|
||||
const parts = path.split('.');
|
||||
let schematype = null;
|
||||
let type = 'adhocOrUndefined';
|
||||
|
||||
filter = filter || {};
|
||||
update = update || {};
|
||||
|
||||
for (let i = 0; i < parts.length; ++i) {
|
||||
const subpath = parts.slice(0, i + 1).join('.').
|
||||
replace(/\.\$\./i, '.0.').replace(/\.\$$/, '.0');
|
||||
schematype = schema.path(subpath);
|
||||
if (schematype == null) {
|
||||
continue;
|
||||
}
|
||||
type = schema.pathType(subpath);
|
||||
if ((schematype.$isSingleNested || schematype.$isMongooseDocumentArrayElement) &&
|
||||
schematype.schema.discriminators != null) {
|
||||
const discriminators = schematype.schema.discriminators;
|
||||
const discriminatorValuePath = subpath + '.' +
|
||||
get(schematype, 'schema.options.discriminatorKey');
|
||||
const discriminatorFilterPath =
|
||||
discriminatorValuePath.replace(/\.\d+\./, '.');
|
||||
let discriminatorKey = null;
|
||||
if (discriminatorValuePath in filter) {
|
||||
discriminatorKey = filter[discriminatorValuePath];
|
||||
}
|
||||
if (discriminatorFilterPath in filter) {
|
||||
discriminatorKey = filter[discriminatorFilterPath];
|
||||
}
|
||||
if (discriminatorKey == null || discriminators[discriminatorKey] == null) {
|
||||
continue;
|
||||
}
|
||||
const rest = parts.slice(i + 1).join('.');
|
||||
schematype = discriminators[discriminatorKey].path(rest);
|
||||
if (schematype != null) {
|
||||
type = discriminators[discriminatorKey]._getPathType(rest);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { type: type, schematype: schematype };
|
||||
};
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
'use strict';
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
module.exports = function(obj) {
|
||||
const keys = Object.keys(obj);
|
||||
const len = keys.length;
|
||||
for (let i = 0; i < len; ++i) {
|
||||
if (keys[i].charAt(0) === '$') {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
'use strict';
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
module.exports = function selectPopulatedFields(query) {
|
||||
const opts = query._mongooseOptions;
|
||||
|
||||
if (opts.populate != null) {
|
||||
const paths = Object.keys(opts.populate);
|
||||
const userProvidedFields = query._userProvidedFields || {};
|
||||
if (query.selectedInclusively()) {
|
||||
for (let i = 0; i < paths.length; ++i) {
|
||||
if (!isPathInFields(userProvidedFields, paths[i])) {
|
||||
query.select(paths[i]);
|
||||
} else if (userProvidedFields[paths[i]] === 0) {
|
||||
delete query._fields[paths[i]];
|
||||
}
|
||||
}
|
||||
} else if (query.selectedExclusively()) {
|
||||
for (let i = 0; i < paths.length; ++i) {
|
||||
if (userProvidedFields[paths[i]] == null) {
|
||||
delete query._fields[paths[i]];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function isPathInFields(userProvidedFields, path) {
|
||||
const pieces = path.split('.');
|
||||
const len = pieces.length;
|
||||
let cur = pieces[0];
|
||||
for (let i = 1; i < len; ++i) {
|
||||
if (userProvidedFields[cur] != null) {
|
||||
return true;
|
||||
}
|
||||
cur += '.' + pieces[i];
|
||||
}
|
||||
return userProvidedFields[cur] != null;
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
'use strict';
|
||||
|
||||
/*!
|
||||
* A query thunk is the function responsible for sending the query to MongoDB,
|
||||
* like `Query#_findOne()` or `Query#_execUpdate()`. The `Query#exec()` function
|
||||
* calls a thunk. The term "thunk" here is the traditional Node.js definition:
|
||||
* a function that takes exactly 1 parameter, a callback.
|
||||
*
|
||||
* This function defines common behavior for all query thunks.
|
||||
*/
|
||||
|
||||
module.exports = function wrapThunk(fn) {
|
||||
return function _wrappedThunk(cb) {
|
||||
++this._executionCount;
|
||||
|
||||
fn.call(this, cb);
|
||||
};
|
||||
};
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
'use strict';
|
||||
|
||||
const get = require('../get');
|
||||
|
||||
module.exports = function applyWriteConcern(schema, options) {
|
||||
const writeConcern = get(schema, 'options.writeConcern', {});
|
||||
if (!('w' in options) && writeConcern.w != null) {
|
||||
options.w = writeConcern.w;
|
||||
}
|
||||
if (!('j' in options) && writeConcern.j != null) {
|
||||
options.j = writeConcern.j;
|
||||
}
|
||||
if (!('wtimeout' in options) && writeConcern.wtimeout != null) {
|
||||
options.wtimeout = writeConcern.wtimeout;
|
||||
}
|
||||
};
|
||||
+124
@@ -0,0 +1,124 @@
|
||||
'use strict';
|
||||
|
||||
const get = require('../get');
|
||||
const utils = require('../../utils');
|
||||
|
||||
/*!
|
||||
* Gather all indexes defined in the schema, including single nested,
|
||||
* document arrays, and embedded discriminators.
|
||||
*/
|
||||
|
||||
module.exports = function getIndexes(schema) {
|
||||
let indexes = [];
|
||||
const schemaStack = new WeakMap();
|
||||
const indexTypes = schema.constructor.indexTypes;
|
||||
|
||||
const collectIndexes = function(schema, prefix) {
|
||||
// Ignore infinitely nested schemas, if we've already seen this schema
|
||||
// along this path there must be a cycle
|
||||
if (schemaStack.has(schema)) {
|
||||
return;
|
||||
}
|
||||
schemaStack.set(schema, true);
|
||||
|
||||
prefix = prefix || '';
|
||||
const keys = Object.keys(schema.paths);
|
||||
const length = keys.length;
|
||||
|
||||
for (let i = 0; i < length; ++i) {
|
||||
const key = keys[i];
|
||||
const path = schema.paths[key];
|
||||
|
||||
if (path.$isMongooseDocumentArray || path.$isSingleNested) {
|
||||
if (get(path, 'options.excludeIndexes') !== true &&
|
||||
get(path, 'schemaOptions.excludeIndexes') !== true) {
|
||||
collectIndexes(path.schema, prefix + key + '.');
|
||||
}
|
||||
|
||||
if (path.schema.discriminators != null) {
|
||||
const discriminators = path.schema.discriminators;
|
||||
const discriminatorKeys = Object.keys(discriminators);
|
||||
for (const discriminatorKey of discriminatorKeys) {
|
||||
collectIndexes(discriminators[discriminatorKey]._originalSchema,
|
||||
prefix + key + '.');
|
||||
}
|
||||
}
|
||||
|
||||
// Retained to minimize risk of backwards breaking changes due to
|
||||
// gh-6113
|
||||
if (path.$isMongooseDocumentArray) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
const index = path._index || (path.caster && path.caster._index);
|
||||
|
||||
if (index !== false && index !== null && index !== undefined) {
|
||||
const field = {};
|
||||
const isObject = utils.isObject(index);
|
||||
const options = isObject ? index : {};
|
||||
const type = typeof index === 'string' ? index :
|
||||
isObject ? index.type :
|
||||
false;
|
||||
|
||||
if (type && indexTypes.indexOf(type) !== -1) {
|
||||
field[prefix + key] = type;
|
||||
} else if (options.text) {
|
||||
field[prefix + key] = 'text';
|
||||
delete options.text;
|
||||
} else {
|
||||
field[prefix + key] = 1;
|
||||
}
|
||||
|
||||
delete options.type;
|
||||
if (!('background' in options)) {
|
||||
options.background = true;
|
||||
}
|
||||
|
||||
indexes.push([field, options]);
|
||||
}
|
||||
}
|
||||
|
||||
schemaStack.delete(schema);
|
||||
|
||||
if (prefix) {
|
||||
fixSubIndexPaths(schema, prefix);
|
||||
} else {
|
||||
schema._indexes.forEach(function(index) {
|
||||
if (!('background' in index[1])) {
|
||||
index[1].background = true;
|
||||
}
|
||||
});
|
||||
indexes = indexes.concat(schema._indexes);
|
||||
}
|
||||
};
|
||||
|
||||
collectIndexes(schema);
|
||||
return indexes;
|
||||
|
||||
/*!
|
||||
* Checks for indexes added to subdocs using Schema.index().
|
||||
* These indexes need their paths prefixed properly.
|
||||
*
|
||||
* schema._indexes = [ [indexObj, options], [indexObj, options] ..]
|
||||
*/
|
||||
|
||||
function fixSubIndexPaths(schema, prefix) {
|
||||
const subindexes = schema._indexes;
|
||||
const len = subindexes.length;
|
||||
for (let i = 0; i < len; ++i) {
|
||||
const indexObj = subindexes[i][0];
|
||||
const keys = Object.keys(indexObj);
|
||||
const klen = keys.length;
|
||||
const newindex = {};
|
||||
|
||||
// use forward iteration, order matters
|
||||
for (let j = 0; j < klen; ++j) {
|
||||
const key = keys[j];
|
||||
newindex[prefix + key] = indexObj[key];
|
||||
}
|
||||
|
||||
indexes.push([newindex, subindexes[i][1]]);
|
||||
}
|
||||
}
|
||||
};
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = handleTimestampOption;
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function handleTimestampOption(arg, prop) {
|
||||
if (arg == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (typeof arg === 'boolean') {
|
||||
return prop;
|
||||
}
|
||||
if (typeof arg[prop] === 'boolean') {
|
||||
return arg[prop] ? prop : null;
|
||||
}
|
||||
if (!(prop in arg)) {
|
||||
return prop;
|
||||
}
|
||||
return arg[prop];
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function merge(s1, s2) {
|
||||
s1.add(s2.obj);
|
||||
|
||||
s1.callQueue = s1.callQueue.concat(s2.callQueue);
|
||||
s1.method(s2.methods);
|
||||
s1.static(s2.statics);
|
||||
|
||||
for (const query in s2.query) {
|
||||
s1.query[query] = s2.query[query];
|
||||
}
|
||||
|
||||
for (const virtual in s2.virtuals) {
|
||||
s1.virtual[virtual] = s2.virtual[virtual].clone();
|
||||
}
|
||||
|
||||
s1.s.hooks.merge(s2.s.hooks, false);
|
||||
};
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
'use strict';
|
||||
|
||||
/*!
|
||||
* Set `$parentSchema` on all schema types, and `$schemaType` on single
|
||||
* nested docs.
|
||||
*
|
||||
* This is a slow path function, should only run when model is compiled
|
||||
*/
|
||||
|
||||
module.exports = function setParentPointers(schema, skipRecursion) {
|
||||
for (const path of Object.keys(schema.paths)) {
|
||||
const schemaType = schema.paths[path];
|
||||
if (schemaType.schema != null) {
|
||||
Object.defineProperty(schemaType.schema, '$schemaType', {
|
||||
configurable: true,
|
||||
writable: false,
|
||||
enumerable: false,
|
||||
value: schemaType
|
||||
});
|
||||
}
|
||||
Object.defineProperty(schemaType, '$parentSchema', {
|
||||
configurable: true,
|
||||
writable: false,
|
||||
enumerable: false,
|
||||
value: schema
|
||||
});
|
||||
}
|
||||
|
||||
// `childSchemas` contains all descendant schemas, so no need to recurse
|
||||
// further.
|
||||
if (skipRecursion) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (const obj of schema.childSchemas) {
|
||||
setParentPointers(obj.schema, true);
|
||||
}
|
||||
};
|
||||
+117
@@ -0,0 +1,117 @@
|
||||
'use strict';
|
||||
|
||||
const modifiedPaths = require('./common').modifiedPaths;
|
||||
|
||||
/**
|
||||
* Applies defaults to update and findOneAndUpdate operations.
|
||||
*
|
||||
* @param {Object} filter
|
||||
* @param {Schema} schema
|
||||
* @param {Object} castedDoc
|
||||
* @param {Object} options
|
||||
* @method setDefaultsOnInsert
|
||||
* @api private
|
||||
*/
|
||||
|
||||
module.exports = function(filter, schema, castedDoc, options) {
|
||||
const keys = Object.keys(castedDoc || {});
|
||||
const updatedKeys = {};
|
||||
const updatedValues = {};
|
||||
const numKeys = keys.length;
|
||||
const modified = {};
|
||||
|
||||
let hasDollarUpdate = false;
|
||||
|
||||
options = options || {};
|
||||
|
||||
if (!options.upsert || !options.setDefaultsOnInsert) {
|
||||
return castedDoc;
|
||||
}
|
||||
|
||||
for (let i = 0; i < numKeys; ++i) {
|
||||
if (keys[i].charAt(0) === '$') {
|
||||
modifiedPaths(castedDoc[keys[i]], '', modified);
|
||||
hasDollarUpdate = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!hasDollarUpdate) {
|
||||
modifiedPaths(castedDoc, '', modified);
|
||||
}
|
||||
|
||||
const paths = Object.keys(filter);
|
||||
const numPaths = paths.length;
|
||||
for (let i = 0; i < numPaths; ++i) {
|
||||
const path = paths[i];
|
||||
const condition = filter[path];
|
||||
if (condition && typeof condition === 'object') {
|
||||
const conditionKeys = Object.keys(condition);
|
||||
const numConditionKeys = conditionKeys.length;
|
||||
let hasDollarKey = false;
|
||||
for (let j = 0; j < numConditionKeys; ++j) {
|
||||
if (conditionKeys[j].charAt(0) === '$') {
|
||||
hasDollarKey = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (hasDollarKey) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
updatedKeys[path] = true;
|
||||
modified[path] = true;
|
||||
}
|
||||
|
||||
if (options && options.overwrite && !hasDollarUpdate) {
|
||||
// Defaults will be set later, since we're overwriting we'll cast
|
||||
// the whole update to a document
|
||||
return castedDoc;
|
||||
}
|
||||
|
||||
schema.eachPath(function(path, schemaType) {
|
||||
if (schemaType.$isSingleNested) {
|
||||
// Only handle nested schemas 1-level deep to avoid infinite
|
||||
// recursion re: https://github.com/mongodb-js/mongoose-autopopulate/issues/11
|
||||
schemaType.schema.eachPath(function(_path, _schemaType) {
|
||||
if (_path === '_id' && _schemaType.auto) {
|
||||
// Ignore _id if auto id so we don't create subdocs
|
||||
return;
|
||||
}
|
||||
|
||||
const def = _schemaType.getDefault(null, true);
|
||||
if (!isModified(modified, path + '.' + _path) &&
|
||||
typeof def !== 'undefined') {
|
||||
castedDoc = castedDoc || {};
|
||||
castedDoc.$setOnInsert = castedDoc.$setOnInsert || {};
|
||||
castedDoc.$setOnInsert[path + '.' + _path] = def;
|
||||
updatedValues[path + '.' + _path] = def;
|
||||
}
|
||||
});
|
||||
} else {
|
||||
const def = schemaType.getDefault(null, true);
|
||||
if (!isModified(modified, path) && typeof def !== 'undefined') {
|
||||
castedDoc = castedDoc || {};
|
||||
castedDoc.$setOnInsert = castedDoc.$setOnInsert || {};
|
||||
castedDoc.$setOnInsert[path] = def;
|
||||
updatedValues[path] = def;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return castedDoc;
|
||||
};
|
||||
|
||||
function isModified(modified, path) {
|
||||
if (modified[path]) {
|
||||
return true;
|
||||
}
|
||||
const sp = path.split('.');
|
||||
let cur = sp[0];
|
||||
for (let i = 1; i < sp.length; ++i) {
|
||||
if (modified[cur]) {
|
||||
return true;
|
||||
}
|
||||
cur += '.' + sp[i];
|
||||
}
|
||||
return false;
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
'use strict';
|
||||
|
||||
exports.validatorErrorSymbol = Symbol.for('mongoose:validatorError');
|
||||
|
||||
exports.documentArrayParent = Symbol.for('mongoose:documentArrayParent');
|
||||
|
||||
exports.modelSymbol = Symbol.for('mongoose#Model');
|
||||
|
||||
exports.getSymbol = Symbol.for('mongoose#Document#get');
|
||||
|
||||
exports.objectIdSymbol = Symbol.for('mongoose#ObjectId');
|
||||
+173
@@ -0,0 +1,173 @@
|
||||
'use strict';
|
||||
|
||||
const handleTimestampOption = require('../schema/handleTimestampOption');
|
||||
|
||||
module.exports = applyTimestampsToChildren;
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function applyTimestampsToChildren(now, update, schema) {
|
||||
if (update == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
const keys = Object.keys(update);
|
||||
let key;
|
||||
let createdAt;
|
||||
let updatedAt;
|
||||
let timestamps;
|
||||
let path;
|
||||
|
||||
const hasDollarKey = keys.length && keys[0].charAt(0) === '$';
|
||||
|
||||
if (hasDollarKey) {
|
||||
if (update.$push) {
|
||||
for (key in update.$push) {
|
||||
const $path = schema.path(key);
|
||||
if (update.$push[key] &&
|
||||
$path &&
|
||||
$path.$isMongooseDocumentArray &&
|
||||
$path.schema.options.timestamps) {
|
||||
timestamps = $path.schema.options.timestamps;
|
||||
createdAt = handleTimestampOption(timestamps, 'createdAt');
|
||||
updatedAt = handleTimestampOption(timestamps, 'updatedAt');
|
||||
if (update.$push[key].$each) {
|
||||
update.$push[key].$each.forEach(function(subdoc) {
|
||||
if (updatedAt != null) {
|
||||
subdoc[updatedAt] = now;
|
||||
}
|
||||
if (createdAt != null) {
|
||||
subdoc[createdAt] = now;
|
||||
}
|
||||
});
|
||||
} else {
|
||||
if (updatedAt != null) {
|
||||
update.$push[key][updatedAt] = now;
|
||||
}
|
||||
if (createdAt != null) {
|
||||
update.$push[key][createdAt] = now;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (update.$set != null) {
|
||||
const keys = Object.keys(update.$set);
|
||||
for (key of keys) {
|
||||
// Replace positional operator `$` and array filters `$[]` and `$[.*]`
|
||||
const keyToSearch = key.
|
||||
replace(/\.\$(\[[^\]]*\])?\./g, '.0.').
|
||||
replace(/\.(\[[^\]]*\])?\$$/, '.0');
|
||||
path = schema.path(keyToSearch);
|
||||
if (!path) {
|
||||
continue;
|
||||
}
|
||||
if (Array.isArray(update.$set[key]) && path.$isMongooseDocumentArray) {
|
||||
applyTimestampsToDocumentArray(update.$set[key], path, now);
|
||||
} else if (update.$set[key] && path.$isSingleNested) {
|
||||
applyTimestampsToSingleNested(update.$set[key], path, now);
|
||||
} else if (path.$parentSchema !== schema && path.$parentSchema != null) {
|
||||
const parentPath = path.$parentSchema.$schemaType;
|
||||
|
||||
if (parentPath == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
timestamps = parentPath.schema.options.timestamps;
|
||||
createdAt = handleTimestampOption(timestamps, 'createdAt');
|
||||
updatedAt = handleTimestampOption(timestamps, 'updatedAt');
|
||||
|
||||
if (updatedAt == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (parentPath.$isSingleNested) {
|
||||
// Single nested is easy
|
||||
update.$set[parentPath.path + '.' + updatedAt] = now;
|
||||
continue;
|
||||
}
|
||||
|
||||
let childPath = key.substr(parentPath.path.length + 1);
|
||||
const firstDot = childPath.indexOf('.');
|
||||
|
||||
// Shouldn't happen, but if it does ignore this path
|
||||
if (firstDot === -1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
childPath = childPath.substr(0, firstDot);
|
||||
|
||||
update.$set[parentPath.path + '.' + childPath + '.' + updatedAt] = now;
|
||||
} else if (path.schema != null && path.schema != schema) {
|
||||
timestamps = path.schema.options.timestamps;
|
||||
createdAt = handleTimestampOption(timestamps, 'createdAt');
|
||||
updatedAt = handleTimestampOption(timestamps, 'updatedAt');
|
||||
|
||||
if (updatedAt != null) {
|
||||
update.$set[key][updatedAt] = now;
|
||||
}
|
||||
if (createdAt != null) {
|
||||
update.$set[key][createdAt] = now;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const keys = Object.keys(update).filter(key => !key.startsWith('$'));
|
||||
for (key of keys) {
|
||||
// Replace positional operator `$` and array filters `$[]` and `$[.*]`
|
||||
const keyToSearch = key.
|
||||
replace(/\.\$(\[[^\]]*\])?\./g, '.0.').
|
||||
replace(/\.(\[[^\]]*\])?\$$/, '.0');
|
||||
path = schema.path(keyToSearch);
|
||||
if (!path) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (Array.isArray(update[key]) && path.$isMongooseDocumentArray) {
|
||||
applyTimestampsToDocumentArray(update[key], path, now);
|
||||
} else if (update[key] != null && path.$isSingleNested) {
|
||||
applyTimestampsToSingleNested(update[key], path, now);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function applyTimestampsToDocumentArray(arr, schematype, now) {
|
||||
const timestamps = schematype.schema.options.timestamps;
|
||||
|
||||
if (!timestamps) {
|
||||
return;
|
||||
}
|
||||
|
||||
const len = arr.length;
|
||||
|
||||
const createdAt = handleTimestampOption(timestamps, 'createdAt');
|
||||
const updatedAt = handleTimestampOption(timestamps, 'updatedAt');
|
||||
for (let i = 0; i < len; ++i) {
|
||||
if (updatedAt != null) {
|
||||
arr[i][updatedAt] = now;
|
||||
}
|
||||
if (createdAt != null) {
|
||||
arr[i][createdAt] = now;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function applyTimestampsToSingleNested(subdoc, schematype, now) {
|
||||
const timestamps = schematype.schema.options.timestamps;
|
||||
if (!timestamps) {
|
||||
return;
|
||||
}
|
||||
|
||||
const createdAt = handleTimestampOption(timestamps, 'createdAt');
|
||||
const updatedAt = handleTimestampOption(timestamps, 'updatedAt');
|
||||
if (updatedAt != null) {
|
||||
subdoc[updatedAt] = now;
|
||||
}
|
||||
if (createdAt != null) {
|
||||
subdoc[createdAt] = now;
|
||||
}
|
||||
}
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
'use strict';
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
const get = require('../get');
|
||||
|
||||
module.exports = applyTimestampsToUpdate;
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function applyTimestampsToUpdate(now, createdAt, updatedAt, currentUpdate, options) {
|
||||
const updates = currentUpdate;
|
||||
let _updates = updates;
|
||||
const overwrite = get(options, 'overwrite', false);
|
||||
const timestamps = get(options, 'timestamps', true);
|
||||
|
||||
// Support skipping timestamps at the query level, see gh-6980
|
||||
if (!timestamps || updates == null) {
|
||||
return currentUpdate;
|
||||
}
|
||||
|
||||
if (overwrite) {
|
||||
if (currentUpdate && currentUpdate.$set) {
|
||||
currentUpdate = currentUpdate.$set;
|
||||
updates.$set = {};
|
||||
_updates = updates.$set;
|
||||
}
|
||||
if (updatedAt && !currentUpdate[updatedAt]) {
|
||||
_updates[updatedAt] = now;
|
||||
}
|
||||
if (createdAt && !currentUpdate[createdAt]) {
|
||||
_updates[createdAt] = now;
|
||||
}
|
||||
return updates;
|
||||
}
|
||||
updates.$set = updates.$set || {};
|
||||
currentUpdate = currentUpdate || {};
|
||||
|
||||
if (updatedAt &&
|
||||
(!currentUpdate.$currentDate || !currentUpdate.$currentDate[updatedAt])) {
|
||||
updates.$set[updatedAt] = now;
|
||||
}
|
||||
|
||||
if (createdAt) {
|
||||
if (currentUpdate[createdAt]) {
|
||||
delete currentUpdate[createdAt];
|
||||
}
|
||||
if (currentUpdate.$set && currentUpdate.$set[createdAt]) {
|
||||
delete currentUpdate.$set[createdAt];
|
||||
}
|
||||
|
||||
updates.$setOnInsert = updates.$setOnInsert || {};
|
||||
updates.$setOnInsert[createdAt] = now;
|
||||
}
|
||||
|
||||
if (Object.keys(updates.$set).length === 0) {
|
||||
delete updates.$set;
|
||||
}
|
||||
|
||||
return updates;
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
'use strict';
|
||||
|
||||
const _modifiedPaths = require('../common').modifiedPaths;
|
||||
|
||||
/**
|
||||
* Given an update document with potential update operators (`$set`, etc.)
|
||||
* returns an object whose keys are the directly modified paths.
|
||||
*
|
||||
* If there are any top-level keys that don't start with `$`, we assume those
|
||||
* will get wrapped in a `$set`. The Mongoose Query is responsible for wrapping
|
||||
* top-level keys in `$set`.
|
||||
*
|
||||
* @param {Object} update
|
||||
* @return {Object} modified
|
||||
*/
|
||||
|
||||
module.exports = function modifiedPaths(update) {
|
||||
const keys = Object.keys(update);
|
||||
const res = {};
|
||||
|
||||
const withoutDollarKeys = {};
|
||||
for (const key of keys) {
|
||||
if (key.startsWith('$')) {
|
||||
_modifiedPaths(update[key], '', res);
|
||||
continue;
|
||||
}
|
||||
withoutDollarKeys[key] = update[key];
|
||||
}
|
||||
|
||||
_modifiedPaths(withoutDollarKeys, '', res);
|
||||
|
||||
return res;
|
||||
};
|
||||
+227
@@ -0,0 +1,227 @@
|
||||
'use strict';
|
||||
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
const Mixed = require('../schema/mixed');
|
||||
const ValidationError = require('../error/validation');
|
||||
const flatten = require('./common').flatten;
|
||||
const modifiedPaths = require('./common').modifiedPaths;
|
||||
const parallel = require('async/parallel');
|
||||
|
||||
/**
|
||||
* Applies validators and defaults to update and findOneAndUpdate operations,
|
||||
* specifically passing a null doc as `this` to validators and defaults
|
||||
*
|
||||
* @param {Query} query
|
||||
* @param {Schema} schema
|
||||
* @param {Object} castedDoc
|
||||
* @param {Object} options
|
||||
* @method runValidatorsOnUpdate
|
||||
* @api private
|
||||
*/
|
||||
|
||||
module.exports = function(query, schema, castedDoc, options) {
|
||||
let _keys;
|
||||
const keys = Object.keys(castedDoc || {});
|
||||
let updatedKeys = {};
|
||||
let updatedValues = {};
|
||||
const isPull = {};
|
||||
const arrayAtomicUpdates = {};
|
||||
const numKeys = keys.length;
|
||||
let hasDollarUpdate = false;
|
||||
const modified = {};
|
||||
let currentUpdate;
|
||||
let key;
|
||||
let i;
|
||||
|
||||
for (i = 0; i < numKeys; ++i) {
|
||||
if (keys[i].charAt(0) === '$') {
|
||||
hasDollarUpdate = true;
|
||||
if (keys[i] === '$push' || keys[i] === '$addToSet') {
|
||||
_keys = Object.keys(castedDoc[keys[i]]);
|
||||
for (let ii = 0; ii < _keys.length; ++ii) {
|
||||
currentUpdate = castedDoc[keys[i]][_keys[ii]];
|
||||
if (currentUpdate && currentUpdate.$each) {
|
||||
arrayAtomicUpdates[_keys[ii]] = (arrayAtomicUpdates[_keys[ii]] || []).
|
||||
concat(currentUpdate.$each);
|
||||
} else {
|
||||
arrayAtomicUpdates[_keys[ii]] = (arrayAtomicUpdates[_keys[ii]] || []).
|
||||
concat([currentUpdate]);
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
modifiedPaths(castedDoc[keys[i]], '', modified);
|
||||
const flat = flatten(castedDoc[keys[i]]);
|
||||
const paths = Object.keys(flat);
|
||||
const numPaths = paths.length;
|
||||
for (let j = 0; j < numPaths; ++j) {
|
||||
let updatedPath = paths[j].replace('.$.', '.0.');
|
||||
updatedPath = updatedPath.replace(/\.\$$/, '.0');
|
||||
key = keys[i];
|
||||
// With `$pull` we might flatten `$in`. Skip stuff nested under `$in`
|
||||
// for the rest of the logic, it will get handled later.
|
||||
if (updatedPath.indexOf('$') !== -1) {
|
||||
continue;
|
||||
}
|
||||
if (key === '$set' || key === '$setOnInsert' ||
|
||||
key === '$pull' || key === '$pullAll') {
|
||||
updatedValues[updatedPath] = flat[paths[j]];
|
||||
isPull[updatedPath] = key === '$pull' || key === '$pullAll';
|
||||
} else if (key === '$unset') {
|
||||
updatedValues[updatedPath] = undefined;
|
||||
}
|
||||
updatedKeys[updatedPath] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!hasDollarUpdate) {
|
||||
modifiedPaths(castedDoc, '', modified);
|
||||
updatedValues = flatten(castedDoc);
|
||||
updatedKeys = Object.keys(updatedValues);
|
||||
}
|
||||
|
||||
const updates = Object.keys(updatedValues);
|
||||
const numUpdates = updates.length;
|
||||
const validatorsToExecute = [];
|
||||
const validationErrors = [];
|
||||
|
||||
const alreadyValidated = [];
|
||||
|
||||
const context = options && options.context === 'query' ? query : null;
|
||||
function iter(i, v) {
|
||||
const schemaPath = schema._getSchema(updates[i]);
|
||||
if (schemaPath) {
|
||||
// gh-4305: `_getSchema()` will report all sub-fields of a 'Mixed' path
|
||||
// as 'Mixed', so avoid double validating them.
|
||||
if (schemaPath instanceof Mixed && schemaPath.$fullPath !== updates[i]) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (v && Array.isArray(v.$in)) {
|
||||
v.$in.forEach((v, i) => {
|
||||
validatorsToExecute.push(function(callback) {
|
||||
schemaPath.doValidate(
|
||||
v,
|
||||
function(err) {
|
||||
if (err) {
|
||||
err.path = updates[i] + '.$in.' + i;
|
||||
validationErrors.push(err);
|
||||
}
|
||||
callback(null);
|
||||
},
|
||||
context,
|
||||
{updateValidator: true});
|
||||
});
|
||||
});
|
||||
} else {
|
||||
if (isPull[updates[i]] &&
|
||||
!Array.isArray(v) &&
|
||||
schemaPath.$isMongooseArray) {
|
||||
v = [v];
|
||||
}
|
||||
|
||||
if (schemaPath.$isMongooseDocumentArrayElement && v != null && v.$__ != null) {
|
||||
alreadyValidated.push(updates[i]);
|
||||
validatorsToExecute.push(function(callback) {
|
||||
schemaPath.doValidate(v, function(err) {
|
||||
if (err) {
|
||||
err.path = updates[i];
|
||||
validationErrors.push(err);
|
||||
return callback(null);
|
||||
}
|
||||
|
||||
v.validate(function(err) {
|
||||
if (err) {
|
||||
if (err.errors) {
|
||||
for (const key of Object.keys(err.errors)) {
|
||||
const _err = err.errors[key];
|
||||
_err.path = updates[i] + '.' + key;
|
||||
validationErrors.push(_err);
|
||||
}
|
||||
}
|
||||
}
|
||||
callback(null);
|
||||
});
|
||||
}, context, { updateValidator: true });
|
||||
});
|
||||
} else {
|
||||
validatorsToExecute.push(function(callback) {
|
||||
for (const path of alreadyValidated) {
|
||||
if (updates[i].startsWith(path + '.')) {
|
||||
return callback(null);
|
||||
}
|
||||
}
|
||||
schemaPath.doValidate(v, function(err) {
|
||||
if (err) {
|
||||
err.path = updates[i];
|
||||
validationErrors.push(err);
|
||||
}
|
||||
callback(null);
|
||||
}, context, { updateValidator: true });
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (i = 0; i < numUpdates; ++i) {
|
||||
iter(i, updatedValues[updates[i]]);
|
||||
}
|
||||
|
||||
const arrayUpdates = Object.keys(arrayAtomicUpdates);
|
||||
const numArrayUpdates = arrayUpdates.length;
|
||||
for (i = 0; i < numArrayUpdates; ++i) {
|
||||
(function(i) {
|
||||
let schemaPath = schema._getSchema(arrayUpdates[i]);
|
||||
if (schemaPath && schemaPath.$isMongooseDocumentArray) {
|
||||
validatorsToExecute.push(function(callback) {
|
||||
schemaPath.doValidate(
|
||||
arrayAtomicUpdates[arrayUpdates[i]],
|
||||
function(err) {
|
||||
if (err) {
|
||||
err.path = arrayUpdates[i];
|
||||
validationErrors.push(err);
|
||||
}
|
||||
callback(null);
|
||||
},
|
||||
options && options.context === 'query' ? query : null);
|
||||
});
|
||||
} else {
|
||||
schemaPath = schema._getSchema(arrayUpdates[i] + '.0');
|
||||
for (let j = 0; j < arrayAtomicUpdates[arrayUpdates[i]].length; ++j) {
|
||||
(function(j) {
|
||||
validatorsToExecute.push(function(callback) {
|
||||
schemaPath.doValidate(
|
||||
arrayAtomicUpdates[arrayUpdates[i]][j],
|
||||
function(err) {
|
||||
if (err) {
|
||||
err.path = arrayUpdates[i];
|
||||
validationErrors.push(err);
|
||||
}
|
||||
callback(null);
|
||||
},
|
||||
options && options.context === 'query' ? query : null,
|
||||
{ updateValidator: true });
|
||||
});
|
||||
})(j);
|
||||
}
|
||||
}
|
||||
})(i);
|
||||
}
|
||||
|
||||
return function(callback) {
|
||||
parallel(validatorsToExecute, function() {
|
||||
if (validationErrors.length) {
|
||||
const err = new ValidationError(null);
|
||||
for (let i = 0; i < validationErrors.length; ++i) {
|
||||
err.addError(validationErrors[i].path, validationErrors[i]);
|
||||
}
|
||||
return callback(err);
|
||||
}
|
||||
callback(null);
|
||||
});
|
||||
};
|
||||
};
|
||||
+956
@@ -0,0 +1,956 @@
|
||||
'use strict';
|
||||
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
if (global.MONGOOSE_DRIVER_PATH) {
|
||||
require('./driver').set(require(global.MONGOOSE_DRIVER_PATH));
|
||||
} else {
|
||||
require('./driver').set(require('./drivers/node-mongodb-native'));
|
||||
}
|
||||
|
||||
const Schema = require('./schema');
|
||||
const SchemaType = require('./schematype');
|
||||
const SchemaTypes = require('./schema/index');
|
||||
const VirtualType = require('./virtualtype');
|
||||
const STATES = require('./connectionstate');
|
||||
const Types = require('./types');
|
||||
const Query = require('./query');
|
||||
const Model = require('./model');
|
||||
const Document = require('./document');
|
||||
const legacyPluralize = require('mongoose-legacy-pluralize');
|
||||
const utils = require('./utils');
|
||||
const pkg = require('../package.json');
|
||||
|
||||
const removeSubdocs = require('./plugins/removeSubdocs');
|
||||
const saveSubdocs = require('./plugins/saveSubdocs');
|
||||
const validateBeforeSave = require('./plugins/validateBeforeSave');
|
||||
|
||||
const Aggregate = require('./aggregate');
|
||||
const PromiseProvider = require('./promise_provider');
|
||||
const shardingPlugin = require('./plugins/sharding');
|
||||
|
||||
const defaultMongooseSymbol = Symbol.for('mongoose:default');
|
||||
|
||||
require('./helpers/printJestWarning');
|
||||
|
||||
/**
|
||||
* Mongoose constructor.
|
||||
*
|
||||
* The exports object of the `mongoose` module is an instance of this class.
|
||||
* Most apps will only use this one instance.
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function Mongoose(options) {
|
||||
this.connections = [];
|
||||
this.models = {};
|
||||
this.modelSchemas = {};
|
||||
// default global options
|
||||
this.options = {
|
||||
pluralization: true
|
||||
};
|
||||
const conn = this.createConnection(); // default connection
|
||||
conn.models = this.models;
|
||||
|
||||
this._pluralize = legacyPluralize;
|
||||
|
||||
// If a user creates their own Mongoose instance, give them a separate copy
|
||||
// of the `Schema` constructor so they get separate custom types. (gh-6933)
|
||||
if (!options || !options[defaultMongooseSymbol]) {
|
||||
const _this = this;
|
||||
this.Schema = function() {
|
||||
this.base = _this;
|
||||
return Schema.apply(this, arguments);
|
||||
};
|
||||
this.Schema.prototype = Object.create(Schema.prototype);
|
||||
|
||||
Object.assign(this.Schema, Schema);
|
||||
this.Schema.base = this;
|
||||
this.Schema.Types = Object.assign({}, Schema.Types);
|
||||
} else {
|
||||
// Hack to work around babel's strange behavior with
|
||||
// `import mongoose, { Schema } from 'mongoose'`. Because `Schema` is not
|
||||
// an own property of a Mongoose global, Schema will be undefined. See gh-5648
|
||||
for (const key of ['Schema', 'model']) {
|
||||
this[key] = Mongoose.prototype[key];
|
||||
}
|
||||
}
|
||||
this.Schema.prototype.base = this;
|
||||
|
||||
Object.defineProperty(this, 'plugins', {
|
||||
configurable: false,
|
||||
enumerable: true,
|
||||
writable: false,
|
||||
value: [
|
||||
[saveSubdocs, { deduplicate: true }],
|
||||
[validateBeforeSave, { deduplicate: true }],
|
||||
[shardingPlugin, { deduplicate: true }],
|
||||
[removeSubdocs, { deduplicate: true }]
|
||||
]
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Expose connection states for user-land
|
||||
*
|
||||
* @memberOf Mongoose
|
||||
* @property STATES
|
||||
* @api public
|
||||
*/
|
||||
Mongoose.prototype.STATES = STATES;
|
||||
|
||||
/**
|
||||
* Sets mongoose options
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* mongoose.set('test', value) // sets the 'test' option to `value`
|
||||
*
|
||||
* mongoose.set('debug', true) // enable logging collection methods + arguments to the console
|
||||
*
|
||||
* mongoose.set('debug', function(collectionName, methodName, arg1, arg2...) {}); // use custom function to log collection methods + arguments
|
||||
*
|
||||
* Currently supported options are:
|
||||
* - 'debug': prints the operations mongoose sends to MongoDB to the console
|
||||
* - 'bufferCommands': enable/disable mongoose's buffering mechanism for all connections and models
|
||||
* - 'useCreateIndex': false by default. Set to `true` to make Mongoose's default index build use `createIndex()` instead of `ensureIndex()` to avoid deprecation warnings from the MongoDB driver.
|
||||
* - 'useFindAndModify': true by default. Set to `false` to make `findOneAndUpdate()` and `findOneAndRemove()` use native `findOneAndUpdate()` rather than `findAndModify()`.
|
||||
* - 'useNewUrlParser': false by default. Set to `true` to make all connections set the `useNewUrlParser` option by default
|
||||
* - 'cloneSchemas': false by default. Set to `true` to `clone()` all schemas before compiling into a model.
|
||||
* - 'applyPluginsToDiscriminators': false by default. Set to true to apply global plugins to discriminator schemas. This typically isn't necessary because plugins are applied to the base schema and discriminators copy all middleware, methods, statics, and properties from the base schema.
|
||||
* - 'objectIdGetter': true by default. Mongoose adds a getter to MongoDB ObjectId's called `_id` that returns `this` for convenience with populate. Set this to false to remove the getter.
|
||||
* - 'runValidators': false by default. Set to true to enable [update validators](/docs/validation.html#update-validators) for all validators by default.
|
||||
* - 'toObject': `{ transform: true, flattenDecimals: true }` by default. Overwrites default objects to [`toObject()`](/docs/api.html#document_Document-toObject)
|
||||
* - 'toJSON': `{ transform: true, flattenDecimals: true }` by default. Overwrites default objects to [`toJSON()`](/docs/api.html#document_Document-toJSON), for determining how Mongoose documents get serialized by `JSON.stringify()`
|
||||
* - 'strict': true by default, may be `false`, `true`, or `'throw'`. Sets the default strict mode for schemas.
|
||||
* - 'selectPopulatedPaths': true by default. Set to false to opt out of Mongoose adding all fields that you `populate()` to your `select()`. The schema-level option `selectPopulatedPaths` overwrites this one.
|
||||
*
|
||||
* @param {String} key
|
||||
* @param {String|Function|Boolean} value
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mongoose.prototype.set = function(key, value) {
|
||||
if (arguments.length === 1) {
|
||||
return this.options[key];
|
||||
}
|
||||
|
||||
this.options[key] = value;
|
||||
|
||||
if (key === 'objectIdGetter') {
|
||||
if (value) {
|
||||
Object.defineProperty(mongoose.Types.ObjectId.prototype, '_id', {
|
||||
enumerable: false,
|
||||
configurable: true,
|
||||
get: function() {
|
||||
return this;
|
||||
}
|
||||
});
|
||||
} else {
|
||||
delete mongoose.Types.ObjectId.prototype._id;
|
||||
}
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets mongoose options
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* mongoose.get('test') // returns the 'test' value
|
||||
*
|
||||
* @param {String} key
|
||||
* @method get
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mongoose.prototype.get = Mongoose.prototype.set;
|
||||
|
||||
/**
|
||||
* Creates a Connection instance.
|
||||
*
|
||||
* Each `connection` instance maps to a single database. This method is helpful when mangaging multiple db connections.
|
||||
*
|
||||
*
|
||||
* _Options passed take precedence over options included in connection strings._
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* // with mongodb:// URI
|
||||
* db = mongoose.createConnection('mongodb://user:pass@localhost:port/database');
|
||||
*
|
||||
* // and options
|
||||
* var opts = { db: { native_parser: true }}
|
||||
* db = mongoose.createConnection('mongodb://user:pass@localhost:port/database', opts);
|
||||
*
|
||||
* // replica sets
|
||||
* db = mongoose.createConnection('mongodb://user:pass@localhost:port,anotherhost:port,yetanother:port/database');
|
||||
*
|
||||
* // and options
|
||||
* var opts = { replset: { strategy: 'ping', rs_name: 'testSet' }}
|
||||
* db = mongoose.createConnection('mongodb://user:pass@localhost:port,anotherhost:port,yetanother:port/database', opts);
|
||||
*
|
||||
* // and options
|
||||
* var opts = { server: { auto_reconnect: false }, user: 'username', pass: 'mypassword' }
|
||||
* db = mongoose.createConnection('localhost', 'database', port, opts)
|
||||
*
|
||||
* // initialize now, connect later
|
||||
* db = mongoose.createConnection();
|
||||
* db.openUri('localhost', 'database', port, [opts]);
|
||||
*
|
||||
* @param {String} [uri] a mongodb:// URI
|
||||
* @param {Object} [options] passed down to the [MongoDB driver's `connect()` function](http://mongodb.github.io/node-mongodb-native/3.0/api/MongoClient.html), except for 4 mongoose-specific options explained below.
|
||||
* @param {String} [options.user] username for authentication, equivalent to `options.auth.user`. Maintained for backwards compatibility.
|
||||
* @param {String} [options.pass] password for authentication, equivalent to `options.auth.password`. Maintained for backwards compatibility.
|
||||
* @param {Boolean} [options.autoIndex=true] Mongoose-specific option. Set to false to disable automatic index creation for all models associated with this connection.
|
||||
* @param {Boolean} [options.bufferCommands=true] Mongoose specific option. Set to false to [disable buffering](http://mongoosejs.com/docs/faq.html#callback_never_executes) on all models associated with this connection.
|
||||
* @return {Connection} the created Connection object. Connections are thenable, so you can do `await mongoose.createConnection()`
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mongoose.prototype.createConnection = function(uri, options, callback) {
|
||||
const conn = new Connection(this);
|
||||
if (typeof options === 'function') {
|
||||
callback = options;
|
||||
options = null;
|
||||
}
|
||||
this.connections.push(conn);
|
||||
|
||||
if (arguments.length > 0) {
|
||||
return conn.openUri(uri, options, callback);
|
||||
}
|
||||
|
||||
return conn;
|
||||
};
|
||||
|
||||
/**
|
||||
* Opens the default mongoose connection.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* mongoose.connect('mongodb://user:pass@localhost:port/database');
|
||||
*
|
||||
* // replica sets
|
||||
* var uri = 'mongodb://user:pass@localhost:port,anotherhost:port,yetanother:port/mydatabase';
|
||||
* mongoose.connect(uri);
|
||||
*
|
||||
* // with options
|
||||
* mongoose.connect(uri, options);
|
||||
*
|
||||
* // optional callback that gets fired when initial connection completed
|
||||
* var uri = 'mongodb://nonexistent.domain:27000';
|
||||
* mongoose.connect(uri, function(error) {
|
||||
* // if error is truthy, the initial connection failed.
|
||||
* })
|
||||
*
|
||||
* @param {String} uri(s)
|
||||
* @param {Object} [options] passed down to the [MongoDB driver's `connect()` function](http://mongodb.github.io/node-mongodb-native/3.0/api/MongoClient.html), except for 4 mongoose-specific options explained below.
|
||||
* @param {String} [options.dbName] The name of the database we want to use. If not provided, use database name from connection string.
|
||||
* @param {String} [options.user] username for authentication, equivalent to `options.auth.user`. Maintained for backwards compatibility.
|
||||
* @param {String} [options.pass] password for authentication, equivalent to `options.auth.password`. Maintained for backwards compatibility.
|
||||
* @param {Boolean} [options.autoIndex=true] Mongoose-specific option. Set to false to disable automatic index creation for all models associated with this connection.
|
||||
* @param {Boolean} [options.bufferCommands=true] Mongoose specific option. Set to false to [disable buffering](http://mongoosejs.com/docs/faq.html#callback_never_executes) on all models associated with this connection.
|
||||
* @param {Boolean} [options.useCreateIndex=true] Mongoose-specific option. If `true`, this connection will use [`createIndex()` instead of `ensureIndex()`](/docs/deprecations.html#-ensureindex-) for automatic index builds via [`Model.init()`](/docs/api.html#model_Model.init).
|
||||
* @param {Boolean} [options.useFindAndModify=true] True by default. Set to `false` to make `findOneAndUpdate()` and `findOneAndRemove()` use native `findOneAndUpdate()` rather than `findAndModify()`.
|
||||
* @param {Boolean} [options.useNewUrlParser=false] False by default. Set to `true` to make all connections set the `useNewUrlParser` option by default.
|
||||
* @param {Function} [callback]
|
||||
* @see Mongoose#createConnection #index_Mongoose-createConnection
|
||||
* @api public
|
||||
* @return {Promise} resolves to `this` if connection succeeded
|
||||
*/
|
||||
|
||||
Mongoose.prototype.connect = function() {
|
||||
const _mongoose = this instanceof Mongoose ? this : mongoose;
|
||||
const conn = _mongoose.connection;
|
||||
return conn.openUri(arguments[0], arguments[1], arguments[2]).then(() => _mongoose);
|
||||
};
|
||||
|
||||
/**
|
||||
* Runs `.close()` on all connections in parallel.
|
||||
*
|
||||
* @param {Function} [callback] called after all connection close, or when first error occurred.
|
||||
* @return {Promise} resolves when all connections are closed, or rejects with the first error that occurred.
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mongoose.prototype.disconnect = function(callback) {
|
||||
return utils.promiseOrCallback(callback, cb => {
|
||||
let remaining = this.connections.length;
|
||||
if (remaining <= 0) {
|
||||
return cb(null);
|
||||
}
|
||||
this.connections.forEach(conn => {
|
||||
conn.close(function(error) {
|
||||
if (error) {
|
||||
return cb(error);
|
||||
}
|
||||
if (!--remaining) {
|
||||
cb(null);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* _Requires MongoDB >= 3.6.0._ Starts a [MongoDB session](https://docs.mongodb.com/manual/release-notes/3.6/#client-sessions)
|
||||
* for benefits like causal consistency, [retryable writes](https://docs.mongodb.com/manual/core/retryable-writes/),
|
||||
* and [transactions](http://thecodebarbarian.com/a-node-js-perspective-on-mongodb-4-transactions.html).
|
||||
*
|
||||
* Calling `mongoose.startSession()` is equivalent to calling `mongoose.connection.startSession()`.
|
||||
* Sessions are scoped to a connection, so calling `mongoose.startSession()`
|
||||
* starts a session on the [default mongoose connection](/docs/api.html#mongoose_Mongoose-connection).
|
||||
*
|
||||
* @param {Object} [options] see the [mongodb driver options](http://mongodb.github.io/node-mongodb-native/3.0/api/MongoClient.html#startSession)
|
||||
* @param {Boolean} [options.causalConsistency=true] set to false to disable causal consistency
|
||||
* @param {Function} [callback]
|
||||
* @return {Promise<ClientSession>} promise that resolves to a MongoDB driver `ClientSession`
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mongoose.prototype.startSession = function() {
|
||||
return this.connection.startSession.apply(this.connection, arguments);
|
||||
};
|
||||
|
||||
/**
|
||||
* Getter/setter around function for pluralizing collection names.
|
||||
*
|
||||
* @param {Function|null} [fn] overwrites the function used to pluralize collection names
|
||||
* @return {Function|null} the current function used to pluralize collection names, defaults to the legacy function from `mongoose-legacy-pluralize`.
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mongoose.prototype.pluralize = function(fn) {
|
||||
if (arguments.length > 0) {
|
||||
this._pluralize = fn;
|
||||
}
|
||||
return this._pluralize;
|
||||
};
|
||||
|
||||
/**
|
||||
* Defines a model or retrieves it.
|
||||
*
|
||||
* Models defined on the `mongoose` instance are available to all connection
|
||||
* created by the same `mongoose` instance.
|
||||
*
|
||||
* If you call `mongoose.model()` with twice the same name but a different schema,
|
||||
* you will get an `OverwriteModelError`. If you call `mongoose.model()` with
|
||||
* the same name and same schema, you'll get the same schema back.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* var mongoose = require('mongoose');
|
||||
*
|
||||
* // define an Actor model with this mongoose instance
|
||||
* const Schema = new Schema({ name: String });
|
||||
* mongoose.model('Actor', schema);
|
||||
*
|
||||
* // create a new connection
|
||||
* var conn = mongoose.createConnection(..);
|
||||
*
|
||||
* // create Actor model
|
||||
* var Actor = conn.model('Actor', schema);
|
||||
* conn.model('Actor') === Actor; // true
|
||||
* conn.model('Actor', schema) === Actor; // true, same schema
|
||||
* conn.model('Actor', schema, 'actors') === Actor; // true, same schema and collection name
|
||||
*
|
||||
* // This throws an `OverwriteModelError` because the schema is different.
|
||||
* conn.model('Actor', new Schema({ name: String }));
|
||||
*
|
||||
* _When no `collection` argument is passed, Mongoose uses the model name. If you don't like this behavior, either pass a collection name, use `mongoose.pluralize()`, or set your schemas collection name option._
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* var schema = new Schema({ name: String }, { collection: 'actor' });
|
||||
*
|
||||
* // or
|
||||
*
|
||||
* schema.set('collection', 'actor');
|
||||
*
|
||||
* // or
|
||||
*
|
||||
* var collectionName = 'actor'
|
||||
* var M = mongoose.model('Actor', schema, collectionName)
|
||||
*
|
||||
* @param {String|Function} name model name or class extending Model
|
||||
* @param {Schema} [schema] the schema to use.
|
||||
* @param {String} [collection] name (optional, inferred from model name)
|
||||
* @param {Boolean} [skipInit] whether to skip initialization (defaults to false)
|
||||
* @return {Model} The model associated with `name`. Mongoose will create the model if it doesn't already exist.
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mongoose.prototype.model = function(name, schema, collection, skipInit) {
|
||||
const _mongoose = this instanceof Mongoose ? this : mongoose;
|
||||
|
||||
let model;
|
||||
if (typeof name === 'function') {
|
||||
model = name;
|
||||
name = model.name;
|
||||
if (!(model.prototype instanceof Model)) {
|
||||
throw new _mongoose.Error('The provided class ' + name + ' must extend Model');
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof schema === 'string') {
|
||||
collection = schema;
|
||||
schema = false;
|
||||
}
|
||||
|
||||
if (utils.isObject(schema) && !(schema.instanceOfSchema)) {
|
||||
schema = new Schema(schema);
|
||||
}
|
||||
if (schema && !schema.instanceOfSchema) {
|
||||
throw new Error('The 2nd parameter to `mongoose.model()` should be a ' +
|
||||
'schema or a POJO');
|
||||
}
|
||||
|
||||
if (typeof collection === 'boolean') {
|
||||
skipInit = collection;
|
||||
collection = null;
|
||||
}
|
||||
|
||||
// handle internal options from connection.model()
|
||||
let options;
|
||||
if (skipInit && utils.isObject(skipInit)) {
|
||||
options = skipInit;
|
||||
skipInit = true;
|
||||
} else {
|
||||
options = {};
|
||||
}
|
||||
|
||||
// look up schema for the collection.
|
||||
if (!_mongoose.modelSchemas[name]) {
|
||||
if (schema) {
|
||||
// cache it so we only apply plugins once
|
||||
_mongoose.modelSchemas[name] = schema;
|
||||
} else {
|
||||
throw new mongoose.Error.MissingSchemaError(name);
|
||||
}
|
||||
}
|
||||
|
||||
const originalSchema = schema;
|
||||
if (schema) {
|
||||
if (_mongoose.get('cloneSchemas')) {
|
||||
schema = schema.clone();
|
||||
}
|
||||
_mongoose._applyPlugins(schema);
|
||||
}
|
||||
|
||||
let sub;
|
||||
|
||||
// connection.model() may be passing a different schema for
|
||||
// an existing model name. in this case don't read from cache.
|
||||
if (_mongoose.models[name] && options.cache !== false) {
|
||||
if (originalSchema &&
|
||||
originalSchema.instanceOfSchema &&
|
||||
originalSchema !== _mongoose.models[name].schema) {
|
||||
throw new _mongoose.Error.OverwriteModelError(name);
|
||||
}
|
||||
|
||||
if (collection && collection !== _mongoose.models[name].collection.name) {
|
||||
// subclass current model with alternate collection
|
||||
model = _mongoose.models[name];
|
||||
schema = model.prototype.schema;
|
||||
sub = model.__subclass(_mongoose.connection, schema, collection);
|
||||
// do not cache the sub model
|
||||
return sub;
|
||||
}
|
||||
|
||||
return _mongoose.models[name];
|
||||
}
|
||||
|
||||
// ensure a schema exists
|
||||
if (!schema) {
|
||||
schema = this.modelSchemas[name];
|
||||
if (!schema) {
|
||||
throw new mongoose.Error.MissingSchemaError(name);
|
||||
}
|
||||
}
|
||||
|
||||
// Apply relevant "global" options to the schema
|
||||
if (!('pluralization' in schema.options)) {
|
||||
schema.options.pluralization = _mongoose.options.pluralization;
|
||||
}
|
||||
|
||||
if (!collection) {
|
||||
collection = schema.get('collection') ||
|
||||
utils.toCollectionName(name, _mongoose.pluralize());
|
||||
}
|
||||
|
||||
const connection = options.connection || _mongoose.connection;
|
||||
model = _mongoose.Model.compile(model || name, schema, collection, connection, _mongoose);
|
||||
|
||||
if (!skipInit) {
|
||||
// Errors handled internally, so safe to ignore error
|
||||
model.init(function $modelInitNoop() {});
|
||||
}
|
||||
|
||||
if (options.cache === false) {
|
||||
return model;
|
||||
}
|
||||
|
||||
_mongoose.models[name] = model;
|
||||
return _mongoose.models[name];
|
||||
};
|
||||
|
||||
/**
|
||||
* Removes the model named `name` from the default connection, if it exists.
|
||||
* You can use this function to clean up any models you created in your tests to
|
||||
* prevent OverwriteModelErrors.
|
||||
*
|
||||
* Equivalent to `mongoose.connection.deleteModel(name)`.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* mongoose.model('User', new Schema({ name: String }));
|
||||
* console.log(mongoose.model('User')); // Model object
|
||||
* mongoose.deleteModel('User');
|
||||
* console.log(mongoose.model('User')); // undefined
|
||||
*
|
||||
* // Usually useful in a Mocha `afterEach()` hook
|
||||
* afterEach(function() {
|
||||
* mongoose.deleteModel(/.+/); // Delete every model
|
||||
* });
|
||||
*
|
||||
* @api public
|
||||
* @param {String|RegExp} name if string, the name of the model to remove. If regexp, removes all models whose name matches the regexp.
|
||||
* @return {Mongoose} this
|
||||
*/
|
||||
|
||||
Mongoose.prototype.deleteModel = function(name) {
|
||||
this.connection.deleteModel(name);
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns an array of model names created on this instance of Mongoose.
|
||||
*
|
||||
* ####Note:
|
||||
*
|
||||
* _Does not include names of models created using `connection.model()`._
|
||||
*
|
||||
* @api public
|
||||
* @return {Array}
|
||||
*/
|
||||
|
||||
Mongoose.prototype.modelNames = function() {
|
||||
const names = Object.keys(this.models);
|
||||
return names;
|
||||
};
|
||||
|
||||
/**
|
||||
* Applies global plugins to `schema`.
|
||||
*
|
||||
* @param {Schema} schema
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Mongoose.prototype._applyPlugins = function(schema, options) {
|
||||
if (schema.$globalPluginsApplied) {
|
||||
return;
|
||||
}
|
||||
let i;
|
||||
let len;
|
||||
if (!options || !options.skipTopLevel) {
|
||||
for (i = 0, len = this.plugins.length; i < len; ++i) {
|
||||
schema.plugin(this.plugins[i][0], this.plugins[i][1]);
|
||||
}
|
||||
schema.$globalPluginsApplied = true;
|
||||
}
|
||||
for (i = 0, len = schema.childSchemas.length; i < len; ++i) {
|
||||
this._applyPlugins(schema.childSchemas[i].schema);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Declares a global plugin executed on all Schemas.
|
||||
*
|
||||
* Equivalent to calling `.plugin(fn)` on each Schema you create.
|
||||
*
|
||||
* @param {Function} fn plugin callback
|
||||
* @param {Object} [opts] optional options
|
||||
* @return {Mongoose} this
|
||||
* @see plugins ./plugins.html
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mongoose.prototype.plugin = function(fn, opts) {
|
||||
this.plugins.push([fn, opts]);
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* The default connection of the mongoose module.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* var mongoose = require('mongoose');
|
||||
* mongoose.connect(...);
|
||||
* mongoose.connection.on('error', cb);
|
||||
*
|
||||
* This is the connection used by default for every model created using [mongoose.model](#index_Mongoose-model).
|
||||
*
|
||||
* @memberOf Mongoose
|
||||
* @instance
|
||||
* @property connection
|
||||
* @return {Connection}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mongoose.prototype.__defineGetter__('connection', function() {
|
||||
return this.connections[0];
|
||||
});
|
||||
|
||||
Mongoose.prototype.__defineSetter__('connection', function(v) {
|
||||
if (v instanceof Connection) {
|
||||
this.connections[0] = v;
|
||||
this.models = v.models;
|
||||
}
|
||||
});
|
||||
|
||||
/*!
|
||||
* Driver dependent APIs
|
||||
*/
|
||||
|
||||
const driver = global.MONGOOSE_DRIVER_PATH || './drivers/node-mongodb-native';
|
||||
|
||||
/*!
|
||||
* Connection
|
||||
*/
|
||||
|
||||
const Connection = require(driver + '/connection');
|
||||
|
||||
/*!
|
||||
* Collection
|
||||
*/
|
||||
|
||||
const Collection = require(driver + '/collection');
|
||||
|
||||
/**
|
||||
* The Mongoose Aggregate constructor
|
||||
*
|
||||
* @method Aggregate
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mongoose.prototype.Aggregate = Aggregate;
|
||||
|
||||
/**
|
||||
* The Mongoose Collection constructor
|
||||
*
|
||||
* @method Collection
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mongoose.prototype.Collection = Collection;
|
||||
|
||||
/**
|
||||
* The Mongoose [Connection](#connection_Connection) constructor
|
||||
*
|
||||
* @memberOf Mongoose
|
||||
* @instance
|
||||
* @method Connection
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mongoose.prototype.Connection = Connection;
|
||||
|
||||
/**
|
||||
* The Mongoose version
|
||||
*
|
||||
* #### Example
|
||||
*
|
||||
* console.log(mongoose.version); // '5.x.x'
|
||||
*
|
||||
* @property version
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mongoose.prototype.version = pkg.version;
|
||||
|
||||
/**
|
||||
* The Mongoose constructor
|
||||
*
|
||||
* The exports of the mongoose module is an instance of this class.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* var mongoose = require('mongoose');
|
||||
* var mongoose2 = new mongoose.Mongoose();
|
||||
*
|
||||
* @method Mongoose
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mongoose.prototype.Mongoose = Mongoose;
|
||||
|
||||
/**
|
||||
* The Mongoose [Schema](#schema_Schema) constructor
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* var mongoose = require('mongoose');
|
||||
* var Schema = mongoose.Schema;
|
||||
* var CatSchema = new Schema(..);
|
||||
*
|
||||
* @method Schema
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mongoose.prototype.Schema = Schema;
|
||||
|
||||
/**
|
||||
* The Mongoose [SchemaType](#schematype_SchemaType) constructor
|
||||
*
|
||||
* @method SchemaType
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mongoose.prototype.SchemaType = SchemaType;
|
||||
|
||||
/**
|
||||
* The various Mongoose SchemaTypes.
|
||||
*
|
||||
* ####Note:
|
||||
*
|
||||
* _Alias of mongoose.Schema.Types for backwards compatibility._
|
||||
*
|
||||
* @property SchemaTypes
|
||||
* @see Schema.SchemaTypes #schema_Schema.Types
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mongoose.prototype.SchemaTypes = Schema.Types;
|
||||
|
||||
/**
|
||||
* The Mongoose [VirtualType](#virtualtype_VirtualType) constructor
|
||||
*
|
||||
* @method VirtualType
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mongoose.prototype.VirtualType = VirtualType;
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
|
||||
Mongoose.prototype.Types = Types;
|
||||
|
||||
/**
|
||||
* The Mongoose [Query](#query_Query) constructor.
|
||||
*
|
||||
* @method Query
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mongoose.prototype.Query = Query;
|
||||
|
||||
/**
|
||||
* The Mongoose [Promise](#promise_Promise) constructor.
|
||||
*
|
||||
* @memberOf Mongoose
|
||||
* @instance
|
||||
* @property Promise
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Object.defineProperty(Mongoose.prototype, 'Promise', {
|
||||
get: function() {
|
||||
return PromiseProvider.get();
|
||||
},
|
||||
set: function(lib) {
|
||||
PromiseProvider.set(lib);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Storage layer for mongoose promises
|
||||
*
|
||||
* @method PromiseProvider
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mongoose.prototype.PromiseProvider = PromiseProvider;
|
||||
|
||||
/**
|
||||
* The Mongoose [Model](#model_Model) constructor.
|
||||
*
|
||||
* @method Model
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mongoose.prototype.Model = Model;
|
||||
|
||||
/**
|
||||
* The Mongoose [Document](#document-js) constructor.
|
||||
*
|
||||
* @method Document
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mongoose.prototype.Document = Document;
|
||||
|
||||
/**
|
||||
* The Mongoose DocumentProvider constructor. Mongoose users should not have to
|
||||
* use this directly
|
||||
*
|
||||
* @method DocumentProvider
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mongoose.prototype.DocumentProvider = require('./document_provider');
|
||||
|
||||
/**
|
||||
* The Mongoose ObjectId [SchemaType](/docs/schematypes.html). Used for
|
||||
* declaring paths in your schema that should be
|
||||
* [MongoDB ObjectIds](https://docs.mongodb.com/manual/reference/method/ObjectId/).
|
||||
* Do not use this to create a new ObjectId instance, use `mongoose.Types.ObjectId`
|
||||
* instead.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* const childSchema = new Schema({ parentId: mongoose.ObjectId });
|
||||
*
|
||||
* @property ObjectId
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mongoose.prototype.ObjectId = SchemaTypes.ObjectId;
|
||||
|
||||
/**
|
||||
* The Mongoose Decimal128 [SchemaType](/docs/schematypes.html). Used for
|
||||
* declaring paths in your schema that should be
|
||||
* [128-bit decimal floating points](http://thecodebarbarian.com/a-nodejs-perspective-on-mongodb-34-decimal.html).
|
||||
* Do not use this to create a new Decimal128 instance, use `mongoose.Types.Decimal128`
|
||||
* instead.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* const vehicleSchema = new Schema({ fuelLevel: mongoose.Decimal128 });
|
||||
*
|
||||
* @property Decimal128
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mongoose.prototype.Decimal128 = SchemaTypes.Decimal128;
|
||||
|
||||
/**
|
||||
* The Mongoose Mixed [SchemaType](/docs/schematypes.html). Used for
|
||||
* declaring paths in your schema that Mongoose's change tracking, casting,
|
||||
* and validation should ignore.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* const schema = new Schema({ arbitrary: mongoose.Mixed });
|
||||
*
|
||||
* @property Mixed
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mongoose.prototype.Mixed = SchemaTypes.Mixed;
|
||||
|
||||
/**
|
||||
* The Mongoose Number [SchemaType](/docs/schematypes.html). Used for
|
||||
* declaring paths in your schema that Mongoose should cast to numbers.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* const schema = new Schema({ num: mongoose.Number });
|
||||
* // Equivalent to:
|
||||
* const schema = new Schema({ num: 'number' });
|
||||
*
|
||||
* @property Number
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mongoose.prototype.Number = SchemaTypes.Number;
|
||||
|
||||
/**
|
||||
* The [MongooseError](#error_MongooseError) constructor.
|
||||
*
|
||||
* @method Error
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mongoose.prototype.Error = require('./error');
|
||||
|
||||
/**
|
||||
* Mongoose uses this function to get the current time when setting
|
||||
* [timestamps](/docs/guide.html#timestamps). You may stub out this function
|
||||
* using a tool like [Sinon](https://www.npmjs.com/package/sinon) for testing.
|
||||
*
|
||||
* @method now
|
||||
* @returns Date the current time
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mongoose.prototype.now = function now() { return new Date(); };
|
||||
|
||||
/**
|
||||
* The Mongoose CastError constructor
|
||||
*
|
||||
* @method CastError
|
||||
* @param {String} type The name of the type
|
||||
* @param {Any} value The value that failed to cast
|
||||
* @param {String} path The path `a.b.c` in the doc where this cast error occurred
|
||||
* @param {Error} [reason] The original error that was thrown
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mongoose.prototype.CastError = require('./error/cast');
|
||||
|
||||
/**
|
||||
* The [node-mongodb-native](https://github.com/mongodb/node-mongodb-native) driver Mongoose uses.
|
||||
*
|
||||
* @property mongo
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mongoose.prototype.mongo = require('mongodb');
|
||||
|
||||
/**
|
||||
* The [mquery](https://github.com/aheckmann/mquery) query builder Mongoose uses.
|
||||
*
|
||||
* @property mquery
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mongoose.prototype.mquery = require('mquery');
|
||||
|
||||
/*!
|
||||
* The exports object is an instance of Mongoose.
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
|
||||
const mongoose = module.exports = exports = new Mongoose({
|
||||
[defaultMongooseSymbol]: true
|
||||
});
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
/*!
|
||||
* Dependencies
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const StateMachine = require('./statemachine');
|
||||
const ActiveRoster = StateMachine.ctor('require', 'modify', 'init', 'default', 'ignore');
|
||||
|
||||
module.exports = exports = InternalCache;
|
||||
|
||||
function InternalCache() {
|
||||
this.strictMode = undefined;
|
||||
this.selected = undefined;
|
||||
this.shardval = undefined;
|
||||
this.saveError = undefined;
|
||||
this.validationError = undefined;
|
||||
this.adhocPaths = undefined;
|
||||
this.removing = undefined;
|
||||
this.inserting = undefined;
|
||||
this.saving = undefined;
|
||||
this.version = undefined;
|
||||
this.getters = {};
|
||||
this._id = undefined;
|
||||
this.populate = undefined; // what we want to populate in this doc
|
||||
this.populated = undefined;// the _ids that have been populated
|
||||
this.wasPopulated = false; // if this doc was the result of a population
|
||||
this.scope = undefined;
|
||||
this.activePaths = new ActiveRoster;
|
||||
this.pathsToScopes = {};
|
||||
this.cachedRequired = {};
|
||||
this.session = null;
|
||||
|
||||
// embedded docs
|
||||
this.ownerDocument = undefined;
|
||||
this.fullPath = undefined;
|
||||
}
|
||||
+4657
File diff suppressed because it is too large
Load Diff
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user