GithubHelp home page GithubHelp logo

eval-usertoken / congo Goto Github PK

View Code? Open in Web Editor NEW

This project forked from thomseddon/congo

0.0 2.0 0.0 105 KB

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

JavaScript 100.00%

congo's Introduction

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

  });
});

congo's People

Watchers

 avatar  avatar

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.