0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00

Added Product, StripeProduct & StripePrice relations (#12877)

refs https://github.com/TryGhost/Team/issues/586

We have to use `belongsToMany` because of the way bookshelf relations
work. In reality the relationship is 'hasMany', e.g. a Product has many
Stripe Prices.

These relations are the minimal needed to satisfy the following
relationships without transforming the results. (e.g. flattening the
StripePrices from a list of StripeProducts for a Product)

Product -> StripeProduct:       product.related('stripeProducts')
StripeProduct -> StripePrice:   stripeProduct.related('stripePrices');
Product -> StripePrice:         product.related('stripePrices');
StripePrice -> Product:         stripePrice.related('stripeProduct.product');
This commit is contained in:
Fabien 'egg' O'Carroll 2021-04-14 19:20:39 +01:00 committed by GitHub
parent 4be4af9c18
commit 4fe417bcab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 1 deletions

View file

@ -25,7 +25,22 @@ const Product = ghostBookshelf.Model.extend({
}
},
members: function members() {
stripeProducts() {
return this.hasMany('StripeProduct', 'product_id', 'id');
},
stripePrices() {
return this.belongsToMany(
'StripePrice',
'stripe_products',
'product_id',
'stripe_product_id',
'id',
'stripe_product_id'
);
},
members() {
return this.belongsToMany('Member', 'members_products', 'product_id', 'member_id');
}
});

View file

@ -5,6 +5,10 @@ const StripeProduct = ghostBookshelf.Model.extend({
product() {
return this.belongsTo('Product', 'product_id', 'id');
},
stripePrices() {
return this.hasMany('StripePrice', 'stripe_product_id', 'stripe_product_id');
}
}, {