Question

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) ?

Was it helpful?

Solution

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....

  }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top