-
-
Notifications
You must be signed in to change notification settings - Fork 153
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: multi_create method for bulk records #406
Comments
I'm not for expanding repository public API to bulk operations, because it's a gateway for "find", "update", "delete" in bulk feature requests. As they aren't common use case, developers can implement by their own, and/or use upcoming |
@davydovanton PS Please use the forum for proposals and please leave GH issues for actionable tickets that we have already agreed on. Thank you very much. 馃挌 |
@jodosha would you point me to a direction how something like |
@spectator You can plug directly on rom commands, be aware that the following line won't update your class SomeRepository < Hanami::Repository
def bulk_insert(data)
command(:create, somes, result: :many).call(data)
end
end |
Thanks a lot @mereghost ! I led myself too deep into ROM commands and completely overlooked something as simple as this. BTW, you can add timestamps plugin like so class SomeRepository < Hanami::Repository
def bulk_insert(data)
command(:create, somes, use: [:timestamps], result: :many).call(data)
end
end |
rom-rb may use a bulk insert whenever you pass in a collection of tuples (or structs, doesn't matter). It of course depends on the db backend, so ie on PG we use multi_insert which does a bulk insert on the database side. This is completely transparent from the user point of view because each adapter may provide custom command subclasses for each database type. It's also worth to mention that in rom-rb there's no concept of inserting a single tuple / struct, it always operates on collections of data, even when you pass in a single tuple it'll be put it into a single-element array and pass down to dataset backend, if its size is > 1 multi insert will be used. ...anyhow, what I'm trying to say is that there's absolutely no need for a special bulk insert API because rom-rb handles it for you, and if you don't like that it's implicit, you can always drop down to the relation API and call Here's what I'm talking about (using rom-4 from master): [3] pry(main)> users = rom.relations[:users]
=> #<ROM::Relation[Users] name=ROM::Relation::Name(users) dataset=#<Sequel::Postgres::Dataset: "SELECT \"id\", \"name\" FROM \"users\" ORDER BY \"users\".\"id\"">>
[4] pry(main)> users.command(:create).call(name: "Jane")
I, [2017-06-25T16:36:49.128106 #23731] INFO -- : (0.082191s) INSERT INTO "users" ("name") VALUES ('Jane') RETURNING *
=> {:id=>2, :name=>"Jane"}
[5] pry(main)> users.command(:create, result: :many).call([{name: "Jane"}, {name: "John"}])
I, [2017-06-25T16:37:15.031153 #23731] INFO -- : (0.000187s) BEGIN
I, [2017-06-25T16:37:15.031634 #23731] INFO -- : (0.000386s) INSERT INTO "users" ("name") VALUES ('Jane'), ('John') RETURNING *
I, [2017-06-25T16:37:15.032716 #23731] INFO -- : (0.000964s) COMMIT
=> [{:id=>5, :name=>"Jane"}, {:id=>6, :name=>"John"}] |
@solnic I see what you mean, but hanami-repository always returns one result even though collection of tuples was passed into |
Now we can use
#create
method for this. But I think it will be better to create the different method for this. We should use#command
here. If you don't mind, I can create PR for this 馃憤How we can use in in projects:
The text was updated successfully, but these errors were encountered: