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

Closed
jsdalton opened this Issue Oct 18, 2012 · 2 comments

Projects

None yet

3 participants

@jsdalton

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.

@jsdalton jsdalton pushed a commit to jsdalton/resourceful that referenced this issue Oct 19, 2012
Jim Dalton Issue #121: Prevent attribute from being set as property on resource …
…if property is not defined
9a40d59
@pksunkara
Contributor

I think this was intended.

@indexzero, @bmeck Can one of you confirm?

@indexzero
Member

@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

@indexzero indexzero closed this Nov 29, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment