You've already forked seamantic
Compare commits
1 Commits
0.0.5
...
2bd654249d
| Author | SHA1 | Date | |
|---|---|---|---|
|
2bd654249d
|
Generated
+1
-1
@@ -1211,7 +1211,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "seamantic"
|
||||
version = "0.0.5"
|
||||
version = "0.0.6"
|
||||
dependencies = [
|
||||
"sea-orm",
|
||||
"sea-orm-migration",
|
||||
|
||||
@@ -13,4 +13,5 @@ A library to enhance SeaORM
|
||||
- example: `cargo run --example=migrations --features=sqlite`
|
||||
- fmt: `cargo fmt --check`
|
||||
- docs: `RUSTDOCFLAGS="--cfg docsrs" cargo +nightly doc --all-features`
|
||||
- semver: `cargo semver-checks --all-features`
|
||||
- publish: `cargo publish --dry-run -p seamantic`
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "seamantic"
|
||||
version = "0.0.5"
|
||||
version = "0.0.6"
|
||||
|
||||
categories = []
|
||||
description = "A library to enhance SeaORM"
|
||||
|
||||
@@ -6,6 +6,7 @@ pub use sea_orm;
|
||||
pub use sea_orm_migration;
|
||||
|
||||
pub mod model;
|
||||
pub mod orm;
|
||||
pub mod schema;
|
||||
|
||||
/// A macro for defining a Migrator with a custom migration table while
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
//! Helpers for working with SeaORM
|
||||
|
||||
mod upsert;
|
||||
pub use upsert::UpsertTrait;
|
||||
@@ -0,0 +1,34 @@
|
||||
use sea_orm::sea_query::{IntoColumnRef, OnConflict};
|
||||
use sea_orm::{ActiveModelTrait, EntityTrait, Insert, Iterable};
|
||||
|
||||
/// This trait add a method on [Insert] to allow for upsert behavior
|
||||
pub trait UpsertTrait: private::Sealed {
|
||||
/// Set ON CONFLICT on primary key to update all other columns
|
||||
fn on_conflict_upsert(self) -> Self;
|
||||
}
|
||||
|
||||
fn primary_key_iter<A: ActiveModelTrait>()
|
||||
-> impl Iterator<Item = <A::Entity as EntityTrait>::PrimaryKey> {
|
||||
<A::Entity as EntityTrait>::PrimaryKey::iter()
|
||||
}
|
||||
|
||||
fn column_iter<A: ActiveModelTrait>() -> impl Iterator<Item = <A::Entity as EntityTrait>::Column> {
|
||||
<<A as ActiveModelTrait>::Entity as EntityTrait>::Column::iter()
|
||||
}
|
||||
|
||||
impl<A: ActiveModelTrait> UpsertTrait for Insert<A> {
|
||||
fn on_conflict_upsert(self) -> Self {
|
||||
self.on_conflict(
|
||||
OnConflict::columns(primary_key_iter::<A>())
|
||||
.update_columns(column_iter::<A>().filter(|col| {
|
||||
!primary_key_iter::<A>().any(|pk| pk.into_column_ref() == col.into_column_ref())
|
||||
}))
|
||||
.to_owned(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
mod private {
|
||||
pub trait Sealed {}
|
||||
impl<A: ::sea_orm::ActiveModelTrait> Sealed for ::sea_orm::Insert<A> {}
|
||||
}
|
||||
Reference in New Issue
Block a user