Skip to content

Commit

Permalink
Add embedded test and small optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Blokland committed Jun 24, 2016
1 parent 0f89492 commit 2ec0e9e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/base-document.js
Original file line number Diff line number Diff line change
Expand Up @@ -480,10 +480,10 @@ class BaseDocument {
}
}
}
var proto = Object.getPrototypeOf(this);
var protoProps = Object.getOwnPropertyNames(proto);

if (this._extractDocPropertiesOnly !== true) {
var proto = Object.getPrototypeOf(this);
var protoProps = Object.getOwnPropertyNames(proto);

for (var i = 0; i < protoProps.length; i++) {
key = protoProps[i];
if (key !== 'constructor' && key !== 'id') {
Expand Down
44 changes: 44 additions & 0 deletions test/embedded.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -669,5 +669,49 @@ describe('Embedded', function() {
expect(json.address.isPoBox).to.be.equal(false);
}).then(done, done);
});

it('should serialize data to JSON and ignore methods recursively if configured', function(done) {
class Address extends EmbeddedDocument {
constructor() {
super();
this.street = String;
}

getBar() {
return 'bar';
}
}

class Person extends Document {
constructor() {
super();

this.name = String;
this.address = Address;
this._extractDocPropertiesOnly = true;
}

static collectionName() {
return 'people';
}

getFoo() {
return 'foo';
}
}

var person = Person.create({
name: 'Scott',
address : {
street : 'Bar street'
}
});

var json = person.toJSON();
expect(json).to.have.keys(['_id', 'name', 'address']);
expect(json.address).to.have.keys(['street']);

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

0 comments on commit 2ec0e9e

Please sign in to comment.