soph_database/support/
instance.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use crate::{
    async_trait,
    error::Error,
    traits::{ErrorTrait, InstanceTrait},
    Database, Result,
};
use soph_core::{
    error::ContainerError,
    support::{app, Container},
};

#[async_trait]
impl InstanceTrait for Database {
    type Error = Error;

    async fn register(_: &Container) -> Result<Self, ContainerError>
    where
        Self: Sized,
    {
        Ok(Self::new().await.map_err(Error::wrap)?)
    }

    async fn boot() -> Result<(), ContainerError>
    where
        Self: Sized,
    {
        let database = app().resolve::<Self>()?;

        database.ping().await.map_err(Error::wrap)?;

        Ok(())
    }
}