mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-03-11 02:12:21 -05:00
Allow forUpdate
for any model
no issue - this was only supported for the Post Model until now - locking should be possible for every resource depending on the use case
This commit is contained in:
parent
90d6ac5a0e
commit
4b21fc1d59
2 changed files with 22 additions and 25 deletions
|
@ -125,6 +125,25 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
|
|||
return validation.validateSchema(this.tableName, this.toJSON());
|
||||
},
|
||||
|
||||
/**
|
||||
* http://knexjs.org/#Builder-forUpdate
|
||||
* https://dev.mysql.com/doc/refman/5.7/en/innodb-locking-reads.html
|
||||
*
|
||||
* Lock target collection/model for further update operations.
|
||||
* This avoids collisions and possible content override cases.
|
||||
*/
|
||||
onFetching: function onFetching(model, columns, options) {
|
||||
if (options.forUpdate && options.transacting) {
|
||||
options.query.forUpdate();
|
||||
}
|
||||
},
|
||||
|
||||
onFetchingCollection: function onFetchingCollection(model, columns, options) {
|
||||
if (options.forUpdate && options.transacting) {
|
||||
options.query.forUpdate();
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Adding resources implies setting these properties on the server side
|
||||
* - set `created_by` based on the context
|
||||
|
@ -369,7 +388,7 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
|
|||
*/
|
||||
permittedOptions: function permittedOptions() {
|
||||
// terms to whitelist for all methods.
|
||||
return ['context', 'include', 'transacting', 'importing'];
|
||||
return ['context', 'include', 'transacting', 'importing', 'forUpdate'];
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
|
@ -56,27 +56,6 @@ Post = ghostBookshelf.Model.extend({
|
|||
return this.updateTags(model, response, options);
|
||||
},
|
||||
|
||||
/**
|
||||
* http://knexjs.org/#Builder-forUpdate
|
||||
* https://dev.mysql.com/doc/refman/5.7/en/innodb-locking-reads.html
|
||||
*
|
||||
* Lock target collection/model for further update operations.
|
||||
* This avoids collisions and possible content override cases.
|
||||
*
|
||||
* `forUpdate` is only supported for posts right now
|
||||
*/
|
||||
onFetching: function onFetching(model, columns, options) {
|
||||
if (options.forUpdate && options.transacting) {
|
||||
options.query.forUpdate();
|
||||
}
|
||||
},
|
||||
|
||||
onFetchingCollection: function onFetchingCollection(model, columns, options) {
|
||||
if (options.forUpdate && options.transacting) {
|
||||
options.query.forUpdate();
|
||||
}
|
||||
},
|
||||
|
||||
onUpdated: function onUpdated(model) {
|
||||
model.statusChanging = model.get('status') !== model.updated('status');
|
||||
model.isPublished = model.get('status') === 'published';
|
||||
|
@ -624,10 +603,9 @@ Post = ghostBookshelf.Model.extend({
|
|||
// whitelists for the `options` hash argument on methods, by method name.
|
||||
// these are the only options that can be passed to Bookshelf / Knex.
|
||||
validOptions = {
|
||||
findOne: ['columns', 'importing', 'withRelated', 'require', 'forUpdate'],
|
||||
findOne: ['columns', 'importing', 'withRelated', 'require'],
|
||||
findPage: ['page', 'limit', 'columns', 'filter', 'order', 'status', 'staticPages'],
|
||||
findAll: ['columns', 'filter', 'forUpdate'],
|
||||
edit: ['forUpdate']
|
||||
findAll: ['columns', 'filter']
|
||||
};
|
||||
|
||||
// The post model additionally supports having a formats option
|
||||
|
|
Loading…
Add table
Reference in a new issue