@@ -16,6 +16,7 @@ It also provides the `crud-storage` and `crud-router` roles for
1616- [ Quickstart] ( #quickstart )
1717- [ API] ( #api )
1818 - [ Insert] ( #insert )
19+ - [ Insert many] ( #insert-many )
1920 - [ Get] ( #get )
2021 - [ Update] ( #update )
2122 - [ Delete] ( #delete )
@@ -233,6 +234,143 @@ crud.insert_object('customers', {
233234...
234235```
235236
237+ ### Insert many
238+
239+ ``` lua
240+ -- Insert batch of tuples
241+ local result , err = crud .insert_many (space_name , tuples , opts )
242+ -- Insert batch of objects
243+ local result , err = crud .insert_object_many (space_name , objects , opts )
244+ ```
245+
246+ where:
247+
248+ * ` space_name ` (` string ` ) - name of the space to insert an object
249+ * ` tuples ` / ` objects ` (` table ` ) - array of tuples/objects to insert
250+ * ` opts ` :
251+ * ` timeout ` (` ?number ` ) - ` vshard.call ` timeout (in seconds)
252+ * ` fields ` (` ?table ` ) - field names for getting only a subset of fields
253+ * ` stop_on_error ` (` ?boolean ` ) - stop on a first error and report error
254+ regarding the failed operation and error about what tuples were not
255+ performed, default is ` false `
256+ * ` rollback_on_error ` (` ?boolean ` ) - any failed operation will lead to
257+ rollback on a storage, where the operation is failed, report error
258+ about what tuples were rollback, default is ` false `
259+
260+ Returns metadata and array with inserted rows, array of errors.
261+ Each error object can contain field ` operation_data ` .
262+
263+ ` operation_data ` field can contain:
264+ * tuple for which the error occurred;
265+ * object with an incorrect format;
266+ * tuple the operation on which was performed but
267+ operation was rollback;
268+ * tuple the operation on which was not performed
269+ because operation was stopped by error.
270+
271+ Right now CRUD cannot provide batch insert with full consistency.
272+ CRUD offers batch insert with partial consistency. That means
273+ that full consistency can be provided only on single replicaset
274+ using ` box ` transactions.
275+
276+ ** Example:**
277+
278+ ``` lua
279+ crud .insert_many (' customers' , {
280+ {1 , box .NULL , ' Elizabeth' , 23 },
281+ {2 , box .NULL , ' Anastasia' , 22 },
282+ })
283+ ---
284+ - metadata :
285+ - {' name' : ' id' , ' type' : ' unsigned' }
286+ - {' name' : ' bucket_id' , ' type' : ' unsigned' }
287+ - {' name' : ' name' , ' type' : ' string' }
288+ - {' name' : ' age' , ' type' : ' number' }
289+ rows :
290+ - [1 , 477 , ' Elizabeth' , 23 ]
291+ - [2 , 401 , ' Anastasia' , 22 ]
292+ ...
293+ crud .insert_object_many (' customers' , {
294+ {id = 3 , name = ' Elizabeth' , age = 24 },
295+ {id = 10 , name = ' Anastasia' , age = 21 },
296+ })
297+ ---
298+ - metadata :
299+ - {' name' : ' id' , ' type' : ' unsigned' }
300+ - {' name' : ' bucket_id' , ' type' : ' unsigned' }
301+ - {' name' : ' name' , ' type' : ' string' }
302+ - {' name' : ' age' , ' type' : ' number' }
303+ rows :
304+ - [3 , 2804 , ' Elizabeth' , 24 ]
305+ - [10 , 569 , ' Anastasia' , 21 ]
306+
307+ -- Partial success
308+ local res , errs = crud .insert_object_many (' customers' , {
309+ {id = 22 , name = ' Alex' , age = 34 },
310+ {id = 3 , name = ' Anastasia' , age = 22 },
311+ {id = 5 , name = ' Sergey' , age = 25 },
312+ })
313+ ---
314+ res
315+ - metadata :
316+ - {' name' : ' id' , ' type' : ' unsigned' }
317+ - {' name' : ' bucket_id' , ' type' : ' unsigned' }
318+ - {' name' : ' name' , ' type' : ' string' }
319+ - {' name' : ' age' , ' type' : ' number' }
320+ rows :
321+ - [5 , 1172 , ' Sergey' , 25 ],
322+ - [22 , 655 , ' Alex' , 34 ],
323+
324+ # errs -- 1
325+ errs [1 ].class_name -- BatchInsertError
326+ errs [1 ].err -- 'Duplicate key exists <...>'
327+ errs [1 ].tuple -- {3, 2804, 'Anastasia', 22}
328+ ...
329+
330+ -- Partial success with stop and rollback on error
331+ -- stop_on_error = true, rollback_on_error = true
332+ -- two error on one storage with rollback, inserts
333+ -- stop by error on this storage inserts before
334+ -- error are rollback
335+ local res , errs = crud .insert_object_many (' customers' , {
336+ {id = 6 , name = ' Alex' , age = 34 },
337+ {id = 92 , name = ' Artur' , age = 29 },
338+ {id = 3 , name = ' Anastasia' , age = 22 },
339+ {id = 4 , name = ' Sergey' , age = 25 },
340+ {id = 9 , name = ' Anna' , age = 30 },
341+ {id = 71 , name = ' Oksana' , age = 29 },
342+ }, {
343+ stop_on_error = true ,
344+ rollback_on_error = true ,
345+ }})
346+ ---
347+ res
348+ - metadata :
349+ - {' name' : ' id' , ' type' : ' unsigned' }
350+ - {' name' : ' bucket_id' , ' type' : ' unsigned' }
351+ - {' name' : ' name' , ' type' : ' string' }
352+ - {' name' : ' age' , ' type' : ' number' }
353+ rows :
354+ - [4 , 1161 , ' Sergey' , 25 ],
355+ - [6 , 1064 , ' Alex' , 34 ],
356+ # errs -- 4
357+ errs [1 ].class_name -- InsertManyError
358+ errs [1 ].err -- 'Duplicate key exists <...>'
359+ errs [1 ].tuple -- {3, 2804, 'Anastasia', 22}
360+
361+ errs [2 ].class_name -- NotPerformedError
362+ errs [2 ].err -- 'Operation with tuple was not performed'
363+ errs [2 ].tuple -- {9, 1644, "Anna", 30}
364+
365+ errs [3 ].class_name -- NotPerformedError
366+ errs [3 ].err -- 'Operation with tuple was not performed'
367+ errs [3 ].tuple -- {71, 1802, "Oksana", 29}
368+
369+ errs [4 ].class_name -- NotPerformedError
370+ errs [4 ].err -- 'Operation with tuple was rollback'
371+ errs [4 ].tuple -- {92, 2040, "Artur", 29}
372+ ```
373+
236374### Get
237375
238376``` lua
0 commit comments