level-foreignkeys
Manage relationships between keys, and stream the results.
Deleting keys automatically cleans up their relationships.
Full Use Example
var level = ;var LevelForeignKeys = ;var ConcatStream = ; var db = ; { db;} { db;} { db;} { db;} { db;} { var results = ; results;} { var results = ; results; } ;
Installing
npm install --save level-foreignkeys
Running Tests
cd node_modules/level-foreignkeysnpm inpm test
Wrapping LevelUp to "Mix-in" Foreign Key Methods
var level = ; //by default, uses leveldown+levelupvar LevelForeignKeys = ; var db = ;
Adding Foreign Key
addForeignKey(key, field, foreignKey, putOptions, callback)
Arguments:
- key: The key you want the relationship to start from.
- field: The name of the relationship type. This may represent the field in your value object. It might not. It is the name that categorizes the relationship.
- foreignKey: The key you want to link the first key to.
- putOptions: In running this function, it does several puts. Pass in the options you want to pass to those put operations.
- callback: Function called when done.
db;
Removing a Foreign Key
In the same way you added the foreign key, you may remove it.
delForeignKey(key, field, foreignKey, delOoptions, callback)
Arguments:
- key: The key you want the relationship to start from.
- field: The name of the relationship type. This may represent the field in your value object. It might not. It is the name that categorizes the relationship.
- foreignKey: The key you want to link the first key to.
- delOptions: In running this function, it does several del calls. Pass in the options you want to pass to those del operations.
- callback: Function called when done.
db;
Streaming Foreign Keys
readForeignKeys(key, field, opts)
Arguments:
- key: The key you want to get the foreign keys of.
- field: The relationship category that you want.
- opts: The extra options Object that you want to merge into the
createReadStream
call that this function makes.
Returns:
A node stream
of key/value entries that have the queried relationship.
var resultStream = db;
Streaming Reversed Relationships
level-foreignkeys also keeps track of the relationships in reverse with separate indexes.
Retreiving them similarly involves starting at the other end of the relationship and using readReverseForeignKey
.
readReverseForeignKeys(key, field, opts)
Arguments:
- key: The foreign key from which you want to get the keys related.
- field: The relationship category that you want.
- opts: The extra options Object that you want to merge into the
createReadStream
call that this function makes.
Returns:
A node stream
of key/value entries that have the queried relationship.
var resultStream = db;