Skip to main content

Model

Trait Model 

Source
pub trait Model:
    Sized
    + Send
    + Sync
    + for<'r> FromRow<'r, AnyRow>
    + Unpin
    + DeserializeOwned {
    const TABLE: &'static str;
    const HAS_SOFT_DELETES: bool = false;
Show 34 methods // Required methods fn find<'life0, 'async_trait>( pool: &'life0 DatabasePool, id: i64, ) -> Pin<Box<dyn Future<Output = Result<Self, Error>> + Send + 'async_trait>> where 'life0: 'async_trait, Self: 'async_trait; fn all<'life0, 'async_trait>( pool: &'life0 DatabasePool, ) -> Pin<Box<dyn Future<Output = Result<Vec<Self>, Error>> + Send + 'async_trait>> where 'life0: 'async_trait, Self: 'async_trait; fn save<'life0, 'life1, 'async_trait>( &'life0 self, pool: &'life1 DatabasePool, ) -> Pin<Box<dyn Future<Output = Result<i64, Error>> + Send + 'async_trait>> where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait; fn delete<'life0, 'async_trait>( pool: &'life0 DatabasePool, id: i64, ) -> Pin<Box<dyn Future<Output = Result<bool, Error>> + Send + 'async_trait>> where 'life0: 'async_trait; // Provided methods fn primary_key(&self) -> i64 { ... } fn select_clause(_kind: DatabaseKind) -> &'static str { ... } fn query(pool: &DatabasePool) -> QueryBuilder<'_, Self> { ... } fn where_eq<'a, V>( pool: &'a DatabasePool, column: &'a str, value: V, ) -> QueryBuilder<'a, Self> where V: 'a + Send + Encode<'a, Any> + Type<Any> { ... } fn where_like<'a, V>( pool: &'a DatabasePool, column: &'a str, pattern: V, ) -> QueryBuilder<'a, Self> where V: 'a + Send + Encode<'a, Any> + Type<Any> { ... } fn where_null<'a>( pool: &'a DatabasePool, column: &'a str, ) -> QueryBuilder<'a, Self> { ... } fn where_not_null<'a>( pool: &'a DatabasePool, column: &'a str, ) -> QueryBuilder<'a, Self> { ... } fn latest<'a>(pool: &'a DatabasePool) -> QueryBuilder<'a, Self> { ... } fn oldest<'a>(pool: &'a DatabasePool) -> QueryBuilder<'a, Self> { ... } fn where_has<'a>( pool: &'a DatabasePool, relation_table: &'a str, foreign_key: &'a str, local_key: &'a str, ) -> QueryBuilder<'a, Self> { ... } fn where_doesnt_have<'a>( pool: &'a DatabasePool, relation_table: &'a str, foreign_key: &'a str, local_key: &'a str, ) -> QueryBuilder<'a, Self> { ... } fn count<'life0, 'async_trait>( pool: &'life0 DatabasePool, ) -> Pin<Box<dyn Future<Output = Result<i64, Error>> + Send + 'async_trait>> where 'life0: 'async_trait, Self: 'async_trait { ... } fn paginate<'life0, 'async_trait>( pool: &'life0 DatabasePool, page: usize, per_page: usize, ) -> Pin<Box<dyn Future<Output = Result<LengthAwarePaginator<Self>, Error>> + Send + 'async_trait>> where 'life0: 'async_trait, Self: 'async_trait { ... } fn destroy<'life0, 'async_trait>( pool: &'life0 DatabasePool, id: i64, ) -> Pin<Box<dyn Future<Output = Result<bool, Error>> + Send + 'async_trait>> where 'life0: 'async_trait, Self: 'async_trait { ... } fn exists<'life0, 'async_trait>( pool: &'life0 DatabasePool, ) -> Pin<Box<dyn Future<Output = Result<bool, Error>> + Send + 'async_trait>> where 'life0: 'async_trait, Self: 'async_trait { ... } fn find_or_fail<'life0, 'async_trait>( pool: &'life0 DatabasePool, id: i64, ) -> Pin<Box<dyn Future<Output = Result<Self, Error>> + Send + 'async_trait>> where 'life0: 'async_trait, Self: 'async_trait { ... } fn first_or_create<'life0, 'async_trait, K, V>( pool: &'life0 DatabasePool, attributes: impl IntoIterator<Item = (K, V)> + Send + 'async_trait, values: impl IntoIterator<Item = (K, V)> + Send + 'async_trait, ) -> Pin<Box<dyn Future<Output = Result<Self, Error>> + Send + 'async_trait>> where 'life0: 'async_trait, K: AsRef<str> + 'async_trait, V: ToString + 'async_trait, Self: 'async_trait { ... } fn update_or_create<'life0, 'async_trait, K, V>( pool: &'life0 DatabasePool, attributes: impl IntoIterator<Item = (K, V)> + Send + 'async_trait, values: impl IntoIterator<Item = (K, V)> + Send + 'async_trait, ) -> Pin<Box<dyn Future<Output = Result<Self, Error>> + Send + 'async_trait>> where 'life0: 'async_trait, K: AsRef<str> + 'async_trait, V: ToString + 'async_trait, Self: 'async_trait { ... } fn find_many<'life0, 'life1, 'async_trait>( pool: &'life0 DatabasePool, ids: &'life1 [i64], ) -> Pin<Box<dyn Future<Output = Result<Vec<Self>, Error>> + Send + 'async_trait>> where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait { ... } fn restore<'life0, 'async_trait>( pool: &'life0 DatabasePool, id: i64, ) -> Pin<Box<dyn Future<Output = Result<bool, Error>> + Send + 'async_trait>> where 'life0: 'async_trait, Self: 'async_trait { ... } fn force_delete<'life0, 'async_trait>( pool: &'life0 DatabasePool, id: i64, ) -> Pin<Box<dyn Future<Output = Result<bool, Error>> + Send + 'async_trait>> where 'life0: 'async_trait, Self: 'async_trait { ... } fn truncate<'life0, 'async_trait>( pool: &'life0 DatabasePool, ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>> where 'life0: 'async_trait, Self: 'async_trait { ... } fn with_trashed<'a>(pool: &'a DatabasePool) -> QueryBuilder<'a, Self> { ... } fn only_trashed<'a>(pool: &'a DatabasePool) -> QueryBuilder<'a, Self> { ... } fn create<'life0, 'async_trait, K, V>( pool: &'life0 DatabasePool, data: impl IntoIterator<Item = (K, V)> + Send + 'async_trait, ) -> Pin<Box<dyn Future<Output = Result<Self, Error>> + Send + 'async_trait>> where 'life0: 'async_trait, K: AsRef<str> + 'async_trait, V: ToString + 'async_trait, Self: 'async_trait { ... } fn has_many<'life0, 'life1, 'async_trait, R>( pool: &'life0 DatabasePool, foreign_key: &'life1 str, local_id: i64, ) -> Pin<Box<dyn Future<Output = Result<Vec<R>, Error>> + Send + 'async_trait>> where 'life0: 'async_trait, 'life1: 'async_trait, R: Model + 'async_trait { ... } fn has_one<'life0, 'life1, 'async_trait, R>( pool: &'life0 DatabasePool, foreign_key: &'life1 str, local_id: i64, ) -> Pin<Box<dyn Future<Output = Result<R, Error>> + Send + 'async_trait>> where 'life0: 'async_trait, 'life1: 'async_trait, R: Model + 'async_trait { ... } fn belongs_to<'life0, 'async_trait, R>( pool: &'life0 DatabasePool, foreign_key_id: i64, ) -> Pin<Box<dyn Future<Output = Result<R, Error>> + Send + 'async_trait>> where 'life0: 'async_trait, R: Model + 'async_trait { ... } fn belongs_to_many<'life0, 'life1, 'life2, 'life3, 'async_trait, R>( pool: &'life0 DatabasePool, pivot_table: &'life1 str, foreign_key: &'life2 str, related_key: &'life3 str, local_id: i64, ) -> Pin<Box<dyn Future<Output = Result<Vec<R>, Error>> + Send + 'async_trait>> where 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait, R: Model + 'async_trait { ... } fn eager_load<'life0, 'life1, 'life2, 'async_trait>( _relation: &'life0 str, _items: &'life1 mut [Self], _pool: &'life2 DatabasePool, ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>> where 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, Self: 'async_trait { ... }
}
Expand description

