Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

abstract-nosql

Package Overview
Dependencies
Maintainers
1
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

abstract-nosql - npm Package Compare versions

Comparing version 1.3.0 to 1.4.0

abstract-nosql.js

160

abstract-chained-batch.js

@@ -1,80 +0,104 @@

/* Copyright (c) 2013 Rod Vagg, MIT License */
// Generated by CoffeeScript 1.8.0
(function() {
var AbstractChainedBatch, Errors, InvalidArgumentError, setImmediate;
function AbstractChainedBatch (db) {
this._db = db
this._operations = []
this._written = false
}
setImmediate = global.setImmediate || process.nextTick;
AbstractChainedBatch.prototype._checkWritten = function () {
if (this._written)
throw new Error('write() already called on this batch')
}
Errors = require("./abstract-error");
AbstractChainedBatch.prototype.put = function (key, value) {
this._checkWritten()
InvalidArgumentError = Errors.InvalidArgumentError;
var err = this._db._checkKey(key, 'key', this._db._isBuffer)
if (err)
throw err
module.exports = AbstractChainedBatch = (function() {
function AbstractChainedBatch(db) {
this._db = db;
this._operations = [];
this._written = false;
}
if (!this._db._isBuffer(key)) key = String(key)
if (!this._db._isBuffer(value)) value = String(value)
AbstractChainedBatch.prototype._checkWritten = function() {
if (this._written) {
throw new Error("write() already called on this batch");
}
};
if (typeof this._put == 'function' )
this._put(key, value)
else
this._operations.push({ type: 'put', key: key, value: value })
AbstractChainedBatch.prototype.put = function(key, value) {
var err;
this._checkWritten();
err = this._db._checkKey(key, "key", this._db._isBuffer);
if (err) {
throw err;
}
if (!this._db._isBuffer(key)) {
key = String(key);
}
if (!this._db._isBuffer(value)) {
value = String(value);
}
if (typeof this._put === "function") {
this._put(key, value);
} else {
this._operations.push({
type: "put",
key: key,
value: value
});
}
return this;
};
return this
}
AbstractChainedBatch.prototype.del = function(key) {
var err;
this._checkWritten();
err = this._db._checkKey(key, "key", this._db._isBuffer);
if (err) {
throw err;
}
if (!this._db._isBuffer(key)) {
key = String(key);
}
if (typeof this._del === "function") {
this._del(key);
} else {
this._operations.push({
type: "del",
key: key
});
}
return this;
};
AbstractChainedBatch.prototype.del = function (key) {
this._checkWritten()
AbstractChainedBatch.prototype.clear = function() {
this._checkWritten();
this._operations = [];
if (typeof this._clear === "function") {
this._clear();
}
return this;
};
var err = this._db._checkKey(key, 'key', this._db._isBuffer)
if (err) throw err
AbstractChainedBatch.prototype.write = function(options, callback) {
this._checkWritten();
if (typeof options === "function") {
callback = options;
}
if (typeof callback !== "function") {
throw new InvalidArgumentError("write() requires a callback argument");
}
if (typeof options !== "object") {
options = {};
}
this._written = true;
if (typeof this._write === "function") {
return this._write(callback);
}
if (typeof this._db._batch === "function") {
return this._db._batch(this._operations, options, callback);
}
return setImmediate(callback);
};
if (!this._db._isBuffer(key)) key = String(key)
return AbstractChainedBatch;
if (typeof this._del == 'function' )
this._del(key)
else
this._operations.push({ type: 'del', key: key })
})();
return this
}
AbstractChainedBatch.prototype.clear = function () {
this._checkWritten()
this._operations = []
if (typeof this._clear == 'function' )
this._clear()
return this
}
AbstractChainedBatch.prototype.write = function (options, callback) {
this._checkWritten()
if (typeof options == 'function')
callback = options
if (typeof callback != 'function')
throw new Error('write() requires a callback argument')
if (typeof options != 'object')
options = {}
this._written = true
if (typeof this._write == 'function' )
return this._write(callback)
if (typeof this._db._batch == 'function')
return this._db._batch(this._operations, options, callback)
process.nextTick(callback)
}
module.exports = AbstractChainedBatch
}).call(this);

