New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

mongodb-async

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongodb-async

Thin & clean async wrapper for mongodb

  • 0.1.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

Quick start

Installation

npm install mongodb-async

Usage

Syntax

Basically you can write any mongodb operation in this way:

myCollection
  .cmd(arg1, arg2, ...)
  .and(callback1)
  .and(callback2, callback3, callback4)
  .fail(errorHandler)
  .done(finishHandler);

Where cmd could be:

open, insert, save, update, remove, rename, findEach, find, findOne, findAndModify, count, distinct, createIndex, ensureIndex, getIndexes, drop, dropIndex, dropIndexes, mapReduce

Example

Let's see some real example

Require mongodb-async, there is only one entry point connect

var connect = require('mongodb-async').connect;

Connect to a server choose your db and collection

var server = connect('127.0.0.1', 27017);
var db = server.db('test_mongodb_async');
var testCollection = db.collection('test');

Yes, you can write it in this way:

var testCollection = connect('127.0.0.1', 27017).db('test_mongodb_async').collection('test');

Make some data

var doc = {x: (new Date).getTime() + ''};

Let's insert it:

var deferred = testCollection.insert(doc, {safe: true});

the onDocSaved function will be called when doc saved successfully

deferred
  .and(function onDocSaved(defer, result) {
    console.log('document saved');
    console.log(result);
    return true;
    // `return true` is equal to `defer.next(result)`, see below
  });

There're 2 ways you can make the deferred callbacks chain going to next step

  1. just return true, and all arguments received in current step would be passed to next step without modification.

  2. call defer.next({x: 'modified'}) if you want to modify data or your operation is async.

If error happens just call defer.error(yourErrorObj).

Find the inserted document:

deferred
  .and(function findDoc(outerDefer, result) {

    // Since you `return true` in previous step, the `result` passed to you here untouched.

    testCollection
      .findOne({x: doc.x})
      .and(function(innerDefer, docFound) {
        console.log('found: %s', docFound.x === result[0].x);
      })
      .fail(outerDefer.error);
      
  });
  

If you have another async operation inside, you can use .fail(outerDefer.error) to route the error to outerDefer's error handling function. Thus you can handle error in just one place

Register error handling function like this

deferred.fail(function dumpFailure(err) {
  console.error(err.stack || err);
});

Here is the above example without comments:

var connect = require('mongodb-async').connect;
var testCollection = connect('127.0.0.1', 27017).db('test_mongodb_async').collection('test');

var doc = {x: (new Date).getTime() + ''};

var deferred = testCollection.insert(doc, {safe: true})
  .and(function onDocSaved(defer, result) {
    console.log('document saved');
    console.log(result);
    return true;
  })
  .and(function findDoc(outerDefer, result) {
    testCollection
      .findOne({x: doc.x})
      .and(function(innerDefer, docFound) {
        console.log('found: %s', docFound.x === result[0].x);
      })
      .fail(outerDefer.error);
  })
  .fail(function dumpFailure(err) {
    console.error(err.stack || err);
  });

Keywords

FAQs

Package last updated on 17 Dec 2011

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc