Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flag to exclude instance properties from enumeration #724

Merged
merged 1 commit into from
May 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/Instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ function Instance(Model, opts) {
opts.changes.push(key);
}
},
enumerable: true
enumerable: !(prop && !prop.enumerable)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prop may be undefined when key is value on a 'hasMany' relationship.

});
};
var addInstanceExtraProperty = function (key) {
Expand Down
7 changes: 6 additions & 1 deletion lib/Property.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,17 @@ exports.normalize = function (opts) {

if (KNOWN_TYPES.indexOf(opts.prop.type) === -1 && !(opts.prop.type in opts.customTypes)) {
throw new ORMError("Unknown property type: " + opts.prop.type, 'NO_SUPPORT');
}
}

if (!opts.prop.hasOwnProperty("required") && opts.settings.get("properties.required")) {
opts.prop.required = true;
}

// Defaults to true. Setting to false hides properties from JSON.stringify(modelInstance).
if (!opts.prop.hasOwnProperty("enumerable") || opts.prop.enumerable === true) {
opts.prop.enumerable = true;
}

// Defaults to true. Rational means floating point here.
if (opts.prop.type == "number" && opts.prop.rational === undefined) {
opts.prop.rational = true;
Expand Down
19 changes: 18 additions & 1 deletion test/integration/instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ describe("Model instance", function() {
name : String,
age : { type: 'integer', required: false },
height : { type: 'integer', required: false },
weight : { type: 'number', required: false },
weight : { type: 'number', required: false, enumerable: true },
secret : { type: 'text', required: false, enumerable: false },
data : { type: 'object', required: false }
}, {
identityCache: false,
Expand Down Expand Up @@ -445,5 +446,21 @@ describe("Model instance", function() {
});
}
});

describe("Enumerable", function () {
it("should not stringify properties marked as not enumerable", function (done) {
Person.create({ name: 'Dilbert', secret: 'dogbert', weight: 100, data: {data: 3} }, function (err, p) {
if (err) return done(err);

var result = JSON.parse(JSON.stringify(p));
should.not.exist(result.secret);
should.exist(result.weight);
should.exist(result.data);
should.exist(result.name);

done();
});
});
});
});
});