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

What is the recommended way to "create or update" a doc? #136

Open
aguynamedben opened this issue Jul 31, 2017 · 1 comment
Open

What is the recommended way to "create or update" a doc? #136

aguynamedben opened this issue Jul 31, 2017 · 1 comment

Comments

@aguynamedben
Copy link

aguynamedben commented Jul 31, 2017

Hi, thanks for your work on this excellent gem.

I've been going through the README and rdocs, and I'm not sure what the recommended way is to "create or update" a doc. I'm looking for something like Rails' find_or_create_by but for couchrest.

My Model

class User < ApplicationRecord
  has_many :events

  def copy_events_to_pouchdb!
    puts "Copying Events for user #{id} to pouchdb"

    server = CouchRest.new("http://localhost:5985")
    db = server.database!("events-user_#{id}")

    events.each do |event|
      puts "  Copying Event #{event.id} to pouchdb"
      doc = CouchRest::Document.new(
        _id: event.id.to_s,
        title: event.title,
      )
      db.save_doc(doc)
    end
  end
end

My Rake task

  desc "Copy all users to pouchdb-server"
  task copy_events_to_pouchdb: :environment do
    User.find_each do |user|
      user.copy_events_to_pouchdb!
    end
  end

The first time this runs, it works and the events are created as expected, but the second time it runs I get

rake aborted!
CouchRest::Conflict: 409 Conflict
/Users/ben/code/mammoth/app/models/user.rb:33:in `block in copy_events_to_pouchdb!'
/Users/ben/code/mammoth/app/models/user.rb:26:in `copy_events_to_pouchdb!'

Do I need to always go t get THEN a put? (because that would produce a _rev field for the document?) The rdocs for Database#save_doc and Document#save imply that it can create or update, but I don't see any clear-cut examples of how to do this.

Thanks again for the time you spend working on this gem.

@aguynamedben aguynamedben changed the title What is the proper way to create or update a doc? What is the recommended way to create or update a doc? Jul 31, 2017
@aguynamedben aguynamedben changed the title What is the recommended way to create or update a doc? What is the recommended way to "create or update" a doc? Jul 31, 2017
@aguynamedben
Copy link
Author

aguynamedben commented Jul 31, 2017

One method is to approximate Rails' find_or_intialize_by like this:

  def copy_events_to_pouchdb!
    puts "Copying Events for user #{id} to pouchdb"

    server = CouchRest.new("http://localhost:5985")
    db = server.database!("events-user_#{id}")

    events.each do |event|
      puts "  Copying Event #{event.id} to pouchdb"

      # First HTTP request
      doc = db.get(event.id.to_s) || CouchRest::Document.new(_id: event.id.to_s)
      doc["title"] = event.title
      # Second HTTP request
      db.save_doc(doc)
    end
  end

The downside is this requires 2 HTTP requests instead of 1.

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