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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use async_trait::async_trait;
use serde::de::DeserializeOwned;
use serde::Serialize;

use crate::{ServiceError, DatabaseConnectionPool, DatabaseRecord};
use crate::query::Query;

/// The main trait of the Aragog library.
/// Trait for structures that can be stored in Database.
/// The trait must be implemented to be used as a record in [`DatabaseRecord`]
///
/// [`DatabaseRecord`]: db/database_record/struct.DatabaseRecord.html
#[async_trait]
pub trait Record {
    /// Finds a document in database from its unique key.
    /// Simple wrapper for [`DatabaseRecord`]<`T`>::[`find`]
    ///
    /// [`DatabaseRecord`]: db/database_record/struct.DatabaseRecord.html
    /// [`find`]: db/database_record/struct.DatabaseRecord.html#method.find
    async fn find(key: &str, db_pool: &DatabaseConnectionPool)
                  -> Result<DatabaseRecord<Self>, ServiceError>
        where Self: DeserializeOwned + Serialize + Clone {
        DatabaseRecord::find(key, &db_pool).await
    }

    /// Finds a document in database matching a `Query`.
    /// Simple wrapper for [`DatabaseRecord`]<`T`>::[`find_where`]
    ///
    /// [`DatabaseRecord`]: db/database_record/struct.DatabaseRecord.html
    /// [`find_where`]: db/database_record/struct.DatabaseRecord.html#method.find_where
    async fn find_where(query: Query, db_pool: &DatabaseConnectionPool)
                           -> Result<DatabaseRecord<Self>, ServiceError>
        where Self: DeserializeOwned + Serialize + Clone {
        DatabaseRecord::find_where(query, &db_pool).await
    }

    /// Finds all documents in database matching a `Query`.
    /// Simple wrapper for [`DatabaseRecord`]<`T`>::[`get_where`]
    ///
    /// [`DatabaseRecord`]: db/database_record/struct.DatabaseRecord.html
    /// [`get_where`]: db/database_record/struct.DatabaseRecord.html#method.get_where
    async fn get_where(query: Query, db_pool: &DatabaseConnectionPool)
                          -> Result<Vec<DatabaseRecord<Self>>, ServiceError>
        where Self: DeserializeOwned + Serialize + Clone {
        DatabaseRecord::get_where(query, &db_pool).await
    }

    /// Returns true if there are any document in database matching a `Query`.
    /// Simple wrapper for [`DatabaseRecord`]<`T`>::[`exists_where`]
    ///
    /// [`DatabaseRecord`]: db/database_record/struct.DatabaseRecord.html
    /// [`exists_where`]: db/database_record/struct.DatabaseRecord.html#method.exists_where
    async fn exists_where(query: Query, db_pool: &DatabaseConnectionPool) -> bool
        where Self: DeserializeOwned + Serialize + Clone
    {
        DatabaseRecord::<Self>::exists_where(query, &db_pool).await
    }

    /// returns the associated Collection
    /// for read and write operations.
    fn collection_name() -> &'static str;
}