mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-24 11:22:19 +03:00
cada88975a
no-issue * Added bulkAdd method to Member,Customer&Subscription model This allows us to keep the db access in the model layer * Updated @tryghost/members-api to 0.27.2 This includes fixes for rate-limiting of requests, and exposes necessary Stripe methods for creating customers and complimentary subscriptions, without affecting the database. * Refactored importer to parallelise tasks where possible By parallelising our tasks we are able to improve the speed at which the entire import completes.
89 lines
2.8 KiB
JavaScript
89 lines
2.8 KiB
JavaScript
const _ = require('lodash');
|
|
const ghostBookshelf = require('./base');
|
|
|
|
const MemberStripeCustomer = ghostBookshelf.Model.extend({
|
|
tableName: 'members_stripe_customers',
|
|
|
|
relationships: ['subscriptions'],
|
|
|
|
relationshipBelongsTo: {
|
|
subscriptions: 'members_stripe_customers_subscriptions'
|
|
},
|
|
|
|
subscriptions() {
|
|
return this.hasMany('StripeCustomerSubscription', 'customer_id', 'customer_id');
|
|
},
|
|
|
|
member() {
|
|
return this.belongsTo('Member', 'member_id', 'id');
|
|
}
|
|
}, {
|
|
async upsert(data, unfilteredOptions) {
|
|
const customerId = data.customer_id;
|
|
const model = await this.findOne({customer_id: customerId}, unfilteredOptions);
|
|
if (model) {
|
|
return this.edit(data, Object.assign({}, unfilteredOptions, {
|
|
id: model.id
|
|
}));
|
|
}
|
|
return this.add(data, unfilteredOptions);
|
|
},
|
|
|
|
async bulkAdd(data, unfilteredOptions = {}) {
|
|
if (!unfilteredOptions.transacting) {
|
|
return ghostBookshelf.transaction((transacting) => {
|
|
return this.bulkAdd(data, Object.assign({transacting}, unfilteredOptions));
|
|
});
|
|
}
|
|
const result = {
|
|
successful: 0,
|
|
unsuccessful: 0,
|
|
errors: []
|
|
};
|
|
|
|
const CHUNK_SIZE = 100;
|
|
|
|
for (const chunk of _.chunk(data, CHUNK_SIZE)) {
|
|
try {
|
|
await ghostBookshelf.knex(this.prototype.tableName).insert(chunk);
|
|
result.successful += chunk.length;
|
|
} catch (err) {
|
|
result.unsuccessful += chunk.length;
|
|
result.errors.push(err);
|
|
}
|
|
}
|
|
return result;
|
|
},
|
|
|
|
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);
|
|
}
|
|
});
|
|
|
|
module.exports = {
|
|
MemberStripeCustomer: ghostBookshelf.model('MemberStripeCustomer', MemberStripeCustomer)
|
|
};
|