levelup persistence adapter for polyclay, the schema-enforcing document mapper

npm install polyclay-levelup
12 downloads in the last week
19 downloads in the last month


A LevelUP persistence adapter for Polyclay. You must specify which LevelDB implementation you'd like to use as a back end for LevelUP, such as LevelDOWN.

Build Status


For LevelUP:

var polyclay = require('polyclay'),
    LevelupAdapter = require('polyclay-levelup');

var Widget = polyclay.Model.buildClass({
        partnum: 'string'
        name: 'string',
        description: 'string',
    singular: 'widget',
    plural: 'widgets',
    index: [ 'name' ]
polyclay.persist(Widget, 'partnum');

var options =
    dbpath: '/path/to/leveldb/dir',
    dbname: 'widgets' // optional
Widget.setStorage(options, polyclay.LevelupAdapter);

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  = require('levelup'),
    sublevel = require('level-sublevel')
    path     = require('path');

var options =
    db:       sublevel(levelup('./db/bigdb', {encoding: 'json'})),
    attachdb: sublevel(levelup(path.join('.', 'db', 'bigdb', 'attachments'), {encoding: 'binary'})),
Widget.setStorage(options, polyclay.LevelupAdapter);

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.

npm loves you