Skip to main content

mongodb/action/
session.rs

1use crate::{
2    bson::Timestamp,
3    client::options::{SessionOptions, TransactionOptions},
4    error::Result,
5    Client,
6    ClientSession,
7};
8
9use super::{action_impl, deeplink, export_doc, option_setters, options_doc};
10
11impl Client {
12    /// Starts a new [`ClientSession`].
13    ///
14    /// `await` will return d[`Result<ClientSession>`].
15    #[deeplink]
16    #[options_doc(start_session)]
17    pub fn start_session(&self) -> StartSession<'_> {
18        StartSession {
19            client: self,
20            options: None,
21        }
22    }
23}
24
25#[cfg(feature = "sync")]
26impl crate::sync::Client {
27    /// Starts a new [`ClientSession`].
28    ///
29    /// [run](StartSession::run) will return d[`Result<crate::sync::ClientSession>`].
30    #[deeplink]
31    #[options_doc(start_session, "run")]
32    pub fn start_session(&self) -> StartSession<'_> {
33        self.async_client.start_session()
34    }
35}
36
37/// Starts a new [`ClientSession`].  Construct with [`Client::start_session`].
38#[must_use]
39pub struct StartSession<'a> {
40    client: &'a Client,
41    options: Option<SessionOptions>,
42}
43
44#[option_setters(crate::client::options::SessionOptions)]
45#[export_doc(start_session)]
46impl StartSession<'_> {}
47
48#[action_impl(sync = crate::sync::ClientSession)]
49impl<'a> Action for StartSession<'a> {
50    type Future = StartSessionFuture;
51
52    async fn execute(self) -> Result<ClientSession> {
53        if let Some(options) = &self.options {
54            options.validate()?;
55        }
56        Ok(ClientSession::new(self.client.clone(), self.options, false).await)
57    }
58}