Git Product home page Git Product logo

co-mongoose's Introduction

co-mongoose forked from iolo/mongoose-q

kriskowal's Q support for mongoose.

for mongodb native nodejs driver, see mongo-q.

usage

  • to apply Q with default suffix 'Q':
// shortest way: mongoose will be loaded by mongoose-q
var mongoose = require('co-mongoose')();
  • use Q-applied model statics:
SomeModel.findByIdQ(....blahblah...)
  .then(function (result) { ... })
  .fail(function (err) { ... })
  .done();
  • use Q-applied model methods:
var someModel = new SomeModel(...);
someModel.populateQ()
  .then(function (result) { ... })
  .fail(function (err) { ... })
  .done();
  • use Q-applied query methods:
SomeModel.find(...).where(...).skip(...).limit(...).sort(...).populate(...)
  .execQ() // no 'Q' suffix for model statics except for execQ()
  .then(function (result) { ... })
  .fail(function (err) { ... })
  .done();
  • to apply Q with custom suffix/prefix:
var mongoose = require('mongoose-q')(require('mongoose'), {prefix:'promiseOf_', suffix:'_withQ'});
SomeModel.promiseOf_findAndUpdate_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 mongoose = require('mongoose-q')(require('mongoose'), {mapper:customMapper});
SomeModel.qFindAndUpdate(...)
  .then(function (result) { ... })
  .fail(function (err) { ... })
  .done();
  • to apply Q with spread:
var mongoose = require('mongoose-q')(require('mongoose'), {spread:true});
SomeModel.updateQ(...)
  .spread(function (affectedRows, raw) { ... })
  .fail(function (err) { ... })
  .done();
SomeModel.updateQ(...)
  .then(function (result) { var affectedRows = result[0], raw = result[1]; ... })
  .fail(function (err) { ... })
  .done();
...
var model = new SomeModel();
...
model.saveQ()
  .spread(function (savedDoc, affectedRows) { ... })
  .fail(function (err) { ... })
  .done();
...
model.saveQ()
  .then(function (result) { var savedDoc = result[0], affectedRows = result[1]; ... })
  .fail(function (err) { ... })
  .done();

NOTE: without spread option(by default), you can access only the first result with then!!

  • to define custom statics/instance methods using Q
SomeSchema.statics.findByName = function (name) {
  return this.findQ({name: name}); // NOTE: returns Promise object.
};
...
var SomeModel = mongoose.model('Some', SomeSchema);
SomeModel.findByName('foo').then(function(result) {
  console.log(result);
});

NOTE: this is not a feature of mongoose-q

That's all folks!

co-mongoose's People

Contributors

colinphanna avatar gerarde avatar iolo avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  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.