Skip to content

Instantly share code, notes, and snippets.

@muddydixon
Last active August 29, 2015 14:01
Show Gist options
  • Save muddydixon/55c003b9683b2c228d6d to your computer and use it in GitHub Desktop.
Save muddydixon/55c003b9683b2c228d6d to your computer and use it in GitHub Desktop.
各dbのdeferred wrapper

deferred化しました

使い方は次のようになります

dLevel = require "dLevel"
db = dLevel("./mydb")
db.set("name", "dLevel")
.then(()->
  db.get("name")
)
.then((val)->
  console.log(val)
  db.end()
)
"use strict"
deferred = require "deferred"
Level = require "level"
module.exports = dLevel = (dbname)->
dLevel = {}
db = Level dbname
for k, v of db
continue unless typeof v is 'function'
do (k, v)->
dLevel[k] = (args...)->
d = deferred()
args.push (err, ret...)->
return d.reject err if err
d.resolve.apply d, ret
db[k].apply db, args
d.promise
dLevel
"use strict"
deferred = require "deferred"
MySql = require "mysql"
dMySql = (config = {})->
mysql = MySql.createConnection(config)
dMySql = {}
for k, v of mysql
continue unless typeof v is 'function'
do (k, v)->
dMySql[k] = (args...)->
d = deferred()
args.push (err, ret...)->
return d.reject err if err
d.resolve.apply d, ret
mysql[k].apply mysql, args
d.promise
# end does not require callback
dMySql.end = ()->
d = deferred()
d.resolve mysql.end()
d.promise
dMySql
"use strict"
deferred = require "deferred"
Redis = require "redis"
dRedis = (port = 6379, host = "localhost", options)->
redis = Redis.createClient(port, host, options)
dRedis = {}
for k, v of redis
continue unless typeof v is 'function'
do (k, v)->
dRedis[k] = (args...)->
d = deferred()
args.push (err, ret...)->
return d.reject err if err
d.resolve.apply d, ret
redis[k].apply redis, args
d.promise
# end does not require callback
dRedis.end = ()->
d = deferred()
d.resolve redis.end()
d.promise
dRedis
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment