initial
This commit is contained in:
+1
@@ -0,0 +1 @@
|
||||
repo_token: GZFmqKPy7XEX0uOl9TDZFUoOQ5AHADMkU
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"extends": ["eslint:recommended"],
|
||||
"env": {
|
||||
"node": true,
|
||||
"mocha": true
|
||||
},
|
||||
"ecmaFeatures": {
|
||||
"es6": true,
|
||||
},
|
||||
"plugins": [
|
||||
],
|
||||
"rules": {
|
||||
"no-console":0
|
||||
}
|
||||
}
|
||||
+1767
File diff suppressed because it is too large
Load Diff
+201
@@ -0,0 +1,201 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "{}"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright {yyyy} {name of copyright owner}
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
NODE = node
|
||||
NPM = npm
|
||||
JSDOC = jsdoc
|
||||
name = all
|
||||
|
||||
generate_docs:
|
||||
# cp -R ./HISTORY.md ./docs/content/meta/release-notes.md
|
||||
hugo -s docs/reference -d ../../public
|
||||
$(JSDOC) -c conf.json -t docs/jsdoc-template/ -d ./public/api
|
||||
cp -R ./public/api/scripts ./public/.
|
||||
cp -R ./public/api/styles ./public/.
|
||||
+415
@@ -0,0 +1,415 @@
|
||||
[](https://nodei.co/npm/mongodb/) [](https://nodei.co/npm/mongodb/)
|
||||
|
||||
[](http://travis-ci.org/mongodb/node-mongodb-native)
|
||||
[](https://coveralls.io/github/mongodb/node-mongodb-native?branch=2.1)
|
||||
[](https://gitter.im/mongodb/node-mongodb-native?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
|
||||
|
||||
# Description
|
||||
|
||||
The official [MongoDB](https://www.mongodb.com/) driver for Node.js. Provides a high-level API on top of [mongodb-core](https://www.npmjs.com/package/mongodb-core) that is meant for end users.
|
||||
|
||||
## MongoDB Node.JS Driver
|
||||
|
||||
| what | where |
|
||||
|---------------|------------------------------------------------|
|
||||
| documentation | http://mongodb.github.io/node-mongodb-native/ |
|
||||
| api-doc | http://mongodb.github.io/node-mongodb-native/2.2/api/ |
|
||||
| source | https://github.com/mongodb/node-mongodb-native |
|
||||
| mongodb | http://www.mongodb.org/ |
|
||||
|
||||
### Blogs of Engineers involved in the driver
|
||||
- Christian Kvalheim [@christkv](https://twitter.com/christkv) <http://christiankvalheim.com>
|
||||
|
||||
### Bugs / Feature Requests
|
||||
|
||||
Think you’ve found a bug? Want to see a new feature in node-mongodb-native? Please open a
|
||||
case in our issue management tool, JIRA:
|
||||
|
||||
- Create an account and login <https://jira.mongodb.org>.
|
||||
- Navigate to the NODE project <https://jira.mongodb.org/browse/NODE>.
|
||||
- Click **Create Issue** - Please provide as much information as possible about the issue type and how to reproduce it.
|
||||
|
||||
Bug reports in JIRA for all driver projects (i.e. NODE, PYTHON, CSHARP, JAVA) and the
|
||||
Core Server (i.e. SERVER) project are **public**.
|
||||
|
||||
### Questions and Bug Reports
|
||||
|
||||
* mailing list: https://groups.google.com/forum/#!forum/node-mongodb-native
|
||||
* jira: http://jira.mongodb.org/
|
||||
|
||||
### Change Log
|
||||
|
||||
http://jira.mongodb.org/browse/NODE
|
||||
|
||||
# Installation
|
||||
|
||||
The recommended way to get started using the Node.js 2.0 driver is by using the `NPM` (Node Package Manager) to install the dependency in your project.
|
||||
|
||||
## MongoDB Driver
|
||||
|
||||
Given that you have created your own project using `npm init` we install the mongodb driver and it's dependencies by executing the following `NPM` command.
|
||||
|
||||
```
|
||||
npm install mongodb --save
|
||||
```
|
||||
|
||||
This will download the MongoDB driver and add a dependency entry in your `package.json` file.
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
The MongoDB driver depends on several other packages. These are.
|
||||
|
||||
* mongodb-core
|
||||
* bson
|
||||
* kerberos
|
||||
* node-gyp
|
||||
|
||||
The `kerberos` package is a C++ extension that requires a build environment to be installed on your system. You must be able to build node.js itself to be able to compile and install the `kerberos` module. Furthermore the `kerberos` module requires the MIT Kerberos package to correctly compile on UNIX operating systems. Consult your UNIX operation system package manager what libraries to install.
|
||||
|
||||
{{% note class="important" %}}
|
||||
Windows already contains the SSPI API used for Kerberos authentication. However you will need to install a full compiler tool chain using visual studio C++ to correctly install the kerberos extension.
|
||||
{{% /note %}}
|
||||
|
||||
### Diagnosing on UNIX
|
||||
|
||||
If you don’t have the build essentials it won’t build. In the case of linux you will need gcc and g++, node.js with all the headers and python. The easiest way to figure out what’s missing is by trying to build the kerberos project. You can do this by performing the following steps.
|
||||
|
||||
```
|
||||
git clone https://github.com/christkv/kerberos.git
|
||||
cd kerberos
|
||||
npm install
|
||||
```
|
||||
|
||||
If all the steps complete you have the right toolchain installed. If you get node-gyp not found you need to install it globally by doing.
|
||||
|
||||
```
|
||||
npm install -g node-gyp
|
||||
```
|
||||
|
||||
If correctly compiles and runs the tests you are golden. We can now try to install the mongod driver by performing the following command.
|
||||
|
||||
```
|
||||
cd yourproject
|
||||
npm install mongodb --save
|
||||
```
|
||||
|
||||
If it still fails the next step is to examine the npm log. Rerun the command but in this case in verbose mode.
|
||||
|
||||
```
|
||||
npm --loglevel verbose install mongodb
|
||||
```
|
||||
|
||||
This will print out all the steps npm is performing while trying to install the module.
|
||||
|
||||
### Diagnosing on Windows
|
||||
|
||||
A known compiler tool chain known to work for compiling `kerberos` on windows is the following.
|
||||
|
||||
* Visual Studio c++ 2010 (do not use higher versions)
|
||||
* Windows 7 64bit SDK
|
||||
* Python 2.7 or higher
|
||||
|
||||
Open visual studio command prompt. Ensure node.exe is in your path and install node-gyp.
|
||||
|
||||
```
|
||||
npm install -g node-gyp
|
||||
```
|
||||
|
||||
Next you will have to build the project manually to test it. Use any tool you use with git and grab the repo.
|
||||
|
||||
```
|
||||
git clone https://github.com/christkv/kerberos.git
|
||||
cd kerberos
|
||||
npm install
|
||||
node-gyp rebuild
|
||||
```
|
||||
|
||||
This should rebuild the driver successfully if you have everything set up correctly.
|
||||
|
||||
### Other possible issues
|
||||
|
||||
Your python installation might be hosed making gyp break. I always recommend that you test your deployment environment first by trying to build node itself on the server in question as this should unearth any issues with broken packages (and there are a lot of broken packages out there).
|
||||
|
||||
Another thing is to ensure your user has write permission to wherever the node modules are being installed.
|
||||
|
||||
QuickStart
|
||||
==========
|
||||
The quick start guide will show you how to setup a simple application using node.js and MongoDB. Its scope is only how to set up the driver and perform the simple crud operations. For more in depth coverage we encourage reading the tutorials.
|
||||
|
||||
Create the package.json file
|
||||
----------------------------
|
||||
Let's create a directory where our application will live. In our case we will put this under our projects directory.
|
||||
|
||||
```
|
||||
mkdir myproject
|
||||
cd myproject
|
||||
```
|
||||
|
||||
Enter the following command and answer the questions to create the initial structure for your new project
|
||||
|
||||
```
|
||||
npm init
|
||||
```
|
||||
|
||||
Next we need to edit the generated package.json file to add the dependency for the MongoDB driver. The package.json file below is just an example and your will look different depending on how you answered the questions after entering `npm init`
|
||||
|
||||
```
|
||||
{
|
||||
"name": "myproject",
|
||||
"version": "1.0.0",
|
||||
"description": "My first project",
|
||||
"main": "index.js",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/christkv/myfirstproject.git"
|
||||
},
|
||||
"dependencies": {
|
||||
"mongodb": "~2.0"
|
||||
},
|
||||
"author": "Christian Kvalheim",
|
||||
"license": "Apache 2.0",
|
||||
"bugs": {
|
||||
"url": "https://github.com/christkv/myfirstproject/issues"
|
||||
},
|
||||
"homepage": "https://github.com/christkv/myfirstproject"
|
||||
}
|
||||
```
|
||||
|
||||
Save the file and return to the shell or command prompt and use **NPM** to install all the dependencies.
|
||||
|
||||
```
|
||||
npm install
|
||||
```
|
||||
|
||||
You should see **NPM** download a lot of files. Once it's done you'll find all the downloaded packages under the **node_modules** directory.
|
||||
|
||||
Booting up a MongoDB Server
|
||||
---------------------------
|
||||
Let's boot up a MongoDB server instance. Download the right MongoDB version from [MongoDB](http://www.mongodb.org), open a new shell or command line and ensure the **mongod** command is in the shell or command line path. Now let's create a database directory (in our case under **/data**).
|
||||
|
||||
```
|
||||
mongod --dbpath=/data --port 27017
|
||||
```
|
||||
|
||||
You should see the **mongod** process start up and print some status information.
|
||||
|
||||
Connecting to MongoDB
|
||||
---------------------
|
||||
Let's create a new **app.js** file that we will use to show the basic CRUD operations using the MongoDB driver.
|
||||
|
||||
First let's add code to connect to the server and the database **myproject**.
|
||||
|
||||
```js
|
||||
var MongoClient = require('mongodb').MongoClient
|
||||
, assert = require('assert');
|
||||
|
||||
// Connection URL
|
||||
var url = 'mongodb://localhost:27017/myproject';
|
||||
// Use connect method to connect to the Server
|
||||
MongoClient.connect(url, function(err, db) {
|
||||
assert.equal(null, err);
|
||||
console.log("Connected correctly to server");
|
||||
|
||||
db.close();
|
||||
});
|
||||
```
|
||||
|
||||
Given that you booted up the **mongod** process earlier the application should connect successfully and print **Connected correctly to server** to the console.
|
||||
|
||||
Let's Add some code to show the different CRUD operations available.
|
||||
|
||||
Inserting a Document
|
||||
--------------------
|
||||
Let's create a function that will insert some documents for us.
|
||||
|
||||
```js
|
||||
var insertDocuments = function(db, callback) {
|
||||
// Get the documents collection
|
||||
var collection = db.collection('documents');
|
||||
// Insert some documents
|
||||
collection.insertMany([
|
||||
{a : 1}, {a : 2}, {a : 3}
|
||||
], function(err, result) {
|
||||
assert.equal(err, null);
|
||||
assert.equal(3, result.result.n);
|
||||
assert.equal(3, result.ops.length);
|
||||
console.log("Inserted 3 documents into the document collection");
|
||||
callback(result);
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
The insert command will return a results object that contains several fields that might be useful.
|
||||
|
||||
* **result** Contains the result document from MongoDB
|
||||
* **ops** Contains the documents inserted with added **_id** fields
|
||||
* **connection** Contains the connection used to perform the insert
|
||||
|
||||
Let's add call the **insertDocuments** command to the **MongoClient.connect** method callback.
|
||||
|
||||
```js
|
||||
var MongoClient = require('mongodb').MongoClient
|
||||
, assert = require('assert');
|
||||
|
||||
// Connection URL
|
||||
var url = 'mongodb://localhost:27017/myproject';
|
||||
// Use connect method to connect to the Server
|
||||
MongoClient.connect(url, function(err, db) {
|
||||
assert.equal(null, err);
|
||||
console.log("Connected correctly to server");
|
||||
|
||||
insertDocuments(db, function() {
|
||||
db.close();
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
We can now run the update **app.js** file.
|
||||
|
||||
```
|
||||
node app.js
|
||||
```
|
||||
|
||||
You should see the following output after running the **app.js** file.
|
||||
|
||||
```
|
||||
Connected correctly to server
|
||||
Inserted 3 documents into the document collection
|
||||
```
|
||||
|
||||
Updating a document
|
||||
-------------------
|
||||
Let's look at how to do a simple document update by adding a new field **b** to the document that has the field **a** set to **2**.
|
||||
|
||||
```js
|
||||
var updateDocument = function(db, callback) {
|
||||
// Get the documents collection
|
||||
var collection = db.collection('documents');
|
||||
// Update document where a is 2, set b equal to 1
|
||||
collection.updateOne({ a : 2 }
|
||||
, { $set: { b : 1 } }, function(err, result) {
|
||||
assert.equal(err, null);
|
||||
assert.equal(1, result.result.n);
|
||||
console.log("Updated the document with the field a equal to 2");
|
||||
callback(result);
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
The method will update the first document where the field **a** is equal to **2** by adding a new field **b** to the document set to **1**. Let's update the callback function from **MongoClient.connect** to include the update method.
|
||||
|
||||
```js
|
||||
var MongoClient = require('mongodb').MongoClient
|
||||
, assert = require('assert');
|
||||
|
||||
// Connection URL
|
||||
var url = 'mongodb://localhost:27017/myproject';
|
||||
// Use connect method to connect to the Server
|
||||
MongoClient.connect(url, function(err, db) {
|
||||
assert.equal(null, err);
|
||||
console.log("Connected correctly to server");
|
||||
|
||||
insertDocuments(db, function() {
|
||||
updateDocument(db, function() {
|
||||
db.close();
|
||||
});
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
Delete a document
|
||||
-----------------
|
||||
Next lets delete the document where the field **a** equals to **3**.
|
||||
|
||||
```js
|
||||
var deleteDocument = function(db, callback) {
|
||||
// Get the documents collection
|
||||
var collection = db.collection('documents');
|
||||
// Delete document where a is 3
|
||||
collection.deleteOne({ a : 3 }, function(err, result) {
|
||||
assert.equal(err, null);
|
||||
assert.equal(1, result.result.n);
|
||||
console.log("Removed the document with the field a equal to 3");
|
||||
callback(result);
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
This will delete the first document where the field **a** equals to **3**. Let's add the method to the **MongoClient
|
||||
.connect** callback function.
|
||||
|
||||
```js
|
||||
var MongoClient = require('mongodb').MongoClient
|
||||
, assert = require('assert');
|
||||
|
||||
// Connection URL
|
||||
var url = 'mongodb://localhost:27017/myproject';
|
||||
// Use connect method to connect to the Server
|
||||
MongoClient.connect(url, function(err, db) {
|
||||
assert.equal(null, err);
|
||||
console.log("Connected correctly to server");
|
||||
|
||||
insertDocuments(db, function() {
|
||||
updateDocument(db, function() {
|
||||
deleteDocument(db, function() {
|
||||
db.close();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
Finally let's retrieve all the documents using a simple find.
|
||||
|
||||
Find All Documents
|
||||
------------------
|
||||
We will finish up the Quickstart CRUD methods by performing a simple query that returns all the documents matching the query.
|
||||
|
||||
```js
|
||||
var findDocuments = function(db, callback) {
|
||||
// Get the documents collection
|
||||
var collection = db.collection('documents');
|
||||
// Find some documents
|
||||
collection.find({}).toArray(function(err, docs) {
|
||||
assert.equal(err, null);
|
||||
assert.equal(2, docs.length);
|
||||
console.log("Found the following records");
|
||||
console.dir(docs);
|
||||
callback(docs);
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
This query will return all the documents in the **documents** collection. Since we deleted a document the total
|
||||
documents returned is **2**. Finally let's add the findDocument method to the **MongoClient.connect** callback.
|
||||
|
||||
```js
|
||||
var MongoClient = require('mongodb').MongoClient
|
||||
, assert = require('assert');
|
||||
|
||||
// Connection URL
|
||||
var url = 'mongodb://localhost:27017/myproject';
|
||||
// Use connect method to connect to the Server
|
||||
MongoClient.connect(url, function(err, db) {
|
||||
assert.equal(null, err);
|
||||
console.log("Connected correctly to server");
|
||||
|
||||
insertDocuments(db, function() {
|
||||
updateDocument(db, function() {
|
||||
deleteDocument(db, function() {
|
||||
findDocuments(db, function() {
|
||||
db.close();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
This concludes the QuickStart of connecting and performing some Basic operations using the MongoDB Node.js driver. For more detailed information you can look at the tutorials covering more specific topics of interest.
|
||||
|
||||
## Next Steps
|
||||
|
||||
* [MongoDB Documentation](http://mongodb.org/)
|
||||
* [Read about Schemas](http://learnmongodbthehardway.com/)
|
||||
* [Star us on GitHub](https://github.com/mongodb/node-mongodb-native)
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
Mongodb-core uses third-party libraries or other resources that may
|
||||
be distributed under licenses different than the Mongodb-core software.
|
||||
|
||||
In the event that we accidentally failed to list a required notice,
|
||||
please bring it to our attention through any of the ways detailed here:
|
||||
|
||||
mongodb-dev@googlegroups.com
|
||||
|
||||
The attached notices are provided for information only.
|
||||
|
||||
For any licenses that require disclosure of source, sources are available at
|
||||
https://github.com/mongodb/node-mongodb-native.
|
||||
|
||||
1) License Notice for nan.h
|
||||
---------------------------
|
||||
|
||||
The MIT License (MIT)
|
||||
=====================
|
||||
|
||||
Copyright (c) 2016 NAN contributors
|
||||
-----------------------------------
|
||||
|
||||
NAN contributors listed at <https://github.com/nodejs/nan#contributors>
|
||||
|
||||
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.
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
var ReplSetManager = require('mongodb-topology-manager').ReplSet,
|
||||
f = require('util').format;
|
||||
|
||||
var rsOptions = {
|
||||
server: {
|
||||
keyFile: __dirname + '/test/functional/data/keyfile.txt',
|
||||
auth: null,
|
||||
replSet: 'rs'
|
||||
},
|
||||
client: {
|
||||
replSet: 'rs'
|
||||
}
|
||||
}
|
||||
|
||||
// Set up the nodes
|
||||
var nodes = [{
|
||||
options: {
|
||||
bind_ip: 'localhost', port: 31000,
|
||||
dbpath: f('%s/../db/31000', __dirname),
|
||||
}
|
||||
}, {
|
||||
options: {
|
||||
bind_ip: 'localhost', port: 31001,
|
||||
dbpath: f('%s/../db/31001', __dirname),
|
||||
}
|
||||
}, {
|
||||
options: {
|
||||
bind_ip: 'localhost', port: 31002,
|
||||
dbpath: f('%s/../db/31002', __dirname),
|
||||
}
|
||||
}]
|
||||
|
||||
// Merge in any node start up options
|
||||
for(var i = 0; i < nodes.length; i++) {
|
||||
for(var name in rsOptions.server) {
|
||||
nodes[i].options[name] = rsOptions.server[name];
|
||||
}
|
||||
}
|
||||
|
||||
// Create a manager
|
||||
var replicasetManager = new ReplSetManager('mongod', nodes, rsOptions.client);
|
||||
// Purge the set
|
||||
replicasetManager.purge().then(function() {
|
||||
// Start the server
|
||||
replicasetManager.start().then(function() {
|
||||
process.exit(0);
|
||||
}).catch(function(e) {
|
||||
console.log("====== ")
|
||||
console.dir(e)
|
||||
// // console.dir(e);
|
||||
});
|
||||
});
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
{
|
||||
"plugins": ["plugins/markdown", "docs/lib/jsdoc/examples_plugin.js"],
|
||||
"source": {
|
||||
"include": [
|
||||
"test/functional/operation_example_tests.js",
|
||||
"test/functional/operation_promises_example_tests.js",
|
||||
"test/functional/operation_generators_example_tests.js",
|
||||
"test/functional/authentication_tests.js",
|
||||
"test/functional/gridfs_stream_tests.js",
|
||||
"lib/admin.js",
|
||||
"lib/collection.js",
|
||||
"lib/cursor.js",
|
||||
"lib/aggregation_cursor.js",
|
||||
"lib/command_cursor.js",
|
||||
"lib/db.js",
|
||||
"lib/mongo_client.js",
|
||||
"lib/mongos.js",
|
||||
"lib/read_preference.js",
|
||||
"lib/replset.js",
|
||||
"lib/server.js",
|
||||
"lib/bulk/common.js",
|
||||
"lib/bulk/ordered.js",
|
||||
"lib/bulk/unordered.js",
|
||||
"lib/gridfs/grid_store.js",
|
||||
"node_modules/mongodb-core/lib/error.js",
|
||||
"lib/gridfs-stream/index.js",
|
||||
"lib/gridfs-stream/download.js",
|
||||
"lib/gridfs-stream/upload.js",
|
||||
"node_modules/mongodb-core/lib/connection/logger.js",
|
||||
"node_modules/bson/lib/bson/binary.js",
|
||||
"node_modules/bson/lib/bson/code.js",
|
||||
"node_modules/bson/lib/bson/db_ref.js",
|
||||
"node_modules/bson/lib/bson/double.js",
|
||||
"node_modules/bson/lib/bson/long.js",
|
||||
"node_modules/bson/lib/bson/objectid.js",
|
||||
"node_modules/bson/lib/bson/symbol.js",
|
||||
"node_modules/bson/lib/bson/timestamp.js",
|
||||
"node_modules/bson/lib/bson/max_key.js",
|
||||
"node_modules/bson/lib/bson/min_key.js",
|
||||
"node_modules/bson/lib/bson/decimal128.js",
|
||||
"node_modules/bson/lib/bson/int_32.js",
|
||||
"node_modules/bson/lib/bson/regexp.js"
|
||||
]
|
||||
},
|
||||
"templates": {
|
||||
"cleverLinks": true,
|
||||
"monospaceLinks": true,
|
||||
"default": {
|
||||
"outputSourceFiles" : true
|
||||
},
|
||||
"applicationName": "Node.js MongoDB Driver API",
|
||||
"disqus": true,
|
||||
"googleAnalytics": "UA-29229787-1",
|
||||
"openGraph": {
|
||||
"title": "",
|
||||
"type": "website",
|
||||
"image": "",
|
||||
"site_name": "",
|
||||
"url": ""
|
||||
},
|
||||
"meta": {
|
||||
"title": "",
|
||||
"description": "",
|
||||
"keyword": ""
|
||||
},
|
||||
"linenums": true
|
||||
},
|
||||
"markdown": {
|
||||
"parser": "gfm",
|
||||
"hardwrap": true,
|
||||
"tags": ["examples"]
|
||||
},
|
||||
"examples": {
|
||||
"indent": 4
|
||||
}
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
// Core module
|
||||
var core = require('mongodb-core'),
|
||||
Instrumentation = require('./lib/apm');
|
||||
|
||||
// Set up the connect function
|
||||
var connect = require('./lib/mongo_client').connect;
|
||||
|
||||
// Expose error class
|
||||
connect.MongoError = core.MongoError;
|
||||
|
||||
// Actual driver classes exported
|
||||
connect.Admin = require('./lib/admin');
|
||||
connect.MongoClient = require('./lib/mongo_client');
|
||||
connect.Db = require('./lib/db');
|
||||
connect.Collection = require('./lib/collection');
|
||||
connect.Server = require('./lib/server');
|
||||
connect.ReplSet = require('./lib/replset');
|
||||
connect.Mongos = require('./lib/mongos');
|
||||
connect.ReadPreference = require('./lib/read_preference');
|
||||
connect.GridStore = require('./lib/gridfs/grid_store');
|
||||
connect.Chunk = require('./lib/gridfs/chunk');
|
||||
connect.Logger = core.Logger;
|
||||
connect.Cursor = require('./lib/cursor');
|
||||
connect.GridFSBucket = require('./lib/gridfs-stream');
|
||||
// Exported to be used in tests not to be used anywhere else
|
||||
connect.CoreServer = require('mongodb-core').Server;
|
||||
connect.CoreConnection = require('mongodb-core').Connection;
|
||||
|
||||
// BSON types exported
|
||||
connect.Binary = core.BSON.Binary;
|
||||
connect.Code = core.BSON.Code;
|
||||
connect.Map = core.BSON.Map;
|
||||
connect.DBRef = core.BSON.DBRef;
|
||||
connect.Double = core.BSON.Double;
|
||||
connect.Int32 = core.BSON.Int32;
|
||||
connect.Long = core.BSON.Long;
|
||||
connect.MinKey = core.BSON.MinKey;
|
||||
connect.MaxKey = core.BSON.MaxKey;
|
||||
connect.ObjectID = core.BSON.ObjectID;
|
||||
connect.ObjectId = core.BSON.ObjectID;
|
||||
connect.Symbol = core.BSON.Symbol;
|
||||
connect.Timestamp = core.BSON.Timestamp;
|
||||
connect.BSONRegExp = core.BSON.BSONRegExp;
|
||||
connect.Decimal128 = core.BSON.Decimal128;
|
||||
|
||||
// Add connect method
|
||||
connect.connect = connect;
|
||||
|
||||
// Set up the instrumentation method
|
||||
connect.instrument = function(options, callback) {
|
||||
if(typeof options == 'function') callback = options, options = {};
|
||||
return new Instrumentation(core, options, callback);
|
||||
}
|
||||
|
||||
// Set our exports to be the connect function
|
||||
module.exports = connect;
|
||||
+231
@@ -0,0 +1,231 @@
|
||||
var MongoClient = require('./').MongoClient,
|
||||
assert = require('assert');
|
||||
|
||||
// var memwatch = require('memwatch-next');
|
||||
// memwatch.on('leak', function(info) {
|
||||
// console.log("======== leak")
|
||||
// });
|
||||
//
|
||||
// memwatch.on('stats', function(stats) {
|
||||
// console.log("======== stats")
|
||||
// console.dir(stats)
|
||||
// });
|
||||
|
||||
// // Take first snapshot
|
||||
// var hd = new memwatch.HeapDiff();
|
||||
|
||||
MongoClient.connect('mongodb://localhost:27017/bench', function(err, db) {
|
||||
var docs = [];
|
||||
var total = 1000;
|
||||
var count = total;
|
||||
var measurements = [];
|
||||
|
||||
// Insert a bunch of documents
|
||||
for(var i = 0; i < 100; i++) {
|
||||
docs.push(JSON.parse(data));
|
||||
}
|
||||
|
||||
var col = db.collection('inserts');
|
||||
|
||||
function execute(col, callback) {
|
||||
var start = new Date().getTime();
|
||||
|
||||
col.find({}).limit(100).toArray(function(e, docs) {
|
||||
measurements.push(new Date().getTime() - start);
|
||||
assert.equal(null, e);
|
||||
callback();
|
||||
});
|
||||
}
|
||||
|
||||
console.log("== insert documents")
|
||||
col.insert(docs, function(e, r) {
|
||||
docs = [];
|
||||
assert.equal(null, e);
|
||||
|
||||
console.log("== start bench")
|
||||
for(var i = 0; i < total; i++) {
|
||||
execute(col, function(e) {
|
||||
count = count - 1;
|
||||
|
||||
if(count == 0) {
|
||||
// Calculate total execution time for operations
|
||||
var totalTime = measurements.reduce(function(prev, curr) {
|
||||
return prev + curr;
|
||||
}, 0);
|
||||
|
||||
console.log("===========================================");
|
||||
console.log("total time: " + totalTime)
|
||||
|
||||
// var diff = hd.end();
|
||||
// console.log("===========================================");
|
||||
// console.log(JSON.stringify(diff, null, 2))
|
||||
|
||||
db.close();
|
||||
process.exit(0)
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
var data = JSON.stringify({
|
||||
"data": [
|
||||
{
|
||||
"_id": 1,
|
||||
"x": 11
|
||||
},
|
||||
{
|
||||
"_id": 2,
|
||||
"x": 22
|
||||
},
|
||||
{
|
||||
"_id": 3,
|
||||
"x": 33
|
||||
}
|
||||
],
|
||||
"collection_name": "test",
|
||||
"database_name": "command-monitoring-tests",
|
||||
"tests": [
|
||||
{
|
||||
"description": "A successful mixed bulk write",
|
||||
"operation": {
|
||||
"name": "bulkWrite",
|
||||
"arguments": {
|
||||
"requests": [
|
||||
{
|
||||
"insertOne": {
|
||||
"document": {
|
||||
"_id": 4,
|
||||
"x": 44
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"updateOne": {
|
||||
"filter": {
|
||||
"_id": 3
|
||||
},
|
||||
"update": {
|
||||
"set": {
|
||||
"x": 333
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"expectations": [
|
||||
{
|
||||
"command_started_event": {
|
||||
"command": {
|
||||
"insert": "test",
|
||||
"documents": [
|
||||
{
|
||||
"_id": 4,
|
||||
"x": 44
|
||||
}
|
||||
],
|
||||
"ordered": true
|
||||
},
|
||||
"command_name": "insert",
|
||||
"database_name": "command-monitoring-tests"
|
||||
}
|
||||
},
|
||||
{
|
||||
"command_succeeded_event": {
|
||||
"reply": {
|
||||
"ok": 1.0,
|
||||
"n": 1
|
||||
},
|
||||
"command_name": "insert"
|
||||
}
|
||||
},
|
||||
{
|
||||
"command_started_event": {
|
||||
"command": {
|
||||
"update": "test",
|
||||
"updates": [
|
||||
{
|
||||
"q": {
|
||||
"_id": 3
|
||||
},
|
||||
"u": {
|
||||
"set": {
|
||||
"x": 333
|
||||
}
|
||||
},
|
||||
"upsert": false,
|
||||
"multi": false
|
||||
}
|
||||
],
|
||||
"ordered": true
|
||||
},
|
||||
"command_name": "update",
|
||||
"database_name": "command-monitoring-tests"
|
||||
}
|
||||
},
|
||||
{
|
||||
"command_succeeded_event": {
|
||||
"reply": {
|
||||
"ok": 1.0,
|
||||
"n": 1
|
||||
},
|
||||
"command_name": "update"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "A successful unordered bulk write with an unacknowledged write concern",
|
||||
"operation": {
|
||||
"name": "bulkWrite",
|
||||
"arguments": {
|
||||
"requests": [
|
||||
{
|
||||
"insertOne": {
|
||||
"document": {
|
||||
"_id": 4,
|
||||
"x": 44
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"ordered": false,
|
||||
"writeConcern": {
|
||||
"w": 0
|
||||
}
|
||||
}
|
||||
},
|
||||
"expectations": [
|
||||
{
|
||||
"command_started_event": {
|
||||
"command": {
|
||||
"insert": "test",
|
||||
"documents": [
|
||||
{
|
||||
"_id": 4,
|
||||
"x": 44
|
||||
}
|
||||
],
|
||||
"ordered": false,
|
||||
"writeConcern": {
|
||||
"w": 0
|
||||
}
|
||||
},
|
||||
"command_name": "insert",
|
||||
"database_name": "command-monitoring-tests"
|
||||
}
|
||||
},
|
||||
{
|
||||
"command_succeeded_event": {
|
||||
"reply": {
|
||||
"ok": 1.0
|
||||
},
|
||||
"command_name": "insert"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
});
|
||||
+579
@@ -0,0 +1,579 @@
|
||||
"use strict";
|
||||
|
||||
var toError = require('./utils').toError,
|
||||
Define = require('./metadata'),
|
||||
shallowClone = require('./utils').shallowClone,
|
||||
assign = require('./utils').assign,
|
||||
authenticate = require('./authenticate');
|
||||
|
||||
/**
|
||||
* @fileOverview The **Admin** class is an internal class that allows convenient access to
|
||||
* the admin functionality and commands for MongoDB.
|
||||
*
|
||||
* **ADMIN Cannot directly be instantiated**
|
||||
* @example
|
||||
* var MongoClient = require('mongodb').MongoClient,
|
||||
* test = require('assert');
|
||||
* // Connection url
|
||||
* var url = 'mongodb://localhost:27017/test';
|
||||
* // Connect using MongoClient
|
||||
* MongoClient.connect(url, function(err, db) {
|
||||
* // Use the admin database for the operation
|
||||
* var adminDb = db.admin();
|
||||
*
|
||||
* // List all the available databases
|
||||
* adminDb.listDatabases(function(err, dbs) {
|
||||
* test.equal(null, err);
|
||||
* test.ok(dbs.databases.length > 0);
|
||||
* db.close();
|
||||
* });
|
||||
* });
|
||||
*/
|
||||
|
||||
/**
|
||||
* Create a new Admin instance (INTERNAL TYPE, do not instantiate directly)
|
||||
* @class
|
||||
* @return {Admin} a collection instance.
|
||||
*/
|
||||
var Admin = function(db, topology, promiseLibrary) {
|
||||
if(!(this instanceof Admin)) return new Admin(db, topology);
|
||||
|
||||
// Internal state
|
||||
this.s = {
|
||||
db: db
|
||||
, topology: topology
|
||||
, promiseLibrary: promiseLibrary
|
||||
}
|
||||
}
|
||||
|
||||
var define = Admin.define = new Define('Admin', Admin, false);
|
||||
|
||||
/**
|
||||
* The callback format for results
|
||||
* @callback Admin~resultCallback
|
||||
* @param {MongoError} error An error instance representing the error during the execution.
|
||||
* @param {object} result The result object if the command was executed successfully.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Execute a command
|
||||
* @method
|
||||
* @param {object} command The command hash
|
||||
* @param {object} [options=null] Optional settings.
|
||||
* @param {(ReadPreference|string)} [options.readPreference=null] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
|
||||
* @param {number} [options.maxTimeMS=null] Number of milliseconds to wait before aborting the query.
|
||||
* @param {Admin~resultCallback} [callback] The command result callback
|
||||
* @return {Promise} returns Promise if no callback passed
|
||||
*/
|
||||
Admin.prototype.command = function(command, options, callback) {
|
||||
var self = this;
|
||||
var args = Array.prototype.slice.call(arguments, 1);
|
||||
callback = args.pop();
|
||||
if(typeof callback != 'function') args.push(callback);
|
||||
options = args.length ? args.shift() : {};
|
||||
|
||||
// Execute using callback
|
||||
if(typeof callback == 'function') return this.s.db.executeDbAdminCommand(command, options, function(err, doc) {
|
||||
return callback != null ? callback(err, doc) : null;
|
||||
});
|
||||
|
||||
// Return a Promise
|
||||
return new this.s.promiseLibrary(function(resolve, reject) {
|
||||
self.s.db.executeDbAdminCommand(command, options, function(err, doc) {
|
||||
if(err) return reject(err);
|
||||
resolve(doc);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
define.classMethod('command', {callback: true, promise:true});
|
||||
|
||||
/**
|
||||
* Retrieve the server information for the current
|
||||
* instance of the db client
|
||||
*
|
||||
* @param {Admin~resultCallback} [callback] The command result callback
|
||||
* @return {Promise} returns Promise if no callback passed
|
||||
*/
|
||||
Admin.prototype.buildInfo = function(callback) {
|
||||
var self = this;
|
||||
// Execute using callback
|
||||
if(typeof callback == 'function') return this.serverInfo(callback);
|
||||
|
||||
// Return a Promise
|
||||
return new this.s.promiseLibrary(function(resolve, reject) {
|
||||
self.serverInfo(function(err, r) {
|
||||
if(err) return reject(err);
|
||||
resolve(r);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
define.classMethod('buildInfo', {callback: true, promise:true});
|
||||
|
||||
/**
|
||||
* Retrieve the server information for the current
|
||||
* instance of the db client
|
||||
*
|
||||
* @param {Admin~resultCallback} [callback] The command result callback
|
||||
* @return {Promise} returns Promise if no callback passed
|
||||
*/
|
||||
Admin.prototype.serverInfo = function(callback) {
|
||||
var self = this;
|
||||
// Execute using callback
|
||||
if(typeof callback == 'function') return this.s.db.executeDbAdminCommand({buildinfo:1}, function(err, doc) {
|
||||
if(err != null) return callback(err, null);
|
||||
callback(null, doc);
|
||||
});
|
||||
|
||||
// Return a Promise
|
||||
return new this.s.promiseLibrary(function(resolve, reject) {
|
||||
self.s.db.executeDbAdminCommand({buildinfo:1}, function(err, doc) {
|
||||
if(err) return reject(err);
|
||||
resolve(doc);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
define.classMethod('serverInfo', {callback: true, promise:true});
|
||||
|
||||
/**
|
||||
* Retrieve this db's server status.
|
||||
*
|
||||
* @param {Admin~resultCallback} [callback] The command result callback
|
||||
* @return {Promise} returns Promise if no callback passed
|
||||
*/
|
||||
Admin.prototype.serverStatus = function(callback) {
|
||||
var self = this;
|
||||
|
||||
// Execute using callback
|
||||
if(typeof callback == 'function') return serverStatus(self, callback)
|
||||
|
||||
// Return a Promise
|
||||
return new this.s.promiseLibrary(function(resolve, reject) {
|
||||
serverStatus(self, function(err, r) {
|
||||
if(err) return reject(err);
|
||||
resolve(r);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
var serverStatus = function(self, callback) {
|
||||
self.s.db.executeDbAdminCommand({serverStatus: 1}, function(err, doc) {
|
||||
if(err == null && doc.ok === 1) {
|
||||
callback(null, doc);
|
||||
} else {
|
||||
if(err) return callback(err, false);
|
||||
return callback(toError(doc), false);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
define.classMethod('serverStatus', {callback: true, promise:true});
|
||||
|
||||
/**
|
||||
* Retrieve the current profiling Level for MongoDB
|
||||
*
|
||||
* @param {Admin~resultCallback} [callback] The command result callback
|
||||
* @return {Promise} returns Promise if no callback passed
|
||||
*/
|
||||
Admin.prototype.profilingLevel = function(callback) {
|
||||
var self = this;
|
||||
|
||||
// Execute using callback
|
||||
if(typeof callback == 'function') return profilingLevel(self, callback)
|
||||
|
||||
// Return a Promise
|
||||
return new this.s.promiseLibrary(function(resolve, reject) {
|
||||
profilingLevel(self, function(err, r) {
|
||||
if(err) return reject(err);
|
||||
resolve(r);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
var profilingLevel = function(self, callback) {
|
||||
self.s.db.executeDbAdminCommand({profile:-1}, function(err, doc) {
|
||||
if(err == null && doc.ok === 1) {
|
||||
var was = doc.was;
|
||||
if(was == 0) return callback(null, "off");
|
||||
if(was == 1) return callback(null, "slow_only");
|
||||
if(was == 2) return callback(null, "all");
|
||||
return callback(new Error("Error: illegal profiling level value " + was), null);
|
||||
} else {
|
||||
err != null ? callback(err, null) : callback(new Error("Error with profile command"), null);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
define.classMethod('profilingLevel', {callback: true, promise:true});
|
||||
|
||||
/**
|
||||
* Ping the MongoDB server and retrieve results
|
||||
*
|
||||
* @param {Admin~resultCallback} [callback] The command result callback
|
||||
* @return {Promise} returns Promise if no callback passed
|
||||
*/
|
||||
Admin.prototype.ping = function(options, callback) {
|
||||
var self = this;
|
||||
var args = Array.prototype.slice.call(arguments, 0);
|
||||
callback = args.pop();
|
||||
if(typeof callback != 'function') args.push(callback);
|
||||
|
||||
// Execute using callback
|
||||
if(typeof callback == 'function') return this.s.db.executeDbAdminCommand({ping: 1}, callback);
|
||||
|
||||
// Return a Promise
|
||||
return new this.s.promiseLibrary(function(resolve, reject) {
|
||||
self.s.db.executeDbAdminCommand({ping: 1}, function(err, r) {
|
||||
if(err) return reject(err);
|
||||
resolve(r);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
define.classMethod('ping', {callback: true, promise:true});
|
||||
|
||||
/**
|
||||
* Authenticate a user against the server.
|
||||
* @method
|
||||
* @param {string} username The username.
|
||||
* @param {string} [password] The password.
|
||||
* @param {Admin~resultCallback} [callback] The command result callback
|
||||
* @return {Promise} returns Promise if no callback passed
|
||||
* @deprecated This method will no longer be available in the next major release 3.x as MongoDB 3.6 will only allow auth against users in the admin db and will no longer allow multiple credentials on a socket. Please authenticate using MongoClient.connect with auth credentials.
|
||||
*/
|
||||
Admin.prototype.authenticate = function(username, password, options, callback) {
|
||||
console.warn("Admin.prototype.authenticate method will no longer be available in the next major release 3.x as MongoDB 3.6 will only allow auth against users in the admin db and will no longer allow multiple credentials on a socket. Please authenticate using MongoClient.connect with auth credentials.");
|
||||
var finalArguments = [this.s.db];
|
||||
if(typeof username == 'string') finalArguments.push(username);
|
||||
if(typeof password == 'string') finalArguments.push(password);
|
||||
if(typeof options == 'function') {
|
||||
finalArguments.push({ authdb: 'admin' });
|
||||
finalArguments.push(options);
|
||||
} else {
|
||||
finalArguments.push(assign({}, options, { authdb: 'admin' }));
|
||||
}
|
||||
|
||||
if(typeof callback == 'function') finalArguments.push(callback);
|
||||
// Execute authenticate method
|
||||
return authenticate.apply(this.s.db, finalArguments);
|
||||
}
|
||||
|
||||
define.classMethod('authenticate', {callback: true, promise:true});
|
||||
|
||||
/**
|
||||
* Logout user from server, fire off on all connections and remove all auth info
|
||||
* @method
|
||||
* @param {Admin~resultCallback} [callback] The command result callback
|
||||
* @return {Promise} returns Promise if no callback passed
|
||||
*/
|
||||
Admin.prototype.logout = function(callback) {
|
||||
var self = this;
|
||||
// Execute using callback
|
||||
if(typeof callback == 'function') return this.s.db.logout({dbName: 'admin'}, callback);
|
||||
|
||||
// Return a Promise
|
||||
return new this.s.promiseLibrary(function(resolve, reject) {
|
||||
self.s.db.logout({dbName: 'admin'}, function(err) {
|
||||
if(err) return reject(err);
|
||||
resolve(true);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
define.classMethod('logout', {callback: true, promise:true});
|
||||
|
||||
// Get write concern
|
||||
var writeConcern = function(options, db) {
|
||||
options = shallowClone(options);
|
||||
|
||||
// If options already contain write concerns return it
|
||||
if(options.w || options.wtimeout || options.j || options.fsync) {
|
||||
return options;
|
||||
}
|
||||
|
||||
// Set db write concern if available
|
||||
if(db.writeConcern) {
|
||||
if(options.w) options.w = db.writeConcern.w;
|
||||
if(options.wtimeout) options.wtimeout = db.writeConcern.wtimeout;
|
||||
if(options.j) options.j = db.writeConcern.j;
|
||||
if(options.fsync) options.fsync = db.writeConcern.fsync;
|
||||
}
|
||||
|
||||
// Return modified options
|
||||
return options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a user to the database.
|
||||
* @method
|
||||
* @param {string} username The username.
|
||||
* @param {string} password The password.
|
||||
* @param {object} [options=null] Optional settings.
|
||||
* @param {(number|string)} [options.w=null] The write concern.
|
||||
* @param {number} [options.wtimeout=null] The write concern timeout.
|
||||
* @param {boolean} [options.j=false] Specify a journal write concern.
|
||||
* @param {boolean} [options.fsync=false] Specify a file sync write concern.
|
||||
* @param {object} [options.customData=null] Custom data associated with the user (only Mongodb 2.6 or higher)
|
||||
* @param {object[]} [options.roles=null] Roles associated with the created user (only Mongodb 2.6 or higher)
|
||||
* @param {Admin~resultCallback} [callback] The command result callback
|
||||
* @return {Promise} returns Promise if no callback passed
|
||||
*/
|
||||
Admin.prototype.addUser = function(username, password, options, callback) {
|
||||
var self = this;
|
||||
var args = Array.prototype.slice.call(arguments, 2);
|
||||
callback = args.pop();
|
||||
if(typeof callback != 'function') args.push(callback);
|
||||
options = args.length ? args.shift() : {};
|
||||
options = options || {};
|
||||
// Get the options
|
||||
options = writeConcern(options, self.s.db)
|
||||
// Set the db name to admin
|
||||
options.dbName = 'admin';
|
||||
|
||||
// Execute using callback
|
||||
if(typeof callback == 'function')
|
||||
return self.s.db.addUser(username, password, options, callback);
|
||||
|
||||
// Return a Promise
|
||||
return new this.s.promiseLibrary(function(resolve, reject) {
|
||||
self.s.db.addUser(username, password, options, function(err, r) {
|
||||
if(err) return reject(err);
|
||||
resolve(r);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
define.classMethod('addUser', {callback: true, promise:true});
|
||||
|
||||
/**
|
||||
* Remove a user from a database
|
||||
* @method
|
||||
* @param {string} username The username.
|
||||
* @param {object} [options=null] Optional settings.
|
||||
* @param {(number|string)} [options.w=null] The write concern.
|
||||
* @param {number} [options.wtimeout=null] The write concern timeout.
|
||||
* @param {boolean} [options.j=false] Specify a journal write concern.
|
||||
* @param {boolean} [options.fsync=false] Specify a file sync write concern.
|
||||
* @param {Admin~resultCallback} [callback] The command result callback
|
||||
* @return {Promise} returns Promise if no callback passed
|
||||
*/
|
||||
Admin.prototype.removeUser = function(username, options, callback) {
|
||||
var self = this;
|
||||
var args = Array.prototype.slice.call(arguments, 1);
|
||||
callback = args.pop();
|
||||
if(typeof callback != 'function') args.push(callback);
|
||||
options = args.length ? args.shift() : {};
|
||||
options = options || {};
|
||||
// Get the options
|
||||
options = writeConcern(options, self.s.db)
|
||||
// Set the db name
|
||||
options.dbName = 'admin';
|
||||
|
||||
// Execute using callback
|
||||
if(typeof callback == 'function')
|
||||
return self.s.db.removeUser(username, options, callback);
|
||||
|
||||
// Return a Promise
|
||||
return new this.s.promiseLibrary(function(resolve, reject) {
|
||||
self.s.db.removeUser(username, options, function(err, r) {
|
||||
if(err) return reject(err);
|
||||
resolve(r);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
define.classMethod('removeUser', {callback: true, promise:true});
|
||||
|
||||
/**
|
||||
* Set the current profiling level of MongoDB
|
||||
*
|
||||
* @param {string} level The new profiling level (off, slow_only, all).
|
||||
* @param {Admin~resultCallback} [callback] The command result callback.
|
||||
* @return {Promise} returns Promise if no callback passed
|
||||
*/
|
||||
Admin.prototype.setProfilingLevel = function(level, callback) {
|
||||
var self = this;
|
||||
|
||||
// Execute using callback
|
||||
if(typeof callback == 'function') return setProfilingLevel(self, level, callback);
|
||||
|
||||
// Return a Promise
|
||||
return new this.s.promiseLibrary(function(resolve, reject) {
|
||||
setProfilingLevel(self, level, function(err, r) {
|
||||
if(err) return reject(err);
|
||||
resolve(r);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
var setProfilingLevel = function(self, level, callback) {
|
||||
var command = {};
|
||||
var profile = 0;
|
||||
|
||||
if(level == "off") {
|
||||
profile = 0;
|
||||
} else if(level == "slow_only") {
|
||||
profile = 1;
|
||||
} else if(level == "all") {
|
||||
profile = 2;
|
||||
} else {
|
||||
return callback(new Error("Error: illegal profiling level value " + level));
|
||||
}
|
||||
|
||||
// Set up the profile number
|
||||
command['profile'] = profile;
|
||||
|
||||
self.s.db.executeDbAdminCommand(command, function(err, doc) {
|
||||
if(err == null && doc.ok === 1)
|
||||
return callback(null, level);
|
||||
return err != null ? callback(err, null) : callback(new Error("Error with profile command"), null);
|
||||
});
|
||||
}
|
||||
|
||||
define.classMethod('setProfilingLevel', {callback: true, promise:true});
|
||||
|
||||
/**
|
||||
* Retrieve the current profiling information for MongoDB
|
||||
*
|
||||
* @param {Admin~resultCallback} [callback] The command result callback.
|
||||
* @return {Promise} returns Promise if no callback passed
|
||||
* @deprecated Query the system.profile collection directly.
|
||||
*/
|
||||
Admin.prototype.profilingInfo = function(callback) {
|
||||
var self = this;
|
||||
|
||||
// Execute using callback
|
||||
if(typeof callback == 'function') return profilingInfo(self, callback);
|
||||
|
||||
// Return a Promise
|
||||
return new this.s.promiseLibrary(function(resolve, reject) {
|
||||
profilingInfo(self, function(err, r) {
|
||||
if(err) return reject(err);
|
||||
resolve(r);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
var profilingInfo = function(self, callback) {
|
||||
try {
|
||||
self.s.topology.cursor("admin.system.profile", { find: 'system.profile', query: {}}, {}).toArray(callback);
|
||||
} catch (err) {
|
||||
return callback(err, null);
|
||||
}
|
||||
}
|
||||
|
||||
define.classMethod('profilingLevel', {callback: true, promise:true});
|
||||
|
||||
/**
|
||||
* Validate an existing collection
|
||||
*
|
||||
* @param {string} collectionName The name of the collection to validate.
|
||||
* @param {object} [options=null] Optional settings.
|
||||
* @param {Admin~resultCallback} [callback] The command result callback.
|
||||
* @return {Promise} returns Promise if no callback passed
|
||||
*/
|
||||
Admin.prototype.validateCollection = function(collectionName, options, callback) {
|
||||
var self = this;
|
||||
var args = Array.prototype.slice.call(arguments, 1);
|
||||
callback = args.pop();
|
||||
if(typeof callback != 'function') args.push(callback);
|
||||
options = args.length ? args.shift() : {};
|
||||
options = options || {};
|
||||
|
||||
// Execute using callback
|
||||
if(typeof callback == 'function')
|
||||
return validateCollection(self, collectionName, options, callback);
|
||||
|
||||
// Return a Promise
|
||||
return new this.s.promiseLibrary(function(resolve, reject) {
|
||||
validateCollection(self, collectionName, options, function(err, r) {
|
||||
if(err) return reject(err);
|
||||
resolve(r);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
var validateCollection = function(self, collectionName, options, callback) {
|
||||
var command = {validate: collectionName};
|
||||
var keys = Object.keys(options);
|
||||
|
||||
// Decorate command with extra options
|
||||
for(var i = 0; i < keys.length; i++) {
|
||||
if(options.hasOwnProperty(keys[i])) {
|
||||
command[keys[i]] = options[keys[i]];
|
||||
}
|
||||
}
|
||||
|
||||
self.s.db.command(command, function(err, doc) {
|
||||
if(err != null) return callback(err, null);
|
||||
|
||||
if(doc.ok === 0)
|
||||
return callback(new Error("Error with validate command"), null);
|
||||
if(doc.result != null && doc.result.constructor != String)
|
||||
return callback(new Error("Error with validation data"), null);
|
||||
if(doc.result != null && doc.result.match(/exception|corrupt/) != null)
|
||||
return callback(new Error("Error: invalid collection " + collectionName), null);
|
||||
if(doc.valid != null && !doc.valid)
|
||||
return callback(new Error("Error: invalid collection " + collectionName), null);
|
||||
|
||||
return callback(null, doc);
|
||||
});
|
||||
}
|
||||
|
||||
define.classMethod('validateCollection', {callback: true, promise:true});
|
||||
|
||||
/**
|
||||
* List the available databases
|
||||
*
|
||||
* @param {Admin~resultCallback} [callback] The command result callback.
|
||||
* @return {Promise} returns Promise if no callback passed
|
||||
*/
|
||||
Admin.prototype.listDatabases = function(callback) {
|
||||
var self = this;
|
||||
// Execute using callback
|
||||
if(typeof callback == 'function') return self.s.db.executeDbAdminCommand({listDatabases:1}, {}, callback);
|
||||
|
||||
// Return a Promise
|
||||
return new this.s.promiseLibrary(function(resolve, reject) {
|
||||
self.s.db.executeDbAdminCommand({listDatabases:1}, {}, function(err, r) {
|
||||
if(err) return reject(err);
|
||||
resolve(r);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
define.classMethod('listDatabases', {callback: true, promise:true});
|
||||
|
||||
/**
|
||||
* Get ReplicaSet status
|
||||
*
|
||||
* @param {Admin~resultCallback} [callback] The command result callback.
|
||||
* @return {Promise} returns Promise if no callback passed
|
||||
*/
|
||||
Admin.prototype.replSetGetStatus = function(callback) {
|
||||
var self = this;
|
||||
// Execute using callback
|
||||
if(typeof callback == 'function') return replSetGetStatus(self, callback);
|
||||
// Return a Promise
|
||||
return new this.s.promiseLibrary(function(resolve, reject) {
|
||||
replSetGetStatus(self, function(err, r) {
|
||||
if(err) return reject(err);
|
||||
resolve(r);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
var replSetGetStatus = function(self, callback) {
|
||||
self.s.db.executeDbAdminCommand({replSetGetStatus:1}, function(err, doc) {
|
||||
if(err == null && doc.ok === 1)
|
||||
return callback(null, doc);
|
||||
if(err) return callback(err, false);
|
||||
callback(toError(doc), false);
|
||||
});
|
||||
}
|
||||
|
||||
define.classMethod('replSetGetStatus', {callback: true, promise:true});
|
||||
|
||||
module.exports = Admin;
|
||||
+438
@@ -0,0 +1,438 @@
|
||||
"use strict";
|
||||
|
||||
var inherits = require('util').inherits
|
||||
, MongoError = require('mongodb-core').MongoError
|
||||
, Readable = require('stream').Readable || require('readable-stream').Readable
|
||||
, Define = require('./metadata')
|
||||
, CoreCursor = require('./cursor');
|
||||
|
||||
/**
|
||||
* @fileOverview The **AggregationCursor** class is an internal class that embodies an aggregation cursor on MongoDB
|
||||
* allowing for iteration over the results returned from the underlying query. It supports
|
||||
* one by one document iteration, conversion to an array or can be iterated as a Node 0.10.X
|
||||
* or higher stream
|
||||
*
|
||||
* **AGGREGATIONCURSOR Cannot directly be instantiated**
|
||||
* @example
|
||||
* var MongoClient = require('mongodb').MongoClient,
|
||||
* test = require('assert');
|
||||
* // Connection url
|
||||
* var url = 'mongodb://localhost:27017/test';
|
||||
* // Connect using MongoClient
|
||||
* MongoClient.connect(url, function(err, db) {
|
||||
* // Create a collection we want to drop later
|
||||
* var col = db.collection('createIndexExample1');
|
||||
* // Insert a bunch of documents
|
||||
* col.insert([{a:1, b:1}
|
||||
* , {a:2, b:2}, {a:3, b:3}
|
||||
* , {a:4, b:4}], {w:1}, function(err, result) {
|
||||
* test.equal(null, err);
|
||||
* // Show that duplicate records got dropped
|
||||
* col.aggregation({}, {cursor: {}}).toArray(function(err, items) {
|
||||
* test.equal(null, err);
|
||||
* test.equal(4, items.length);
|
||||
* db.close();
|
||||
* });
|
||||
* });
|
||||
* });
|
||||
*/
|
||||
|
||||
/**
|
||||
* Namespace provided by the browser.
|
||||
* @external Readable
|
||||
*/
|
||||
|
||||
/**
|
||||
* Creates a new Aggregation Cursor instance (INTERNAL TYPE, do not instantiate directly)
|
||||
* @class AggregationCursor
|
||||
* @extends external:Readable
|
||||
* @fires AggregationCursor#data
|
||||
* @fires AggregationCursor#end
|
||||
* @fires AggregationCursor#close
|
||||
* @fires AggregationCursor#readable
|
||||
* @return {AggregationCursor} an AggregationCursor instance.
|
||||
*/
|
||||
var AggregationCursor = function(bson, ns, cmd, options, topology, topologyOptions) {
|
||||
CoreCursor.apply(this, Array.prototype.slice.call(arguments, 0));
|
||||
var state = AggregationCursor.INIT;
|
||||
var streamOptions = {};
|
||||
|
||||
// MaxTimeMS
|
||||
var maxTimeMS = null;
|
||||
|
||||
// Get the promiseLibrary
|
||||
var promiseLibrary = options.promiseLibrary;
|
||||
|
||||
// No promise library selected fall back
|
||||
if(!promiseLibrary) {
|
||||
promiseLibrary = typeof global.Promise == 'function' ?
|
||||
global.Promise : require('es6-promise').Promise;
|
||||
}
|
||||
|
||||
// Set up
|
||||
Readable.call(this, {objectMode: true});
|
||||
|
||||
// Internal state
|
||||
this.s = {
|
||||
// MaxTimeMS
|
||||
maxTimeMS: maxTimeMS
|
||||
// State
|
||||
, state: state
|
||||
// Stream options
|
||||
, streamOptions: streamOptions
|
||||
// BSON
|
||||
, bson: bson
|
||||
// Namespace
|
||||
, ns: ns
|
||||
// Command
|
||||
, cmd: cmd
|
||||
// Options
|
||||
, options: options
|
||||
// Topology
|
||||
, topology: topology
|
||||
// Topology Options
|
||||
, topologyOptions: topologyOptions
|
||||
// Promise library
|
||||
, promiseLibrary: promiseLibrary
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* AggregationCursor stream data event, fired for each document in the cursor.
|
||||
*
|
||||
* @event AggregationCursor#data
|
||||
* @type {object}
|
||||
*/
|
||||
|
||||
/**
|
||||
* AggregationCursor stream end event
|
||||
*
|
||||
* @event AggregationCursor#end
|
||||
* @type {null}
|
||||
*/
|
||||
|
||||
/**
|
||||
* AggregationCursor stream close event
|
||||
*
|
||||
* @event AggregationCursor#close
|
||||
* @type {null}
|
||||
*/
|
||||
|
||||
/**
|
||||
* AggregationCursor stream readable event
|
||||
*
|
||||
* @event AggregationCursor#readable
|
||||
* @type {null}
|
||||
*/
|
||||
|
||||
// Inherit from Readable
|
||||
inherits(AggregationCursor, Readable);
|
||||
|
||||
// Extend the Cursor
|
||||
for(var name in CoreCursor.prototype) {
|
||||
AggregationCursor.prototype[name] = CoreCursor.prototype[name];
|
||||
}
|
||||
|
||||
var define = AggregationCursor.define = new Define('AggregationCursor', AggregationCursor, true);
|
||||
|
||||
/**
|
||||
* Set the batch size for the cursor.
|
||||
* @method
|
||||
* @param {number} value The batchSize for the cursor.
|
||||
* @throws {MongoError}
|
||||
* @return {AggregationCursor}
|
||||
*/
|
||||
AggregationCursor.prototype.batchSize = function(value) {
|
||||
if(this.s.state == AggregationCursor.CLOSED || this.isDead()) throw MongoError.create({message: "Cursor is closed", driver:true });
|
||||
if(typeof value != 'number') throw MongoError.create({message: "batchSize requires an integer", driver:true });
|
||||
if(this.s.cmd.cursor) this.s.cmd.cursor.batchSize = value;
|
||||
this.setCursorBatchSize(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
define.classMethod('batchSize', {callback: false, promise:false, returns: [AggregationCursor]});
|
||||
|
||||
/**
|
||||
* Add a geoNear stage to the aggregation pipeline
|
||||
* @method
|
||||
* @param {object} document The geoNear stage document.
|
||||
* @return {AggregationCursor}
|
||||
*/
|
||||
AggregationCursor.prototype.geoNear = function(document) {
|
||||
this.s.cmd.pipeline.push({$geoNear: document});
|
||||
return this;
|
||||
}
|
||||
|
||||
define.classMethod('geoNear', {callback: false, promise:false, returns: [AggregationCursor]});
|
||||
|
||||
/**
|
||||
* Add a group stage to the aggregation pipeline
|
||||
* @method
|
||||
* @param {object} document The group stage document.
|
||||
* @return {AggregationCursor}
|
||||
*/
|
||||
AggregationCursor.prototype.group = function(document) {
|
||||
this.s.cmd.pipeline.push({$group: document});
|
||||
return this;
|
||||
}
|
||||
|
||||
define.classMethod('group', {callback: false, promise:false, returns: [AggregationCursor]});
|
||||
|
||||
/**
|
||||
* Add a limit stage to the aggregation pipeline
|
||||
* @method
|
||||
* @param {number} value The state limit value.
|
||||
* @return {AggregationCursor}
|
||||
*/
|
||||
AggregationCursor.prototype.limit = function(value) {
|
||||
this.s.cmd.pipeline.push({$limit: value});
|
||||
return this;
|
||||
}
|
||||
|
||||
define.classMethod('limit', {callback: false, promise:false, returns: [AggregationCursor]});
|
||||
|
||||
/**
|
||||
* Add a match stage to the aggregation pipeline
|
||||
* @method
|
||||
* @param {object} document The match stage document.
|
||||
* @return {AggregationCursor}
|
||||
*/
|
||||
AggregationCursor.prototype.match = function(document) {
|
||||
this.s.cmd.pipeline.push({$match: document});
|
||||
return this;
|
||||
}
|
||||
|
||||
define.classMethod('match', {callback: false, promise:false, returns: [AggregationCursor]});
|
||||
|
||||
/**
|
||||
* Add a maxTimeMS stage to the aggregation pipeline
|
||||
* @method
|
||||
* @param {number} value The state maxTimeMS value.
|
||||
* @return {AggregationCursor}
|
||||
*/
|
||||
AggregationCursor.prototype.maxTimeMS = function(value) {
|
||||
if(this.s.topology.lastIsMaster().minWireVersion > 2) {
|
||||
this.s.cmd.maxTimeMS = value;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
define.classMethod('maxTimeMS', {callback: false, promise:false, returns: [AggregationCursor]});
|
||||
|
||||
/**
|
||||
* Add a out stage to the aggregation pipeline
|
||||
* @method
|
||||
* @param {number} destination The destination name.
|
||||
* @return {AggregationCursor}
|
||||
*/
|
||||
AggregationCursor.prototype.out = function(destination) {
|
||||
this.s.cmd.pipeline.push({$out: destination});
|
||||
return this;
|
||||
}
|
||||
|
||||
define.classMethod('out', {callback: false, promise:false, returns: [AggregationCursor]});
|
||||
|
||||
/**
|
||||
* Add a project stage to the aggregation pipeline
|
||||
* @method
|
||||
* @param {object} document The project stage document.
|
||||
* @return {AggregationCursor}
|
||||
*/
|
||||
AggregationCursor.prototype.project = function(document) {
|
||||
this.s.cmd.pipeline.push({$project: document});
|
||||
return this;
|
||||
}
|
||||
|
||||
define.classMethod('project', {callback: false, promise:false, returns: [AggregationCursor]});
|
||||
|
||||
/**
|
||||
* Add a lookup stage to the aggregation pipeline
|
||||
* @method
|
||||
* @param {object} document The lookup stage document.
|
||||
* @return {AggregationCursor}
|
||||
*/
|
||||
AggregationCursor.prototype.lookup = function(document) {
|
||||
this.s.cmd.pipeline.push({$lookup: document});
|
||||
return this;
|
||||
}
|
||||
|
||||
define.classMethod('lookup', {callback: false, promise:false, returns: [AggregationCursor]});
|
||||
|
||||
/**
|
||||
* Add a redact stage to the aggregation pipeline
|
||||
* @method
|
||||
* @param {object} document The redact stage document.
|
||||
* @return {AggregationCursor}
|
||||
*/
|
||||
AggregationCursor.prototype.redact = function(document) {
|
||||
this.s.cmd.pipeline.push({$redact: document});
|
||||
return this;
|
||||
}
|
||||
|
||||
define.classMethod('redact', {callback: false, promise:false, returns: [AggregationCursor]});
|
||||
|
||||
/**
|
||||
* Add a skip stage to the aggregation pipeline
|
||||
* @method
|
||||
* @param {number} value The state skip value.
|
||||
* @return {AggregationCursor}
|
||||
*/
|
||||
AggregationCursor.prototype.skip = function(value) {
|
||||
this.s.cmd.pipeline.push({$skip: value});
|
||||
return this;
|
||||
}
|
||||
|
||||
define.classMethod('skip', {callback: false, promise:false, returns: [AggregationCursor]});
|
||||
|
||||
/**
|
||||
* Add a sort stage to the aggregation pipeline
|
||||
* @method
|
||||
* @param {object} document The sort stage document.
|
||||
* @return {AggregationCursor}
|
||||
*/
|
||||
AggregationCursor.prototype.sort = function(document) {
|
||||
this.s.cmd.pipeline.push({$sort: document});
|
||||
return this;
|
||||
}
|
||||
|
||||
define.classMethod('sort', {callback: false, promise:false, returns: [AggregationCursor]});
|
||||
|
||||
/**
|
||||
* Add a unwind stage to the aggregation pipeline
|
||||
* @method
|
||||
* @param {number} field The unwind field name.
|
||||
* @return {AggregationCursor}
|
||||
*/
|
||||
AggregationCursor.prototype.unwind = function(field) {
|
||||
this.s.cmd.pipeline.push({$unwind: field});
|
||||
return this;
|
||||
}
|
||||
|
||||
define.classMethod('unwind', {callback: false, promise:false, returns: [AggregationCursor]});
|
||||
|
||||
AggregationCursor.prototype.get = AggregationCursor.prototype.toArray;
|
||||
|
||||
// Inherited methods
|
||||
define.classMethod('toArray', {callback: true, promise:true});
|
||||
define.classMethod('each', {callback: true, promise:false});
|
||||
define.classMethod('forEach', {callback: true, promise:false});
|
||||
define.classMethod('hasNext', {callback: true, promise:true});
|
||||
define.classMethod('next', {callback: true, promise:true});
|
||||
define.classMethod('close', {callback: true, promise:true});
|
||||
define.classMethod('isClosed', {callback: false, promise:false, returns: [Boolean]});
|
||||
define.classMethod('rewind', {callback: false, promise:false});
|
||||
define.classMethod('bufferedCount', {callback: false, promise:false, returns: [Number]});
|
||||
define.classMethod('readBufferedDocuments', {callback: false, promise:false, returns: [Array]});
|
||||
|
||||
/**
|
||||
* Get the next available document from the cursor, returns null if no more documents are available.
|
||||
* @function AggregationCursor.prototype.next
|
||||
* @param {AggregationCursor~resultCallback} [callback] The result callback.
|
||||
* @throws {MongoError}
|
||||
* @return {Promise} returns Promise if no callback passed
|
||||
*/
|
||||
|
||||
/**
|
||||
* Check if there is any document still available in the cursor
|
||||
* @function AggregationCursor.prototype.hasNext
|
||||
* @param {AggregationCursor~resultCallback} [callback] The result callback.
|
||||
* @throws {MongoError}
|
||||
* @return {Promise} returns Promise if no callback passed
|
||||
*/
|
||||
|
||||
/**
|
||||
* The callback format for results
|
||||
* @callback AggregationCursor~toArrayResultCallback
|
||||
* @param {MongoError} error An error instance representing the error during the execution.
|
||||
* @param {object[]} documents All the documents the satisfy the cursor.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Returns an array of documents. The caller is responsible for making sure that there
|
||||
* is enough memory to store the results. Note that the array only contain partial
|
||||
* results when this cursor had been previously accessed. In that case,
|
||||
* cursor.rewind() can be used to reset the cursor.
|
||||
* @method AggregationCursor.prototype.toArray
|
||||
* @param {AggregationCursor~toArrayResultCallback} [callback] The result callback.
|
||||
* @throws {MongoError}
|
||||
* @return {Promise} returns Promise if no callback passed
|
||||
*/
|
||||
|
||||
/**
|
||||
* The callback format for results
|
||||
* @callback AggregationCursor~resultCallback
|
||||
* @param {MongoError} error An error instance representing the error during the execution.
|
||||
* @param {(object|null)} result The result object if the command was executed successfully.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Iterates over all the documents for this cursor. As with **{cursor.toArray}**,
|
||||
* not all of the elements will be iterated if this cursor had been previously accessed.
|
||||
* In that case, **{cursor.rewind}** can be used to reset the cursor. However, unlike
|
||||
* **{cursor.toArray}**, the cursor will only hold a maximum of batch size elements
|
||||
* at any given time if batch size is specified. Otherwise, the caller is responsible
|
||||
* for making sure that the entire result can fit the memory.
|
||||
* @method AggregationCursor.prototype.each
|
||||
* @param {AggregationCursor~resultCallback} callback The result callback.
|
||||
* @throws {MongoError}
|
||||
* @return {null}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Close the cursor, sending a AggregationCursor command and emitting close.
|
||||
* @method AggregationCursor.prototype.close
|
||||
* @param {AggregationCursor~resultCallback} [callback] The result callback.
|
||||
* @return {Promise} returns Promise if no callback passed
|
||||
*/
|
||||
|
||||
/**
|
||||
* Is the cursor closed
|
||||
* @method AggregationCursor.prototype.isClosed
|
||||
* @return {boolean}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Execute the explain for the cursor
|
||||
* @method AggregationCursor.prototype.explain
|
||||
* @param {AggregationCursor~resultCallback} [callback] The result callback.
|
||||
* @return {Promise} returns Promise if no callback passed
|
||||
*/
|
||||
|
||||
/**
|
||||
* Clone the cursor
|
||||
* @function AggregationCursor.prototype.clone
|
||||
* @return {AggregationCursor}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Resets the cursor
|
||||
* @function AggregationCursor.prototype.rewind
|
||||
* @return {AggregationCursor}
|
||||
*/
|
||||
|
||||
/**
|
||||
* The callback format for the forEach iterator method
|
||||
* @callback AggregationCursor~iteratorCallback
|
||||
* @param {Object} doc An emitted document for the iterator
|
||||
*/
|
||||
|
||||
/**
|
||||
* The callback error format for the forEach iterator method
|
||||
* @callback AggregationCursor~endCallback
|
||||
* @param {MongoError} error An error instance representing the error during the execution.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Iterates over all the documents for this cursor using the iterator, callback pattern.
|
||||
* @method AggregationCursor.prototype.forEach
|
||||
* @param {AggregationCursor~iteratorCallback} iterator The iteration callback.
|
||||
* @param {AggregationCursor~endCallback} callback The end callback.
|
||||
* @throws {MongoError}
|
||||
* @return {null}
|
||||
*/
|
||||
|
||||
AggregationCursor.INIT = 0;
|
||||
AggregationCursor.OPEN = 1;
|
||||
AggregationCursor.CLOSED = 2;
|
||||
|
||||
module.exports = AggregationCursor;
|
||||
+596
@@ -0,0 +1,596 @@
|
||||
var EventEmitter = require('events').EventEmitter,
|
||||
inherits = require('util').inherits;
|
||||
|
||||
// Get prototypes
|
||||
var AggregationCursor = require('./aggregation_cursor'),
|
||||
CommandCursor = require('./command_cursor'),
|
||||
OrderedBulkOperation = require('./bulk/ordered').OrderedBulkOperation,
|
||||
UnorderedBulkOperation = require('./bulk/unordered').UnorderedBulkOperation,
|
||||
GridStore = require('./gridfs/grid_store'),
|
||||
Cursor = require('./cursor'),
|
||||
Collection = require('./collection'),
|
||||
Db = require('./db');
|
||||
|
||||
var basicOperationIdGenerator = {
|
||||
operationId: 1,
|
||||
|
||||
next: function() {
|
||||
return this.operationId++;
|
||||
}
|
||||
}
|
||||
|
||||
var basicTimestampGenerator = {
|
||||
current: function() {
|
||||
return new Date().getTime();
|
||||
},
|
||||
|
||||
duration: function(start, end) {
|
||||
return end - start;
|
||||
}
|
||||
}
|
||||
|
||||
var senstiveCommands = ['authenticate', 'saslStart', 'saslContinue', 'getnonce',
|
||||
'createUser', 'updateUser', 'copydbgetnonce', 'copydbsaslstart', 'copydb'];
|
||||
|
||||
var Instrumentation = function(core, options, callback) {
|
||||
options = options || {};
|
||||
|
||||
// Optional id generators
|
||||
var operationIdGenerator = options.operationIdGenerator || basicOperationIdGenerator;
|
||||
// Optional timestamp generator
|
||||
var timestampGenerator = options.timestampGenerator || basicTimestampGenerator;
|
||||
// Extend with event emitter functionality
|
||||
EventEmitter.call(this);
|
||||
|
||||
// Contains all the instrumentation overloads
|
||||
this.overloads = [];
|
||||
|
||||
// ---------------------------------------------------------
|
||||
//
|
||||
// Instrument prototype
|
||||
//
|
||||
// ---------------------------------------------------------
|
||||
|
||||
var instrumentPrototype = function(callback) {
|
||||
var instrumentations = []
|
||||
|
||||
// Classes to support
|
||||
var classes = [GridStore, OrderedBulkOperation, UnorderedBulkOperation,
|
||||
CommandCursor, AggregationCursor, Cursor, Collection, Db];
|
||||
|
||||
// Add instrumentations to the available list
|
||||
for(var i = 0; i < classes.length; i++) {
|
||||
if(classes[i].define) {
|
||||
instrumentations.push(classes[i].define.generate());
|
||||
}
|
||||
}
|
||||
|
||||
// Return the list of instrumentation points
|
||||
callback(null, instrumentations);
|
||||
}
|
||||
|
||||
// Did the user want to instrument the prototype
|
||||
if(typeof callback == 'function') {
|
||||
instrumentPrototype(callback);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------
|
||||
//
|
||||
// Server
|
||||
//
|
||||
// ---------------------------------------------------------
|
||||
|
||||
// Reference
|
||||
var self = this;
|
||||
// Names of methods we need to wrap
|
||||
var methods = ['command', 'insert', 'update', 'remove'];
|
||||
// Prototype
|
||||
var proto = core.Server.prototype;
|
||||
// Core server method we are going to wrap
|
||||
methods.forEach(function(x) {
|
||||
var func = proto[x];
|
||||
|
||||
// Add to overloaded methods
|
||||
self.overloads.push({proto: proto, name:x, func:func});
|
||||
|
||||
// The actual prototype
|
||||
proto[x] = function() {
|
||||
var requestId = core.Query.nextRequestId();
|
||||
// Get the arguments
|
||||
var args = Array.prototype.slice.call(arguments, 0);
|
||||
var ns = args[0];
|
||||
var commandObj = args[1];
|
||||
var options = args[2] || {};
|
||||
var keys = Object.keys(commandObj);
|
||||
var commandName = keys[0];
|
||||
var db = ns.split('.')[0];
|
||||
|
||||
// Get the collection
|
||||
var col = ns.split('.');
|
||||
col.shift();
|
||||
col = col.join('.');
|
||||
|
||||
// Do we have a legacy insert/update/remove command
|
||||
if(x == 'insert') { //} && !this.lastIsMaster().maxWireVersion) {
|
||||
commandName = 'insert';
|
||||
|
||||
// Re-write the command
|
||||
commandObj = {
|
||||
insert: col, documents: commandObj
|
||||
}
|
||||
|
||||
if(options.writeConcern && Object.keys(options.writeConcern).length > 0) {
|
||||
commandObj.writeConcern = options.writeConcern;
|
||||
}
|
||||
|
||||
commandObj.ordered = options.ordered != undefined ? options.ordered : true;
|
||||
} else if(x == 'update') { // && !this.lastIsMaster().maxWireVersion) {
|
||||
commandName = 'update';
|
||||
|
||||
// Re-write the command
|
||||
commandObj = {
|
||||
update: col, updates: commandObj
|
||||
}
|
||||
|
||||
if(options.writeConcern && Object.keys(options.writeConcern).length > 0) {
|
||||
commandObj.writeConcern = options.writeConcern;
|
||||
}
|
||||
|
||||
commandObj.ordered = options.ordered != undefined ? options.ordered : true;
|
||||
} else if(x == 'remove') { //&& !this.lastIsMaster().maxWireVersion) {
|
||||
commandName = 'delete';
|
||||
|
||||
// Re-write the command
|
||||
commandObj = {
|
||||
delete: col, deletes: commandObj
|
||||
}
|
||||
|
||||
if(options.writeConcern && Object.keys(options.writeConcern).length > 0) {
|
||||
commandObj.writeConcern = options.writeConcern;
|
||||
}
|
||||
|
||||
commandObj.ordered = options.ordered != undefined ? options.ordered : true;
|
||||
}
|
||||
|
||||
// Get the callback
|
||||
var callback = args.pop();
|
||||
// Set current callback operation id from the current context or create
|
||||
// a new one
|
||||
var ourOpId = callback.operationId || operationIdGenerator.next();
|
||||
|
||||
// Get a connection reference for this server instance
|
||||
var connection = this.s.pool.get()
|
||||
|
||||
// Emit the start event for the command
|
||||
var command = {
|
||||
// Returns the command.
|
||||
command: commandObj,
|
||||
// Returns the database name.
|
||||
databaseName: db,
|
||||
// Returns the command name.
|
||||
commandName: commandName,
|
||||
// Returns the driver generated request id.
|
||||
requestId: requestId,
|
||||
// Returns the driver generated operation id.
|
||||
// This is used to link events together such as bulk write operations. OPTIONAL.
|
||||
operationId: ourOpId,
|
||||
// Returns the connection id for the command. For languages that do not have this,
|
||||
// this MUST return the driver equivalent which MUST include the server address and port.
|
||||
// The name of this field is flexible to match the object that is returned from the driver.
|
||||
connectionId: connection
|
||||
};
|
||||
|
||||
// Filter out any sensitive commands
|
||||
if(senstiveCommands.indexOf(commandName.toLowerCase()) != -1) {
|
||||
command.commandObj = {};
|
||||
command.commandObj[commandName] = true;
|
||||
}
|
||||
|
||||
// Emit the started event
|
||||
self.emit('started', command)
|
||||
|
||||
// Start time
|
||||
var startTime = timestampGenerator.current();
|
||||
|
||||
// Push our handler callback
|
||||
args.push(function(err, r) {
|
||||
var endTime = timestampGenerator.current();
|
||||
var command = {
|
||||
duration: timestampGenerator.duration(startTime, endTime),
|
||||
commandName: commandName,
|
||||
requestId: requestId,
|
||||
operationId: ourOpId,
|
||||
connectionId: connection
|
||||
};
|
||||
|
||||
// If we have an error
|
||||
if(err || (r && r.result && r.result.ok == 0)) {
|
||||
command.failure = err || r.result.writeErrors || r.result;
|
||||
|
||||
// Filter out any sensitive commands
|
||||
if(senstiveCommands.indexOf(commandName.toLowerCase()) != -1) {
|
||||
command.failure = {};
|
||||
}
|
||||
|
||||
self.emit('failed', command);
|
||||
} else if(commandObj && commandObj.writeConcern
|
||||
&& commandObj.writeConcern.w == 0) {
|
||||
// If we have write concern 0
|
||||
command.reply = {ok:1};
|
||||
self.emit('succeeded', command);
|
||||
} else {
|
||||
command.reply = r && r.result ? r.result : r;
|
||||
|
||||
// Filter out any sensitive commands
|
||||
if(senstiveCommands.indexOf(commandName.toLowerCase()) != -1) {
|
||||
command.reply = {};
|
||||
}
|
||||
|
||||
self.emit('succeeded', command);
|
||||
}
|
||||
|
||||
// Return to caller
|
||||
callback(err, r);
|
||||
});
|
||||
|
||||
// Apply the call
|
||||
func.apply(this, args);
|
||||
}
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------
|
||||
//
|
||||
// Bulk Operations
|
||||
//
|
||||
// ---------------------------------------------------------
|
||||
|
||||
// Inject ourselves into the Bulk methods
|
||||
methods = ['execute'];
|
||||
var prototypes = [
|
||||
require('./bulk/ordered').Bulk.prototype,
|
||||
require('./bulk/unordered').Bulk.prototype
|
||||
]
|
||||
|
||||
prototypes.forEach(function(proto) {
|
||||
// Core server method we are going to wrap
|
||||
methods.forEach(function(x) {
|
||||
var func = proto[x];
|
||||
|
||||
// Add to overloaded methods
|
||||
self.overloads.push({proto: proto, name:x, func:func});
|
||||
|
||||
// The actual prototype
|
||||
proto[x] = function() {
|
||||
// Get the arguments
|
||||
var args = Array.prototype.slice.call(arguments, 0);
|
||||
// Set an operation Id on the bulk object
|
||||
this.operationId = operationIdGenerator.next();
|
||||
|
||||
// Get the callback
|
||||
var callback = args.pop();
|
||||
// If we have a callback use this
|
||||
if(typeof callback == 'function') {
|
||||
args.push(function(err, r) {
|
||||
// Return to caller
|
||||
callback(err, r);
|
||||
});
|
||||
|
||||
// Apply the call
|
||||
func.apply(this, args);
|
||||
} else {
|
||||
return func.apply(this, args);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------
|
||||
//
|
||||
// Cursor
|
||||
//
|
||||
// ---------------------------------------------------------
|
||||
|
||||
// Inject ourselves into the Cursor methods
|
||||
methods = ['_find', '_getmore', '_killcursor'];
|
||||
prototypes = [
|
||||
require('./cursor').prototype,
|
||||
require('./command_cursor').prototype,
|
||||
require('./aggregation_cursor').prototype
|
||||
]
|
||||
|
||||
// Command name translation
|
||||
var commandTranslation = {
|
||||
'_find': 'find', '_getmore': 'getMore', '_killcursor': 'killCursors', '_explain': 'explain'
|
||||
}
|
||||
|
||||
prototypes.forEach(function(proto) {
|
||||
|
||||
// Core server method we are going to wrap
|
||||
methods.forEach(function(x) {
|
||||
var func = proto[x];
|
||||
|
||||
// Add to overloaded methods
|
||||
self.overloads.push({proto: proto, name:x, func:func});
|
||||
|
||||
// The actual prototype
|
||||
proto[x] = function() {
|
||||
var cursor = this;
|
||||
var requestId = core.Query.nextRequestId();
|
||||
var ourOpId = operationIdGenerator.next();
|
||||
var parts = this.ns.split('.');
|
||||
var db = parts[0];
|
||||
|
||||
// Get the collection
|
||||
parts.shift();
|
||||
var collection = parts.join('.');
|
||||
|
||||
// Set the command
|
||||
var command = this.query;
|
||||
var cmd = this.s.cmd;
|
||||
|
||||
// If we have a find method, set the operationId on the cursor
|
||||
if(x == '_find') {
|
||||
cursor.operationId = ourOpId;
|
||||
}
|
||||
|
||||
// Do we have a find command rewrite it
|
||||
if(x == '_getmore') {
|
||||
command = {
|
||||
getMore: this.cursorState.cursorId,
|
||||
collection: collection,
|
||||
batchSize: cmd.batchSize
|
||||
}
|
||||
|
||||
if(cmd.maxTimeMS) command.maxTimeMS = cmd.maxTimeMS;
|
||||
} else if(x == '_killcursor') {
|
||||
command = {
|
||||
killCursors: collection,
|
||||
cursors: [this.cursorState.cursorId]
|
||||
}
|
||||
} else if(cmd.find) {
|
||||
command = {
|
||||
find: collection, filter: cmd.query
|
||||
}
|
||||
|
||||
if(cmd.sort) command.sort = cmd.sort;
|
||||
if(cmd.fields) command.projection = cmd.fields;
|
||||
if(cmd.limit && cmd.limit < 0) {
|
||||
command.limit = Math.abs(cmd.limit);
|
||||
command.singleBatch = true;
|
||||
} else if(cmd.limit) {
|
||||
command.limit = Math.abs(cmd.limit);
|
||||
}
|
||||
|
||||
// Options
|
||||
if(cmd.skip) command.skip = cmd.skip;
|
||||
if(cmd.hint) command.hint = cmd.hint;
|
||||
if(cmd.batchSize) command.batchSize = cmd.batchSize;
|
||||
if(typeof cmd.returnKey == 'boolean') command.returnKey = cmd.returnKey;
|
||||
if(cmd.comment) command.comment = cmd.comment;
|
||||
if(cmd.min) command.min = cmd.min;
|
||||
if(cmd.max) command.max = cmd.max;
|
||||
if(cmd.maxScan) command.maxScan = cmd.maxScan;
|
||||
if(cmd.maxTimeMS) command.maxTimeMS = cmd.maxTimeMS;
|
||||
|
||||
// Flags
|
||||
if(typeof cmd.awaitData == 'boolean') command.awaitData = cmd.awaitData;
|
||||
if(typeof cmd.snapshot == 'boolean') command.snapshot = cmd.snapshot;
|
||||
if(typeof cmd.tailable == 'boolean') command.tailable = cmd.tailable;
|
||||
if(typeof cmd.oplogReplay == 'boolean') command.oplogReplay = cmd.oplogReplay;
|
||||
if(typeof cmd.noCursorTimeout == 'boolean') command.noCursorTimeout = cmd.noCursorTimeout;
|
||||
if(typeof cmd.partial == 'boolean') command.partial = cmd.partial;
|
||||
if(typeof cmd.showDiskLoc == 'boolean') command.showRecordId = cmd.showDiskLoc;
|
||||
|
||||
// Read Concern
|
||||
if(cmd.readConcern) command.readConcern = cmd.readConcern;
|
||||
|
||||
// Override method
|
||||
if(cmd.explain) command.explain = cmd.explain;
|
||||
if(cmd.exhaust) command.exhaust = cmd.exhaust;
|
||||
|
||||
// If we have a explain flag
|
||||
if(cmd.explain) {
|
||||
// Create fake explain command
|
||||
command = {
|
||||
explain: command,
|
||||
verbosity: 'allPlansExecution'
|
||||
}
|
||||
|
||||
// Set readConcern on the command if available
|
||||
if(cmd.readConcern) command.readConcern = cmd.readConcern
|
||||
|
||||
// Set up the _explain name for the command
|
||||
x = '_explain';
|
||||
}
|
||||
} else {
|
||||
command = cmd;
|
||||
}
|
||||
|
||||
// Set up the connection
|
||||
var connectionId = null;
|
||||
|
||||
// Set local connection
|
||||
if(this.connection) connectionId = this.connection;
|
||||
if(!connectionId && this.server && this.server.getConnection) connectionId = this.server.getConnection();
|
||||
|
||||
// Get the command Name
|
||||
var commandName = x == '_find' ? Object.keys(command)[0] : commandTranslation[x];
|
||||
|
||||
// Emit the start event for the command
|
||||
command = {
|
||||
// Returns the command.
|
||||
command: command,
|
||||
// Returns the database name.
|
||||
databaseName: db,
|
||||
// Returns the command name.
|
||||
commandName: commandName,
|
||||
// Returns the driver generated request id.
|
||||
requestId: requestId,
|
||||
// Returns the driver generated operation id.
|
||||
// This is used to link events together such as bulk write operations. OPTIONAL.
|
||||
operationId: this.operationId,
|
||||
// Returns the connection id for the command. For languages that do not have this,
|
||||
// this MUST return the driver equivalent which MUST include the server address and port.
|
||||
// The name of this field is flexible to match the object that is returned from the driver.
|
||||
connectionId: connectionId
|
||||
};
|
||||
|
||||
// Get the arguments
|
||||
var args = Array.prototype.slice.call(arguments, 0);
|
||||
|
||||
// Get the callback
|
||||
var callback = args.pop();
|
||||
|
||||
// We do not have a callback but a Promise
|
||||
if(typeof callback == 'function' || command.commandName == 'killCursors') {
|
||||
var startTime = timestampGenerator.current();
|
||||
// Emit the started event
|
||||
self.emit('started', command)
|
||||
|
||||
// Emit succeeded event with killcursor if we have a legacy protocol
|
||||
if(command.commandName == 'killCursors'
|
||||
&& this.server.lastIsMaster()
|
||||
&& this.server.lastIsMaster().maxWireVersion < 4) {
|
||||
// Emit the succeeded command
|
||||
command = {
|
||||
duration: timestampGenerator.duration(startTime, timestampGenerator.current()),
|
||||
commandName: commandName,
|
||||
requestId: requestId,
|
||||
operationId: cursor.operationId,
|
||||
connectionId: cursor.server.getConnection(),
|
||||
reply: [{ok:1}]
|
||||
};
|
||||
|
||||
// Apply callback to the list of args
|
||||
args.push(callback);
|
||||
// Apply the call
|
||||
func.apply(this, args);
|
||||
// Emit the command
|
||||
return self.emit('succeeded', command)
|
||||
}
|
||||
|
||||
// Add our callback handler
|
||||
args.push(function(err, r) {
|
||||
if(err) {
|
||||
// Command
|
||||
var command = {
|
||||
duration: timestampGenerator.duration(startTime, timestampGenerator.current()),
|
||||
commandName: commandName,
|
||||
requestId: requestId,
|
||||
operationId: ourOpId,
|
||||
connectionId: cursor.server.getConnection(),
|
||||
failure: err };
|
||||
|
||||
// Emit the command
|
||||
self.emit('failed', command)
|
||||
} else {
|
||||
// Do we have a getMore
|
||||
if(commandName.toLowerCase() == 'getmore' && r == null) {
|
||||
r = {
|
||||
cursor: {
|
||||
id: cursor.cursorState.cursorId,
|
||||
ns: cursor.ns,
|
||||
nextBatch: cursor.cursorState.documents
|
||||
}, ok:1
|
||||
}
|
||||
} else if((commandName.toLowerCase() == 'find'
|
||||
|| commandName.toLowerCase() == 'aggregate'
|
||||
|| commandName.toLowerCase() == 'listcollections') && r == null) {
|
||||
r = {
|
||||
cursor: {
|
||||
id: cursor.cursorState.cursorId,
|
||||
ns: cursor.ns,
|
||||
firstBatch: cursor.cursorState.documents
|
||||
}, ok:1
|
||||
}
|
||||
} else if(commandName.toLowerCase() == 'killcursors' && r == null) {
|
||||
r = {
|
||||
cursorsUnknown:[cursor.cursorState.lastCursorId],
|
||||
ok:1
|
||||
}
|
||||
}
|
||||
|
||||
// cursor id is zero, we can issue success command
|
||||
command = {
|
||||
duration: timestampGenerator.duration(startTime, timestampGenerator.current()),
|
||||
commandName: commandName,
|
||||
requestId: requestId,
|
||||
operationId: cursor.operationId,
|
||||
connectionId: cursor.server.getConnection(),
|
||||
reply: r && r.result ? r.result : r
|
||||
};
|
||||
|
||||
// Emit the command
|
||||
self.emit('succeeded', command)
|
||||
}
|
||||
|
||||
// Return
|
||||
if(!callback) return;
|
||||
|
||||
// Return to caller
|
||||
callback(err, r);
|
||||
});
|
||||
|
||||
// Apply the call
|
||||
func.apply(this, args);
|
||||
} else {
|
||||
// Assume promise, push back the missing value
|
||||
args.push(callback);
|
||||
// Get the promise
|
||||
var promise = func.apply(this, args);
|
||||
// Return a new promise
|
||||
return new cursor.s.promiseLibrary(function(resolve, reject) {
|
||||
var startTime = timestampGenerator.current();
|
||||
// Emit the started event
|
||||
self.emit('started', command)
|
||||
// Execute the function
|
||||
promise.then(function() {
|
||||
// cursor id is zero, we can issue success command
|
||||
var command = {
|
||||
duration: timestampGenerator.duration(startTime, timestampGenerator.current()),
|
||||
commandName: commandName,
|
||||
requestId: requestId,
|
||||
operationId: cursor.operationId,
|
||||
connectionId: cursor.server.getConnection(),
|
||||
reply: cursor.cursorState.documents
|
||||
};
|
||||
|
||||
// Emit the command
|
||||
self.emit('succeeded', command)
|
||||
}).catch(function(err) {
|
||||
// Command
|
||||
var command = {
|
||||
duration: timestampGenerator.duration(startTime, timestampGenerator.current()),
|
||||
commandName: commandName,
|
||||
requestId: requestId,
|
||||
operationId: ourOpId,
|
||||
connectionId: cursor.server.getConnection(),
|
||||
failure: err };
|
||||
|
||||
// Emit the command
|
||||
self.emit('failed', command)
|
||||
// reject the promise
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
inherits(Instrumentation, EventEmitter);
|
||||
|
||||
Instrumentation.prototype.uninstrument = function() {
|
||||
for(var i = 0; i < this.overloads.length; i++) {
|
||||
var obj = this.overloads[i];
|
||||
obj.proto[obj.name] = obj.func;
|
||||
}
|
||||
|
||||
// Remove all listeners
|
||||
this.removeAllListeners('started');
|
||||
this.removeAllListeners('succeeded');
|
||||
this.removeAllListeners('failed');
|
||||
}
|
||||
|
||||
module.exports = Instrumentation;
|
||||
+110
@@ -0,0 +1,110 @@
|
||||
var shallowClone = require('./utils').shallowClone
|
||||
, handleCallback = require('./utils').handleCallback
|
||||
, MongoError = require('mongodb-core').MongoError
|
||||
, f = require('util').format;
|
||||
|
||||
var authenticate = function(self, username, password, options, callback) {
|
||||
// Did the user destroy the topology
|
||||
if(self.serverConfig && self.serverConfig.isDestroyed()) return callback(new MongoError('topology was destroyed'));
|
||||
|
||||
// the default db to authenticate against is 'self'
|
||||
// if authenticate is called from a retry context, it may be another one, like admin
|
||||
var authdb = options.dbName ? options.dbName : self.databaseName;
|
||||
authdb = self.authSource ? self.authSource : authdb;
|
||||
authdb = options.authdb ? options.authdb : authdb;
|
||||
authdb = options.authSource ? options.authSource : authdb;
|
||||
|
||||
// Callback
|
||||
var _callback = function(err, result) {
|
||||
if(self.listeners('authenticated').length > 0) {
|
||||
self.emit('authenticated', err, result);
|
||||
}
|
||||
|
||||
// Return to caller
|
||||
handleCallback(callback, err, result);
|
||||
}
|
||||
|
||||
// authMechanism
|
||||
var authMechanism = options.authMechanism || '';
|
||||
authMechanism = authMechanism.toUpperCase();
|
||||
|
||||
// If classic auth delegate to auth command
|
||||
if(authMechanism == 'MONGODB-CR') {
|
||||
self.s.topology.auth('mongocr', authdb, username, password, function(err) {
|
||||
if(err) return handleCallback(callback, err, false);
|
||||
_callback(null, true);
|
||||
});
|
||||
} else if(authMechanism == 'PLAIN') {
|
||||
self.s.topology.auth('plain', authdb, username, password, function(err) {
|
||||
if(err) return handleCallback(callback, err, false);
|
||||
_callback(null, true);
|
||||
});
|
||||
} else if(authMechanism == 'MONGODB-X509') {
|
||||
self.s.topology.auth('x509', authdb, username, password, function(err) {
|
||||
if(err) return handleCallback(callback, err, false);
|
||||
_callback(null, true);
|
||||
});
|
||||
} else if(authMechanism == 'SCRAM-SHA-1') {
|
||||
self.s.topology.auth('scram-sha-1', authdb, username, password, function(err) {
|
||||
if(err) return handleCallback(callback, err, false);
|
||||
_callback(null, true);
|
||||
});
|
||||
} else if(authMechanism == 'GSSAPI') {
|
||||
if(process.platform == 'win32') {
|
||||
self.s.topology.auth('sspi', authdb, username, password, options, function(err) {
|
||||
if(err) return handleCallback(callback, err, false);
|
||||
_callback(null, true);
|
||||
});
|
||||
} else {
|
||||
self.s.topology.auth('gssapi', authdb, username, password, options, function(err) {
|
||||
if(err) return handleCallback(callback, err, false);
|
||||
_callback(null, true);
|
||||
});
|
||||
}
|
||||
} else if(authMechanism == 'DEFAULT') {
|
||||
self.s.topology.auth('default', authdb, username, password, function(err) {
|
||||
if(err) return handleCallback(callback, err, false);
|
||||
_callback(null, true);
|
||||
});
|
||||
} else {
|
||||
handleCallback(callback, MongoError.create({message: f("authentication mechanism %s not supported", options.authMechanism), driver:true}));
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = function(self, username, password, options, callback) {
|
||||
if(typeof options == 'function') callback = options, options = {};
|
||||
// Shallow copy the options
|
||||
options = shallowClone(options);
|
||||
|
||||
// Set default mechanism
|
||||
if(!options.authMechanism) {
|
||||
options.authMechanism = 'DEFAULT';
|
||||
} else if(options.authMechanism != 'GSSAPI'
|
||||
&& options.authMechanism != 'DEFAULT'
|
||||
&& options.authMechanism != 'MONGODB-CR'
|
||||
&& options.authMechanism != 'MONGODB-X509'
|
||||
&& options.authMechanism != 'SCRAM-SHA-1'
|
||||
&& options.authMechanism != 'PLAIN') {
|
||||
return handleCallback(callback, MongoError.create({message: "only DEFAULT, GSSAPI, PLAIN, MONGODB-X509, SCRAM-SHA-1 or MONGODB-CR is supported by authMechanism", driver:true}));
|
||||
}
|
||||
|
||||
// If we have a callback fallback
|
||||
if(typeof callback == 'function') return authenticate(self, username, password, options, function(err, r) {
|
||||
// Support failed auth method
|
||||
if(err && err.message && err.message.indexOf('saslStart') != -1) err.code = 59;
|
||||
// Reject error
|
||||
if(err) return callback(err, r);
|
||||
callback(null, r);
|
||||
});
|
||||
|
||||
// Return a promise
|
||||
return new self.s.promiseLibrary(function(resolve, reject) {
|
||||
authenticate(self, username, password, options, function(err, r) {
|
||||
// Support failed auth method
|
||||
if(err && err.message && err.message.indexOf('saslStart') != -1) err.code = 59;
|
||||
// Reject error
|
||||
if(err) return reject(err);
|
||||
resolve(r);
|
||||
});
|
||||
});
|
||||
};
|
||||
+440
@@ -0,0 +1,440 @@
|
||||
"use strict";
|
||||
|
||||
var Long = require('mongodb-core').BSON.Long,
|
||||
Timestamp = require('mongodb-core').BSON.Timestamp;
|
||||
|
||||
// Error codes
|
||||
var UNKNOWN_ERROR = 8;
|
||||
var INVALID_BSON_ERROR = 22;
|
||||
var WRITE_CONCERN_ERROR = 64;
|
||||
var MULTIPLE_ERROR = 65;
|
||||
|
||||
// Insert types
|
||||
var INSERT = 1;
|
||||
var UPDATE = 2;
|
||||
var REMOVE = 3
|
||||
|
||||
|
||||
// Get write concern
|
||||
var writeConcern = function(target, col, options) {
|
||||
var writeConcern = {};
|
||||
|
||||
// Collection level write concern
|
||||
if(col.writeConcern && col.writeConcern.w != null) writeConcern.w = col.writeConcern.w;
|
||||
if(col.writeConcern && col.writeConcern.j != null) writeConcern.j = col.writeConcern.j;
|
||||
if(col.writeConcern && col.writeConcern.fsync != null) writeConcern.fsync = col.writeConcern.fsync;
|
||||
if(col.writeConcern && col.writeConcern.wtimeout != null) writeConcern.wtimeout = col.writeConcern.wtimeout;
|
||||
|
||||
// Options level write concern
|
||||
if(options && options.w != null) writeConcern.w = options.w;
|
||||
if(options && options.wtimeout != null) writeConcern.wtimeout = options.wtimeout;
|
||||
if(options && options.j != null) writeConcern.j = options.j;
|
||||
if(options && options.fsync != null) writeConcern.fsync = options.fsync;
|
||||
|
||||
// Return write concern
|
||||
return writeConcern;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to define properties
|
||||
* @ignore
|
||||
*/
|
||||
var defineReadOnlyProperty = function(self, name, value) {
|
||||
Object.defineProperty(self, name, {
|
||||
enumerable: true
|
||||
, get: function() {
|
||||
return value;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Keeps the state of a unordered batch so we can rewrite the results
|
||||
* correctly after command execution
|
||||
* @ignore
|
||||
*/
|
||||
var Batch = function(batchType, originalZeroIndex) {
|
||||
this.originalZeroIndex = originalZeroIndex;
|
||||
this.currentIndex = 0;
|
||||
this.originalIndexes = [];
|
||||
this.batchType = batchType;
|
||||
this.operations = [];
|
||||
this.size = 0;
|
||||
this.sizeBytes = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps a legacy operation so we can correctly rewrite it's error
|
||||
* @ignore
|
||||
*/
|
||||
var LegacyOp = function(batchType, operation, index) {
|
||||
this.batchType = batchType;
|
||||
this.index = index;
|
||||
this.operation = operation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new BulkWriteResult instance (INTERNAL TYPE, do not instantiate directly)
|
||||
*
|
||||
* @class
|
||||
* @property {boolean} ok Did bulk operation correctly execute
|
||||
* @property {number} nInserted number of inserted documents
|
||||
* @property {number} nUpdated number of documents updated logically
|
||||
* @property {number} nUpserted Number of upserted documents
|
||||
* @property {number} nModified Number of documents updated physically on disk
|
||||
* @property {number} nRemoved Number of removed documents
|
||||
* @return {BulkWriteResult} a BulkWriteResult instance
|
||||
*/
|
||||
var BulkWriteResult = function(bulkResult) {
|
||||
defineReadOnlyProperty(this, "ok", bulkResult.ok);
|
||||
defineReadOnlyProperty(this, "nInserted", bulkResult.nInserted);
|
||||
defineReadOnlyProperty(this, "nUpserted", bulkResult.nUpserted);
|
||||
defineReadOnlyProperty(this, "nMatched", bulkResult.nMatched);
|
||||
defineReadOnlyProperty(this, "nModified", bulkResult.nModified);
|
||||
defineReadOnlyProperty(this, "nRemoved", bulkResult.nRemoved);
|
||||
|
||||
/**
|
||||
* Return an array of inserted ids
|
||||
*
|
||||
* @return {object[]}
|
||||
*/
|
||||
this.getInsertedIds = function() {
|
||||
return bulkResult.insertedIds;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of upserted ids
|
||||
*
|
||||
* @return {object[]}
|
||||
*/
|
||||
this.getUpsertedIds = function() {
|
||||
return bulkResult.upserted;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the upserted id at position x
|
||||
*
|
||||
* @param {number} index the number of the upserted id to return, returns undefined if no result for passed in index
|
||||
* @return {object}
|
||||
*/
|
||||
this.getUpsertedIdAt = function(index) {
|
||||
return bulkResult.upserted[index];
|
||||
}
|
||||
|
||||
/**
|
||||
* Return raw internal result
|
||||
*
|
||||
* @return {object}
|
||||
*/
|
||||
this.getRawResponse = function() {
|
||||
return bulkResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the bulk operation contains a write error
|
||||
*
|
||||
* @return {boolean}
|
||||
*/
|
||||
this.hasWriteErrors = function() {
|
||||
return bulkResult.writeErrors.length > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of write errors off the bulk operation
|
||||
*
|
||||
* @return {number}
|
||||
*/
|
||||
this.getWriteErrorCount = function() {
|
||||
return bulkResult.writeErrors.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a specific write error object
|
||||
*
|
||||
* @param {number} index of the write error to return, returns null if there is no result for passed in index
|
||||
* @return {WriteError}
|
||||
*/
|
||||
this.getWriteErrorAt = function(index) {
|
||||
if(index < bulkResult.writeErrors.length) {
|
||||
return bulkResult.writeErrors[index];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve all write errors
|
||||
*
|
||||
* @return {object[]}
|
||||
*/
|
||||
this.getWriteErrors = function() {
|
||||
return bulkResult.writeErrors;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve lastOp if available
|
||||
*
|
||||
* @return {object}
|
||||
*/
|
||||
this.getLastOp = function() {
|
||||
return bulkResult.lastOp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the write concern error if any
|
||||
*
|
||||
* @return {WriteConcernError}
|
||||
*/
|
||||
this.getWriteConcernError = function() {
|
||||
if(bulkResult.writeConcernErrors.length == 0) {
|
||||
return null;
|
||||
} else if(bulkResult.writeConcernErrors.length == 1) {
|
||||
// Return the error
|
||||
return bulkResult.writeConcernErrors[0];
|
||||
} else {
|
||||
|
||||
// Combine the errors
|
||||
var errmsg = "";
|
||||
for(var i = 0; i < bulkResult.writeConcernErrors.length; i++) {
|
||||
var err = bulkResult.writeConcernErrors[i];
|
||||
errmsg = errmsg + err.errmsg;
|
||||
|
||||
// TODO: Something better
|
||||
if(i == 0) errmsg = errmsg + " and ";
|
||||
}
|
||||
|
||||
return new WriteConcernError({ errmsg : errmsg, code : WRITE_CONCERN_ERROR });
|
||||
}
|
||||
}
|
||||
|
||||
this.toJSON = function() {
|
||||
return bulkResult;
|
||||
}
|
||||
|
||||
this.toString = function() {
|
||||
return "BulkWriteResult(" + this.toJSON(bulkResult) + ")";
|
||||
}
|
||||
|
||||
this.isOk = function() {
|
||||
return bulkResult.ok == 1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new WriteConcernError instance (INTERNAL TYPE, do not instantiate directly)
|
||||
*
|
||||
* @class
|
||||
* @property {number} code Write concern error code.
|
||||
* @property {string} errmsg Write concern error message.
|
||||
* @return {WriteConcernError} a WriteConcernError instance
|
||||
*/
|
||||
var WriteConcernError = function(err) {
|
||||
if(!(this instanceof WriteConcernError)) return new WriteConcernError(err);
|
||||
|
||||
// Define properties
|
||||
defineReadOnlyProperty(this, "code", err.code);
|
||||
defineReadOnlyProperty(this, "errmsg", err.errmsg);
|
||||
|
||||
this.toJSON = function() {
|
||||
return {code: err.code, errmsg: err.errmsg};
|
||||
}
|
||||
|
||||
this.toString = function() {
|
||||
return "WriteConcernError(" + err.errmsg + ")";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new WriteError instance (INTERNAL TYPE, do not instantiate directly)
|
||||
*
|
||||
* @class
|
||||
* @property {number} code Write concern error code.
|
||||
* @property {number} index Write concern error original bulk operation index.
|
||||
* @property {string} errmsg Write concern error message.
|
||||
* @return {WriteConcernError} a WriteConcernError instance
|
||||
*/
|
||||
var WriteError = function(err) {
|
||||
if(!(this instanceof WriteError)) return new WriteError(err);
|
||||
|
||||
// Define properties
|
||||
defineReadOnlyProperty(this, "code", err.code);
|
||||
defineReadOnlyProperty(this, "index", err.index);
|
||||
defineReadOnlyProperty(this, "errmsg", err.errmsg);
|
||||
|
||||
//
|
||||
// Define access methods
|
||||
this.getOperation = function() {
|
||||
return err.op;
|
||||
}
|
||||
|
||||
this.toJSON = function() {
|
||||
return {code: err.code, index: err.index, errmsg: err.errmsg, op: err.op};
|
||||
}
|
||||
|
||||
this.toString = function() {
|
||||
return "WriteError(" + JSON.stringify(this.toJSON()) + ")";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges results into shared data structure
|
||||
* @ignore
|
||||
*/
|
||||
var mergeBatchResults = function(ordered, batch, bulkResult, err, result) {
|
||||
// If we have an error set the result to be the err object
|
||||
if(err) {
|
||||
result = err;
|
||||
} else if(result && result.result) {
|
||||
result = result.result;
|
||||
} else if(result == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Do we have a top level error stop processing and return
|
||||
if(result.ok == 0 && bulkResult.ok == 1) {
|
||||
bulkResult.ok = 0;
|
||||
|
||||
var writeError = {
|
||||
index: 0
|
||||
, code: result.code || 0
|
||||
, errmsg: result.message
|
||||
, op: batch.operations[0]
|
||||
};
|
||||
|
||||
bulkResult.writeErrors.push(new WriteError(writeError));
|
||||
return;
|
||||
} else if(result.ok == 0 && bulkResult.ok == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Deal with opTime if available
|
||||
if(result.opTime || result.lastOp) {
|
||||
var opTime = result.lastOp || result.opTime;
|
||||
var lastOpTS = null;
|
||||
var lastOpT = null;
|
||||
|
||||
// We have a time stamp
|
||||
if(opTime && opTime._bsontype == 'Timestamp') {
|
||||
if(bulkResult.lastOp == null) {
|
||||
bulkResult.lastOp = opTime;
|
||||
} else if(opTime.greaterThan(bulkResult.lastOp)) {
|
||||
bulkResult.lastOp = opTime;
|
||||
}
|
||||
} else {
|
||||
// Existing TS
|
||||
if(bulkResult.lastOp) {
|
||||
lastOpTS = typeof bulkResult.lastOp.ts == 'number'
|
||||
? Long.fromNumber(bulkResult.lastOp.ts) : bulkResult.lastOp.ts;
|
||||
lastOpT = typeof bulkResult.lastOp.t == 'number'
|
||||
? Long.fromNumber(bulkResult.lastOp.t) : bulkResult.lastOp.t;
|
||||
}
|
||||
|
||||
// Current OpTime TS
|
||||
var opTimeTS = typeof opTime.ts == 'number'
|
||||
? Long.fromNumber(opTime.ts) : opTime.ts;
|
||||
var opTimeT = typeof opTime.t == 'number'
|
||||
? Long.fromNumber(opTime.t) : opTime.t;
|
||||
|
||||
// Compare the opTime's
|
||||
if(bulkResult.lastOp == null) {
|
||||
bulkResult.lastOp = opTime;
|
||||
} else if(opTimeTS.greaterThan(lastOpTS)) {
|
||||
bulkResult.lastOp = opTime;
|
||||
} else if(opTimeTS.equals(lastOpTS)) {
|
||||
if(opTimeT.greaterThan(lastOpT)) {
|
||||
bulkResult.lastOp = opTime;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If we have an insert Batch type
|
||||
if(batch.batchType == INSERT && result.n) {
|
||||
bulkResult.nInserted = bulkResult.nInserted + result.n;
|
||||
}
|
||||
|
||||
// If we have an insert Batch type
|
||||
if(batch.batchType == REMOVE && result.n) {
|
||||
bulkResult.nRemoved = bulkResult.nRemoved + result.n;
|
||||
}
|
||||
|
||||
var nUpserted = 0;
|
||||
|
||||
// We have an array of upserted values, we need to rewrite the indexes
|
||||
if(Array.isArray(result.upserted)) {
|
||||
nUpserted = result.upserted.length;
|
||||
|
||||
for(var i = 0; i < result.upserted.length; i++) {
|
||||
bulkResult.upserted.push({
|
||||
index: result.upserted[i].index + batch.originalZeroIndex
|
||||
, _id: result.upserted[i]._id
|
||||
});
|
||||
}
|
||||
} else if(result.upserted) {
|
||||
|
||||
nUpserted = 1;
|
||||
|
||||
bulkResult.upserted.push({
|
||||
index: batch.originalZeroIndex
|
||||
, _id: result.upserted
|
||||
});
|
||||
}
|
||||
|
||||
// If we have an update Batch type
|
||||
if(batch.batchType == UPDATE && result.n) {
|
||||
var nModified = result.nModified;
|
||||
bulkResult.nUpserted = bulkResult.nUpserted + nUpserted;
|
||||
bulkResult.nMatched = bulkResult.nMatched + (result.n - nUpserted);
|
||||
|
||||
if(typeof nModified == 'number') {
|
||||
bulkResult.nModified = bulkResult.nModified + nModified;
|
||||
} else {
|
||||
bulkResult.nModified = null;
|
||||
}
|
||||
}
|
||||
|
||||
if(Array.isArray(result.writeErrors)) {
|
||||
for(i = 0; i < result.writeErrors.length; i++) {
|
||||
|
||||
writeError = {
|
||||
index: batch.originalZeroIndex + result.writeErrors[i].index
|
||||
, code: result.writeErrors[i].code
|
||||
, errmsg: result.writeErrors[i].errmsg
|
||||
, op: batch.operations[result.writeErrors[i].index]
|
||||
};
|
||||
|
||||
bulkResult.writeErrors.push(new WriteError(writeError));
|
||||
}
|
||||
}
|
||||
|
||||
if(result.writeConcernError) {
|
||||
bulkResult.writeConcernErrors.push(new WriteConcernError(result.writeConcernError));
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Clone the options
|
||||
var cloneOptions = function(options) {
|
||||
var clone = {};
|
||||
var keys = Object.keys(options);
|
||||
for(var i = 0; i < keys.length; i++) {
|
||||
clone[keys[i]] = options[keys[i]];
|
||||
}
|
||||
|
||||
return clone;
|
||||
}
|
||||
|
||||
// Exports symbols
|
||||
exports.BulkWriteResult = BulkWriteResult;
|
||||
exports.WriteError = WriteError;
|
||||
exports.Batch = Batch;
|
||||
exports.LegacyOp = LegacyOp;
|
||||
exports.mergeBatchResults = mergeBatchResults;
|
||||
exports.cloneOptions = cloneOptions;
|
||||
exports.writeConcern = writeConcern;
|
||||
exports.INVALID_BSON_ERROR = INVALID_BSON_ERROR;
|
||||
exports.WRITE_CONCERN_ERROR = WRITE_CONCERN_ERROR;
|
||||
exports.MULTIPLE_ERROR = MULTIPLE_ERROR;
|
||||
exports.UNKNOWN_ERROR = UNKNOWN_ERROR;
|
||||
exports.INSERT = INSERT;
|
||||
exports.UPDATE = UPDATE;
|
||||
exports.REMOVE = REMOVE;
|
||||
+539
@@ -0,0 +1,539 @@
|
||||
"use strict";
|
||||
|
||||
var common = require('./common')
|
||||
, utils = require('../utils')
|
||||
, toError = require('../utils').toError
|
||||
, handleCallback = require('../utils').handleCallback
|
||||
, shallowClone = utils.shallowClone
|
||||
, BulkWriteResult = common.BulkWriteResult
|
||||
, ObjectID = require('mongodb-core').BSON.ObjectID
|
||||
, Define = require('../metadata')
|
||||
, BSON = require('mongodb-core').BSON
|
||||
, Batch = common.Batch
|
||||
, mergeBatchResults = common.mergeBatchResults;
|
||||
|
||||
var bson = new BSON([BSON.Binary, BSON.Code, BSON.DBRef, BSON.Decimal128,
|
||||
BSON.Double, BSON.Int32, BSON.Long, BSON.Map, BSON.MaxKey, BSON.MinKey,
|
||||
BSON.ObjectId, BSON.BSONRegExp, BSON.Symbol, BSON.Timestamp]);
|
||||
|
||||
/**
|
||||
* Create a FindOperatorsOrdered instance (INTERNAL TYPE, do not instantiate directly)
|
||||
* @class
|
||||
* @return {FindOperatorsOrdered} a FindOperatorsOrdered instance.
|
||||
*/
|
||||
var FindOperatorsOrdered = function(self) {
|
||||
this.s = self.s;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a single update document to the bulk operation
|
||||
*
|
||||
* @method
|
||||
* @param {object} doc update operations
|
||||
* @throws {MongoError}
|
||||
* @return {OrderedBulkOperation}
|
||||
*/
|
||||
FindOperatorsOrdered.prototype.update = function(updateDocument) {
|
||||
// Perform upsert
|
||||
var upsert = typeof this.s.currentOp.upsert == 'boolean' ? this.s.currentOp.upsert : false;
|
||||
|
||||
// Establish the update command
|
||||
var document = {
|
||||
q: this.s.currentOp.selector
|
||||
, u: updateDocument
|
||||
, multi: true
|
||||
, upsert: upsert
|
||||
}
|
||||
|
||||
// Clear out current Op
|
||||
this.s.currentOp = null;
|
||||
// Add the update document to the list
|
||||
return addToOperationsList(this, common.UPDATE, document);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a single update one document to the bulk operation
|
||||
*
|
||||
* @method
|
||||
* @param {object} doc update operations
|
||||
* @throws {MongoError}
|
||||
* @return {OrderedBulkOperation}
|
||||
*/
|
||||
FindOperatorsOrdered.prototype.updateOne = function(updateDocument) {
|
||||
// Perform upsert
|
||||
var upsert = typeof this.s.currentOp.upsert == 'boolean' ? this.s.currentOp.upsert : false;
|
||||
|
||||
// Establish the update command
|
||||
var document = {
|
||||
q: this.s.currentOp.selector
|
||||
, u: updateDocument
|
||||
, multi: false
|
||||
, upsert: upsert
|
||||
}
|
||||
|
||||
// Clear out current Op
|
||||
this.s.currentOp = null;
|
||||
// Add the update document to the list
|
||||
return addToOperationsList(this, common.UPDATE, document);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a replace one operation to the bulk operation
|
||||
*
|
||||
* @method
|
||||
* @param {object} doc the new document to replace the existing one with
|
||||
* @throws {MongoError}
|
||||
* @return {OrderedBulkOperation}
|
||||
*/
|
||||
FindOperatorsOrdered.prototype.replaceOne = function(updateDocument) {
|
||||
this.updateOne(updateDocument);
|
||||
}
|
||||
|
||||
/**
|
||||
* Upsert modifier for update bulk operation
|
||||
*
|
||||
* @method
|
||||
* @throws {MongoError}
|
||||
* @return {FindOperatorsOrdered}
|
||||
*/
|
||||
FindOperatorsOrdered.prototype.upsert = function() {
|
||||
this.s.currentOp.upsert = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a remove one operation to the bulk operation
|
||||
*
|
||||
* @method
|
||||
* @throws {MongoError}
|
||||
* @return {OrderedBulkOperation}
|
||||
*/
|
||||
FindOperatorsOrdered.prototype.deleteOne = function() {
|
||||
// Establish the update command
|
||||
var document = {
|
||||
q: this.s.currentOp.selector
|
||||
, limit: 1
|
||||
}
|
||||
|
||||
// Clear out current Op
|
||||
this.s.currentOp = null;
|
||||
// Add the remove document to the list
|
||||
return addToOperationsList(this, common.REMOVE, document);
|
||||
}
|
||||
|
||||
// Backward compatibility
|
||||
FindOperatorsOrdered.prototype.removeOne = FindOperatorsOrdered.prototype.deleteOne;
|
||||
|
||||
/**
|
||||
* Add a remove operation to the bulk operation
|
||||
*
|
||||
* @method
|
||||
* @throws {MongoError}
|
||||
* @return {OrderedBulkOperation}
|
||||
*/
|
||||
FindOperatorsOrdered.prototype.delete = function() {
|
||||
// Establish the update command
|
||||
var document = {
|
||||
q: this.s.currentOp.selector
|
||||
, limit: 0
|
||||
}
|
||||
|
||||
// Clear out current Op
|
||||
this.s.currentOp = null;
|
||||
// Add the remove document to the list
|
||||
return addToOperationsList(this, common.REMOVE, document);
|
||||
}
|
||||
|
||||
// Backward compatibility
|
||||
FindOperatorsOrdered.prototype.remove = FindOperatorsOrdered.prototype.delete;
|
||||
|
||||
// Add to internal list of documents
|
||||
var addToOperationsList = function(_self, docType, document) {
|
||||
// Get the bsonSize
|
||||
var bsonSize = bson.calculateObjectSize(document, {
|
||||
checkKeys: false,
|
||||
});
|
||||
|
||||
// Throw error if the doc is bigger than the max BSON size
|
||||
if(bsonSize >= _self.s.maxBatchSizeBytes) {
|
||||
throw toError("document is larger than the maximum size " + _self.s.maxBatchSizeBytes);
|
||||
}
|
||||
|
||||
// Create a new batch object if we don't have a current one
|
||||
if(_self.s.currentBatch == null) _self.s.currentBatch = new Batch(docType, _self.s.currentIndex);
|
||||
|
||||
// Check if we need to create a new batch
|
||||
if(((_self.s.currentBatchSize + 1) >= _self.s.maxWriteBatchSize)
|
||||
|| ((_self.s.currentBatchSizeBytes + _self.s.currentBatchSizeBytes) >= _self.s.maxBatchSizeBytes)
|
||||
|| (_self.s.currentBatch.batchType != docType)) {
|
||||
// Save the batch to the execution stack
|
||||
_self.s.batches.push(_self.s.currentBatch);
|
||||
|
||||
// Create a new batch
|
||||
_self.s.currentBatch = new Batch(docType, _self.s.currentIndex);
|
||||
|
||||
// Reset the current size trackers
|
||||
_self.s.currentBatchSize = 0;
|
||||
_self.s.currentBatchSizeBytes = 0;
|
||||
} else {
|
||||
// Update current batch size
|
||||
_self.s.currentBatchSize = _self.s.currentBatchSize + 1;
|
||||
_self.s.currentBatchSizeBytes = _self.s.currentBatchSizeBytes + bsonSize;
|
||||
}
|
||||
|
||||
if(docType == common.INSERT) {
|
||||
_self.s.bulkResult.insertedIds.push({index: _self.s.currentIndex, _id: document._id});
|
||||
}
|
||||
|
||||
// We have an array of documents
|
||||
if(Array.isArray(document)) {
|
||||
throw toError("operation passed in cannot be an Array");
|
||||
} else {
|
||||
_self.s.currentBatch.originalIndexes.push(_self.s.currentIndex);
|
||||
_self.s.currentBatch.operations.push(document)
|
||||
_self.s.currentBatchSizeBytes = _self.s.currentBatchSizeBytes + bsonSize;
|
||||
_self.s.currentIndex = _self.s.currentIndex + 1;
|
||||
}
|
||||
|
||||
// Return self
|
||||
return _self;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new OrderedBulkOperation instance (INTERNAL TYPE, do not instantiate directly)
|
||||
* @class
|
||||
* @property {number} length Get the number of operations in the bulk.
|
||||
* @return {OrderedBulkOperation} a OrderedBulkOperation instance.
|
||||
*/
|
||||
function OrderedBulkOperation(topology, collection, options) {
|
||||
options = options == null ? {} : options;
|
||||
// TODO Bring from driver information in isMaster
|
||||
var executed = false;
|
||||
|
||||
// Current item
|
||||
var currentOp = null;
|
||||
|
||||
// Handle to the bson serializer, used to calculate running sizes
|
||||
var bson = topology.bson;
|
||||
|
||||
// Namespace for the operation
|
||||
var namespace = collection.collectionName;
|
||||
|
||||
// Set max byte size
|
||||
var maxBatchSizeBytes = topology.isMasterDoc && topology.isMasterDoc.maxBsonObjectSize
|
||||
? topology.isMasterDoc.maxBsonObjectSize : (1024*1025*16);
|
||||
var maxWriteBatchSize = topology.isMasterDoc && topology.isMasterDoc.maxWriteBatchSize
|
||||
? topology.isMasterDoc.maxWriteBatchSize : 1000;
|
||||
|
||||
// Get the write concern
|
||||
var writeConcern = common.writeConcern(shallowClone(options), collection, options);
|
||||
|
||||
// Get the promiseLibrary
|
||||
var promiseLibrary = options.promiseLibrary;
|
||||
|
||||
// No promise library selected fall back
|
||||
if(!promiseLibrary) {
|
||||
promiseLibrary = typeof global.Promise == 'function' ?
|
||||
global.Promise : require('es6-promise').Promise;
|
||||
}
|
||||
|
||||
// Final results
|
||||
var bulkResult = {
|
||||
ok: 1
|
||||
, writeErrors: []
|
||||
, writeConcernErrors: []
|
||||
, insertedIds: []
|
||||
, nInserted: 0
|
||||
, nUpserted: 0
|
||||
, nMatched: 0
|
||||
, nModified: 0
|
||||
, nRemoved: 0
|
||||
, upserted: []
|
||||
};
|
||||
|
||||
// Internal state
|
||||
this.s = {
|
||||
// Final result
|
||||
bulkResult: bulkResult
|
||||
// Current batch state
|
||||
, currentBatch: null
|
||||
, currentIndex: 0
|
||||
, currentBatchSize: 0
|
||||
, currentBatchSizeBytes: 0
|
||||
, batches: []
|
||||
// Write concern
|
||||
, writeConcern: writeConcern
|
||||
// Max batch size options
|
||||
, maxBatchSizeBytes: maxBatchSizeBytes
|
||||
, maxWriteBatchSize: maxWriteBatchSize
|
||||
// Namespace
|
||||
, namespace: namespace
|
||||
// BSON
|
||||
, bson: bson
|
||||
// Topology
|
||||
, topology: topology
|
||||
// Options
|
||||
, options: options
|
||||
// Current operation
|
||||
, currentOp: currentOp
|
||||
// Executed
|
||||
, executed: executed
|
||||
// Collection
|
||||
, collection: collection
|
||||
// Promise Library
|
||||
, promiseLibrary: promiseLibrary
|
||||
// Fundamental error
|
||||
, err: null
|
||||
// Bypass validation
|
||||
, bypassDocumentValidation: typeof options.bypassDocumentValidation == 'boolean' ? options.bypassDocumentValidation : false
|
||||
}
|
||||
}
|
||||
|
||||
var define = OrderedBulkOperation.define = new Define('OrderedBulkOperation', OrderedBulkOperation, false);
|
||||
|
||||
OrderedBulkOperation.prototype.raw = function(op) {
|
||||
var key = Object.keys(op)[0];
|
||||
|
||||
// Set up the force server object id
|
||||
var forceServerObjectId = typeof this.s.options.forceServerObjectId == 'boolean'
|
||||
? this.s.options.forceServerObjectId : this.s.collection.s.db.options.forceServerObjectId;
|
||||
|
||||
// Update operations
|
||||
if((op.updateOne && op.updateOne.q)
|
||||
|| (op.updateMany && op.updateMany.q)
|
||||
|| (op.replaceOne && op.replaceOne.q)) {
|
||||
op[key].multi = op.updateOne || op.replaceOne ? false : true;
|
||||
return addToOperationsList(this, common.UPDATE, op[key]);
|
||||
}
|
||||
|
||||
// Crud spec update format
|
||||
if(op.updateOne || op.updateMany || op.replaceOne) {
|
||||
var multi = op.updateOne || op.replaceOne ? false : true;
|
||||
var operation = {q: op[key].filter, u: op[key].update || op[key].replacement, multi: multi}
|
||||
operation.upsert = op[key].upsert ? true: false;
|
||||
if(op.collation) operation.collation = op.collation;
|
||||
return addToOperationsList(this, common.UPDATE, operation);
|
||||
}
|
||||
|
||||
// Remove operations
|
||||
if(op.removeOne || op.removeMany || (op.deleteOne && op.deleteOne.q) || op.deleteMany && op.deleteMany.q) {
|
||||
op[key].limit = op.removeOne ? 1 : 0;
|
||||
return addToOperationsList(this, common.REMOVE, op[key]);
|
||||
}
|
||||
|
||||
// Crud spec delete operations, less efficient
|
||||
if(op.deleteOne || op.deleteMany) {
|
||||
var limit = op.deleteOne ? 1 : 0;
|
||||
operation = {q: op[key].filter, limit: limit}
|
||||
if(op.collation) operation.collation = op.collation;
|
||||
return addToOperationsList(this, common.REMOVE, operation);
|
||||
}
|
||||
|
||||
// Insert operations
|
||||
if(op.insertOne && op.insertOne.document == null) {
|
||||
if(forceServerObjectId !== true && op.insertOne._id == null) op.insertOne._id = new ObjectID();
|
||||
return addToOperationsList(this, common.INSERT, op.insertOne);
|
||||
} else if(op.insertOne && op.insertOne.document) {
|
||||
if(forceServerObjectId !== true && op.insertOne.document._id == null) op.insertOne.document._id = new ObjectID();
|
||||
return addToOperationsList(this, common.INSERT, op.insertOne.document);
|
||||
}
|
||||
|
||||
if(op.insertMany) {
|
||||
for(var i = 0; i < op.insertMany.length; i++) {
|
||||
if(forceServerObjectId !== true && op.insertMany[i]._id == null) op.insertMany[i]._id = new ObjectID();
|
||||
addToOperationsList(this, common.INSERT, op.insertMany[i]);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// No valid type of operation
|
||||
throw toError("bulkWrite only supports insertOne, insertMany, updateOne, updateMany, removeOne, removeMany, deleteOne, deleteMany");
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a single insert document to the bulk operation
|
||||
*
|
||||
* @param {object} doc the document to insert
|
||||
* @throws {MongoError}
|
||||
* @return {OrderedBulkOperation}
|
||||
*/
|
||||
OrderedBulkOperation.prototype.insert = function(document) {
|
||||
if(this.s.collection.s.db.options.forceServerObjectId !== true && document._id == null) document._id = new ObjectID();
|
||||
return addToOperationsList(this, common.INSERT, document);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initiate a find operation for an update/updateOne/remove/removeOne/replaceOne
|
||||
*
|
||||
* @method
|
||||
* @param {object} selector The selector for the bulk operation.
|
||||
* @throws {MongoError}
|
||||
* @return {FindOperatorsOrdered}
|
||||
*/
|
||||
OrderedBulkOperation.prototype.find = function(selector) {
|
||||
if (!selector) {
|
||||
throw toError("Bulk find operation must specify a selector");
|
||||
}
|
||||
|
||||
// Save a current selector
|
||||
this.s.currentOp = {
|
||||
selector: selector
|
||||
}
|
||||
|
||||
return new FindOperatorsOrdered(this);
|
||||
}
|
||||
|
||||
Object.defineProperty(OrderedBulkOperation.prototype, 'length', {
|
||||
enumerable: true,
|
||||
get: function() {
|
||||
return this.s.currentIndex;
|
||||
}
|
||||
});
|
||||
|
||||
//
|
||||
// Execute next write command in a chain
|
||||
var executeCommands = function(self, callback) {
|
||||
if(self.s.batches.length == 0) {
|
||||
return handleCallback(callback, null, new BulkWriteResult(self.s.bulkResult));
|
||||
}
|
||||
|
||||
// Ordered execution of the command
|
||||
var batch = self.s.batches.shift();
|
||||
|
||||
var resultHandler = function(err, result) {
|
||||
// Error is a driver related error not a bulk op error, terminate
|
||||
if(err && err.driver || err && err.message) {
|
||||
return handleCallback(callback, err);
|
||||
}
|
||||
|
||||
// If we have and error
|
||||
if(err) err.ok = 0;
|
||||
// Merge the results together
|
||||
var mergeResult = mergeBatchResults(true, batch, self.s.bulkResult, err, result);
|
||||
if(mergeResult != null) {
|
||||
return handleCallback(callback, null, new BulkWriteResult(self.s.bulkResult));
|
||||
}
|
||||
|
||||
// If we are ordered and have errors and they are
|
||||
// not all replication errors terminate the operation
|
||||
if(self.s.bulkResult.writeErrors.length > 0) {
|
||||
return handleCallback(callback, toError(self.s.bulkResult.writeErrors[0]), new BulkWriteResult(self.s.bulkResult));
|
||||
}
|
||||
|
||||
// Execute the next command in line
|
||||
executeCommands(self, callback);
|
||||
}
|
||||
|
||||
var finalOptions = {ordered: true}
|
||||
if(self.s.writeConcern != null) {
|
||||
finalOptions.writeConcern = self.s.writeConcern;
|
||||
}
|
||||
|
||||
// Set an operationIf if provided
|
||||
if(self.operationId) {
|
||||
resultHandler.operationId = self.operationId;
|
||||
}
|
||||
|
||||
// Serialize functions
|
||||
if(self.s.options.serializeFunctions) {
|
||||
finalOptions.serializeFunctions = true
|
||||
}
|
||||
|
||||
// Serialize functions
|
||||
if(self.s.options.ignoreUndefined) {
|
||||
finalOptions.ignoreUndefined = true
|
||||
}
|
||||
|
||||
// Is the bypassDocumentValidation options specific
|
||||
if(self.s.bypassDocumentValidation == true) {
|
||||
finalOptions.bypassDocumentValidation = true;
|
||||
}
|
||||
|
||||
try {
|
||||
if(batch.batchType == common.INSERT) {
|
||||
self.s.topology.insert(self.s.collection.namespace, batch.operations, finalOptions, resultHandler);
|
||||
} else if(batch.batchType == common.UPDATE) {
|
||||
self.s.topology.update(self.s.collection.namespace, batch.operations, finalOptions, resultHandler);
|
||||
} else if(batch.batchType == common.REMOVE) {
|
||||
self.s.topology.remove(self.s.collection.namespace, batch.operations, finalOptions, resultHandler);
|
||||
}
|
||||
} catch(err) {
|
||||
// Force top level error
|
||||
err.ok = 0;
|
||||
// Merge top level error and return
|
||||
handleCallback(callback, null, mergeBatchResults(false, batch, self.s.bulkResult, err, null));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The callback format for results
|
||||
* @callback OrderedBulkOperation~resultCallback
|
||||
* @param {MongoError} error An error instance representing the error during the execution.
|
||||
* @param {BulkWriteResult} result The bulk write result.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Execute the ordered bulk operation
|
||||
*
|
||||
* @method
|
||||
* @param {object} [options=null] Optional settings.
|
||||
* @param {(number|string)} [options.w=null] The write concern.
|
||||
* @param {number} [options.wtimeout=null] The write concern timeout.
|
||||
* @param {boolean} [options.j=false] Specify a journal write concern.
|
||||
* @param {boolean} [options.fsync=false] Specify a file sync write concern.
|
||||
* @param {OrderedBulkOperation~resultCallback} [callback] The result callback
|
||||
* @throws {MongoError}
|
||||
* @return {Promise} returns Promise if no callback passed
|
||||
*/
|
||||
OrderedBulkOperation.prototype.execute = function(_writeConcern, callback) {
|
||||
var self = this;
|
||||
if (this.s.executed) {
|
||||
var executedError = toError('batch cannot be re-executed');
|
||||
return (typeof callback === 'function') ?
|
||||
callback(executedError, null) : this.s.promiseLibrary.reject(executedError);
|
||||
}
|
||||
|
||||
if (typeof _writeConcern === 'function') {
|
||||
callback = _writeConcern;
|
||||
} else if (_writeConcern && typeof _writeConcern === 'object') {
|
||||
this.s.writeConcern = _writeConcern;
|
||||
}
|
||||
|
||||
// If we have current batch
|
||||
if (this.s.currentBatch) this.s.batches.push(this.s.currentBatch)
|
||||
|
||||
// If we have no operations in the bulk raise an error
|
||||
if (this.s.batches.length == 0) {
|
||||
var emptyBatchError = toError('Invalid Operation, no operations specified');
|
||||
return (typeof callback === 'function') ?
|
||||
callback(emptyBatchError, null) : this.s.promiseLibrary.reject(emptyBatchError);
|
||||
}
|
||||
|
||||
// Execute using callback
|
||||
if (typeof callback === 'function') {
|
||||
return executeCommands(this, callback);
|
||||
}
|
||||
|
||||
// Return a Promise
|
||||
return new this.s.promiseLibrary(function(resolve, reject) {
|
||||
executeCommands(self, function(err, r) {
|
||||
if (err) return reject(err);
|
||||
resolve(r);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
define.classMethod('execute', {callback: true, promise:false});
|
||||
|
||||
/**
|
||||
* Returns an unordered batch object
|
||||
* @ignore
|
||||
*/
|
||||
var initializeOrderedBulkOp = function(topology, collection, options) {
|
||||
return new OrderedBulkOperation(topology, collection, options);
|
||||
}
|
||||
|
||||
initializeOrderedBulkOp.OrderedBulkOperation = OrderedBulkOperation;
|
||||
module.exports = initializeOrderedBulkOp;
|
||||
module.exports.Bulk = OrderedBulkOperation;
|
||||
+541
@@ -0,0 +1,541 @@
|
||||
"use strict";
|
||||
|
||||
var common = require('./common')
|
||||
, utils = require('../utils')
|
||||
, toError = require('../utils').toError
|
||||
, handleCallback = require('../utils').handleCallback
|
||||
, shallowClone = utils.shallowClone
|
||||
, BulkWriteResult = common.BulkWriteResult
|
||||
, ObjectID = require('mongodb-core').BSON.ObjectID
|
||||
, BSON = require('mongodb-core').BSON
|
||||
, Define = require('../metadata')
|
||||
, Batch = common.Batch
|
||||
, mergeBatchResults = common.mergeBatchResults;
|
||||
|
||||
var bson = new BSON([BSON.Binary, BSON.Code, BSON.DBRef, BSON.Decimal128,
|
||||
BSON.Double, BSON.Int32, BSON.Long, BSON.Map, BSON.MaxKey, BSON.MinKey,
|
||||
BSON.ObjectId, BSON.BSONRegExp, BSON.Symbol, BSON.Timestamp]);
|
||||
|
||||
/**
|
||||
* Create a FindOperatorsUnordered instance (INTERNAL TYPE, do not instantiate directly)
|
||||
* @class
|
||||
* @property {number} length Get the number of operations in the bulk.
|
||||
* @return {FindOperatorsUnordered} a FindOperatorsUnordered instance.
|
||||
*/
|
||||
var FindOperatorsUnordered = function(self) {
|
||||
this.s = self.s;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a single update document to the bulk operation
|
||||
*
|
||||
* @method
|
||||
* @param {object} doc update operations
|
||||
* @throws {MongoError}
|
||||
* @return {UnorderedBulkOperation}
|
||||
*/
|
||||
FindOperatorsUnordered.prototype.update = function(updateDocument) {
|
||||
// Perform upsert
|
||||
var upsert = typeof this.s.currentOp.upsert == 'boolean' ? this.s.currentOp.upsert : false;
|
||||
|
||||
// Establish the update command
|
||||
var document = {
|
||||
q: this.s.currentOp.selector
|
||||
, u: updateDocument
|
||||
, multi: true
|
||||
, upsert: upsert
|
||||
}
|
||||
|
||||
// Clear out current Op
|
||||
this.s.currentOp = null;
|
||||
// Add the update document to the list
|
||||
return addToOperationsList(this, common.UPDATE, document);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a single update one document to the bulk operation
|
||||
*
|
||||
* @method
|
||||
* @param {object} doc update operations
|
||||
* @throws {MongoError}
|
||||
* @return {UnorderedBulkOperation}
|
||||
*/
|
||||
FindOperatorsUnordered.prototype.updateOne = function(updateDocument) {
|
||||
// Perform upsert
|
||||
var upsert = typeof this.s.currentOp.upsert == 'boolean' ? this.s.currentOp.upsert : false;
|
||||
|
||||
// Establish the update command
|
||||
var document = {
|
||||
q: this.s.currentOp.selector
|
||||
, u: updateDocument
|
||||
, multi: false
|
||||
, upsert: upsert
|
||||
}
|
||||
|
||||
// Clear out current Op
|
||||
this.s.currentOp = null;
|
||||
// Add the update document to the list
|
||||
return addToOperationsList(this, common.UPDATE, document);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a replace one operation to the bulk operation
|
||||
*
|
||||
* @method
|
||||
* @param {object} doc the new document to replace the existing one with
|
||||
* @throws {MongoError}
|
||||
* @return {UnorderedBulkOperation}
|
||||
*/
|
||||
FindOperatorsUnordered.prototype.replaceOne = function(updateDocument) {
|
||||
this.updateOne(updateDocument);
|
||||
}
|
||||
|
||||
/**
|
||||
* Upsert modifier for update bulk operation
|
||||
*
|
||||
* @method
|
||||
* @throws {MongoError}
|
||||
* @return {FindOperatorsUnordered}
|
||||
*/
|
||||
FindOperatorsUnordered.prototype.upsert = function() {
|
||||
this.s.currentOp.upsert = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a remove one operation to the bulk operation
|
||||
*
|
||||
* @method
|
||||
* @throws {MongoError}
|
||||
* @return {UnorderedBulkOperation}
|
||||
*/
|
||||
FindOperatorsUnordered.prototype.removeOne = function() {
|
||||
// Establish the update command
|
||||
var document = {
|
||||
q: this.s.currentOp.selector
|
||||
, limit: 1
|
||||
}
|
||||
|
||||
// Clear out current Op
|
||||
this.s.currentOp = null;
|
||||
// Add the remove document to the list
|
||||
return addToOperationsList(this, common.REMOVE, document);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a remove operation to the bulk operation
|
||||
*
|
||||
* @method
|
||||
* @throws {MongoError}
|
||||
* @return {UnorderedBulkOperation}
|
||||
*/
|
||||
FindOperatorsUnordered.prototype.remove = function() {
|
||||
// Establish the update command
|
||||
var document = {
|
||||
q: this.s.currentOp.selector
|
||||
, limit: 0
|
||||
}
|
||||
|
||||
// Clear out current Op
|
||||
this.s.currentOp = null;
|
||||
// Add the remove document to the list
|
||||
return addToOperationsList(this, common.REMOVE, document);
|
||||
}
|
||||
|
||||
//
|
||||
// Add to the operations list
|
||||
//
|
||||
var addToOperationsList = function(_self, docType, document) {
|
||||
// Get the bsonSize
|
||||
var bsonSize = bson.calculateObjectSize(document, {
|
||||
checkKeys: false,
|
||||
});
|
||||
// Throw error if the doc is bigger than the max BSON size
|
||||
if(bsonSize >= _self.s.maxBatchSizeBytes) throw toError("document is larger than the maximum size " + _self.s.maxBatchSizeBytes);
|
||||
// Holds the current batch
|
||||
_self.s.currentBatch = null;
|
||||
// Get the right type of batch
|
||||
if(docType == common.INSERT) {
|
||||
_self.s.currentBatch = _self.s.currentInsertBatch;
|
||||
} else if(docType == common.UPDATE) {
|
||||
_self.s.currentBatch = _self.s.currentUpdateBatch;
|
||||
} else if(docType == common.REMOVE) {
|
||||
_self.s.currentBatch = _self.s.currentRemoveBatch;
|
||||
}
|
||||
|
||||
// Create a new batch object if we don't have a current one
|
||||
if(_self.s.currentBatch == null) _self.s.currentBatch = new Batch(docType, _self.s.currentIndex);
|
||||
|
||||
// Check if we need to create a new batch
|
||||
if(((_self.s.currentBatch.size + 1) >= _self.s.maxWriteBatchSize)
|
||||
|| ((_self.s.currentBatch.sizeBytes + bsonSize) >= _self.s.maxBatchSizeBytes)
|
||||
|| (_self.s.currentBatch.batchType != docType)) {
|
||||
// Save the batch to the execution stack
|
||||
_self.s.batches.push(_self.s.currentBatch);
|
||||
|
||||
// Create a new batch
|
||||
_self.s.currentBatch = new Batch(docType, _self.s.currentIndex);
|
||||
}
|
||||
|
||||
// We have an array of documents
|
||||
if(Array.isArray(document)) {
|
||||
throw toError("operation passed in cannot be an Array");
|
||||
} else {
|
||||
_self.s.currentBatch.operations.push(document);
|
||||
_self.s.currentBatch.originalIndexes.push(_self.s.currentIndex);
|
||||
_self.s.currentIndex = _self.s.currentIndex + 1;
|
||||
}
|
||||
|
||||
// Save back the current Batch to the right type
|
||||
if(docType == common.INSERT) {
|
||||
_self.s.currentInsertBatch = _self.s.currentBatch;
|
||||
_self.s.bulkResult.insertedIds.push({index: _self.s.bulkResult.insertedIds.length, _id: document._id});
|
||||
} else if(docType == common.UPDATE) {
|
||||
_self.s.currentUpdateBatch = _self.s.currentBatch;
|
||||
} else if(docType == common.REMOVE) {
|
||||
_self.s.currentRemoveBatch = _self.s.currentBatch;
|
||||
}
|
||||
|
||||
// Update current batch size
|
||||
_self.s.currentBatch.size = _self.s.currentBatch.size + 1;
|
||||
_self.s.currentBatch.sizeBytes = _self.s.currentBatch.sizeBytes + bsonSize;
|
||||
|
||||
// Return self
|
||||
return _self;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new UnorderedBulkOperation instance (INTERNAL TYPE, do not instantiate directly)
|
||||
* @class
|
||||
* @property {number} length Get the number of operations in the bulk.
|
||||
* @return {UnorderedBulkOperation} a UnorderedBulkOperation instance.
|
||||
*/
|
||||
var UnorderedBulkOperation = function(topology, collection, options) {
|
||||
options = options == null ? {} : options;
|
||||
|
||||
// Get the namespace for the write operations
|
||||
var namespace = collection.collectionName;
|
||||
// Used to mark operation as executed
|
||||
var executed = false;
|
||||
|
||||
// Current item
|
||||
// var currentBatch = null;
|
||||
var currentOp = null;
|
||||
|
||||
// Handle to the bson serializer, used to calculate running sizes
|
||||
var bson = topology.bson;
|
||||
|
||||
// Set max byte size
|
||||
var maxBatchSizeBytes = topology.isMasterDoc && topology.isMasterDoc.maxBsonObjectSize
|
||||
? topology.isMasterDoc.maxBsonObjectSize : (1024*1025*16);
|
||||
var maxWriteBatchSize = topology.isMasterDoc && topology.isMasterDoc.maxWriteBatchSize
|
||||
? topology.isMasterDoc.maxWriteBatchSize : 1000;
|
||||
|
||||
// Get the write concern
|
||||
var writeConcern = common.writeConcern(shallowClone(options), collection, options);
|
||||
|
||||
// Get the promiseLibrary
|
||||
var promiseLibrary = options.promiseLibrary;
|
||||
|
||||
// No promise library selected fall back
|
||||
if(!promiseLibrary) {
|
||||
promiseLibrary = typeof global.Promise == 'function' ?
|
||||
global.Promise : require('es6-promise').Promise;
|
||||
}
|
||||
|
||||
// Final results
|
||||
var bulkResult = {
|
||||
ok: 1
|
||||
, writeErrors: []
|
||||
, writeConcernErrors: []
|
||||
, insertedIds: []
|
||||
, nInserted: 0
|
||||
, nUpserted: 0
|
||||
, nMatched: 0
|
||||
, nModified: 0
|
||||
, nRemoved: 0
|
||||
, upserted: []
|
||||
};
|
||||
|
||||
// Internal state
|
||||
this.s = {
|
||||
// Final result
|
||||
bulkResult: bulkResult
|
||||
// Current batch state
|
||||
, currentInsertBatch: null
|
||||
, currentUpdateBatch: null
|
||||
, currentRemoveBatch: null
|
||||
, currentBatch: null
|
||||
, currentIndex: 0
|
||||
, batches: []
|
||||
// Write concern
|
||||
, writeConcern: writeConcern
|
||||
// Max batch size options
|
||||
, maxBatchSizeBytes: maxBatchSizeBytes
|
||||
, maxWriteBatchSize: maxWriteBatchSize
|
||||
// Namespace
|
||||
, namespace: namespace
|
||||
// BSON
|
||||
, bson: bson
|
||||
// Topology
|
||||
, topology: topology
|
||||
// Options
|
||||
, options: options
|
||||
// Current operation
|
||||
, currentOp: currentOp
|
||||
// Executed
|
||||
, executed: executed
|
||||
// Collection
|
||||
, collection: collection
|
||||
// Promise Library
|
||||
, promiseLibrary: promiseLibrary
|
||||
// Bypass validation
|
||||
, bypassDocumentValidation: typeof options.bypassDocumentValidation == 'boolean' ? options.bypassDocumentValidation : false
|
||||
}
|
||||
}
|
||||
|
||||
var define = UnorderedBulkOperation.define = new Define('UnorderedBulkOperation', UnorderedBulkOperation, false);
|
||||
|
||||
/**
|
||||
* Add a single insert document to the bulk operation
|
||||
*
|
||||
* @param {object} doc the document to insert
|
||||
* @throws {MongoError}
|
||||
* @return {UnorderedBulkOperation}
|
||||
*/
|
||||
UnorderedBulkOperation.prototype.insert = function(document) {
|
||||
if(this.s.collection.s.db.options.forceServerObjectId !== true && document._id == null) document._id = new ObjectID();
|
||||
return addToOperationsList(this, common.INSERT, document);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initiate a find operation for an update/updateOne/remove/removeOne/replaceOne
|
||||
*
|
||||
* @method
|
||||
* @param {object} selector The selector for the bulk operation.
|
||||
* @throws {MongoError}
|
||||
* @return {FindOperatorsUnordered}
|
||||
*/
|
||||
UnorderedBulkOperation.prototype.find = function(selector) {
|
||||
if (!selector) {
|
||||
throw toError("Bulk find operation must specify a selector");
|
||||
}
|
||||
|
||||
// Save a current selector
|
||||
this.s.currentOp = {
|
||||
selector: selector
|
||||
}
|
||||
|
||||
return new FindOperatorsUnordered(this);
|
||||
}
|
||||
|
||||
Object.defineProperty(UnorderedBulkOperation.prototype, 'length', {
|
||||
enumerable: true,
|
||||
get: function() {
|
||||
return this.s.currentIndex;
|
||||
}
|
||||
});
|
||||
|
||||
UnorderedBulkOperation.prototype.raw = function(op) {
|
||||
var key = Object.keys(op)[0];
|
||||
|
||||
// Set up the force server object id
|
||||
var forceServerObjectId = typeof this.s.options.forceServerObjectId == 'boolean'
|
||||
? this.s.options.forceServerObjectId : this.s.collection.s.db.options.forceServerObjectId;
|
||||
|
||||
// Update operations
|
||||
if((op.updateOne && op.updateOne.q)
|
||||
|| (op.updateMany && op.updateMany.q)
|
||||
|| (op.replaceOne && op.replaceOne.q)) {
|
||||
op[key].multi = op.updateOne || op.replaceOne ? false : true;
|
||||
return addToOperationsList(this, common.UPDATE, op[key]);
|
||||
}
|
||||
|
||||
// Crud spec update format
|
||||
if(op.updateOne || op.updateMany || op.replaceOne) {
|
||||
var multi = op.updateOne || op.replaceOne ? false : true;
|
||||
var operation = {q: op[key].filter, u: op[key].update || op[key].replacement, multi: multi}
|
||||
if(op[key].upsert) operation.upsert = true;
|
||||
return addToOperationsList(this, common.UPDATE, operation);
|
||||
}
|
||||
|
||||
// Remove operations
|
||||
if(op.removeOne || op.removeMany || (op.deleteOne && op.deleteOne.q) || op.deleteMany && op.deleteMany.q) {
|
||||
op[key].limit = op.removeOne ? 1 : 0;
|
||||
return addToOperationsList(this, common.REMOVE, op[key]);
|
||||
}
|
||||
|
||||
// Crud spec delete operations, less efficient
|
||||
if(op.deleteOne || op.deleteMany) {
|
||||
var limit = op.deleteOne ? 1 : 0;
|
||||
operation = {q: op[key].filter, limit: limit}
|
||||
return addToOperationsList(this, common.REMOVE, operation);
|
||||
}
|
||||
|
||||
// Insert operations
|
||||
if(op.insertOne && op.insertOne.document == null) {
|
||||
if(forceServerObjectId !== true && op.insertOne._id == null) op.insertOne._id = new ObjectID();
|
||||
return addToOperationsList(this, common.INSERT, op.insertOne);
|
||||
} else if(op.insertOne && op.insertOne.document) {
|
||||
if(forceServerObjectId !== true && op.insertOne.document._id == null) op.insertOne.document._id = new ObjectID();
|
||||
return addToOperationsList(this, common.INSERT, op.insertOne.document);
|
||||
}
|
||||
|
||||
if(op.insertMany) {
|
||||
for(var i = 0; i < op.insertMany.length; i++) {
|
||||
if(forceServerObjectId !== true && op.insertMany[i]._id == null) op.insertMany[i]._id = new ObjectID();
|
||||
addToOperationsList(this, common.INSERT, op.insertMany[i]);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// No valid type of operation
|
||||
throw toError("bulkWrite only supports insertOne, insertMany, updateOne, updateMany, removeOne, removeMany, deleteOne, deleteMany");
|
||||
}
|
||||
|
||||
//
|
||||
// Execute the command
|
||||
var executeBatch = function(self, batch, callback) {
|
||||
var finalOptions = {ordered: false}
|
||||
if(self.s.writeConcern != null) {
|
||||
finalOptions.writeConcern = self.s.writeConcern;
|
||||
}
|
||||
|
||||
var resultHandler = function(err, result) {
|
||||
// Error is a driver related error not a bulk op error, terminate
|
||||
if(err && err.driver || err && err.message) {
|
||||
return handleCallback(callback, err);
|
||||
}
|
||||
|
||||
// If we have and error
|
||||
if(err) err.ok = 0;
|
||||
handleCallback(callback, null, mergeBatchResults(false, batch, self.s.bulkResult, err, result));
|
||||
}
|
||||
|
||||
// Set an operationIf if provided
|
||||
if(self.operationId) {
|
||||
resultHandler.operationId = self.operationId;
|
||||
}
|
||||
|
||||
// Serialize functions
|
||||
if(self.s.options.serializeFunctions) {
|
||||
finalOptions.serializeFunctions = true
|
||||
}
|
||||
|
||||
// Is the bypassDocumentValidation options specific
|
||||
if(self.s.bypassDocumentValidation == true) {
|
||||
finalOptions.bypassDocumentValidation = true;
|
||||
}
|
||||
|
||||
try {
|
||||
if(batch.batchType == common.INSERT) {
|
||||
self.s.topology.insert(self.s.collection.namespace, batch.operations, finalOptions, resultHandler);
|
||||
} else if(batch.batchType == common.UPDATE) {
|
||||
self.s.topology.update(self.s.collection.namespace, batch.operations, finalOptions, resultHandler);
|
||||
} else if(batch.batchType == common.REMOVE) {
|
||||
self.s.topology.remove(self.s.collection.namespace, batch.operations, finalOptions, resultHandler);
|
||||
}
|
||||
} catch(err) {
|
||||
// Force top level error
|
||||
err.ok = 0;
|
||||
// Merge top level error and return
|
||||
handleCallback(callback, null, mergeBatchResults(false, batch, self.s.bulkResult, err, null));
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Execute all the commands
|
||||
var executeBatches = function(self, callback) {
|
||||
var numberOfCommandsToExecute = self.s.batches.length;
|
||||
// Execute over all the batches
|
||||
for(var i = 0; i < self.s.batches.length; i++) {
|
||||
executeBatch(self, self.s.batches[i], function(err) {
|
||||
// Driver layer error capture it
|
||||
if(err) error = err;
|
||||
// Count down the number of commands left to execute
|
||||
numberOfCommandsToExecute = numberOfCommandsToExecute - 1;
|
||||
|
||||
// Execute
|
||||
if(numberOfCommandsToExecute == 0) {
|
||||
// Driver level error
|
||||
if(error) return handleCallback(callback, error);
|
||||
// Treat write errors
|
||||
var error = self.s.bulkResult.writeErrors.length > 0 ? toError(self.s.bulkResult.writeErrors[0]) : null;
|
||||
handleCallback(callback, error, new BulkWriteResult(self.s.bulkResult));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The callback format for results
|
||||
* @callback UnorderedBulkOperation~resultCallback
|
||||
* @param {MongoError} error An error instance representing the error during the execution.
|
||||
* @param {BulkWriteResult} result The bulk write result.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Execute the ordered bulk operation
|
||||
*
|
||||
* @method
|
||||
* @param {object} [options=null] Optional settings.
|
||||
* @param {(number|string)} [options.w=null] The write concern.
|
||||
* @param {number} [options.wtimeout=null] The write concern timeout.
|
||||
* @param {boolean} [options.j=false] Specify a journal write concern.
|
||||
* @param {boolean} [options.fsync=false] Specify a file sync write concern.
|
||||
* @param {UnorderedBulkOperation~resultCallback} [callback] The result callback
|
||||
* @throws {MongoError}
|
||||
* @return {Promise} returns Promise if no callback passed
|
||||
*/
|
||||
UnorderedBulkOperation.prototype.execute = function(_writeConcern, callback) {
|
||||
var self = this;
|
||||
if (this.s.executed) {
|
||||
var executedError = toError('batch cannot be re-executed');
|
||||
return (typeof callback === 'function') ?
|
||||
callback(executedError, null) : this.s.promiseLibrary.reject(executedError);
|
||||
}
|
||||
|
||||
if (typeof _writeConcern === 'function') {
|
||||
callback = _writeConcern;
|
||||
} else if (_writeConcern && typeof _writeConcern === 'object') {
|
||||
this.s.writeConcern = _writeConcern;
|
||||
}
|
||||
|
||||
// If we have current batch
|
||||
if (this.s.currentInsertBatch) this.s.batches.push(this.s.currentInsertBatch);
|
||||
if (this.s.currentUpdateBatch) this.s.batches.push(this.s.currentUpdateBatch);
|
||||
if (this.s.currentRemoveBatch) this.s.batches.push(this.s.currentRemoveBatch);
|
||||
|
||||
// If we have no operations in the bulk raise an error
|
||||
if (this.s.batches.length == 0) {
|
||||
var emptyBatchError = toError('Invalid Operation, no operations specified');
|
||||
return (typeof callback === 'function') ?
|
||||
callback(emptyBatchError, null) : this.s.promiseLibrary.reject(emptyBatchError);
|
||||
}
|
||||
|
||||
// Execute using callback
|
||||
if (typeof callback === 'function') return executeBatches(this, callback);
|
||||
|
||||
// Return a Promise
|
||||
return new this.s.promiseLibrary(function(resolve, reject) {
|
||||
executeBatches(self, function(err, r) {
|
||||
if (err) return reject(err);
|
||||
resolve(r);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
define.classMethod('execute', {callback: true, promise:false});
|
||||
|
||||
/**
|
||||
* Returns an unordered batch object
|
||||
* @ignore
|
||||
*/
|
||||
var initializeUnorderedBulkOp = function(topology, collection, options) {
|
||||
return new UnorderedBulkOperation(topology, collection, options);
|
||||
}
|
||||
|
||||
initializeUnorderedBulkOp.UnorderedBulkOperation = UnorderedBulkOperation;
|
||||
module.exports = initializeUnorderedBulkOp;
|
||||
module.exports.Bulk = UnorderedBulkOperation;
|
||||
+3377
File diff suppressed because it is too large
Load Diff
+320
@@ -0,0 +1,320 @@
|
||||
"use strict";
|
||||
|
||||
var inherits = require('util').inherits
|
||||
, ReadPreference = require('./read_preference')
|
||||
, MongoError = require('mongodb-core').MongoError
|
||||
, Readable = require('stream').Readable || require('readable-stream').Readable
|
||||
, Define = require('./metadata')
|
||||
, CoreCursor = require('./cursor')
|
||||
, CoreReadPreference = require('mongodb-core').ReadPreference;
|
||||
|
||||
/**
|
||||
* @fileOverview The **CommandCursor** class is an internal class that embodies a
|
||||
* generalized cursor based on a MongoDB command allowing for iteration over the
|
||||
* results returned. It supports one by one document iteration, conversion to an
|
||||
* array or can be iterated as a Node 0.10.X or higher stream
|
||||
*
|
||||
* **CommandCursor Cannot directly be instantiated**
|
||||
* @example
|
||||
* var MongoClient = require('mongodb').MongoClient,
|
||||
* test = require('assert');
|
||||
* // Connection url
|
||||
* var url = 'mongodb://localhost:27017/test';
|
||||
* // Connect using MongoClient
|
||||
* MongoClient.connect(url, function(err, db) {
|
||||
* // Create a collection we want to drop later
|
||||
* var col = db.collection('listCollectionsExample1');
|
||||
* // Insert a bunch of documents
|
||||
* col.insert([{a:1, b:1}
|
||||
* , {a:2, b:2}, {a:3, b:3}
|
||||
* , {a:4, b:4}], {w:1}, function(err, result) {
|
||||
* test.equal(null, err);
|
||||
*
|
||||
* // List the database collections available
|
||||
* db.listCollections().toArray(function(err, items) {
|
||||
* test.equal(null, err);
|
||||
* db.close();
|
||||
* });
|
||||
* });
|
||||
* });
|
||||
*/
|
||||
|
||||
/**
|
||||
* Namespace provided by the browser.
|
||||
* @external Readable
|
||||
*/
|
||||
|
||||
/**
|
||||
* Creates a new Command Cursor instance (INTERNAL TYPE, do not instantiate directly)
|
||||
* @class CommandCursor
|
||||
* @extends external:Readable
|
||||
* @fires CommandCursor#data
|
||||
* @fires CommandCursor#end
|
||||
* @fires CommandCursor#close
|
||||
* @fires CommandCursor#readable
|
||||
* @return {CommandCursor} an CommandCursor instance.
|
||||
*/
|
||||
var CommandCursor = function(bson, ns, cmd, options, topology, topologyOptions) {
|
||||
CoreCursor.apply(this, Array.prototype.slice.call(arguments, 0));
|
||||
var state = CommandCursor.INIT;
|
||||
var streamOptions = {};
|
||||
|
||||
// MaxTimeMS
|
||||
var maxTimeMS = null;
|
||||
|
||||
// Get the promiseLibrary
|
||||
var promiseLibrary = options.promiseLibrary;
|
||||
|
||||
// No promise library selected fall back
|
||||
if(!promiseLibrary) {
|
||||
promiseLibrary = typeof global.Promise == 'function' ?
|
||||
global.Promise : require('es6-promise').Promise;
|
||||
}
|
||||
|
||||
// Set up
|
||||
Readable.call(this, {objectMode: true});
|
||||
|
||||
// Internal state
|
||||
this.s = {
|
||||
// MaxTimeMS
|
||||
maxTimeMS: maxTimeMS
|
||||
// State
|
||||
, state: state
|
||||
// Stream options
|
||||
, streamOptions: streamOptions
|
||||
// BSON
|
||||
, bson: bson
|
||||
// Namespace
|
||||
, ns: ns
|
||||
// Command
|
||||
, cmd: cmd
|
||||
// Options
|
||||
, options: options
|
||||
// Topology
|
||||
, topology: topology
|
||||
// Topology Options
|
||||
, topologyOptions: topologyOptions
|
||||
// Promise library
|
||||
, promiseLibrary: promiseLibrary
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* CommandCursor stream data event, fired for each document in the cursor.
|
||||
*
|
||||
* @event CommandCursor#data
|
||||
* @type {object}
|
||||
*/
|
||||
|
||||
/**
|
||||
* CommandCursor stream end event
|
||||
*
|
||||
* @event CommandCursor#end
|
||||
* @type {null}
|
||||
*/
|
||||
|
||||
/**
|
||||
* CommandCursor stream close event
|
||||
*
|
||||
* @event CommandCursor#close
|
||||
* @type {null}
|
||||
*/
|
||||
|
||||
/**
|
||||
* CommandCursor stream readable event
|
||||
*
|
||||
* @event CommandCursor#readable
|
||||
* @type {null}
|
||||
*/
|
||||
|
||||
// Inherit from Readable
|
||||
inherits(CommandCursor, Readable);
|
||||
|
||||
// Set the methods to inherit from prototype
|
||||
var methodsToInherit = ['_next', 'next', 'hasNext', 'each', 'forEach', 'toArray'
|
||||
, 'rewind', 'bufferedCount', 'readBufferedDocuments', 'close', 'isClosed', 'kill', 'setCursorBatchSize'
|
||||
, '_find', '_getmore', '_killcursor', 'isDead', 'explain', 'isNotified', 'isKilled'];
|
||||
|
||||
// Only inherit the types we need
|
||||
for(var i = 0; i < methodsToInherit.length; i++) {
|
||||
CommandCursor.prototype[methodsToInherit[i]] = CoreCursor.prototype[methodsToInherit[i]];
|
||||
}
|
||||
|
||||
var define = CommandCursor.define = new Define('CommandCursor', CommandCursor, true);
|
||||
|
||||
/**
|
||||
* Set the ReadPreference for the cursor.
|
||||
* @method
|
||||
* @param {(string|ReadPreference)} readPreference The new read preference for the cursor.
|
||||
* @throws {MongoError}
|
||||
* @return {Cursor}
|
||||
*/
|
||||
CommandCursor.prototype.setReadPreference = function(r) {
|
||||
if(this.s.state == CommandCursor.CLOSED || this.isDead()) throw MongoError.create({message: "Cursor is closed", driver:true});
|
||||
if(this.s.state != CommandCursor.INIT) throw MongoError.create({message: 'cannot change cursor readPreference after cursor has been accessed', driver:true});
|
||||
|
||||
if(r instanceof ReadPreference) {
|
||||
this.s.options.readPreference = new CoreReadPreference(r.mode, r.tags, {maxStalenessSeconds: r.maxStalenessSeconds});
|
||||
} else if(typeof r == 'string') {
|
||||
this.s.options.readPreference = new CoreReadPreference(r);
|
||||
} else if(r instanceof CoreReadPreference) {
|
||||
this.s.options.readPreference = r;
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
define.classMethod('setReadPreference', {callback: false, promise:false, returns: [CommandCursor]});
|
||||
|
||||
/**
|
||||
* Set the batch size for the cursor.
|
||||
* @method
|
||||
* @param {number} value The batchSize for the cursor.
|
||||
* @throws {MongoError}
|
||||
* @return {CommandCursor}
|
||||
*/
|
||||
CommandCursor.prototype.batchSize = function(value) {
|
||||
if(this.s.state == CommandCursor.CLOSED || this.isDead()) throw MongoError.create({message: "Cursor is closed", driver:true});
|
||||
if(typeof value != 'number') throw MongoError.create({message: "batchSize requires an integer", driver:true});
|
||||
if(this.s.cmd.cursor) this.s.cmd.cursor.batchSize = value;
|
||||
this.setCursorBatchSize(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
define.classMethod('batchSize', {callback: false, promise:false, returns: [CommandCursor]});
|
||||
|
||||
/**
|
||||
* Add a maxTimeMS stage to the aggregation pipeline
|
||||
* @method
|
||||
* @param {number} value The state maxTimeMS value.
|
||||
* @return {CommandCursor}
|
||||
*/
|
||||
CommandCursor.prototype.maxTimeMS = function(value) {
|
||||
if(this.s.topology.lastIsMaster().minWireVersion > 2) {
|
||||
this.s.cmd.maxTimeMS = value;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
define.classMethod('maxTimeMS', {callback: false, promise:false, returns: [CommandCursor]});
|
||||
|
||||
CommandCursor.prototype.get = CommandCursor.prototype.toArray;
|
||||
|
||||
define.classMethod('get', {callback: true, promise:false});
|
||||
|
||||
// Inherited methods
|
||||
define.classMethod('toArray', {callback: true, promise:true});
|
||||
define.classMethod('each', {callback: true, promise:false});
|
||||
define.classMethod('forEach', {callback: true, promise:false});
|
||||
define.classMethod('next', {callback: true, promise:true});
|
||||
define.classMethod('hasNext', {callback: true, promise:true});
|
||||
define.classMethod('close', {callback: true, promise:true});
|
||||
define.classMethod('isClosed', {callback: false, promise:false, returns: [Boolean]});
|
||||
define.classMethod('rewind', {callback: false, promise:false});
|
||||
define.classMethod('bufferedCount', {callback: false, promise:false, returns: [Number]});
|
||||
define.classMethod('readBufferedDocuments', {callback: false, promise:false, returns: [Array]});
|
||||
|
||||
/**
|
||||
* Get the next available document from the cursor, returns null if no more documents are available.
|
||||
* @function CommandCursor.prototype.next
|
||||
* @param {CommandCursor~resultCallback} [callback] The result callback.
|
||||
* @throws {MongoError}
|
||||
* @return {Promise} returns Promise if no callback passed
|
||||
*/
|
||||
|
||||
/**
|
||||
* Check if there is any document still available in the cursor
|
||||
* @function CommandCursor.prototype.hasNext
|
||||
* @param {CommandCursor~resultCallback} [callback] The result callback.
|
||||
* @throws {MongoError}
|
||||
* @return {Promise} returns Promise if no callback passed
|
||||
*/
|
||||
|
||||
/**
|
||||
* The callback format for results
|
||||
* @callback CommandCursor~toArrayResultCallback
|
||||
* @param {MongoError} error An error instance representing the error during the execution.
|
||||
* @param {object[]} documents All the documents the satisfy the cursor.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Returns an array of documents. The caller is responsible for making sure that there
|
||||
* is enough memory to store the results. Note that the array only contain partial
|
||||
* results when this cursor had been previously accessed.
|
||||
* @method CommandCursor.prototype.toArray
|
||||
* @param {CommandCursor~toArrayResultCallback} [callback] The result callback.
|
||||
* @throws {MongoError}
|
||||
* @return {Promise} returns Promise if no callback passed
|
||||
*/
|
||||
|
||||
/**
|
||||
* The callback format for results
|
||||
* @callback CommandCursor~resultCallback
|
||||
* @param {MongoError} error An error instance representing the error during the execution.
|
||||
* @param {(object|null)} result The result object if the command was executed successfully.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Iterates over all the documents for this cursor. As with **{cursor.toArray}**,
|
||||
* not all of the elements will be iterated if this cursor had been previously accessed.
|
||||
* In that case, **{cursor.rewind}** can be used to reset the cursor. However, unlike
|
||||
* **{cursor.toArray}**, the cursor will only hold a maximum of batch size elements
|
||||
* at any given time if batch size is specified. Otherwise, the caller is responsible
|
||||
* for making sure that the entire result can fit the memory.
|
||||
* @method CommandCursor.prototype.each
|
||||
* @param {CommandCursor~resultCallback} callback The result callback.
|
||||
* @throws {MongoError}
|
||||
* @return {null}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Close the cursor, sending a KillCursor command and emitting close.
|
||||
* @method CommandCursor.prototype.close
|
||||
* @param {CommandCursor~resultCallback} [callback] The result callback.
|
||||
* @return {Promise} returns Promise if no callback passed
|
||||
*/
|
||||
|
||||
/**
|
||||
* Is the cursor closed
|
||||
* @method CommandCursor.prototype.isClosed
|
||||
* @return {boolean}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Clone the cursor
|
||||
* @function CommandCursor.prototype.clone
|
||||
* @return {CommandCursor}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Resets the cursor
|
||||
* @function CommandCursor.prototype.rewind
|
||||
* @return {CommandCursor}
|
||||
*/
|
||||
|
||||
/**
|
||||
* The callback format for the forEach iterator method
|
||||
* @callback CommandCursor~iteratorCallback
|
||||
* @param {Object} doc An emitted document for the iterator
|
||||
*/
|
||||
|
||||
/**
|
||||
* The callback error format for the forEach iterator method
|
||||
* @callback CommandCursor~endCallback
|
||||
* @param {MongoError} error An error instance representing the error during the execution.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Iterates over all the documents for this cursor using the iterator, callback pattern.
|
||||
* @method CommandCursor.prototype.forEach
|
||||
* @param {CommandCursor~iteratorCallback} iterator The iteration callback.
|
||||
* @param {CommandCursor~endCallback} callback The end callback.
|
||||
* @throws {MongoError}
|
||||
* @return {null}
|
||||
*/
|
||||
|
||||
CommandCursor.INIT = 0;
|
||||
CommandCursor.OPEN = 1;
|
||||
CommandCursor.CLOSED = 2;
|
||||
|
||||
module.exports = CommandCursor;
|
||||
+1208
File diff suppressed because it is too large
Load Diff
+1805
File diff suppressed because it is too large
Load Diff
+409
@@ -0,0 +1,409 @@
|
||||
var stream = require('stream'),
|
||||
util = require('util');
|
||||
|
||||
module.exports = GridFSBucketReadStream;
|
||||
|
||||
/**
|
||||
* A readable stream that enables you to read buffers from GridFS.
|
||||
*
|
||||
* Do not instantiate this class directly. Use `openDownloadStream()` instead.
|
||||
*
|
||||
* @class
|
||||
* @param {Collection} chunks Handle for chunks collection
|
||||
* @param {Collection} files Handle for files collection
|
||||
* @param {Object} readPreference The read preference to use
|
||||
* @param {Object} filter The query to use to find the file document
|
||||
* @param {Object} [options=null] Optional settings.
|
||||
* @param {Number} [options.sort=null] Optional sort for the file find query
|
||||
* @param {Number} [options.skip=null] Optional skip for the file find query
|
||||
* @param {Number} [options.start=null] Optional 0-based offset in bytes to start streaming from
|
||||
* @param {Number} [options.end=null] Optional 0-based offset in bytes to stop streaming before
|
||||
* @fires GridFSBucketReadStream#error
|
||||
* @fires GridFSBucketReadStream#file
|
||||
* @return {GridFSBucketReadStream} a GridFSBucketReadStream instance.
|
||||
*/
|
||||
|
||||
function GridFSBucketReadStream(chunks, files, readPreference, filter, options) {
|
||||
this.s = {
|
||||
bytesRead: 0,
|
||||
chunks: chunks,
|
||||
cursor: null,
|
||||
expected: 0,
|
||||
files: files,
|
||||
filter: filter,
|
||||
init: false,
|
||||
expectedEnd: 0,
|
||||
file: null,
|
||||
options: options,
|
||||
readPreference: readPreference
|
||||
};
|
||||
|
||||
stream.Readable.call(this);
|
||||
}
|
||||
|
||||
util.inherits(GridFSBucketReadStream, stream.Readable);
|
||||
|
||||
/**
|
||||
* An error occurred
|
||||
*
|
||||
* @event GridFSBucketReadStream#error
|
||||
* @type {Error}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Fires when the stream loaded the file document corresponding to the
|
||||
* provided id.
|
||||
*
|
||||
* @event GridFSBucketReadStream#file
|
||||
* @type {object}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Emitted when a chunk of data is available to be consumed.
|
||||
*
|
||||
* @event GridFSBucketReadStream#data
|
||||
* @type {object}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Fired when the stream is exhausted (no more data events).
|
||||
*
|
||||
* @event GridFSBucketReadStream#end
|
||||
* @type {object}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Fired when the stream is exhausted and the underlying cursor is killed
|
||||
*
|
||||
* @event GridFSBucketReadStream#close
|
||||
* @type {object}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Reads from the cursor and pushes to the stream.
|
||||
* @method
|
||||
*/
|
||||
|
||||
GridFSBucketReadStream.prototype._read = function() {
|
||||
var _this = this;
|
||||
if (this.destroyed) {
|
||||
return;
|
||||
}
|
||||
|
||||
waitForFile(_this, function() {
|
||||
doRead(_this);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets the 0-based offset in bytes to start streaming from. Throws
|
||||
* an error if this stream has entered flowing mode
|
||||
* (e.g. if you've already called `on('data')`)
|
||||
* @method
|
||||
* @param {Number} start Offset in bytes to start reading at
|
||||
* @return {GridFSBucketReadStream}
|
||||
*/
|
||||
|
||||
GridFSBucketReadStream.prototype.start = function(start) {
|
||||
throwIfInitialized(this);
|
||||
this.s.options.start = start;
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets the 0-based offset in bytes to start streaming from. Throws
|
||||
* an error if this stream has entered flowing mode
|
||||
* (e.g. if you've already called `on('data')`)
|
||||
* @method
|
||||
* @param {Number} end Offset in bytes to stop reading at
|
||||
* @return {GridFSBucketReadStream}
|
||||
*/
|
||||
|
||||
GridFSBucketReadStream.prototype.end = function(end) {
|
||||
throwIfInitialized(this);
|
||||
this.s.options.end = end;
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Marks this stream as aborted (will never push another `data` event)
|
||||
* and kills the underlying cursor. Will emit the 'end' event, and then
|
||||
* the 'close' event once the cursor is successfully killed.
|
||||
*
|
||||
* @method
|
||||
* @param {GridFSBucket~errorCallback} [callback] called when the cursor is successfully closed or an error occurred.
|
||||
* @fires GridFSBucketWriteStream#close
|
||||
* @fires GridFSBucketWriteStream#end
|
||||
*/
|
||||
|
||||
GridFSBucketReadStream.prototype.abort = function(callback) {
|
||||
var _this = this;
|
||||
this.push(null);
|
||||
this.destroyed = true;
|
||||
if (this.s.cursor) {
|
||||
this.s.cursor.close(function(error) {
|
||||
_this.emit('close');
|
||||
callback && callback(error);
|
||||
});
|
||||
} else {
|
||||
if (!this.s.init) {
|
||||
// If not initialized, fire close event because we will never
|
||||
// get a cursor
|
||||
_this.emit('close');
|
||||
}
|
||||
callback && callback();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
|
||||
function throwIfInitialized(self) {
|
||||
if (self.s.init) {
|
||||
throw new Error('You cannot change options after the stream has entered' +
|
||||
'flowing mode!');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
|
||||
function doRead(_this) {
|
||||
if (_this.destroyed) {
|
||||
return;
|
||||
}
|
||||
|
||||
_this.s.cursor.next(function(error, doc) {
|
||||
if (_this.destroyed) {
|
||||
return;
|
||||
}
|
||||
if (error) {
|
||||
return __handleError(_this, error);
|
||||
}
|
||||
if (!doc) {
|
||||
_this.push(null);
|
||||
return _this.s.cursor.close(function(error) {
|
||||
if (error) {
|
||||
return __handleError(_this, error);
|
||||
}
|
||||
_this.emit('close');
|
||||
});
|
||||
}
|
||||
|
||||
var bytesRemaining = _this.s.file.length - _this.s.bytesRead;
|
||||
var expectedN = _this.s.expected++;
|
||||
var expectedLength = Math.min(_this.s.file.chunkSize,
|
||||
bytesRemaining);
|
||||
|
||||
if (doc.n > expectedN) {
|
||||
var errmsg = 'ChunkIsMissing: Got unexpected n: ' + doc.n +
|
||||
', expected: ' + expectedN;
|
||||
return __handleError(_this, new Error(errmsg));
|
||||
}
|
||||
|
||||
if (doc.n < expectedN) {
|
||||
errmsg = 'ExtraChunk: Got unexpected n: ' + doc.n +
|
||||
', expected: ' + expectedN;
|
||||
return __handleError(_this, new Error(errmsg));
|
||||
}
|
||||
|
||||
var buf = Buffer.isBuffer(doc.data) ? doc.data : doc.data.buffer;
|
||||
|
||||
if (buf.length !== expectedLength) {
|
||||
if (bytesRemaining <= 0) {
|
||||
errmsg = 'ExtraChunk: Got unexpected n: ' + doc.n;
|
||||
return __handleError(_this, new Error(errmsg));
|
||||
}
|
||||
|
||||
errmsg = 'ChunkIsWrongSize: Got unexpected length: ' +
|
||||
buf.length + ', expected: ' + expectedLength;
|
||||
return __handleError(_this, new Error(errmsg));
|
||||
}
|
||||
|
||||
_this.s.bytesRead += buf.length;
|
||||
|
||||
if (buf.length === 0) {
|
||||
return _this.push(null);
|
||||
}
|
||||
|
||||
var sliceStart = null;
|
||||
var sliceEnd = null;
|
||||
|
||||
if (_this.s.bytesToSkip != null) {
|
||||
sliceStart = _this.s.bytesToSkip;
|
||||
_this.s.bytesToSkip = 0;
|
||||
}
|
||||
|
||||
if (expectedN === _this.s.expectedEnd && _this.s.bytesToTrim != null) {
|
||||
sliceEnd = _this.s.bytesToTrim;
|
||||
}
|
||||
|
||||
// If the remaining amount of data left is < chunkSize read the right amount of data
|
||||
if (_this.s.options.end && (
|
||||
(_this.s.options.end - _this.s.bytesToSkip) < buf.length
|
||||
)) {
|
||||
sliceEnd = (_this.s.options.end - _this.s.bytesToSkip);
|
||||
}
|
||||
|
||||
if (sliceStart != null || sliceEnd != null) {
|
||||
buf = buf.slice(sliceStart || 0, sliceEnd || buf.length);
|
||||
}
|
||||
|
||||
_this.push(buf);
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
|
||||
function init(self) {
|
||||
var findOneOptions = {};
|
||||
if (self.s.readPreference) {
|
||||
findOneOptions.readPreference = self.s.readPreference;
|
||||
}
|
||||
if (self.s.options && self.s.options.sort) {
|
||||
findOneOptions.sort = self.s.options.sort;
|
||||
}
|
||||
if (self.s.options && self.s.options.skip) {
|
||||
findOneOptions.skip = self.s.options.skip;
|
||||
}
|
||||
|
||||
self.s.files.findOne(self.s.filter, findOneOptions, function(error, doc) {
|
||||
if (error) {
|
||||
return __handleError(self, error);
|
||||
}
|
||||
if (!doc) {
|
||||
var identifier = self.s.filter._id ?
|
||||
self.s.filter._id.toString() : self.s.filter.filename;
|
||||
var errmsg = 'FileNotFound: file ' + identifier + ' was not found';
|
||||
var err = new Error(errmsg);
|
||||
err.code = 'ENOENT';
|
||||
return __handleError(self, err);
|
||||
}
|
||||
|
||||
// If document is empty, kill the stream immediately and don't
|
||||
// execute any reads
|
||||
if (doc.length <= 0) {
|
||||
self.push(null);
|
||||
return;
|
||||
}
|
||||
|
||||
if (self.destroyed) {
|
||||
// If user destroys the stream before we have a cursor, wait
|
||||
// until the query is done to say we're 'closed' because we can't
|
||||
// cancel a query.
|
||||
self.emit('close');
|
||||
return;
|
||||
}
|
||||
|
||||
self.s.bytesToSkip = handleStartOption(self, doc, self.s.options);
|
||||
|
||||
var filter = { files_id: doc._id };
|
||||
|
||||
// Currently (MongoDB 3.4.4) skip function does not support the index,
|
||||
// it needs to retrieve all the documents first and then skip them. (CS-25811)
|
||||
// As work around we use $gte on the "n" field.
|
||||
if (self.s.options && self.s.options.start != null){
|
||||
var skip = Math.floor(self.s.options.start / doc.chunkSize);
|
||||
if (skip > 0){
|
||||
filter["n"] = {"$gte": skip};
|
||||
}
|
||||
}
|
||||
self.s.cursor = self.s.chunks.find(filter).sort({ n: 1 });
|
||||
|
||||
if (self.s.readPreference) {
|
||||
self.s.cursor.setReadPreference(self.s.readPreference);
|
||||
}
|
||||
|
||||
self.s.expectedEnd = Math.ceil(doc.length / doc.chunkSize);
|
||||
self.s.file = doc;
|
||||
self.s.bytesToTrim = handleEndOption(self, doc, self.s.cursor,
|
||||
self.s.options);
|
||||
self.emit('file', doc);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
|
||||
function waitForFile(_this, callback) {
|
||||
if (_this.s.file) {
|
||||
return callback();
|
||||
}
|
||||
|
||||
if (!_this.s.init) {
|
||||
init(_this);
|
||||
_this.s.init = true;
|
||||
}
|
||||
|
||||
_this.once('file', function() {
|
||||
callback();
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
|
||||
function handleStartOption(stream, doc, options) {
|
||||
if (options && options.start != null) {
|
||||
if (options.start > doc.length) {
|
||||
throw new Error('Stream start (' + options.start + ') must not be ' +
|
||||
'more than the length of the file (' + doc.length +')');
|
||||
}
|
||||
if (options.start < 0) {
|
||||
throw new Error('Stream start (' + options.start + ') must not be ' +
|
||||
'negative');
|
||||
}
|
||||
if (options.end != null && options.end < options.start) {
|
||||
throw new Error('Stream start (' + options.start + ') must not be ' +
|
||||
'greater than stream end (' + options.end + ')');
|
||||
}
|
||||
|
||||
stream.s.bytesRead = Math.floor(options.start / doc.chunkSize) *
|
||||
doc.chunkSize;
|
||||
stream.s.expected = Math.floor(options.start / doc.chunkSize);
|
||||
|
||||
return options.start - stream.s.bytesRead;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
|
||||
function handleEndOption(stream, doc, cursor, options) {
|
||||
if (options && options.end != null) {
|
||||
if (options.end > doc.length) {
|
||||
throw new Error('Stream end (' + options.end + ') must not be ' +
|
||||
'more than the length of the file (' + doc.length +')')
|
||||
}
|
||||
if (options.start < 0) {
|
||||
throw new Error('Stream end (' + options.end + ') must not be ' +
|
||||
'negative');
|
||||
}
|
||||
|
||||
var start = options.start != null ?
|
||||
Math.floor(options.start / doc.chunkSize) :
|
||||
0;
|
||||
|
||||
cursor.limit(Math.ceil(options.end / doc.chunkSize) - start);
|
||||
|
||||
stream.s.expectedEnd = Math.ceil(options.end / doc.chunkSize);
|
||||
|
||||
return (Math.ceil(options.end / doc.chunkSize) * doc.chunkSize) -
|
||||
options.end;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
|
||||
function __handleError(_this, error) {
|
||||
_this.emit('error', error);
|
||||
}
|
||||
+367
@@ -0,0 +1,367 @@
|
||||
var Emitter = require('events').EventEmitter;
|
||||
var GridFSBucketReadStream = require('./download');
|
||||
var GridFSBucketWriteStream = require('./upload');
|
||||
var shallowClone = require('../utils').shallowClone;
|
||||
var toError = require('../utils').toError;
|
||||
var util = require('util');
|
||||
|
||||
var DEFAULT_GRIDFS_BUCKET_OPTIONS = {
|
||||
bucketName: 'fs',
|
||||
chunkSizeBytes: 255 * 1024
|
||||
};
|
||||
|
||||
module.exports = GridFSBucket;
|
||||
|
||||
/**
|
||||
* Constructor for a streaming GridFS interface
|
||||
* @class
|
||||
* @param {Db} db A db handle
|
||||
* @param {object} [options=null] Optional settings.
|
||||
* @param {string} [options.bucketName="fs"] The 'files' and 'chunks' collections will be prefixed with the bucket name followed by a dot.
|
||||
* @param {number} [options.chunkSizeBytes=255 * 1024] Number of bytes stored in each chunk. Defaults to 255KB
|
||||
* @param {object} [options.writeConcern=null] Optional write concern to be passed to write operations, for instance `{ w: 1 }`
|
||||
* @param {object} [options.readPreference=null] Optional read preference to be passed to read operations
|
||||
* @fires GridFSBucketWriteStream#index
|
||||
* @return {GridFSBucket}
|
||||
*/
|
||||
|
||||
function GridFSBucket(db, options) {
|
||||
Emitter.apply(this);
|
||||
this.setMaxListeners(0);
|
||||
|
||||
if (options && typeof options === 'object') {
|
||||
options = shallowClone(options);
|
||||
var keys = Object.keys(DEFAULT_GRIDFS_BUCKET_OPTIONS);
|
||||
for (var i = 0; i < keys.length; ++i) {
|
||||
if (!options[keys[i]]) {
|
||||
options[keys[i]] = DEFAULT_GRIDFS_BUCKET_OPTIONS[keys[i]];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
options = DEFAULT_GRIDFS_BUCKET_OPTIONS;
|
||||
}
|
||||
|
||||
this.s = {
|
||||
db: db,
|
||||
options: options,
|
||||
_chunksCollection: db.collection(options.bucketName + '.chunks'),
|
||||
_filesCollection: db.collection(options.bucketName + '.files'),
|
||||
checkedIndexes: false,
|
||||
calledOpenUploadStream: false,
|
||||
promiseLibrary: db.s.promiseLibrary ||
|
||||
(typeof global.Promise == 'function' ? global.Promise : require('es6-promise').Promise)
|
||||
};
|
||||
}
|
||||
|
||||
util.inherits(GridFSBucket, Emitter);
|
||||
|
||||
/**
|
||||
* When the first call to openUploadStream is made, the upload stream will
|
||||
* check to see if it needs to create the proper indexes on the chunks and
|
||||
* files collections. This event is fired either when 1) it determines that
|
||||
* no index creation is necessary, 2) when it successfully creates the
|
||||
* necessary indexes.
|
||||
*
|
||||
* @event GridFSBucket#index
|
||||
* @type {Error}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Returns a writable stream (GridFSBucketWriteStream) for writing
|
||||
* buffers to GridFS. The stream's 'id' property contains the resulting
|
||||
* file's id.
|
||||
* @method
|
||||
* @param {string} filename The value of the 'filename' key in the files doc
|
||||
* @param {object} [options=null] Optional settings.
|
||||
* @param {number} [options.chunkSizeBytes=null] Optional overwrite this bucket's chunkSizeBytes for this file
|
||||
* @param {object} [options.metadata=null] Optional object to store in the file document's `metadata` field
|
||||
* @param {string} [options.contentType=null] Optional string to store in the file document's `contentType` field
|
||||
* @param {array} [options.aliases=null] Optional array of strings to store in the file document's `aliases` field
|
||||
* @return {GridFSBucketWriteStream}
|
||||
*/
|
||||
|
||||
GridFSBucket.prototype.openUploadStream = function(filename, options) {
|
||||
if (options) {
|
||||
options = shallowClone(options);
|
||||
} else {
|
||||
options = {};
|
||||
}
|
||||
if (!options.chunkSizeBytes) {
|
||||
options.chunkSizeBytes = this.s.options.chunkSizeBytes;
|
||||
}
|
||||
return new GridFSBucketWriteStream(this, filename, options);
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns a writable stream (GridFSBucketWriteStream) for writing
|
||||
* buffers to GridFS for a custom file id. The stream's 'id' property contains the resulting
|
||||
* file's id.
|
||||
* @method
|
||||
* @param {string|number|object} id A custom id used to identify the file
|
||||
* @param {string} filename The value of the 'filename' key in the files doc
|
||||
* @param {object} [options=null] Optional settings.
|
||||
* @param {number} [options.chunkSizeBytes=null] Optional overwrite this bucket's chunkSizeBytes for this file
|
||||
* @param {object} [options.metadata=null] Optional object to store in the file document's `metadata` field
|
||||
* @param {string} [options.contentType=null] Optional string to store in the file document's `contentType` field
|
||||
* @param {array} [options.aliases=null] Optional array of strings to store in the file document's `aliases` field
|
||||
* @return {GridFSBucketWriteStream}
|
||||
*/
|
||||
|
||||
GridFSBucket.prototype.openUploadStreamWithId = function(id, filename, options) {
|
||||
if (options) {
|
||||
options = shallowClone(options);
|
||||
} else {
|
||||
options = {};
|
||||
}
|
||||
|
||||
if (!options.chunkSizeBytes) {
|
||||
options.chunkSizeBytes = this.s.options.chunkSizeBytes;
|
||||
}
|
||||
|
||||
options.id = id;
|
||||
|
||||
return new GridFSBucketWriteStream(this, filename, options);
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns a readable stream (GridFSBucketReadStream) for streaming file
|
||||
* data from GridFS.
|
||||
* @method
|
||||
* @param {ObjectId} id The id of the file doc
|
||||
* @param {Object} [options=null] Optional settings.
|
||||
* @param {Number} [options.start=null] Optional 0-based offset in bytes to start streaming from
|
||||
* @param {Number} [options.end=null] Optional 0-based offset in bytes to stop streaming before
|
||||
* @return {GridFSBucketReadStream}
|
||||
*/
|
||||
|
||||
GridFSBucket.prototype.openDownloadStream = function(id, options) {
|
||||
var filter = { _id: id };
|
||||
options = {
|
||||
start: options && options.start,
|
||||
end: options && options.end
|
||||
};
|
||||
|
||||
return new GridFSBucketReadStream(this.s._chunksCollection,
|
||||
this.s._filesCollection, this.s.options.readPreference, filter, options);
|
||||
};
|
||||
|
||||
/**
|
||||
* Deletes a file with the given id
|
||||
* @method
|
||||
* @param {ObjectId} id The id of the file doc
|
||||
* @param {GridFSBucket~errorCallback} [callback]
|
||||
*/
|
||||
|
||||
GridFSBucket.prototype.delete = function(id, callback) {
|
||||
if (typeof callback === 'function') {
|
||||
return _delete(this, id, callback);
|
||||
}
|
||||
|
||||
var _this = this;
|
||||
return new this.s.promiseLibrary(function(resolve, reject) {
|
||||
_delete(_this, id, function(error, res) {
|
||||
if (error) {
|
||||
reject(error);
|
||||
} else {
|
||||
resolve(res);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
|
||||
function _delete(_this, id, callback) {
|
||||
_this.s._filesCollection.deleteOne({ _id: id }, function(error, res) {
|
||||
if (error) {
|
||||
return callback(error);
|
||||
}
|
||||
|
||||
_this.s._chunksCollection.deleteMany({ files_id: id }, function(error) {
|
||||
if (error) {
|
||||
return callback(error);
|
||||
}
|
||||
|
||||
// Delete orphaned chunks before returning FileNotFound
|
||||
if (!res.result.n) {
|
||||
var errmsg = 'FileNotFound: no file with id ' + id + ' found';
|
||||
return callback(new Error(errmsg));
|
||||
}
|
||||
|
||||
callback();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience wrapper around find on the files collection
|
||||
* @method
|
||||
* @param {Object} filter
|
||||
* @param {Object} [options=null] Optional settings for cursor
|
||||
* @param {number} [options.batchSize=null] Optional batch size for cursor
|
||||
* @param {number} [options.limit=null] Optional limit for cursor
|
||||
* @param {number} [options.maxTimeMS=null] Optional maxTimeMS for cursor
|
||||
* @param {boolean} [options.noCursorTimeout=null] Optionally set cursor's `noCursorTimeout` flag
|
||||
* @param {number} [options.skip=null] Optional skip for cursor
|
||||
* @param {object} [options.sort=null] Optional sort for cursor
|
||||
* @return {Cursor}
|
||||
*/
|
||||
|
||||
GridFSBucket.prototype.find = function(filter, options) {
|
||||
filter = filter || {};
|
||||
options = options || {};
|
||||
|
||||
var cursor = this.s._filesCollection.find(filter);
|
||||
|
||||
if (options.batchSize != null) {
|
||||
cursor.batchSize(options.batchSize);
|
||||
}
|
||||
if (options.limit != null) {
|
||||
cursor.limit(options.limit);
|
||||
}
|
||||
if (options.maxTimeMS != null) {
|
||||
cursor.maxTimeMS(options.maxTimeMS);
|
||||
}
|
||||
if (options.noCursorTimeout != null) {
|
||||
cursor.addCursorFlag('noCursorTimeout', options.noCursorTimeout);
|
||||
}
|
||||
if (options.skip != null) {
|
||||
cursor.skip(options.skip);
|
||||
}
|
||||
if (options.sort != null) {
|
||||
cursor.sort(options.sort);
|
||||
}
|
||||
|
||||
return cursor;
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns a readable stream (GridFSBucketReadStream) for streaming the
|
||||
* file with the given name from GridFS. If there are multiple files with
|
||||
* the same name, this will stream the most recent file with the given name
|
||||
* (as determined by the `uploadDate` field). You can set the `revision`
|
||||
* option to change this behavior.
|
||||
* @method
|
||||
* @param {String} filename The name of the file to stream
|
||||
* @param {Object} [options=null] Optional settings
|
||||
* @param {number} [options.revision=-1] The revision number relative to the oldest file with the given filename. 0 gets you the oldest file, 1 gets you the 2nd oldest, -1 gets you the newest.
|
||||
* @param {Number} [options.start=null] Optional 0-based offset in bytes to start streaming from
|
||||
* @param {Number} [options.end=null] Optional 0-based offset in bytes to stop streaming before
|
||||
* @return {GridFSBucketReadStream}
|
||||
*/
|
||||
|
||||
GridFSBucket.prototype.openDownloadStreamByName = function(filename, options) {
|
||||
var sort = { uploadDate: -1 };
|
||||
var skip = null;
|
||||
if (options && options.revision != null) {
|
||||
if (options.revision >= 0) {
|
||||
sort = { uploadDate: 1 };
|
||||
skip = options.revision;
|
||||
} else {
|
||||
skip = -options.revision - 1;
|
||||
}
|
||||
}
|
||||
|
||||
var filter = { filename: filename };
|
||||
options = {
|
||||
sort: sort,
|
||||
skip: skip,
|
||||
start: options && options.start,
|
||||
end: options && options.end
|
||||
};
|
||||
return new GridFSBucketReadStream(this.s._chunksCollection,
|
||||
this.s._filesCollection, this.s.options.readPreference, filter, options);
|
||||
};
|
||||
|
||||
/**
|
||||
* Renames the file with the given _id to the given string
|
||||
* @method
|
||||
* @param {ObjectId} id the id of the file to rename
|
||||
* @param {String} filename new name for the file
|
||||
* @param {GridFSBucket~errorCallback} [callback]
|
||||
*/
|
||||
|
||||
GridFSBucket.prototype.rename = function(id, filename, callback) {
|
||||
if (typeof callback === 'function') {
|
||||
return _rename(this, id, filename, callback);
|
||||
}
|
||||
|
||||
var _this = this;
|
||||
return new this.s.promiseLibrary(function(resolve, reject) {
|
||||
_rename(_this, id, filename, function(error, res) {
|
||||
if (error) {
|
||||
reject(error);
|
||||
} else {
|
||||
resolve(res);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
|
||||
function _rename(_this, id, filename, callback) {
|
||||
var filter = { _id: id };
|
||||
var update = { $set: { filename: filename } };
|
||||
_this.s._filesCollection.updateOne(filter, update, function(error, res) {
|
||||
if (error) {
|
||||
return callback(error);
|
||||
}
|
||||
if (!res.result.n) {
|
||||
return callback(toError('File with id ' + id + ' not found'));
|
||||
}
|
||||
callback();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes this bucket's files collection, followed by its chunks collection.
|
||||
* @method
|
||||
* @param {GridFSBucket~errorCallback} [callback]
|
||||
*/
|
||||
|
||||
GridFSBucket.prototype.drop = function(callback) {
|
||||
if (typeof callback === 'function') {
|
||||
return _drop(this, callback);
|
||||
}
|
||||
|
||||
var _this = this;
|
||||
return new this.s.promiseLibrary(function(resolve, reject) {
|
||||
_drop(_this, function(error, res) {
|
||||
if (error) {
|
||||
reject(error);
|
||||
} else {
|
||||
resolve(res);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
|
||||
function _drop(_this, callback) {
|
||||
_this.s._filesCollection.drop(function(error) {
|
||||
if (error) {
|
||||
return callback(error);
|
||||
}
|
||||
_this.s._chunksCollection.drop(function(error) {
|
||||
if (error) {
|
||||
return callback(error);
|
||||
}
|
||||
|
||||
return callback();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback format for all GridFSBucket methods that can accept a callback.
|
||||
* @callback GridFSBucket~errorCallback
|
||||
* @param {MongoError} error An error instance representing any errors that occurred
|
||||
*/
|
||||
+527
@@ -0,0 +1,527 @@
|
||||
var core = require('mongodb-core');
|
||||
var crypto = require('crypto');
|
||||
var stream = require('stream');
|
||||
var util = require('util');
|
||||
|
||||
var ERROR_NAMESPACE_NOT_FOUND = 26;
|
||||
|
||||
module.exports = GridFSBucketWriteStream;
|
||||
|
||||
/**
|
||||
* A writable stream that enables you to write buffers to GridFS.
|
||||
*
|
||||
* Do not instantiate this class directly. Use `openUploadStream()` instead.
|
||||
*
|
||||
* @class
|
||||
* @param {GridFSBucket} bucket Handle for this stream's corresponding bucket
|
||||
* @param {string} filename The value of the 'filename' key in the files doc
|
||||
* @param {object} [options=null] Optional settings.
|
||||
* @param {string|number|object} [options.id=null] Custom file id for the GridFS file.
|
||||
* @param {number} [options.chunkSizeBytes=null] The chunk size to use, in bytes
|
||||
* @param {number} [options.w=null] The write concern
|
||||
* @param {number} [options.wtimeout=null] The write concern timeout
|
||||
* @param {number} [options.j=null] The journal write concern
|
||||
* @fires GridFSBucketWriteStream#error
|
||||
* @fires GridFSBucketWriteStream#finish
|
||||
* @return {GridFSBucketWriteStream} a GridFSBucketWriteStream instance.
|
||||
*/
|
||||
|
||||
function GridFSBucketWriteStream(bucket, filename, options) {
|
||||
options = options || {};
|
||||
this.bucket = bucket;
|
||||
this.chunks = bucket.s._chunksCollection;
|
||||
this.filename = filename;
|
||||
this.files = bucket.s._filesCollection;
|
||||
this.options = options;
|
||||
// Signals the write is all done
|
||||
this.done = false;
|
||||
|
||||
this.id = options.id ? options.id : core.BSON.ObjectId();
|
||||
this.chunkSizeBytes = this.options.chunkSizeBytes;
|
||||
this.bufToStore = new Buffer(this.chunkSizeBytes);
|
||||
this.length = 0;
|
||||
this.md5 = crypto.createHash('md5');
|
||||
this.n = 0;
|
||||
this.pos = 0;
|
||||
this.state = {
|
||||
streamEnd: false,
|
||||
outstandingRequests: 0,
|
||||
errored: false,
|
||||
aborted: false,
|
||||
promiseLibrary: this.bucket.s.promiseLibrary
|
||||
};
|
||||
|
||||
if (!this.bucket.s.calledOpenUploadStream) {
|
||||
this.bucket.s.calledOpenUploadStream = true;
|
||||
|
||||
var _this = this;
|
||||
checkIndexes(this, function() {
|
||||
_this.bucket.s.checkedIndexes = true;
|
||||
_this.bucket.emit('index');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
util.inherits(GridFSBucketWriteStream, stream.Writable);
|
||||
|
||||
/**
|
||||
* An error occurred
|
||||
*
|
||||
* @event GridFSBucketWriteStream#error
|
||||
* @type {Error}
|
||||
*/
|
||||
|
||||
/**
|
||||
* `end()` was called and the write stream successfully wrote the file
|
||||
* metadata and all the chunks to MongoDB.
|
||||
*
|
||||
* @event GridFSBucketWriteStream#finish
|
||||
* @type {object}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Write a buffer to the stream.
|
||||
*
|
||||
* @method
|
||||
* @param {Buffer} chunk Buffer to write
|
||||
* @param {String} encoding Optional encoding for the buffer
|
||||
* @param {Function} callback Function to call when the chunk was added to the buffer, or if the entire chunk was persisted to MongoDB if this chunk caused a flush.
|
||||
* @return {Boolean} False if this write required flushing a chunk to MongoDB. True otherwise.
|
||||
*/
|
||||
|
||||
GridFSBucketWriteStream.prototype.write = function(chunk, encoding, callback) {
|
||||
var _this = this;
|
||||
return waitForIndexes(this, function() {
|
||||
return doWrite(_this, chunk, encoding, callback);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Places this write stream into an aborted state (all future writes fail)
|
||||
* and deletes all chunks that have already been written.
|
||||
*
|
||||
* @method
|
||||
* @param {GridFSBucket~errorCallback} callback called when chunks are successfully removed or error occurred
|
||||
* @return {Promise} if no callback specified
|
||||
*/
|
||||
|
||||
GridFSBucketWriteStream.prototype.abort = function(callback) {
|
||||
if (this.state.streamEnd) {
|
||||
var error = new Error('Cannot abort a stream that has already completed');
|
||||
if (typeof callback == 'function') {
|
||||
return callback(error);
|
||||
}
|
||||
return this.state.promiseLibrary.reject(error);
|
||||
}
|
||||
if (this.state.aborted) {
|
||||
error = new Error('Cannot call abort() on a stream twice');
|
||||
if (typeof callback == 'function') {
|
||||
return callback(error);
|
||||
}
|
||||
return this.state.promiseLibrary.reject(error);
|
||||
}
|
||||
this.state.aborted = true;
|
||||
this.chunks.deleteMany({ files_id: this.id }, function(error) {
|
||||
if(typeof callback == 'function') callback(error);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Tells the stream that no more data will be coming in. The stream will
|
||||
* persist the remaining data to MongoDB, write the files document, and
|
||||
* then emit a 'finish' event.
|
||||
*
|
||||
* @method
|
||||
* @param {Buffer} chunk Buffer to write
|
||||
* @param {String} encoding Optional encoding for the buffer
|
||||
* @param {Function} callback Function to call when all files and chunks have been persisted to MongoDB
|
||||
*/
|
||||
|
||||
GridFSBucketWriteStream.prototype.end = function(chunk, encoding, callback) {
|
||||
var _this = this;
|
||||
if(typeof chunk == 'function') {
|
||||
callback = chunk, chunk = null, encoding = null;
|
||||
} else if(typeof encoding == 'function') {
|
||||
callback = encoding, encoding = null;
|
||||
}
|
||||
|
||||
if (checkAborted(this, callback)) {
|
||||
return;
|
||||
}
|
||||
this.state.streamEnd = true;
|
||||
|
||||
if (callback) {
|
||||
this.once('finish', function(result) {
|
||||
callback(null, result);
|
||||
});
|
||||
}
|
||||
|
||||
if (!chunk) {
|
||||
waitForIndexes(this, function() {
|
||||
writeRemnant(_this);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
this.write(chunk, encoding, function() {
|
||||
writeRemnant(_this);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
|
||||
function __handleError(_this, error, callback) {
|
||||
if (_this.state.errored) {
|
||||
return;
|
||||
}
|
||||
_this.state.errored = true;
|
||||
if (callback) {
|
||||
return callback(error);
|
||||
}
|
||||
_this.emit('error', error);
|
||||
}
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
|
||||
function createChunkDoc(filesId, n, data) {
|
||||
return {
|
||||
_id: core.BSON.ObjectId(),
|
||||
files_id: filesId,
|
||||
n: n,
|
||||
data: data
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
|
||||
function checkChunksIndex(_this, callback) {
|
||||
_this.chunks.listIndexes().toArray(function(error, indexes) {
|
||||
if (error) {
|
||||
// Collection doesn't exist so create index
|
||||
if (error.code === ERROR_NAMESPACE_NOT_FOUND) {
|
||||
var index = { files_id: 1, n: 1 };
|
||||
_this.chunks.createIndex(index, { background: false, unique: true }, function(error) {
|
||||
if (error) {
|
||||
return callback(error);
|
||||
}
|
||||
|
||||
callback();
|
||||
});
|
||||
return;
|
||||
}
|
||||
return callback(error);
|
||||
}
|
||||
|
||||
var hasChunksIndex = false;
|
||||
indexes.forEach(function(index) {
|
||||
if (index.key) {
|
||||
var keys = Object.keys(index.key);
|
||||
if (keys.length === 2 && index.key.files_id === 1 &&
|
||||
index.key.n === 1) {
|
||||
hasChunksIndex = true;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (hasChunksIndex) {
|
||||
callback();
|
||||
} else {
|
||||
index = { files_id: 1, n: 1 };
|
||||
var indexOptions = getWriteOptions(_this);
|
||||
|
||||
indexOptions.background = false;
|
||||
indexOptions.unique = true;
|
||||
|
||||
_this.chunks.createIndex(index, indexOptions, function(error) {
|
||||
if (error) {
|
||||
return callback(error);
|
||||
}
|
||||
|
||||
callback();
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
|
||||
function checkDone(_this, callback) {
|
||||
if(_this.done) return true;
|
||||
if (_this.state.streamEnd &&
|
||||
_this.state.outstandingRequests === 0 &&
|
||||
!_this.state.errored) {
|
||||
// Set done so we dont' trigger duplicate createFilesDoc
|
||||
_this.done = true;
|
||||
// Create a new files doc
|
||||
var filesDoc = createFilesDoc(_this.id, _this.length, _this.chunkSizeBytes,
|
||||
_this.md5.digest('hex'), _this.filename, _this.options.contentType,
|
||||
_this.options.aliases, _this.options.metadata);
|
||||
|
||||
if (checkAborted(_this, callback)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
_this.files.insert(filesDoc, getWriteOptions(_this), function(error) {
|
||||
if (error) {
|
||||
return __handleError(_this, error, callback);
|
||||
}
|
||||
_this.emit('finish', filesDoc);
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
|
||||
function checkIndexes(_this, callback) {
|
||||
_this.files.findOne({}, { _id: 1 }, function(error, doc) {
|
||||
if (error) {
|
||||
return callback(error);
|
||||
}
|
||||
if (doc) {
|
||||
return callback();
|
||||
}
|
||||
|
||||
_this.files.listIndexes().toArray(function(error, indexes) {
|
||||
if (error) {
|
||||
// Collection doesn't exist so create index
|
||||
if (error.code === ERROR_NAMESPACE_NOT_FOUND) {
|
||||
var index = { filename: 1, uploadDate: 1 };
|
||||
_this.files.createIndex(index, { background: false }, function(error) {
|
||||
if (error) {
|
||||
return callback(error);
|
||||
}
|
||||
|
||||
checkChunksIndex(_this, callback);
|
||||
});
|
||||
return;
|
||||
}
|
||||
return callback(error);
|
||||
}
|
||||
|
||||
var hasFileIndex = false;
|
||||
indexes.forEach(function(index) {
|
||||
var keys = Object.keys(index.key);
|
||||
if (keys.length === 2 && index.key.filename === 1 &&
|
||||
index.key.uploadDate === 1) {
|
||||
hasFileIndex = true;
|
||||
}
|
||||
});
|
||||
|
||||
if (hasFileIndex) {
|
||||
checkChunksIndex(_this, callback);
|
||||
} else {
|
||||
index = { filename: 1, uploadDate: 1 };
|
||||
|
||||
var indexOptions = getWriteOptions(_this);
|
||||
|
||||
indexOptions.background = false;
|
||||
|
||||
_this.files.createIndex(index, indexOptions, function(error) {
|
||||
if (error) {
|
||||
return callback(error);
|
||||
}
|
||||
|
||||
checkChunksIndex(_this, callback);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
|
||||
function createFilesDoc(_id, length, chunkSize, md5, filename, contentType,
|
||||
aliases, metadata) {
|
||||
var ret = {
|
||||
_id: _id,
|
||||
length: length,
|
||||
chunkSize: chunkSize,
|
||||
uploadDate: new Date(),
|
||||
md5: md5,
|
||||
filename: filename
|
||||
};
|
||||
|
||||
if (contentType) {
|
||||
ret.contentType = contentType;
|
||||
}
|
||||
|
||||
if (aliases) {
|
||||
ret.aliases = aliases;
|
||||
}
|
||||
|
||||
if (metadata) {
|
||||
ret.metadata = metadata;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
|
||||
function doWrite(_this, chunk, encoding, callback) {
|
||||
if (checkAborted(_this, callback)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
var inputBuf = (Buffer.isBuffer(chunk)) ?
|
||||
chunk : new Buffer(chunk, encoding);
|
||||
|
||||
_this.length += inputBuf.length;
|
||||
|
||||
// Input is small enough to fit in our buffer
|
||||
if (_this.pos + inputBuf.length < _this.chunkSizeBytes) {
|
||||
inputBuf.copy(_this.bufToStore, _this.pos);
|
||||
_this.pos += inputBuf.length;
|
||||
|
||||
callback && callback();
|
||||
|
||||
// Note that we reverse the typical semantics of write's return value
|
||||
// to be compatible with node's `.pipe()` function.
|
||||
// True means client can keep writing.
|
||||
return true;
|
||||
}
|
||||
|
||||
// Otherwise, buffer is too big for current chunk, so we need to flush
|
||||
// to MongoDB.
|
||||
var inputBufRemaining = inputBuf.length;
|
||||
var spaceRemaining = _this.chunkSizeBytes - _this.pos;
|
||||
var numToCopy = Math.min(spaceRemaining, inputBuf.length);
|
||||
var outstandingRequests = 0;
|
||||
while (inputBufRemaining > 0) {
|
||||
var inputBufPos = inputBuf.length - inputBufRemaining;
|
||||
inputBuf.copy(_this.bufToStore, _this.pos,
|
||||
inputBufPos, inputBufPos + numToCopy);
|
||||
_this.pos += numToCopy;
|
||||
spaceRemaining -= numToCopy;
|
||||
if (spaceRemaining === 0) {
|
||||
_this.md5.update(_this.bufToStore);
|
||||
var doc = createChunkDoc(_this.id, _this.n, _this.bufToStore);
|
||||
++_this.state.outstandingRequests;
|
||||
++outstandingRequests;
|
||||
|
||||
if (checkAborted(_this, callback)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
_this.chunks.insert(doc, getWriteOptions(_this), function(error) {
|
||||
if (error) {
|
||||
return __handleError(_this, error);
|
||||
}
|
||||
--_this.state.outstandingRequests;
|
||||
--outstandingRequests;
|
||||
|
||||
if (!outstandingRequests) {
|
||||
_this.emit('drain', doc);
|
||||
callback && callback();
|
||||
checkDone(_this);
|
||||
}
|
||||
});
|
||||
|
||||
spaceRemaining = _this.chunkSizeBytes;
|
||||
_this.pos = 0;
|
||||
++_this.n;
|
||||
}
|
||||
inputBufRemaining -= numToCopy;
|
||||
numToCopy = Math.min(spaceRemaining, inputBufRemaining);
|
||||
}
|
||||
|
||||
// Note that we reverse the typical semantics of write's return value
|
||||
// to be compatible with node's `.pipe()` function.
|
||||
// False means the client should wait for the 'drain' event.
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
|
||||
function getWriteOptions(_this) {
|
||||
var obj = {};
|
||||
if (_this.options.writeConcern) {
|
||||
obj.w = _this.options.writeConcern.w;
|
||||
obj.wtimeout = _this.options.writeConcern.wtimeout;
|
||||
obj.j = _this.options.writeConcern.j;
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
|
||||
function waitForIndexes(_this, callback) {
|
||||
if (_this.bucket.s.checkedIndexes) {
|
||||
return callback(false);
|
||||
}
|
||||
|
||||
_this.bucket.once('index', function() {
|
||||
callback(true);
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
|
||||
function writeRemnant(_this, callback) {
|
||||
// Buffer is empty, so don't bother to insert
|
||||
if (_this.pos === 0) {
|
||||
return checkDone(_this, callback);
|
||||
}
|
||||
|
||||
++_this.state.outstandingRequests;
|
||||
|
||||
// Create a new buffer to make sure the buffer isn't bigger than it needs
|
||||
// to be.
|
||||
var remnant = new Buffer(_this.pos);
|
||||
_this.bufToStore.copy(remnant, 0, 0, _this.pos);
|
||||
_this.md5.update(remnant);
|
||||
var doc = createChunkDoc(_this.id, _this.n, remnant);
|
||||
|
||||
// If the stream was aborted, do not write remnant
|
||||
if (checkAborted(_this, callback)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
_this.chunks.insert(doc, getWriteOptions(_this), function(error) {
|
||||
if (error) {
|
||||
return __handleError(_this, error);
|
||||
}
|
||||
--_this.state.outstandingRequests;
|
||||
checkDone(_this);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
|
||||
function checkAborted(_this, callback) {
|
||||
if (_this.state.aborted) {
|
||||
if(typeof callback == 'function') {
|
||||
callback(new Error('this stream has been aborted'));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
+230
@@ -0,0 +1,230 @@
|
||||
"use strict";
|
||||
|
||||
var Binary = require('mongodb-core').BSON.Binary,
|
||||
ObjectID = require('mongodb-core').BSON.ObjectID;
|
||||
|
||||
/**
|
||||
* Class for representing a single chunk in GridFS.
|
||||
*
|
||||
* @class
|
||||
*
|
||||
* @param file {GridStore} The {@link GridStore} object holding this chunk.
|
||||
* @param mongoObject {object} The mongo object representation of this chunk.
|
||||
*
|
||||
* @throws Error when the type of data field for {@link mongoObject} is not
|
||||
* supported. Currently supported types for data field are instances of
|
||||
* {@link String}, {@link Array}, {@link Binary} and {@link Binary}
|
||||
* from the bson module
|
||||
*
|
||||
* @see Chunk#buildMongoObject
|
||||
*/
|
||||
var Chunk = function(file, mongoObject, writeConcern) {
|
||||
if(!(this instanceof Chunk)) return new Chunk(file, mongoObject);
|
||||
|
||||
this.file = file;
|
||||
var mongoObjectFinal = mongoObject == null ? {} : mongoObject;
|
||||
this.writeConcern = writeConcern || {w:1};
|
||||
this.objectId = mongoObjectFinal._id == null ? new ObjectID() : mongoObjectFinal._id;
|
||||
this.chunkNumber = mongoObjectFinal.n == null ? 0 : mongoObjectFinal.n;
|
||||
this.data = new Binary();
|
||||
|
||||
if(typeof mongoObjectFinal.data == "string") {
|
||||
var buffer = new Buffer(mongoObjectFinal.data.length);
|
||||
buffer.write(mongoObjectFinal.data, 0, mongoObjectFinal.data.length, 'binary');
|
||||
this.data = new Binary(buffer);
|
||||
} else if(Array.isArray(mongoObjectFinal.data)) {
|
||||
buffer = new Buffer(mongoObjectFinal.data.length);
|
||||
var data = mongoObjectFinal.data.join('');
|
||||
buffer.write(data, 0, data.length, 'binary');
|
||||
this.data = new Binary(buffer);
|
||||
} else if(mongoObjectFinal.data && mongoObjectFinal.data._bsontype === 'Binary') {
|
||||
this.data = mongoObjectFinal.data;
|
||||
} else if(!Buffer.isBuffer(mongoObjectFinal.data) && !(mongoObjectFinal.data == null)){
|
||||
throw Error("Illegal chunk format");
|
||||
}
|
||||
|
||||
// Update position
|
||||
this.internalPosition = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* Writes a data to this object and advance the read/write head.
|
||||
*
|
||||
* @param data {string} the data to write
|
||||
* @param callback {function(*, GridStore)} This will be called after executing
|
||||
* this method. The first parameter will contain null and the second one
|
||||
* will contain a reference to this object.
|
||||
*/
|
||||
Chunk.prototype.write = function(data, callback) {
|
||||
this.data.write(data, this.internalPosition, data.length, 'binary');
|
||||
this.internalPosition = this.data.length();
|
||||
if(callback != null) return callback(null, this);
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Reads data and advances the read/write head.
|
||||
*
|
||||
* @param length {number} The length of data to read.
|
||||
*
|
||||
* @return {string} The data read if the given length will not exceed the end of
|
||||
* the chunk. Returns an empty String otherwise.
|
||||
*/
|
||||
Chunk.prototype.read = function(length) {
|
||||
// Default to full read if no index defined
|
||||
length = length == null || length == 0 ? this.length() : length;
|
||||
|
||||
if(this.length() - this.internalPosition + 1 >= length) {
|
||||
var data = this.data.read(this.internalPosition, length);
|
||||
this.internalPosition = this.internalPosition + length;
|
||||
return data;
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
};
|
||||
|
||||
Chunk.prototype.readSlice = function(length) {
|
||||
if ((this.length() - this.internalPosition) >= length) {
|
||||
var data = null;
|
||||
if (this.data.buffer != null) { //Pure BSON
|
||||
data = this.data.buffer.slice(this.internalPosition, this.internalPosition + length);
|
||||
} else { //Native BSON
|
||||
data = new Buffer(length);
|
||||
length = this.data.readInto(data, this.internalPosition);
|
||||
}
|
||||
this.internalPosition = this.internalPosition + length;
|
||||
return data;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Checks if the read/write head is at the end.
|
||||
*
|
||||
* @return {boolean} Whether the read/write head has reached the end of this
|
||||
* chunk.
|
||||
*/
|
||||
Chunk.prototype.eof = function() {
|
||||
return this.internalPosition == this.length() ? true : false;
|
||||
};
|
||||
|
||||
/**
|
||||
* Reads one character from the data of this chunk and advances the read/write
|
||||
* head.
|
||||
*
|
||||
* @return {string} a single character data read if the the read/write head is
|
||||
* not at the end of the chunk. Returns an empty String otherwise.
|
||||
*/
|
||||
Chunk.prototype.getc = function() {
|
||||
return this.read(1);
|
||||
};
|
||||
|
||||
/**
|
||||
* Clears the contents of the data in this chunk and resets the read/write head
|
||||
* to the initial position.
|
||||
*/
|
||||
Chunk.prototype.rewind = function() {
|
||||
this.internalPosition = 0;
|
||||
this.data = new Binary();
|
||||
};
|
||||
|
||||
/**
|
||||
* Saves this chunk to the database. Also overwrites existing entries having the
|
||||
* same id as this chunk.
|
||||
*
|
||||
* @param callback {function(*, GridStore)} This will be called after executing
|
||||
* this method. The first parameter will contain null and the second one
|
||||
* will contain a reference to this object.
|
||||
*/
|
||||
Chunk.prototype.save = function(options, callback) {
|
||||
var self = this;
|
||||
if(typeof options == 'function') {
|
||||
callback = options;
|
||||
options = {};
|
||||
}
|
||||
|
||||
self.file.chunkCollection(function(err, collection) {
|
||||
if(err) return callback(err);
|
||||
|
||||
// Merge the options
|
||||
var writeOptions = { upsert: true };
|
||||
for(var name in options) writeOptions[name] = options[name];
|
||||
for(name in self.writeConcern) writeOptions[name] = self.writeConcern[name];
|
||||
|
||||
if(self.data.length() > 0) {
|
||||
self.buildMongoObject(function(mongoObject) {
|
||||
var options = {forceServerObjectId:true};
|
||||
for(var name in self.writeConcern) {
|
||||
options[name] = self.writeConcern[name];
|
||||
}
|
||||
|
||||
collection.replaceOne({'_id':self.objectId}, mongoObject, writeOptions, function(err) {
|
||||
callback(err, self);
|
||||
});
|
||||
});
|
||||
} else {
|
||||
callback(null, self);
|
||||
}
|
||||
// });
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a mongoDB object representation of this chunk.
|
||||
*
|
||||
* @param callback {function(Object)} This will be called after executing this
|
||||
* method. The object will be passed to the first parameter and will have
|
||||
* the structure:
|
||||
*
|
||||
* <pre><code>
|
||||
* {
|
||||
* '_id' : , // {number} id for this chunk
|
||||
* 'files_id' : , // {number} foreign key to the file collection
|
||||
* 'n' : , // {number} chunk number
|
||||
* 'data' : , // {bson#Binary} the chunk data itself
|
||||
* }
|
||||
* </code></pre>
|
||||
*
|
||||
* @see <a href="http://www.mongodb.org/display/DOCS/GridFS+Specification#GridFSSpecification-{{chunks}}">MongoDB GridFS Chunk Object Structure</a>
|
||||
*/
|
||||
Chunk.prototype.buildMongoObject = function(callback) {
|
||||
var mongoObject = {
|
||||
'files_id': this.file.fileId,
|
||||
'n': this.chunkNumber,
|
||||
'data': this.data};
|
||||
// If we are saving using a specific ObjectId
|
||||
if(this.objectId != null) mongoObject._id = this.objectId;
|
||||
|
||||
callback(mongoObject);
|
||||
};
|
||||
|
||||
/**
|
||||
* @return {number} the length of the data
|
||||
*/
|
||||
Chunk.prototype.length = function() {
|
||||
return this.data.length();
|
||||
};
|
||||
|
||||
/**
|
||||
* The position of the read/write head
|
||||
* @name position
|
||||
* @lends Chunk#
|
||||
* @field
|
||||
*/
|
||||
Object.defineProperty(Chunk.prototype, "position", { enumerable: true
|
||||
, get: function () {
|
||||
return this.internalPosition;
|
||||
}
|
||||
, set: function(value) {
|
||||
this.internalPosition = value;
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* The default chunk size
|
||||
* @constant
|
||||
*/
|
||||
Chunk.DEFAULT_CHUNK_SIZE = 1024 * 255;
|
||||
|
||||
module.exports = Chunk;
|
||||
+1954
File diff suppressed because it is too large
Load Diff
+64
@@ -0,0 +1,64 @@
|
||||
var f = require('util').format;
|
||||
|
||||
var Define = function(name, object, stream) {
|
||||
this.name = name;
|
||||
this.object = object;
|
||||
this.stream = typeof stream == 'boolean' ? stream : false;
|
||||
this.instrumentations = {};
|
||||
}
|
||||
|
||||
Define.prototype.classMethod = function(name, options) {
|
||||
var keys = Object.keys(options).sort();
|
||||
var key = generateKey(keys, options);
|
||||
|
||||
// Add a list of instrumentations
|
||||
if(this.instrumentations[key] == null) {
|
||||
this.instrumentations[key] = {
|
||||
methods: [], options: options
|
||||
}
|
||||
}
|
||||
|
||||
// Push to list of method for this instrumentation
|
||||
this.instrumentations[key].methods.push(name);
|
||||
}
|
||||
|
||||
var generateKey = function(keys, options) {
|
||||
var parts = [];
|
||||
for(var i = 0; i < keys.length; i++) {
|
||||
parts.push(f('%s=%s', keys[i], options[keys[i]]));
|
||||
}
|
||||
|
||||
return parts.join();
|
||||
}
|
||||
|
||||
Define.prototype.staticMethod = function(name, options) {
|
||||
options.static = true;
|
||||
var keys = Object.keys(options).sort();
|
||||
var key = generateKey(keys, options);
|
||||
|
||||
// Add a list of instrumentations
|
||||
if(this.instrumentations[key] == null) {
|
||||
this.instrumentations[key] = {
|
||||
methods: [], options: options
|
||||
}
|
||||
}
|
||||
|
||||
// Push to list of method for this instrumentation
|
||||
this.instrumentations[key].methods.push(name);
|
||||
}
|
||||
|
||||
Define.prototype.generate = function() {
|
||||
// Generate the return object
|
||||
var object = {
|
||||
name: this.name, obj: this.object, stream: this.stream,
|
||||
instrumentations: []
|
||||
}
|
||||
|
||||
for(var name in this.instrumentations) {
|
||||
object.instrumentations.push(this.instrumentations[name]);
|
||||
}
|
||||
|
||||
return object;
|
||||
}
|
||||
|
||||
module.exports = Define;
|
||||
+531
@@ -0,0 +1,531 @@
|
||||
"use strict";
|
||||
|
||||
var parse = require('./url_parser')
|
||||
, Server = require('./server')
|
||||
, Mongos = require('./mongos')
|
||||
, ReplSet = require('./replset')
|
||||
, EventEmitter = require('events').EventEmitter
|
||||
, inherits = require('util').inherits
|
||||
, Define = require('./metadata')
|
||||
, ReadPreference = require('./read_preference')
|
||||
, Logger = require('mongodb-core').Logger
|
||||
, MongoError = require('mongodb-core').MongoError
|
||||
, Db = require('./db')
|
||||
, f = require('util').format
|
||||
, assign = require('./utils').assign
|
||||
, shallowClone = require('./utils').shallowClone
|
||||
, authenticate = require('./authenticate');
|
||||
|
||||
/**
|
||||
* @fileOverview The **MongoClient** class is a class that allows for making Connections to MongoDB.
|
||||
*
|
||||
* @example
|
||||
* var MongoClient = require('mongodb').MongoClient,
|
||||
* test = require('assert');
|
||||
* // Connection url
|
||||
* var url = 'mongodb://localhost:27017/test';
|
||||
* // Connect using MongoClient
|
||||
* MongoClient.connect(url, function(err, db) {
|
||||
* // Get an additional db
|
||||
* db.close();
|
||||
* });
|
||||
*/
|
||||
var validOptionNames = ['poolSize', 'ssl', 'sslValidate', 'sslCA', 'sslCert', 'ciphers', 'ecdhCurve',
|
||||
'sslKey', 'sslPass', 'sslCRL', 'autoReconnect', 'noDelay', 'keepAlive', 'connectTimeoutMS', 'family',
|
||||
'socketTimeoutMS', 'reconnectTries', 'reconnectInterval', 'ha', 'haInterval',
|
||||
'replicaSet', 'secondaryAcceptableLatencyMS', 'acceptableLatencyMS',
|
||||
'connectWithNoPrimary', 'authSource', 'w', 'wtimeout', 'j', 'forceServerObjectId',
|
||||
'serializeFunctions', 'ignoreUndefined', 'raw', 'bufferMaxEntries',
|
||||
'readPreference', 'pkFactory', 'promiseLibrary', 'readConcern', 'maxStalenessSeconds',
|
||||
'loggerLevel', 'logger', 'promoteValues', 'promoteBuffers', 'promoteLongs',
|
||||
'domainsEnabled', 'keepAliveInitialDelay', 'checkServerIdentity', 'validateOptions', 'appname', 'auth'];
|
||||
var ignoreOptionNames = ['native_parser'];
|
||||
var legacyOptionNames = ['server', 'replset', 'replSet', 'mongos', 'db'];
|
||||
|
||||
function validOptions(options) {
|
||||
var _validOptions = validOptionNames.concat(legacyOptionNames);
|
||||
|
||||
for(var name in options) {
|
||||
if(ignoreOptionNames.indexOf(name) != -1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if(_validOptions.indexOf(name) == -1 && options.validateOptions) {
|
||||
return new MongoError(f('option %s is not supported', name));
|
||||
} else if(_validOptions.indexOf(name) == -1) {
|
||||
console.warn(f('the options [%s] is not supported', name));
|
||||
}
|
||||
|
||||
if(legacyOptionNames.indexOf(name) != -1) {
|
||||
console.warn(f('the server/replset/mongos options are deprecated, '
|
||||
+ 'all their options are supported at the top level of the options object [%s]', validOptionNames));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new MongoClient instance
|
||||
* @class
|
||||
* @return {MongoClient} a MongoClient instance.
|
||||
*/
|
||||
function MongoClient() {
|
||||
if(!(this instanceof MongoClient)) return new MongoClient();
|
||||
|
||||
// Set up event emitter
|
||||
EventEmitter.call(this);
|
||||
|
||||
/**
|
||||
* The callback format for results
|
||||
* @callback MongoClient~connectCallback
|
||||
* @param {MongoError} error An error instance representing the error during the execution.
|
||||
* @param {Db} db The connected database.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Connect to MongoDB using a url as documented at
|
||||
*
|
||||
* docs.mongodb.org/manual/reference/connection-string/
|
||||
*
|
||||
* Note that for replicasets the replicaSet query parameter is required in the 2.0 driver
|
||||
*
|
||||
* @method
|
||||
* @param {string} url The connection URI string
|
||||
* @param {object} [options] Optional settings.
|
||||
* @param {number} [options.poolSize=5] poolSize The maximum size of the individual server pool.
|
||||
* @param {boolean} [options.ssl=false] Enable SSL connection.
|
||||
* @param {Buffer} [options.sslCA=undefined] SSL Certificate store binary buffer
|
||||
* @param {Buffer} [options.sslCRL=undefined] SSL Certificate revocation list binary buffer
|
||||
* @param {Buffer} [options.sslCert=undefined] SSL Certificate binary buffer
|
||||
* @param {Buffer} [options.sslKey=undefined] SSL Key file binary buffer
|
||||
* @param {string} [options.sslPass=undefined] SSL Certificate pass phrase
|
||||
* @param {boolean|function} [options.checkServerIdentity=true] Ensure we check server identify during SSL, set to false to disable checking. Only works for Node 0.12.x or higher. You can pass in a boolean or your own checkServerIdentity override function.
|
||||
* @param {boolean} [options.autoReconnect=true] Enable autoReconnect for single server instances
|
||||
* @param {boolean} [options.noDelay=true] TCP Connection no delay
|
||||
* @param {number} [options.family=4] Version of IP stack. Defaults to 4.
|
||||
* @param {number} [options.keepAlive=30000] The number of milliseconds to wait before initiating keepAlive on the TCP socket.
|
||||
* @param {number} [options.connectTimeoutMS=30000] TCP Connection timeout setting
|
||||
* @param {number} [options.socketTimeoutMS=360000] TCP Socket timeout setting
|
||||
* @param {number} [options.reconnectTries=30] Server attempt to reconnect #times
|
||||
* @param {number} [options.reconnectInterval=1000] Server will wait # milliseconds between retries
|
||||
* @param {boolean} [options.ha=true] Control if high availability monitoring runs for Replicaset or Mongos proxies.
|
||||
* @param {number} [options.haInterval=10000] The High availability period for replicaset inquiry
|
||||
* @param {string} [options.replicaSet=undefined] The Replicaset set name
|
||||
* @param {number} [options.secondaryAcceptableLatencyMS=15] Cutoff latency point in MS for Replicaset member selection
|
||||
* @param {number} [options.acceptableLatencyMS=15] Cutoff latency point in MS for Mongos proxies selection.
|
||||
* @param {boolean} [options.connectWithNoPrimary=false] Sets if the driver should connect even if no primary is available
|
||||
* @param {string} [options.authSource=undefined] Define the database to authenticate against
|
||||
* @param {string} [options.auth.user=undefined] The username for auth
|
||||
* @param {string} [options.auth.password=undefined] The password for auth
|
||||
* @param {(number|string)} [options.w=null] The write concern.
|
||||
* @param {number} [options.wtimeout=null] The write concern timeout.
|
||||
* @param {boolean} [options.j=false] Specify a journal write concern.
|
||||
* @param {boolean} [options.forceServerObjectId=false] Force server to assign _id values instead of driver.
|
||||
* @param {boolean} [options.serializeFunctions=false] Serialize functions on any object.
|
||||
* @param {Boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields.
|
||||
* @param {boolean} [options.raw=false] Return document results as raw BSON buffers.
|
||||
* @param {boolean} [options.promoteLongs=true] Promotes Long values to number if they fit inside the 53 bits resolution.
|
||||
* @param {boolean} [options.promoteBuffers=false] Promotes Binary BSON values to native Node Buffers.
|
||||
* @param {boolean} [options.promoteValues=true] Promotes BSON values to native types where possible, set to false to only receive wrapper types.
|
||||
* @param {number} [options.bufferMaxEntries=-1] Sets a cap on how many operations the driver will buffer up before giving up on getting a working connection, default is -1 which is unlimited.
|
||||
* @param {(ReadPreference|string)} [options.readPreference=null] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
|
||||
* @param {boolean} [options.domainsEnabled=false] Enable the wrapping of the callback in the current domain, disabled by default to avoid perf hit.
|
||||
* @param {object} [options.pkFactory=null] A primary key factory object for generation of custom _id keys.
|
||||
* @param {object} [options.promiseLibrary=null] A Promise library class the application wishes to use such as Bluebird, must be ES6 compatible
|
||||
* @param {object} [options.readConcern=null] Specify a read concern for the collection. (only MongoDB 3.2 or higher supported)
|
||||
* @param {string} [options.readConcern.level='local'] Specify a read concern level for the collection operations, one of [local|majority]. (only MongoDB 3.2 or higher supported)
|
||||
* @param {number} [options.maxStalenessSeconds=undefined] The max staleness to secondary reads (values under 10 seconds cannot be guaranteed);
|
||||
* @param {string} [options.appname=undefined] The name of the application that created this MongoClient instance. MongoDB 3.4 and newer will print this value in the server log upon establishing each connection. It is also recorded in the slow query log and profile collections.
|
||||
* @param {string} [options.loggerLevel=undefined] The logging level (error/warn/info/debug)
|
||||
* @param {object} [options.logger=undefined] Custom logger object
|
||||
* @param {object} [options.validateOptions=false] Validate MongoClient passed in options for correctness.
|
||||
* @param {MongoClient~connectCallback} [callback] The command result callback
|
||||
* @return {Promise} returns Promise if no callback passed
|
||||
*/
|
||||
this.connect = MongoClient.connect;
|
||||
}
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
inherits(MongoClient, EventEmitter);
|
||||
|
||||
var define = MongoClient.define = new Define('MongoClient', MongoClient, false);
|
||||
|
||||
/**
|
||||
* Connect to MongoDB using a url as documented at
|
||||
*
|
||||
* docs.mongodb.org/manual/reference/connection-string/
|
||||
*
|
||||
* Note that for replicasets the replicaSet query parameter is required in the 2.0 driver
|
||||
*
|
||||
* @method
|
||||
* @static
|
||||
* @param {string} url The connection URI string
|
||||
* @param {object} [options] Optional settings.
|
||||
* @param {number} [options.poolSize=5] poolSize The maximum size of the individual server pool.
|
||||
* @param {boolean} [options.ssl=false] Enable SSL connection.
|
||||
* @param {Buffer} [options.sslCA=undefined] SSL Certificate store binary buffer
|
||||
* @param {Buffer} [options.sslCRL=undefined] SSL Certificate revocation list binary buffer
|
||||
* @param {Buffer} [options.sslCert=undefined] SSL Certificate binary buffer
|
||||
* @param {Buffer} [options.sslKey=undefined] SSL Key file binary buffer
|
||||
* @param {string} [options.sslPass=undefined] SSL Certificate pass phrase
|
||||
* @param {boolean|function} [options.checkServerIdentity=true] Ensure we check server identify during SSL, set to false to disable checking. Only works for Node 0.12.x or higher. You can pass in a boolean or your own checkServerIdentity override function.
|
||||
* @param {boolean} [options.autoReconnect=true] Enable autoReconnect for single server instances
|
||||
* @param {boolean} [options.noDelay=true] TCP Connection no delay
|
||||
* @param {number} [options.family=4] Version of IP stack. Defaults to 4.
|
||||
* @param {boolean} [options.keepAlive=30000] The number of milliseconds to wait before initiating keepAlive on the TCP socket.
|
||||
* @param {number} [options.connectTimeoutMS=30000] TCP Connection timeout setting
|
||||
* @param {number} [options.socketTimeoutMS=360000] TCP Socket timeout setting
|
||||
* @param {number} [options.reconnectTries=30] Server attempt to reconnect #times
|
||||
* @param {number} [options.reconnectInterval=1000] Server will wait # milliseconds between retries
|
||||
* @param {boolean} [options.ha=true] Control if high availability monitoring runs for Replicaset or Mongos proxies.
|
||||
* @param {number} [options.haInterval=10000] The High availability period for replicaset inquiry
|
||||
* @param {string} [options.replicaSet=undefined] The Replicaset set name
|
||||
* @param {number} [options.secondaryAcceptableLatencyMS=15] Cutoff latency point in MS for Replicaset member selection
|
||||
* @param {number} [options.acceptableLatencyMS=15] Cutoff latency point in MS for Mongos proxies selection.
|
||||
* @param {boolean} [options.connectWithNoPrimary=false] Sets if the driver should connect even if no primary is available
|
||||
* @param {string} [options.authSource=undefined] Define the database to authenticate against
|
||||
* @param {string} [options.auth.user=undefined] The username for auth
|
||||
* @param {string} [options.auth.password=undefined] The password for auth
|
||||
* @param {(number|string)} [options.w=null] The write concern.
|
||||
* @param {number} [options.wtimeout=null] The write concern timeout.
|
||||
* @param {boolean} [options.j=false] Specify a journal write concern.
|
||||
* @param {boolean} [options.forceServerObjectId=false] Force server to assign _id values instead of driver.
|
||||
* @param {boolean} [options.serializeFunctions=false] Serialize functions on any object.
|
||||
* @param {Boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields.
|
||||
* @param {boolean} [options.raw=false] Return document results as raw BSON buffers.
|
||||
* @param {boolean} [options.promoteLongs=true] Promotes Long values to number if they fit inside the 53 bits resolution.
|
||||
* @param {boolean} [options.promoteBuffers=false] Promotes Binary BSON values to native Node Buffers.
|
||||
* @param {boolean} [options.promoteValues=true] Promotes BSON values to native types where possible, set to false to only receive wrapper types.
|
||||
* @param {number} [options.bufferMaxEntries=-1] Sets a cap on how many operations the driver will buffer up before giving up on getting a working connection, default is -1 which is unlimited.
|
||||
* @param {(ReadPreference|string)} [options.readPreference=null] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
|
||||
* @param {boolean} [options.domainsEnabled=false] Enable the wrapping of the callback in the current domain, disabled by default to avoid perf hit.
|
||||
* @param {object} [options.pkFactory=null] A primary key factory object for generation of custom _id keys.
|
||||
* @param {object} [options.promiseLibrary=null] A Promise library class the application wishes to use such as Bluebird, must be ES6 compatible
|
||||
* @param {object} [options.readConcern=null] Specify a read concern for the collection. (only MongoDB 3.2 or higher supported)
|
||||
* @param {string} [options.readConcern.level='local'] Specify a read concern level for the collection operations, one of [local|majority]. (only MongoDB 3.2 or higher supported)
|
||||
* @param {number} [options.maxStalenessSeconds=undefined] The max staleness to secondary reads (values under 10 seconds cannot be guaranteed);
|
||||
* @param {string} [options.appname=undefined] The name of the application that created this MongoClient instance. MongoDB 3.4 and newer will print this value in the server log upon establishing each connection. It is also recorded in the slow query log and profile collections.
|
||||
* @param {string} [options.loggerLevel=undefined] The logging level (error/warn/info/debug)
|
||||
* @param {object} [options.logger=undefined] Custom logger object
|
||||
* @param {object} [options.validateOptions=false] Validate MongoClient passed in options for correctness.
|
||||
* @param {MongoClient~connectCallback} [callback] The command result callback
|
||||
* @return {Promise} returns Promise if no callback passed
|
||||
*/
|
||||
MongoClient.connect = function(url, options, callback) {
|
||||
var args = Array.prototype.slice.call(arguments, 1);
|
||||
callback = typeof args[args.length - 1] == 'function' ? args.pop() : null;
|
||||
options = args.length ? args.shift() : null;
|
||||
options = options || {};
|
||||
var self = this;
|
||||
|
||||
// Validate options object
|
||||
var err = validOptions(options);
|
||||
|
||||
// Get the promiseLibrary
|
||||
var promiseLibrary = options.promiseLibrary;
|
||||
|
||||
// No promise library selected fall back
|
||||
if(!promiseLibrary) {
|
||||
promiseLibrary = typeof global.Promise == 'function' ?
|
||||
global.Promise : require('es6-promise').Promise;
|
||||
}
|
||||
|
||||
// Return a promise
|
||||
if(typeof callback != 'function') {
|
||||
return new promiseLibrary(function(resolve, reject) {
|
||||
// Did we have a validation error
|
||||
if(err) return reject(err);
|
||||
// Attempt to connect
|
||||
connect(self, url, options, function(err, db) {
|
||||
if(err) return reject(err);
|
||||
resolve(db);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Did we have a validation error
|
||||
if(err) return callback(err);
|
||||
// Fallback to callback based connect
|
||||
connect(self, url, options, callback);
|
||||
}
|
||||
|
||||
define.staticMethod('connect', {callback: true, promise:true});
|
||||
|
||||
var mergeOptions = function(target, source, flatten) {
|
||||
for(var name in source) {
|
||||
if(source[name] && typeof source[name] == 'object' && flatten) {
|
||||
target = mergeOptions(target, source[name], flatten);
|
||||
} else {
|
||||
target[name] = source[name];
|
||||
}
|
||||
}
|
||||
|
||||
return target;
|
||||
}
|
||||
|
||||
var createUnifiedOptions = function(finalOptions, options) {
|
||||
var childOptions = ['mongos', 'server', 'db'
|
||||
, 'replset', 'db_options', 'server_options', 'rs_options', 'mongos_options'];
|
||||
var noMerge = ['readconcern'];
|
||||
|
||||
for(var name in options) {
|
||||
if(noMerge.indexOf(name.toLowerCase()) != -1) {
|
||||
finalOptions[name] = options[name];
|
||||
} else if(childOptions.indexOf(name.toLowerCase()) != -1) {
|
||||
finalOptions = mergeOptions(finalOptions, options[name], false);
|
||||
} else {
|
||||
if(options[name] && typeof options[name] == 'object' && !Buffer.isBuffer(options[name]) && !Array.isArray(options[name])) {
|
||||
finalOptions = mergeOptions(finalOptions, options[name], true);
|
||||
} else {
|
||||
finalOptions[name] = options[name];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return finalOptions;
|
||||
}
|
||||
|
||||
function translateOptions(options) {
|
||||
// If we have a readPreference passed in by the db options
|
||||
if(typeof options.readPreference == 'string' || typeof options.read_preference == 'string') {
|
||||
options.readPreference = new ReadPreference(options.readPreference || options.read_preference);
|
||||
}
|
||||
|
||||
// Do we have readPreference tags, add them
|
||||
if(options.readPreference && (options.readPreferenceTags || options.read_preference_tags)) {
|
||||
options.readPreference.tags = options.readPreferenceTags || options.read_preference_tags;
|
||||
}
|
||||
|
||||
// Do we have maxStalenessSeconds
|
||||
if(options.maxStalenessSeconds) {
|
||||
options.readPreference.maxStalenessSeconds = options.maxStalenessSeconds;
|
||||
}
|
||||
|
||||
// Set the socket and connection timeouts
|
||||
if(options.socketTimeoutMS == null) options.socketTimeoutMS = 360000;
|
||||
if(options.connectTimeoutMS == null) options.connectTimeoutMS = 30000;
|
||||
|
||||
// Create server instances
|
||||
return options.servers.map(function(serverObj) {
|
||||
return serverObj.domain_socket ?
|
||||
new Server(serverObj.domain_socket, 27017, options)
|
||||
: new Server(serverObj.host, serverObj.port, options);
|
||||
});
|
||||
}
|
||||
|
||||
//
|
||||
// Collect all events in order from SDAM
|
||||
//
|
||||
function collectEvents(self, db) {
|
||||
var collectedEvents = [];
|
||||
|
||||
if(self instanceof MongoClient) {
|
||||
var events = ["timeout", "close", 'serverOpening', 'serverDescriptionChanged', 'serverHeartbeatStarted',
|
||||
'serverHeartbeatSucceeded', 'serverHeartbeatFailed', 'serverClosed', 'topologyOpening',
|
||||
'topologyClosed', 'topologyDescriptionChanged', 'joined', 'left', 'ping', 'ha', 'all', 'fullsetup'];
|
||||
events.forEach(function(event) {
|
||||
db.serverConfig.on(event, function(object1, object2) {
|
||||
collectedEvents.push({
|
||||
event: event, object1: object1, object2: object2
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
return collectedEvents;
|
||||
}
|
||||
|
||||
//
|
||||
// Replay any events due to single server connection switching to Mongos
|
||||
//
|
||||
function replayEvents(self, events) {
|
||||
for(var i = 0; i < events.length; i++) {
|
||||
self.emit(events[i].event, events[i].object1, events[i].object2);
|
||||
}
|
||||
}
|
||||
|
||||
function relayEvents(self, db) {
|
||||
if(self instanceof MongoClient) {
|
||||
var events = ["timeout", "close", 'serverOpening', 'serverDescriptionChanged', 'serverHeartbeatStarted',
|
||||
'serverHeartbeatSucceeded', 'serverHeartbeatFailed', 'serverClosed', 'topologyOpening',
|
||||
'topologyClosed', 'topologyDescriptionChanged', 'joined', 'left', 'ping', 'ha', 'all', 'fullsetup'];
|
||||
events.forEach(function(event) {
|
||||
db.serverConfig.on(event, function(object1, object2) {
|
||||
self.emit(event, object1, object2);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function createReplicaset(self, options, callback) {
|
||||
// Set default options
|
||||
var servers = translateOptions(options);
|
||||
// Create Db instance
|
||||
var db = new Db(options.dbName, new ReplSet(servers, options), options);
|
||||
// Propegate the events to the client
|
||||
relayEvents(self, db);
|
||||
// Open the connection
|
||||
db.open(callback);
|
||||
}
|
||||
|
||||
function createMongos(self, options, callback) {
|
||||
// Set default options
|
||||
var servers = translateOptions(options);
|
||||
// Create Db instance
|
||||
var db = new Db(options.dbName, new Mongos(servers, options), options)
|
||||
// Propegate the events to the client
|
||||
relayEvents(self, db);
|
||||
// Open the connection
|
||||
db.open(callback);
|
||||
}
|
||||
|
||||
function createServer(self, options, callback) {
|
||||
// Set default options
|
||||
var servers = translateOptions(options);
|
||||
// Create db instance
|
||||
var db = new Db(options.dbName, servers[0], options);
|
||||
// Propegate the events to the client
|
||||
var collectedEvents = collectEvents(self, db);
|
||||
// Create Db instance
|
||||
db.open(function(err, db) {
|
||||
if(err) return callback(err);
|
||||
// Check if we are really speaking to a mongos
|
||||
var ismaster = db.serverConfig.lastIsMaster();
|
||||
|
||||
// Do we actually have a mongos
|
||||
if(ismaster && ismaster.msg == 'isdbgrid') {
|
||||
// Destroy the current connection
|
||||
db.close();
|
||||
// Create mongos connection instead
|
||||
return createMongos(self, options, callback);
|
||||
}
|
||||
|
||||
// Fire all the events
|
||||
replayEvents(self, collectedEvents);
|
||||
// Propegate the events to the client
|
||||
relayEvents(self, db);
|
||||
// Otherwise callback
|
||||
callback(err, db);
|
||||
});
|
||||
}
|
||||
|
||||
function connectHandler(options, callback) {
|
||||
return function (err, db) {
|
||||
if(err) {
|
||||
return process.nextTick(function() {
|
||||
try {
|
||||
callback(err, null);
|
||||
} catch (err) {
|
||||
if(db) db.close();
|
||||
throw err
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// No authentication just reconnect
|
||||
if(!options.auth) {
|
||||
return process.nextTick(function() {
|
||||
try {
|
||||
callback(err, db);
|
||||
} catch (err) {
|
||||
if(db) db.close();
|
||||
throw err
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// What db to authenticate against
|
||||
var authentication_db = db;
|
||||
if(options.authSource) {
|
||||
authentication_db = db.db(options.authSource);
|
||||
}
|
||||
|
||||
// Authenticate
|
||||
authenticate(authentication_db, options.user, options.password, options, function(err, success) {
|
||||
if(success){
|
||||
process.nextTick(function() {
|
||||
try {
|
||||
callback(null, db);
|
||||
} catch (err) {
|
||||
if(db) db.close();
|
||||
throw err
|
||||
}
|
||||
});
|
||||
} else {
|
||||
if(db) db.close();
|
||||
process.nextTick(function() {
|
||||
try {
|
||||
callback(err ? err : new Error('Could not authenticate user ' + options.auth[0]), null);
|
||||
} catch (err) {
|
||||
if(db) db.close();
|
||||
throw err
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Connect using MongoClient
|
||||
*/
|
||||
var connect = function(self, url, options, callback) {
|
||||
options = options || {};
|
||||
options = shallowClone(options);
|
||||
|
||||
// If callback is null throw an exception
|
||||
if(callback == null) {
|
||||
throw new Error("no callback function provided");
|
||||
}
|
||||
|
||||
// Get a logger for MongoClient
|
||||
var logger = Logger('MongoClient', options);
|
||||
|
||||
parse(url, options, function(err, object) {
|
||||
if (err) return callback(err);
|
||||
|
||||
// Parse the string
|
||||
var _finalOptions = createUnifiedOptions({}, object);
|
||||
_finalOptions = mergeOptions(_finalOptions, object, false);
|
||||
_finalOptions = createUnifiedOptions(_finalOptions, options);
|
||||
|
||||
// Check if we have connection and socket timeout set
|
||||
if(_finalOptions.socketTimeoutMS == null) _finalOptions.socketTimeoutMS = 360000;
|
||||
if(_finalOptions.connectTimeoutMS == null) _finalOptions.connectTimeoutMS = 30000;
|
||||
|
||||
if (_finalOptions.db_options && _finalOptions.db_options.auth) {
|
||||
delete _finalOptions.db_options.auth;
|
||||
}
|
||||
|
||||
// Failure modes
|
||||
if(object.servers.length == 0) {
|
||||
throw new Error("connection string must contain at least one seed host");
|
||||
}
|
||||
|
||||
// Do we have a replicaset then skip discovery and go straight to connectivity
|
||||
if(_finalOptions.replicaSet || _finalOptions.rs_name) {
|
||||
return createReplicaset(self, _finalOptions, connectHandler(_finalOptions, connectCallback));
|
||||
} else if(object.servers.length > 1) {
|
||||
return createMongos(self, _finalOptions, connectHandler(_finalOptions, connectCallback));
|
||||
} else {
|
||||
return createServer(self, _finalOptions, connectHandler(_finalOptions, connectCallback));
|
||||
}
|
||||
});
|
||||
|
||||
function connectCallback(err, db) {
|
||||
if(err && err.message == 'no mongos proxies found in seed list') {
|
||||
if(logger.isWarn()) {
|
||||
logger.warn(f('seed list contains no mongos proxies, replicaset connections requires the parameter replicaSet to be supplied in the URI or options object, mongodb://server:port/db?replicaSet=name'));
|
||||
}
|
||||
|
||||
// Return a more specific error message for MongoClient.connect
|
||||
return callback(new MongoError('seed list contains no mongos proxies, replicaset connections requires the parameter replicaSet to be supplied in the URI or options object, mongodb://server:port/db?replicaSet=name'));
|
||||
}
|
||||
|
||||
// Return the error and db instance
|
||||
callback(err, db);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = MongoClient
|
||||
+533
@@ -0,0 +1,533 @@
|
||||
"use strict";
|
||||
|
||||
var EventEmitter = require('events').EventEmitter
|
||||
, inherits = require('util').inherits
|
||||
, f = require('util').format
|
||||
, ServerCapabilities = require('./topology_base').ServerCapabilities
|
||||
, MongoError = require('mongodb-core').MongoError
|
||||
, CMongos = require('mongodb-core').Mongos
|
||||
, Cursor = require('./cursor')
|
||||
, AggregationCursor = require('./aggregation_cursor')
|
||||
, CommandCursor = require('./command_cursor')
|
||||
, Define = require('./metadata')
|
||||
, Server = require('./server')
|
||||
, Store = require('./topology_base').Store
|
||||
, MAX_JS_INT = require('./utils').MAX_JS_INT
|
||||
, translateOptions = require('./utils').translateOptions
|
||||
, filterOptions = require('./utils').filterOptions
|
||||
, mergeOptions = require('./utils').mergeOptions
|
||||
, getReadPreference = require('./utils').getReadPreference
|
||||
, os = require('os');
|
||||
|
||||
// Get package.json variable
|
||||
var driverVersion = require('../package.json').version;
|
||||
var nodejsversion = f('Node.js %s, %s', process.version, os.endianness());
|
||||
var type = os.type();
|
||||
var name = process.platform;
|
||||
var architecture = process.arch;
|
||||
var release = os.release();
|
||||
|
||||
/**
|
||||
* @fileOverview The **Mongos** class is a class that represents a Mongos Proxy topology and is
|
||||
* used to construct connections.
|
||||
*
|
||||
* **Mongos Should not be used, use MongoClient.connect**
|
||||
* @example
|
||||
* var Db = require('mongodb').Db,
|
||||
* Mongos = require('mongodb').Mongos,
|
||||
* Server = require('mongodb').Server,
|
||||
* test = require('assert');
|
||||
* // Connect using Mongos
|
||||
* var server = new Server('localhost', 27017);
|
||||
* var db = new Db('test', new Mongos([server]));
|
||||
* db.open(function(err, db) {
|
||||
* // Get an additional db
|
||||
* db.close();
|
||||
* });
|
||||
*/
|
||||
|
||||
// Allowed parameters
|
||||
var legalOptionNames = ['ha', 'haInterval', 'acceptableLatencyMS'
|
||||
, 'poolSize', 'ssl', 'checkServerIdentity', 'sslValidate', 'ciphers', 'ecdhCurve'
|
||||
, 'sslCA', 'sslCRL', 'sslCert', 'sslKey', 'sslPass', 'socketOptions', 'bufferMaxEntries'
|
||||
, 'store', 'auto_reconnect', 'autoReconnect', 'emitError'
|
||||
, 'keepAlive', 'noDelay', 'connectTimeoutMS', 'socketTimeoutMS'
|
||||
, 'loggerLevel', 'logger', 'reconnectTries', 'appname', 'domainsEnabled'
|
||||
, 'servername', 'promoteLongs', 'promoteValues', 'promoteBuffers'];
|
||||
|
||||
/**
|
||||
* Creates a new Mongos instance
|
||||
* @class
|
||||
* @deprecated
|
||||
* @param {Server[]} servers A seedlist of servers participating in the replicaset.
|
||||
* @param {object} [options=null] Optional settings.
|
||||
* @param {booelan} [options.ha=true] Turn on high availability monitoring.
|
||||
* @param {number} [options.haInterval=5000] Time between each replicaset status check.
|
||||
* @param {number} [options.poolSize=5] Number of connections in the connection pool for each server instance, set to 5 as default for legacy reasons.
|
||||
* @param {number} [options.acceptableLatencyMS=15] Cutoff latency point in MS for MongoS proxy selection
|
||||
* @param {boolean} [options.ssl=false] Use ssl connection (needs to have a mongod server with ssl support)
|
||||
* @param {boolean|function} [options.checkServerIdentity=true] Ensure we check server identify during SSL, set to false to disable checking. Only works for Node 0.12.x or higher. You can pass in a boolean or your own checkServerIdentity override function.
|
||||
* @param {object} [options.sslValidate=true] Validate mongod server certificate against ca (needs to have a mongod server with ssl support, 2.4 or higher)
|
||||
* @param {array} [options.sslCA=null] Array of valid certificates either as Buffers or Strings (needs to have a mongod server with ssl support, 2.4 or higher)
|
||||
* @param {array} [options.sslCRL=null] Array of revocation certificates either as Buffers or Strings (needs to have a mongod server with ssl support, 2.4 or higher)
|
||||
* @param {(Buffer|string)} [options.sslCert=null] String or buffer containing the certificate we wish to present (needs to have a mongod server with ssl support, 2.4 or higher)
|
||||
* @param {(Buffer|string)} [options.sslKey=null] String or buffer containing the certificate private key we wish to present (needs to have a mongod server with ssl support, 2.4 or higher)
|
||||
* @param {(Buffer|string)} [options.sslPass=null] String or buffer containing the certificate password (needs to have a mongod server with ssl support, 2.4 or higher)
|
||||
* @param {string} [options.servername=null] String containing the server name requested via TLS SNI.
|
||||
* @param {object} [options.socketOptions=null] Socket options
|
||||
* @param {boolean} [options.socketOptions.noDelay=true] TCP Socket NoDelay option.
|
||||
* @param {number} [options.socketOptions.keepAlive=0] TCP KeepAlive on the socket with a X ms delay before start.
|
||||
* @param {number} [options.socketOptions.connectTimeoutMS=0] TCP Connection timeout setting
|
||||
* @param {number} [options.socketOptions.socketTimeoutMS=0] TCP Socket timeout setting
|
||||
* @param {boolean} [options.domainsEnabled=false] Enable the wrapping of the callback in the current domain, disabled by default to avoid perf hit.
|
||||
* @fires Mongos#connect
|
||||
* @fires Mongos#ha
|
||||
* @fires Mongos#joined
|
||||
* @fires Mongos#left
|
||||
* @fires Mongos#fullsetup
|
||||
* @fires Mongos#open
|
||||
* @fires Mongos#close
|
||||
* @fires Mongos#error
|
||||
* @fires Mongos#timeout
|
||||
* @fires Mongos#parseError
|
||||
* @property {string} parserType the parser type used (c++ or js).
|
||||
* @return {Mongos} a Mongos instance.
|
||||
*/
|
||||
var Mongos = function(servers, options) {
|
||||
if(!(this instanceof Mongos)) return new Mongos(servers, options);
|
||||
options = options || {};
|
||||
var self = this;
|
||||
|
||||
// Filter the options
|
||||
options = filterOptions(options, legalOptionNames);
|
||||
|
||||
// Ensure all the instances are Server
|
||||
for(var i = 0; i < servers.length; i++) {
|
||||
if(!(servers[i] instanceof Server)) {
|
||||
throw MongoError.create({message: "all seed list instances must be of the Server type", driver:true});
|
||||
}
|
||||
}
|
||||
|
||||
// Stored options
|
||||
var storeOptions = {
|
||||
force: false
|
||||
, bufferMaxEntries: typeof options.bufferMaxEntries == 'number' ? options.bufferMaxEntries : MAX_JS_INT
|
||||
}
|
||||
|
||||
// Shared global store
|
||||
var store = options.store || new Store(self, storeOptions);
|
||||
|
||||
// Set up event emitter
|
||||
EventEmitter.call(this);
|
||||
|
||||
// Build seed list
|
||||
var seedlist = servers.map(function(x) {
|
||||
return {host: x.host, port: x.port}
|
||||
});
|
||||
|
||||
// Get the reconnect option
|
||||
var reconnect = typeof options.auto_reconnect == 'boolean' ? options.auto_reconnect : true;
|
||||
reconnect = typeof options.autoReconnect == 'boolean' ? options.autoReconnect : reconnect;
|
||||
|
||||
// Clone options
|
||||
var clonedOptions = mergeOptions({}, {
|
||||
disconnectHandler: store,
|
||||
cursorFactory: Cursor,
|
||||
reconnect: reconnect,
|
||||
emitError: typeof options.emitError == 'boolean' ? options.emitError : true,
|
||||
size: typeof options.poolSize == 'number' ? options.poolSize : 5
|
||||
});
|
||||
|
||||
// Translate any SSL options and other connectivity options
|
||||
clonedOptions = translateOptions(clonedOptions, options);
|
||||
|
||||
// Socket options
|
||||
var socketOptions = options.socketOptions && Object.keys(options.socketOptions).length > 0
|
||||
? options.socketOptions : options;
|
||||
|
||||
// Translate all the options to the mongodb-core ones
|
||||
clonedOptions = translateOptions(clonedOptions, socketOptions);
|
||||
if(typeof clonedOptions.keepAlive == 'number') {
|
||||
clonedOptions.keepAliveInitialDelay = clonedOptions.keepAlive;
|
||||
clonedOptions.keepAlive = clonedOptions.keepAlive > 0;
|
||||
}
|
||||
|
||||
// Build default client information
|
||||
this.clientInfo = {
|
||||
driver: {
|
||||
name: "nodejs",
|
||||
version: driverVersion
|
||||
},
|
||||
os: {
|
||||
type: type,
|
||||
name: name,
|
||||
architecture: architecture,
|
||||
version: release
|
||||
},
|
||||
platform: nodejsversion
|
||||
}
|
||||
|
||||
// Build default client information
|
||||
clonedOptions.clientInfo = this.clientInfo;
|
||||
// Do we have an application specific string
|
||||
if(options.appname) {
|
||||
clonedOptions.clientInfo.application = { name: options.appname };
|
||||
}
|
||||
|
||||
// Create the Mongos
|
||||
var mongos = new CMongos(seedlist, clonedOptions)
|
||||
// Server capabilities
|
||||
var sCapabilities = null;
|
||||
|
||||
// Internal state
|
||||
this.s = {
|
||||
// Create the Mongos
|
||||
mongos: mongos
|
||||
// Server capabilities
|
||||
, sCapabilities: sCapabilities
|
||||
// Debug turned on
|
||||
, debug: clonedOptions.debug
|
||||
// Store option defaults
|
||||
, storeOptions: storeOptions
|
||||
// Cloned options
|
||||
, clonedOptions: clonedOptions
|
||||
// Actual store of callbacks
|
||||
, store: store
|
||||
// Options
|
||||
, options: options
|
||||
}
|
||||
}
|
||||
|
||||
var define = Mongos.define = new Define('Mongos', Mongos, false);
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
inherits(Mongos, EventEmitter);
|
||||
|
||||
// Last ismaster
|
||||
Object.defineProperty(Mongos.prototype, 'isMasterDoc', {
|
||||
enumerable:true, get: function() { return this.s.mongos.lastIsMaster(); }
|
||||
});
|
||||
|
||||
Object.defineProperty(Mongos.prototype, 'parserType', {
|
||||
enumerable:true, get: function() {
|
||||
return this.s.mongos.parserType;
|
||||
}
|
||||
});
|
||||
|
||||
// BSON property
|
||||
Object.defineProperty(Mongos.prototype, 'bson', {
|
||||
enumerable: true, get: function() {
|
||||
return this.s.mongos.s.bson;
|
||||
}
|
||||
});
|
||||
|
||||
Object.defineProperty(Mongos.prototype, 'haInterval', {
|
||||
enumerable:true, get: function() { return this.s.mongos.s.haInterval; }
|
||||
});
|
||||
|
||||
// Connect
|
||||
Mongos.prototype.connect = function(db, _options, callback) {
|
||||
var self = this;
|
||||
if('function' === typeof _options) callback = _options, _options = {};
|
||||
if(_options == null) _options = {};
|
||||
if(!('function' === typeof callback)) callback = null;
|
||||
self.s.options = _options;
|
||||
|
||||
// Update bufferMaxEntries
|
||||
self.s.storeOptions.bufferMaxEntries = db.bufferMaxEntries;
|
||||
|
||||
// Error handler
|
||||
var connectErrorHandler = function() {
|
||||
return function(err) {
|
||||
// Remove all event handlers
|
||||
var events = ['timeout', 'error', 'close'];
|
||||
events.forEach(function(e) {
|
||||
self.removeListener(e, connectErrorHandler);
|
||||
});
|
||||
|
||||
self.s.mongos.removeListener('connect', connectErrorHandler);
|
||||
|
||||
// Try to callback
|
||||
try {
|
||||
callback(err);
|
||||
} catch(err) {
|
||||
process.nextTick(function() { throw err; })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Actual handler
|
||||
var errorHandler = function(event) {
|
||||
return function(err) {
|
||||
if(event != 'error') {
|
||||
self.emit(event, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Error handler
|
||||
var reconnectHandler = function() {
|
||||
self.emit('reconnect');
|
||||
self.s.store.execute();
|
||||
}
|
||||
|
||||
// relay the event
|
||||
var relay = function(event) {
|
||||
return function(t, server) {
|
||||
self.emit(event, t, server);
|
||||
}
|
||||
}
|
||||
|
||||
// Connect handler
|
||||
var connectHandler = function() {
|
||||
// Clear out all the current handlers left over
|
||||
var events = ["timeout", "error", "close", 'fullsetup'];
|
||||
events.forEach(function(e) {
|
||||
self.s.mongos.removeAllListeners(e);
|
||||
});
|
||||
|
||||
// Set up listeners
|
||||
self.s.mongos.once('timeout', errorHandler('timeout'));
|
||||
self.s.mongos.once('error', errorHandler('error'));
|
||||
self.s.mongos.once('close', errorHandler('close'));
|
||||
|
||||
// Set up serverConfig listeners
|
||||
self.s.mongos.on('fullsetup', function() { self.emit('fullsetup', self); });
|
||||
|
||||
// Emit open event
|
||||
self.emit('open', null, self);
|
||||
|
||||
// Return correctly
|
||||
try {
|
||||
callback(null, self);
|
||||
} catch(err) {
|
||||
process.nextTick(function() { throw err; })
|
||||
}
|
||||
}
|
||||
|
||||
// Clear out all the current handlers left over
|
||||
var events = ["timeout", "error", "close", 'serverOpening', 'serverDescriptionChanged', 'serverHeartbeatStarted',
|
||||
'serverHeartbeatSucceeded', 'serverHeartbeatFailed', 'serverClosed', 'topologyOpening',
|
||||
'topologyClosed', 'topologyDescriptionChanged'];
|
||||
events.forEach(function(e) {
|
||||
self.s.mongos.removeAllListeners(e);
|
||||
});
|
||||
|
||||
// Set up SDAM listeners
|
||||
self.s.mongos.on('serverDescriptionChanged', relay('serverDescriptionChanged'));
|
||||
self.s.mongos.on('serverHeartbeatStarted', relay('serverHeartbeatStarted'));
|
||||
self.s.mongos.on('serverHeartbeatSucceeded', relay('serverHeartbeatSucceeded'));
|
||||
self.s.mongos.on('serverHeartbeatFailed', relay('serverHeartbeatFailed'));
|
||||
self.s.mongos.on('serverOpening', relay('serverOpening'));
|
||||
self.s.mongos.on('serverClosed', relay('serverClosed'));
|
||||
self.s.mongos.on('topologyOpening', relay('topologyOpening'));
|
||||
self.s.mongos.on('topologyClosed', relay('topologyClosed'));
|
||||
self.s.mongos.on('topologyDescriptionChanged', relay('topologyDescriptionChanged'));
|
||||
|
||||
// Set up listeners
|
||||
self.s.mongos.once('timeout', connectErrorHandler('timeout'));
|
||||
self.s.mongos.once('error', connectErrorHandler('error'));
|
||||
self.s.mongos.once('close', connectErrorHandler('close'));
|
||||
self.s.mongos.once('connect', connectHandler);
|
||||
// Join and leave events
|
||||
self.s.mongos.on('joined', relay('joined'));
|
||||
self.s.mongos.on('left', relay('left'));
|
||||
|
||||
// Reconnect server
|
||||
self.s.mongos.on('reconnect', reconnectHandler);
|
||||
|
||||
// Start connection
|
||||
self.s.mongos.connect(_options);
|
||||
}
|
||||
|
||||
// Server capabilities
|
||||
Mongos.prototype.capabilities = function() {
|
||||
if(this.s.sCapabilities) return this.s.sCapabilities;
|
||||
if(this.s.mongos.lastIsMaster() == null) return null;
|
||||
this.s.sCapabilities = new ServerCapabilities(this.s.mongos.lastIsMaster());
|
||||
return this.s.sCapabilities;
|
||||
}
|
||||
|
||||
define.classMethod('capabilities', {callback: false, promise:false, returns: [ServerCapabilities]});
|
||||
|
||||
// Command
|
||||
Mongos.prototype.command = function(ns, cmd, options, callback) {
|
||||
this.s.mongos.command(ns, cmd, getReadPreference(options), callback);
|
||||
}
|
||||
|
||||
define.classMethod('command', {callback: true, promise:false});
|
||||
|
||||
// Insert
|
||||
Mongos.prototype.insert = function(ns, ops, options, callback) {
|
||||
this.s.mongos.insert(ns, ops, options, function(e, m) {
|
||||
callback(e, m)
|
||||
});
|
||||
}
|
||||
|
||||
define.classMethod('insert', {callback: true, promise:false});
|
||||
|
||||
// Update
|
||||
Mongos.prototype.update = function(ns, ops, options, callback) {
|
||||
this.s.mongos.update(ns, ops, options, callback);
|
||||
}
|
||||
|
||||
define.classMethod('update', {callback: true, promise:false});
|
||||
|
||||
// Remove
|
||||
Mongos.prototype.remove = function(ns, ops, options, callback) {
|
||||
this.s.mongos.remove(ns, ops, options, callback);
|
||||
}
|
||||
|
||||
define.classMethod('remove', {callback: true, promise:false});
|
||||
|
||||
// Destroyed
|
||||
Mongos.prototype.isDestroyed = function() {
|
||||
return this.s.mongos.isDestroyed();
|
||||
}
|
||||
|
||||
// IsConnected
|
||||
Mongos.prototype.isConnected = function() {
|
||||
return this.s.mongos.isConnected();
|
||||
}
|
||||
|
||||
define.classMethod('isConnected', {callback: false, promise:false, returns: [Boolean]});
|
||||
|
||||
// Insert
|
||||
Mongos.prototype.cursor = function(ns, cmd, options) {
|
||||
options.disconnectHandler = this.s.store;
|
||||
return this.s.mongos.cursor(ns, cmd, options);
|
||||
}
|
||||
|
||||
define.classMethod('cursor', {callback: false, promise:false, returns: [Cursor, AggregationCursor, CommandCursor]});
|
||||
|
||||
Mongos.prototype.lastIsMaster = function() {
|
||||
return this.s.mongos.lastIsMaster();
|
||||
}
|
||||
|
||||
/**
|
||||
* Unref all sockets
|
||||
* @method
|
||||
*/
|
||||
Mongos.prototype.unref = function () {
|
||||
return this.s.mongos.unref();
|
||||
}
|
||||
|
||||
Mongos.prototype.close = function(forceClosed) {
|
||||
this.s.mongos.destroy({
|
||||
force: typeof forceClosed == 'boolean' ? forceClosed : false,
|
||||
});
|
||||
// We need to wash out all stored processes
|
||||
if(forceClosed == true) {
|
||||
this.s.storeOptions.force = forceClosed;
|
||||
this.s.store.flush();
|
||||
}
|
||||
}
|
||||
|
||||
define.classMethod('close', {callback: false, promise:false});
|
||||
|
||||
Mongos.prototype.auth = function() {
|
||||
var args = Array.prototype.slice.call(arguments, 0);
|
||||
this.s.mongos.auth.apply(this.s.mongos, args);
|
||||
}
|
||||
|
||||
define.classMethod('auth', {callback: true, promise:false});
|
||||
|
||||
Mongos.prototype.logout = function() {
|
||||
var args = Array.prototype.slice.call(arguments, 0);
|
||||
this.s.mongos.logout.apply(this.s.mongos, args);
|
||||
}
|
||||
|
||||
define.classMethod('logout', {callback: true, promise:false});
|
||||
|
||||
/**
|
||||
* All raw connections
|
||||
* @method
|
||||
* @return {array}
|
||||
*/
|
||||
Mongos.prototype.connections = function() {
|
||||
return this.s.mongos.connections();
|
||||
}
|
||||
|
||||
define.classMethod('connections', {callback: false, promise:false, returns:[Array]});
|
||||
|
||||
/**
|
||||
* A mongos connect event, used to verify that the connection is up and running
|
||||
*
|
||||
* @event Mongos#connect
|
||||
* @type {Mongos}
|
||||
*/
|
||||
|
||||
/**
|
||||
* The mongos high availability event
|
||||
*
|
||||
* @event Mongos#ha
|
||||
* @type {function}
|
||||
* @param {string} type The stage in the high availability event (start|end)
|
||||
* @param {boolean} data.norepeat This is a repeating high availability process or a single execution only
|
||||
* @param {number} data.id The id for this high availability request
|
||||
* @param {object} data.state An object containing the information about the current replicaset
|
||||
*/
|
||||
|
||||
/**
|
||||
* A server member left the mongos set
|
||||
*
|
||||
* @event Mongos#left
|
||||
* @type {function}
|
||||
* @param {string} type The type of member that left (primary|secondary|arbiter)
|
||||
* @param {Server} server The server object that left
|
||||
*/
|
||||
|
||||
/**
|
||||
* A server member joined the mongos set
|
||||
*
|
||||
* @event Mongos#joined
|
||||
* @type {function}
|
||||
* @param {string} type The type of member that joined (primary|secondary|arbiter)
|
||||
* @param {Server} server The server object that joined
|
||||
*/
|
||||
|
||||
/**
|
||||
* Mongos fullsetup event, emitted when all proxies in the topology have been connected to.
|
||||
*
|
||||
* @event Mongos#fullsetup
|
||||
* @type {Mongos}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Mongos open event, emitted when mongos can start processing commands.
|
||||
*
|
||||
* @event Mongos#open
|
||||
* @type {Mongos}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Mongos close event
|
||||
*
|
||||
* @event Mongos#close
|
||||
* @type {object}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Mongos error event, emitted if there is an error listener.
|
||||
*
|
||||
* @event Mongos#error
|
||||
* @type {MongoError}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Mongos timeout event
|
||||
*
|
||||
* @event Mongos#timeout
|
||||
* @type {object}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Mongos parseError event
|
||||
*
|
||||
* @event Mongos#parseError
|
||||
* @type {object}
|
||||
*/
|
||||
|
||||
module.exports = Mongos;
|
||||
+131
@@ -0,0 +1,131 @@
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* @fileOverview The **ReadPreference** class is a class that represents a MongoDB ReadPreference and is
|
||||
* used to construct connections.
|
||||
*
|
||||
* @example
|
||||
* var Db = require('mongodb').Db,
|
||||
* ReplSet = require('mongodb').ReplSet,
|
||||
* Server = require('mongodb').Server,
|
||||
* ReadPreference = require('mongodb').ReadPreference,
|
||||
* test = require('assert');
|
||||
* // Connect using ReplSet
|
||||
* var server = new Server('localhost', 27017);
|
||||
* var db = new Db('test', new ReplSet([server]));
|
||||
* db.open(function(err, db) {
|
||||
* test.equal(null, err);
|
||||
* // Perform a read
|
||||
* var cursor = db.collection('t').find({});
|
||||
* cursor.setReadPreference(ReadPreference.PRIMARY);
|
||||
* cursor.toArray(function(err, docs) {
|
||||
* test.equal(null, err);
|
||||
* db.close();
|
||||
* });
|
||||
* });
|
||||
*/
|
||||
|
||||
/**
|
||||
* Creates a new ReadPreference instance
|
||||
*
|
||||
* Read Preferences
|
||||
* - **ReadPreference.PRIMARY**, Read from primary only. All operations produce an error (throw an exception where applicable) if primary is unavailable. Cannot be combined with tags (This is the default.).
|
||||
* - **ReadPreference.PRIMARY_PREFERRED**, Read from primary if available, otherwise a secondary.
|
||||
* - **ReadPreference.SECONDARY**, Read from secondary if available, otherwise error.
|
||||
* - **ReadPreference.SECONDARY_PREFERRED**, Read from a secondary if available, otherwise read from the primary.
|
||||
* - **ReadPreference.NEAREST**, All modes read from among the nearest candidates, but unlike other modes, NEAREST will include both the primary and all secondaries in the random selection.
|
||||
*
|
||||
* @class
|
||||
* @param {string} mode The ReadPreference mode as listed above.
|
||||
* @param {array|object} tags An object representing read preference tags.
|
||||
* @param {object} [options] Additional read preference options
|
||||
* @param {number} [options.maxStalenessSeconds] Max Secondary Read Staleness in Seconds
|
||||
* @return {ReadPreference} a ReadPreference instance.
|
||||
*/
|
||||
var ReadPreference = function(mode, tags, options) {
|
||||
if(!(this instanceof ReadPreference)) {
|
||||
return new ReadPreference(mode, tags, options);
|
||||
}
|
||||
|
||||
this._type = 'ReadPreference';
|
||||
this.mode = mode;
|
||||
this.tags = tags;
|
||||
this.options = options;
|
||||
|
||||
// If no tags were passed in
|
||||
if(tags && typeof tags == 'object' && !Array.isArray(tags)) {
|
||||
if(tags.maxStalenessSeconds) {
|
||||
this.options = tags;
|
||||
this.tags = null;
|
||||
}
|
||||
}
|
||||
|
||||
// Add the maxStalenessSeconds value to the read Preference
|
||||
if(this.options && this.options.maxStalenessSeconds) {
|
||||
this.maxStalenessSeconds = this.options.maxStalenessSeconds;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate if a mode is legal
|
||||
*
|
||||
* @method
|
||||
* @param {string} mode The string representing the read preference mode.
|
||||
* @return {boolean}
|
||||
*/
|
||||
ReadPreference.isValid = function(_mode) {
|
||||
return (_mode == ReadPreference.PRIMARY || _mode == ReadPreference.PRIMARY_PREFERRED
|
||||
|| _mode == ReadPreference.SECONDARY || _mode == ReadPreference.SECONDARY_PREFERRED
|
||||
|| _mode == ReadPreference.NEAREST
|
||||
|| _mode == true || _mode == false || _mode == null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate if a mode is legal
|
||||
*
|
||||
* @method
|
||||
* @param {string} mode The string representing the read preference mode.
|
||||
* @return {boolean}
|
||||
*/
|
||||
ReadPreference.prototype.isValid = function(mode) {
|
||||
var _mode = typeof mode == 'string' ? mode : this.mode;
|
||||
return ReadPreference.isValid(_mode);
|
||||
}
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
ReadPreference.prototype.toObject = function() {
|
||||
var object = {mode:this.mode};
|
||||
|
||||
if(this.tags != null) {
|
||||
object['tags'] = this.tags;
|
||||
}
|
||||
|
||||
if(this.maxStalenessSeconds) {
|
||||
object['maxStalenessSeconds'] = this.maxStalenessSeconds;
|
||||
}
|
||||
|
||||
return object;
|
||||
}
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
ReadPreference.prototype.toJSON = function() {
|
||||
return this.toObject();
|
||||
}
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
ReadPreference.PRIMARY = 'primary';
|
||||
ReadPreference.PRIMARY_PREFERRED = 'primaryPreferred';
|
||||
ReadPreference.SECONDARY = 'secondary';
|
||||
ReadPreference.SECONDARY_PREFERRED = 'secondaryPreferred';
|
||||
ReadPreference.NEAREST = 'nearest'
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
module.exports = ReadPreference;
|
||||
+582
@@ -0,0 +1,582 @@
|
||||
"use strict";
|
||||
|
||||
var EventEmitter = require('events').EventEmitter
|
||||
, inherits = require('util').inherits
|
||||
, f = require('util').format
|
||||
, Server = require('./server')
|
||||
, Cursor = require('./cursor')
|
||||
, AggregationCursor = require('./aggregation_cursor')
|
||||
, CommandCursor = require('./command_cursor')
|
||||
, ReadPreference = require('./read_preference')
|
||||
, MongoError = require('mongodb-core').MongoError
|
||||
, ServerCapabilities = require('./topology_base').ServerCapabilities
|
||||
, Store = require('./topology_base').Store
|
||||
, Define = require('./metadata')
|
||||
, CReplSet = require('mongodb-core').ReplSet
|
||||
, CoreReadPreference = require('mongodb-core').ReadPreference
|
||||
, MAX_JS_INT = require('./utils').MAX_JS_INT
|
||||
, translateOptions = require('./utils').translateOptions
|
||||
, filterOptions = require('./utils').filterOptions
|
||||
, getReadPreference = require('./utils').getReadPreference
|
||||
, mergeOptions = require('./utils').mergeOptions
|
||||
, os = require('os');
|
||||
/**
|
||||
* @fileOverview The **ReplSet** class is a class that represents a Replicaset topology and is
|
||||
* used to construct connections.
|
||||
*
|
||||
* **ReplSet Should not be used, use MongoClient.connect**
|
||||
* @example
|
||||
* var Db = require('mongodb').Db,
|
||||
* ReplSet = require('mongodb').ReplSet,
|
||||
* Server = require('mongodb').Server,
|
||||
* test = require('assert');
|
||||
* // Connect using ReplSet
|
||||
* var server = new Server('localhost', 27017);
|
||||
* var db = new Db('test', new ReplSet([server]));
|
||||
* db.open(function(err, db) {
|
||||
* // Get an additional db
|
||||
* db.close();
|
||||
* });
|
||||
*/
|
||||
|
||||
// Allowed parameters
|
||||
var legalOptionNames = ['ha', 'haInterval', 'replicaSet', 'rs_name', 'secondaryAcceptableLatencyMS'
|
||||
, 'connectWithNoPrimary', 'poolSize', 'ssl', 'checkServerIdentity', 'sslValidate'
|
||||
, 'sslCA', 'sslCert', 'sslCRL', 'sslKey', 'sslPass', 'socketOptions', 'bufferMaxEntries'
|
||||
, 'store', 'auto_reconnect', 'autoReconnect', 'emitError'
|
||||
, 'keepAlive', 'noDelay', 'connectTimeoutMS', 'socketTimeoutMS', 'strategy', 'debug', 'family'
|
||||
, 'loggerLevel', 'logger', 'reconnectTries', 'appname', 'domainsEnabled'
|
||||
, 'servername', 'promoteLongs', 'promoteValues', 'promoteBuffers', 'maxStalenessSeconds'];
|
||||
|
||||
// Get package.json variable
|
||||
var driverVersion = require('../package.json').version;
|
||||
var nodejsversion = f('Node.js %s, %s', process.version, os.endianness());
|
||||
var type = os.type();
|
||||
var name = process.platform;
|
||||
var architecture = process.arch;
|
||||
var release = os.release();
|
||||
|
||||
/**
|
||||
* Creates a new ReplSet instance
|
||||
* @class
|
||||
* @deprecated
|
||||
* @param {Server[]} servers A seedlist of servers participating in the replicaset.
|
||||
* @param {object} [options=null] Optional settings.
|
||||
* @param {boolean} [options.ha=true] Turn on high availability monitoring.
|
||||
* @param {number} [options.haInterval=10000] Time between each replicaset status check.
|
||||
* @param {string} [options.replicaSet] The name of the replicaset to connect to.
|
||||
* @param {number} [options.secondaryAcceptableLatencyMS=15] Sets the range of servers to pick when using NEAREST (lowest ping ms + the latency fence, ex: range of 1 to (1 + 15) ms)
|
||||
* @param {boolean} [options.connectWithNoPrimary=false] Sets if the driver should connect even if no primary is available
|
||||
* @param {number} [options.poolSize=5] Number of connections in the connection pool for each server instance, set to 5 as default for legacy reasons.
|
||||
* @param {boolean} [options.ssl=false] Use ssl connection (needs to have a mongod server with ssl support)
|
||||
* @param {boolean|function} [options.checkServerIdentity=true] Ensure we check server identify during SSL, set to false to disable checking. Only works for Node 0.12.x or higher. You can pass in a boolean or your own checkServerIdentity override function.
|
||||
* @param {object} [options.sslValidate=true] Validate mongod server certificate against ca (needs to have a mongod server with ssl support, 2.4 or higher)
|
||||
* @param {array} [options.sslCA=null] Array of valid certificates either as Buffers or Strings (needs to have a mongod server with ssl support, 2.4 or higher)
|
||||
* @param {array} [options.sslCRL=null] Array of revocation certificates either as Buffers or Strings (needs to have a mongod server with ssl support, 2.4 or higher)
|
||||
* @param {(Buffer|string)} [options.sslCert=null] String or buffer containing the certificate we wish to present (needs to have a mongod server with ssl support, 2.4 or higher)
|
||||
* @param {(Buffer|string)} [options.sslKey=null] String or buffer containing the certificate private key we wish to present (needs to have a mongod server with ssl support, 2.4 or higher)
|
||||
* @param {(Buffer|string)} [options.sslPass=null] String or buffer containing the certificate password (needs to have a mongod server with ssl support, 2.4 or higher)
|
||||
* @param {string} [options.servername=null] String containing the server name requested via TLS SNI.
|
||||
* @param {object} [options.socketOptions=null] Socket options
|
||||
* @param {boolean} [options.socketOptions.noDelay=true] TCP Socket NoDelay option.
|
||||
* @param {number} [options.socketOptions.keepAlive=0] TCP KeepAlive on the socket with a X ms delay before start.
|
||||
* @param {number} [options.socketOptions.connectTimeoutMS=10000] TCP Connection timeout setting
|
||||
* @param {number} [options.socketOptions.socketTimeoutMS=0] TCP Socket timeout setting
|
||||
* @param {boolean} [options.domainsEnabled=false] Enable the wrapping of the callback in the current domain, disabled by default to avoid perf hit.
|
||||
* @param {number} [options.maxStalenessSeconds=undefined] The max staleness to secondary reads (values under 10 seconds cannot be guaranteed);
|
||||
* @fires ReplSet#connect
|
||||
* @fires ReplSet#ha
|
||||
* @fires ReplSet#joined
|
||||
* @fires ReplSet#left
|
||||
* @fires ReplSet#fullsetup
|
||||
* @fires ReplSet#open
|
||||
* @fires ReplSet#close
|
||||
* @fires ReplSet#error
|
||||
* @fires ReplSet#timeout
|
||||
* @fires ReplSet#parseError
|
||||
* @property {string} parserType the parser type used (c++ or js).
|
||||
* @return {ReplSet} a ReplSet instance.
|
||||
*/
|
||||
var ReplSet = function(servers, options) {
|
||||
if(!(this instanceof ReplSet)) return new ReplSet(servers, options);
|
||||
options = options || {};
|
||||
var self = this;
|
||||
// Set up event emitter
|
||||
EventEmitter.call(this);
|
||||
|
||||
// Filter the options
|
||||
options = filterOptions(options, legalOptionNames);
|
||||
|
||||
// Ensure all the instances are Server
|
||||
for(var i = 0; i < servers.length; i++) {
|
||||
if(!(servers[i] instanceof Server)) {
|
||||
throw MongoError.create({message: "all seed list instances must be of the Server type", driver:true});
|
||||
}
|
||||
}
|
||||
|
||||
// Stored options
|
||||
var storeOptions = {
|
||||
force: false
|
||||
, bufferMaxEntries: typeof options.bufferMaxEntries == 'number' ? options.bufferMaxEntries : MAX_JS_INT
|
||||
}
|
||||
|
||||
// Shared global store
|
||||
var store = options.store || new Store(self, storeOptions);
|
||||
|
||||
// Build seed list
|
||||
var seedlist = servers.map(function(x) {
|
||||
return {host: x.host, port: x.port}
|
||||
});
|
||||
|
||||
// Clone options
|
||||
var clonedOptions = mergeOptions({}, {
|
||||
disconnectHandler: store,
|
||||
cursorFactory: Cursor,
|
||||
reconnect: false,
|
||||
emitError: typeof options.emitError == 'boolean' ? options.emitError : true,
|
||||
size: typeof options.poolSize == 'number' ? options.poolSize : 5
|
||||
});
|
||||
|
||||
// Translate any SSL options and other connectivity options
|
||||
clonedOptions = translateOptions(clonedOptions, options);
|
||||
|
||||
// Socket options
|
||||
var socketOptions = options.socketOptions && Object.keys(options.socketOptions).length > 0
|
||||
? options.socketOptions : options;
|
||||
|
||||
// Translate all the options to the mongodb-core ones
|
||||
clonedOptions = translateOptions(clonedOptions, socketOptions);
|
||||
if(typeof clonedOptions.keepAlive == 'number') {
|
||||
clonedOptions.keepAliveInitialDelay = clonedOptions.keepAlive;
|
||||
clonedOptions.keepAlive = clonedOptions.keepAlive > 0;
|
||||
}
|
||||
|
||||
// Client info
|
||||
this.clientInfo = {
|
||||
driver: {
|
||||
name: "nodejs",
|
||||
version: driverVersion
|
||||
},
|
||||
os: {
|
||||
type: type,
|
||||
name: name,
|
||||
architecture: architecture,
|
||||
version: release
|
||||
},
|
||||
platform: nodejsversion
|
||||
}
|
||||
|
||||
// Build default client information
|
||||
clonedOptions.clientInfo = this.clientInfo;
|
||||
// Do we have an application specific string
|
||||
if(options.appname) {
|
||||
clonedOptions.clientInfo.application = { name: options.appname };
|
||||
}
|
||||
|
||||
// Create the ReplSet
|
||||
var replset = new CReplSet(seedlist, clonedOptions);
|
||||
|
||||
// Listen to reconnect event
|
||||
replset.on('reconnect', function() {
|
||||
self.emit('reconnect');
|
||||
store.execute();
|
||||
});
|
||||
|
||||
// Internal state
|
||||
this.s = {
|
||||
// Replicaset
|
||||
replset: replset
|
||||
// Server capabilities
|
||||
, sCapabilities: null
|
||||
// Debug tag
|
||||
, tag: options.tag
|
||||
// Store options
|
||||
, storeOptions: storeOptions
|
||||
// Cloned options
|
||||
, clonedOptions: clonedOptions
|
||||
// Store
|
||||
, store: store
|
||||
// Options
|
||||
, options: options
|
||||
}
|
||||
|
||||
// Debug
|
||||
if(clonedOptions.debug) {
|
||||
// Last ismaster
|
||||
Object.defineProperty(this, 'replset', {
|
||||
enumerable:true, get: function() { return replset; }
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
inherits(ReplSet, EventEmitter);
|
||||
|
||||
// Last ismaster
|
||||
Object.defineProperty(ReplSet.prototype, 'isMasterDoc', {
|
||||
enumerable:true, get: function() { return this.s.replset.lastIsMaster(); }
|
||||
});
|
||||
|
||||
Object.defineProperty(ReplSet.prototype, 'parserType', {
|
||||
enumerable:true, get: function() {
|
||||
return this.s.replset.parserType;
|
||||
}
|
||||
});
|
||||
|
||||
// BSON property
|
||||
Object.defineProperty(ReplSet.prototype, 'bson', {
|
||||
enumerable: true, get: function() {
|
||||
return this.s.replset.s.bson;
|
||||
}
|
||||
});
|
||||
|
||||
Object.defineProperty(ReplSet.prototype, 'haInterval', {
|
||||
enumerable:true, get: function() { return this.s.replset.s.haInterval; }
|
||||
});
|
||||
|
||||
var define = ReplSet.define = new Define('ReplSet', ReplSet, false);
|
||||
|
||||
// Ensure the right read Preference object
|
||||
var translateReadPreference = function(options) {
|
||||
if(typeof options.readPreference == 'string') {
|
||||
options.readPreference = new CoreReadPreference(options.readPreference);
|
||||
} else if(options.readPreference instanceof ReadPreference) {
|
||||
options.readPreference = new CoreReadPreference(options.readPreference.mode
|
||||
, options.readPreference.tags, {maxStalenessSeconds: options.readPreference.maxStalenessSeconds});
|
||||
}
|
||||
|
||||
return options;
|
||||
}
|
||||
|
||||
// Connect method
|
||||
ReplSet.prototype.connect = function(db, _options, callback) {
|
||||
var self = this;
|
||||
if('function' === typeof _options) callback = _options, _options = {};
|
||||
if(_options == null) _options = {};
|
||||
if(!('function' === typeof callback)) callback = null;
|
||||
self.s.options = _options;
|
||||
|
||||
// Update bufferMaxEntries
|
||||
self.s.storeOptions.bufferMaxEntries = db.bufferMaxEntries;
|
||||
|
||||
// Actual handler
|
||||
var errorHandler = function(event) {
|
||||
return function(err) {
|
||||
if(event != 'error') {
|
||||
self.emit(event, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Clear out all the current handlers left over
|
||||
var events = ["timeout", "error", "close", 'serverOpening', 'serverDescriptionChanged', 'serverHeartbeatStarted',
|
||||
'serverHeartbeatSucceeded', 'serverHeartbeatFailed', 'serverClosed', 'topologyOpening',
|
||||
'topologyClosed', 'topologyDescriptionChanged', 'joined', 'left', 'ping', 'ha'];
|
||||
events.forEach(function(e) {
|
||||
self.s.replset.removeAllListeners(e);
|
||||
});
|
||||
|
||||
// relay the event
|
||||
var relay = function(event) {
|
||||
return function(t, server) {
|
||||
self.emit(event, t, server);
|
||||
}
|
||||
}
|
||||
|
||||
// Replset events relay
|
||||
var replsetRelay = function(event) {
|
||||
return function(t, server) {
|
||||
self.emit(event, t, server.lastIsMaster(), server);
|
||||
}
|
||||
}
|
||||
|
||||
// Relay ha
|
||||
var relayHa = function(t, state) {
|
||||
self.emit('ha', t, state);
|
||||
|
||||
if(t == 'start') {
|
||||
self.emit('ha_connect', t, state);
|
||||
} else if(t == 'end') {
|
||||
self.emit('ha_ismaster', t, state);
|
||||
}
|
||||
}
|
||||
|
||||
// Set up serverConfig listeners
|
||||
self.s.replset.on('joined', replsetRelay('joined'));
|
||||
self.s.replset.on('left', relay('left'));
|
||||
self.s.replset.on('ping', relay('ping'));
|
||||
self.s.replset.on('ha', relayHa);
|
||||
|
||||
// Set up SDAM listeners
|
||||
self.s.replset.on('serverDescriptionChanged', relay('serverDescriptionChanged'));
|
||||
self.s.replset.on('serverHeartbeatStarted', relay('serverHeartbeatStarted'));
|
||||
self.s.replset.on('serverHeartbeatSucceeded', relay('serverHeartbeatSucceeded'));
|
||||
self.s.replset.on('serverHeartbeatFailed', relay('serverHeartbeatFailed'));
|
||||
self.s.replset.on('serverOpening', relay('serverOpening'));
|
||||
self.s.replset.on('serverClosed', relay('serverClosed'));
|
||||
self.s.replset.on('topologyOpening', relay('topologyOpening'));
|
||||
self.s.replset.on('topologyClosed', relay('topologyClosed'));
|
||||
self.s.replset.on('topologyDescriptionChanged', relay('topologyDescriptionChanged'));
|
||||
|
||||
self.s.replset.on('fullsetup', function() {
|
||||
self.emit('fullsetup', self, self);
|
||||
});
|
||||
|
||||
self.s.replset.on('all', function() {
|
||||
self.emit('all', null, self);
|
||||
});
|
||||
|
||||
// Connect handler
|
||||
var connectHandler = function() {
|
||||
// Set up listeners
|
||||
self.s.replset.once('timeout', errorHandler('timeout'));
|
||||
self.s.replset.once('error', errorHandler('error'));
|
||||
self.s.replset.once('close', errorHandler('close'));
|
||||
|
||||
// Emit open event
|
||||
self.emit('open', null, self);
|
||||
|
||||
// Return correctly
|
||||
try {
|
||||
callback(null, self);
|
||||
} catch(err) {
|
||||
process.nextTick(function() { throw err; })
|
||||
}
|
||||
}
|
||||
|
||||
// Error handler
|
||||
var connectErrorHandler = function() {
|
||||
return function(err) {
|
||||
['timeout', 'error', 'close'].forEach(function(e) {
|
||||
self.s.replset.removeListener(e, connectErrorHandler);
|
||||
});
|
||||
|
||||
self.s.replset.removeListener('connect', connectErrorHandler);
|
||||
// Destroy the replset
|
||||
self.s.replset.destroy();
|
||||
|
||||
// Try to callback
|
||||
try {
|
||||
callback(err);
|
||||
} catch(err) {
|
||||
if(!self.s.replset.isConnected())
|
||||
process.nextTick(function() { throw err; })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Set up listeners
|
||||
self.s.replset.once('timeout', connectErrorHandler('timeout'));
|
||||
self.s.replset.once('error', connectErrorHandler('error'));
|
||||
self.s.replset.once('close', connectErrorHandler('close'));
|
||||
self.s.replset.once('connect', connectHandler);
|
||||
|
||||
// Start connection
|
||||
self.s.replset.connect(_options);
|
||||
}
|
||||
|
||||
// Server capabilities
|
||||
ReplSet.prototype.capabilities = function() {
|
||||
if(this.s.sCapabilities) return this.s.sCapabilities;
|
||||
if(this.s.replset.lastIsMaster() == null) return null;
|
||||
this.s.sCapabilities = new ServerCapabilities(this.s.replset.lastIsMaster());
|
||||
return this.s.sCapabilities;
|
||||
}
|
||||
|
||||
define.classMethod('capabilities', {callback: false, promise:false, returns: [ServerCapabilities]});
|
||||
|
||||
// Command
|
||||
ReplSet.prototype.command = function(ns, cmd, options, callback) {
|
||||
this.s.replset.command(ns, cmd, getReadPreference(options), callback);
|
||||
}
|
||||
|
||||
define.classMethod('command', {callback: true, promise:false});
|
||||
|
||||
// Insert
|
||||
ReplSet.prototype.insert = function(ns, ops, options, callback) {
|
||||
this.s.replset.insert(ns, ops, options, callback);
|
||||
}
|
||||
|
||||
define.classMethod('insert', {callback: true, promise:false});
|
||||
|
||||
// Update
|
||||
ReplSet.prototype.update = function(ns, ops, options, callback) {
|
||||
this.s.replset.update(ns, ops, options, callback);
|
||||
}
|
||||
|
||||
define.classMethod('update', {callback: true, promise:false});
|
||||
|
||||
// Remove
|
||||
ReplSet.prototype.remove = function(ns, ops, options, callback) {
|
||||
this.s.replset.remove(ns, ops, options, callback);
|
||||
}
|
||||
|
||||
define.classMethod('remove', {callback: true, promise:false});
|
||||
|
||||
// Destroyed
|
||||
ReplSet.prototype.isDestroyed = function() {
|
||||
return this.s.replset.isDestroyed();
|
||||
}
|
||||
|
||||
// IsConnected
|
||||
ReplSet.prototype.isConnected = function(options) {
|
||||
options = options || {};
|
||||
|
||||
// If we passed in a readPreference, translate to
|
||||
// a CoreReadPreference instance
|
||||
if(options.readPreference) {
|
||||
options.readPreference = translateReadPreference(options.readPreference);
|
||||
}
|
||||
|
||||
return this.s.replset.isConnected(options);
|
||||
}
|
||||
|
||||
define.classMethod('isConnected', {callback: false, promise:false, returns: [Boolean]});
|
||||
|
||||
// Insert
|
||||
ReplSet.prototype.cursor = function(ns, cmd, options) {
|
||||
options = translateReadPreference(options);
|
||||
options.disconnectHandler = this.s.store;
|
||||
return this.s.replset.cursor(ns, cmd, options);
|
||||
}
|
||||
|
||||
define.classMethod('cursor', {callback: false, promise:false, returns: [Cursor, AggregationCursor, CommandCursor]});
|
||||
|
||||
ReplSet.prototype.lastIsMaster = function() {
|
||||
return this.s.replset.lastIsMaster();
|
||||
}
|
||||
|
||||
/**
|
||||
* Unref all sockets
|
||||
* @method
|
||||
*/
|
||||
ReplSet.prototype.unref = function() {
|
||||
return this.s.replset.unref();
|
||||
}
|
||||
|
||||
ReplSet.prototype.close = function(forceClosed) {
|
||||
var self = this;
|
||||
// Call destroy on the topology
|
||||
this.s.replset.destroy({
|
||||
force: typeof forceClosed == 'boolean' ? forceClosed : false,
|
||||
});
|
||||
// We need to wash out all stored processes
|
||||
if(forceClosed == true) {
|
||||
this.s.storeOptions.force = forceClosed;
|
||||
this.s.store.flush();
|
||||
}
|
||||
|
||||
var events = ['timeout', 'error', 'close', 'joined', 'left'];
|
||||
events.forEach(function(e) {
|
||||
self.removeAllListeners(e);
|
||||
});
|
||||
}
|
||||
|
||||
define.classMethod('close', {callback: false, promise:false});
|
||||
|
||||
ReplSet.prototype.auth = function() {
|
||||
var args = Array.prototype.slice.call(arguments, 0);
|
||||
this.s.replset.auth.apply(this.s.replset, args);
|
||||
}
|
||||
|
||||
define.classMethod('auth', {callback: true, promise:false});
|
||||
|
||||
ReplSet.prototype.logout = function() {
|
||||
var args = Array.prototype.slice.call(arguments, 0);
|
||||
this.s.replset.logout.apply(this.s.replset, args);
|
||||
}
|
||||
|
||||
define.classMethod('logout', {callback: true, promise:false});
|
||||
|
||||
/**
|
||||
* All raw connections
|
||||
* @method
|
||||
* @return {array}
|
||||
*/
|
||||
ReplSet.prototype.connections = function() {
|
||||
return this.s.replset.connections();
|
||||
}
|
||||
|
||||
define.classMethod('connections', {callback: false, promise:false, returns:[Array]});
|
||||
|
||||
/**
|
||||
* A replset connect event, used to verify that the connection is up and running
|
||||
*
|
||||
* @event ReplSet#connect
|
||||
* @type {ReplSet}
|
||||
*/
|
||||
|
||||
/**
|
||||
* The replset high availability event
|
||||
*
|
||||
* @event ReplSet#ha
|
||||
* @type {function}
|
||||
* @param {string} type The stage in the high availability event (start|end)
|
||||
* @param {boolean} data.norepeat This is a repeating high availability process or a single execution only
|
||||
* @param {number} data.id The id for this high availability request
|
||||
* @param {object} data.state An object containing the information about the current replicaset
|
||||
*/
|
||||
|
||||
/**
|
||||
* A server member left the replicaset
|
||||
*
|
||||
* @event ReplSet#left
|
||||
* @type {function}
|
||||
* @param {string} type The type of member that left (primary|secondary|arbiter)
|
||||
* @param {Server} server The server object that left
|
||||
*/
|
||||
|
||||
/**
|
||||
* A server member joined the replicaset
|
||||
*
|
||||
* @event ReplSet#joined
|
||||
* @type {function}
|
||||
* @param {string} type The type of member that joined (primary|secondary|arbiter)
|
||||
* @param {Server} server The server object that joined
|
||||
*/
|
||||
|
||||
/**
|
||||
* ReplSet open event, emitted when replicaset can start processing commands.
|
||||
*
|
||||
* @event ReplSet#open
|
||||
* @type {Replset}
|
||||
*/
|
||||
|
||||
/**
|
||||
* ReplSet fullsetup event, emitted when all servers in the topology have been connected to.
|
||||
*
|
||||
* @event ReplSet#fullsetup
|
||||
* @type {Replset}
|
||||
*/
|
||||
|
||||
/**
|
||||
* ReplSet close event
|
||||
*
|
||||
* @event ReplSet#close
|
||||
* @type {object}
|
||||
*/
|
||||
|
||||
/**
|
||||
* ReplSet error event, emitted if there is an error listener.
|
||||
*
|
||||
* @event ReplSet#error
|
||||
* @type {MongoError}
|
||||
*/
|
||||
|
||||
/**
|
||||
* ReplSet timeout event
|
||||
*
|
||||
* @event ReplSet#timeout
|
||||
* @type {object}
|
||||
*/
|
||||
|
||||
/**
|
||||
* ReplSet parseError event
|
||||
*
|
||||
* @event ReplSet#parseError
|
||||
* @type {object}
|
||||
*/
|
||||
|
||||
module.exports = ReplSet;
|
||||
+518
@@ -0,0 +1,518 @@
|
||||
"use strict";
|
||||
|
||||
var EventEmitter = require('events').EventEmitter
|
||||
, inherits = require('util').inherits
|
||||
, CServer = require('mongodb-core').Server
|
||||
, Cursor = require('./cursor')
|
||||
, AggregationCursor = require('./aggregation_cursor')
|
||||
, CommandCursor = require('./command_cursor')
|
||||
, f = require('util').format
|
||||
, ServerCapabilities = require('./topology_base').ServerCapabilities
|
||||
, Store = require('./topology_base').Store
|
||||
, Define = require('./metadata')
|
||||
, MongoError = require('mongodb-core').MongoError
|
||||
, MAX_JS_INT = require('./utils').MAX_JS_INT
|
||||
, translateOptions = require('./utils').translateOptions
|
||||
, filterOptions = require('./utils').filterOptions
|
||||
, mergeOptions = require('./utils').mergeOptions
|
||||
, getReadPreference = require('./utils').getReadPreference
|
||||
, os = require('os');
|
||||
|
||||
// Get package.json variable
|
||||
var driverVersion = require('../package.json').version;
|
||||
var nodejsversion = f('Node.js %s, %s', process.version, os.endianness());
|
||||
var type = os.type();
|
||||
var name = process.platform;
|
||||
var architecture = process.arch;
|
||||
var release = os.release();
|
||||
|
||||
/**
|
||||
* @fileOverview The **Server** class is a class that represents a single server topology and is
|
||||
* used to construct connections.
|
||||
*
|
||||
* **Server Should not be used, use MongoClient.connect**
|
||||
* @example
|
||||
* var Db = require('mongodb').Db,
|
||||
* Server = require('mongodb').Server,
|
||||
* test = require('assert');
|
||||
* // Connect using single Server
|
||||
* var db = new Db('test', new Server('localhost', 27017););
|
||||
* db.open(function(err, db) {
|
||||
* // Get an additional db
|
||||
* db.close();
|
||||
* });
|
||||
*/
|
||||
|
||||
// Allowed parameters
|
||||
var legalOptionNames = ['ha', 'haInterval', 'acceptableLatencyMS'
|
||||
, 'poolSize', 'ssl', 'checkServerIdentity', 'sslValidate', 'ciphers', 'ecdhCurve'
|
||||
, 'sslCA', 'sslCRL', 'sslCert', 'sslKey', 'sslPass', 'socketOptions', 'bufferMaxEntries'
|
||||
, 'store', 'auto_reconnect', 'autoReconnect', 'emitError'
|
||||
, 'keepAlive', 'noDelay', 'connectTimeoutMS', 'socketTimeoutMS', 'family'
|
||||
, 'loggerLevel', 'logger', 'reconnectTries', 'reconnectInterval', 'monitoring'
|
||||
, 'appname', 'domainsEnabled'
|
||||
, 'servername', 'promoteLongs', 'promoteValues', 'promoteBuffers'];
|
||||
|
||||
/**
|
||||
* Creates a new Server instance
|
||||
* @class
|
||||
* @deprecated
|
||||
* @param {string} host The host for the server, can be either an IP4, IP6 or domain socket style host.
|
||||
* @param {number} [port] The server port if IP4.
|
||||
* @param {object} [options=null] Optional settings.
|
||||
* @param {number} [options.poolSize=5] Number of connections in the connection pool for each server instance, set to 5 as default for legacy reasons.
|
||||
* @param {boolean} [options.ssl=false] Use ssl connection (needs to have a mongod server with ssl support)
|
||||
* @param {object} [options.sslValidate=true] Validate mongod server certificate against ca (needs to have a mongod server with ssl support, 2.4 or higher)
|
||||
* @param {boolean|function} [options.checkServerIdentity=true] Ensure we check server identify during SSL, set to false to disable checking. Only works for Node 0.12.x or higher. You can pass in a boolean or your own checkServerIdentity override function.
|
||||
* @param {array} [options.sslCA=null] Array of valid certificates either as Buffers or Strings (needs to have a mongod server with ssl support, 2.4 or higher)
|
||||
* @param {array} [options.sslCRL=null] Array of revocation certificates either as Buffers or Strings (needs to have a mongod server with ssl support, 2.4 or higher)
|
||||
* @param {(Buffer|string)} [options.sslCert=null] String or buffer containing the certificate we wish to present (needs to have a mongod server with ssl support, 2.4 or higher)
|
||||
* @param {(Buffer|string)} [options.sslKey=null] String or buffer containing the certificate private key we wish to present (needs to have a mongod server with ssl support, 2.4 or higher)
|
||||
* @param {(Buffer|string)} [options.sslPass=null] String or buffer containing the certificate password (needs to have a mongod server with ssl support, 2.4 or higher)
|
||||
* @param {string} [options.servername=null] String containing the server name requested via TLS SNI.
|
||||
* @param {boolean} [options.autoReconnect=true] Reconnect on error or timeout.
|
||||
* @param {object} [options.socketOptions=null] Socket options
|
||||
* @param {boolean} [options.socketOptions.noDelay=true] TCP Socket NoDelay option.
|
||||
* @param {number} [options.socketOptions.keepAlive=0] TCP KeepAlive on the socket with a X ms delay before start.
|
||||
* @param {number} [options.socketOptions.connectTimeoutMS=0] TCP Connection timeout setting
|
||||
* @param {number} [options.socketOptions.socketTimeoutMS=0] TCP Socket timeout setting
|
||||
* @param {number} [options.reconnectTries=30] Server attempt to reconnect #times
|
||||
* @param {number} [options.reconnectInterval=1000] Server will wait # milliseconds between retries
|
||||
* @param {number} [options.monitoring=true] Triggers the server instance to call ismaster
|
||||
* @param {number} [options.haInterval=10000] The interval of calling ismaster when monitoring is enabled.
|
||||
* @param {boolean} [options.domainsEnabled=false] Enable the wrapping of the callback in the current domain, disabled by default to avoid perf hit.
|
||||
* @fires Server#connect
|
||||
* @fires Server#close
|
||||
* @fires Server#error
|
||||
* @fires Server#timeout
|
||||
* @fires Server#parseError
|
||||
* @fires Server#reconnect
|
||||
* @property {string} parserType the parser type used (c++ or js).
|
||||
* @return {Server} a Server instance.
|
||||
*/
|
||||
var Server = function(host, port, options) {
|
||||
options = options || {};
|
||||
if(!(this instanceof Server)) return new Server(host, port, options);
|
||||
EventEmitter.call(this);
|
||||
var self = this;
|
||||
|
||||
// Filter the options
|
||||
options = filterOptions(options, legalOptionNames);
|
||||
|
||||
// Stored options
|
||||
var storeOptions = {
|
||||
force: false
|
||||
, bufferMaxEntries: typeof options.bufferMaxEntries == 'number' ? options.bufferMaxEntries : MAX_JS_INT
|
||||
}
|
||||
|
||||
// Shared global store
|
||||
var store = options.store || new Store(self, storeOptions);
|
||||
|
||||
// Detect if we have a socket connection
|
||||
if(host.indexOf('\/') != -1) {
|
||||
if(port != null && typeof port == 'object') {
|
||||
options = port;
|
||||
port = null;
|
||||
}
|
||||
} else if(port == null) {
|
||||
throw MongoError.create({message: 'port must be specified', driver:true});
|
||||
}
|
||||
|
||||
// Get the reconnect option
|
||||
var reconnect = typeof options.auto_reconnect == 'boolean' ? options.auto_reconnect : true;
|
||||
reconnect = typeof options.autoReconnect == 'boolean' ? options.autoReconnect : reconnect;
|
||||
|
||||
// Clone options
|
||||
var clonedOptions = mergeOptions({}, {
|
||||
host: host, port: port, disconnectHandler: store,
|
||||
cursorFactory: Cursor,
|
||||
reconnect: reconnect,
|
||||
emitError: typeof options.emitError == 'boolean' ? options.emitError : true,
|
||||
size: typeof options.poolSize == 'number' ? options.poolSize : 5
|
||||
});
|
||||
|
||||
// Translate any SSL options and other connectivity options
|
||||
clonedOptions = translateOptions(clonedOptions, options);
|
||||
|
||||
// Socket options
|
||||
var socketOptions = options.socketOptions && Object.keys(options.socketOptions).length > 0
|
||||
? options.socketOptions : options;
|
||||
|
||||
// Translate all the options to the mongodb-core ones
|
||||
clonedOptions = translateOptions(clonedOptions, socketOptions);
|
||||
if(typeof clonedOptions.keepAlive == 'number') {
|
||||
clonedOptions.keepAliveInitialDelay = clonedOptions.keepAlive;
|
||||
clonedOptions.keepAlive = clonedOptions.keepAlive > 0;
|
||||
}
|
||||
|
||||
// Build default client information
|
||||
this.clientInfo = {
|
||||
driver: {
|
||||
name: "nodejs",
|
||||
version: driverVersion
|
||||
},
|
||||
os: {
|
||||
type: type,
|
||||
name: name,
|
||||
architecture: architecture,
|
||||
version: release
|
||||
},
|
||||
platform: nodejsversion
|
||||
}
|
||||
|
||||
// Build default client information
|
||||
clonedOptions.clientInfo = this.clientInfo;
|
||||
// Do we have an application specific string
|
||||
if(options.appname) {
|
||||
clonedOptions.clientInfo.application = { name: options.appname };
|
||||
}
|
||||
|
||||
// Create an instance of a server instance from mongodb-core
|
||||
var server = new CServer(clonedOptions);
|
||||
|
||||
// Define the internal properties
|
||||
this.s = {
|
||||
// Create an instance of a server instance from mongodb-core
|
||||
server: server
|
||||
// Server capabilities
|
||||
, sCapabilities: null
|
||||
// Cloned options
|
||||
, clonedOptions: clonedOptions
|
||||
// Reconnect
|
||||
, reconnect: clonedOptions.reconnect
|
||||
// Emit error
|
||||
, emitError: clonedOptions.emitError
|
||||
// Pool size
|
||||
, poolSize: clonedOptions.size
|
||||
// Store Options
|
||||
, storeOptions: storeOptions
|
||||
// Store
|
||||
, store: store
|
||||
// Host
|
||||
, host: host
|
||||
// Port
|
||||
, port: port
|
||||
// Options
|
||||
, options: options
|
||||
}
|
||||
}
|
||||
|
||||
inherits(Server, EventEmitter);
|
||||
|
||||
var define = Server.define = new Define('Server', Server, false);
|
||||
|
||||
// BSON property
|
||||
Object.defineProperty(Server.prototype, 'bson', {
|
||||
enumerable: true, get: function() {
|
||||
return this.s.server.s.bson;
|
||||
}
|
||||
});
|
||||
|
||||
// Last ismaster
|
||||
Object.defineProperty(Server.prototype, 'isMasterDoc', {
|
||||
enumerable:true, get: function() {
|
||||
return this.s.server.lastIsMaster();
|
||||
}
|
||||
});
|
||||
|
||||
Object.defineProperty(Server.prototype, 'parserType', {
|
||||
enumerable:true, get: function() {
|
||||
return this.s.server.parserType;
|
||||
}
|
||||
});
|
||||
|
||||
// Last ismaster
|
||||
Object.defineProperty(Server.prototype, 'poolSize', {
|
||||
enumerable:true, get: function() { return this.s.server.connections().length; }
|
||||
});
|
||||
|
||||
Object.defineProperty(Server.prototype, 'autoReconnect', {
|
||||
enumerable:true, get: function() { return this.s.reconnect; }
|
||||
});
|
||||
|
||||
Object.defineProperty(Server.prototype, 'host', {
|
||||
enumerable:true, get: function() { return this.s.host; }
|
||||
});
|
||||
|
||||
Object.defineProperty(Server.prototype, 'port', {
|
||||
enumerable:true, get: function() { return this.s.port; }
|
||||
});
|
||||
|
||||
// Connect
|
||||
Server.prototype.connect = function(db, _options, callback) {
|
||||
var self = this;
|
||||
if('function' === typeof _options) callback = _options, _options = {};
|
||||
if(_options == null) _options = {};
|
||||
if(!('function' === typeof callback)) callback = null;
|
||||
self.s.options = _options;
|
||||
|
||||
// Update bufferMaxEntries
|
||||
self.s.storeOptions.bufferMaxEntries = db.bufferMaxEntries;
|
||||
|
||||
// Error handler
|
||||
var connectErrorHandler = function() {
|
||||
return function(err) {
|
||||
// Remove all event handlers
|
||||
var events = ['timeout', 'error', 'close'];
|
||||
events.forEach(function(e) {
|
||||
self.s.server.removeListener(e, connectHandlers[e]);
|
||||
});
|
||||
|
||||
self.s.server.removeListener('connect', connectErrorHandler);
|
||||
|
||||
// Try to callback
|
||||
try {
|
||||
callback(err);
|
||||
} catch(err) {
|
||||
process.nextTick(function() { throw err; })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Actual handler
|
||||
var errorHandler = function(event) {
|
||||
return function(err) {
|
||||
if(event != 'error') {
|
||||
self.emit(event, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Error handler
|
||||
var reconnectHandler = function() {
|
||||
self.emit('reconnect', self);
|
||||
self.s.store.execute();
|
||||
}
|
||||
|
||||
// Reconnect failed
|
||||
var reconnectFailedHandler = function(err) {
|
||||
self.emit('reconnectFailed', err);
|
||||
self.s.store.flush(err);
|
||||
}
|
||||
|
||||
// Destroy called on topology, perform cleanup
|
||||
var destroyHandler = function() {
|
||||
self.s.store.flush();
|
||||
}
|
||||
|
||||
// relay the event
|
||||
var relay = function(event) {
|
||||
return function(t, server) {
|
||||
self.emit(event, t, server);
|
||||
}
|
||||
}
|
||||
|
||||
// Connect handler
|
||||
var connectHandler = function() {
|
||||
// Clear out all the current handlers left over
|
||||
["timeout", "error", "close", 'destroy'].forEach(function(e) {
|
||||
self.s.server.removeAllListeners(e);
|
||||
});
|
||||
|
||||
// Set up listeners
|
||||
self.s.server.on('timeout', errorHandler('timeout'));
|
||||
self.s.server.once('error', errorHandler('error'));
|
||||
self.s.server.on('close', errorHandler('close'));
|
||||
// Only called on destroy
|
||||
self.s.server.on('destroy', destroyHandler);
|
||||
|
||||
// Emit open event
|
||||
self.emit('open', null, self);
|
||||
|
||||
// Return correctly
|
||||
try {
|
||||
callback(null, self);
|
||||
} catch(err) {
|
||||
console.log(err.stack)
|
||||
process.nextTick(function() { throw err; })
|
||||
}
|
||||
}
|
||||
|
||||
// Set up listeners
|
||||
var connectHandlers = {
|
||||
timeout: connectErrorHandler('timeout'),
|
||||
error: connectErrorHandler('error'),
|
||||
close: connectErrorHandler('close')
|
||||
};
|
||||
|
||||
// Clear out all the current handlers left over
|
||||
["timeout", "error", "close", 'serverOpening', 'serverDescriptionChanged', 'serverHeartbeatStarted',
|
||||
'serverHeartbeatSucceeded', 'serverHeartbeatFailed', 'serverClosed', 'topologyOpening',
|
||||
'topologyClosed', 'topologyDescriptionChanged'].forEach(function(e) {
|
||||
self.s.server.removeAllListeners(e);
|
||||
});
|
||||
|
||||
// Add the event handlers
|
||||
self.s.server.once('timeout', connectHandlers.timeout);
|
||||
self.s.server.once('error', connectHandlers.error);
|
||||
self.s.server.once('close', connectHandlers.close);
|
||||
self.s.server.once('connect', connectHandler);
|
||||
// Reconnect server
|
||||
self.s.server.on('reconnect', reconnectHandler);
|
||||
self.s.server.on('reconnectFailed', reconnectFailedHandler);
|
||||
|
||||
// Set up SDAM listeners
|
||||
self.s.server.on('serverDescriptionChanged', relay('serverDescriptionChanged'));
|
||||
self.s.server.on('serverHeartbeatStarted', relay('serverHeartbeatStarted'));
|
||||
self.s.server.on('serverHeartbeatSucceeded', relay('serverHeartbeatSucceeded'));
|
||||
self.s.server.on('serverHeartbeatFailed', relay('serverHeartbeatFailed'));
|
||||
self.s.server.on('serverOpening', relay('serverOpening'));
|
||||
self.s.server.on('serverClosed', relay('serverClosed'));
|
||||
self.s.server.on('topologyOpening', relay('topologyOpening'));
|
||||
self.s.server.on('topologyClosed', relay('topologyClosed'));
|
||||
self.s.server.on('topologyDescriptionChanged', relay('topologyDescriptionChanged'));
|
||||
self.s.server.on('attemptReconnect', relay('attemptReconnect'));
|
||||
self.s.server.on('monitoring', relay('monitoring'));
|
||||
|
||||
// Start connection
|
||||
self.s.server.connect(_options);
|
||||
}
|
||||
|
||||
// Server capabilities
|
||||
Server.prototype.capabilities = function() {
|
||||
if(this.s.sCapabilities) return this.s.sCapabilities;
|
||||
if(this.s.server.lastIsMaster() == null) return null;
|
||||
this.s.sCapabilities = new ServerCapabilities(this.s.server.lastIsMaster());
|
||||
return this.s.sCapabilities;
|
||||
}
|
||||
|
||||
define.classMethod('capabilities', {callback: false, promise:false, returns: [ServerCapabilities]});
|
||||
|
||||
// Command
|
||||
Server.prototype.command = function(ns, cmd, options, callback) {
|
||||
this.s.server.command(ns, cmd, getReadPreference(options), callback);
|
||||
}
|
||||
|
||||
define.classMethod('command', {callback: true, promise:false});
|
||||
|
||||
// Insert
|
||||
Server.prototype.insert = function(ns, ops, options, callback) {
|
||||
this.s.server.insert(ns, ops, options, callback);
|
||||
}
|
||||
|
||||
define.classMethod('insert', {callback: true, promise:false});
|
||||
|
||||
// Update
|
||||
Server.prototype.update = function(ns, ops, options, callback) {
|
||||
this.s.server.update(ns, ops, options, callback);
|
||||
}
|
||||
|
||||
define.classMethod('update', {callback: true, promise:false});
|
||||
|
||||
// Remove
|
||||
Server.prototype.remove = function(ns, ops, options, callback) {
|
||||
this.s.server.remove(ns, ops, options, callback);
|
||||
}
|
||||
|
||||
define.classMethod('remove', {callback: true, promise:false});
|
||||
|
||||
// IsConnected
|
||||
Server.prototype.isConnected = function() {
|
||||
return this.s.server.isConnected();
|
||||
}
|
||||
|
||||
Server.prototype.isDestroyed = function() {
|
||||
return this.s.server.isDestroyed();
|
||||
}
|
||||
|
||||
define.classMethod('isConnected', {callback: false, promise:false, returns: [Boolean]});
|
||||
|
||||
// Insert
|
||||
Server.prototype.cursor = function(ns, cmd, options) {
|
||||
options.disconnectHandler = this.s.store;
|
||||
return this.s.server.cursor(ns, cmd, options);
|
||||
}
|
||||
|
||||
define.classMethod('cursor', {callback: false, promise:false, returns: [Cursor, AggregationCursor, CommandCursor]});
|
||||
|
||||
Server.prototype.lastIsMaster = function() {
|
||||
return this.s.server.lastIsMaster();
|
||||
}
|
||||
|
||||
/**
|
||||
* Unref all sockets
|
||||
* @method
|
||||
*/
|
||||
Server.prototype.unref = function() {
|
||||
this.s.server.unref();
|
||||
}
|
||||
|
||||
Server.prototype.close = function(forceClosed) {
|
||||
this.s.server.destroy();
|
||||
// We need to wash out all stored processes
|
||||
if(forceClosed == true) {
|
||||
this.s.storeOptions.force = forceClosed;
|
||||
this.s.store.flush();
|
||||
}
|
||||
}
|
||||
|
||||
define.classMethod('close', {callback: false, promise:false});
|
||||
|
||||
Server.prototype.auth = function() {
|
||||
var args = Array.prototype.slice.call(arguments, 0);
|
||||
this.s.server.auth.apply(this.s.server, args);
|
||||
}
|
||||
|
||||
define.classMethod('auth', {callback: true, promise:false});
|
||||
|
||||
Server.prototype.logout = function() {
|
||||
var args = Array.prototype.slice.call(arguments, 0);
|
||||
this.s.server.logout.apply(this.s.server, args);
|
||||
}
|
||||
|
||||
define.classMethod('logout', {callback: true, promise:false});
|
||||
|
||||
/**
|
||||
* All raw connections
|
||||
* @method
|
||||
* @return {array}
|
||||
*/
|
||||
Server.prototype.connections = function() {
|
||||
return this.s.server.connections();
|
||||
}
|
||||
|
||||
define.classMethod('connections', {callback: false, promise:false, returns:[Array]});
|
||||
|
||||
/**
|
||||
* Server connect event
|
||||
*
|
||||
* @event Server#connect
|
||||
* @type {object}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Server close event
|
||||
*
|
||||
* @event Server#close
|
||||
* @type {object}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Server reconnect event
|
||||
*
|
||||
* @event Server#reconnect
|
||||
* @type {object}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Server error event
|
||||
*
|
||||
* @event Server#error
|
||||
* @type {MongoError}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Server timeout event
|
||||
*
|
||||
* @event Server#timeout
|
||||
* @type {object}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Server parseError event
|
||||
*
|
||||
* @event Server#parseError
|
||||
* @type {object}
|
||||
*/
|
||||
|
||||
module.exports = Server;
|
||||
+191
@@ -0,0 +1,191 @@
|
||||
"use strict";
|
||||
|
||||
var MongoError = require('mongodb-core').MongoError
|
||||
, f = require('util').format;
|
||||
|
||||
// The store of ops
|
||||
var Store = function(topology, storeOptions) {
|
||||
var self = this;
|
||||
var storedOps = [];
|
||||
storeOptions = storeOptions || {force:false, bufferMaxEntries: -1}
|
||||
|
||||
// Internal state
|
||||
this.s = {
|
||||
storedOps: storedOps
|
||||
, storeOptions: storeOptions
|
||||
, topology: topology
|
||||
}
|
||||
|
||||
Object.defineProperty(this, 'length', {
|
||||
enumerable:true, get: function() { return self.s.storedOps.length; }
|
||||
});
|
||||
}
|
||||
|
||||
Store.prototype.add = function(opType, ns, ops, options, callback) {
|
||||
if(this.s.storeOptions.force) {
|
||||
return callback(MongoError.create({message: "db closed by application", driver:true}));
|
||||
}
|
||||
|
||||
if(this.s.storeOptions.bufferMaxEntries == 0) {
|
||||
return callback(MongoError.create({message: f("no connection available for operation and number of stored operation > %s", this.s.storeOptions.bufferMaxEntries), driver:true }));
|
||||
}
|
||||
|
||||
if(this.s.storeOptions.bufferMaxEntries > 0 && this.s.storedOps.length > this.s.storeOptions.bufferMaxEntries) {
|
||||
while(this.s.storedOps.length > 0) {
|
||||
var op = this.s.storedOps.shift();
|
||||
op.c(MongoError.create({message: f("no connection available for operation and number of stored operation > %s", this.s.storeOptions.bufferMaxEntries), driver:true }));
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
this.s.storedOps.push({t: opType, n: ns, o: ops, op: options, c: callback})
|
||||
}
|
||||
|
||||
Store.prototype.addObjectAndMethod = function(opType, object, method, params, callback) {
|
||||
if(this.s.storeOptions.force) {
|
||||
return callback(MongoError.create({message: "db closed by application", driver:true }));
|
||||
}
|
||||
|
||||
if(this.s.storeOptions.bufferMaxEntries == 0) {
|
||||
return callback(MongoError.create({message: f("no connection available for operation and number of stored operation > %s", this.s.storeOptions.bufferMaxEntries), driver:true }));
|
||||
}
|
||||
|
||||
if(this.s.storeOptions.bufferMaxEntries > 0 && this.s.storedOps.length > this.s.storeOptions.bufferMaxEntries) {
|
||||
while(this.s.storedOps.length > 0) {
|
||||
var op = this.s.storedOps.shift();
|
||||
op.c(MongoError.create({message: f("no connection available for operation and number of stored operation > %s", this.s.storeOptions.bufferMaxEntries), driver:true }));
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
this.s.storedOps.push({t: opType, m: method, o: object, p: params, c: callback})
|
||||
}
|
||||
|
||||
Store.prototype.flush = function(err) {
|
||||
while(this.s.storedOps.length > 0) {
|
||||
this.s.storedOps.shift().c(err || MongoError.create({message: f("no connection available for operation"), driver:true }));
|
||||
}
|
||||
}
|
||||
|
||||
var primaryOptions = ['primary', 'primaryPreferred', 'nearest', 'secondaryPreferred'];
|
||||
var secondaryOptions = ['secondary', 'secondaryPreferred'];
|
||||
|
||||
Store.prototype.execute = function(options) {
|
||||
options = options || {};
|
||||
// Get current ops
|
||||
var ops = this.s.storedOps;
|
||||
// Reset the ops
|
||||
this.s.storedOps = [];
|
||||
|
||||
// Unpack options
|
||||
var executePrimary = typeof options.executePrimary === 'boolean'
|
||||
? options.executePrimary : true;
|
||||
var executeSecondary = typeof options.executeSecondary === 'boolean'
|
||||
? options.executeSecondary : true;
|
||||
|
||||
// Execute all the stored ops
|
||||
while(ops.length > 0) {
|
||||
var op = ops.shift();
|
||||
|
||||
if(op.t == 'cursor') {
|
||||
if(executePrimary && executeSecondary) {
|
||||
op.o[op.m].apply(op.o, op.p);
|
||||
} else if(executePrimary && op.o.options
|
||||
&& op.o.options.readPreference
|
||||
&& primaryOptions.indexOf(op.o.options.readPreference.mode) != -1) {
|
||||
op.o[op.m].apply(op.o, op.p);
|
||||
} else if(!executePrimary && executeSecondary && op.o.options
|
||||
&& op.o.options.readPreference
|
||||
&& secondaryOptions.indexOf(op.o.options.readPreference.mode) != -1) {
|
||||
op.o[op.m].apply(op.o, op.p);
|
||||
}
|
||||
} else if(op.t == 'auth') {
|
||||
this.s.topology[op.t].apply(this.s.topology, op.o);
|
||||
} else {
|
||||
if(executePrimary && executeSecondary) {
|
||||
this.s.topology[op.t](op.n, op.o, op.op, op.c);
|
||||
} else if(executePrimary && op.op && op.op.readPreference
|
||||
&& primaryOptions.indexOf(op.op.readPreference.mode) != -1) {
|
||||
this.s.topology[op.t](op.n, op.o, op.op, op.c);
|
||||
} else if(!executePrimary && executeSecondary && op.op && op.op.readPreference
|
||||
&& secondaryOptions.indexOf(op.op.readPreference.mode) != -1) {
|
||||
this.s.topology[op.t](op.n, op.o, op.op, op.c);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Store.prototype.all = function() {
|
||||
return this.s.storedOps;
|
||||
}
|
||||
|
||||
// Server capabilities
|
||||
var ServerCapabilities = function(ismaster) {
|
||||
var setup_get_property = function(object, name, value) {
|
||||
Object.defineProperty(object, name, {
|
||||
enumerable: true
|
||||
, get: function () { return value; }
|
||||
});
|
||||
}
|
||||
|
||||
// Capabilities
|
||||
var aggregationCursor = false;
|
||||
var writeCommands = false;
|
||||
var textSearch = false;
|
||||
var authCommands = false;
|
||||
var listCollections = false;
|
||||
var listIndexes = false;
|
||||
var maxNumberOfDocsInBatch = ismaster.maxWriteBatchSize || 1000;
|
||||
var commandsTakeWriteConcern = false;
|
||||
var commandsTakeCollation = false;
|
||||
|
||||
if(ismaster.minWireVersion >= 0) {
|
||||
textSearch = true;
|
||||
}
|
||||
|
||||
if(ismaster.maxWireVersion >= 1) {
|
||||
aggregationCursor = true;
|
||||
authCommands = true;
|
||||
}
|
||||
|
||||
if(ismaster.maxWireVersion >= 2) {
|
||||
writeCommands = true;
|
||||
}
|
||||
|
||||
if(ismaster.maxWireVersion >= 3) {
|
||||
listCollections = true;
|
||||
listIndexes = true;
|
||||
}
|
||||
|
||||
if(ismaster.maxWireVersion >= 5) {
|
||||
commandsTakeWriteConcern = true;
|
||||
commandsTakeCollation = true;
|
||||
}
|
||||
|
||||
// If no min or max wire version set to 0
|
||||
if(ismaster.minWireVersion == null) {
|
||||
ismaster.minWireVersion = 0;
|
||||
}
|
||||
|
||||
if(ismaster.maxWireVersion == null) {
|
||||
ismaster.maxWireVersion = 0;
|
||||
}
|
||||
|
||||
// Map up read only parameters
|
||||
setup_get_property(this, "hasAggregationCursor", aggregationCursor);
|
||||
setup_get_property(this, "hasWriteCommands", writeCommands);
|
||||
setup_get_property(this, "hasTextSearch", textSearch);
|
||||
setup_get_property(this, "hasAuthCommands", authCommands);
|
||||
setup_get_property(this, "hasListCollectionsCommand", listCollections);
|
||||
setup_get_property(this, "hasListIndexesCommand", listIndexes);
|
||||
setup_get_property(this, "minWireVersion", ismaster.minWireVersion);
|
||||
setup_get_property(this, "maxWireVersion", ismaster.maxWireVersion);
|
||||
setup_get_property(this, "maxNumberOfDocsInBatch", maxNumberOfDocsInBatch);
|
||||
setup_get_property(this, "commandsTakeWriteConcern", commandsTakeWriteConcern);
|
||||
setup_get_property(this, "commandsTakeCollation", commandsTakeCollation);
|
||||
}
|
||||
|
||||
exports.Store = Store;
|
||||
exports.ServerCapabilities = ServerCapabilities;
|
||||
+526
@@ -0,0 +1,526 @@
|
||||
"use strict";
|
||||
|
||||
var ReadPreference = require('./read_preference'),
|
||||
parser = require('url'),
|
||||
f = require('util').format,
|
||||
assign = require('./utils').assign,
|
||||
dns = require('dns');
|
||||
|
||||
module.exports = function(url, options, callback) {
|
||||
if (typeof options === 'function') (callback = options), (options = {});
|
||||
options = options || {};
|
||||
|
||||
var result = parser.parse(url, true);
|
||||
if (result.protocol !== 'mongodb:' && result.protocol !== 'mongodb+srv:') {
|
||||
return callback(new Error('invalid schema, expected mongodb or mongodb+srv'));
|
||||
}
|
||||
|
||||
if (result.protocol === 'mongodb+srv:') {
|
||||
|
||||
if (result.hostname.split('.').length < 3) {
|
||||
return callback(new Error('uri does not have hostname, domainname and tld'));
|
||||
}
|
||||
|
||||
result.domainLength = result.hostname.split('.').length;
|
||||
|
||||
if (result.pathname && result.pathname.match(',')) {
|
||||
return callback(new Error('invalid uri, cannot contain multiple hostnames'));
|
||||
}
|
||||
|
||||
if (result.port) {
|
||||
return callback(new Error('Ports not accepted with mongodb+srv'));
|
||||
}
|
||||
|
||||
var srvAddress = '_mongodb._tcp.' + result.host;
|
||||
dns.resolveSrv(srvAddress, function(err, addresses) {
|
||||
if (err) return callback(err);
|
||||
|
||||
if (addresses.length === 0) {
|
||||
return callback(new Error('No addresses found at host'));
|
||||
}
|
||||
|
||||
for (var i = 0; i < addresses.length; i++) {
|
||||
if (!matchesParentDomain(addresses[i].name, result.hostname, result.domainLength)) {
|
||||
return callback(new Error('srv record does not share hostname with parent uri'));
|
||||
}
|
||||
}
|
||||
|
||||
var connectionStrings = addresses.map(function(address, i) {
|
||||
if (i === 0) return 'mongodb://' + address.name + ':' + address.port;
|
||||
else return address.name + ':' + address.port;
|
||||
});
|
||||
|
||||
var connectionString = connectionStrings.join(',') + '/';
|
||||
var connectionStringOptions = [];
|
||||
|
||||
// Default to SSL true
|
||||
if (!options.ssl && !result.search) {
|
||||
connectionStringOptions.push('ssl=true');
|
||||
} else if (!options.ssl && result.search && !result.search.match('ssl')) {
|
||||
connectionStringOptions.push('ssl=true');
|
||||
}
|
||||
|
||||
// Keep original uri options
|
||||
if (result.search) {
|
||||
connectionStringOptions.push(result.search.replace('?', ''));
|
||||
}
|
||||
|
||||
dns.resolveTxt(result.host, function(err, record) {
|
||||
if (err && err.code !== 'ENODATA') return callback(err);
|
||||
if (err && err.code === 'ENODATA') record = null;
|
||||
|
||||
if (record) {
|
||||
if (record.length > 1) {
|
||||
return callback(new Error('multiple text records not allowed'));
|
||||
}
|
||||
|
||||
record = record[0];
|
||||
if (record.length > 1) record = record.join('');
|
||||
else record = record[0];
|
||||
|
||||
if (!record.includes('authSource') && !record.includes('replicaSet')) {
|
||||
return callback(new Error('text record must only set `authSource` or `replicaSet`'));
|
||||
}
|
||||
|
||||
connectionStringOptions.push(record);
|
||||
}
|
||||
|
||||
// Add any options to the connection string
|
||||
if (connectionStringOptions.length) {
|
||||
connectionString += '?' + connectionStringOptions.join('&');
|
||||
}
|
||||
|
||||
parseHandler(connectionString, options, callback);
|
||||
});
|
||||
});
|
||||
} else {
|
||||
parseHandler(url, options, callback);
|
||||
}
|
||||
};
|
||||
|
||||
function matchesParentDomain(srvAddress, parentDomain) {
|
||||
var regex = /^.*?\./;
|
||||
var srv = '.' + srvAddress.replace(regex, '');
|
||||
var parent = '.' + parentDomain.replace(regex, '');
|
||||
if (srv.endsWith(parent)) return true;
|
||||
else return false;
|
||||
}
|
||||
|
||||
function parseHandler(address, options, callback) {
|
||||
var result, err;
|
||||
try {
|
||||
result = parseConnectionString(address, options);
|
||||
} catch (e) {
|
||||
err = e;
|
||||
}
|
||||
|
||||
return err ? callback(err, null) : callback(null, result);
|
||||
}
|
||||
|
||||
function parseConnectionString(url, options) {
|
||||
// Variables
|
||||
var connection_part = '';
|
||||
var auth_part = '';
|
||||
var query_string_part = '';
|
||||
var dbName = 'admin';
|
||||
|
||||
// Url parser result
|
||||
var result = parser.parse(url, true);
|
||||
|
||||
if((result.hostname == null || result.hostname == '') && url.indexOf('.sock') == -1) {
|
||||
throw new Error('no hostname or hostnames provided in connection string');
|
||||
}
|
||||
|
||||
if(result.port == '0') {
|
||||
throw new Error('invalid port (zero) with hostname');
|
||||
}
|
||||
|
||||
if(!isNaN(parseInt(result.port, 10)) && parseInt(result.port, 10) > 65535) {
|
||||
throw new Error('invalid port (larger than 65535) with hostname');
|
||||
}
|
||||
|
||||
if(result.path
|
||||
&& result.path.length > 0
|
||||
&& result.path[0] != '/'
|
||||
&& url.indexOf('.sock') == -1) {
|
||||
throw new Error('missing delimiting slash between hosts and options');
|
||||
}
|
||||
|
||||
if(result.query) {
|
||||
for(var name in result.query) {
|
||||
if(name.indexOf('::') != -1) {
|
||||
throw new Error('double colon in host identifier');
|
||||
}
|
||||
|
||||
if(result.query[name] == '') {
|
||||
throw new Error('query parameter ' + name + ' is an incomplete value pair');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(result.auth) {
|
||||
var parts = result.auth.split(':');
|
||||
if(url.indexOf(result.auth) != -1 && parts.length > 2) {
|
||||
throw new Error('Username with password containing an unescaped colon');
|
||||
}
|
||||
|
||||
if(url.indexOf(result.auth) != -1 && result.auth.indexOf('@') != -1) {
|
||||
throw new Error('Username containing an unescaped at-sign');
|
||||
}
|
||||
}
|
||||
|
||||
// Remove query
|
||||
var clean = url.split('?').shift();
|
||||
|
||||
// Extract the list of hosts
|
||||
var strings = clean.split(',');
|
||||
var hosts = [];
|
||||
|
||||
for(var i = 0; i < strings.length; i++) {
|
||||
var hostString = strings[i];
|
||||
|
||||
if(hostString.indexOf('mongodb') != -1) {
|
||||
if(hostString.indexOf('@') != -1) {
|
||||
hosts.push(hostString.split('@').pop())
|
||||
} else {
|
||||
hosts.push(hostString.substr('mongodb://'.length));
|
||||
}
|
||||
} else if(hostString.indexOf('/') != -1) {
|
||||
hosts.push(hostString.split('/').shift());
|
||||
} else if(hostString.indexOf('/') == -1) {
|
||||
hosts.push(hostString.trim());
|
||||
}
|
||||
}
|
||||
|
||||
for(i = 0; i < hosts.length; i++) {
|
||||
var r = parser.parse(f('mongodb://%s', hosts[i].trim()));
|
||||
if(r.path && r.path.indexOf(':') != -1) {
|
||||
// Not connecting to a socket so check for an extra slash in the hostname.
|
||||
// Using String#split as perf is better than match.
|
||||
if (r.path.split('/').length > 1) {
|
||||
throw new Error('slash in host identifier');
|
||||
} else {
|
||||
throw new Error('double colon in host identifier');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If we have a ? mark cut the query elements off
|
||||
if(url.indexOf("?") != -1) {
|
||||
query_string_part = url.substr(url.indexOf("?") + 1);
|
||||
connection_part = url.substring("mongodb://".length, url.indexOf("?"))
|
||||
} else {
|
||||
connection_part = url.substring("mongodb://".length);
|
||||
}
|
||||
|
||||
// Check if we have auth params
|
||||
if(connection_part.indexOf("@") != -1) {
|
||||
auth_part = connection_part.split("@")[0];
|
||||
connection_part = connection_part.split("@")[1];
|
||||
}
|
||||
|
||||
// Check if the connection string has a db
|
||||
if(connection_part.indexOf(".sock") != -1) {
|
||||
if(connection_part.indexOf(".sock/") != -1) {
|
||||
dbName = connection_part.split(".sock/")[1];
|
||||
// Check if multiple database names provided, or just an illegal trailing backslash
|
||||
if (dbName.indexOf("/") != -1) {
|
||||
if (dbName.split("/").length == 2 && dbName.split("/")[1].length == 0) {
|
||||
throw new Error('Illegal trailing backslash after database name');
|
||||
}
|
||||
throw new Error('More than 1 database name in URL');
|
||||
}
|
||||
connection_part = connection_part.split("/", connection_part.indexOf(".sock") + ".sock".length);
|
||||
}
|
||||
} else if(connection_part.indexOf("/") != -1) {
|
||||
// Check if multiple database names provided, or just an illegal trailing backslash
|
||||
if (connection_part.split("/").length > 2) {
|
||||
if (connection_part.split("/")[2].length == 0) {
|
||||
throw new Error('Illegal trailing backslash after database name');
|
||||
}
|
||||
throw new Error('More than 1 database name in URL');
|
||||
}
|
||||
dbName = connection_part.split("/")[1];
|
||||
connection_part = connection_part.split("/")[0];
|
||||
}
|
||||
|
||||
// Result object
|
||||
var object = {};
|
||||
|
||||
// Pick apart the authentication part of the string
|
||||
var authPart = auth_part || '';
|
||||
var auth = authPart.split(':', 2);
|
||||
|
||||
// Decode the URI components
|
||||
auth[0] = decodeURIComponent(auth[0]);
|
||||
if(auth[1]){
|
||||
auth[1] = decodeURIComponent(auth[1]);
|
||||
}
|
||||
|
||||
// Add auth to final object if we have 2 elements
|
||||
if(auth.length == 2) object.auth = {user: auth[0], password: auth[1]};
|
||||
// if user provided auth options, use that
|
||||
if(options && options.auth != null) object.auth = options.auth;
|
||||
|
||||
// Variables used for temporary storage
|
||||
var hostPart;
|
||||
var urlOptions;
|
||||
var servers;
|
||||
var serverOptions = {socketOptions: {}};
|
||||
var dbOptions = {read_preference_tags: []};
|
||||
var replSetServersOptions = {socketOptions: {}};
|
||||
var mongosOptions = {socketOptions: {}};
|
||||
// Add server options to final object
|
||||
object.server_options = serverOptions;
|
||||
object.db_options = dbOptions;
|
||||
object.rs_options = replSetServersOptions;
|
||||
object.mongos_options = mongosOptions;
|
||||
|
||||
// Let's check if we are using a domain socket
|
||||
if(url.match(/\.sock/)) {
|
||||
// Split out the socket part
|
||||
var domainSocket = url.substring(
|
||||
url.indexOf("mongodb://") + "mongodb://".length
|
||||
, url.lastIndexOf(".sock") + ".sock".length);
|
||||
// Clean out any auth stuff if any
|
||||
if(domainSocket.indexOf("@") != -1) domainSocket = domainSocket.split("@")[1];
|
||||
servers = [{domain_socket: domainSocket}];
|
||||
} else {
|
||||
// Split up the db
|
||||
hostPart = connection_part;
|
||||
// Deduplicate servers
|
||||
var deduplicatedServers = {};
|
||||
|
||||
// Parse all server results
|
||||
servers = hostPart.split(',').map(function(h) {
|
||||
var _host, _port, ipv6match;
|
||||
//check if it matches [IPv6]:port, where the port number is optional
|
||||
if ((ipv6match = /\[([^\]]+)\](?:\:(.+))?/.exec(h))) {
|
||||
_host = ipv6match[1];
|
||||
_port = parseInt(ipv6match[2], 10) || 27017;
|
||||
} else {
|
||||
//otherwise assume it's IPv4, or plain hostname
|
||||
var hostPort = h.split(':', 2);
|
||||
_host = hostPort[0] || 'localhost';
|
||||
_port = hostPort[1] != null ? parseInt(hostPort[1], 10) : 27017;
|
||||
// Check for localhost?safe=true style case
|
||||
if(_host.indexOf("?") != -1) _host = _host.split(/\?/)[0];
|
||||
}
|
||||
|
||||
// No entry returned for duplicate server
|
||||
if(deduplicatedServers[_host + "_" + _port]) return null;
|
||||
deduplicatedServers[_host + "_" + _port] = 1;
|
||||
|
||||
// Return the mapped object
|
||||
return {host: _host, port: _port};
|
||||
}).filter(function(x) {
|
||||
return x != null;
|
||||
});
|
||||
}
|
||||
|
||||
// Get the db name
|
||||
object.dbName = dbName || 'admin';
|
||||
// Split up all the options
|
||||
urlOptions = (query_string_part || '').split(/[&;]/);
|
||||
// Ugh, we have to figure out which options go to which constructor manually.
|
||||
urlOptions.forEach(function(opt) {
|
||||
if(!opt) return;
|
||||
var splitOpt = opt.split('='), name = splitOpt[0], value = splitOpt[1];
|
||||
// Options implementations
|
||||
switch(name) {
|
||||
case 'slaveOk':
|
||||
case 'slave_ok':
|
||||
serverOptions.slave_ok = (value == 'true');
|
||||
dbOptions.slaveOk = (value == 'true');
|
||||
break;
|
||||
case 'maxPoolSize':
|
||||
case 'poolSize':
|
||||
serverOptions.poolSize = parseInt(value, 10);
|
||||
replSetServersOptions.poolSize = parseInt(value, 10);
|
||||
break;
|
||||
case 'appname':
|
||||
object.appname = decodeURIComponent(value);
|
||||
break;
|
||||
case 'autoReconnect':
|
||||
case 'auto_reconnect':
|
||||
serverOptions.auto_reconnect = (value == 'true');
|
||||
break;
|
||||
case 'minPoolSize':
|
||||
throw new Error("minPoolSize not supported");
|
||||
case 'maxIdleTimeMS':
|
||||
throw new Error("maxIdleTimeMS not supported");
|
||||
case 'waitQueueMultiple':
|
||||
throw new Error("waitQueueMultiple not supported");
|
||||
case 'waitQueueTimeoutMS':
|
||||
throw new Error("waitQueueTimeoutMS not supported");
|
||||
case 'uuidRepresentation':
|
||||
throw new Error("uuidRepresentation not supported");
|
||||
case 'ssl':
|
||||
if(value == 'prefer') {
|
||||
serverOptions.ssl = value;
|
||||
replSetServersOptions.ssl = value;
|
||||
mongosOptions.ssl = value;
|
||||
break;
|
||||
}
|
||||
serverOptions.ssl = (value == 'true');
|
||||
replSetServersOptions.ssl = (value == 'true');
|
||||
mongosOptions.ssl = (value == 'true');
|
||||
break;
|
||||
case 'sslValidate':
|
||||
serverOptions.sslValidate = (value == 'true');
|
||||
replSetServersOptions.sslValidate = (value == 'true');
|
||||
mongosOptions.sslValidate = (value == 'true');
|
||||
break;
|
||||
case 'replicaSet':
|
||||
case 'rs_name':
|
||||
replSetServersOptions.rs_name = value;
|
||||
break;
|
||||
case 'reconnectWait':
|
||||
replSetServersOptions.reconnectWait = parseInt(value, 10);
|
||||
break;
|
||||
case 'retries':
|
||||
replSetServersOptions.retries = parseInt(value, 10);
|
||||
break;
|
||||
case 'readSecondary':
|
||||
case 'read_secondary':
|
||||
replSetServersOptions.read_secondary = (value == 'true');
|
||||
break;
|
||||
case 'fsync':
|
||||
dbOptions.fsync = (value == 'true');
|
||||
break;
|
||||
case 'journal':
|
||||
dbOptions.j = (value == 'true');
|
||||
break;
|
||||
case 'safe':
|
||||
dbOptions.safe = (value == 'true');
|
||||
break;
|
||||
case 'nativeParser':
|
||||
case 'native_parser':
|
||||
dbOptions.native_parser = (value == 'true');
|
||||
break;
|
||||
case 'readConcernLevel':
|
||||
dbOptions.readConcern = {level: value};
|
||||
break;
|
||||
case 'connectTimeoutMS':
|
||||
serverOptions.socketOptions.connectTimeoutMS = parseInt(value, 10);
|
||||
replSetServersOptions.socketOptions.connectTimeoutMS = parseInt(value, 10);
|
||||
mongosOptions.socketOptions.connectTimeoutMS = parseInt(value, 10);
|
||||
break;
|
||||
case 'socketTimeoutMS':
|
||||
serverOptions.socketOptions.socketTimeoutMS = parseInt(value, 10);
|
||||
replSetServersOptions.socketOptions.socketTimeoutMS = parseInt(value, 10);
|
||||
mongosOptions.socketOptions.socketTimeoutMS = parseInt(value, 10);
|
||||
break;
|
||||
case 'w':
|
||||
dbOptions.w = parseInt(value, 10);
|
||||
if(isNaN(dbOptions.w)) dbOptions.w = value;
|
||||
break;
|
||||
case 'authSource':
|
||||
dbOptions.authSource = value;
|
||||
break;
|
||||
case 'gssapiServiceName':
|
||||
dbOptions.gssapiServiceName = value;
|
||||
break;
|
||||
case 'authMechanism':
|
||||
if(value == 'GSSAPI') {
|
||||
// If no password provided decode only the principal
|
||||
if(object.auth == null) {
|
||||
var urlDecodeAuthPart = decodeURIComponent(authPart);
|
||||
if(urlDecodeAuthPart.indexOf("@") == -1) throw new Error("GSSAPI requires a provided principal");
|
||||
object.auth = {user: urlDecodeAuthPart, password: null};
|
||||
} else {
|
||||
object.auth.user = decodeURIComponent(object.auth.user);
|
||||
}
|
||||
} else if(value == 'MONGODB-X509') {
|
||||
object.auth = {user: decodeURIComponent(authPart)};
|
||||
}
|
||||
|
||||
// Only support GSSAPI or MONGODB-CR for now
|
||||
if(value != 'GSSAPI'
|
||||
&& value != 'MONGODB-X509'
|
||||
&& value != 'MONGODB-CR'
|
||||
&& value != 'DEFAULT'
|
||||
&& value != 'SCRAM-SHA-1'
|
||||
&& value != 'PLAIN')
|
||||
throw new Error("only DEFAULT, GSSAPI, PLAIN, MONGODB-X509, SCRAM-SHA-1 or MONGODB-CR is supported by authMechanism");
|
||||
|
||||
// Authentication mechanism
|
||||
dbOptions.authMechanism = value;
|
||||
break;
|
||||
case 'authMechanismProperties':
|
||||
// Split up into key, value pairs
|
||||
var values = value.split(',');
|
||||
var o = {};
|
||||
// For each value split into key, value
|
||||
values.forEach(function(x) {
|
||||
var v = x.split(':');
|
||||
o[v[0]] = v[1];
|
||||
});
|
||||
|
||||
// Set all authMechanismProperties
|
||||
dbOptions.authMechanismProperties = o;
|
||||
// Set the service name value
|
||||
if(typeof o.SERVICE_NAME == 'string') dbOptions.gssapiServiceName = o.SERVICE_NAME;
|
||||
if(typeof o.SERVICE_REALM == 'string') dbOptions.gssapiServiceRealm = o.SERVICE_REALM;
|
||||
if(typeof o.CANONICALIZE_HOST_NAME == 'string') dbOptions.gssapiCanonicalizeHostName = o.CANONICALIZE_HOST_NAME == 'true' ? true : false;
|
||||
break;
|
||||
case 'wtimeoutMS':
|
||||
dbOptions.wtimeout = parseInt(value, 10);
|
||||
break;
|
||||
case 'readPreference':
|
||||
if(!ReadPreference.isValid(value)) throw new Error("readPreference must be either primary/primaryPreferred/secondary/secondaryPreferred/nearest");
|
||||
dbOptions.readPreference = value;
|
||||
break;
|
||||
case 'maxStalenessSeconds':
|
||||
dbOptions.maxStalenessSeconds = parseInt(value, 10);
|
||||
break;
|
||||
case 'readPreferenceTags':
|
||||
// Decode the value
|
||||
value = decodeURIComponent(value);
|
||||
// Contains the tag object
|
||||
var tagObject = {};
|
||||
if(value == null || value == '') {
|
||||
dbOptions.read_preference_tags.push(tagObject);
|
||||
break;
|
||||
}
|
||||
|
||||
// Split up the tags
|
||||
var tags = value.split(/\,/);
|
||||
for(var i = 0; i < tags.length; i++) {
|
||||
var parts = tags[i].trim().split(/\:/);
|
||||
tagObject[parts[0]] = parts[1];
|
||||
}
|
||||
|
||||
// Set the preferences tags
|
||||
dbOptions.read_preference_tags.push(tagObject);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
// No tags: should be null (not [])
|
||||
if(dbOptions.read_preference_tags.length === 0) {
|
||||
dbOptions.read_preference_tags = null;
|
||||
}
|
||||
|
||||
// Validate if there are an invalid write concern combinations
|
||||
if((dbOptions.w == -1 || dbOptions.w == 0) && (
|
||||
dbOptions.journal == true
|
||||
|| dbOptions.fsync == true
|
||||
|| dbOptions.safe == true)) throw new Error("w set to -1 or 0 cannot be combined with safe/w/journal/fsync")
|
||||
|
||||
// If no read preference set it to primary
|
||||
if(!dbOptions.readPreference) {
|
||||
dbOptions.readPreference = 'primary';
|
||||
}
|
||||
|
||||
// make sure that user-provided options are applied with priority
|
||||
dbOptions = assign(dbOptions, options);
|
||||
|
||||
// Add servers to result
|
||||
object.servers = servers;
|
||||
|
||||
// Returned parsed object
|
||||
return object;
|
||||
}
|
||||
+375
@@ -0,0 +1,375 @@
|
||||
"use strict";
|
||||
|
||||
var MongoError = require('mongodb-core').MongoError,
|
||||
ReadPreference = require('./read_preference'),
|
||||
CoreReadPreference = require('mongodb-core').ReadPreference;
|
||||
|
||||
var shallowClone = function(obj) {
|
||||
var copy = {};
|
||||
for(var name in obj) copy[name] = obj[name];
|
||||
return copy;
|
||||
}
|
||||
|
||||
// Figure out the read preference
|
||||
var getReadPreference = function(options) {
|
||||
var r = null
|
||||
if(options.readPreference) {
|
||||
r = options.readPreference
|
||||
} else {
|
||||
return options;
|
||||
}
|
||||
|
||||
if(r instanceof ReadPreference) {
|
||||
options.readPreference = new CoreReadPreference(r.mode, r.tags, {maxStalenessSeconds: r.maxStalenessSeconds});
|
||||
} else if(typeof r == 'string') {
|
||||
options.readPreference = new CoreReadPreference(r);
|
||||
} else if(r && !(r instanceof ReadPreference) && typeof r == 'object') {
|
||||
var mode = r.mode || r.preference;
|
||||
if (mode && typeof mode == 'string') {
|
||||
options.readPreference = new CoreReadPreference(mode, r.tags, {maxStalenessSeconds: r.maxStalenessSeconds});
|
||||
}
|
||||
}
|
||||
|
||||
return options;
|
||||
}
|
||||
|
||||
// Set simple property
|
||||
var getSingleProperty = function(obj, name, value) {
|
||||
Object.defineProperty(obj, name, {
|
||||
enumerable:true,
|
||||
get: function() {
|
||||
return value
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
var formatSortValue = exports.formatSortValue = function(sortDirection) {
|
||||
var value = ("" + sortDirection).toLowerCase();
|
||||
|
||||
switch (value) {
|
||||
case 'ascending':
|
||||
case 'asc':
|
||||
case '1':
|
||||
return 1;
|
||||
case 'descending':
|
||||
case 'desc':
|
||||
case '-1':
|
||||
return -1;
|
||||
default:
|
||||
throw new Error("Illegal sort clause, must be of the form "
|
||||
+ "[['field1', '(ascending|descending)'], "
|
||||
+ "['field2', '(ascending|descending)']]");
|
||||
}
|
||||
};
|
||||
|
||||
var formattedOrderClause = exports.formattedOrderClause = function(sortValue) {
|
||||
var orderBy = {};
|
||||
if(sortValue == null) return null;
|
||||
if (Array.isArray(sortValue)) {
|
||||
if(sortValue.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
for(var i = 0; i < sortValue.length; i++) {
|
||||
if(sortValue[i].constructor == String) {
|
||||
orderBy[sortValue[i]] = 1;
|
||||
} else {
|
||||
orderBy[sortValue[i][0]] = formatSortValue(sortValue[i][1]);
|
||||
}
|
||||
}
|
||||
} else if(sortValue != null && typeof sortValue == 'object') {
|
||||
orderBy = sortValue;
|
||||
} else if (typeof sortValue == 'string') {
|
||||
orderBy[sortValue] = 1;
|
||||
} else {
|
||||
throw new Error("Illegal sort clause, must be of the form " +
|
||||
"[['field1', '(ascending|descending)'], ['field2', '(ascending|descending)']]");
|
||||
}
|
||||
|
||||
return orderBy;
|
||||
};
|
||||
|
||||
var checkCollectionName = function checkCollectionName (collectionName) {
|
||||
if('string' !== typeof collectionName) {
|
||||
throw new MongoError("collection name must be a String");
|
||||
}
|
||||
|
||||
if(!collectionName || collectionName.indexOf('..') != -1) {
|
||||
throw new MongoError("collection names cannot be empty");
|
||||
}
|
||||
|
||||
if(collectionName.indexOf('$') != -1 &&
|
||||
collectionName.match(/((^\$cmd)|(oplog\.\$main))/) == null) {
|
||||
throw new MongoError("collection names must not contain '$'");
|
||||
}
|
||||
|
||||
if(collectionName.match(/^\.|\.$/) != null) {
|
||||
throw new MongoError("collection names must not start or end with '.'");
|
||||
}
|
||||
|
||||
// Validate that we are not passing 0x00 in the collection name
|
||||
if(!!~collectionName.indexOf("\x00")) {
|
||||
throw new MongoError("collection names cannot contain a null character");
|
||||
}
|
||||
};
|
||||
|
||||
var handleCallback = function(callback, err, value1, value2) {
|
||||
try {
|
||||
if(callback == null) return;
|
||||
if(callback) {
|
||||
return value2 ? callback(err, value1, value2) : callback(err, value1);
|
||||
}
|
||||
} catch(err) {
|
||||
process.nextTick(function() { throw err; });
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrap a Mongo error document in an Error instance
|
||||
* @ignore
|
||||
* @api private
|
||||
*/
|
||||
var toError = function(error) {
|
||||
if (error instanceof Error) return error;
|
||||
|
||||
var msg = error.err || error.errmsg || error.errMessage || error;
|
||||
var e = MongoError.create({message: msg, driver:true});
|
||||
|
||||
// Get all object keys
|
||||
var keys = typeof error == 'object'
|
||||
? Object.keys(error)
|
||||
: [];
|
||||
|
||||
for(var i = 0; i < keys.length; i++) {
|
||||
try {
|
||||
e[keys[i]] = error[keys[i]];
|
||||
} catch(err) {
|
||||
// continue
|
||||
}
|
||||
}
|
||||
|
||||
return e;
|
||||
}
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
var normalizeHintField = function normalizeHintField(hint) {
|
||||
var finalHint = null;
|
||||
|
||||
if(typeof hint == 'string') {
|
||||
finalHint = hint;
|
||||
} else if(Array.isArray(hint)) {
|
||||
finalHint = {};
|
||||
|
||||
hint.forEach(function(param) {
|
||||
finalHint[param] = 1;
|
||||
});
|
||||
} else if(hint != null && typeof hint == 'object') {
|
||||
finalHint = {};
|
||||
for (var name in hint) {
|
||||
finalHint[name] = hint[name];
|
||||
}
|
||||
}
|
||||
|
||||
return finalHint;
|
||||
};
|
||||
|
||||
/**
|
||||
* Create index name based on field spec
|
||||
*
|
||||
* @ignore
|
||||
* @api private
|
||||
*/
|
||||
var parseIndexOptions = function(fieldOrSpec) {
|
||||
var fieldHash = {};
|
||||
var indexes = [];
|
||||
var keys;
|
||||
|
||||
// Get all the fields accordingly
|
||||
if('string' == typeof fieldOrSpec) {
|
||||
// 'type'
|
||||
indexes.push(fieldOrSpec + '_' + 1);
|
||||
fieldHash[fieldOrSpec] = 1;
|
||||
} else if(Array.isArray(fieldOrSpec)) {
|
||||
fieldOrSpec.forEach(function(f) {
|
||||
if('string' == typeof f) {
|
||||
// [{location:'2d'}, 'type']
|
||||
indexes.push(f + '_' + 1);
|
||||
fieldHash[f] = 1;
|
||||
} else if(Array.isArray(f)) {
|
||||
// [['location', '2d'],['type', 1]]
|
||||
indexes.push(f[0] + '_' + (f[1] || 1));
|
||||
fieldHash[f[0]] = f[1] || 1;
|
||||
} else if(isObject(f)) {
|
||||
// [{location:'2d'}, {type:1}]
|
||||
keys = Object.keys(f);
|
||||
keys.forEach(function(k) {
|
||||
indexes.push(k + '_' + f[k]);
|
||||
fieldHash[k] = f[k];
|
||||
});
|
||||
} else {
|
||||
// undefined (ignore)
|
||||
}
|
||||
});
|
||||
} else if(isObject(fieldOrSpec)) {
|
||||
// {location:'2d', type:1}
|
||||
keys = Object.keys(fieldOrSpec);
|
||||
keys.forEach(function(key) {
|
||||
indexes.push(key + '_' + fieldOrSpec[key]);
|
||||
fieldHash[key] = fieldOrSpec[key];
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
name: indexes.join("_"), keys: keys, fieldHash: fieldHash
|
||||
}
|
||||
}
|
||||
|
||||
var isObject = exports.isObject = function (arg) {
|
||||
return '[object Object]' == Object.prototype.toString.call(arg)
|
||||
}
|
||||
|
||||
var debugOptions = function(debugFields, options) {
|
||||
var finaloptions = {};
|
||||
debugFields.forEach(function(n) {
|
||||
finaloptions[n] = options[n];
|
||||
});
|
||||
|
||||
return finaloptions;
|
||||
}
|
||||
|
||||
var decorateCommand = function(command, options, exclude) {
|
||||
for(var name in options) {
|
||||
if(exclude[name] == null) command[name] = options[name];
|
||||
}
|
||||
|
||||
return command;
|
||||
}
|
||||
|
||||
var mergeOptions = function(target, source) {
|
||||
for(var name in source) {
|
||||
target[name] = source[name];
|
||||
}
|
||||
|
||||
return target;
|
||||
}
|
||||
|
||||
// Merge options with translation
|
||||
var translateOptions = function(target, source) {
|
||||
var translations = {
|
||||
// SSL translation options
|
||||
'sslCA': 'ca', 'sslCRL': 'crl', 'sslValidate': 'rejectUnauthorized', 'sslKey': 'key',
|
||||
'sslCert': 'cert', 'sslPass': 'passphrase',
|
||||
// SocketTimeout translation options
|
||||
'socketTimeoutMS': 'socketTimeout', 'connectTimeoutMS': 'connectionTimeout',
|
||||
// Replicaset options
|
||||
'replicaSet': 'setName', 'rs_name': 'setName', 'secondaryAcceptableLatencyMS': 'acceptableLatency',
|
||||
'connectWithNoPrimary': 'secondaryOnlyConnectionAllowed',
|
||||
// Mongos options
|
||||
'acceptableLatencyMS': 'localThresholdMS'
|
||||
}
|
||||
|
||||
for(var name in source) {
|
||||
if(translations[name]) {
|
||||
target[translations[name]] = source[name];
|
||||
} else {
|
||||
target[name] = source[name];
|
||||
}
|
||||
}
|
||||
|
||||
return target;
|
||||
}
|
||||
|
||||
var filterOptions = function(options, names) {
|
||||
var filterOptions = {};
|
||||
|
||||
for(var name in options) {
|
||||
if(names.indexOf(name) != -1) filterOptions[name] = options[name];
|
||||
}
|
||||
|
||||
// Filtered options
|
||||
return filterOptions;
|
||||
}
|
||||
|
||||
// Object.assign method or polyfill
|
||||
var assign = Object.assign ? Object.assign : function assign(target) {
|
||||
if (target === undefined || target === null) {
|
||||
throw new TypeError('Cannot convert first argument to object');
|
||||
}
|
||||
|
||||
var to = Object(target);
|
||||
for (var i = 1; i < arguments.length; i++) {
|
||||
var nextSource = arguments[i];
|
||||
if (nextSource === undefined || nextSource === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
var keysArray = Object.keys(Object(nextSource));
|
||||
for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) {
|
||||
var nextKey = keysArray[nextIndex];
|
||||
var desc = Object.getOwnPropertyDescriptor(nextSource, nextKey);
|
||||
if (desc !== undefined && desc.enumerable) {
|
||||
to[nextKey] = nextSource[nextKey];
|
||||
}
|
||||
}
|
||||
}
|
||||
return to;
|
||||
}
|
||||
|
||||
// Write concern keys
|
||||
var writeConcernKeys = ['w', 'j', 'wtimeout', 'fsync'];
|
||||
|
||||
// Merge the write concern options
|
||||
var mergeOptionsAndWriteConcern = function(targetOptions, sourceOptions, keys, mergeWriteConcern) {
|
||||
// Mix in any allowed options
|
||||
for(var i = 0; i < keys.length; i++) {
|
||||
if(!targetOptions[keys[i]] && sourceOptions[keys[i]] != undefined) {
|
||||
targetOptions[keys[i]] = sourceOptions[keys[i]];
|
||||
}
|
||||
}
|
||||
|
||||
// No merging of write concern
|
||||
if(!mergeWriteConcern) return targetOptions;
|
||||
|
||||
// Found no write Concern options
|
||||
var found = false;
|
||||
for(var i = 0; i < writeConcernKeys.length; i++) {
|
||||
if(targetOptions[writeConcernKeys[i]]) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(!found) {
|
||||
for(var i = 0; i < writeConcernKeys.length; i++) {
|
||||
if(sourceOptions[writeConcernKeys[i]]) {
|
||||
targetOptions[writeConcernKeys[i]] = sourceOptions[writeConcernKeys[i]];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return targetOptions;
|
||||
}
|
||||
|
||||
exports.filterOptions = filterOptions;
|
||||
exports.mergeOptions = mergeOptions;
|
||||
exports.translateOptions = translateOptions;
|
||||
exports.shallowClone = shallowClone;
|
||||
exports.getSingleProperty = getSingleProperty;
|
||||
exports.checkCollectionName = checkCollectionName;
|
||||
exports.toError = toError;
|
||||
exports.formattedOrderClause = formattedOrderClause;
|
||||
exports.parseIndexOptions = parseIndexOptions;
|
||||
exports.normalizeHintField = normalizeHintField;
|
||||
exports.handleCallback = handleCallback;
|
||||
exports.decorateCommand = decorateCommand;
|
||||
exports.isObject = isObject;
|
||||
exports.debugOptions = debugOptions;
|
||||
exports.MAX_JS_INT = 0x20000000000000;
|
||||
exports.assign = assign;
|
||||
exports.mergeOptionsAndWriteConcern = mergeOptionsAndWriteConcern;
|
||||
exports.getReadPreference = getReadPreference;
|
||||
+90
@@ -0,0 +1,90 @@
|
||||
{
|
||||
"_from": "mongodb@2.2.34",
|
||||
"_id": "mongodb@2.2.34",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha1-o09Zu+thdUrsQy3nLD/iFSakTBo=",
|
||||
"_location": "/mongodb",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "version",
|
||||
"registry": true,
|
||||
"raw": "mongodb@2.2.34",
|
||||
"name": "mongodb",
|
||||
"escapedName": "mongodb",
|
||||
"rawSpec": "2.2.34",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "2.2.34"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/mongoose"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/mongodb/-/mongodb-2.2.34.tgz",
|
||||
"_shasum": "a34f59bbeb61754aec432de72c3fe21526a44c1a",
|
||||
"_spec": "mongodb@2.2.34",
|
||||
"_where": "/app/node_modules/mongoose",
|
||||
"author": {
|
||||
"name": "Christian Kvalheim"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/mongodb/node-mongodb-native/issues"
|
||||
},
|
||||
"bundleDependencies": false,
|
||||
"dependencies": {
|
||||
"es6-promise": "3.2.1",
|
||||
"mongodb-core": "2.1.18",
|
||||
"readable-stream": "2.2.7"
|
||||
},
|
||||
"deprecated": false,
|
||||
"description": "The official MongoDB driver for Node.js",
|
||||
"devDependencies": {
|
||||
"JSONStream": "^1.0.7",
|
||||
"betterbenchmarks": "^0.1.0",
|
||||
"bluebird": "3.4.6",
|
||||
"bson": "latest",
|
||||
"cli-table": "^0.3.1",
|
||||
"co": "4.6.0",
|
||||
"colors": "^1.1.2",
|
||||
"coveralls": "^2.11.6",
|
||||
"eslint": "^3.8.1",
|
||||
"event-stream": "^3.3.2",
|
||||
"gleak": "0.5.0",
|
||||
"integra": "0.1.8",
|
||||
"jsdoc": "3.4.0",
|
||||
"ldjson-stream": "^1.2.1",
|
||||
"mongodb-extended-json": "1.7.1",
|
||||
"mongodb-topology-manager": "1.0.x",
|
||||
"mongodb-version-manager": "github:christkv/mongodb-version-manager#master",
|
||||
"nyc": "^8.1.0",
|
||||
"optimist": "0.6.1",
|
||||
"rimraf": "2.5.4",
|
||||
"semver": "5.3.0",
|
||||
"worker-farm": "^1.3.1"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=0.10.3"
|
||||
},
|
||||
"homepage": "https://github.com/mongodb/node-mongodb-native",
|
||||
"keywords": [
|
||||
"mongodb",
|
||||
"driver",
|
||||
"official"
|
||||
],
|
||||
"license": "Apache-2.0",
|
||||
"main": "index.js",
|
||||
"name": "mongodb",
|
||||
"nyc": {
|
||||
"include": [
|
||||
"lib/**/*.js"
|
||||
]
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+ssh://git@github.com/mongodb/node-mongodb-native.git"
|
||||
},
|
||||
"scripts": {
|
||||
"coverage": "nyc node test/runner.js -t functional && node_modules/.bin/nyc report --reporter=text-lcov | node_modules/.bin/coveralls",
|
||||
"lint": "eslint lib",
|
||||
"test": "node test/runner.js -t functional"
|
||||
},
|
||||
"version": "2.2.34"
|
||||
}
|
||||
+931
@@ -0,0 +1,931 @@
|
||||
<a name="2.2.34"></a>
|
||||
## [2.2.34](https://github.com/mongodb/node-mongodb-native/compare/v2.2.33...v2.2.34) (2018-01-03)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* **collection:** allow { upsert: 1 } for findOneAndUpdate() and update() ([#1580](https://github.com/mongodb/node-mongodb-native/issues/1580)) ([0f338c8](https://github.com/mongodb/node-mongodb-native/commit/0f338c8)), closes [Automattic/mongoose#5839](https://github.com/Automattic/mongoose/issues/5839)
|
||||
* **GridFS:** fix TypeError: doc.data.length is not a function ([811de0c](https://github.com/mongodb/node-mongodb-native/commit/811de0c))
|
||||
* **import:** adds missing import to lib/authenticate.js ([10db9a2](https://github.com/mongodb/node-mongodb-native/commit/10db9a2))
|
||||
* **list-collections:** ensure default of primary ReadPreference ([0935306](https://github.com/mongodb/node-mongodb-native/commit/0935306))
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* **ss:** adds missing ssl options ssl options for `ciphers` and `ecdhCurve` ([bd4fb53](https://github.com/mongodb/node-mongodb-native/commit/bd4fb53))
|
||||
* **url parser:** add dns seedlist support ([2d357bc](https://github.com/mongodb/node-mongodb-native/commit/2d357bc))
|
||||
|
||||
|
||||
|
||||
<a name="2.2.33"></a>
|
||||
## [2.2.33](https://github.com/mongodb/node-mongodb-native/compare/v2.2.32...v2.2.33) (2017-10-12)
|
||||
|
||||
|
||||
|
||||
<a name="2.2.32"></a>
|
||||
## [2.2.32](https://github.com/mongodb/node-mongodb-native/compare/v2.2.31...v2.2.32) (2017-10-12)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* **aggregation:** ensure that the `cursor` key is always present ([beccc83](https://github.com/mongodb/node-mongodb-native/commit/beccc83))
|
||||
* **collection:** allow passing `noCursorTimeout` as an option to `find()` ([7bd839b](https://github.com/mongodb/node-mongodb-native/commit/7bd839b))
|
||||
* **cursor:** `hasNext` should propagate errors when using callback ([129d540](https://github.com/mongodb/node-mongodb-native/commit/129d540))
|
||||
* **db:** bubble up `reconnectFailed` event from Server topology ([d159eab](https://github.com/mongodb/node-mongodb-native/commit/d159eab))
|
||||
|
||||
|
||||
|
||||
<a name="2.2.31"></a>
|
||||
## [2.2.31](https://github.com/mongodb/node-mongodb-native/compare/v0.9.3...v2.2.31) (2017-08-08)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* **bulk:** bulk operations should not throw an error on empty batch ([84433e7](https://github.com/mongodb/node-mongodb-native/commit/84433e7))
|
||||
* **url-parser:** ensure user options are applied to parsing ([7116ea4](https://github.com/mongodb/node-mongodb-native/commit/7116ea4))
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* **auth:** allow auth option in MongoClient.connect ([fd686d7](https://github.com/mongodb/node-mongodb-native/commit/fd686d7))
|
||||
* **MongoClient:** add `appname` to list of valid option names ([bd4bdf6](https://github.com/mongodb/node-mongodb-native/commit/bd4bdf6))
|
||||
|
||||
|
||||
|
||||
<a name="2.2.29"></a>
|
||||
## 2.2.29 (2017-06-19)
|
||||
|
||||
|
||||
|
||||
<a name="2.2.28"></a>
|
||||
## 2.2.28 (2017-06-02)
|
||||
|
||||
|
||||
|
||||
<a name="2.2.27"></a>
|
||||
## 2.2.27 (2017-05-22)
|
||||
|
||||
|
||||
|
||||
<a name="2.2.26"></a>
|
||||
## 2.2.26 (2017-04-18)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* **db:** don't remove database name if collectionName == dbName ([ec88b07](https://github.com/mongodb/node-mongodb-native/commit/ec88b07))
|
||||
|
||||
|
||||
|
||||
<a name="2.2.25"></a>
|
||||
## 2.2.25 (2017-03-17)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* **collection:** pass batchSize to AggregationCursor ([b5c188b](https://github.com/mongodb/node-mongodb-native/commit/b5c188b))
|
||||
* **UnorderedBulkOperation:** push correct index for INSERT ops ([e53d02c](https://github.com/mongodb/node-mongodb-native/commit/e53d02c))
|
||||
* don't rely on global toString() for checking if object ([6ef70b8](https://github.com/mongodb/node-mongodb-native/commit/6ef70b8)), closes [tmpvar/jsdom#1752](https://github.com/tmpvar/jsdom/issues/1752) [Automattic/mongoose#5033](https://github.com/Automattic/mongoose/issues/5033)
|
||||
|
||||
|
||||
|
||||
<a name="2.2.24"></a>
|
||||
## 2.2.24 (2017-02-14)
|
||||
|
||||
|
||||
|
||||
<a name="2.2.23"></a>
|
||||
## 2.2.23 (2017-02-13)
|
||||
|
||||
|
||||
|
||||
<a name="2.2.22"></a>
|
||||
## 2.2.22 (2017-01-24)
|
||||
|
||||
|
||||
|
||||
<a name="2.2.21"></a>
|
||||
## 2.2.21 (2017-01-13)
|
||||
|
||||
|
||||
|
||||
<a name="2.2.20"></a>
|
||||
## 2.2.20 (2017-01-11)
|
||||
|
||||
|
||||
|
||||
<a name="2.2.19"></a>
|
||||
## 2.2.19 (2017-01-03)
|
||||
|
||||
|
||||
|
||||
<a name="2.2.18"></a>
|
||||
## 2.2.18 (2017-01-03)
|
||||
|
||||
|
||||
|
||||
<a name="2.2.17"></a>
|
||||
## 2.2.17 (2017-01-03)
|
||||
|
||||
|
||||
|
||||
<a name="2.2.16"></a>
|
||||
## 2.2.16 (2016-12-13)
|
||||
|
||||
|
||||
|
||||
<a name="2.2.15"></a>
|
||||
## 2.2.15 (2016-12-10)
|
||||
|
||||
|
||||
|
||||
<a name="2.2.14"></a>
|
||||
## 2.2.14 (2016-12-08)
|
||||
|
||||
|
||||
|
||||
<a name="2.2.13"></a>
|
||||
## 2.2.13 (2016-11-30)
|
||||
|
||||
|
||||
|
||||
<a name="2.2.12"></a>
|
||||
## 2.2.12 (2016-11-29)
|
||||
|
||||
|
||||
|
||||
<a name="2.2.11"></a>
|
||||
## 2.2.11 (2016-10-21)
|
||||
|
||||
|
||||
|
||||
<a name="2.2.10"></a>
|
||||
## 2.2.10 (2016-09-15)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* **db:** don't fall back to insert if 'IndexOptionsConflict' error ([a26fe5f](https://github.com/mongodb/node-mongodb-native/commit/a26fe5f)), closes [Automattic/mongoose#4459](https://github.com/Automattic/mongoose/issues/4459)
|
||||
|
||||
|
||||
|
||||
<a name="2.2.9"></a>
|
||||
## 2.2.9 (2016-08-29)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* **collection:** don't treat ObjectId as object for mapReduce scope ([a1ca631](https://github.com/mongodb/node-mongodb-native/commit/a1ca631))
|
||||
|
||||
|
||||
|
||||
<a name="2.2.8"></a>
|
||||
## 2.2.8 (2016-08-23)
|
||||
|
||||
|
||||
|
||||
<a name="2.2.6"></a>
|
||||
## 2.2.6 (2016-08-16)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* allow passing in an array of tags to ReadPreference constructor ([5b8067c](https://github.com/mongodb/node-mongodb-native/commit/5b8067c))
|
||||
|
||||
|
||||
|
||||
<a name="2.2.5"></a>
|
||||
## 2.2.5 (2016-07-28)
|
||||
|
||||
|
||||
|
||||
<a name="2.2.4"></a>
|
||||
## 2.2.4 (2016-07-19)
|
||||
|
||||
|
||||
|
||||
<a name="2.2.3"></a>
|
||||
## 2.2.3 (2016-07-19)
|
||||
|
||||
|
||||
|
||||
<a name="2.2.2"></a>
|
||||
## 2.2.2 (2016-07-15)
|
||||
|
||||
|
||||
|
||||
<a name="2.2.1"></a>
|
||||
## 2.2.1 (2016-07-11)
|
||||
|
||||
|
||||
|
||||
<a name="2.2.0"></a>
|
||||
# 2.2.0 (2016-07-08)
|
||||
|
||||
|
||||
|
||||
<a name="2.2.0-alpha2"></a>
|
||||
# 2.2.0-alpha2 (2016-07-06)
|
||||
|
||||
|
||||
|
||||
<a name="2.2.0-alpha1"></a>
|
||||
# 2.2.0-alpha1 (2016-07-05)
|
||||
|
||||
|
||||
|
||||
<a name="2.1.21"></a>
|
||||
## 2.1.21 (2016-05-30)
|
||||
|
||||
|
||||
|
||||
<a name="2.1.18"></a>
|
||||
## 2.1.18 (2016-04-27)
|
||||
|
||||
|
||||
|
||||
<a name="2.1.17"></a>
|
||||
## 2.1.17 (2016-04-26)
|
||||
|
||||
|
||||
|
||||
<a name="2.1.16"></a>
|
||||
## 2.1.16 (2016-04-07)
|
||||
|
||||
|
||||
|
||||
<a name="2.1.15"></a>
|
||||
## 2.1.15 (2016-04-06)
|
||||
|
||||
|
||||
|
||||
<a name="2.1.14"></a>
|
||||
## 2.1.14 (2016-03-30)
|
||||
|
||||
|
||||
|
||||
<a name="2.1.13"></a>
|
||||
## 2.1.13 (2016-03-29)
|
||||
|
||||
|
||||
|
||||
<a name="2.1.12"></a>
|
||||
## 2.1.12 (2016-03-29)
|
||||
|
||||
|
||||
|
||||
<a name="2.1.11"></a>
|
||||
## 2.1.11 (2016-03-23)
|
||||
|
||||
|
||||
|
||||
<a name="2.1.10"></a>
|
||||
## 2.1.10 (2016-03-21)
|
||||
|
||||
|
||||
|
||||
<a name="2.1.9"></a>
|
||||
## 2.1.9 (2016-03-16)
|
||||
|
||||
|
||||
|
||||
<a name="2.1.8"></a>
|
||||
## 2.1.8 (2016-03-14)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* **db:** one possible fix for Automattic/mongoose[#3864](https://github.com/mongodb/node-mongodb-native/issues/3864) ([4712aab](https://github.com/mongodb/node-mongodb-native/commit/4712aab))
|
||||
|
||||
|
||||
|
||||
<a name="2.1.7"></a>
|
||||
## 2.1.7 (2016-02-09)
|
||||
|
||||
|
||||
|
||||
<a name="2.1.6"></a>
|
||||
## 2.1.6 (2016-02-05)
|
||||
|
||||
|
||||
|
||||
<a name="2.1.5"></a>
|
||||
## 2.1.5 (2016-02-04)
|
||||
|
||||
|
||||
|
||||
<a name="2.1.4"></a>
|
||||
## 2.1.4 (2016-01-14)
|
||||
|
||||
|
||||
|
||||
<a name="2.1.3"></a>
|
||||
## 2.1.3 (2016-01-04)
|
||||
|
||||
|
||||
|
||||
<a name="2.1.2"></a>
|
||||
## 2.1.2 (2015-12-23)
|
||||
|
||||
|
||||
|
||||
<a name="2.1.1"></a>
|
||||
## 2.1.1 (2015-12-13)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.50"></a>
|
||||
## 2.0.50 (2015-12-06)
|
||||
|
||||
|
||||
|
||||
<a name="2.1.0"></a>
|
||||
# 2.1.0 (2015-12-06)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* **url_parser:** readPreference option ([db57c6b](https://github.com/mongodb/node-mongodb-native/commit/db57c6b))
|
||||
|
||||
|
||||
|
||||
<a name="2.0.49"></a>
|
||||
## 2.0.49 (2015-11-20)
|
||||
|
||||
|
||||
|
||||
<a name="2.1.0-rc1"></a>
|
||||
# 2.1.0-rc1 (2015-11-16)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.48"></a>
|
||||
## 2.0.48 (2015-11-07)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.47"></a>
|
||||
## 2.0.47 (2015-10-28)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.46"></a>
|
||||
## 2.0.46 (2015-10-15)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.45"></a>
|
||||
## 2.0.45 (2015-09-30)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.44"></a>
|
||||
## 2.0.44 (2015-09-28)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.43"></a>
|
||||
## 2.0.43 (2015-09-14)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.42"></a>
|
||||
## 2.0.42 (2015-08-18)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.40"></a>
|
||||
## 2.0.40 (2015-08-06)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.39"></a>
|
||||
## 2.0.39 (2015-07-14)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.38"></a>
|
||||
## 2.0.38 (2015-07-14)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.37"></a>
|
||||
## 2.0.37 (2015-07-14)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.36"></a>
|
||||
## 2.0.36 (2015-07-07)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.35"></a>
|
||||
## 2.0.35 (2015-06-22)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.34"></a>
|
||||
## 2.0.34 (2015-06-17)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.33"></a>
|
||||
## 2.0.33 (2015-05-19)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.32"></a>
|
||||
## 2.0.32 (2015-05-18)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.31"></a>
|
||||
## 2.0.31 (2015-05-08)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.30"></a>
|
||||
## 2.0.30 (2015-05-07)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.29"></a>
|
||||
## 2.0.29 (2015-05-07)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.28"></a>
|
||||
## 2.0.28 (2015-04-24)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.27"></a>
|
||||
## 2.0.27 (2015-04-07)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.26"></a>
|
||||
## 2.0.26 (2015-04-07)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.25"></a>
|
||||
## 2.0.25 (2015-03-26)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.24"></a>
|
||||
## 2.0.24 (2015-03-24)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.23"></a>
|
||||
## 2.0.23 (2015-03-21)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.22"></a>
|
||||
## 2.0.22 (2015-03-16)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.21"></a>
|
||||
## 2.0.21 (2015-03-06)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.20"></a>
|
||||
## 2.0.20 (2015-03-04)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.19"></a>
|
||||
## 2.0.19 (2015-03-03)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.18"></a>
|
||||
## 2.0.18 (2015-02-27)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.17"></a>
|
||||
## 2.0.17 (2015-02-27)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.16"></a>
|
||||
## 2.0.16 (2015-02-16)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.15"></a>
|
||||
## 2.0.15 (2015-02-02)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.14"></a>
|
||||
## 2.0.14 (2015-01-21)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.13"></a>
|
||||
## 2.0.13 (2015-01-08)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.12"></a>
|
||||
## 2.0.12 (2014-12-22)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.11"></a>
|
||||
## 2.0.11 (2014-12-18)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.10"></a>
|
||||
## 2.0.10 (2014-12-18)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.9"></a>
|
||||
## 2.0.9 (2014-12-02)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.8"></a>
|
||||
## 2.0.8 (2014-11-28)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.7"></a>
|
||||
## 2.0.7 (2014-11-20)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.5"></a>
|
||||
## 2.0.5 (2014-10-29)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.4"></a>
|
||||
## 2.0.4 (2014-10-23)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.3"></a>
|
||||
## 2.0.3 (2014-10-14)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.2"></a>
|
||||
## 2.0.2 (2014-10-08)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.1"></a>
|
||||
## 2.0.1 (2014-10-07)
|
||||
|
||||
|
||||
|
||||
<a name="2.0.0"></a>
|
||||
# 2.0.0 (2014-10-07)
|
||||
|
||||
|
||||
|
||||
<a name="1.4.8"></a>
|
||||
## 1.4.8 (2014-08-01)
|
||||
|
||||
|
||||
|
||||
<a name="1.4.7"></a>
|
||||
## 1.4.7 (2014-06-18)
|
||||
|
||||
|
||||
|
||||
<a name="1.4.5"></a>
|
||||
## 1.4.5 (2014-05-20)
|
||||
|
||||
|
||||
|
||||
<a name="1.4.4"></a>
|
||||
## 1.4.4 (2014-05-13)
|
||||
|
||||
|
||||
|
||||
<a name="1.4.3"></a>
|
||||
## 1.4.3 (2014-05-01)
|
||||
|
||||
|
||||
|
||||
<a name="1.4.2"></a>
|
||||
## 1.4.2 (2014-04-15)
|
||||
|
||||
|
||||
|
||||
<a name="1.4.1"></a>
|
||||
## 1.4.1 (2014-04-09)
|
||||
|
||||
|
||||
|
||||
<a name="1.4.0"></a>
|
||||
# 1.4.0 (2014-04-03)
|
||||
|
||||
|
||||
|
||||
<a name="1.4.0-RC10"></a>
|
||||
# 1.4.0-RC10 (2014-03-22)
|
||||
|
||||
|
||||
|
||||
<a name="1.4.0-RC9"></a>
|
||||
# 1.4.0-RC9 (2014-03-12)
|
||||
|
||||
|
||||
|
||||
<a name="1.4.0-RC4"></a>
|
||||
# 1.4.0-RC4 (2014-01-10)
|
||||
|
||||
|
||||
|
||||
<a name="1.3.23"></a>
|
||||
## 1.3.23 (2013-12-13)
|
||||
|
||||
|
||||
|
||||
<a name="1.3.22"></a>
|
||||
## 1.3.22 (2013-12-11)
|
||||
|
||||
|
||||
|
||||
<a name="1.3.21"></a>
|
||||
## 1.3.21 (2013-12-11)
|
||||
|
||||
|
||||
|
||||
<a name="1.4.0-RC2"></a>
|
||||
# 1.4.0-RC2 (2013-12-10)
|
||||
|
||||
|
||||
|
||||
<a name="1.4.0-RC1"></a>
|
||||
# 1.4.0-RC1 (2013-12-10)
|
||||
|
||||
|
||||
|
||||
<a name="1.3.19"></a>
|
||||
## 1.3.19 (2013-08-21)
|
||||
|
||||
|
||||
|
||||
<a name="1.3.17"></a>
|
||||
## 1.3.17 (2013-08-07)
|
||||
|
||||
|
||||
|
||||
<a name="1.3.16"></a>
|
||||
## 1.3.16 (2013-08-05)
|
||||
|
||||
|
||||
|
||||
<a name="1.3.15"></a>
|
||||
## 1.3.15 (2013-08-01)
|
||||
|
||||
|
||||
|
||||
<a name="1.3.14"></a>
|
||||
## 1.3.14 (2013-08-01)
|
||||
|
||||
|
||||
|
||||
<a name="1.3.13"></a>
|
||||
## 1.3.13 (2013-07-31)
|
||||
|
||||
|
||||
|
||||
<a name="1.3.11"></a>
|
||||
## 1.3.11 (2013-07-04)
|
||||
|
||||
|
||||
|
||||
<a name="1.3.10"></a>
|
||||
## 1.3.10 (2013-06-17)
|
||||
|
||||
|
||||
|
||||
<a name="1.3.9"></a>
|
||||
## 1.3.9 (2013-06-06)
|
||||
|
||||
|
||||
|
||||
<a name="1.3.8"></a>
|
||||
## 1.3.8 (2013-05-31)
|
||||
|
||||
|
||||
|
||||
<a name="1.3.7"></a>
|
||||
## 1.3.7 (2013-05-29)
|
||||
|
||||
|
||||
|
||||
<a name="1.3.6"></a>
|
||||
## 1.3.6 (2013-05-27)
|
||||
|
||||
|
||||
|
||||
<a name="1.3.5"></a>
|
||||
## 1.3.5 (2013-05-14)
|
||||
|
||||
|
||||
|
||||
<a name="1.3.4"></a>
|
||||
## 1.3.4 (2013-05-14)
|
||||
|
||||
|
||||
|
||||
<a name="1.3.3"></a>
|
||||
## 1.3.3 (2013-05-09)
|
||||
|
||||
|
||||
|
||||
<a name="1.3.2"></a>
|
||||
## 1.3.2 (2013-05-08)
|
||||
|
||||
|
||||
|
||||
<a name="1.3.1"></a>
|
||||
## 1.3.1 (2013-05-06)
|
||||
|
||||
|
||||
|
||||
<a name="1.3.0"></a>
|
||||
# 1.3.0 (2013-04-26)
|
||||
|
||||
|
||||
|
||||
<a name="1.2.14"></a>
|
||||
## 1.2.14 (2013-03-14)
|
||||
|
||||
|
||||
|
||||
<a name="1.2.13"></a>
|
||||
## 1.2.13 (2013-02-22)
|
||||
|
||||
|
||||
|
||||
<a name="1.2.12"></a>
|
||||
## 1.2.12 (2013-02-13)
|
||||
|
||||
|
||||
|
||||
<a name="1.2.11"></a>
|
||||
## 1.2.11 (2013-01-29)
|
||||
|
||||
|
||||
|
||||
<a name="1.2.10"></a>
|
||||
## 1.2.10 (2013-01-25)
|
||||
|
||||
|
||||
|
||||
<a name="1.2.9"></a>
|
||||
## 1.2.9 (2013-01-15)
|
||||
|
||||
|
||||
|
||||
<a name="1.2.8"></a>
|
||||
## 1.2.8 (2013-01-07)
|
||||
|
||||
|
||||
|
||||
<a name="1.2.7"></a>
|
||||
## 1.2.7 (2012-12-23)
|
||||
|
||||
|
||||
|
||||
<a name="1.2.6"></a>
|
||||
## 1.2.6 (2012-12-19)
|
||||
|
||||
|
||||
|
||||
<a name="1.2.5"></a>
|
||||
## 1.2.5 (2012-12-12)
|
||||
|
||||
|
||||
|
||||
<a name="1.2.4"></a>
|
||||
## 1.2.4 (2012-12-11)
|
||||
|
||||
|
||||
|
||||
<a name="1.2.3"></a>
|
||||
## 1.2.3 (2012-12-10)
|
||||
|
||||
|
||||
|
||||
<a name="1.2.0"></a>
|
||||
# 1.2.0 (2012-11-27)
|
||||
|
||||
|
||||
|
||||
<a name="1.1.11"></a>
|
||||
## 1.1.11 (2012-10-10)
|
||||
|
||||
|
||||
|
||||
<a name="1.1.8"></a>
|
||||
## 1.1.8 (2012-10-01)
|
||||
|
||||
|
||||
|
||||
<a name="1.1.7"></a>
|
||||
## 1.1.7 (2012-09-10)
|
||||
|
||||
|
||||
|
||||
<a name="1.1.5"></a>
|
||||
## 1.1.5 (2012-08-29)
|
||||
|
||||
|
||||
|
||||
<a name="1.0.2"></a>
|
||||
## 1.0.2 (2012-05-15)
|
||||
|
||||
|
||||
|
||||
<a name="0.9.9"></a>
|
||||
## 0.9.9 (2012-02-13)
|
||||
|
||||
|
||||
|
||||
<a name="0.9.8"></a>
|
||||
## 0.9.8 (2012-01-17)
|
||||
|
||||
|
||||
|
||||
<a name="0.9.7"></a>
|
||||
## 0.9.7 (2011-11-10)
|
||||
|
||||
|
||||
|
||||
<a name="0.6.8"></a>
|
||||
## 0.6.8 (2011-08-01)
|
||||
|
||||
|
||||
|
||||
<a name="0.9.6-5"></a>
|
||||
## 0.9.6-5 (2011-07-06)
|
||||
|
||||
|
||||
|
||||
<a name="0.9.5"></a>
|
||||
## 0.9.5 (2011-06-20)
|
||||
|
||||
|
||||
|
||||
<a name="0.9.4"></a>
|
||||
## 0.9.4 (2011-04-29)
|
||||
|
||||
|
||||
|
||||
<a name="0.9.3"></a>
|
||||
## 0.9.3 (2011-04-15)
|
||||
|
||||
|
||||
|
||||
<a name="0.9.2"></a>
|
||||
## 0.9.2 (2011-03-22)
|
||||
|
||||
|
||||
|
||||
<a name="0.9.0"></a>
|
||||
# 0.9.0 (2011-02-27)
|
||||
|
||||
|
||||
|
||||
<a name="0.8.1"></a>
|
||||
## 0.8.1 (2010-08-21)
|
||||
|
||||
|
||||
|
||||
<a name="0.7.9"></a>
|
||||
## 0.7.9 (2010-07-05)
|
||||
|
||||
|
||||
|
||||
<a name="0.7.7"></a>
|
||||
## 0.7.7 (2010-06-15)
|
||||
|
||||
|
||||
|
||||
<a name="0.7.6"></a>
|
||||
## 0.7.6 (2010-06-10)
|
||||
|
||||
|
||||
|
||||
<a name="0.7.5"></a>
|
||||
## 0.7.5 (2010-05-27)
|
||||
|
||||
|
||||
|
||||
+3299
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user