Skip to content

Commit

Permalink
Revert "Temporarily revert merged PR balderdashy#483"
Browse files Browse the repository at this point in the history
This reverts commit 384758b.
  • Loading branch information
rachaelshaw committed Nov 18, 2020
1 parent 4d0d2bb commit 6f02ce2
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 164 deletions.
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)

![[email protected]](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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
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

0 comments on commit 6f02ce2

Please sign in to comment.