I want to promisify an ODM/ORM. How would you implement promise interface while having other methods such as find()
, insert()
, update()
etc. So you could do
var Users = Collection('users')
Users.find({name: 'joe'})
.then(users => users.update({name: 'jim'))
.then(console.log)
I'm thinking of inheriting from Promise
but how do you customize the then() to return your ORM instance to ensure we're working with the instance with all the queries made in sequence.
Right now I'm using composition but then I have to proxy every method call which is getting ugly.
Eg:
var Promise = require('bluebird')
var Collection = function(Storage, name) {
this.promise = new Promise(function(resolve, reject) {
self.resolve = resolve
self.reject = reject
})
}
Collection.prototype.find = function(query) {
// async query stuff here
Storage.doAsyncQueryStuff(function (err, results) {
err && this.reject(err)
this.resolve(results)
})
}
Collection.prototype.then = function(callback) {
var self = this
this.promise.then(function() {
callback && callback.apply(self, arguments)
})
return this
}
What I'm trying to do is:
var inherits = require('util').inherits
var Promise = require('bluebird')
var Collection = function(Storage, name) {
var self = this
// error here
Promise.call(
this,
function(resolve, reject) {
self.resolve = resolve
self.reject = reject
})
}
inherits(Collection, Promise)
I can't seem to get Promise to be initialized. Or should I be doing this a different way?