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

Support to exclude methods when extracting toJSON() #79

Merged
merged 2 commits into from
Jun 27, 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
16 changes: 10 additions & 6 deletions lib/base-document.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class BaseDocument {
_id: { type: DB().nativeIdType() }, // Native ID to backend database
};

this._extractDocPropertiesOnly = null;
this._id = null;
}

Expand Down Expand Up @@ -479,12 +480,15 @@ class BaseDocument {
}
}
}
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'){
values[key] = this[key];
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') {
values[key] = this[key];
}
}
}
return values;
Expand Down
55 changes: 55 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1649,5 +1649,60 @@ describe('Document', function() {
expect(json.children[1]).to.not.be.an.instanceof(Person);
}).then(done, done);
});

it('should serialize data to JSON and ignore methods if configured', function(done) {
class Person extends Document {
constructor() {
super();

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

static collectionName() {
return 'people';
}

getFoo() {
return 'foo';
}
}

var person = Person.create({
name: 'Scott'
});

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

it('should serialize data to JSON and include methods if configured', function(done) {
class Person extends Document {
constructor() {
super();

this.name = String;
this._extractDocPropertiesOnly = false;
}

static collectionName() {
return 'people';
}

getFoo() {
return 'foo';
}
}

var person = Person.create({
name: 'Scott'
});

var json = person.toJSON();
expect(json).to.have.keys(['_id', 'name', 'getFoo']);
done();
});

});
});
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();
});
});
});