문제

In order to create a new GridFS instance, a database should be provided

var grid = new Grid(db, 'fs');

How can I get the database without reconnecting (thinking of something like sails.db or sails.adapter-mongo.db) ?

도움이 되었습니까?

해결책

I'm new to sailsjs but I had similar problem with mongodb so, I started to dig into sails source code and found this. (please correct me if I'm wrong)

Each sails model you create(generate) is bound with connection object. This defines adapter that whatever you config in adapter config file.

Referring to sails doc "custom adapter"

Custom-adapters must have properties pointing db instance.

In summary, you can access it through

YourModelName.connections.dbNameFromConfigfile

e.g.

adapter.js (0.9) or connection.js (0.10.x)

module.exports.connections = {


  someMongodbServer: {
    adapter: 'sails-mongo',
    host: 'localhost',
    port: 27017,
    // user: 'username',
    // password: 'password',
    database: 'fileDb'
  },



};

File.js (Model)

module.exports = {

}

FileController.js (Controller)

module.exports = {
  upload: function(req, res){
    var db = File.connections.someMongodbServer;  // name at config file


     your code here....

  }

}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top