Skip to content

Congo is a thin wrapper for node-mongodb-native that makes connections and collections a little easier

Notifications You must be signed in to change notification settings

eval-usertoken/congo

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

congo

congo is a thin wrapper for node-mongodb-native that makes connections and collections a little easier.

Installation

npm

npm install congo

GitHub

npm install https://github.com/martinrue/congo/tarball/master

Configuration

The first thing you must do is call configure to tell congo where the database is and various other options about how the database connection should be made.

Simple

var database = require('congo');

database.configure('localhost', 'mydb');

Full

var database = require('congo');

var config = {
  host: 'localhost',
  name: 'mydb',
  port: 27017,
  reconnect: true,
  pool: 10
};

database.configure(config);

Querying

Get Database

After the call to configure, call get to retrieve a database object.

database.get(function(err, db) {
  // db is a connected database object
});

Collections

All non-system collections are attached directly onto the database object for you, so querying a collection is as simple as:

database.get(function(err, db) {
  db.users.findOne({}, function(err, user) {
    
  });
});

For convenience, congo also attaches a new findAll function to each collection object. This allows you to avoid having to deal with the cursor if you simply want to retrieve all results from a multi-result query:

database.get(function(err, db) {
  db.users.findAll({}, function(err, users) {
    // users is an array of all users from the database
  });
});

New Collections

Because congo queries all existing collections and attaches them to the database object, any new collections you intend to create won't have a corresponding db.[collection]. For this reason, you can supply an array of collections in the config that are always mapped onto the database object:

var database = require('congo');

var config = {
  host: 'localhost',
  name: 'mydb',
  port: 27017,
  reconnect: true,
  pool: 10,
  collections: ['users', 'products', 'orders']
};

database.configure(config);

database.get(function(err, db) {
  // db.users, db.products and db.orders will all be available, 
  // irrespective of whether they existed in the database or not
});

Connections

One pooled database connection is created internally and congo reuses it. This means you can call get anywhere and not worry about creating unnecessary additional connections.

If reconnect: true is set on the configuration object, the mongo driver will reconnect automatically and this also queues commands and replays them once connections are reestablished.

If reconnect: false is set (the default), congo itself reconnects closed connections on each get call and does not queue any failed commands.

Events

Database events such as 'close' and 'error' are propogated. You can listen to these by passing a callback to the on function:

database.on('close', function() {
  // database connection was closed
});

database.on('error', function() {
  // error occurred on the database
});

ObjectID, DBRef, BSON, etc. Functions

The ObjectID et al. functions that you would normally be able to access from require('mongo').[func] have all been extended onto congo and can be used in exactly the same way:

var database = require('congo');

database.get(function(err, db) {
  db.users.findOne({ _id: database.ObjectID('...') }, function(err, user) {

  });
});

About

Congo is a thin wrapper for node-mongodb-native that makes connections and collections a little easier

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%