Skip to main content

CreateMany

Struct CreateMany 

Source
pub struct CreateMany<M>
where M: Model,
{ /* private fields */ }
Expand description

A builder for inserting multiple records of the same model in a single statement.

Records are accumulated with item or with_item, then executed with exec. Alternatively, call into_expr to embed the batch insert as an expression inside another statement (e.g., a has-many association insert).

§Examples

use toasty::stmt::CreateMany;

let users = CreateMany::<User>::new()
    .with_item(|u| u.name("Alice"))
    .with_item(|u| u.name("Bob"))
    .exec(&mut db)
    .await
    .unwrap();
assert_eq!(users.len(), 2);

Implementations§

Source§

impl<M> CreateMany<M>
where M: Model,

Source

pub fn new() -> CreateMany<M>

Create an empty CreateMany builder with no records queued.

Source

pub fn item(self, item: impl IntoInsert<Model = M>) -> CreateMany<M>

Append a record from any value that implements IntoInsert for model M.

Returns self for method chaining.

Source

pub fn with_item( self, f: impl FnOnce(<M as Model>::Create) -> <M as Model>::Create, ) -> CreateMany<M>

Append a record using a closure that configures the model’s generated create builder.

f receives a default create builder and must return it after setting the desired fields.

Returns self for method chaining.

Source

pub fn into_expr(self) -> Expr<List<M>>

Convert the collected inserts into a list expression suitable for embedding in a parent insert statement (e.g., as a nested HasMany value).

Unlike exec, this does not run any database query.

Source

pub async fn exec(self, executor: &mut dyn Executor) -> Result<Vec<M>, Error>

Execute the batch insert and return the created records.

Returns an empty Vec if no records were queued.

Trait Implementations§

Source§

impl<M> Default for CreateMany<M>
where M: Model,

Source§

fn default() -> CreateMany<M>

Returns the “default value” for a type. Read more
Source§

impl<M> IntoExpr<List<M>> for CreateMany<M>
where M: Model,

Source§

fn into_expr(self) -> Expr<List<M>>

Consume self and produce an Expr<T>. Read more
Source§

fn by_ref(&self) -> Expr<List<M>>

Produce an Expr<T> from a reference without consuming self. Read more

Auto Trait Implementations§

§

impl<M> Freeze for CreateMany<M>

§

impl<M> RefUnwindSafe for CreateMany<M>
where M: RefUnwindSafe,

§

impl<M> Send for CreateMany<M>
where M: Send,

§

impl<M> Sync for CreateMany<M>
where M: Sync,

§

impl<M> Unpin for CreateMany<M>
where M: Unpin,

§

impl<M> UnsafeUnpin for CreateMany<M>

§

impl<M> UnwindSafe for CreateMany<M>
where M: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more