@@ -1,2 +0,12 @@

module.exports = require('abstract-object/Error')
var Errors = require('abstract-object/Error')
var createError = Errors.createError
var OpenError = createError("CanNotOpen", 51)
var CloseError = createError("CanNotClose", 52)
var AlreadyEndError = createError("AlreadyEnd", 53)
Errors.OpenError = OpenError
Errors.CloseError = CloseError
Errors.AlreadyEndError = AlreadyEndError
module.exports = Errors;

@@ -1,92 +0,129 @@

/* Copyright (c) 2013 Rod Vagg, MIT License */
var NotImplementedError = require('./abstract-error').NotImplementedError
function AbstractIterator (db) {
this.db = db
this._ended = false
this._nexting = false
}
// Generated by CoffeeScript 1.8.0
(function() {
var AbstractError, AbstractIterator, AlreadyEndError, Errors, InvalidArgumentError, NotImplementedError, inherits, util;
AbstractIterator.prototype._next = function(callback) {
var self = this
if (this._nextSync) setImmediate(function(){
try {
var result = self._nextSync()
self._nexting = false
if (result) {
callback(null, result[0], result[1])
} else
callback()
} catch(e) {
self._nexting = false
callback(e)
}
})
else
setImmediate(function () {
self._nexting = false
callback()
})
}
util = require("abstract-object/lib/util");
AbstractIterator.prototype._end = function(callback) {
var self = this
if (this._endSync) setImmediate(function(){
try {
var result = self._endSync()
callback(null, result)
} catch(e) {
callback(e)
}
})
setImmediate(function () {
callback()
})
}
inherits = util.inherits;
AbstractIterator.prototype.nextSync = function () {
if (this._nextSync) {
this._nexting = true
var result = this._nextSync()
this._nexting = false
return result
}
throw new NotImplementedError()
}
Errors = require("./abstract-error");
AbstractIterator.prototype.endSync = function () {
if (this._endSync) return this._endSync()
throw new NotImplementedError()
}
AbstractError = Errors.AbstractError;
AbstractIterator.prototype.next = function (callback) {
var self = this
NotImplementedError = Errors.NotImplementedError;
if (typeof callback != 'function')
throw new Error('next() requires a callback argument')
InvalidArgumentError = Errors.InvalidArgumentError;
if (self._ended)
return callback(new Error('cannot call next() after end()'))
if (self._nexting)
return callback(new Error('cannot call next() before previous next() has completed'))
AlreadyEndError = Errors.AlreadyEndError;
self._nexting = true
return self._next(function () {
self._nexting = false
callback.apply(null, arguments)
})
}
module.exports = AbstractIterator = (function() {
AbstractIterator.AlreadyEndError = AlreadyEndError;
AbstractIterator.prototype.end = function (callback) {
if (typeof callback != 'function')
throw new Error('end() requires a callback argument')
function AbstractIterator(db) {
this.db = db;
this._ended = false;
this._nexting = false;
}
if (this._ended)
return callback(new Error('end() already called on iterator'))
AbstractIterator.prototype._next = function(callback) {
var self;
self = this;
if (this._nextSync) {
return setImmediate(function() {
var e, result;
try {
result = self._nextSync();
self._nexting = false;
if (result) {
return callback(null, result[0], result[1]);
} else {
return callback();
}
} catch (_error) {
e = _error;
self._nexting = false;
return callback(e);
}
});
} else {
return setImmediate(function() {
self._nexting = false;
return callback();
});
}
};
this._ended = true
AbstractIterator.prototype._end = function(callback) {
var self;
self = this;
if (this._endSync) {
return setImmediate(function() {
var e, result;
try {
result = self._endSync();
return callback(null, result);
} catch (_error) {
e = _error;
return callback(e);
}
});
} else {
return setImmediate(function() {
return callback();
});
}
};
return this._end(callback)
}
AbstractIterator.prototype.nextSync = function() {
var result;
if (this._nextSync) {
this._nexting = true;
result = this._nextSync();
this._nexting = false;
return result;
}
throw new NotImplementedError();
};
module.exports = AbstractIterator
AbstractIterator.prototype.endSync = function() {
if (this._endSync) {
return this._endSync();
}
throw new NotImplementedError();
};
AbstractIterator.prototype.next = function(callback) {
var self;
if (typeof callback !== "function") {
throw new InvalidArgumentError("next() requires a callback argument");
}
if (this._ended) {
return callback(new AlreadyEndError("cannot call next() after end()"));
}
if (this._nexting) {
return callback(new AlreadyEndError("cannot call next() before previous next() has completed"));
}
this._nexting = true;
self = this;
return this._next(function() {
self._nexting = false;
return callback.apply(null, arguments);
});
};
AbstractIterator.prototype.end = function(callback) {
if (typeof callback !== "function") {
throw new InvalidArgumentError("end() requires a callback argument");
}
if (this._ended) {
return callback(new AlreadyEndError("end() already called on iterator"));
}
this._ended = true;
return this._end(callback);
};
return AbstractIterator;
})();
}).call(this);

