Skip to content

Commit c80565f

Browse files
committed
Add flag to exclude instance properties from enumeration
1 parent 4d3ad7d commit c80565f

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

lib/Instance.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ function Instance(Model, opts) {
519519
opts.changes.push(key);
520520
}
521521
},
522-
enumerable: true
522+
enumerable: !(prop && !prop.enumerable)
523523
});
524524
};
525525
var addInstanceExtraProperty = function (key) {

lib/Property.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,17 @@ exports.normalize = function (opts) {
4040

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

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

49+
// Defaults to true. Setting to false hides properties from JSON.stringify(modelInstance).
50+
if (!opts.prop.hasOwnProperty("enumerable") || opts.prop.enumerable === true) {
51+
opts.prop.enumerable = true;
52+
}
53+
4954
// Defaults to true. Rational means floating point here.
5055
if (opts.prop.type == "number" && opts.prop.rational === undefined) {
5156
opts.prop.rational = true;

test/integration/instance.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ describe("Model instance", function() {
1616
name : String,
1717
age : { type: 'integer', required: false },
1818
height : { type: 'integer', required: false },
19-
weight : { type: 'number', required: false },
19+
weight : { type: 'number', required: false, enumerable: true },
20+
secret : { type: 'text', required: false, enumerable: false },
2021
data : { type: 'object', required: false }
2122
}, {
2223
identityCache: false,
@@ -445,5 +446,21 @@ describe("Model instance", function() {
445446
});
446447
}
447448
});
449+
450+
describe("Enumerable", function () {
451+
it("should not stringify properties marked as not enumerable", function (done) {
452+
Person.create({ name: 'Dilbert', secret: 'dogbert', weight: 100, data: {data: 3} }, function (err, p) {
453+
if (err) return done(err);
454+
455+
var result = JSON.parse(JSON.stringify(p));
456+
should.not.exist(result.secret);
457+
should.exist(result.weight);
458+
should.exist(result.data);
459+
should.exist(result.name);
460+
461+
done();
462+
});
463+
});
464+
});
448465
});
449466
});

0 commit comments

Comments
 (0)