Last active
August 29, 2015 14:23
-
-
Save mattsenior/0493e486612efafa337e to your computer and use it in GitHub Desktop.
DataScript backwards relation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var d = require('datascript'); | |
var db = d.db_with( | |
d.empty_db({ | |
'chicken': { ':db/valueType': ':db.type/ref' } | |
}), | |
[ | |
{ ':db/id': 1, 'name': 'Chicken' }, | |
{ ':db/id': 2, 'name': 'Egg 1', 'chicken': 1 }, | |
{ ':db/id': 3, 'name': 'Egg 2', 'chicken': 1 } | |
] | |
); | |
// When using `entity`, `_chicken` will get both eggs: | |
var chicken = d.entity( db, 1 ); | |
var eggsViaEntity = chicken.get('_chicken').map( function( egg ) { | |
return egg.get('name'); | |
} ); | |
console.log( eggsViaEntity ); | |
// => [ 'Egg 1', 'Egg 2' ] | |
// When using `pull` only a single egg is returned: | |
var eggsViaPull = d.pull( db, '["name" {"_chicken" ["name"]}]', 1 ); | |
console.log( eggsViaPull ); | |
// => { 'name': 'Chicken', '_chicken': { 'name': 'Egg 1' } } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment