Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

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

Closed
jsdalton opened this Issue · 2 comments

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 referenced this issue from a commit in jsdalton/resourceful
Jim Dalton Issue #121: Prevent attribute from being set as property on resource …
…if property is not defined
9a40d59
@pksunkara

I think this was intended.

@indexzero, @bmeck Can one of you confirm?

@indexzero
Owner

@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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.