Giter Club home page Giter Club logo

Comments (4)

fMeow avatar fMeow commented on August 17, 2024

Could you please explain the idea a little bit, or give an example? I have no idea what the proposed trait is used for.

from arangors.

LucaCoduriV avatar LucaCoduriV commented on August 17, 2024

Yes, sorry, I have written this with my phone.

Here is an example:

trait DbClient {
    async fn aql_query<R>(&self, aql: AqlQuery<'_>) -> Result<Vec<R>, ClientError>
        where R: DeserializeOwned + Send;
}

impl<T> DbClient for Database<T>
    where T: ClientExt + Send {
    async fn aql_query<R>(&self, aql: AqlQuery<'_>) -> Result<Vec<R>, ClientError>
        where R: DeserializeOwned + Send {
        self.aql_query(aql).await
    }
}

impl<T> DbClient for Transaction<T>
    where T: ClientExt + Send {
    async fn aql_query<R>(&self, aql: AqlQuery<'_>) -> Result<Vec<R>, ClientError>
        where R: DeserializeOwned + Send {
        self.aql_query(aql).await
    }
}
impl<T> UserDAO for T
where T: DbClient + Send + Sync{
    async fn add_user(&self, user: User) -> Result<User, DAOError> {
        let json = serde_json::value::to_value(&user).unwrap();

    let aql = AqlQuery::builder()
        .query(r#"
                INSERT @user INTO @@collection
                LET result = NEW
                RETURN {_id:result._id, _key:result._key, _rev:result._rev}
                "#)
        .bind_var("user", json)
        .bind_var("@collection", USERS_COLLECTION)
        .build();

    let resp: Vec<DocumentHeader> = db.aql_query(aql).await?;

    if let Some(header) = resp.into_iter().next() {
        user.document_header = Some(header);
        return Ok(user);
    }
    Err("Response was empty".into())
    }
}

As you can see, I can now use my add_user function on Database struct and Transaction struct.
Maybe there is an other way to do it but I did not find any better way.

from arangors.

LucaCoduriV avatar LucaCoduriV commented on August 17, 2024

?

from arangors.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.