Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add extended traits #238

Merged
merged 1 commit into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ rspc = ["dep:rspc"]
sqlite-create-many = ["psl/sqlite-create-many"]
migrations = ["migration-core", "include_dir", "tempdir", "tokio/fs", "tracing"]
mocking = ["tokio"]
mutation-callbacks = []
# mutation-callbacks = []

mysql = ["query-core/mysql", "migration-core/mysql"]
sqlite = ["query-core/sqlite", "migration-core/sqlite"]
Expand Down
2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ default = ["mysql", "sqlite", "mssql", "postgresql", "mongodb"]
rspc = []
migrations = []
sqlite-create-many = ["prisma-client-rust-sdk/sqlite-create-many"]
mutation-callbacks = []
# mutation-callbacks = []
mocking = []

mysql = ["prisma-client-rust-sdk/mysql"]
Expand Down
8 changes: 4 additions & 4 deletions cli/src/generator/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ pub fn generate(args: &GenerateArgs) -> TokenStream {

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

(Self(internals), store)
}
Expand Down Expand Up @@ -111,7 +111,7 @@ pub fn generate(args: &GenerateArgs) -> TokenStream {

#mock_ctor

pub fn _query_raw<T: serde::de::DeserializeOwned>(&self, query: #pcr::Raw) -> #pcr::QueryRaw<T> {
pub fn _query_raw<T: #pcr::Data>(&self, query: #pcr::Raw) -> #pcr::QueryRaw<T> {
#pcr::QueryRaw::new(
&self.0,
query,
Expand All @@ -127,7 +127,7 @@ pub fn generate(args: &GenerateArgs) -> TokenStream {
)
}

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

Expand Down
2 changes: 1 addition & 1 deletion cli/src/generator/models/include_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ fn model_macro<'a>(
let selection_struct = quote! {
pub struct Selection(Vec<::prisma_client_rust::Selection>);

impl ::prisma_client_rust::#variant_ident::#selection_type for Selection {
impl ::prisma_client_rust::#selection_type for Selection {
type Data = Data;
type ModelData = $crate::#module_path::#model_name_snake::Data;

Expand Down
22 changes: 11 additions & 11 deletions cli/src/generator/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,17 +291,17 @@ pub fn generate(args: &GenerateArgs, module_path: TokenStream) -> Vec<TokenStrea
pub type UniqueArgs = ::prisma_client_rust::UniqueArgs<Actions<'static>>;
pub type ManyArgs = ::prisma_client_rust::ManyArgs<Actions<'static>>;

pub type Count<'a> = ::prisma_client_rust::Count<'a, Actions<'static>>;
pub type Create<'a> = ::prisma_client_rust::Create<'a, Actions<'static>>;
pub type CreateMany<'a> = ::prisma_client_rust::CreateMany<'a, Actions<'static>>;
pub type FindUnique<'a> = ::prisma_client_rust::FindUnique<'a, Actions<'static>>;
pub type FindMany<'a> = ::prisma_client_rust::FindMany<'a, Actions<'static>>;
pub type FindFirst<'a> = ::prisma_client_rust::FindFirst<'a, Actions<'static>>;
pub type Update<'a> = ::prisma_client_rust::Update<'a, Actions<'static>>;
pub type UpdateMany<'a> = ::prisma_client_rust::UpdateMany<'a, Actions<'static>>;
pub type Upsert<'a> = ::prisma_client_rust::Upsert<'a, Actions<'static>>;
pub type Delete<'a> = ::prisma_client_rust::Delete<'a, Actions<'static>>;
pub type DeleteMany<'a> = ::prisma_client_rust::DeleteMany<'a, Actions<'static>>;
pub type Count<'a> = ::prisma_client_rust::Count<'a, Actions<'a>>;
pub type Create<'a> = ::prisma_client_rust::Create<'a, Actions<'a>>;
pub type CreateMany<'a> = ::prisma_client_rust::CreateMany<'a, Actions<'a>>;
pub type FindUnique<'a> = ::prisma_client_rust::FindUnique<'a, Actions<'a>>;
pub type FindMany<'a> = ::prisma_client_rust::FindMany<'a, Actions<'a>>;
pub type FindFirst<'a> = ::prisma_client_rust::FindFirst<'a, Actions<'a>>;
pub type Update<'a> = ::prisma_client_rust::Update<'a, Actions<'a>>;
pub type UpdateMany<'a> = ::prisma_client_rust::UpdateMany<'a, Actions<'a>>;
pub type Upsert<'a> = ::prisma_client_rust::Upsert<'a, Actions<'a>>;
pub type Delete<'a> = ::prisma_client_rust::Delete<'a, Actions<'a>>;
pub type DeleteMany<'a> = ::prisma_client_rust::DeleteMany<'a, Actions<'a>>;

#actions_struct
}
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ prisma-client-rust = { path = "..", features = [
"sqlite-create-many",
"sqlite",
"migrations",
"mutation-callbacks",
# "mutation-callbacks",
"mocking",
], default-features = false }
76 changes: 19 additions & 57 deletions integration-tests/tests/count.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
use crate::db::*;
use crate::utils::*;

#[tokio::test]
async fn basic() -> TestResult {
let client = client().await;

async fn create_posts(client: &PrismaClient) -> TestResult {
client
.post()
.create("Hi from Prisma!".to_string(), true, vec![])
.create_many(vec![
post::create("Hi from Prisma!".to_string(), true, vec![]),
post::create("Hi from Prisma!".to_string(), true, vec![]),
post::create("Hi from Prisma!".to_string(), false, vec![]),
])
.exec()
.await?;

Ok(())
}

#[tokio::test]
async fn basic() -> TestResult {
let client = client().await;

create_posts(&client).await?;

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

assert_eq!(count, 1);
assert_eq!(count, 3);

cleanup(client).await
}
Expand All @@ -37,23 +47,7 @@ async fn no_results() -> TestResult {
async fn where_() -> TestResult {
let client = client().await;

client
.post()
.create("Hi from Prisma!".to_string(), true, vec![])
.exec()
.await?;

client
.post()
.create("Hi from Prisma!".to_string(), false, vec![])
.exec()
.await?;

client
.post()
.create("Hi from Prisma!".to_string(), true, vec![])
.exec()
.await?;
create_posts(&client).await?;

let published_count = client
.post()
Expand All @@ -76,23 +70,7 @@ async fn where_() -> TestResult {
async fn take() -> TestResult {
let client = client().await;

client
.post()
.create("Hi from Prisma!".to_string(), true, vec![])
.exec()
.await?;

client
.post()
.create("Hi from Prisma!".to_string(), true, vec![])
.exec()
.await?;

client
.post()
.create("Hi from Prisma!".to_string(), true, vec![])
.exec()
.await?;
create_posts(&client).await?;

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

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

client
.post()
.create("Hi from Prisma!".to_string(), true, vec![])
.exec()
.await?;

client
.post()
.create("Hi from Prisma!".to_string(), true, vec![])
.exec()
.await?;

client
.post()
.create("Hi from Prisma!".to_string(), true, vec![])
.exec()
.await?;
create_posts(&client).await?;

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

Expand Down
2 changes: 1 addition & 1 deletion integration-tests/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async fn aaaa_run_migrations() -> TestResult {
}

mod batch;
mod callbacks;
// mod callbacks;
mod count;
mod create;
mod create_many;
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::utils::*;

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

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

Expand Down Expand Up @@ -31,7 +31,7 @@ async fn returns() -> TestResult {

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

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

Expand Down
2 changes: 1 addition & 1 deletion prisma-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ prisma-client-rust-cli = { features = [
"sqlite-create-many",
"sqlite",
"migrations",
"mutation-callbacks",
# "mutation-callbacks",
"mocking"
], default_features = false, path = "../cli" }
98 changes: 2 additions & 96 deletions src/actions.rs
Original file line number Diff line number Diff line change
@@ -1,106 +1,12 @@
use prisma_models::PrismaValue;
use query_core::{Selection, SelectionArgument};
use serde::de::DeserializeOwned;

use crate::SerializedWhereInput;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ModelQueryType {
FindUnique,
FindFirst,
FindMany,
Count,
}

impl ModelQueryType {
pub fn name(&self) -> &'static str {
match self {
Self::FindUnique => "findUnique",
Self::FindFirst => "findFirst",
Self::FindMany => "findMany",
Self::Count => "aggregate",
}
}
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ModelMutationType {
Create,
CreateMany,
Update,
UpdateMany,
Delete,
DeleteMany,
Upsert,
}

impl ModelMutationType {
pub fn name(&self) -> &'static str {
match self {
Self::Create => "createOne",
Self::CreateMany => "createMany",
Self::Update => "updateOne",
Self::UpdateMany => "updateMany",
Self::Delete => "deleteOne",
Self::DeleteMany => "deleteMany",
Self::Upsert => "upsertOne",
}
}
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ModelActionType {
Query(ModelQueryType),
Mutation(ModelMutationType),
}

impl ModelActionType {
pub fn name(&self) -> &'static str {
match self {
Self::Query(q) => q.name(),
Self::Mutation(q) => q.name(),
}
}
}

pub trait ModelActions {
type Data: DeserializeOwned;
type Where: WhereInput;
type Set: Into<(String, PrismaValue)>;
type With: Into<Selection>;
type OrderBy: Into<(String, PrismaValue)>;
type Cursor: Into<Self::Where>;

const MODEL: &'static str;

fn scalar_selections() -> Vec<Selection>;
}
use crate::{ModelWriteOperation, SerializedWhereInput};

pub trait WhereInput {
fn serialize(self) -> SerializedWhereInput;
}

pub trait ModelAction {
type Actions: ModelActions;

const TYPE: ModelActionType;

fn base_selection(
arguments: impl IntoIterator<Item = SelectionArgument>,
nested_selections: impl IntoIterator<Item = Selection>,
) -> Selection {
Selection::new(
format!("{}{}", Self::TYPE.name(), Self::Actions::MODEL),
Some("result".to_string()),
arguments.into_iter().collect::<Vec<_>>(),
nested_selections.into_iter().collect::<Vec<_>>(),
)
}
}

#[derive(Debug, PartialEq, Eq)]
pub struct ModelMutationCallbackData {
pub action: ModelMutationType,
pub action: ModelWriteOperation,
pub model: &'static str,
}

Expand Down
Loading