drogus / bulk_api

MIT License
149 stars 7 forks source link

Bulk create doesn't return json of created records #15

Closed danielwanja closed 13 years ago

danielwanja commented 13 years ago

The update, put "/api/bulk, returns all updated records. The create, post "/api/bulk", returns only the first and not all created records. This is an issue as we need the ids and other attributes that Rails assigned during the create.

In engine_spec.rb I've added these two tests. The create fails, the update works.

it "should create given records and return all created records" do
  post "/api/bulk", { :tasks => [{:title => "New1"}, {:title => "New2"}] }
  body = JSON.parse(last_response.body)
  body["tasks"].length.should == 2
end

it "should update given records and return all updated records" do
  task = Task.create(:title => "Bar")
  put "/api/bulk", { :tasks => [{:title => "Foo2", :id => @task.id}, {:title => "Bar2", :id => task.id}]}

  @task.reload.title.should == "Foo2"
  task.reload.title.should == "Bar2"

  body = JSON.parse(last_response.body)
  body["tasks"].length.should == 2
  body["tasks"][0]["title"].should == "Foo2"
  body["tasks"][1]["title"].should == "Bar2"

end
drogus commented 13 years ago

As stated in readme, request to create need to have _local_id attribute, otherwise you will not have track of which record you get in response for sproutcore, which will lead to inconsistencies in data store.

danielwanja commented 13 years ago

I understand the use of the _localid, especially in case of validation errors. However if you create five new records how to get their id once they have been created if the post doesn't return the new attributes and id? How do you deal with the scenario I create 5 new records, create them, then do changes, and update them? When is the datastore update with the just created ids?

drogus commented 13 years ago

local_ids are basically guids from sproutcore, so the flow in such situation should go like this:

Is that information that you need or have I missed something?

danielwanja commented 13 years ago

That's exactly what I was expecting. But in my tests I don't get all the record information back from the create but only the first record. So If I save two records I just get the json for one back. See the it "should create given records and return all created records" test it fails and returns only one instead of two records.

I'm creating a Flex library that does similar work (https://github.com/danielwanja/bulk_data_source_flex) than your javascript library and that's when I noticed this issue. I went to the Rails code and added the test to confirm the issue.