mongo_data/repository/
base_repository.rs1use 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#[async_trait]
16pub trait Repository<
17 T: BaseDocument + Debug + serde::ser::Serialize + for<'de> serde::Deserialize<'de>,
18>
19{
20 async fn save(&self, entity: T, ctx: Option<&Context>) -> Result<ObjectId, Error>;
22
23 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 async fn get_all_pageable(
33 &self,
34 request: PageableRequest,
35 ctx: Option<&Context>,
36 ) -> Result<PageableResponse<T>, Error>;
37
38 async fn count_documents_in_collection(&self, ctx: Option<&Context>) -> Result<u64, Error>;
40
41 async fn find_by_id(&self, id: &str, ctx: Option<&Context>) -> Result<T, Error>;
43
44 async fn find_by_raw_id(&self, id: ObjectId, ctx: Option<&Context>) -> Result<T, Error>;
46
47 async fn find_by_ids(&self, ids: Vec<String>, ctx: Option<&Context>) -> Result<Vec<T>, Error>;
49
50 async fn find_by_raw_ids(
52 &self,
53 pids: Vec<ObjectId>,
54 ctx: Option<&Context>,
55 ) -> Result<Vec<T>, Error>;
56
57 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 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 async fn update(&self, document: &T, ctx: Option<&Context>) -> Result<ObjectId, Error>;
75
76 async fn delete_by_id(&self, id: ObjectId, ctx: Option<&Context>) -> Result<(), Error>;
78
79 async fn delete_by_ids(&self, ids: Vec<ObjectId>, ctx: Option<&Context>) -> Result<(), Error>;
81
82 async fn delete(
84 &self,
85 filter: Document,
86 ctx: Option<&Context>,
87 options: Option<FindOneAndDeleteOptions>,
88 ) -> Result<(), Error>;
89
90 async fn delete_many(
92 &self,
93 filter: Document,
94 ctx: Option<&Context>,
95 options: Option<DeleteOptions>,
96 ) -> Result<(), Error>;
97}