polyclay-rethink

0.2.0 • Public • Published

polyclay-rethink

Rethinkdb backing storage for polyclay models

on npm Tests Coverage Dependencies

How-to

var polyclay = require('polyclay'),
    Adapter = require('polyclay-rethink');
 
var Widget = polyclay.Model.buildClass({
    properties:
    {
        partnum: 'string'
        name: 'string',
        description: 'string',
    },
    singular: 'widget',
    plural: 'widgets',
    index: [ 'name' ]
});
polyclay.persist(Widget, 'partnum');
 
var options =
{
    host:      'localhost',
    port:      28015,
    authKey:   'optional auth key',
    database:  'test', // required
    tablename: 'widget_table', // optional
    dbopts:    { }, // optional
};
Widget.setStorage(options, Adapter);

If necessary, the adapter will create the database and the table named for the model. If tablename isn't provided, the model's plural will be used instead.

If you need to specify options for table sharding, set them in dbopts. They'll be passed to rethink.tableCreate().

Secondary indexes

To create a secondary index on a property, add the name of the property to the index option in the model builder. This option must be an array.

In the example above, a secondary index is created for the name property. A finder named byName() is created on the model constructor. This function will always start with by and then add the name of the property with the first letter upper-cased. E.g., if you create an index for a property named snake_case, you'll get a finder named bySnake_case, which will be silly-looking but is at least predictable. The alias findByProperty() is also created, for historical reasons.

Widget.byName('flux capacitor', function(err, widgets)
{
    apply21Gigawatts(widgets[0]);
});

TODO

Make the finders flexible about promises vs arrays.

Package Sidebar

Install

npm i polyclay-rethink

Weekly Downloads

1

Version

0.2.0

License

ISC

Last publish

Collaborators

  • ceejbot