modella-level-relations

levelup based modella relations

modella-level-relations

npm install [--save/--save-dev] modella-level-relations
var relations = require('modella-level-relations'),
    modella = require('modella'),
    sublevel = require('sublevel'),
    store = require('level-modella'),
    level = require('level'),
    timehat = require('timehat')
    assert = require('assert'),
    series = require('map-series')
 
var db = level('/tmp/relations')
var sub = sublevel(db)
var User = modella('User')
 
User.use(store(sub.sublevel('users')))
User.use(relations.plugin(sub.sublevel('relations')))
User.attr('id')
User.attr('name')
 
var frank = User({
  id: timehat(),
  name: 'frank'
})
 
var charlie = User({
  id: timehat(),
  name: 'charlie'
})
 
series([frank, charlie], function (userfn) {
  user.save(fn)
}, function (err) {
  if(err) throw err
 
  User.relation('followers').put(frank, charlie, function (errrelation) {
    if(err) throw err
 
    var now = new Date()
    assert(relation.from === frank.primary())
    assert(relation.to === charlie.primary())
    assert(typeof relation.id === 'string')
    assert(relation.id.length > 0)
    assert(timehat.toDate(relation.id).getUTCMonth() === now.getUTCMonth())
    assert(timehat.toDate(relation.id).getUTCDate() === now.getUTCDate())
    assert(timehat.toDate(relation.id).getUTCHours() === now.getUTCHours())
    assert(timehat.toDate(relation.id).getYear() === now.getYear())
 
 
    User.relation('followers').get(frank).on('data', function (follower) {
      assert(follower.name() === charlie.name())
      assert(follower.primary() === charlie.primary())
      assert(follower.__relation === relation.id)
    })
  })
})

On every Model that needs relations, Model.use should be called:

var relations = require('modella-level-relations')
var store = require('level-modella')
var level = require('level')('/path/to/my/db')
 
var User = modella('User')
User.use(store(level))
User.use(relations.plugin(level))

Important: modella-level-relations requires level as the modella backend.

User.relation('followers').put(model_instance_a, model_instance_b, function (errrelation) {})

emits pre relation and relation events

User.relation('followers').has(model_instance_a, model_instance_b, function (errhas) {})
User.relation('followers').get(model_instance_a).on('data', function (follower) {})
User.relation('followers').each(model_instance_a, function (follower) {}, function (err) {})
User.relation('followers').all(model_instance_a, function (errfollowers) {})
Todo.relation('author').one(todo, function (errauthor) {})
  • start: the relation id key you wish to start the read at.
  • end: the relation id key you wish to end the read at.
  • reverse (boolean, default: true): by default, the relations are fetched in reverse order: the last added relation is the first to be fetched
  • limit (number, default: -1): limit the number of results
User.relation('followers').del(model_instance_a, model_instance_b, function (err) {})

emits relation event

User.relation('followers').delAll(model_instance_a, function (err) {})
User.relation('followers').count(model_instance_a, function (errcount) {})
User.relation('followers').toggle(model_instance_a, function (err) {})
var relations = require('modella-level-relations')
 
// `a` -> following -> `b` 
// `b` -> followers -> `a` 
relations('following', 'followers').put(a, b, function (errrelations) {})
var relations = require('modella-level-relations')
 
// `a` -> !following -> `b` 
// `b` -> !followers -> `a` 
relations('following', 'followers').del(a, b, function (errrelations) {})
var relations = require('modella-level-relations')
 
// `a` -> !following -> all 
// each -> !followers -> `a` 
relations('following', 'followers').delAll(a, function (err) {})
var relations = require('modella-level-relations')
 
// (`a` -> following -> `b`) && (`b` -> followers -> `a`) 
relations('following', 'followers').has(a, b, function (errhas) {})
var relations = require('modella-level-relations')
 
// `a` -> !following -> `b` 
// `b` -> !followers -> `a` 
relations('following', 'followers').toggle(a, b, function (errrelations) {})

MIT