Bulk Insert
When adding a lot of data into your database using a bulk insert can save a lot of time. This has to do with the way databases work, and is true at the database level.
There are many different ways to “Bulk Insert” into a database, Welds tries to provide a common interface for the best option for each of its backends.
bulk_insert(_,_)
The bulk_insert function is that common interface. It is pretty strait forward taking in a Client and a slice of WeldsModels
bulk_insert is not accessible via the query builder like other operations. You will need to include it with:
#![allow(unused)]
fn main() {
use welds::query::insert::bulk_insert;
}
First build a slice of data that you want to insert.
#![allow(unused)]
fn main() {
#[derive(WeldsModel)]
#[welds(table = "products")]
pub struct Product {
#[welds(primary_key)]
pub id: i32,
pub name: String,
}
}
#![allow(unused)]
fn main() {
let mut products: Vec<Product> = Vec::default();
for i in (0..1000) {
products.push( Product {
id: 0,
name: format!("product #{}", i)
});
}
}
Things to notice:
- The column
idis marked as aprimary_key. Typically in welds if the primary_key field isDefault::default()it is ignored when inserting. Here in Bulk insert the primary_key is ALWAYS ignored. If you want to insert a primary_key column, you can use the method:welds::query::insert::bulk_insert_with_ids - DbState is not used with bulk operations
You can now insert your data with a call to bulk_insert
#![allow(unused)]
fn main() {
use welds::query::insert::bulk_insert;
bulk_insert(&client, &products).await?;
}
Bulk Insert - Advanced Options
There are a couple extra, options for inserting into databases. They allow for overriding of the tablename. These can be extremely useful when importing data or using a temp table. They don’t contain “rust unsafe” code, but are labeled as unsafe because they to NOT protect from SQL injection. It goes without saying, don’t expose the tablename to a user.
bulk_insert_override_tablename_unsafe
and
bulk_insert_with_ids_override_tablename_unsafe
These two function work just like there safe counterparts, but allow manual setting of the tablename.