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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use serde::de::DeserializeOwned;
use serde::Serialize;
use crate::db::transaction::Transaction;
use crate::query::{Query, RecordQueryResult};
use crate::transaction::TransactionBuilder;
use crate::{DatabaseAccess, DatabaseConnectionPool, DatabaseRecord, ServiceError};
/// 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`]: struct.DatabaseRecord.html
#[maybe_async::maybe_async]
pub trait Record: DeserializeOwned + Serialize + Clone {
/// Finds a document in database from its unique key.
/// Simple wrapper for [`DatabaseRecord`]<`T`>::[`find`]
///
/// [`DatabaseRecord`]: struct.DatabaseRecord.html
/// [`find`]: struct.DatabaseRecord.html#method.find
async fn find<D>(key: &str, db_accessor: &D) -> Result<DatabaseRecord<Self>, ServiceError>
where
D: DatabaseAccess,
{
DatabaseRecord::find(key, db_accessor).await
}
/// Finds all documents in database matching a `Query`.
/// Simple wrapper for [`DatabaseRecord`]<`T`>::[`get`]
///
/// [`DatabaseRecord`]: struct.DatabaseRecord.html
/// [`get`]: struct.DatabaseRecord.html#method.get
async fn get<D>(query: Query, db_accessor: &D) -> Result<RecordQueryResult<Self>, ServiceError>
where
D: DatabaseAccess,
{
DatabaseRecord::get(query, db_accessor).await
}
/// Returns true if there are any document in database matching a `Query`.
/// Simple wrapper for [`DatabaseRecord`]<`T`>::[`exists`]
///
/// [`DatabaseRecord`]: struct.DatabaseRecord.html
/// [`exists`]: struct.DatabaseRecord.html#method.exists
async fn exists<D>(query: Query, db_accessor: &D) -> bool
where
D: DatabaseAccess,
{
DatabaseRecord::<Self>::exists(query, db_accessor).await
}
/// Creates a new `Query` instance for `Self`.
///
/// # Example
/// ```rust
/// # use aragog::query::Query;
/// # use aragog::Record;
/// # use serde::{Serialize, Deserialize};
/// #[derive(Record, Clone, Serialize, Deserialize)]
/// pub struct User { }
///
/// // All three statements are equivalent:
/// let q = User::query();
/// let q = Query::new(User::collection_name());
/// let q = Query::new("User");
/// ```
fn query() -> Query {
Query::new(Self::collection_name())
}
/// returns the associated Collection
/// for read and write operations.
fn collection_name() -> &'static str;
/// method called by [`DatabaseRecord`]::[`create`]
/// before the database operation.
///
/// Define hooks manually or with macros (see the book)
///
/// [`DatabaseRecord`]: struct.DatabaseRecord.html
/// [`create`]: struct.DatabaseRecored.html#method.create
async fn before_create_hook<D>(&mut self, db_accessor: &D) -> Result<(), ServiceError>
where
D: DatabaseAccess;
/// method called by [`DatabaseRecord`]::[`save`]
/// before the database operation.
///
/// Define hooks manually or with macros (see the book)
///
/// [`DatabaseRecord`]: struct.DatabaseRecord.html
/// [`save`]: struct.DatabaseRecored.html#method.save
async fn before_save_hook<D>(&mut self, db_accessor: &D) -> Result<(), ServiceError>
where
D: DatabaseAccess;
/// method called by [`DatabaseRecord`]::[`delete`]
/// before the database operation.
///
/// Define hooks manually or with macros (see the book)
///
/// [`DatabaseRecord`]: struct.DatabaseRecord.html
/// [`delete`]: struct.DatabaseRecored.html#method.delete
async fn before_delete_hook<D>(&mut self, db_accessor: &D) -> Result<(), ServiceError>
where
D: DatabaseAccess;
/// method called automatically by [`DatabaseRecord`]::[`create`]
/// after the database operation.
///
/// Define hooks manually or with macros (see the book)
///
/// [`DatabaseRecord`]: struct.DatabaseRecord.html
/// [`create`]: struct.DatabaseRecored.html#method.create
async fn after_create_hook<D>(&mut self, db_accessor: &D) -> Result<(), ServiceError>
where
D: DatabaseAccess;
/// method called automatically by [`DatabaseRecord`]::[`save`]
/// after the database operation.
///
/// Define hooks manually or with macros (see the book)
///
/// [`DatabaseRecord`]: struct.DatabaseRecord.html
/// [`save`]: struct.DatabaseRecored.html#method.save
async fn after_save_hook<D>(&mut self, db_accessor: &D) -> Result<(), ServiceError>
where
D: DatabaseAccess;
/// method called automatically by [`DatabaseRecord`]::[`delete`]
/// after the database operation.
///
/// Define hooks manually or with macros (see the book)
///
/// [`DatabaseRecord`]: struct.DatabaseRecord.html
/// [`delete`]: struct.DatabaseRecored.html#method.delete
async fn after_delete_hook<D>(&mut self, db_accessor: &D) -> Result<(), ServiceError>
where
D: DatabaseAccess;
/// Returns a transaction builder on this collection only.
fn transaction_builder() -> TransactionBuilder {
TransactionBuilder::new().collections(vec![Self::collection_name().to_string()])
}
/// Builds a transaction for this collection only.
///
/// # Arguments
///
/// * `db_pool` - The current database connection pool
async fn transaction(db_pool: &DatabaseConnectionPool) -> Result<Transaction, ServiceError> {
Self::transaction_builder().build(db_pool).await
}
}