Skip to main content

nestrs_prisma/
client.rs

1//! Shared types for the declarative `prisma_model!` client (sort order, repository handle).
2
3#[cfg(feature = "sqlx")]
4use std::marker::PhantomData;
5#[cfg(feature = "sqlx")]
6use std::sync::Arc;
7
8#[cfg(feature = "sqlx")]
9use crate::PrismaService;
10
11/// SQL `ASC` / `DESC` (used by generated `order` helpers).
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub enum SortOrder {
14    Asc,
15    Desc,
16}
17
18impl SortOrder {
19    pub fn as_sql(self) -> &'static str {
20        match self {
21            SortOrder::Asc => "ASC",
22            SortOrder::Desc => "DESC",
23        }
24    }
25}
26
27/// Per-model repository bound to a table (constructed via the `prisma_model!` extension trait on [`Arc<PrismaService>`]).
28#[cfg(feature = "sqlx")]
29pub struct ModelRepository<M> {
30    /// Keeps [`PrismaService`] (and thus pool options) alive for the lifetime of this handle.
31    #[allow(dead_code)]
32    pub(crate) prisma: Arc<PrismaService>,
33    pub(crate) _marker: PhantomData<M>,
34}
35
36#[cfg(feature = "sqlx")]
37impl<M> ModelRepository<M> {
38    /// Wraps a [`PrismaService`] for typed queries (normally constructed via `prisma_model!` extension traits).
39    pub fn new(prisma: Arc<PrismaService>) -> Self {
40        Self {
41            prisma,
42            _marker: PhantomData,
43        }
44    }
45
46    /// Accesses the underlying shared [`PrismaService`] handle.
47    pub fn prisma(&self) -> Arc<PrismaService> {
48        Arc::clone(&self.prisma)
49    }
50}