flatiron / resourceful

an isomorphic Resource engine for JavaScript

Home Page:http://github.com/flatiron/resourceful

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Properties included in initial data used to create resource are saved and become gettable/settable

jsdalton opened this issue · comments

Not sure if bug or feature, but I found this behavior unusual:

var resourceful = require('resourceful');
resourceful.use('couchdb', { database: 'dummy' });

var Creature = resourceful.define('creature', function () {
  this.bool('vertebrate');
});

var cow = new(Creature)({
    diet:      'grass', // not defined in schema
    vertebrate: true
});
cow.save(function (err, cow) {
    if (err) return console.log(err);
    console.log(cow.diet); // grass
    cow.diet = 'corn';
    cow.color = 'purple'; 
    cow.save(function (err, cow) {
        if (err) return console.log(err);
        console.log(cow.diet); // corn
        console.log(cow.color); // undefined
    });
});

(I used couchdb but this holds true for memory as well.

In a nutshell, if you set a property in the initial data passed to the constructor (e.g. diet above), it becomes a de-facto part of the resource thereafter -- i.e. it's stored in the database and it's gettable and settable. This is not true for properties set as attributes on the resource directly.

Like I said, if this is just an undocumented feature, then cool. I just found it a bit surprising.

I think this was intended.

@indexzero, @bmeck Can one of you confirm?

@pksunkara @jsdalton Yes this is intended. We opted not to throw errors when extra data comes in but throw away data not defined in the schema