Giter VIP home page Giter VIP logo

Comments (3)

alFReD-NSH avatar alFReD-NSH commented on July 22, 2024

How do we handle multiple connections?

from monk.

bodokaiser avatar bodokaiser commented on July 22, 2024

over some mongodb connection pool or you just take the returned db instance and handle it your self

from monk.

scurker avatar scurker commented on July 22, 2024

Monk is so close to being able to do this out of the box. While not perfect, I've been able to wrap monk in order to cache the connection.

var monk = require('monk');

function MongoConnector() {
  this.collections = {};
  this._queue = [];
  this.on('open', function (db) {
    this._queue.forEach(function (cb) {
      cb(db);
    });
  });
}

MongoConnector.prototype = Object.create(monk.prototype);
MongoConnector.prototype.executeWhenOpened = function() {
  switch (this._state) {
    case 'open':
      return Promise.resolve(this._db);
    case 'opening':
    case 'closed':
    default:
      return new Promise(resolve => {
        this._queue.push(resolve);
      });
  }
};

var connector = module.exports = new MongoConnector;

module.exports.connect = (uri, opts) => {
  return new Promise(resolve => {
    connector.open(uri, opts, resolve);
  });
};

module.exports.disconnect = (force) => {
  return new Promise(resolve => {
    connector.close(force, resolve);
  });
};

Now, I'm able to initialize the connection by requiring my file:

var connector = require('./path/to/connector/');
connector.connect('mongodb://localhost/mydb');

And of course, in some other file:

var connector = require('./path/to/connector/');
var collection = connector.get('mycollection');
collection.find({});

I don't think it would be difficult to implement this in a pull request, the only issue would be making the uri optional for monk.

from monk.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.