pub enum PKeyPolicy<T> {
Fixed(T),
Generate,
}
Expand description
Helper struct to use with functions that insert data into the database.
§Examples
§Usage when inserting to a database
By calling .into_uuid()
function implemented by PKeyPolicy<Uuid>
, this enum can be used with
SQLX queries while letting the caller dictate how the primary key should be decided.
async fn insert(
conn: &mut PgConnection,
pkey_policy: PKeyPolicy<Uuid>,
) -> ModelResult<Uuid> {
let res = sqlx::query!(
"INSERT INTO organizations (id) VALUES ($1) RETURNING id",
pkey_policy.into_uuid(),
)
.fetch_one(conn)
.await?;
Ok(res.id)
}
// Insert using generated id.
let foo_1_id = insert(conn, PKeyPolicy::Generate).await.unwrap();
// Insert using fixed id.
let uuid = Uuid::parse_str("8fce44cf-738e-4fc9-8d8e-47c350fd3a7f").unwrap();
let foo_2_id = insert(conn, PKeyPolicy::Fixed(uuid)).await.unwrap();
assert_eq!(foo_2_id, uuid);
§Usage in a higher-order function.
When PKeyPolicy
is used with a higher-order function, an arbitrary struct can be provided
instead. The data can be mapped further by calling the .map()
or .map_ref()
methods.
struct FooBar {
foo: Uuid,
bar: Uuid,
}
async fn multiple_inserts(
conn: &mut PgConnection,
pkey_policy: PKeyPolicy<FooBar>,
) -> ModelResult<()> {
foos::insert(conn, pkey_policy.map_ref(|x| x.foo)).await?;
bars::insert(conn, pkey_policy.map_ref(|x| x.bar)).await?;
Ok(())
}
// Insert using generated ids.
assert!(multiple_inserts(conn, PKeyPolicy::Generate).await.is_ok());
// Insert using fixed ids.
let foobar = FooBar {
foo: Uuid::parse_str("52760668-cc9d-4144-9226-d2aacb83bea9").unwrap(),
bar: Uuid::parse_str("ce9bd0cd-0e66-4522-a1b4-52a9347a115c").unwrap(),
};
assert!(multiple_inserts(conn, PKeyPolicy::Fixed(foobar)).await.is_ok());
Variants§
Fixed(T)
Ids will be generated based on the associated data. Usually only used in local test environments where reproducible database states are desired.
Generate
Ids will be generated on the database level. This should be the default behavior.
Implementations§
Source§impl<T> PKeyPolicy<T>
impl<T> PKeyPolicy<T>
Sourcepub fn map<U, F>(self, f: F) -> PKeyPolicy<U>where
F: FnOnce(T) -> U,
pub fn map<U, F>(self, f: F) -> PKeyPolicy<U>where
F: FnOnce(T) -> U,
Maps PKeyPolicy<T>
to PKeyPolicy<U>
by applying a function to the contained value.
Sourcepub fn map_ref<U, F>(&self, f: F) -> PKeyPolicy<U>
pub fn map_ref<U, F>(&self, f: F) -> PKeyPolicy<U>
Maps a reference of contained data in Fixed(T)
to PKeyPolicy<U>
by applying a function
to the contained value. This is useful whenever a referenced value can be used instead of
having to consume the original value.
Auto Trait Implementations§
impl<T> Freeze for PKeyPolicy<T>where
T: Freeze,
impl<T> RefUnwindSafe for PKeyPolicy<T>where
T: RefUnwindSafe,
impl<T> Send for PKeyPolicy<T>where
T: Send,
impl<T> Sync for PKeyPolicy<T>where
T: Sync,
impl<T> Unpin for PKeyPolicy<T>where
T: Unpin,
impl<T> UnwindSafe for PKeyPolicy<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more