@@ -1,518 +0,1 @@

/* Copyright (c) 2013 Rod Vagg, MIT License */
var xtend = require('xtend')
, Errors = require('./abstract-error')
, AbstractError = Errors.AbstractError
, NotImplementedError = Errors.NotImplementedError
, AbstractIterator = require('./abstract-iterator')
, AbstractChainedBatch = require('./abstract-chained-batch')
, setImmediate = global.setImmediate || process.nextTick
function AbstractNoSQL (location) {
//not all database have the location argument.
if (location && typeof location != 'string')
throw new Error('constructor requires a location string argument')
this.location = location
}
//the optimal low-level sync functions:
AbstractNoSQL.prototype.isExistsSync = function (key, options) {
if (this._isExistsSync) {
var result = this._isExistsSync(key, options)
return result
} else if (this._getSync) try {
this._getSync(key, options)
return true
} catch(err) {
//if (/^NotFound/.test(err.message))
if (AbstractError.isNotFound(err))
return false
else
throw err
}
throw new NotImplementedError()
}
AbstractNoSQL.prototype.getSync = function (key, options) {
if (this._getSync) {
var result = this._getSync(key, options)
return result
}
throw new NotImplementedError()
}
AbstractNoSQL.prototype.putSync = function (key, value, options) {
if (this._putSync) {
var result = this._putSync(key, value, options)
return result
}
throw new NotImplementedError()
}
AbstractNoSQL.prototype.delSync = function (key, options) {
if (this._delSync) {
var result = this._delSync(key, options)
return result
}
throw new NotImplementedError()
}
AbstractNoSQL.prototype.batchSync = function (operations, options) {
if (this._batchSync) {
var result = this._batchSync(operations, options)
return result
}
throw new NotImplementedError()
}
AbstractNoSQL.prototype.approximateSizeSync = function (start, end) {
if (this._approximateSizeSync) {
var result = this._approximateSizeSync(start, end)
return result
}
throw new NotImplementedError()
}
AbstractNoSQL.prototype.openSync = function (options) {
if (this._openSync) {
var result = this._openSync(options)
this._opened = result
return result
}
throw new NotImplementedError()
}
//if successful should return true.
AbstractNoSQL.prototype.closeSync = function () {
if (this._closeSync) {
var result = this._closeSync()
this._opened = !result
return result
}
throw new NotImplementedError()
}
//the async methods simulated by sync methods:
//the derived class can override these methods to implement the real async methods for better performance.
AbstractNoSQL.prototype._open = function (options, callback) {
var that = this
if (this._openSync) setImmediate(function() {
var result
try {
result = that._openSync(options)
} catch (err) {
callback(err)
return
}
callback(null, result)
})
else
setImmediate(callback)
}
AbstractNoSQL.prototype._close = function (callback) {
var that = this
if (this._closeSync) setImmediate(function() {
var result
try {
result = that._closeSync()
} catch (err) {
callback(err)
return
}
callback(null, result)
})
else
setImmediate(callback)
}
AbstractNoSQL.prototype._isExists = function (key, options, callback) {
var that = this
if (this._isExistsSync) setImmediate(function() {
var result
try {
result = that._isExistsSync(key, options)
} catch (err) {
callback(err)
return
}
callback(null, result)
})
else this._get(key, options, function(err, value){
if (err) {
if (AbstractError.isNotFound(err))
callback(null, false)
else
callback(err)
} else
callback(null, true)
})
}
AbstractNoSQL.prototype._get = function (key, options, callback) {
var that = this
if (this._getSync) setImmediate(function() {
var result
try {
result = that._getSync(key, options)
} catch (err) {
callback(err)
return
}
callback(null, result)
})
else
setImmediate(callback)
}
AbstractNoSQL.prototype._put = function (key, value, options, callback) {
var that = this
if (this._putSync) setImmediate(function() {
var result
try {
result = that._putSync(key, value, options)
} catch (err) {
callback(err)
return
}
callback(null, result)
})
else
setImmediate(callback)
}
AbstractNoSQL.prototype._del = function (key, options, callback) {
var that = this
if (this._delSync) setImmediate(function() {
var result
try {
result = that._delSync(key, options)
} catch (err) {
callback(err)
return
}
callback(null, result)
})
else
setImmediate(callback)
}
AbstractNoSQL.prototype._batch = function (array, options, callback) {
var that = this
if (this._batchSync) setImmediate(function() {
var result
try {
result = that._batchSync(array, options)
} catch (err) {
callback(err)
return
}
callback(null, result)
})
else
setImmediate(callback)
}
//TODO: remove from here, not a necessary primitive
AbstractNoSQL.prototype._approximateSize = function (start, end, callback) {
var that = this
if (this._approximateSizeSync) setImmediate(function() {
var result
try {
result = that._approximateSizeSync(start, end)
} catch (err) {
callback(err)
return
}
callback(null, result)
})
else
setImmediate(callback)
}
//slower impl:
/*
AbstractNoSQL.prototype._exec = function (fn, args, callback) {
var that = this
if (fn) setImmediate(function() {
var result
try {
result = fn.apply(that, args)
} catch (err) {
callback(err)
return
}
callback(null, result)
})
setImmediate(callback)
}
AbstractNoSQL.prototype._open = function (options, callback) {
this._exec(this._openSync, [options], callback)
}
*/
AbstractNoSQL.prototype.open = function (options, callback) {
if (typeof options == 'function')
callback = options
if (typeof options != 'object')
options = {}
options.createIfMissing = options.createIfMissing != false
options.errorIfExists = !!options.errorIfExists
if (callback) {
var that = this
this._open(options, function(err, result){
that._opened = !err && (result == null || result === true)
callback(err, result)
})
}
else
return this.openSync(options)
}
AbstractNoSQL.prototype.close = function (callback) {
if (callback) {
if (typeof callback === 'function') {
var that = this
this._close(function(err, result){
that._opened = !(err == null && (result == null || result === true))
callback(err, result)
})
}
else
throw new Error('close() requires callback function argument')
}
else
return this.closeSync()
}
AbstractNoSQL.prototype.isExists = function (key, options, callback) {
if (typeof options == 'function') {
callback = options
options = {}
}
if (!this._isBuffer(key))
key = String(key)
if (callback) {
this._isExists(key, options, callback)
} else {
return this.isExistsSync(key, options)
}
}
AbstractNoSQL.prototype.get = function (key, options, callback) {
var err
if (typeof options == 'function')
callback = options
if (err = this._checkKey(key, 'key', this._isBuffer)) {
if (callback)
return callback(err)
else
throw err
}
if (!this._isBuffer(key))
key = String(key)
if (typeof options != 'object')
options = {}
options.asBuffer = options.asBuffer != false
if (callback) {
this._get(key, options, callback)
} else {
return this.getSync(key, options)
}
}
AbstractNoSQL.prototype.put = function (key, value, options, callback) {
var err
if (typeof options == 'function')
callback = options
if (err = this._checkKey(key, 'key', this._isBuffer)) {
if (callback)
return callback(err)
else
throw err
}
if (!this._isBuffer(key))
key = String(key)
// coerce value to string in node, don't touch it in browser
// (indexeddb can store any JS type)
if (value != null && !this._isBuffer(value) && !process.browser)
value = String(value)
if (typeof options != 'object')
options = {}
if (callback) {
this._put(key, value, options, callback)
} else {
return this.putSync(key, value, options)
}
}
AbstractNoSQL.prototype.del = function (key, options, callback) {
var err
if (typeof options == 'function')
callback = options
if (err = this._checkKey(key, 'key', this._isBuffer)) {
if (callback)
return callback(err)
else
throw err
}
if (!this._isBuffer(key))
key = String(key)
if (typeof options != 'object')
options = {}
if (callback) {
this._del(key, options, callback)
}
else {
return this.delSync(key, options)
}
}
AbstractNoSQL.prototype.batch = function (array, options, callback) {
if (!arguments.length)
return this._chainedBatch()
if (typeof options == 'function')
callback = options
if (typeof array == 'function')
callback = array
if (!Array.isArray(array)) {
var vError = new Error('batch(array) requires an array argument')
if (callback)
return callback(vError)
else
throw vError
}
if (!options || typeof options != 'object')
options = {}
var i = 0
, l = array.length
, e
, err
for (; i < l; i++) {
e = array[i]
if (typeof e != 'object')
continue
if (err = this._checkKey(e.type, 'type', this._isBuffer)) {
if (callback)
return callback(err)
else
throw err
}
if (err = this._checkKey(e.key, 'key', this._isBuffer))
if (callback)
return callback(err)
else
throw err
}
if (callback) {
this._batch(array, options, callback)
} else {
return this.batchSync(array, options)
}
}
//TODO: remove from here, not a necessary primitive
AbstractNoSQL.prototype.approximateSize = function (start, end, callback) {
if ( start == null
|| end == null
|| typeof start == 'function'
|| typeof end == 'function') {
throw new Error('approximateSize() requires valid `start`, `end` and `callback`(for async) arguments')
}
if (!this._isBuffer(start))
start = String(start)
if (!this._isBuffer(end))
end = String(end)
if(callback)
this._approximateSize(start, end, callback)
else
return this.approximateSize(start, end)
}
AbstractNoSQL.prototype._setupIteratorOptions = function (options) {
var self = this
options = xtend(options)
;[ 'start', 'end', 'gt', 'gte', 'lt', 'lte' ].forEach(function (o) {
if (options[o] && self._isBuffer(options[o]) && options[o].length === 0)
delete options[o]
})
options.reverse = !!options.reverse
options.keys = options.keys != false
options.values = options.values != false
options.limit = 'limit' in options ? options.limit : -1
options.keyAsBuffer = options.keyAsBuffer != false
options.valueAsBuffer = options.valueAsBuffer != false
return options
}
//should override this to test sync
AbstractNoSQL.prototype.IteratorClass = AbstractIterator
AbstractNoSQL.prototype.iterator = function (options) {
if (typeof options != 'object')
options = {}
options = this._setupIteratorOptions(options)
if (typeof this._iterator == 'function')
return this._iterator(options)
return new this.IteratorClass(this)
}
AbstractNoSQL.prototype._chainedBatch = function () {
return new AbstractChainedBatch(this)
}
AbstractNoSQL.prototype._isBuffer = function (obj) {
return Buffer.isBuffer(obj)
}
AbstractNoSQL.prototype._checkKey = function (obj, type) {
if (obj === null || obj === undefined)
return new Error(type + ' cannot be `null` or `undefined`')
if (this._isBuffer(obj)) {
if (obj.length === 0)
return new Error(type + ' cannot be an empty Buffer')
} else if (String(obj) === '')
return new Error(type + ' cannot be an empty String')
}
AbstractNoSQL.prototype.isOpen = function() {
return !!this._opened
}
module.exports.AbstractLevelDOWN = AbstractNoSQL
module.exports.AbstractNoSQL = AbstractNoSQL
module.exports.AbstractIterator = AbstractIterator
module.exports.AbstractChainedBatch = AbstractChainedBatch
module.exports = require('./abstract-nosql')

