Any alternatives to sails-mongo adapter? According to Sails-Mongo Compatibility, sails-mongo only supports up to MongoDB 4.2.
Current issue I face is the below:
error: Sending 500 ("Server Error") response:
MongoError: Unsupported OP_QUERY command: find. The client driver may require an upgrade. For
more details see https://dochub.mongodb.org/core/legacy-opcode-removal
My package.json versions are
"sails-mongo":"^1.2.0",
"mongodb":"^5.2.0",
"connect-mongo":"^5.0.0",
"engines":{
"node":"14.x",
}
Here's what i get when i run npm list mongodb
├─┬ connect-mongo@5.0.0
│ └── mongodb@5.2.0 deduped
├── mongodb@5.2.0
└─┬ sails-mongo@2.0.1
└── mongodb@3.7.3
On sails.js you need to use different libs to support newer versions of mongodb driver. There is an issue about it. As sails-mongo
is not being updated, and won't work with mongodb 6.0.
I created a repo with the full solution: https://github.com/Goostavo/sails-mongodb-6
I'm using the following packages and files.
I'm also using configs for both standard host mongodb and serverless mongodb cluster on Atlas. When running on ATLAS i need to have some extra configuration, on datasets i use the config/env/
folders, and for sessions i made a crazy if/else loop to make the right connection string.
The configurations are set on a .env
file or using ENVIRONMENT VARIABLES
.
"connect-mongodb-session": "^3.1.1",
"sails-mongo-cloud": "^3.0.1",
File datastores.js
:
/**
* Datastores
* (sails.config.datastores)
*/
let cfg = {
host: 'localhost',
port: 27017,
database: process.env.MONGO_DATABASE || 'localDB'
};
module.exports.datastores = {
default: {
adapter: require('sails-mongo-cloud'),
url: 'mongodb://' + cfg.host + ':' + cfg.port + '/' + cfg.database,
socketTimeoutMS: 360000
},
};
The session.js
file:
/**
* Session Configuration
* (sails.config.session)
*
* Use the settings below to configure session integration in your app.
* (for additional recommended settings, see `config/env/production.js`)
*
* For all available options, see:
* https://sailsjs.com/config/session
*/
const cfg = {
host: process.env.MONGO_HOST || 'localhost',
port: process.env.MONGO_PORT || 27017,
database: process.env.MONGO_DATABASE || 'localDB',
user: process.env.MONGO_USER,
password: process.env.MONGO_PWD,
useatlas: process.env.USE_ATLAS
};
let url;
if (process.env.NODE_ENV === 'production' && cfg.useatlas) {
url = 'mongodb+srv://' + cfg.user + ':' + cfg.password + '@' + cfg.host + '/' + cfg.database + '?retryWrites=true&w=majority';
} else if (process.env.NODE_ENV === 'production'){
url = 'mongodb://' + cfg.user + ':' + cfg.password + '@' + cfg.host + '/' + cfg.database + '?retryWrites=true';
} else {
if (!cfg.user) {
url = 'mongodb://' + cfg.host + '/' + cfg.database + '?retryWrites=true';
} else {
url = 'mongodb://' + cfg.user + ':' + cfg.password + '@' + cfg.host + '/' + cfg.database + '?retryWrites=true';
}
}
module.exports.session = {
/***************************************************************************
* *
* Session secret is automatically generated when your new app is created *
* Replace at your own risk in production-- you will invalidate the cookies *
* of your users, forcing them to log in again. *
* *
***************************************************************************/
secret: 'sails is awesome',
/***************************************************************************
* *
* Customize when built-in session support will be skipped. *
* *
* (Useful for performance tuning; particularly to avoid wasting cycles on *
* session management when responding to simple requests for static assets, *
* like images or stylesheets.) *
* *
* https://sailsjs.com/config/session *
* *
***************************************************************************/
cookie: {
maxAge: 30 * 24 * 60 * 60 * 1000 // 30 days
},
adapter: 'connect-mongodb-session',
'uri': url,
//collection: 'sessions',
// isSessionDisabled: function (req){
// return !!req.path.match(req._sails.LOOKS_LIKE_ASSET_RX);
// },
// isSessionDisabled: function (req){
// return !!req.path.match(req._sails.LOOKS_LIKE_ASSET_RX);
// },
};