rpc_rs/
macros.rs

1//! Macros for `rpc-rs`.
2
3/// A macro to create a module for a prisma-client-rust module.
4/// Your client must be an `Arc<...>`, as the state of the router.
5///
6/// Params:
7/// - `client` - The path to the `PrismaClient` struct.
8/// - `module` - The path to the module inside the generated `prisma` mod.
9/// - `container` - The module's name. If you use `client.xxx.create(...)`, the container would be `xxx`.
10/// - `primary_key` - The name of the model's primary key.
11#[cfg(feature = "prisma")]
12#[macro_export]
13macro_rules! prisma_single_module {
14    {
15        client: $client: ty,
16        module: $module: ident,
17        container: $cont: ident,
18        primary_key: $pkey: ident,
19    } => {
20        Module::builder()
21                .read(wrap(|cx: Arc<$client>, item_id: i32| async move {
22                    cx.$cont()
23                        .find_first(vec![$module::$pkey::equals(item_id)])
24                        .exec()
25                        .await
26                        .unwrap_or_default()
27                }))
28                .create(wrap(
29                    |cx: Arc<$client>, item: $module::CreateUnchecked| async move {
30                        cx.$cont()
31                            .create_many(vec![item])
32                            .exec()
33                            .await
34                            .map_err(|v| v.to_string())
35                    },
36                ))
37                .update(wrap(
38                    |cx: Arc<$client>, update: $module::Update| async move {
39                        cx.$cont()
40                            .update($module::$pkey::equals(update.id), update.as_params())
41                            .exec()
42                            .await
43                            .map_err(|v| v.to_string())
44                    },
45                ))
46                .delete(wrap(
47                    |cx: Arc<$client>, item_id: i32| async move {
48                        cx.$cont()
49                            .delete($module::$pkey::equals(item_id))
50                            .exec()
51                            .await
52                            .map_err(|v| v.to_string())
53                    },
54                ))
55                .build()
56    };
57}
58
59/// A macro to create a module for a prisma-client-rust module.
60/// Your client must be an `Arc<...>`, as the state of the router.
61/// This will work with a `Vec<...>` of the model.
62///
63/// Params:
64/// - `client` - The path to the `PrismaClient` struct.
65/// - `module` - The path to the module inside the generated `prisma` mod.
66/// - `container` - The module's name. If you use `client.xxx.create(...)`, the container would be `xxx`.
67/// - `primary_key` - The name of the model's primary key.
68#[cfg(feature = "prisma")]
69#[macro_export]
70macro_rules! prisma_multi_module {
71    {
72        client: $client: ty,
73        module: $module: ident,
74        container: $cont: ident,
75        primary_key: $pkey: ident,
76    } => {
77        Module::builder()
78                .read(wrap(|cx: Arc<$client>, _: ()| async move {
79                    cx.$cont()
80                        .find_many(vec![])
81                        .exec()
82                        .await
83                        .unwrap_or_default()
84                }))
85                .create(wrap(
86                    |cx: Arc<$client>, m: Vec<$module::CreateUnchecked>| async move {
87                        cx.$cont()
88                            .create_many(m)
89                            .exec()
90                            .await
91                            .map_err(|v| v.to_string())
92                    },
93                ))
94                .delete(wrap(|cx: Arc<$client>, ids: Vec<i32>| async move {
95                    cx.$cont()
96                        .delete_many(ids.iter().map(|v| $module::$pkey::equals(v.clone())).collect())
97                        .exec()
98                        .await
99                        .map_err(|v| v.to_string())
100                }))
101                .build()
102    };
103}
104
105/// A macro to create a module for a prisma-client-rust module.
106/// Your client must be an `Arc<...>`, as the state of the router.
107///
108/// Params:
109/// - `router` - The variable containing router (must be `mut`).
110/// - `single` - The name for the single route.
111/// - `multi` - The name for the multi route.
112/// - `client` - The path to the `PrismaClient` struct.
113/// - `module` - The path to the module inside the generated `prisma` mod.
114/// - `container` - The module's name. If you use `client.xxx.create(...)`, the container would be `xxx`.
115/// - `primary_key` - The name of the model's primary key.
116#[cfg(feature = "prisma")]
117#[macro_export]
118macro_rules! prisma_module {
119    ($router: ident += [$single: expr, $multi: expr] {
120        client: $client: ty,
121        module: $module: ident,
122        container: $cont: ident,
123        primary_key: $pkey: ident,
124    }) => {
125        $router = $router.mount(
126            $single,
127            $crate::prisma_single_module! {
128                client: $client,
129                module: $module,
130                container: $cont,
131                primary_key: $pkey,
132            },
133        );
134
135        $router = $router.mount(
136            $multi,
137            $crate::prisma_multi_module! {
138                client: $client,
139                module: $module,
140                container: $cont,
141                primary_key: $pkey,
142            },
143        );
144    };
145}