From c80565f166a50e55830632d17d4f2559e5cc354f Mon Sep 17 00:00:00 2001 From: Adam Sheldon Date: Fri, 6 May 2016 11:54:55 -0700 Subject: [PATCH] Add flag to exclude instance properties from enumeration --- lib/Instance.js | 2 +- lib/Property.js | 7 ++++++- test/integration/instance.js | 19 ++++++++++++++++++- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/lib/Instance.js b/lib/Instance.js index bfca4dea..030bbd8c 100755 --- a/lib/Instance.js +++ b/lib/Instance.js @@ -519,7 +519,7 @@ function Instance(Model, opts) { opts.changes.push(key); } }, - enumerable: true + enumerable: !(prop && !prop.enumerable) }); }; var addInstanceExtraProperty = function (key) { diff --git a/lib/Property.js b/lib/Property.js index fdfc6ba4..c00375ba 100644 --- a/lib/Property.js +++ b/lib/Property.js @@ -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; diff --git a/test/integration/instance.js b/test/integration/instance.js index 1c45382a..882481ca 100644 --- a/test/integration/instance.js +++ b/test/integration/instance.js @@ -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, @@ -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(); + }); + }); + }); }); });