1 | # NoSQL Stream
|
2 |
|
3 | [![Build Status](https://secure.travis-ci.org/snowyu/nosql-stream.png?branch=master)](http://travis-ci.org/snowyu/nosql-stream)
|
4 |
|
5 | [![NPM](https://nodei.co/npm/nosql-stream.png?stars&downloads&downloadRank)](https://nodei.co/npm/nosql-stream/)
|
6 |
|
7 | Add the streamable ability to the [abstract-nosql](https://github.com/snowyu/node-abstract-nosql) database.
|
8 |
|
9 | ## ReadStream
|
10 |
|
11 | ReadStream is used to search and read the [abstract-nosql](https://github.com/snowyu/node-abstract-nosql) database.
|
12 |
|
13 | You must implement the db.iterator(options), iterator.next() and iterator.end() to use. (See [AbstractIterator](https://github.com/snowyu/node-abstract-iterator))
|
14 |
|
15 | * db.iterator(options): create an iterator instance
|
16 | * iterator.next() and iterator.end(): the instance method of the iterator
|
17 |
|
18 | The resulting stream is a Node.js-style [Readable Stream](http://nodejs.org/docs/latest/api/stream.html#stream_readable_stream)
|
19 | where `'data'` events emit objects with `'key'` and `'value'` pairs.
|
20 |
|
21 | You can also use the `gt`, `lt` and `limit` options to control the
|
22 | range of keys that are streamed. And you can use the filter function to filter the resulting stream.
|
23 |
|
24 |
|
25 | ### Usage
|
26 |
|
27 | ReadStream(db, [options[, makeData]])
|
28 |
|
29 |
|
30 | __arguments__
|
31 |
|
32 | * db: the [abstract-nosql](https://github.com/snowyu/abstract-nosql) db instance must be exists.
|
33 | * options object(note: some options depend on the implementation of the Iterator)
|
34 | * db: the same with the db argument
|
35 | * `'next'`: the raw key data to ensure the readStream return keys is greater than the key. See `'last'` event.
|
36 | * note: this will affect the range[gt/gte or lt/lte(reverse)] options.
|
37 | * `'filter'` *(function)*: to filter data in the stream
|
38 | * function filter(key, value) if return:
|
39 | * 0(consts.FILTER_INCLUDED): include this item(default)
|
40 | * 1(consts.FILTER_EXCLUDED): exclude this item.
|
41 | * -1(consts.FILTER_STOPPED): stop stream.
|
42 | * note: the filter function argument 'key' and 'value' may be null, it is affected via keys and values of this options.
|
43 | * `'range'` *(string or array)*: the keys are in the give range as the following format:
|
44 | * string:
|
45 | * "[a, b]": from a to b. a,b included. this means {gte:'a', lte: 'b'}
|
46 | * "(a, b]": from a to b. b included, a excluded. this means {gt:'a', lte:'b'}
|
47 | * "[, b)" : from begining to b, begining included, b excluded. this means {lt:'b'}
|
48 | * "(, b)" : from begining to b, begining excluded, b excluded. this means {gt:null, lt:'b'}
|
49 | * note: this will affect the gt/gte/lt/lte options.
|
50 | * "(,)": this is not be allowed. the ending should be a value always.
|
51 | * array: the key list to get. eg, ['a', 'b', 'c']
|
52 | * `gt`/`gte`/`lt`/`lte` options will be ignored.
|
53 | * `'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.
|
54 | * `'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.
|
55 | * `'start', 'end'` legacy ranges - instead use `'gte', 'lte'`
|
56 | * `'match'` *(string)*: use the minmatch to match the specified keys.
|
57 | * Note: It will affect the range[gt/gte or lt/lte(reverse)] options maybe.
|
58 | * `'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.
|
59 | * `'reverse'` *(boolean, default: `false`)*: a boolean, set true and the stream output will be reversed.
|
60 | * `'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.
|
61 | * `'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.
|
62 |
|
63 | * makeData function
|
64 | * just overwrite this if you wanna decode or transform the data.
|
65 |
|
66 |
|
67 | ```js
|
68 |
|
69 | var NoSQLStream=require('nosql-stream')
|
70 | var FILTER_EXCLUDED = require('nosql-stream/lib/consts').FILTER_EXCLUDED
|
71 | var ReadStream = NoSQLStream.ReadStream
|
72 |
|
73 |
|
74 | function filter(key,value) {
|
75 | if (key % 2 === 0) return FILTER_EXCLUDED
|
76 | }
|
77 | var readStream = ReadStream(db, {filter:filter})
|
78 | //or:
|
79 | var readStream = new ReadStream(db, {filter:filter})
|
80 |
|
81 | readStream.on('data', function (data) {
|
82 | console.log(data.key, '=', data.value)
|
83 | })
|
84 | .on('error', function (err) {
|
85 | console.log('Oh my!', err)
|
86 | })
|
87 | .on('close', function () {
|
88 | console.log('Stream closed')
|
89 | })
|
90 | .on('end', function () {
|
91 | console.log('Stream closed')
|
92 | })
|
93 |
|
94 |
|
95 | ```
|
96 |
|
97 | ## WriteStream
|
98 |
|
99 | WriteStream is used to write data to the [abstract-nosql](https://github.com/snowyu/abstract-nosql) database.
|
100 |
|
101 | The WriteStream is a Node.js-style [Writable Stream](http://nodejs.org/docs/latest/api/stream.html#stream_writable_stream) which accepts objects with `'key'` and `'value'` pairs on its `write()` method.
|
102 |
|
103 | The WriteStream will buffer writes and submit them as a `batch()` operations where writes occur *within the same tick*.
|
104 |
|
105 | ### Usage
|
106 |
|
107 | WriteStream(db, [options])
|
108 |
|
109 | __arguments__
|
110 |
|
111 | * options object
|
112 | * db: the [abstract-nosql](https://github.com/snowyu/abstract-nosql) db instance must be exists.
|
113 | * db: the same with options.db
|
114 |
|
115 |
|
116 | ```js
|
117 |
|
118 | var NoSQLStream=require('nosql-stream')
|
119 | var WriteStream = NoSQLStream.WriteStream
|
120 |
|
121 | var ws = WriteStream(db)
|
122 | //or:
|
123 | var ws = new WriteStream(db)
|
124 |
|
125 |
|
126 | ws.on('error', function (err) {
|
127 | console.log('Oh my!', err)
|
128 | })
|
129 | ws.on('finish', function () {
|
130 | console.log('Write Stream finish')
|
131 | })
|
132 |
|
133 | ws.write({ key: 'name', value: 'Yuri Irsenovich Kim' })
|
134 | ws.write({ key: 'dob', value: '16 February 1941' })
|
135 | ws.write({ key: 'spouse', value: 'Kim Young-sook' })
|
136 | ws.write({ key: 'occupation', value: 'Clown' })
|
137 | ws.end()
|
138 |
|
139 | ```
|
140 |
|
141 | # AbstractIterator
|
142 |
|
143 | You must implement the AbstractIterator if you wanna the database supports the ReadStreamable ability.
|
144 |
|
145 | [AbstractIterator](https://github.com/snowyu/node-abstract-iterator)
|
146 |
|
147 |
|
148 | * AbstractIterator(db[, options])
|
149 | * db: Provided with the current instance of [AbstractNoSql](https://github.com/snowyu/node-abstract-nosql).
|
150 | * options: the iterator options. see the ReadStream's options.
|
151 | * next([callback]):
|
152 | * nextSync():
|
153 | * end([callback]):
|
154 | * it's the alias for free method() to keep comaptiable with abstract-leveldown.
|
155 | * endSync():
|
156 | * it's the alias for freeSync method() to keep comaptiable with abstract-leveldown.
|
157 | * free():
|
158 | * freeSync():
|
159 |
|
160 | The following methods need to be implemented:
|
161 |
|
162 | ## Sync methods:
|
163 |
|
164 | ### AbstractIterator#_nextSync()
|
165 |
|
166 | Get the next element of this iterator.
|
167 |
|
168 | __return__
|
169 |
|
170 | * if any result: return a two elements of array
|
171 | * the first is the key, the first element could be null or undefined if options.keys is false
|
172 | * the second is the value, the second element could be null or undefined if options.values is false
|
173 | * or return false, if no any data yet.
|
174 |
|
175 |
|
176 | #### AbstractIterator#_endSync()
|
177 |
|
178 | end the iterator.
|
179 |
|
180 | ### Async methods:
|
181 |
|
182 | these async methods are optional to be implemented.
|
183 |
|
184 | #### AbstractIterator#_next(callback)
|
185 | #### AbstractIterator#_end(callback)
|
186 |
|