@@ -19,2 +19,19 @@ module.exports.close = function (leveldown, test, testCommon) {

})
test('test database close event', function (t) {
var db = leveldown(testCommon.location())
db.once("closed", function(){
t.notOk(db.isOpen())
t.notOk(db.opened)
t.end()
})
db.open(function (err) {
t.error(err)
t.ok(db.isOpen())
t.ok(db.opened)
db.close(function () {
t.notOk(db.isOpen())
t.notOk(db.opened)
})
})
})
}

@@ -16,4 +16,6 @@ module.exports.setUp = function (test, testCommon) {

t.ok(db.isOpen())
t.ok(db.opened)
db.close(function () {
t.notOk(db.isOpen())
t.notOk(db.opened)
t.end()

@@ -31,4 +33,6 @@ })

t.ok(db.isOpen())
t.ok(db.opened)
db.close(function () {
t.notOk(db.isOpen())
t.notOk(db.opened)
t.end()

@@ -58,2 +62,30 @@ })

})
test('test database open event', function (t) {
var db = leveldown(testCommon.location())
db.once("open", function(){
t.ok(db.isOpen())
t.ok(db.opened)
t.end()
})
db.open(function (err) {
t.error(err)
t.ok(db.isOpen())
t.ok(db.opened)
})
})
test('test database ready event', function (t) {
var db = leveldown(testCommon.location())
db.once("ready", function(){
t.ok(db.isOpen())
t.ok(db.opened)
t.end()
})
db.open(function (err) {
t.error(err)
t.ok(db.isOpen())
t.ok(db.opened)
})
})
}

