mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-01 13:54:35 +03:00
4fe417bcab
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');
62 lines
2.0 KiB
JavaScript
62 lines
2.0 KiB
JavaScript
const ghostBookshelf = require('./base');
|
|
|
|
const StripeProduct = ghostBookshelf.Model.extend({
|
|
tableName: 'stripe_products',
|
|
|
|
product() {
|
|
return this.belongsTo('Product', 'product_id', 'id');
|
|
},
|
|
|
|
stripePrices() {
|
|
return this.hasMany('StripePrice', 'stripe_product_id', 'stripe_product_id');
|
|
}
|
|
|
|
}, {
|
|
async upsert(data, unfilteredOptions = {}) {
|
|
const stripeProductId = data.stripe_product_id;
|
|
const model = await this.findOne({stripe_product_id: stripeProductId}, unfilteredOptions);
|
|
if (model) {
|
|
return this.edit(data, Object.assign({}, unfilteredOptions, {
|
|
id: model.id
|
|
}));
|
|
}
|
|
return this.add(data, unfilteredOptions);
|
|
},
|
|
|
|
add(data, unfilteredOptions = {}) {
|
|
if (!unfilteredOptions.transacting) {
|
|
return ghostBookshelf.transaction((transacting) => {
|
|
return this.add(data, Object.assign({transacting}, unfilteredOptions));
|
|
});
|
|
}
|
|
return ghostBookshelf.Model.add.call(this, data, unfilteredOptions);
|
|
},
|
|
|
|
edit(data, unfilteredOptions = {}) {
|
|
if (!unfilteredOptions.transacting) {
|
|
return ghostBookshelf.transaction((transacting) => {
|
|
return this.edit(data, Object.assign({transacting}, unfilteredOptions));
|
|
});
|
|
}
|
|
return ghostBookshelf.Model.edit.call(this, data, unfilteredOptions);
|
|
},
|
|
|
|
destroy(unfilteredOptions = {}) {
|
|
if (!unfilteredOptions.transacting) {
|
|
return ghostBookshelf.transaction((transacting) => {
|
|
return this.destroy(Object.assign({transacting}, unfilteredOptions));
|
|
});
|
|
}
|
|
return ghostBookshelf.Model.destroy.call(this, unfilteredOptions);
|
|
}
|
|
});
|
|
|
|
const StripeProducts = ghostBookshelf.Collection.extend({
|
|
model: StripeProduct
|
|
});
|
|
|
|
module.exports = {
|
|
StripeProduct: ghostBookshelf.model('StripeProduct', StripeProduct),
|
|
StripeProducts: ghostBookshelf.collection('StripeProducts', StripeProducts)
|
|
};
|