1.11.0 • Published 10 years ago

modella-level-relations v1.11.0

Weekly downloads
3
License
MIT
Repository
github
Last release
10 years ago

modella-level-relations

NPM version Build Status Dependency Status Coverage Status

install

npm install [--save/--save-dev] modella-level-relations

example

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 (user, fn) {
  user.save(fn)
}, function (err) {
  if(err) throw err

  User.relation('followers').put(frank, charlie, function (err, relation) {
    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)
    })
  })
})

api

use

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.

put(from, to, callback)

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

emits pre relation and relation events

has(from, to, callback)

User.relation('followers').has(model_instance_a, model_instance_b, function (err, has) {})

get(from, options)

User.relation('followers').get(model_instance_a).on('data', function (follower) {})

each(from, options, each, end)

User.relation('followers').each(model_instance_a, function (follower) {}, function (err) {})

all(from, options, fn)

User.relation('followers').all(model_instance_a, function (err, followers) {})

one(from, options, fn)

Todo.relation('author').one(todo, function (err, author) {})

options

  • 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

del(from, to, callback)

User.relation('followers').del(model_instance_a, model_instance_b, function (err) {})

emits relation event

delAll(from, callback)

User.relation('followers').delAll(model_instance_a, function (err) {})

count(from, callback)

User.relation('followers').count(model_instance_a, function (err, count) {})

toggle(from, to, callback)

User.relation('followers').toggle(model_instance_a, function (err) {})

relations.put(from, to, callback)

var relations = require('modella-level-relations')

// `a` -> following -> `b`
// `b` -> followers -> `a`
relations('following', 'followers').put(a, b, function (err, relations) {})

relations.del(from, to, callback)

var relations = require('modella-level-relations')

// `a` -> !following -> `b`
// `b` -> !followers -> `a`
relations('following', 'followers').del(a, b, function (err, relations) {})

relations.delAll(from, to, callback)

var relations = require('modella-level-relations')

// `a` -> !following -> all
// each -> !followers -> `a`
relations('following', 'followers').delAll(a, function (err) {})

relations.has(from, to, callback)

var relations = require('modella-level-relations')

// (`a` -> following -> `b`) && (`b` -> followers -> `a`)
relations('following', 'followers').has(a, b, function (err, has) {})

relations.toggle(from, to, callback)

var relations = require('modella-level-relations')

// `a` -> !following -> `b`
// `b` -> !followers -> `a`
relations('following', 'followers').toggle(a, b, function (err, relations) {})

license

MIT

1.11.0

10 years ago

1.10.0

10 years ago

1.9.2

10 years ago

1.9.1

10 years ago

1.9.0

10 years ago

1.8.0

10 years ago

1.7.1

10 years ago

1.7.0

10 years ago

1.6.0

10 years ago

1.5.4

10 years ago

1.5.3

10 years ago

1.5.2

10 years ago

1.5.1

10 years ago

1.5.0

10 years ago

1.4.0

10 years ago

1.3.0

10 years ago

1.2.0

10 years ago

1.1.0

10 years ago

1.0.0

10 years ago

0.4.0

10 years ago

0.3.3

10 years ago

0.3.2

10 years ago

0.3.1

10 years ago

0.3.0

10 years ago

0.2.0

10 years ago

0.0.1

10 years ago