abstract-nosql
Advanced tools
Comparing version 1.1.0 to 1.2.0
/* Copyright (c) 2013 Rod Vagg, MIT License */ | ||
var NotImplementedError = require('./abstract-error').NotImplementedError | ||
function AbstractIterator (db) { | ||
@@ -53,3 +54,3 @@ this.db = db | ||
} | ||
throw new Error("NotImplemented") | ||
throw new NotImplementedError() | ||
} | ||
@@ -59,3 +60,3 @@ | ||
if (this._endSync) return this._endSync() | ||
throw new Error("NotImplemented") | ||
throw new NotImplementedError() | ||
} | ||
@@ -62,0 +63,0 @@ |
/* 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') | ||
@@ -7,7 +10,4 @@ , AbstractChainedBatch = require('./abstract-chained-batch') | ||
function isNotFoundError(err) { | ||
return (err.message.substring(0,8) === "NotFound") | ||
} | ||
function AbstractLevelDOWN (location) { | ||
function AbstractNoSQL (location) { | ||
//not all database have the location argument. | ||
@@ -20,3 +20,3 @@ if (location && typeof location != 'string') | ||
//the optimal low-level sync functions: | ||
AbstractLevelDOWN.prototype.isExistsSync = function (key, options) { | ||
AbstractNoSQL.prototype.isExistsSync = function (key, options) { | ||
if (this._isExistsSync) { | ||
@@ -30,3 +30,3 @@ var result = this._isExistsSync(key, options) | ||
//if (/^NotFound/.test(err.message)) | ||
if (isNotFoundError(err)) | ||
if (AbstractError.isNotFound(err)) | ||
return false | ||
@@ -36,6 +36,6 @@ else | ||
} | ||
throw new Error("NotImplemented") | ||
throw new NotImplementedError() | ||
} | ||
AbstractLevelDOWN.prototype.getSync = function (key, options) { | ||
AbstractNoSQL.prototype.getSync = function (key, options) { | ||
if (this._getSync) { | ||
@@ -45,6 +45,6 @@ var result = this._getSync(key, options) | ||
} | ||
throw new Error("NotImplemented") | ||
throw new NotImplementedError() | ||
} | ||
AbstractLevelDOWN.prototype.putSync = function (key, value, options) { | ||
AbstractNoSQL.prototype.putSync = function (key, value, options) { | ||
if (this._putSync) { | ||
@@ -54,6 +54,6 @@ var result = this._putSync(key, value, options) | ||
} | ||
throw new Error("NotImplemented") | ||
throw new NotImplementedError() | ||
} | ||
AbstractLevelDOWN.prototype.delSync = function (key, options) { | ||
AbstractNoSQL.prototype.delSync = function (key, options) { | ||
if (this._delSync) { | ||
@@ -63,6 +63,6 @@ var result = this._delSync(key, options) | ||
} | ||
throw new Error("NotImplemented") | ||
throw new NotImplementedError() | ||
} | ||
AbstractLevelDOWN.prototype.batchSync = function (operations, options) { | ||
AbstractNoSQL.prototype.batchSync = function (operations, options) { | ||
if (this._batchSync) { | ||
@@ -72,6 +72,6 @@ var result = this._batchSync(operations, options) | ||
} | ||
throw new Error("NotImplemented") | ||
throw new NotImplementedError() | ||
} | ||
AbstractLevelDOWN.prototype.approximateSizeSync = function (start, end) { | ||
AbstractNoSQL.prototype.approximateSizeSync = function (start, end) { | ||
if (this._approximateSizeSync) { | ||
@@ -81,6 +81,6 @@ var result = this._approximateSizeSync(start, end) | ||
} | ||
throw new Error("NotImplemented") | ||
throw new NotImplementedError() | ||
} | ||
AbstractLevelDOWN.prototype.openSync = function (options) { | ||
AbstractNoSQL.prototype.openSync = function (options) { | ||
if (this._openSync) { | ||
@@ -90,7 +90,7 @@ var result = this._openSync(options) | ||
} | ||
throw new Error("NotImplemented") | ||
throw new NotImplementedError() | ||
} | ||
//if successful should return true. | ||
AbstractLevelDOWN.prototype.closeSync = function () { | ||
AbstractNoSQL.prototype.closeSync = function () { | ||
if (this._closeSync) { | ||
@@ -100,3 +100,3 @@ var result = this._closeSync() | ||
} | ||
throw new Error("NotImplemented") | ||
throw new NotImplementedError() | ||
} | ||
@@ -106,3 +106,3 @@ | ||
//the derived class can override these methods to implement the real async methods for better performance. | ||
AbstractLevelDOWN.prototype._open = function (options, callback) { | ||
AbstractNoSQL.prototype._open = function (options, callback) { | ||
var that = this | ||
@@ -122,3 +122,3 @@ if (this._openSync) setImmediate(function() { | ||
} | ||
AbstractLevelDOWN.prototype._close = function (callback) { | ||
AbstractNoSQL.prototype._close = function (callback) { | ||
var that = this | ||
@@ -138,3 +138,3 @@ if (this._closeSync) setImmediate(function() { | ||
} | ||
AbstractLevelDOWN.prototype._isExists = function (key, options, callback) { | ||
AbstractNoSQL.prototype._isExists = function (key, options, callback) { | ||
var that = this | ||
@@ -153,3 +153,3 @@ if (this._isExistsSync) setImmediate(function() { | ||
if (err) { | ||
if (isNotFoundError(err)) | ||
if (AbstractError.isNotFound(err)) | ||
callback(null, false) | ||
@@ -162,3 +162,3 @@ else | ||
} | ||
AbstractLevelDOWN.prototype._get = function (key, options, callback) { | ||
AbstractNoSQL.prototype._get = function (key, options, callback) { | ||
var that = this | ||
@@ -178,3 +178,3 @@ if (this._getSync) setImmediate(function() { | ||
} | ||
AbstractLevelDOWN.prototype._put = function (key, value, options, callback) { | ||
AbstractNoSQL.prototype._put = function (key, value, options, callback) { | ||
var that = this | ||
@@ -194,3 +194,3 @@ if (this._putSync) setImmediate(function() { | ||
} | ||
AbstractLevelDOWN.prototype._del = function (key, options, callback) { | ||
AbstractNoSQL.prototype._del = function (key, options, callback) { | ||
var that = this | ||
@@ -210,3 +210,3 @@ if (this._delSync) setImmediate(function() { | ||
} | ||
AbstractLevelDOWN.prototype._batch = function (array, options, callback) { | ||
AbstractNoSQL.prototype._batch = function (array, options, callback) { | ||
var that = this | ||
@@ -227,3 +227,3 @@ if (this._batchSync) setImmediate(function() { | ||
//TODO: remove from here, not a necessary primitive | ||
AbstractLevelDOWN.prototype._approximateSize = function (start, end, callback) { | ||
AbstractNoSQL.prototype._approximateSize = function (start, end, callback) { | ||
var that = this | ||
@@ -245,3 +245,3 @@ if (this._approximateSizeSync) setImmediate(function() { | ||
/* | ||
AbstractLevelDOWN.prototype._exec = function (fn, args, callback) { | ||
AbstractNoSQL.prototype._exec = function (fn, args, callback) { | ||
var that = this | ||
@@ -260,7 +260,7 @@ if (fn) setImmediate(function() { | ||
} | ||
AbstractLevelDOWN.prototype._open = function (options, callback) { | ||
AbstractNoSQL.prototype._open = function (options, callback) { | ||
this._exec(this._openSync, [options], callback) | ||
} | ||
*/ | ||
AbstractLevelDOWN.prototype.open = function (options, callback) { | ||
AbstractNoSQL.prototype.open = function (options, callback) { | ||
if (typeof options == 'function') | ||
@@ -281,3 +281,3 @@ callback = options | ||
AbstractLevelDOWN.prototype.close = function (callback) { | ||
AbstractNoSQL.prototype.close = function (callback) { | ||
if (callback) { | ||
@@ -293,3 +293,3 @@ if (typeof callback === 'function') | ||
AbstractLevelDOWN.prototype.isExists = function (key, options, callback) { | ||
AbstractNoSQL.prototype.isExists = function (key, options, callback) { | ||
if (typeof options == 'function') { | ||
@@ -310,3 +310,3 @@ callback = options | ||
AbstractLevelDOWN.prototype.get = function (key, options, callback) { | ||
AbstractNoSQL.prototype.get = function (key, options, callback) { | ||
var err | ||
@@ -339,3 +339,3 @@ | ||
AbstractLevelDOWN.prototype.put = function (key, value, options, callback) { | ||
AbstractNoSQL.prototype.put = function (key, value, options, callback) { | ||
var err | ||
@@ -371,3 +371,3 @@ | ||
AbstractLevelDOWN.prototype.del = function (key, options, callback) { | ||
AbstractNoSQL.prototype.del = function (key, options, callback) { | ||
var err | ||
@@ -399,3 +399,3 @@ | ||
AbstractLevelDOWN.prototype.batch = function (array, options, callback) { | ||
AbstractNoSQL.prototype.batch = function (array, options, callback) { | ||
if (!arguments.length) | ||
@@ -453,3 +453,3 @@ return this._chainedBatch() | ||
//TODO: remove from here, not a necessary primitive | ||
AbstractLevelDOWN.prototype.approximateSize = function (start, end, callback) { | ||
AbstractNoSQL.prototype.approximateSize = function (start, end, callback) { | ||
if ( start == null | ||
@@ -474,3 +474,3 @@ || end == null | ||
AbstractLevelDOWN.prototype._setupIteratorOptions = function (options) { | ||
AbstractNoSQL.prototype._setupIteratorOptions = function (options) { | ||
var self = this | ||
@@ -496,5 +496,5 @@ | ||
//should override this to test sync | ||
AbstractLevelDOWN.prototype.IteratorClass = AbstractIterator | ||
AbstractNoSQL.prototype.IteratorClass = AbstractIterator | ||
AbstractLevelDOWN.prototype.iterator = function (options) { | ||
AbstractNoSQL.prototype.iterator = function (options) { | ||
if (typeof options != 'object') | ||
@@ -511,11 +511,11 @@ options = {} | ||
AbstractLevelDOWN.prototype._chainedBatch = function () { | ||
AbstractNoSQL.prototype._chainedBatch = function () { | ||
return new AbstractChainedBatch(this) | ||
} | ||
AbstractLevelDOWN.prototype._isBuffer = function (obj) { | ||
AbstractNoSQL.prototype._isBuffer = function (obj) { | ||
return Buffer.isBuffer(obj) | ||
} | ||
AbstractLevelDOWN.prototype._checkKey = function (obj, type) { | ||
AbstractNoSQL.prototype._checkKey = function (obj, type) { | ||
@@ -532,4 +532,5 @@ if (obj === null || obj === undefined) | ||
module.exports.AbstractLevelDOWN = AbstractLevelDOWN | ||
module.exports.AbstractLevelDOWN = AbstractNoSQL | ||
module.exports.AbstractNoSQL = AbstractNoSQL | ||
module.exports.AbstractIterator = AbstractIterator | ||
module.exports.AbstractChainedBatch = AbstractChainedBatch |
{ | ||
"name": "abstract-nosql", | ||
"description": "An abstract prototype for nosql database(LevelDOWN API)", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"contributors": [ | ||
@@ -6,0 +6,0 @@ "Riceball LEE <snowyu.lee@gmail.com> (https://github.com/snowyu)", |
@@ -35,2 +35,3 @@ # Abstract NoSQL Database [![Build Status](https://secure.travis-ci.org/snowyu/node-abstract-nosql.png)](http://travis-ci.org/snowyu/node-abstract-nosql) | ||
+ Add the AbstractError and error code supports. | ||
* DB constructor allows no location. | ||
@@ -45,2 +46,45 @@ + Add synchronous methods supports. | ||
## AbstractError Classes | ||
### AbstractError | ||
All Errors are derived from the AbstractError. | ||
* Members: | ||
* message: the error message. | ||
* code: the error code. | ||
* Methods: | ||
* ok() | ||
* notFound() | ||
* .... | ||
* invalidFormat() | ||
* Class Methods: | ||
* AbstractError.isOk(err) | ||
* AbstractError.isNotFound(err) | ||
* ... | ||
the error codes: | ||
* AbstractError.Ok = 0 | ||
* AbstractError.NotFound = 1 | ||
* AbstractError.Corruption = 2 | ||
* AbstractError.NotSupported = 3 | ||
* AbstractError.InvalidArgument = 4 | ||
* AbstractError.IO = 5 | ||
* AbstractError.NotOpened = 6 | ||
* AbstractError.InvalidType = 7 | ||
* AbstractError.InvalidFormat = 8 | ||
### Other Error Classes: | ||
* NotFoundError | ||
* CorruptionError | ||
* NotSupportedError/NotImplementedError | ||
* InvalidArgumentError | ||
* IOError | ||
* NotOpenedError | ||
* InvalidTypeError | ||
* InvalidFormatError | ||
## Example | ||
@@ -47,0 +91,0 @@ |
@@ -16,2 +16,4 @@ const tap = require('tap') | ||
require('./abstract/error-test').all(factory, tap.test, testCommon) | ||
require('./abstract/leveldown-test').args(factory, tap.test, testCommon) | ||
@@ -18,0 +20,0 @@ |
133190
29
3121
372