modella-sublevel
Modella plugin to attach a model to a sublevel
-based backing store, with optional secondary indexing.
Usage
var sublevel = var MyModel = // primary key field required (defaults to `id` or `_id` attr per modella API // unindexed attribute // explicitly indexed field // unique fields are implicitly implicitly indexed to verify uniqueness // model initialized, but saving instances will throw until store is attachedvar model = mykey: 'a' // unique fields are inherently `required: true` uniquefield: 'foo' model
Attaching a store
// some time later, assuming `db` is in scope...var store = db // attach new sublevel as backing store for our modelMyModel // now we can save model instancesmodel
Model-aware store methods
// the backing store is made available on the `store` keyMyModelstore === store // store methods are overriden to do model creation/validationmyStore
Store-aware model methods
// modella API extended with some static convenience methodsMyModel // again, result of save is a model instance, which we can manipulate model model) // convenient static methods are available for interfacing with the storeMyModel // query by primary key (if `createReadStream` available on sublevel)Model // query on a secondary index explicitly (if supported by sublevel)Modelquery // observe changes on an indexed attribute (if `createLiveStream` avaialable)Modeltail
Detaching a store
The attach
method can be called without a store reference to detach store from model:
OtherModelOtherModelstore === undefined
When detaching, any methods overwritten on the backing store instance will be replaced with their original versions.