ContextDB - JSON Context for LevelDB
This module creates instances of JsonContext from a leveldb database using levelup. Datasources are automatically generated from matchers and watch for realtime changes.
Installation
$ npm install contextdb
API
require('level-json-context')(db, options)
Pass in an instance of a levelup database and specify matchers. Ensure the database has valueEncoding: 'json'
. Returns an instance of contextDB
Options:
- matchers: Object containing named matchers. See matcher options on JSON Context for all options. Placeholders can be specified anywhere in the matcher filter by using
{$query: 'queryToGetData'}
- primaryKey: Choose the object key to use as the primary index. Defaults to
'id'
. - incrementingKey: (defaults to '_seq') Add an incrementing ID to objects
- timestamps: (defaults to
true
) whether to automatically add timestamps to edited objectscreated_at
,updated_at
,deleted_at
. Required if usingdatasource.emitChangesSince
.
var LevelDB = var ContextDB = var db = var contextDB =
contextDB.applyChange(object, cb)
Push objects into the database. This will also notify all relevant datasources listening.
All changes will be accepted so this should only be triggered by trusted sources.
var newObject = id: 1 parentId: 'site' name: "Home" type: 'page' contextDB
contextDB.applyChanges(arrayOfObjects, cb)
Batch version of applyChange
.
contextDB.forceIndex()
Forces the database to reindex all the matchers. Could take some time depending on how many items there are in the DB. This is automatically run if you change the match
portion of any matcher.
contextDB.generate(options, callback(err, datasource))
Returns a datasource (instance of JSON Context) prepopulated with the relevent data as chosen by matchers and starting data.
It will recieve live events from the database for all specified matchers until datasource.destroy()
is called.
Changes pushed in using datasource.pushChange
will be checked against matchers and if pass, applied to the database.
Options:
- data: The starting point for the datasource. Matchers should use $query to hook into the specified attributes.
- matcherRefs: An array of refs from the matchers specified when creating the contextDB. This option is order sensitive as matchers can refer to the result of another matcher.
datasource.emitChangesSince(timestamp)
Will cause the datasource to emit all changes to any listeners that have occured since the given timestamp (including deletions)
Example
var params = parentId: 1 userId: 'user_123' token: 'some_unique_random_string'var matcherRefs = 'current_user' 'items_for_parent' var userDatasources = {} contextDB
And handle the realtime connections:
var Shoe =
And on the client:
var stream = stream //log instream