polyclay-levelup
A LevelUP persistence adapter for Polyclay.
How-to
For LevelUP:
var polyclay = LevelupAdapter = ; var Widget = polyclayModel;polyclay; var options = dbpath: '/path/to/leveldb/dir' dbname: 'widgets' // optional; will be inferred from plural if not;Widget;
The Levelup object is available at obj.adapter.db
. The objects store is a level-sublevel object at obj.adapter.objects
. The attachments data store is available at obj.adapter.attachdb
. Sublevel is used to namespace keys so you can safely re-use a leveldb database you're using for other purposes. Note that this adapter requires json encoding.
You may also pass pre-existing sublevel-wrapped leveldb objects to the storage function:
var levelup = sublevel = path = ; var options = db: attachdb: ;Widget;
If you pass in pre-constructed levelup instances, it's up to you to make sure they're wrapped with sublevel() and have the correct encodings.
Secondary indexes
The adapter uses level-indexing to provide secondary indexes on fields you select. To add secondary indexes, pass an array of property names in the index
field of the model options. The example above creates a secondary index on the name
field of the model.
Widget.find()
is a version of the find function described in the level-indexing docs that returns a fully-constructed model instead of a json structure. Any byFieldName()
functions are also made available on the Model constructor (aka the class); the versions on the model return fully-constructed objects.
TODO: promisify these finders so you can either pass a callback or not as you prefer.