orm-random

ORM Random instance get/find

ORM Random instance get/find

This plugin adds random support find for any ORM driver.

Of course you need orm to use it. Other than that, no more dependencies.

npm install orm-random
Model.getRandom([ conditions ], callback);
Model.findRandom([ conditions ], [ limit = 1 ], callback);
var orm = require("orm");
 
orm.connect("mysql://username:password@host/database", function (errdb) {
    if (err) throw err;
 
    db.use(fts);
 
    db.use(require("orm-random"));
 
    var Person = db.define("person", {
        name      : String,
        surname   : String,
        age       : Number
    });
 
    Person.findRandom(10, { age: orm.gte(18) }, function (errpeople) {
        // returns 10 random people (might have dups) with age at least 18 
    });
});