Skip to content


Subversion checkout URL

You can clone with
Download ZIP
an isomorphic Resource engine for JavaScript

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.


Defining resources

Here's the simplest of resources:

var Creature = resourceful.define('creature');

The returned Creature object is a resource constructor, in other words, a function.

Now let's add some properties to this constructor:'diet'); // Defaults to String'vertebrate', Boolean);'belly', Array);

And add a method to the prototype:

Creature.prototype.feed = function (food) {

Now lets instantiate a Creature, and feed it:

var wolf = new(Creature)({
    diet:      'carnivor',
    vertebrate: true

You can also define resources this way:

var Creature = resourceful.define('creature', function () {'diet');'vertebrate', Boolean);'belly', Array);

    this.prototype.feed = function (food) {

Defining properties with, type='string', options={})

Lets define a legs property, which is the number of legs the creature has:'legs', Number);

Note that this form is equivalent:'legs', 'number');

If we wanted to constrain the possible values the property could take, we could pass in an object as the last parameter:'legs', Number, {
    required: true,

    minimum: 0,
    maximum: 8,

    assert: function (val) {
        return val % 2 === 0;

Now resourceful won't let Creature instances be saved unless the legs property has a value between 0 and 8, and is even,

This style is also valid for defining properties:'legs', Number)
        .assert(function (val) { return val % 2 === 0 });

If you want to access and modify an already defined property, you can do it this way:['legs'].maximum(6);

Saving and fetching resources

Wolf.create({ name: 'Wolverine', age: 68 }, function (err, wolf) {
    if (err) { throw new(Error)(err) }

    console.log(wolf); // { _id: 42, resource: 'wolf', name: 'Wolverine', age: 68 }

    wolf.age ++; (err) {
        if (!err) console.log('happy birthday ' + + '!');

Wolf.get(42, function (e, wolf) {
    if (e) { throw new(Error)(e) }

    wolf.update({ fur: 'curly' }, function (e, wolf) {
        console.log(wolf.fur); // "curly"

Resource constructor methods

These methods are available on all user-defined resource constructors, as well as on the default resourceful.Resource constructor.

Resource.get(id, [callback])

Fetch a resource by id.

Resource.update(id, properties, [callback])

Update a resource with properties.

Resource.destroy(id, [callback])

Destroy a resource by id.


Fetches all resources of this type., [callback])

Resource.create(properties, [callback])

Resource prototype methods

These are the prototype methods, available on resource instances created with the new operator.[callback])

Resource.prototype.update(properties, [callback])



Something went wrong with that request. Please try again.