level-match-index
Index and filter LevelDB databases and watch for future changes.
Example
Set up the view indexes and filters:
var Index = var level = var sub = var db = var views = post: Indexdb match: type: 'post' index: 'id' single: true postsByTag: Indexdb match: type: 'post' index: many: 'tags' // index each tag in array seperately commentsByPost: Indexdb match: type: 'comment' index: 'postId'
Add some data:
var post1 = {
id: 'post-1', // used for matching as specified above
type: 'post', //
title: 'Typical Blog Post Example',
tags: [ 'test post', 'long winded' ],
body: 'etc...',
date: Date.now()
}
var post2 = {
id: 'post-2',
type: 'post',
title: 'Typical Blog Post Example',
tags: [ 'test post', 'exciting' ],
body: 'etc...',
date: Date.now()
}
var comment1 = {
id: 'comment-1',
type: 'comment', // used for matching as specified above
postId: post1.id, //
name: 'Matt McKegg',
body: 'cool story bro',
date: Date.now()
}
var comment2 = {
id: 'comment-2',
type: 'comment',
postId: post1.id,
name: 'Joe Blogs',
body: 'I do not understand!',
date: Date.now()
}
db.batch([
{key: post1.id, value: post1, type: 'put'},
{key: post2.id, value: post2, type: 'put'},
{key: comment1.id, value: comment1, type: 'put'},
{key: comment2.id, value: comment2, type: 'put'}
])
Now query the views:
var result = post: null comments: views { views} { t}
Or by tags:
var posts = views { t}
Watch for future changes:
var comment3 = id: 'comment-2' type: 'comment' // used for matching as specified above postId: post1id // name: 'Bobby' body: 'Done yet?' date: Date var remove = views db // remove the watcher hook if no longer needed
Query params
Same example as above but instead of specifying the postId for comments index, pull it out using a query:
var result = post: null comments: views { // specify a value to extract as query and specify where to get it from as read option views} { t}