Code Monkey home page Code Monkey logo

mongoomise's Introduction

mongoomise

promisify mongoose by any promise library

Support List

Default Usage

var mongoose = require('mongoose')

// load your models first
var User = mongoose.mode('User', UserSchema)
//...


// choose your fav library
require('mongoomise').promisifyAll(mongoose, require('bluebird'))
// require('mongoomise').promisifyAll(mongoose, require('q'))
// require('mongoomise').promisifyAll(mongoose, require('rsvp'))
// require('mongoomise').promisifyAll(mongoose, require('when'))
// require('mongoomise').promisifyAll(mongoose, require('es6-promise'))

// start flying
User.findOneAsync().then(function(user){
	user.pv += 1
	return user.saveAsync()
}).then(function(results){
	console.log(results)
})

Multipe Connections

var mongoose = require('mongoose')
var mongoomise = require('mongoomise')
var connection = mongoose.createConnection('..')

//load models
var User = connection.model('User', UserSchema)
var UserX = mongoose.model('User', UserSchema)
//...

//just promisify the connection, the mongoose will be auto promisified
mongoomise.promisifyAll(connection, require('bluebird'))

// start flying
User.findOneAsync().then(function(user){
	user.pv += 1
	return user.saveAsync()
}).then(function(results){
	console.log(results)
	UserX.findOneAsync()
	// ...
})

Updated

  • support promisify connection from mongoose.createConnection
  • add a test for mongoose.createConnection
  • support promisify custom Schema static methods

TODO

  • support custom instance methods

Notes

  • Do I have to change my existing mongoose related code? No, just follow your old style.
  • Does hooks like Schema.pre work as usual? Yes. some useful discussion here
  • Does it support custom model static method? Yes!
  • mongoomise.promisifyAll should be invoked after all models are loaded

mongoose basics

  • your models extends from mongoose.Model
  • schemas are stored on global mongoose but models may not
  • mongoose.models.ModelName equals to mongoose.model('ModelName')
  • ModelName.schema equals to mongoose.modelSchemas.ModelName
  • static methods should be extended on mongoose.Model with a dynamic context
  • instance methods should be extended on mongoose.Model.prototype
  • custom model static methods are stored in MyModel.schema.statics
  • connection instance store its own models
  • it will lookup to mongoose when no models found on connection

Test

mocha ./test/mocha

Benchmark

node benchmark

the benchmark contains:

  • bluebird - using bluebird.promisifyAll(mongoose)
  • mongoomise/bluebird - using mongoomise with bluebird
  • mongoomise/Q - using mongoomise with Q
  • mongoomise/RSVP - using mongoomise with RSVP
  • mongoomise/when - using mongoomise with when.js
  • mongoomise/es6Promise - using mongoomise with es6-promise

in my MacAir, mongoomise/bluebird win the championship.

mongoomise's People

Contributors

hustkiwi avatar inhuman-cpc avatar omegascorp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

mongoomise's Issues

mongoomise do not work with "mongoose.createConnection"

hi simongfxu, thank you for your work firstly.
i try to use mongoomise with mongoose.createConnection, but it failed.
the step is as follow:

1, create mongoose connection first

this.connection = mongoose.createConnection(mongo.mongodb, mongo.options);

2, read schemas and create model and require mongoomise

        fs.readdirAsync(self.schemasDir)
        .then(function (files) {
            for (var i in files) {
                var file = files[i];
                if (path.extname(file) === '.js') {
                    var modelName = path.basename(file.replace(path.extname(file), ''));
                    var propName = modelName.charAt(0).toUpperCase() + modelName.slice(1);
                    if (propName.indexOf('_') !== -1) {
                        propName = propName.replace(/_(.)/g, function (match, letter) {
                          return letter.toUpperCase();
                        });
                    }

                    var schema = require(path.join(self.schemasDir, file));
                    self[propName] = self.connection.model(propName, schema);
                }
            }
            require('mongoomise').promisifyAll(mongoose, require('bluebird'));
        }).error(function (error) {
            throw error;
        });

3, then the failure info is output as follow:

Possibly unhandled Error: promisification should be done after all of your models are loaded
    at Object.exports.promisifyAll (/home/jakey/workspace/corrida.server/game-server/node_modules/mongoomise/src/main.js:37:9)

Aggregators

Hey there,

Seems the aggregators have been missed.

Model
  .aggregator()
  .sort('something')
  .grout('bySomethingElse')
  .execAsync() // undefined function

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.