rikas / zoho_hub

Zoho CRM API V2 Wrapper
MIT License
25 stars 30 forks source link

Multi record update (Bulk update) support #38

Open tsukaby opened 4 years ago

tsukaby commented 4 years ago

The following code is very slow (because the PUT request is executed 200 times)

[tsukaby@tsukamac zoho_hub]% bin/console
Refreshing token...
[1] pry(main)> class Contact < ZohoHub::BaseRecord;attributes(:id, :email);end
=> [:id, :email]
[2] pry(main)> all = Contact.all
[3] pry(main)> all.count
=> 200
[4] pry(main)> all.map(&:save)

# Took 3 minutes.

Is there an API on zoho_hub to solve this easily? If it don't exist, i think create one.

What interface do you think should be? I think a following interface.

module ZohoHub
  class BaseRecord
    class << self
      def bulk_upsert(arr)

By the way, I wrote the following code and performed a batch update.

class Account < ZohoHub::BaseRecord
  attributes(:id, :name)
  attribute_translation(
      id: :id, # Make lowercase when converting to json.
  )
end

accounts = Account.all

# A maximum of 100 records can be inserted/updated per API call.
# https://www.zoho.com/crm/developer/docs/api/upsert-records.html
batch_size = 100

accounts.each_slice(batch_size) { |arr|
  ZohoHub.connection.put(
      'Accounts',
      {data: arr.map(&:to_params), trigger: 'approval'}.to_json)
}
tsukaby commented 4 years ago

Is it better to prepare a README or an example without creating a bulk update method?