mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-20 17:32:15 +03:00
5a659c9ebe
refs https://github.com/TryGhost/Team/issues/586 Adds new models for stripe price and product tables
58 lines
1.9 KiB
JavaScript
58 lines
1.9 KiB
JavaScript
const ghostBookshelf = require('./base');
|
|
|
|
const StripeProduct = ghostBookshelf.Model.extend({
|
|
tableName: 'stripe_products',
|
|
|
|
product() {
|
|
return this.belongsTo('Product', 'product_id', '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)
|
|
};
|