@@ -60,0 +92,0 @@

{
"name": "abstract-nosql",
"description": "An abstract prototype for nosql database(LevelDOWN API)",
"version": "1.3.0",
"version": "1.4.0",
"contributors": [

@@ -29,2 +29,3 @@ "Riceball LEE <snowyu.lee@gmail.com> (https://github.com/snowyu)",

"nosql",
"database",
"leveldb",

@@ -36,3 +37,3 @@ "leveldown",

"dependencies": {
"abstract-object": "^1.1.0",
"abstract-object": "^1.2.0",
"xtend": "~3.0.0"

@@ -50,4 +51,5 @@ },

"test": "node ./test.js"
,"build": "coffee -o . -c src/*.coffee"
},
"license": "MIT"
}

@@ -37,2 +37,3 @@ # Abstract NoSQL Database [![Build Status](https://secure.travis-ci.org/snowyu/node-abstract-nosql.png)](http://travis-ci.org/snowyu/node-abstract-nosql)

* DB constructor allows no location.
* Add IteratorClass supports.
+ Add synchronous methods supports.

@@ -45,5 +46,11 @@ * Add the synchronous methods support now. You can implement the synchronous methods only.

* it will use the \_get/\_getSync method if no \_isExists or \_isExistsSync implemented
+ the AbstractNoSQL class supports events now.
* open/ready event after the database is opened.
* closed event after the database is closed.
+ isOpen()/opened to test the database whether opened.
## AbstractError Classes
see [abstract-object](https://github.com/snowyu/abstract-object)
### AbstractError

@@ -89,3 +96,14 @@

* InvalidFormatError
* OpenError
* CloseError
* AlreadyEndError
```js
var OpenError = createError("CanNotOpen", 51)
var CloseError = createError("CanNotClose", 52)
var AlreadyEndError = createError("AlreadyEnd", 53)
```
## Example

@@ -92,0 +110,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc