mongo_data/repository/
base_repository.rs

1use crate::document::document::BaseDocument;
2use crate::document::pageable::{PageableRequest, PageableResponse};
3use async_trait::async_trait;
4use bson::oid::ObjectId;
5use bson::Document;
6use mongodb::error::Error;
7use mongodb::options::{
8    DeleteOptions, FindOneAndDeleteOptions, FindOneOptions, FindOptions, InsertManyOptions,
9};
10use opentelemetry::Context;
11use std::fmt::Debug;
12
13/// Base repository trait.
14/// The context passed here is open telemetry context which is useful for telemetry/monitoring purposes
15#[async_trait]
16pub trait Repository<
17    T: BaseDocument + Debug + serde::ser::Serialize + for<'de> serde::Deserialize<'de>,
18>
19{
20    /// Save a new document
21    async fn save(&self, entity: T, ctx: Option<&Context>) -> Result<ObjectId, Error>;
22
23    /// Save many documents
24    async fn save_many(
25        &self,
26        entities: Vec<T>,
27        ctx: Option<&Context>,
28        options: Option<InsertManyOptions>,
29    ) -> Result<Vec<ObjectId>, Error>;
30
31    /// Get all documents from a collection (pageable)
32    async fn get_all_pageable(
33        &self,
34        request: PageableRequest,
35        ctx: Option<&Context>,
36    ) -> Result<PageableResponse<T>, Error>;
37
38    /// Get the number of documents in a collection
39    async fn count_documents_in_collection(&self, ctx: Option<&Context>) -> Result<u64, Error>;
40
41    /// find a document by ObjectId hex(string)
42    async fn find_by_id(&self, id: &str, ctx: Option<&Context>) -> Result<T, Error>;
43
44    /// find a document by ObjectId
45    async fn find_by_raw_id(&self, id: ObjectId, ctx: Option<&Context>) -> Result<T, Error>;
46
47    /// Find documents by a list of ObjectId hex(string)
48    async fn find_by_ids(&self, ids: Vec<String>, ctx: Option<&Context>) -> Result<Vec<T>, Error>;
49
50    /// Find documents by a list of ObjectIds
51    async fn find_by_raw_ids(
52        &self,
53        pids: Vec<ObjectId>,
54        ctx: Option<&Context>,
55    ) -> Result<Vec<T>, Error>;
56
57    /// Find 1 document by a user defined filter
58    async fn find_by_filter_and_options(
59        &self,
60        filter: Document,
61        options: Option<FindOneOptions>,
62        ctx: Option<&Context>,
63    ) -> Result<T, Error>;
64
65    /// Find a list of documents by a user defined filter
66    async fn find_all_by_filter_and_options(
67        &self,
68        filter: Document,
69        options: Option<FindOptions>,
70        ctx: Option<&Context>,
71    ) -> Result<Vec<T>, Error>;
72
73    /// Update an existing document
74    async fn update(&self, document: &T, ctx: Option<&Context>) -> Result<ObjectId, Error>;
75
76    /// Delete an existing document by ObjectID
77    async fn delete_by_id(&self, id: ObjectId, ctx: Option<&Context>) -> Result<(), Error>;
78
79    /// Delete a list existing documents by ObjectIDs
80    async fn delete_by_ids(&self, ids: Vec<ObjectId>, ctx: Option<&Context>) -> Result<(), Error>;
81
82    /// Delete an existing document by a user filter
83    async fn delete(
84        &self,
85        filter: Document,
86        ctx: Option<&Context>,
87        options: Option<FindOneAndDeleteOptions>,
88    ) -> Result<(), Error>;
89
90    /// Delete existing documents by a user defined filter
91    async fn delete_many(
92        &self,
93        filter: Document,
94        ctx: Option<&Context>,
95        options: Option<DeleteOptions>,
96    ) -> Result<(), Error>;
97}