Trait yang wajib diimplementasikan oleh setiap Model.

Menyediakan interface CRUD async yang seragam. Implementasi detail query ada di masing-masing struct model.

§Contoh

ⓘ
let user = User::find(&pool, 1).await?;
let all  = User::all(&pool).await?;

Required Associated Constants§

Source

const TABLE: &'static str

Nama tabel di database (konstanta compile-time)

Provided Associated Constants§

Source

const HAS_SOFT_DELETES: bool = false

Apakah model ini mengaktifkan Soft Deletes (kolom deleted_at)

Required Methods§

Source

fn find<'life0, 'async_trait>( pool: &'life0 DatabasePool, id: i64, ) -> Pin<Box<dyn Future<Output = Result<Self, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Ambil satu record berdasarkan primary key. Mengembalikan error jika tidak ditemukan.

Source

fn all<'life0, 'async_trait>( pool: &'life0 DatabasePool, ) -> Pin<Box<dyn Future<Output = Result<Vec<Self>, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Ambil semua record yang aktif (bukan soft-deleted).

Source

fn save<'life0, 'life1, 'async_trait>( &'life0 self, pool: &'life1 DatabasePool, ) -> Pin<Box<dyn Future<Output = Result<i64, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Simpan record ini ke database. Mengembalikan last_insert_rowid jika sukses.

Source

fn delete<'life0, 'async_trait>( pool: &'life0 DatabasePool, id: i64, ) -> Pin<Box<dyn Future<Output = Result<bool, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait,

Soft delete — set deleted_at = NOW() tanpa menghapus baris.

Provided Methods§

Source

fn primary_key(&self) -> i64

Nilai primary key record ini

Source

fn select_clause(_kind: DatabaseKind) -> &'static str

Kembalikan daftar kolom yang di-select secara aman sesuai dialect database. Override otomatis dihasilkan oleh #[derive(PakemModel)].

Source

fn query(pool: &DatabasePool) -> QueryBuilder<'_, Self>

Inisialisasi QueryBuilder untuk model ini.

Source

fn where_eq<'a, V>( pool: &'a DatabasePool, column: &'a str, value: V, ) -> QueryBuilder<'a, Self>
where V: 'a + Send + Encode<'a, Any> + Type<Any>,

Shortcut query WHERE column = value

Source

fn where_like<'a, V>( pool: &'a DatabasePool, column: &'a str, pattern: V, ) -> QueryBuilder<'a, Self>
where V: 'a + Send + Encode<'a, Any> + Type<Any>,

Shortcut query WHERE column LIKE pattern

Source

fn where_null<'a>( pool: &'a DatabasePool, column: &'a str, ) -> QueryBuilder<'a, Self>

Shortcut query WHERE column IS NULL

Source

fn where_not_null<'a>( pool: &'a DatabasePool, column: &'a str, ) -> QueryBuilder<'a, Self>

Shortcut query WHERE column IS NOT NULL

Source

fn latest<'a>(pool: &'a DatabasePool) -> QueryBuilder<'a, Self>

Shortcut ORDER BY created_at DESC

Source

fn oldest<'a>(pool: &'a DatabasePool) -> QueryBuilder<'a, Self>

Shortcut ORDER BY created_at ASC

Source

fn where_has<'a>( pool: &'a DatabasePool, relation_table: &'a str, foreign_key: &'a str, local_key: &'a str, ) -> QueryBuilder<'a, Self>

Shortcut query WHERE EXISTS relasi (Laravel: Model::whereHas)

Source

fn where_doesnt_have<'a>( pool: &'a DatabasePool, relation_table: &'a str, foreign_key: &'a str, local_key: &'a str, ) -> QueryBuilder<'a, Self>

Shortcut query WHERE NOT EXISTS relasi (Laravel: Model::whereDoesntHave)

Source

fn count<'life0, 'async_trait>( pool: &'life0 DatabasePool, ) -> Pin<Box<dyn Future<Output = Result<i64, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Hitung total record aktif di database

Source

fn paginate<'life0, 'async_trait>( pool: &'life0 DatabasePool, page: usize, per_page: usize, ) -> Pin<Box<dyn Future<Output = Result<LengthAwarePaginator<Self>, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Ambil data terpaginasi langsung dari Model

Source

fn destroy<'life0, 'async_trait>( pool: &'life0 DatabasePool, id: i64, ) -> Pin<Box<dyn Future<Output = Result<bool, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Hapus record berdasarkan ID (alias delete ala Laravel Destroy)

Source

fn exists<'life0, 'async_trait>( pool: &'life0 DatabasePool, ) -> Pin<Box<dyn Future<Output = Result<bool, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Cek apakah terdapat record yang memenuhi kriteria

Source

fn find_or_fail<'life0, 'async_trait>( pool: &'life0 DatabasePool, id: i64, ) -> Pin<Box<dyn Future<Output = Result<Self, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Ambil record berdasarkan ID atau return Error.

Source

fn first_or_create<'life0, 'async_trait, K, V>( pool: &'life0 DatabasePool, attributes: impl IntoIterator<Item = (K, V)> + Send + 'async_trait, values: impl IntoIterator<Item = (K, V)> + Send + 'async_trait, ) -> Pin<Box<dyn Future<Output = Result<Self, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, K: AsRef<str> + 'async_trait, V: ToString + 'async_trait, Self: 'async_trait,

Cari record berdasarkan kriteria, atau buat baru jika tidak ditemukan.

Source

fn update_or_create<'life0, 'async_trait, K, V>( pool: &'life0 DatabasePool, attributes: impl IntoIterator<Item = (K, V)> + Send + 'async_trait, values: impl IntoIterator<Item = (K, V)> + Send + 'async_trait, ) -> Pin<Box<dyn Future<Output = Result<Self, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, K: AsRef<str> + 'async_trait, V: ToString + 'async_trait, Self: 'async_trait,

Update record jika ditemukan berdasarkan atribut, atau buat baru jika tidak ada (Laravel: updateOrCreate).

Source

fn find_many<'life0, 'life1, 'async_trait>( pool: &'life0 DatabasePool, ids: &'life1 [i64], ) -> Pin<Box<dyn Future<Output = Result<Vec<Self>, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Ambil sekumpulan record berdasarkan array of primary key IDs (Laravel: findMany).

Source

fn restore<'life0, 'async_trait>( pool: &'life0 DatabasePool, id: i64, ) -> Pin<Box<dyn Future<Output = Result<bool, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Pulihkan record yang telah di-soft-delete (Laravel: restore)

Source

fn force_delete<'life0, 'async_trait>( pool: &'life0 DatabasePool, id: i64, ) -> Pin<Box<dyn Future<Output = Result<bool, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Hapus record secara permanen (Hard Delete) dari database (Laravel: forceDelete)

Source

fn truncate<'life0, 'async_trait>( pool: &'life0 DatabasePool, ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Kosongkan seluruh data tabel model (Laravel: truncate)

Source

fn with_trashed<'a>(pool: &'a DatabasePool) -> QueryBuilder<'a, Self>

Shortcut query yang menyertakan data yang sudah di-soft-delete (Laravel: withTrashed)

Source

fn only_trashed<'a>(pool: &'a DatabasePool) -> QueryBuilder<'a, Self>

Shortcut query yang HANYA mengambil data yang telah di-soft-delete (Laravel: onlyTrashed)

Source

fn create<'life0, 'async_trait, K, V>( pool: &'life0 DatabasePool, data: impl IntoIterator<Item = (K, V)> + Send + 'async_trait, ) -> Pin<Box<dyn Future<Output = Result<Self, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, K: AsRef<str> + 'async_trait, V: ToString + 'async_trait, Self: 'async_trait,

Buat record baru dari key-value pairs secara dinamis.

Source

fn has_many<'life0, 'life1, 'async_trait, R>( pool: &'life0 DatabasePool, foreign_key: &'life1 str, local_id: i64, ) -> Pin<Box<dyn Future<Output = Result<Vec<R>, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, R: Model + 'async_trait,

Source

fn has_one<'life0, 'life1, 'async_trait, R>( pool: &'life0 DatabasePool, foreign_key: &'life1 str, local_id: i64, ) -> Pin<Box<dyn Future<Output = Result<R, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, R: Model + 'async_trait,

Definisi relasi Has-One. Contoh: user.has_one::<Profile>(db, "user_id", user.id).await

Source

fn belongs_to<'life0, 'async_trait, R>( pool: &'life0 DatabasePool, foreign_key_id: i64, ) -> Pin<Box<dyn Future<Output = Result<R, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, R: Model + 'async_trait,

Source

fn belongs_to_many<'life0, 'life1, 'life2, 'life3, 'async_trait, R>( pool: &'life0 DatabasePool, pivot_table: &'life1 str, foreign_key: &'life2 str, related_key: &'life3 str, local_id: i64, ) -> Pin<Box<dyn Future<Output = Result<Vec<R>, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait, R: Model + 'async_trait,

Definisi relasi Many-To-Many. Contoh: post.belongs_to_many::<Tag>(db, "post_tag", "post_id", "tag_id", post.id).await

Source

fn eager_load<'life0, 'life1, 'life2, 'async_trait>( _relation: &'life0 str, _items: &'life1 mut [Self], _pool: &'life2 DatabasePool, ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, Self: 'async_trait,

Eager Load relasi untuk sekumpulan model (N+1 Solution). Di-override oleh model untuk mendukung .with(“relation”)

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§