Struct aragog::DatabaseRecord
source · pub struct DatabaseRecord<T> {
pub record: T,
/* private fields */
}
Expand description
Fields§
§record: T
The deserialized stored document
Implementations§
source§impl<T: Record> DatabaseRecord<T>
impl<T: Record> DatabaseRecord<T>
sourcepub async fn create_with_options<D>(
record: T,
db_accessor: &D,
options: OperationOptions
) -> Result<Self, Error>where
D: DatabaseAccess + ?Sized,
pub async fn create_with_options<D>( record: T, db_accessor: &D, options: OperationOptions ) -> Result<Self, Error>where D: DatabaseAccess + ?Sized,
Creates a document in database. The function will write a new document and return a database record containing the newly created key
Note
This method should be used for very specific cases, prefer using create
instead.
If you want global operation options (always wait for sync, always ignore hooks, etc)
configure your DatabaseConnection
with with_operation_options
to have a customs set
of default options.
Hooks
This function will launch T
hooks before_create
and after_create
unless the options
argument disables hooks.
Arguments
record
- The document to create, it will be returned exactly as theDatabaseRecord<T>
recorddb_accessor
- database connection referenceoptions
- Operation options to apply
Returns
On success a new instance of Self
is returned, with the key
value filled and record
filled with the
argument value.
An Error
is returned if the operation or the hooks failed.
sourcepub async fn create_with_key_and_options<D>(
record: T,
key: String,
db_accessor: &D,
options: OperationOptions
) -> Result<Self, Error>where
D: DatabaseAccess + ?Sized,
pub async fn create_with_key_and_options<D>( record: T, key: String, db_accessor: &D, options: OperationOptions ) -> Result<Self, Error>where D: DatabaseAccess + ?Sized,
Creates a document in database with a custom key. The function will write a new document and return a database record containing the newly created key
Note
This method should be used for very specific cases, prefer using create_with_key
instead.
If you want global operation options (always wait for sync, always ignore hooks, etc)
configure your DatabaseConnection
with with_operation_options
to have a customs set
of default options.
Hooks
This function will launch T
hooks before_create
and after_create
unless the options
argument disables hooks.
Arguments
record
- The document to create, it will be returned exactly as theDatabaseRecord<T>
recordkey
- The custom key to applydb_accessor
- database connection referenceoptions
- Operation options to apply
Returns
On success a new instance of Self
is returned, with the key
value filled and record
filled with the
argument value.
An Error
is returned if the operation or the hooks failed.
sourcepub async fn create<D>(record: T, db_accessor: &D) -> Result<Self, Error>where
D: DatabaseAccess + ?Sized,
pub async fn create<D>(record: T, db_accessor: &D) -> Result<Self, Error>where D: DatabaseAccess + ?Sized,
Creates a document in database. The function will write a new document and return a database record containing the newly created key
Hooks
This function will launch T
hooks before_create
and after_create
unless the db_accessor
operations options specifically disable hooks.
Arguments
record
- The document to create, it will be returned exactly as theDatabaseRecord<T>
recorddb_accessor
- database connection reference
Returns
On success a new instance of Self
is returned, with the key
value filled and record
filled with the
argument value.
An Error
is returned if the operation or the hooks failed.
sourcepub async fn create_with_key<D>(
record: T,
key: String,
db_accessor: &D
) -> Result<Self, Error>where
D: DatabaseAccess + ?Sized,
pub async fn create_with_key<D>( record: T, key: String, db_accessor: &D ) -> Result<Self, Error>where D: DatabaseAccess + ?Sized,
Creates a document in database with a custom key. The function will write a new document and return a database record containing the newly created key
Hooks
This function will launch T
hooks before_create
and after_create
unless the db_accessor
operations options specifically disable hooks.
Arguments
record
- The document to create, it will be returned exactly as theDatabaseRecord<T>
recordkey
- the custom document keydb_accessor
- database connection reference
Returns
On success a new instance of Self
is returned, with the key
value filled and record
filled with the
argument value.
An Error
is returned if the operation or the hooks failed.
sourcepub async fn force_create<D>(record: T, db_accessor: &D) -> Result<Self, Error>where
D: DatabaseAccess + ?Sized,
pub async fn force_create<D>(record: T, db_accessor: &D) -> Result<Self, Error>where D: DatabaseAccess + ?Sized,
Creates a document in database. The function will write a new document and return a database record containing the newly created key.
Note
This function will override the default operations options:
- Revision will be ignored
- Hooks will be skipped and should be used sparingly.
Hooks
This function will skip all hooks.
Arguments
record
- The document to create, it will be returned exactly as theDatabaseRecord<T>
recorddb_accessor
- database connection reference
Returns
On success a new instance of Self
is returned, with the key
value filled and record
filled with the
argument value
On failure an Error
is returned.
sourcepub async fn save_with_options<D>(
&mut self,
db_accessor: &D,
options: OperationOptions
) -> Result<(), Error>where
D: DatabaseAccess + ?Sized,
pub async fn save_with_options<D>( &mut self, db_accessor: &D, options: OperationOptions ) -> Result<(), Error>where D: DatabaseAccess + ?Sized,
Writes in the database the new state of the record, “saving it”.
Note
This method should be used for very specific cases, prefer using save
instead.
If you want global operation options (always wait for sync, always ignore hooks, etc)
configure your DatabaseConnection
with with_operation_options
to have a customs set
of default options.
Hooks
This function will launch T
hooks before_save
and after_save
unless the options
argument disables hooks.
Arguments:
db_accessor
- database connection referenceoptions
- Operation options to apply
Returns
On success ()
is returned, meaning that the current instance is up to date with the database state.
An Error
is returned if the operation or the hooks failed.
sourcepub async fn save<D>(&mut self, db_accessor: &D) -> Result<(), Error>where
D: DatabaseAccess + ?Sized,
pub async fn save<D>(&mut self, db_accessor: &D) -> Result<(), Error>where D: DatabaseAccess + ?Sized,
Writes in the database the new state of the record, “saving it”.
Hooks
This function will launch T
hooks before_save
and after_save
unless the db_accessor
operations options specifically disable hooks.
Arguments:
db_accessor
- database connection reference
Returns
On success ()
is returned, meaning that the current instance is up to date with the database state.
An Error
is returned if the operation or the hooks failed.
sourcepub async fn force_save<D>(&mut self, db_accessor: &D) -> Result<(), Error>where
D: DatabaseAccess + ?Sized,
pub async fn force_save<D>(&mut self, db_accessor: &D) -> Result<(), Error>where D: DatabaseAccess + ?Sized,
Writes in the database the new state of the record.
Note
This function will override the default operations options:
- Revision will be ignored
- Hooks will be skipped and should be used sparingly.
Hooks
This function will skip all hooks.
Arguments:
db_accessor
- database connection reference
Returns
On success ()
is returned, meaning that the current instance is up to date with the database state.
On failure an Error
is returned.
sourcepub async fn delete_with_options<D>(
&mut self,
db_accessor: &D,
options: OperationOptions
) -> Result<(), Error>where
D: DatabaseAccess + ?Sized,
pub async fn delete_with_options<D>( &mut self, db_accessor: &D, options: OperationOptions ) -> Result<(), Error>where D: DatabaseAccess + ?Sized,
Removes the record from the database. The structure won’t be freed or emptied but the document won’t exist in the global state
Note
This method should be used for very specific cases, prefer using delete
instead.
If you want global operation options (always wait for sync, always ignore hooks, etc)
configure your DatabaseConnection
with with_operation_options
to have a customs set
of default options
Hooks
This function will launch T
hooks before_delete
and after_delete
unless the options
argument disables hooks.
Arguments:
db_accessor
- database connection reference
Returns
On success ()
is returned, meaning that the record is now deleted, the structure should not be used afterwards.
An Error
is returned if the operation or the hooks failed.
sourcepub async fn delete<D>(&mut self, db_accessor: &D) -> Result<(), Error>where
D: DatabaseAccess + ?Sized,
pub async fn delete<D>(&mut self, db_accessor: &D) -> Result<(), Error>where D: DatabaseAccess + ?Sized,
Removes the record from the database. The structure won’t be freed or emptied but the document won’t exist in the global state
Hooks
This function will launch T
hooks before_delete
and after_delete
unless the db_accessor
operations options specifically disable hooks.
Arguments:
db_accessor
- database connection reference
Returns
On success ()
is returned, meaning that the record is now deleted, the structure should not be used afterwards.
An Error
is returned if the operation or the hooks failed.
sourcepub async fn force_delete<D>(&mut self, db_accessor: &D) -> Result<(), Error>where
D: DatabaseAccess + ?Sized,
pub async fn force_delete<D>(&mut self, db_accessor: &D) -> Result<(), Error>where D: DatabaseAccess + ?Sized,
Removes the record from the database. The structure won’t be freed or emptied but the document won’t exist in the global state
Note
This function will override the default operations options:
- Revision will be ignored
- Hooks will be skipped and should be used sparingly.
Hooks
This function will skip all hooks.
Arguments:
db_accessor
- database connection reference
Returns
On success ()
is returned, meaning that the record is now deleted, the structure should not be used afterwards.
On failure an Error
is returned.
sourcepub async fn link<A, B, D>(
from_record: &DatabaseRecord<A>,
to_record: &DatabaseRecord<B>,
db_accessor: &D,
edge_record: T
) -> Result<DatabaseRecord<EdgeRecord<T>>, Error>where
A: Record,
B: Record,
D: DatabaseAccess + ?Sized,
T: Record + Send,
pub async fn link<A, B, D>( from_record: &DatabaseRecord<A>, to_record: &DatabaseRecord<B>, db_accessor: &D, edge_record: T ) -> Result<DatabaseRecord<EdgeRecord<T>>, Error>where A: Record, B: Record, D: DatabaseAccess + ?Sized, T: Record + Send,
Creates and returns edge between from_record
and target_record
.
Hooks
This function will launch T
hooks before_create
and after_create
.
Example
#[derive(Clone, Record, Serialize, Deserialize)]
struct Edge {
description: String,
}
let user_a = DatabaseRecord::create(User { }, &db_accessor).await.unwrap();
let user_b = DatabaseRecord::create(User { }, &db_accessor).await.unwrap();
let edge = DatabaseRecord::link(&user_a, &user_b, &db_accessor,
Edge { description: "description".to_string() }
).await.unwrap();
assert_eq!(edge.id_from(), user_a.id());
assert_eq!(edge.id_to(), user_b.id());
assert_eq!(&edge.description, "description");
sourcepub async fn find<D>(key: &str, db_accessor: &D) -> Result<Self, Error>where
D: DatabaseAccess + ?Sized,
pub async fn find<D>(key: &str, db_accessor: &D) -> Result<Self, Error>where D: DatabaseAccess + ?Sized,
Retrieves a record from the database with the associated unique key
Arguments:
key
- the unique record key as a string slicedb_accessor
- database connection reference
Returns
On success Self
is returned,
On failure an Error
is returned:
NotFound
on invalid document keyUnprocessableEntity
on data corruption
sourcepub async fn reload<D>(self, db_accessor: &D) -> Result<Self, Error>where
D: DatabaseAccess + ?Sized,
T: Send,
pub async fn reload<D>(self, db_accessor: &D) -> Result<Self, Error>where D: DatabaseAccess + ?Sized, T: Send,
Reloads a record from the database, returning the new record.
Arguments
db_accessor
- database connection reference
Returns
On success Self
is returned,
On failure an Error
is returned:
NotFound
on invalid document keyUnprocessableEntity
on data corruption
sourcepub async fn reload_mut<D>(&mut self, db_accessor: &D) -> Result<(), Error>where
D: DatabaseAccess + ?Sized,
T: Send,
pub async fn reload_mut<D>(&mut self, db_accessor: &D) -> Result<(), Error>where D: DatabaseAccess + ?Sized, T: Send,
Reloads a record from the database.
Returns
On success ()
is returned and self
is updated,
On failure an Error
is returned:
NotFound
on invalid document keyUnprocessableEntity
on data corruption
sourcepub async fn get<D>(
query: &Query,
db_accessor: &D
) -> Result<QueryResult<T>, Error>where
D: DatabaseAccess + ?Sized,
pub async fn get<D>( query: &Query, db_accessor: &D ) -> Result<QueryResult<T>, Error>where D: DatabaseAccess + ?Sized,
Retrieves all records from the database matching the associated conditions.
Arguments:
query
- TheQuery
to matchdb_accessor
- database connection reference
Note
This is simply an AQL request wrapper.
Returns
On success a QueryResult
with a vector of Self
is returned. It can be empty.
On failure an Error
is returned:
NotFound
if no document matches the conditionUnprocessableEntity
on data corruption
Example
let query = User::query().filter(Filter::new(Comparison::field("username").equals_str("RobertSurcouf"))
.and(Comparison::field("age").greater_than(10)));
// Both lines are equivalent:
DatabaseRecord::<User>::get(&query, &db_accessor).await.unwrap();
User::get(&query, &db_accessor).await.unwrap();
sourcepub async fn get_in_batches<D>(
query: &Query,
db_accessor: &D,
batch_size: u32
) -> Result<QueryCursor<T>, Error>where
D: DatabaseAccess + ?Sized,
pub async fn get_in_batches<D>( query: &Query, db_accessor: &D, batch_size: u32 ) -> Result<QueryCursor<T>, Error>where D: DatabaseAccess + ?Sized,
Retrieves all records from the database matching the associated conditions in batches.
Arguments:
query
- TheQuery
to matchdb_accessor
- database connection referencebatch_size
- The maximum number of documents in a batch
Returns
On success a QueryCursor
is returned. It can be empty.
On failure an Error
is returned:
NotFound
if no document matches the conditionUnprocessableEntity
on data corruption
Example
let query = User::query().filter(Filter::new(Comparison::field("age").greater_than(10)));
// Both lines are equivalent:
DatabaseRecord::<User>::get_in_batches(&query, &db_accessor, 100).await.unwrap();
User::get_in_batches(&query, &db_accessor, 100).await.unwrap();
sourcepub async fn aql_get<D>(
query: &str,
db_accessor: &D
) -> Result<QueryResult<T>, Error>where
D: DatabaseAccess + ?Sized,
pub async fn aql_get<D>( query: &str, db_accessor: &D ) -> Result<QueryResult<T>, Error>where D: DatabaseAccess + ?Sized,
Retrieves all records from the database matching the associated conditions.
Arguments:
query
- The AQL request stringdb_accessor
- database connection reference
Returns
On success a QueryResult
with a vector of Self
is returned. It is can be empty.
On failure an Error
is returned:
NotFound
if no document matches the conditionUnprocessableEntity
on data corruption
Warning
If you call this method on a graph query only the documents that can be serialized into T
will be returned.
Example
let query = r#"FOR i in User FILTER i.username == "RoertSurcouf" && i.age > 10 return i"#;
DatabaseRecord::<User>::aql_get(query, &db_accessor).await.unwrap();
sourcepub fn outbound_query(&self, min: u16, max: u16, edge_collection: &str) -> Query
pub fn outbound_query(&self, min: u16, max: u16, edge_collection: &str) -> Query
Creates a new outbound graph Query
with self
as a start vertex
Arguments
edge_collection
- The name of the queried edge collectionmin
- The minimum depth of the graph requestmax
- The maximum depth of the graph request
Example
let record = User::find("123", &db_accessor).await.unwrap();
// Both statements are equivalent
let q = record.outbound_query(1, 2, "ChildOf");
let q = Query::outbound(1, 2, "ChildOf", record.id());
sourcepub fn inbound_query(&self, min: u16, max: u16, edge_collection: &str) -> Query
pub fn inbound_query(&self, min: u16, max: u16, edge_collection: &str) -> Query
Creates a new inbound graph Query
with self
as a start vertex
Arguments
edge_collection
- The name of the queried edge collectionmin
- The minimum depth of the graph requestmax
- The maximum depth of the graph request
Example
let record = User::find("123", &db_accessor).await.unwrap();
// Both statements are equivalent
let q = record.inbound_query(1, 2, "ChildOf");
let q = Query::inbound(1, 2, "ChildOf", record.id());
sourcepub fn outbound_graph(&self, min: u16, max: u16, named_graph: &str) -> Query
pub fn outbound_graph(&self, min: u16, max: u16, named_graph: &str) -> Query
Creates a new outbound graph Query
with self
as a start vertex
Arguments
min
- The minimum depth of the graph requestmax
- The maximum depth of the graph requestnamed_graph
- The named graph to traverse
Example
let record = User::find("123", &db_accessor).await.unwrap();
// Both statements are equivalent
let q = record.outbound_graph(1, 2, "SomeGraph");
let q = Query::outbound_graph(1, 2, "SomeGraph", record.id());
sourcepub fn inbound_graph(&self, min: u16, max: u16, named_graph: &str) -> Query
pub fn inbound_graph(&self, min: u16, max: u16, named_graph: &str) -> Query
Creates a new inbound graph Query
with self
as a start vertex
Arguments
min
- The minimum depth of the graph requestmax
- The maximum depth of the graph requestnamed_graph
- The named graph to traverse
Example
let record = User::find("123", &db_accessor).await.unwrap();
// Both statements are equivalent
let q = record.inbound_graph(1, 2, "SomeGraph");
let q = Query::inbound_graph(1, 2, "SomeGraph", record.id());
sourcepub async fn exists<D>(query: &Query, db_accessor: &D) -> boolwhere
D: DatabaseAccess + ?Sized,
pub async fn exists<D>(query: &Query, db_accessor: &D) -> boolwhere D: DatabaseAccess + ?Sized,
Checks if any document matching the associated conditions exist
Arguments:
query
- TheQuery
to matchdb_accessor
- database connection reference
Note
This is simply an AQL request wrapper.
Returns
On success true
is returned, false
if nothing exists.
Example
let query = User::query().filter(
Filter::new(Comparison::field("username").equals_str("MichelDu93"))
.and(Comparison::field("age").greater_than(10)));
User::exists(&query, &db_accessor).await;
Trait Implementations§
source§impl<T: Clone> Clone for DatabaseRecord<T>
impl<T: Clone> Clone for DatabaseRecord<T>
source§fn clone(&self) -> DatabaseRecord<T>
fn clone(&self) -> DatabaseRecord<T>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more