mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-10 23:36:14 -05:00
Merge pull request #6072 from ErisDS/use-gql
Refactor old processOptions/where to use GQL JSON
This commit is contained in:
commit
b64a0cc1f4
4 changed files with 64 additions and 36 deletions
|
@ -274,20 +274,16 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
|
||||||
// Filter options so that only permitted ones remain
|
// Filter options so that only permitted ones remain
|
||||||
options = this.filterOptions(options, 'findPage');
|
options = this.filterOptions(options, 'findPage');
|
||||||
|
|
||||||
// Extend the model defaults
|
// This applies default properties like 'staticPages' and 'status'
|
||||||
options = _.defaults(options, this.findPageDefaultOptions());
|
// And then converts them to 'where' options... this behaviour is effectively deprecated in favour
|
||||||
|
// of using filter - it's only be being kept here so that we can transition cleanly.
|
||||||
// Run specific conversion of model query options to where options
|
this.processOptions(_.defaults(options, this.findPageDefaultOptions()));
|
||||||
options = this.processOptions(itemCollection, options);
|
|
||||||
|
|
||||||
// Ensure only valid fields/columns are added to query
|
|
||||||
if (options.columns) {
|
|
||||||
options.columns = _.intersection(options.columns, this.prototype.permittedAttributes());
|
|
||||||
}
|
|
||||||
|
|
||||||
// If there are `where` conditionals specified, add those to the query.
|
// If there are `where` conditionals specified, add those to the query.
|
||||||
if (options.where) {
|
if (options.where) {
|
||||||
itemCollection.query('where', options.where);
|
itemCollection.query(function (qb) {
|
||||||
|
gql.knexify(qb, options.where);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply FILTER
|
// Apply FILTER
|
||||||
|
@ -304,6 +300,11 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
|
||||||
// TODO: this should just be done for all methods @ the API level
|
// TODO: this should just be done for all methods @ the API level
|
||||||
options.withRelated = _.union(options.withRelated, options.include);
|
options.withRelated = _.union(options.withRelated, options.include);
|
||||||
|
|
||||||
|
// Ensure only valid fields/columns are added to query
|
||||||
|
if (options.columns) {
|
||||||
|
options.columns = _.intersection(options.columns, this.prototype.permittedAttributes());
|
||||||
|
}
|
||||||
|
|
||||||
if (options.order) {
|
if (options.order) {
|
||||||
options.order = self.parseOrderOption(options.order);
|
options.order = self.parseOrderOption(options.order);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -333,8 +333,7 @@ Post = ghostBookshelf.Model.extend({
|
||||||
findPageDefaultOptions: function findPageDefaultOptions() {
|
findPageDefaultOptions: function findPageDefaultOptions() {
|
||||||
return {
|
return {
|
||||||
staticPages: false, // include static pages
|
staticPages: false, // include static pages
|
||||||
status: 'published',
|
status: 'published'
|
||||||
where: {}
|
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -347,14 +346,28 @@ Post = ghostBookshelf.Model.extend({
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
processOptions: function processOptions(itemCollection, options) {
|
/**
|
||||||
|
* @deprecated in favour of filter
|
||||||
|
*/
|
||||||
|
processOptions: function processOptions(options) {
|
||||||
|
if (!options.staticPages && !options.status) {
|
||||||
|
return options;
|
||||||
|
}
|
||||||
|
|
||||||
|
// This is the only place that 'options.where' is set now
|
||||||
|
options.where = {statements: []};
|
||||||
|
|
||||||
// Step 4: Setup filters (where clauses)
|
// Step 4: Setup filters (where clauses)
|
||||||
if (options.staticPages !== 'all') {
|
if (options.staticPages !== 'all') {
|
||||||
// convert string true/false to boolean
|
// convert string true/false to boolean
|
||||||
if (!_.isBoolean(options.staticPages)) {
|
if (!_.isBoolean(options.staticPages)) {
|
||||||
options.staticPages = _.contains(['true', '1'], options.staticPages);
|
options.staticPages = _.contains(['true', '1'], options.staticPages);
|
||||||
}
|
}
|
||||||
options.where['posts.page'] = options.staticPages;
|
options.where.statements.push({prop: 'page', op: '=', value: options.staticPages});
|
||||||
|
delete options.staticPages;
|
||||||
|
} else if (options.staticPages === 'all') {
|
||||||
|
options.where.statements.push({prop: 'page', op: 'IN', value: [true, false]});
|
||||||
|
delete options.staticPages;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unless `all` is passed as an option, filter on
|
// Unless `all` is passed as an option, filter on
|
||||||
|
@ -362,7 +375,11 @@ Post = ghostBookshelf.Model.extend({
|
||||||
if (options.status !== 'all') {
|
if (options.status !== 'all') {
|
||||||
// make sure that status is valid
|
// make sure that status is valid
|
||||||
options.status = _.contains(['published', 'draft'], options.status) ? options.status : 'published';
|
options.status = _.contains(['published', 'draft'], options.status) ? options.status : 'published';
|
||||||
options.where['posts.status'] = options.status;
|
options.where.statements.push({prop: 'status', op: '=', value: options.status});
|
||||||
|
delete options.status;
|
||||||
|
} else {
|
||||||
|
options.where.statements.push({prop: 'status', op: 'IN', value: ['published', 'draft']});
|
||||||
|
delete options.status;
|
||||||
}
|
}
|
||||||
|
|
||||||
return options;
|
return options;
|
||||||
|
|
|
@ -59,16 +59,17 @@ Tag = ghostBookshelf.Model.extend({
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
findPageDefaultOptions: function findPageDefaultOptions() {
|
findPageDefaultOptions: function findPageDefaultOptions() {
|
||||||
return {
|
return {};
|
||||||
where: {}
|
|
||||||
};
|
|
||||||
},
|
},
|
||||||
|
|
||||||
orderDefaultOptions: function orderDefaultOptions() {
|
orderDefaultOptions: function orderDefaultOptions() {
|
||||||
return {};
|
return {};
|
||||||
},
|
},
|
||||||
|
|
||||||
processOptions: function processOptions(itemCollection, options) {
|
/**
|
||||||
|
* @deprecated in favour of filter
|
||||||
|
*/
|
||||||
|
processOptions: function processOptions(options) {
|
||||||
return options;
|
return options;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -166,9 +166,7 @@ User = ghostBookshelf.Model.extend({
|
||||||
}, {
|
}, {
|
||||||
findPageDefaultOptions: function findPageDefaultOptions() {
|
findPageDefaultOptions: function findPageDefaultOptions() {
|
||||||
return {
|
return {
|
||||||
status: 'active',
|
status: 'active'
|
||||||
where: {},
|
|
||||||
whereIn: {}
|
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -180,28 +178,39 @@ User = ghostBookshelf.Model.extend({
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
processOptions: function processOptions(itemCollection, options) {
|
/**
|
||||||
// TODO: there are multiple statuses that make a user "active" or "invited" - we a way to translate/map them:
|
* @deprecated in favour of filter
|
||||||
// TODO (cont'd from above): * valid "active" statuses: active, warn-1, warn-2, warn-3, warn-4, locked
|
*/
|
||||||
// TODO (cont'd from above): * valid "invited" statuses" invited, invited-pending
|
processOptions: function processOptions(options) {
|
||||||
|
if (!options.status) {
|
||||||
|
return options;
|
||||||
|
}
|
||||||
|
|
||||||
|
// This is the only place that 'options.where' is set now
|
||||||
|
options.where = {statements: []};
|
||||||
|
|
||||||
|
var allStates = activeStates.concat(invitedStates),
|
||||||
|
value;
|
||||||
|
|
||||||
// Filter on the status. A status of 'all' translates to no filter since we want all statuses
|
// Filter on the status. A status of 'all' translates to no filter since we want all statuses
|
||||||
if (options.status && options.status !== 'all') {
|
if (options.status !== 'all') {
|
||||||
// make sure that status is valid
|
// make sure that status is valid
|
||||||
// TODO: need a better way of getting a list of statuses other than hard-coding them...
|
options.status = allStates.indexOf(options.status) > -1 ? options.status : 'active';
|
||||||
options.status = _.indexOf(
|
|
||||||
['active', 'warn-1', 'warn-2', 'warn-3', 'warn-4', 'locked', 'invited', 'inactive'],
|
|
||||||
options.status) !== -1 ? options.status : 'active';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.status === 'active') {
|
if (options.status === 'active') {
|
||||||
itemCollection.query().whereIn('status', activeStates);
|
value = activeStates;
|
||||||
} else if (options.status === 'invited') {
|
} else if (options.status === 'invited') {
|
||||||
itemCollection.query().whereIn('status', invitedStates);
|
value = invitedStates;
|
||||||
} else if (options.status !== 'all') {
|
} else if (options.status === 'all') {
|
||||||
options.where.status = options.status;
|
value = allStates;
|
||||||
|
} else {
|
||||||
|
value = options.status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
options.where.statements.push({prop: 'status', op: 'IN', value: value});
|
||||||
|
delete options.status;
|
||||||
|
|
||||||
return options;
|
return options;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue