abstract-nosql
Version:
An abstract prototype for nosql database with sync and streamable (LevelDOWN API)
93 lines (81 loc) • 2.5 kB
text/coffeescript
# Copyright (c) 2013 Rod Vagg, MIT License
setImmediate = global.setImmediate or process.nextTick
Errors = require("./abstract-error")
InvalidArgumentError = Errors.InvalidArgumentError
module.exports = class AbstractChainedBatch
constructor: (db) ->
= db
= []
= false
_checkWritten: ->
throw new Error("write() already called on this batch") if
put: (key, value) ->
err = ._checkKey(key, "key", ._isBuffer)
throw err if err
key = String(key) unless ._isBuffer(key)
value = String(value) unless ._isBuffer(value)
if typeof is "function"
key, value
else
.push
type: "put"
key: key
value: value
this
del: (key) ->
err = ._checkKey(key, "key", ._isBuffer)
throw err if err
key = String(key) unless ._isBuffer(key)
if typeof is "function"
key
else
.push
type: "del"
key: key
this
clear: ->
= []
if typeof is "function"
this
_write: (options, callback) ->
that = this
if
setImmediate ->
result = undefined
try
result = that._writeSync(options)
catch err
callback err
return
callback null, result
else if typeof ._batch is "function"
._batch( , options, callback)
else
setImmediate callback
writeSync: (options) ->
= true
if
result =
else if typeof ._batchSync is "function"
result = ._batchSync( , options)
result
writeAsync: (options, callback) ->
callback = options if typeof options is "function"
options = {} unless typeof options is "object"
unless typeof callback is "function"
throw new InvalidArgumentError("write() requires a callback argument")
= true
return if typeof is "function"
setImmediate callback
write: (options, callback) ->
callback = options if typeof options is "function"
options = {} unless typeof options is "object"
if typeof callback is "function"
else
options