替代Sails Mongo Adapter支持MongoDB 5.0和6.0?

iezvtpos  于 2023-06-05  发布在  Go
关注(0)|答案(1)|浏览(105)

任何替代sails-mongo适配器?根据Sails-Mongo兼容性,sails-mongo仅支持MongoDB 4.2。
我目前面临的问题如下:

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

我的package.json版本是

"sails-mongo":"^1.2.0",
"mongodb":"^5.2.0",
"connect-mongo":"^5.0.0",
"engines":{
  "node":"14.x",
}

下面是运行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
gwbalxhn

gwbalxhn1#

在sails.js上,你需要使用不同的库来支持较新版本的mongodb驱动程序。有一个关于它的问题。因为sails-mongo没有更新,所以不能在mongodb 6.0中使用。
我用完整的解决方案创建了一个repo:https://github.com/Goostavo/sails-mongodb-6
我正在使用以下包和文件。
我还在Atlas上使用了标准主机mongodb和无服务器mongodb集群的配置。在ATLAS上运行时,我需要一些额外的配置,在数据集上我使用config/env/文件夹,对于会话,我做了一个疯狂的if/else循环来创建正确的连接字符串。
配置设置在.env文件上或使用ENVIRONMENT VARIABLES

"connect-mongodb-session": "^3.1.1",
    "sails-mongo-cloud": "^3.0.1",

文件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
  },

};

session.js文件:

/**
 * 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);
  // },
};

相关问题