mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-10 23:36:14 -05:00
Added benefits relation to Product model
refs https://github.com/TryGhost/Team/issues/806 This relation sets up the ability to both read and write relations via the Product model, allowing us to expose benefits via the Admin Product API.
This commit is contained in:
parent
cbac3d1eb0
commit
c57e612286
1 changed files with 17 additions and 0 deletions
|
@ -4,6 +4,12 @@ const _ = require('lodash');
|
|||
const Product = ghostBookshelf.Model.extend({
|
||||
tableName: 'products',
|
||||
|
||||
relationships: ['benefits'],
|
||||
|
||||
relationshipBelongsTo: {
|
||||
benefits: 'benefits'
|
||||
},
|
||||
|
||||
async onSaving(model, _attr, options) {
|
||||
ghostBookshelf.Model.prototype.onSaving.apply(this, arguments);
|
||||
|
||||
|
@ -78,6 +84,17 @@ const Product = ghostBookshelf.Model.extend({
|
|||
return filteredKeys;
|
||||
},
|
||||
|
||||
benefits() {
|
||||
return this.belongsToMany('Benefit', 'products_benefits', 'product_id', 'benefit_id')
|
||||
.withPivot('sort_order')
|
||||
.query('orderBy', 'sort_order', 'ASC')
|
||||
.query((qb) => {
|
||||
// avoids bookshelf adding a `DISTINCT` to the query
|
||||
// we know the result set will already be unique and DISTINCT hurts query performance
|
||||
qb.columns('benefits.*');
|
||||
});
|
||||
},
|
||||
|
||||
monthlyPrice() {
|
||||
return this.belongsTo('StripePrice', 'monthly_price_id', 'id');
|
||||
},
|
||||
|
|
Loading…
Add table
Reference in a new issue