Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi record update (Bulk update) support #38

Open
tsukaby opened this issue Oct 3, 2019 · 1 comment
Open

Multi record update (Bulk update) support #38

tsukaby opened this issue Oct 3, 2019 · 1 comment

Comments

@tsukaby
Copy link
Contributor

tsukaby commented Oct 3, 2019

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
Copy link
Contributor Author

tsukaby commented Oct 3, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant