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

Reduce toJSON implementation: use the power of bookshelf #9423

Merged
merged 1 commit into from
Feb 14, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 14 additions & 33 deletions core/server/models/base/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,7 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({

// Bookshelf `initialize` - declare a constructor-like method for model creation
initialize: function initialize() {
var self = this,
options = arguments[1] || {};

// make options include available for toJSON()
if (options.include) {
this.include = _.clone(options.include);
}
var self = this;

[
'fetching',
Expand Down Expand Up @@ -325,32 +319,20 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
return this.fixBools(this.fixDatesWhenFetch(attrs));
},

/**
* `shallow` - won't return relations
* `omitPivot` - won't return pivot fields
*
* `toJSON` calls `serialize`.
*
* @param options
* @returns {*}
*/
toJSON: function toJSON(options) {
var attrs = _.extend({}, this.attributes),
self = this;
options = options || {};
options = _.pick(options, ['shallow', 'baseKey', 'include', 'context']);

if (options && options.shallow) {
return attrs;
}

if (options && options.include) {
this.include = _.union(this.include, options.include);
}

_.each(this.relations, function each(relation, key) {
if (key.substring(0, 7) !== '_pivot_') {
// if include is set, expand to full object
var fullKey = _.isEmpty(options.baseKey) ? key : options.baseKey + '.' + key;
if (_.includes(self.include, fullKey)) {
attrs[key] = relation.toJSON(_.extend({}, options, {baseKey: fullKey, include: self.include}));
}
}
});
var opts = _.cloneDeep(options || {});
opts.omitPivot = true;

// @TODO upgrade bookshelf & knex and use serialize & toJSON to do this in a neater way (see #6103)
return proto.finalize.call(this, attrs);
return proto.toJSON.call(this, opts);
},

// Get attributes that have been updated (values before a .save() call)
Expand Down Expand Up @@ -608,8 +590,7 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
data = this.filterData(data);
options = this.filterOptions(options, 'findOne');

// We pass include to forge so that toJSON has access
return this.forge(data, {include: options.include}).fetch(options);
return this.forge(data).fetch(options);
},

/**
Expand Down
2 changes: 1 addition & 1 deletion core/server/models/invite.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Invite = ghostBookshelf.Model.extend({
data = this.filterData(data, 'findOne');
options.withRelated = _.union(options.withRelated, options.include);

var invite = this.forge(data, {include: options.include});
var invite = this.forge(data);
return invite.fetch(options);
},

Expand Down
6 changes: 4 additions & 2 deletions core/server/models/plugins/include-count.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@ module.exports = function (Bookshelf) {
return modelProto.fetchAll.apply(this, arguments);
},

finalize: function (attrs) {
var countRegex = /^(count)(__)(.*)$/;
serialize: function serialize(options) {
var attrs = modelProto.serialize.call(this, options),
countRegex = /^(count)(__)(.*)$/;

_.forOwn(attrs, function (value, key) {
var match = key.match(countRegex);
if (match) {
Expand Down
6 changes: 2 additions & 4 deletions core/server/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,14 +384,12 @@ User = ghostBookshelf.Model.extend({
options.withRelated = _.union(options.withRelated, ['roles']);
options.include = _.union(options.include, ['roles']);

query = this.forge(data, {include: options.include});

query = this.forge(data);
query.query('join', 'roles_users', 'users.id', '=', 'roles_users.user_id');
query.query('join', 'roles', 'roles_users.role_id', '=', 'roles.id');
query.query('where', 'roles.name', '=', lookupRole);
} else {
// We pass include to forge so that toJSON has access
query = this.forge(data, {include: options.include});
query = this.forge(data);
}

if (status === 'active') {
Expand Down