Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[implements #6899] Update mongo drivers to latest version 3.5.5 #483

Merged
merged 20 commits into from
Nov 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
2a2ad24
Return 0 if there are no documents to sum
Josebaseba Aug 22, 2017
4278651
Merge branch 'master' of git://github.com/balderdashy/sails-mongo
Josebaseba Mar 29, 2020
40f5e09
update mongo driver and support +srv protocol
Josebaseba Mar 29, 2020
6b645b8
Update manager client connection
Josebaseba Mar 29, 2020
e96ad1c
Update package version to 1.2.0
Josebaseba Mar 29, 2020
d9068bc
Add 'retryWrites' to whitelist constants
Josebaseba Mar 29, 2020
167a5a9
Update collection.insert with .insertOne or .insertMany, avoiding dep…
Josebaseba Mar 30, 2020
3d2b617
Merge remote-tracking branch 'balderdashy/master'
luislobo Jun 28, 2020
059737f
Updated options whitelist constants based on current MongoDB 3.5 options
luislobo Jun 29, 2020
805b5b6
Make use of updateOne
luislobo Jun 29, 2020
cacdb52
Add test for client existence on create manager test
luislobo Jun 29, 2020
36d8157
Remove .jshint
luislobo Jun 29, 2020
e0640e7
Add .vscode ignore
luislobo Jun 29, 2020
ef4b004
Adds comments about MongoClient in the create manager file
luislobo Jun 29, 2020
d0b14f6
Updates mongodb version, and makes version 1.3.0 for more safety
luislobo Jun 29, 2020
2a638df
Updates changelog and readme
luislobo Jun 29, 2020
7d2c991
Merge pull request #1 from luislobo/upgrade-mongodb-drivers
Josebaseba Jun 29, 2020
69d0cdf
Update config-whitelist.constant.js
Josebaseba Nov 16, 2020
5c06944
Merge branch 'master' into master
Josebaseba Nov 16, 2020
f78f9fc
Remove comment that seems out-of-date now
rachaelshaw Nov 16, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,5 @@ nbproject
# misc
############################
dump.rdb

\.vscode/
134 changes: 0 additions & 134 deletions .jshintrc

This file was deleted.

12 changes: 8 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# sails-mongo Changelog

### 1.2.1
### 1.3.0
* [COMPATIBILITY] Upgrade mongodb driver to version 3.5.9. For more information about breaking changes, check the [Readme](./README.md) file. [@Josebaseba], [@luislobo]
* [DEPENDENCIES] Updates other dependencies to the latest available. [@luislobo]

### 1.2.1
* [ENHANCEMENT] Adds `npm run start-mongodb` and `npm run stop-mongodb` scripts to start/stop a MongoDB docker instance [@luislobo]
* [ENHANCEMENT] Adds `npm run mongodb-shell` to run a MongoDB Shell CLI that connects to the MongoDB docker instance [@luislobo]
* [INTERNAL] Bump and pin dependency versions [@luislobo]
Expand Down Expand Up @@ -46,10 +49,11 @@
* [STABILITY] Locked the dependency versions down.

---
[@Josebaseba]: https://github.com/Josebaseba
[@Mordred]: https://github.com/Mordred
[@andyhu]: https://github.com/andyhu
[@anterodev]: https://github.com/anterodev
[@luislobo]: https://github.com/luislobo
[@miccar]: https://github.com/miccarr
[@nicoespeon]: https://github.com/nicoespeon
[@andyhu]: https://github.com/andyhu
[@Mordred]: https://github.com/Mordred
[@vbud]: https://github.com/vbud
[@miccar]: https://github.com/miccarr
65 changes: 64 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,66 @@ Then [connect the adapter](http://sailsjs.com/documentation/reference/configurat

Visit [Models & ORM](http://sailsjs.com/docs/concepts/models-and-orm) in the docs for more information about using models, datastores, and adapters in your app/microservice.

## MongoDB Driver
From `sails-mongo` version 1.3.0 and above, the adapter uses [MongoDB driver for Node.js v3.5.9 (or above)](https://www.npmjs.com/package/mongodb).
The updated MongoDB driver changes the way it handles connections internally, and implements the concept of [MongoClient].

`manager` still returns a `database`. Access to the [MongoClient] object is done via `manager.client`:
```javascript
// Returns a MongoClient instance
Pet.getDatastore().manager.client
```

With access to the [MongoClient] object, now you have access to the latest MongoDB improvements, like [ClientSession],
and with it, transactions, [change streams](https://mongodb.github.io/node-mongodb-native/3.5/api/ChangeStream.html), and other new features.

#### `.native` still works but you can better use the client

With `native`:

```javascript
Pet.native(function (err, collection) {
if (err) {
return res.serverError(err);
}

collection.find({}, {
name: true
}).toArray(function (err, results) {
if (err) {
return res.serverError(err);
}
res.ok(results);
});
});
```

with `client`:

```javascript
try {
// This is an instance of MongoClient
// https://mongodb.github.io/node-mongodb-native/3.5/api/MongoClient.html
const mongoClient = Pet.getDatastore().manager.client;
const results = await mongoClient.db('test')
.collection('pet')
.find({}, { name: 1 })
.toArray();
res.ok(results);
} catch (err) {
res.serverError(err);
}
```

## Configuration options
This version uses [MongoDB 3.5.x connection options](https://mongodb.github.io/node-mongodb-native/3.5/api/MongoClient.html#.connect).

Check them out as there are some updated, changed, new and deprecated options.

## Roadmap

#### NEXT FEATURES TO BE IMPLEMENTED
- Waterline Built-in transactions, instead of using MongoClient

## Compatibility

Expand Down Expand Up @@ -110,7 +170,6 @@ Thanks so much to Ted Kulp ([@tedkulp](https://twitter.com/tedkulp)) and Robin P
To report a bug, [click here](http://sailsjs.com/bugs).



## License

This [core adapter](http://sailsjs.com/documentation/concepts/extending-sails/adapters/available-adapters) is available under the **MIT license**.
Expand All @@ -120,3 +179,7 @@ As for [Waterline](http://waterlinejs.org) and the [Sails framework](http://sail
© [The Sails Co.](http://sailsjs.com/about)

![image_squidhome@2x.png](http://i.imgur.com/RIvu9.png)

---
[MongoClient]: https://mongodb.github.io/node-mongodb-native/3.5/api/MongoClient.html
[ClientSession]: https://mongodb.github.io/node-mongodb-native/3.5/api/ClientSession.html
24 changes: 17 additions & 7 deletions lib/private/constants/config-whitelist.constant.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
// CONFIG_WHITELIST
//
// The set of non-standard property names in configuration to consider valid.
// Leave `undefined` to tolerate almost anything-- or set to an empty array to
// Leave 'undefined' to tolerate almost anything-- or set to an empty array to
// prevent everything except standard properties.
//
// > http://mongodb.github.io/node-mongodb-native/2.2/reference/connecting/connection-settings/
// > https://mongodb.github.io/node-mongodb-native/3.5/api/MongoClient.html#.connect
module.exports = [

// SSL Options:
'ssl', 'sslValidate', 'sslCA', 'sslCert', 'sslKey', 'sslPass',
'ssl', 'sslValidate', 'sslCA', 'sslCert', 'sslKey', 'sslPass', 'sslCRL', 'checkServerIdentity',

// TLS Options:
'tls', 'tlsInsecure', 'tlsCAFile', 'tlsCertificateKeyFile', 'tlsCertificateKeyFilePassword',
'tlsAllowInvalidCertificates', 'tlsAllowInvalidHostnames',

// Connection Options:
'poolSize', 'autoReconnect', 'noDelay', 'keepAlive', 'connectTimeoutMS',
'socketTimeoutMS', 'reconnectTries', 'reconnectInterval',
'poolSize', 'autoReconnect', 'noDelay', 'keepAlive', 'keepAliveInitialDelay', 'connectTimeoutMS',
'socketTimeoutMS', 'family', 'reconnectTries', 'reconnectInterval', 'retryWrites',

// Other Options:
'ha', 'haInterval', 'replicaSet', 'secondaryAcceptableLatencyMS',
'acceptableLatencyMS', 'connectWithNoPrimary', 'authSource', 'w',
'wtimeout', 'j', 'forceServerObjectId', 'serializeFunctions',
'ignoreUndefined', 'raw', 'promoteLongs', 'bufferMaxEntries',
'readPreference', 'pkFactory', 'readConcern', 'appname'
'ignoreUndefined', 'raw', 'bufferMaxEntries', 'readPreference',
'pkFactory', 'promiseLibrary', 'readConcern', 'maxStalenessSeconds',
'loggerLevel', 'logger', 'promoteValues', 'promoteLongs', 'promoteBuffers',
'domainsEnabled', 'validateOptions', 'appname', 'auth.user', 'auth.password',
'authMechanism', 'compression', 'fsync', 'readPreferenceTags', 'numberOfRetries',
'auto_reconnect', 'monitorCommands', 'minSize', 'useNewUrlParser', 'useUnifiedTopology',
'localThresholdMS', 'serverSelectionTimeoutMS', 'heartbeatFrequencyMS', 'autoEncryption',
'driverInfo'

];
16 changes: 12 additions & 4 deletions lib/private/machines/create-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,22 @@ module.exports = {
var mongoUrl = _clientConfig.url;
_clientConfig = _.omit(_clientConfig, ['url', 'user', 'password', 'host', 'port', 'database']);

NodeMongoDBNativeLib.MongoClient.connect(mongoUrl, _clientConfig, function connectCb(err, db) {
// Use unified topology. MongoDB node maintainers recommends this to be enabled
// https://github.com/mongodb/node-mongodb-native/releases/tag/v3.2.1
// Use new url parser to remove warnings
_clientConfig = Object.assign({
useNewUrlParser: true,
useUnifiedTopology: true
}, _clientConfig);

// http://mongodb.github.io/node-mongodb-native/3.5/api/MongoClient.html#.connect
NodeMongoDBNativeLib.MongoClient.connect(mongoUrl, _clientConfig, function connectCb(err, client) {
if (err) {
return exits.error(err);
}

// `db` will be our manager.
// (This variable is just for clarity.)
var manager = db;
var manager = client.db(_clientConfig.database);
manager.client = client;

// Now mutate this manager, giving it a telltale.
//
Expand Down
4 changes: 2 additions & 2 deletions lib/private/machines/destroy-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ module.exports = {
// If the manager doesn't have a `close` function for some reason,
// then catch that ahead of time so we can provide a slightly nicer
// error message and help prevent confusion.
if (!_.isObject(inputs.manager) || !_.isFunction(inputs.manager.close)) {
if (!_.isObject(inputs.manager.client) || !_.isFunction(inputs.manager.client.close)) {
return exits.error(new Error('The provided `manager` is not a valid manager created by this driver. (It should be a dictionary which contains a `close` function, at the very least.)'));
}

// Call close on the manager
inputs.manager.close();
inputs.manager.client.close();

return exits.success({
meta: inputs.meta
Expand Down
2 changes: 1 addition & 1 deletion lib/private/machines/release-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ module.exports = {
// If the connection doesn't have a `close` function for some reason,
// then catch that ahead of time so we can provide a slightly nicer
// error message and help prevent confusion.
if (!_.isObject(inputs.connection) || !_.isFunction(inputs.connection.close)) {
if (!_.isObject(inputs.connection.client) || !_.isFunction(inputs.connection.client.close)) {
return exits.badConnection();
}

Expand Down
2 changes: 1 addition & 1 deletion lib/private/normalize-datastore-config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ module.exports = function normalizeDatastoreConfig (dsConfig, whitelist, expecte

// If relevant, validate that the RIGHT protocol was found.
if (expectedProtocolPrefix) {
if (parsedConnectionStr.protocol !== expectedProtocolPrefix+':') {
if (parsedConnectionStr.protocol !== expectedProtocolPrefix+':' && parsedConnectionStr.protocol !== expectedProtocolPrefix+'+srv:') {
throw flaverr('E_BAD_CONFIG', new Error(
'Provided URL ('+util.inspect(dsConfig.url,{depth:5})+') has an invalid protocol.\n'+
'If included, the protocol must be "'+expectedProtocolPrefix+'://".\n'+
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sails-mongo",
"version": "1.2.1",
"version": "1.3.0",
"description": "Mongo DB adapter for Sails.js/Waterline.",
"main": "./lib",
"scripts": {
Expand Down Expand Up @@ -34,7 +34,7 @@
"async": "3.2.0",
"flaverr": "^1.10.0",
"machine": "^15.2.2",
"mongodb": "2.2.25",
"mongodb": "3.5.9",
"qs": "6.9.4"
},
"devDependencies": {
Expand Down
2 changes: 2 additions & 0 deletions test/connectable/create-manager.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var assert = require('assert');
var createManager = require('machine').build(require('../../').createManager);
var MongoClient = require('mongodb').MongoClient;

describe('Connectable ::', function() {
describe('Create Manager', function() {
Expand Down Expand Up @@ -43,6 +44,7 @@ describe('Connectable ::', function() {

try {
assert(report.manager);
assert(report.manager.client instanceof MongoClient );
} catch (e) { return done(e); }

return done();
Expand Down
Loading