node-mongodb-native: How can I share the db api object of the connection callback through my application

匆匆过客 提交于 2020-01-02 09:11:30

问题


I am currently rolling back from mongoose to node-mongodb-native.

So I am quite new at this topic. However my issue currently is that want to create a database collection on server start which I then can use through the application. Unfortunately I only find examples in the repository where you only can do database actions directly in the callback of the connect function.

docs:

var mongodb = require("mongodb"),
    mongoServer = new mongodb.Server('localhost', 27017),
    dbConnector = new mongodb.Db('example', mongoServer);

db_connector.open(function(err, db) {
    if (err) throw new Error(err);

    // here I can do my queries etc.
});

But how can I get access to the db object in the callback when I am in some route callback? Currently the only idea I would have is wrapping the application into the callback:

var mongodb = require("mongodb"),
    express = require("express"),
    mongoServer = new mongodb.Server('localhost', 27017),
    dbConnector = new mongodb.Db('example', mongoServer);

var app = new express();

db_connector.open(function(err, db) {
    if (err) throw new Error(err);

    app.get('/products', function(req, res, next) {
        db.collection('products', function(err, collection) {
            if (err) next(new Error(err));
            collection.find({}, function(err, products) {
                res.send(products);
            });
        });
    });

});

But I do not think this is the way it should meant to be?

Isn't there the way to create a sync database connection call which I then can easily use through the whole application how it was by mongoose?

Regards bodo


回答1:


Db.open opens the connection to mongodb and returns a reference to itself. See here for the sourcecode: https://github.com/mongodb/node-mongodb-native/blob/master/lib/mongodb/db.js#L245

All you want is to hold off on starting your express app listening on it's port and receiving requests until your connection to mongodb has been established.

So what you could do is this:

var mongodb = require("mongodb"),
    express = require("express"),
    mongoServer = new mongodb.Server('localhost', 27017),
    dbConnector = new mongodb.Db('example', mongoServer),
    db;

var app = new express();

app.get('/products', function(req, res, next) {
  db.collection('products', function(err, collection) {
    if (err) next(new Error(err));
    collection.find({}, function(err, products) {
      res.send(products);
    });
  });
});

db_connector.open(function(err, opendb) {
  if (err) throw new Error(err);
  db = opendb;
  app.listen(3000);
});

What I'm not sure about though is whether this is a good idea. This solution doesn't allow you to recreate your connection if there has been a connection break or you restarted your mongodb process. So while the above might work, it might be a better idea to create a method that will wrap the creation of a connection to mongodb.



来源:https://stackoverflow.com/questions/13863675/node-mongodb-native-how-can-i-share-the-db-api-object-of-the-connection-callbac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!