Code Monkey home page Code Monkey logo

mongo-q's Introduction

mongo-q

DEPRECATED ES6 Promise is supported by official mongodb node.js driver.

kriskowal's Q support for mongodb.

for mongoose, see mongoose-q.

WARNING!!! recent mongodb driver(>=1.9.20) is NOT fully supported. see issue #1

usage

  • to apply Q with default suffix 'Q':
var mongodb = require('mongo-q')(require('mongodb'));
// verbose way: mongoQ is unused
var mongodb = require('mongodb'),
    mongoQ = require('mongo-q')(mongodb)
// shortest way: mongodb will be loaded by mongo-q
var mongodb = require('mongo-q')();
  • use Q-applied mongodb client library:
mongodb.MongoClient.connectQ(....)
  .then(function (db) {
    return db.collectionQ('test');
  })
  .then(function (coll) {
    return coll.findQ();
  })
  .then(function (cursor) {
    return cursor.toArrayQ();
    // workaround for mongodb >= 1.9.20
    //return Q.nfcall(cursor.toArray);
  })
  .then(function (result) {
    ...
  })
  .fail(function (err) { ... })
  .done();
  • you can mix them:
mongodb.MongoClient.connectQ(....)
  .then(function (db) {
    return db.collectionQ('test');
  })
  .then(function (coll) {
    return coll.find().toArrayQ(); // <----- HERE!
    // workaround for mongodb >= 1.9.20
    //return Q.nfcall(coll.find().toArray);
  })
  .then(function (result) {
    ...
  })
  .fail(function (err) { ... })
  .done();
  • to apply Q with custom suffix/prefix:
var mongodb = require('mongodb-q')(require('mongodb'), {prefix:'promiseOf_', suffix:'_withQ'});
mongodb.MongoClient.promiseOf_connect_withQ(...)
  .then(function (db) {
    return db.promiseOf_collection_withQ(...)
  })
  .then(function (coll) {
    return coll.promiseOf_find_withQ();
  })
  .then(function (cursor) {
    return cursor.promiseOf_toArray_withQ();
  })
  .then(function (result) {
    ...
  })
  .fail(function (err) { ... })
  .done();
  • to apply Q with custom name mapper:
function customMapper(name) {
  return 'q' + name.charAt(0).toUpperCase() + name.substring(1);
}
var mongodb = require('mongodb-q')(require('mongodb'), {mapper:customMapper});
mongodb.MongoClient.qConnect(...)
  .then(function (db) { ... 
    return db.qCollection('test');
  })
  .then(function (coll) {
    return coll.qFind();
  })
  .then(function (cursor) {
    return cursor.toArray();
  })
  .then(function (result) {
    ...
  })
  .fail(function (err) { ... })
  .done();
  • to apply Q with spread:
var mongodb = require('mongo-q')(require('mongodb'), {spread:true});
...

NOTE: at this time, mongodb client library is well formed enough. 'spread' option is needless.

That's all folks!

mongo-q's People

Contributors

ahizzle avatar iolo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

mongo-q's Issues

TypeError: Object #<Object> has no method 'toArrayQ'

I'm trying out mongo-q in my REST API. Here's the original non-promise code I'm converting from which works fine:

mongo.Db.connect(mongoUri, function (err, db) {
db.collection('checkboxes', function(err, collection) {
collection.find().toArray(function(err, items) {
res.send(items);
});
});
});

The code with promises looks like this:

mongo.Db.connectQ(mongoUri)
.then(function(db){
return db.collectionQ('checkboxes');
})
.then(function(collection){
return collection.find().toArrayQ();
})
.then(function(items){
res.send(items);
})
.fail(function(err){
console.log('Error: ', err);
})
.done();

Am I doing something wrong?

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.