Rodabase
Transactional, replicable document store for Node.js and browsers. Built on LevelDB.
- Streams and middleware based asynchronous API.
- Transactions guarantee linearizable local operations.
- Causal+ consistent, transport-agnostic multi master replication.
- Storage backends: LevelDB on Node.js; IndexedDB on browser.
$ npm install rodabase
License
MIT
API
API stable; documentation in progress.
rodabase(path, [options])
var rodabase = ; var roda = ;
roda(name)
.put(id, doc, [tx], [cb])
Create a new document or update an existing document doc
by specifying id
.
Optionally bind to a transaction instance tx
.
;
.post(doc, [tx], [cb])
Create a new document doc
with an auto-generated _id
.
Auto generated _id is a unique, URL-safe, time sorted string.
Optionally bind to a transaction instance tx
.
;
.get(id, [tx], [cb])
Retrieve a document specified by id
. If id
not exists, callback with notFound
error.
Optionally bind to a transaction instance tx
.
;
.del(id, [tx], [cb])
Delete a document specified by id
. If document not exists, callback with notFound
error.
Optionally bind to a transaction instance tx
.
Transaction
Transactions in Rodabase guarantee linearizable consistency for local operations, which avoids unexpected behavior and simplifies application development.
LevelDB supports atomic batched operations,
while durability is configurable via sync
option of LevelDB.
Rodabase leverages level-transactions for two-phase locking and snapshot isolation, which makes it ACID compliant.
roda.transaction()
Creates a new transaction instance. get()
, put()
, del()
, getBy()
methods can be binded to the transaction instance, to perform operations in a sequential, atomic, isolated manner.
//Transactional get and putvar tx = roda;tx;
Hooks
.use('validate', [hook...])
validation
triggered when putting a document. Invoked at the beginning of a write operation, result can be validated and changes can be made before the document is locked.
Context object consists of the following properties:
result
: Result document before locking.
var people = ; people; people;people;people; //will not trigger validate
.use('diff', [hook...])
diff
triggered when putting and deleting a document.
Invoked when document is locked, current and resulting states of document are accessible.
It also exposes the transaction instance, which makes it a very powerful mechanism for a lot of use cases, such as
enforcing data integrity and permissions, creating arbitrary triggers and versioning patterns.
Context object consists of the following properties:
current
: Current state of document.null
if this is an insert.result
: Resulting document.null
if this is a delete.transaction
: Transaction instance.
var data = ;var logs = ; data; var tx = roda; data;data;data;data; tx;
.use('conflict', [hook...])
Indexes
Rodabase supports secondary indexes using mapper function. Indexes are calculated transactionally, results can be retrieved right after callback of a successful write.
.registerIndex(name, mapper)
Register an index named name
using mapper
function.
mapper
is provided with document object and emit function function(doc, emit){}
.
emit
conists of arguments emit(key, [doc], [unique])
that must be called synchronously within the mapper
:
key
index key. Unlike_id
,key
can be arbitrary object for sorting, such as String, Number, Date or prefixing with Array. Exceptnull
orundefined
key is not allowed.doc
object, optionally specify the mapped document object.unique
boolean. Iftrue
,key
must be unique within the index, otherwise writes callback withexists
error. Defaultfalse
.
//Non unique index; //Unique index; //Multiple emits, Array prefixed; //Conditional emit, sorted by updated;
.rebuildIndex([tag], [cb])
Indexes need to be rebuilt when registerIndex()
after a document is committed, or when mapper
function has changed.
rebuildIndex()
will rebuild all registered index within the roda section. Optionally specify tag
so that indexes will only get rebuilt when tag
has changed.
users;
.readStream([options])
Obtain a ReadStream of the Roda section by calling the readStream()
method.
You can specify range options control the range of documents that are streamed. options
accepts following properties:
gt
(greater than),gte
(greater than or equal) define the lower bound of_id
or_key
to be streamed. Whenreverse: true
the order will be reversed, but the documents streamed will be the same.lt
(less than),lte
(less than or equal) define the higher bound of_id
or_key
to be streamed. Whenreverse: true
the order will be reversed, but the documents streamed will be the same.reverse
boolean, defaultfalse
, settrue
to reverse stream output.limit
number, limit the number of results. Default no limit.index
define index to be used. Default indexed by_id
.prefix
define string or array prefix of_id
or_key
to be streamed. Default no prefix.
var JSONStream = ; //JSON transform stream //Streams consumption ; //pipe to console app; ; ; ;
.getBy(index, key, [tx], [cb])
Retrieve a uniquely indexed document specified by index
and key
.
Only available for indexes with unique
flag.
If key
not exists, callback with notFound
error.
Optionally bind to a transaction instance tx
.
//email index; //Transactionalvar tx = roda; ; tx;
Replication
Rodabase supports multi-master replication that preserves Causal+ - causal consistency with convergent conflict handling. The implementation loosely follows the COPS-CD approach as presented in the article: Don’t Settle for Eventual: Scalable Causal Consistency for Wide-Area Storage with COPS.
- Maintaining partial ordering that respects potential causality, using Lamport clocks.
- Keeping track of nearest gets-from dependency for each write.
- Replication queue that commits write only when causal dependencies have been satisfied.
.replicateStream([options])
Rodabase exposes replication mechanism as Node.js duplex stream, which is transport-agnostic.
Example below shows a browser-server replication using Shoe (SockJS).
Browser:
var shoe = ; var rodabase = ;var roda = ;var posts = ; var stream = ;var repl = posts; repl; //do stuffsposts;posts;
Server:
var shoe = ;var http = ; var rodabase = ;var roda = ;var posts = ; var server = http;server; var sock = ;sock; //do stuffsposts;posts;
Extras
Special Fields
Special fields are reserved of identifying states of documents:
_rev
(revision) current revision of document that resembles a lamport clock. Consists of two parts:mid
- ID ofroda()
section.seq
- lamport timestamp that increments based on casual dependencies.
_from
(gets from) nearest gets-from dependency. Generated on write operation from a replicated document._after
(write after)seq
of previous local write for keeping track of execution order.