Skip to content

Commit

Permalink
Include related virtualAttributes in toJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
venables committed Mar 26, 2019
1 parent bc4d11e commit ddaeb41
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions lib/model/modelToJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function toJson(model, optIn) {
const modelClass = model.constructor;

const opt = {
virtuals: getVirtuals(optIn, modelClass),
virtuals: getVirtuals(optIn),
shallow: isShallow(optIn),
omit: getOmit(optIn, modelClass),
pick: null,
Expand Down Expand Up @@ -40,13 +40,13 @@ function toDatabaseJson(model, builder) {
return mergeQueryProps(model, json, opt.omitFromJson, builder);
}

function getVirtuals(opt, modelClass) {
function getVirtuals(opt) {
if (!opt) {
return modelClass.getVirtualAttributes();
return true;
} else if (Array.isArray(opt.virtuals)) {
return opt.virtuals;
} else if (opt.virtuals) {
return modelClass.getVirtualAttributes();
return true;
} else {
return null;
}
Expand All @@ -68,6 +68,7 @@ function getPick(modelClass) {
function toExternalJsonImpl(model, opt) {
const json = {};
const keys = Object.keys(model);
const vAttr = model.constructor.getVirtualAttributes();

for (let i = 0, l = keys.length; i < l; ++i) {
const key = keys[i];
Expand All @@ -76,8 +77,10 @@ function toExternalJsonImpl(model, opt) {
assignJsonValue(json, key, value, opt);
}

if (opt.virtuals !== null) {
if (Array.isArray(opt.virtuals)) {
assignVirtualAttributes(json, model, opt.virtuals, opt);
} else if (vAttr && opt.virtuals === true) {
assignVirtualAttributes(json, model, vAttr, opt);
}

return json;
Expand Down

0 comments on commit ddaeb41

Please sign in to comment.