abstract-nosql
Advanced tools
Comparing version 1.10.5 to 2.0.0
// Generated by CoffeeScript 1.8.0 | ||
(function() { | ||
var AbstractChainedBatch, AbstractError, AbstractIterator, AbstractNoSQL, AbstractObject, CloseError, Codec, Errors, InvalidArgumentError, NoSqlStream, NotImplementedError, OpenError, ReadStream, WriteStream, inherits, isArray, isFunction, isString, setImmediate, utf8ByteLength, xtend; | ||
var AbstractChainedBatch, AbstractError, AbstractIterator, AbstractNoSQL, AbstractObject, CloseError, Codec, Errors, InvalidArgumentError, NotImplementedError, OpenError, inherits, isArray, isFunction, isString, setImmediate, utf8ByteLength, xtend; | ||
xtend = require("xtend"); | ||
try { | ||
NoSqlStream = require("nosql-stream"); | ||
ReadStream = NoSqlStream.ReadStream; | ||
WriteStream = NoSqlStream.WriteStream; | ||
} catch (_error) {} | ||
AbstractObject = require("abstract-object"); | ||
@@ -755,3 +749,3 @@ | ||
} | ||
options.asBuffer = options.asBuffer !== false; | ||
options.asBuffer = options.asBuffer === true; | ||
return this._get(key, options, callback); | ||
@@ -938,44 +932,2 @@ }; | ||
AbstractNoSQL.prototype.readStream = function(options, makeData) { | ||
var opt; | ||
if (ReadStream) { | ||
opt = xtend(this._options, options); | ||
return ReadStream(this, opt, makeData); | ||
} else { | ||
return console.error("please `npm install nosql-stream` first"); | ||
} | ||
}; | ||
AbstractNoSQL.prototype.createReadStream = AbstractNoSQL.prototype.readStream; | ||
AbstractNoSQL.prototype.valueStream = function(options, makeData) { | ||
var opt; | ||
opt = xtend(options); | ||
opt.keys = false; | ||
return this.readStream(opt, makeData); | ||
}; | ||
AbstractNoSQL.prototype.createValueStream = AbstractNoSQL.prototype.valueStream; | ||
AbstractNoSQL.prototype.keyStream = function(options, makeData) { | ||
var opt; | ||
opt = xtend(options); | ||
opt.values = false; | ||
return this.readStream(opt, makeData); | ||
}; | ||
AbstractNoSQL.prototype.createKeyStream = AbstractNoSQL.prototype.keyStream; | ||
AbstractNoSQL.prototype.writeStream = function(options) { | ||
var opt; | ||
if (WriteStream) { | ||
opt = xtend(this._options, options); | ||
return WriteStream(this, opt); | ||
} else { | ||
return console.error("please `npm install nosql-stream` first"); | ||
} | ||
}; | ||
AbstractNoSQL.prototype.createWriteStream = AbstractNoSQL.prototype.writeStream; | ||
return AbstractNoSQL; | ||
@@ -982,0 +934,0 @@ |
@@ -96,4 +96,5 @@ var db | ||
db.get('foo', function (err, value) { | ||
db.get('foo', function (err, result) { | ||
t.error(err) | ||
/* | ||
var result | ||
@@ -105,3 +106,3 @@ if (isTypedArray(value)) { | ||
result = value.toString() | ||
} | ||
}*/ | ||
t.equal(result, 'bar') | ||
@@ -128,4 +129,5 @@ t.end() | ||
db.get('foobatch1', function (err, value) { | ||
db.get('foobatch1', function (err, result) { | ||
t.error(err) | ||
/* | ||
var result | ||
@@ -137,3 +139,3 @@ if (isTypedArray(value)) { | ||
result = value.toString() | ||
} | ||
}*/ | ||
t.equal(result, 'bar1') | ||
@@ -150,11 +152,4 @@ done() | ||
db.get('foobatch3', function (err, value) { | ||
db.get('foobatch3', function (err, result) { | ||
t.error(err) | ||
var result | ||
if (isTypedArray(value)) { | ||
result = String.fromCharCode.apply(null, new Uint16Array(value)) | ||
} else { | ||
t.ok(typeof Buffer != 'undefined' && value instanceof Buffer) | ||
result = value.toString() | ||
} | ||
t.equal(result, 'bar3') | ||
@@ -161,0 +156,0 @@ done() |
@@ -20,6 +20,7 @@ var db | ||
t.error(err) | ||
db.get('foo', function (err, value) { | ||
db.get('foo', function (err, result) { | ||
t.error(err) | ||
t.ok(typeof value !== 'string', 'should not be string by default') | ||
t.ok(typeof result === 'string', 'should be string by default') | ||
/* | ||
var result | ||
@@ -36,9 +37,11 @@ if (isTypedArray(value)) { | ||
} | ||
*/ | ||
t.equal(result, 'bar') | ||
db.get('foo', {}, function (err, value) { // same but with {} | ||
db.get('foo', {}, function (err, result) { // same but with {} | ||
t.error(err) | ||
t.ok(typeof value !== 'string', 'should not be string by default') | ||
t.ok(typeof result === 'string', 'should be string by default') | ||
/* | ||
var result | ||
@@ -54,3 +57,3 @@ if (isTypedArray(value)) { | ||
} | ||
} | ||
}*/ | ||
@@ -57,0 +60,0 @@ t.equal(result, 'bar') |
@@ -47,3 +47,3 @@ /**** SETUP & UTILITY STUFF ****/ | ||
t.error(err, 'no error, has key/value for `' + type + '`') | ||
t.ok(Buffer.isBuffer(_value), 'is a Buffer') | ||
//t.ok(Buffer.isBuffer(_value), 'is a Buffer') | ||
var result = _value | ||
@@ -50,0 +50,0 @@ if (hasExpectedResult) { |
{ | ||
"name": "abstract-nosql", | ||
"description": "An abstract prototype for nosql database with sync and streamable (LevelDOWN API)", | ||
"version": "1.10.5", | ||
"version": "2.0.0", | ||
"contributors": [ | ||
@@ -43,3 +43,2 @@ "Riceball LEE <snowyu.lee@gmail.com> (https://github.com/snowyu)", | ||
"abstract-iterator": ">=0.10.0", | ||
"nosql-stream": ">=1.2.2", | ||
"rimraf": "*", | ||
@@ -46,0 +45,0 @@ "sinon": "*", |
177
README.md
@@ -28,5 +28,18 @@ # Abstract NoSQL Database [![Build Status](https://secure.travis-ci.org/snowyu/node-abstract-nosql.png?branch=master)](http://travis-ci.org/snowyu/node-abstract-nosql) | ||
* the modularization with abstract-nosql | ||
### v2.x.x | ||
* the modularization(feature plugin) with abstract-nosql | ||
* [nosql-encoding](https://github.com/snowyu/node-nosql-encoding) | ||
* [nosql-stream](https://github.com/snowyu/nosql-stream) | ||
* Let the user decide whether to use these features. | ||
* (`broken changes`) remove the streamable feature from buildin. this is a plugin now. | ||
* (`broken changes`) defaults to disable asBuffer option. | ||
* pls use the `getBuffer` method to get as buffer. | ||
### V1.x.x | ||
+ Can add the encoding key/value ability via two ways: | ||
* see the [nosql-encoding](https://github.com/snowyu/node-nosql-encoding) package. | ||
* see the [encoding-iterator](https://github.com/snowyu/node-encoding-iterator) package. | ||
+ getBuffer/getBufferSync(key, destBuffer, options) optional method. | ||
@@ -39,2 +52,4 @@ * the key's value will be put into the destBuffer if destBuffer is not null. | ||
- Remove the AbstractIterator to [abstract-iterator](https://github.com/snowyu/node-abstract-iterator) package | ||
+ Add the stream ability | ||
* You should install [nosql-stream](https://github.com/snowyu/nosql-stream) package first to use this feature. | ||
+ Add the AbstractError and error code supports. | ||
@@ -124,3 +139,3 @@ * DB constructor allows no location. | ||
## Streamable | ||
## Streamable plugin | ||
@@ -140,160 +155,2 @@ Once implements the [AbstractIterator](https://github.com/snowyu/node-abstract-iterator): | ||
### AbstractNoSql.keyStream(createKeyStream) | ||
create a readable stream. | ||
the data item is key. | ||
### AbstractNoSql.valueStream(createValueStream) | ||
create a readable stream. | ||
the data item is value. | ||
### AbstractNoSql.readStream(createReadStream) | ||
create a readable stream. | ||
the data item is an object: {key:key, value:value}. | ||
* AbstractNoSql.readStream([options]) | ||
* AbstractNoSql.createReadStream | ||
__arguments__ | ||
* options: the optional options object(note: some options depend on the implementation of the Iterator) | ||
* `'next'`: the raw key data to ensure the readStream return keys is greater than the key. See `'last'` event. | ||
* note: this will affect the range[gt/gte or lt/lte(reverse)] options. | ||
* `'filter'` *(function)*: to filter data in the stream | ||
* function filter(key, value) if return: | ||
* 0(consts.FILTER_INCLUDED): include this item(default) | ||
* 1(consts.FILTER_EXCLUDED): exclude this item. | ||
* -1(consts.FILTER_STOPPED): stop stream. | ||
* note: the filter function argument 'key' and 'value' may be null, it is affected via keys and values of this options. | ||
* `'range'` *(string or array)*: the keys are in the give range as the following format: | ||
* string: | ||
* "[a, b]": from a to b. a,b included. this means {gte='a', lte = 'b'} | ||
* "(a, b]": from a to b. b included, a excluded. this means {gt='a', lte='b'} | ||
* "[, b)" from begining to b, begining included, b excluded. this means {lt='b'} | ||
* note: this will affect the gt/gte/lt/lte options. | ||
* array: the key list to get. eg, ['a', 'b', 'c'] | ||
* `'gt'` (greater than), `'gte'` (greater than or equal) define the lower bound of the range to be streamed. Only records where the key is greater than (or equal to) this option will be included in the range. When `reverse=true` the order will be reversed, but the records streamed will be the same. | ||
* `'lt'` (less than), `'lte'` (less than or equal) define the higher bound of the range to be streamed. Only key/value pairs where the key is less than (or equal to) this option will be included in the range. When `reverse=true` the order will be reversed, but the records streamed will be the same. | ||
* `'start', 'end'` legacy ranges - instead use `'gte', 'lte'` | ||
* `'match'` *(string)*: use the minmatch to match the specified keys. | ||
* Note: It will affect the range[gt/gte or lt/lte(reverse)] options maybe. | ||
* `'limit'` *(number, default: `-1`)*: limit the number of results collected by this stream. This number represents a *maximum* number of results and may not be reached if you get to the end of the data first. A value of `-1` means there is no limit. When `reverse=true` the highest keys will be returned instead of the lowest keys. | ||
* `'reverse'` *(boolean, default: `false`)*: a boolean, set true and the stream output will be reversed. | ||
* `'keys'` *(boolean, default: `true`)*: whether the `'data'` event should contain keys. If set to `true` and `'values'` set to `false` then `'data'` events will simply be keys, rather than objects with a `'key'` property. Used internally by the `createKeyStream()` method. | ||
* `'values'` *(boolean, default: `true`)*: whether the `'data'` event should contain values. If set to `true` and `'keys'` set to `false` then `'data'` events will simply be values, rather than objects with a `'value'` property. Used internally by the `createValueStream()` method. | ||
__return__ | ||
* object: the read stream object | ||
#### Events | ||
the standard `'data'`, '`error'`, `'end'` and `'close'` events are emitted. | ||
the `'last'` event will be emitted when the last data arrived, the argument is the last raw key. | ||
if no more data the last key is `undefined`. | ||
```js | ||
var MemDB = require("memdown-sync") | ||
var db1 = MemDB("db1") | ||
var db2 = MemDB("db2") | ||
var ws = db1.writeStream() | ||
var ws2 = db2.createWriteStream() | ||
ws.on('error', function (err) { | ||
console.log('Oh my!', err) | ||
}) | ||
ws.on('finish', function () { | ||
console.log('Write Stream finish') | ||
//read all data through the ReadStream | ||
db1.readStream().on('data', function (data) { | ||
console.log(data.key, '=', data.value) | ||
}) | ||
.on('error', function (err) { | ||
console.log('Oh my!', err) | ||
}) | ||
.on('close', function () { | ||
console.log('Stream closed') | ||
}) | ||
.on('end', function () { | ||
console.log('Stream closed') | ||
}) | ||
.pipe(ws2) //copy Database db1 to db2: | ||
}) | ||
ws.write({ key: 'name', value: 'Yuri Irsenovich Kim' }) | ||
ws.write({ key: 'dob', value: '16 February 1941' }) | ||
ws.write({ key: 'spouse', value: 'Kim Young-sook' }) | ||
ws.write({ key: 'occupation', value: 'Clown' }) | ||
ws.end() | ||
``` | ||
filter usage: | ||
```js | ||
db.createReadStream({filter: function(key, value){ | ||
if (/^hit/.test(key)) | ||
return db.FILTER_INCLUDED | ||
else key == 'endStream' | ||
return db.FILTER_STOPPED | ||
else | ||
return db.FILTER_EXCLUDED | ||
}}) | ||
.on('data', function (data) { | ||
console.log(data.key, '=', data.value) | ||
}) | ||
.on('error', function (err) { | ||
console.log('Oh my!', err) | ||
}) | ||
.on('close', function () { | ||
console.log('Stream closed') | ||
}) | ||
.on('end', function () { | ||
console.log('Stream closed') | ||
}) | ||
``` | ||
next and last usage for paged data demo: | ||
``` js | ||
var callbackStream = require('callback-stream') | ||
var lastKey = null; | ||
function nextPage(db, aLastKey, aPageSize, cb) { | ||
var stream = db.readStream({next: aLastKey, limit: aPageSize}) | ||
stream.on('last', function(aLastKey){ | ||
lastKey = aLastKey; | ||
}); | ||
stream.pipe(callbackStream(function(err, data){ | ||
cb(data, lastKey) | ||
})) | ||
} | ||
var pageNo = 1; | ||
dataCallback = function(data, lastKey) { | ||
console.log("page:", pageNo); | ||
console.log(data); | ||
++pageNo; | ||
if (lastKey) { | ||
nextPage(db, lastKey, 10, dataCallback); | ||
} | ||
else | ||
console.log("no more data"); | ||
} | ||
nextPage(db, lastKey, 10, dataCallback); | ||
``` | ||
## Extensible API | ||
@@ -300,0 +157,0 @@ |
@@ -151,3 +151,3 @@ const tap = require('tap') | ||
, expectedCb = function () {} | ||
, expectedOptions = { asBuffer: true } | ||
, expectedOptions = { asBuffer: false } | ||
, expectedKey = 'a key' | ||
@@ -154,0 +154,0 @@ , test |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4
148273
3008
487