Skip to content
This repository was archived by the owner on Mar 21, 2025. It is now read-only.

Commit 357a3f4

Browse files
authored
add extended traits (#238)
1 parent e024546 commit 357a3f4

33 files changed

+671
-780
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ rspc = ["dep:rspc"]
2525
sqlite-create-many = ["psl/sqlite-create-many"]
2626
migrations = ["migration-core", "include_dir", "tempdir", "tokio/fs", "tracing"]
2727
mocking = ["tokio"]
28-
mutation-callbacks = []
28+
# mutation-callbacks = []
2929

3030
mysql = ["query-core/mysql", "migration-core/mysql"]
3131
sqlite = ["query-core/sqlite", "migration-core/sqlite"]

cli/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ default = ["mysql", "sqlite", "mssql", "postgresql", "mongodb"]
1010
rspc = []
1111
migrations = []
1212
sqlite-create-many = ["prisma-client-rust-sdk/sqlite-create-many"]
13-
mutation-callbacks = []
13+
# mutation-callbacks = []
1414
mocking = []
1515

1616
mysql = ["prisma-client-rust-sdk/mysql"]

cli/src/generator/client.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ pub fn generate(args: &GenerateArgs) -> TokenStream {
5555

5656
let mock_ctor = cfg!(feature = "mocking").then(|| {
5757
quote! {
58-
pub async fn _mock() -> (Self, #pcr::MockStore) {
59-
let (internals, store) = #pcr::PrismaClientInternals::new_mock(#pcr::ActionNotifier::new()).await;
58+
pub fn _mock() -> (Self, #pcr::MockStore) {
59+
let (internals, store) = #pcr::PrismaClientInternals::new_mock(#pcr::ActionNotifier::new());
6060

6161
(Self(internals), store)
6262
}
@@ -111,7 +111,7 @@ pub fn generate(args: &GenerateArgs) -> TokenStream {
111111

112112
#mock_ctor
113113

114-
pub fn _query_raw<T: serde::de::DeserializeOwned>(&self, query: #pcr::Raw) -> #pcr::QueryRaw<T> {
114+
pub fn _query_raw<T: #pcr::Data>(&self, query: #pcr::Raw) -> #pcr::QueryRaw<T> {
115115
#pcr::QueryRaw::new(
116116
&self.0,
117117
query,
@@ -127,7 +127,7 @@ pub fn generate(args: &GenerateArgs) -> TokenStream {
127127
)
128128
}
129129

130-
pub async fn _batch<T: #pcr::BatchContainer<Marker>, Marker>(&self, queries: T) -> #pcr::Result<T::ReturnType> {
130+
pub async fn _batch<'a, T: #pcr::BatchContainer<'a, Marker>, Marker>(&self, queries: T) -> #pcr::Result<<T as #pcr::BatchContainer<'a, Marker>>::ReturnType> {
131131
#pcr::batch(queries, &self.0).await
132132
}
133133

cli/src/generator/models/include_select.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ fn model_macro<'a>(
421421
let selection_struct = quote! {
422422
pub struct Selection(Vec<::prisma_client_rust::Selection>);
423423

424-
impl ::prisma_client_rust::#variant_ident::#selection_type for Selection {
424+
impl ::prisma_client_rust::#selection_type for Selection {
425425
type Data = Data;
426426
type ModelData = $crate::#module_path::#model_name_snake::Data;
427427

cli/src/generator/models/mod.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -291,17 +291,17 @@ pub fn generate(args: &GenerateArgs, module_path: TokenStream) -> Vec<TokenStrea
291291
pub type UniqueArgs = ::prisma_client_rust::UniqueArgs<Actions<'static>>;
292292
pub type ManyArgs = ::prisma_client_rust::ManyArgs<Actions<'static>>;
293293

294-
pub type Count<'a> = ::prisma_client_rust::Count<'a, Actions<'static>>;
295-
pub type Create<'a> = ::prisma_client_rust::Create<'a, Actions<'static>>;
296-
pub type CreateMany<'a> = ::prisma_client_rust::CreateMany<'a, Actions<'static>>;
297-
pub type FindUnique<'a> = ::prisma_client_rust::FindUnique<'a, Actions<'static>>;
298-
pub type FindMany<'a> = ::prisma_client_rust::FindMany<'a, Actions<'static>>;
299-
pub type FindFirst<'a> = ::prisma_client_rust::FindFirst<'a, Actions<'static>>;
300-
pub type Update<'a> = ::prisma_client_rust::Update<'a, Actions<'static>>;
301-
pub type UpdateMany<'a> = ::prisma_client_rust::UpdateMany<'a, Actions<'static>>;
302-
pub type Upsert<'a> = ::prisma_client_rust::Upsert<'a, Actions<'static>>;
303-
pub type Delete<'a> = ::prisma_client_rust::Delete<'a, Actions<'static>>;
304-
pub type DeleteMany<'a> = ::prisma_client_rust::DeleteMany<'a, Actions<'static>>;
294+
pub type Count<'a> = ::prisma_client_rust::Count<'a, Actions<'a>>;
295+
pub type Create<'a> = ::prisma_client_rust::Create<'a, Actions<'a>>;
296+
pub type CreateMany<'a> = ::prisma_client_rust::CreateMany<'a, Actions<'a>>;
297+
pub type FindUnique<'a> = ::prisma_client_rust::FindUnique<'a, Actions<'a>>;
298+
pub type FindMany<'a> = ::prisma_client_rust::FindMany<'a, Actions<'a>>;
299+
pub type FindFirst<'a> = ::prisma_client_rust::FindFirst<'a, Actions<'a>>;
300+
pub type Update<'a> = ::prisma_client_rust::Update<'a, Actions<'a>>;
301+
pub type UpdateMany<'a> = ::prisma_client_rust::UpdateMany<'a, Actions<'a>>;
302+
pub type Upsert<'a> = ::prisma_client_rust::Upsert<'a, Actions<'a>>;
303+
pub type Delete<'a> = ::prisma_client_rust::Delete<'a, Actions<'a>>;
304+
pub type DeleteMany<'a> = ::prisma_client_rust::DeleteMany<'a, Actions<'a>>;
305305

306306
#actions_struct
307307
}

integration-tests/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ prisma-client-rust = { path = "..", features = [
1818
"sqlite-create-many",
1919
"sqlite",
2020
"migrations",
21-
"mutation-callbacks",
21+
# "mutation-callbacks",
2222
"mocking",
2323
], default-features = false }

integration-tests/tests/count.rs

Lines changed: 19 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
11
use crate::db::*;
22
use crate::utils::*;
33

4-
#[tokio::test]
5-
async fn basic() -> TestResult {
6-
let client = client().await;
7-
4+
async fn create_posts(client: &PrismaClient) -> TestResult {
85
client
96
.post()
10-
.create("Hi from Prisma!".to_string(), true, vec![])
7+
.create_many(vec![
8+
post::create("Hi from Prisma!".to_string(), true, vec![]),
9+
post::create("Hi from Prisma!".to_string(), true, vec![]),
10+
post::create("Hi from Prisma!".to_string(), false, vec![]),
11+
])
1112
.exec()
1213
.await?;
1314

15+
Ok(())
16+
}
17+
18+
#[tokio::test]
19+
async fn basic() -> TestResult {
20+
let client = client().await;
21+
22+
create_posts(&client).await?;
23+
1424
let count = client.post().count(vec![]).exec().await?;
1525

16-
assert_eq!(count, 1);
26+
assert_eq!(count, 3);
1727

1828
cleanup(client).await
1929
}
@@ -37,23 +47,7 @@ async fn no_results() -> TestResult {
3747
async fn where_() -> TestResult {
3848
let client = client().await;
3949

40-
client
41-
.post()
42-
.create("Hi from Prisma!".to_string(), true, vec![])
43-
.exec()
44-
.await?;
45-
46-
client
47-
.post()
48-
.create("Hi from Prisma!".to_string(), false, vec![])
49-
.exec()
50-
.await?;
51-
52-
client
53-
.post()
54-
.create("Hi from Prisma!".to_string(), true, vec![])
55-
.exec()
56-
.await?;
50+
create_posts(&client).await?;
5751

5852
let published_count = client
5953
.post()
@@ -76,23 +70,7 @@ async fn where_() -> TestResult {
7670
async fn take() -> TestResult {
7771
let client = client().await;
7872

79-
client
80-
.post()
81-
.create("Hi from Prisma!".to_string(), true, vec![])
82-
.exec()
83-
.await?;
84-
85-
client
86-
.post()
87-
.create("Hi from Prisma!".to_string(), true, vec![])
88-
.exec()
89-
.await?;
90-
91-
client
92-
.post()
93-
.create("Hi from Prisma!".to_string(), true, vec![])
94-
.exec()
95-
.await?;
73+
create_posts(&client).await?;
9674

9775
let count = client.post().count(vec![]).take(1).exec().await?;
9876

@@ -105,23 +83,7 @@ async fn take() -> TestResult {
10583
async fn skip() -> TestResult {
10684
let client = client().await;
10785

108-
client
109-
.post()
110-
.create("Hi from Prisma!".to_string(), true, vec![])
111-
.exec()
112-
.await?;
113-
114-
client
115-
.post()
116-
.create("Hi from Prisma!".to_string(), true, vec![])
117-
.exec()
118-
.await?;
119-
120-
client
121-
.post()
122-
.create("Hi from Prisma!".to_string(), true, vec![])
123-
.exec()
124-
.await?;
86+
create_posts(&client).await?;
12587

12688
let count = client.post().count(vec![]).skip(1).exec().await?;
12789

integration-tests/tests/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ async fn aaaa_run_migrations() -> TestResult {
1919
}
2020

2121
mod batch;
22-
mod callbacks;
22+
// mod callbacks;
2323
mod count;
2424
mod create;
2525
mod create_many;

integration-tests/tests/mock.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::utils::*;
33

44
#[tokio::test]
55
async fn returns() -> TestResult {
6-
let (client, mock) = PrismaClient::_mock().await;
6+
let (client, mock) = PrismaClient::_mock();
77

88
user::select!(basic_user { id name });
99

@@ -31,7 +31,7 @@ async fn returns() -> TestResult {
3131

3232
#[tokio::test]
3333
async fn returns_many() -> TestResult {
34-
let (client, mock) = PrismaClient::_mock().await;
34+
let (client, mock) = PrismaClient::_mock();
3535

3636
user::select!(basic_user { id name });
3737

prisma-cli/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ prisma-client-rust-cli = { features = [
1111
"sqlite-create-many",
1212
"sqlite",
1313
"migrations",
14-
"mutation-callbacks",
14+
# "mutation-callbacks",
1515
"mocking"
1616
], default_features = false, path = "../cli" }

src/actions.rs

Lines changed: 2 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,12 @@
1-
use prisma_models::PrismaValue;
2-
use query_core::{Selection, SelectionArgument};
3-
use serde::de::DeserializeOwned;
4-
5-
use crate::SerializedWhereInput;
6-
7-
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
8-
pub enum ModelQueryType {
9-
FindUnique,
10-
FindFirst,
11-
FindMany,
12-
Count,
13-
}
14-
15-
impl ModelQueryType {
16-
pub fn name(&self) -> &'static str {
17-
match self {
18-
Self::FindUnique => "findUnique",
19-
Self::FindFirst => "findFirst",
20-
Self::FindMany => "findMany",
21-
Self::Count => "aggregate",
22-
}
23-
}
24-
}
25-
26-
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
27-
pub enum ModelMutationType {
28-
Create,
29-
CreateMany,
30-
Update,
31-
UpdateMany,
32-
Delete,
33-
DeleteMany,
34-
Upsert,
35-
}
36-
37-
impl ModelMutationType {
38-
pub fn name(&self) -> &'static str {
39-
match self {
40-
Self::Create => "createOne",
41-
Self::CreateMany => "createMany",
42-
Self::Update => "updateOne",
43-
Self::UpdateMany => "updateMany",
44-
Self::Delete => "deleteOne",
45-
Self::DeleteMany => "deleteMany",
46-
Self::Upsert => "upsertOne",
47-
}
48-
}
49-
}
50-
51-
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
52-
pub enum ModelActionType {
53-
Query(ModelQueryType),
54-
Mutation(ModelMutationType),
55-
}
56-
57-
impl ModelActionType {
58-
pub fn name(&self) -> &'static str {
59-
match self {
60-
Self::Query(q) => q.name(),
61-
Self::Mutation(q) => q.name(),
62-
}
63-
}
64-
}
65-
66-
pub trait ModelActions {
67-
type Data: DeserializeOwned;
68-
type Where: WhereInput;
69-
type Set: Into<(String, PrismaValue)>;
70-
type With: Into<Selection>;
71-
type OrderBy: Into<(String, PrismaValue)>;
72-
type Cursor: Into<Self::Where>;
73-
74-
const MODEL: &'static str;
75-
76-
fn scalar_selections() -> Vec<Selection>;
77-
}
1+
use crate::{ModelWriteOperation, SerializedWhereInput};
782

793
pub trait WhereInput {
804
fn serialize(self) -> SerializedWhereInput;
815
}
826

83-
pub trait ModelAction {
84-
type Actions: ModelActions;
85-
86-
const TYPE: ModelActionType;
87-
88-
fn base_selection(
89-
arguments: impl IntoIterator<Item = SelectionArgument>,
90-
nested_selections: impl IntoIterator<Item = Selection>,
91-
) -> Selection {
92-
Selection::new(
93-
format!("{}{}", Self::TYPE.name(), Self::Actions::MODEL),
94-
Some("result".to_string()),
95-
arguments.into_iter().collect::<Vec<_>>(),
96-
nested_selections.into_iter().collect::<Vec<_>>(),
97-
)
98-
}
99-
}
100-
1017
#[derive(Debug, PartialEq, Eq)]
1028
pub struct ModelMutationCallbackData {
103-
pub action: ModelMutationType,
9+
pub action: ModelWriteOperation,
10410
pub model: &'static str,
10511
}
10612

0 commit comments

Comments
 (0)