Skip to content

Commit

Permalink
use custom PrismaValue as much as possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Brendonovich committed May 10, 2024
1 parent c2e7796 commit 9259b47
Show file tree
Hide file tree
Showing 21 changed files with 89 additions and 99 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions crates/lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ default = []
rspc = ["dep:rspc", "specta"]
specta = ["dep:specta", "prisma-client-rust-generator-macros/specta"]
sqlite-create-many = ["psl/sqlite-create-many"]
migrations = ["schema-core", "dep:include_dir", "dep:tempfile", "tokio/fs", "dep:tracing"]
migrations = [
"schema-core",
"dep:include_dir",
"dep:tempfile",
"tokio/fs",
"dep:tracing",
]
mocking = ["tokio"]
# mutation-callbacks = []

Expand All @@ -27,7 +33,8 @@ serde.workspace = true
serde_json.workspace = true
chrono = { version = "0.4.19", features = ["serde"] }
thiserror = "1.0.30"
bigdecimal = { version = "0.3", features = ["serde"] }
bigdecimal = { version = "0.4", features = ["serde"] }
bigdecimal_03 = { package = "bigdecimal", version = "0.3" }
uuid = { version = "1", features = ["serde"] }
indexmap = "1.8.2"
serde-value = "0.7.0"
Expand Down
3 changes: 2 additions & 1 deletion crates/lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ use std::collections::HashMap;

pub use bigdecimal;
pub use chrono;
pub use prisma_models::{self, PrismaValue};
pub use prisma_models;
pub use prisma_value::PrismaValue;
pub use psl;
pub use query_core;
pub use query_core::{schema, Selection};
Expand Down
12 changes: 8 additions & 4 deletions crates/lib/src/prisma_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub enum PrismaValue {
String(String),
Boolean(bool),
Enum(String),
Int(i32),
Int(i64),
Uuid(Uuid),
List(Vec<PrismaValue>),
Json(serde_json::Value),
Expand Down Expand Up @@ -78,7 +78,7 @@ impl From<prisma_models::PrismaValue> for PrismaValue {
prisma_models::PrismaValue::String(value) => Self::String(value),
prisma_models::PrismaValue::Boolean(value) => Self::Boolean(value),
prisma_models::PrismaValue::Enum(value) => Self::Enum(value),
prisma_models::PrismaValue::Int(value) => Self::Int(value as i32),
prisma_models::PrismaValue::Int(value) => Self::Int(value),
prisma_models::PrismaValue::Uuid(value) => Self::Uuid(value),
prisma_models::PrismaValue::List(value) => {
Self::List(value.into_iter().map(Into::into).collect())
Expand Down Expand Up @@ -145,8 +145,12 @@ impl From<PrismaValue> for prisma_models::PrismaValue {
}
PrismaValue::Null => Self::Null,
PrismaValue::DateTime(value) => Self::DateTime(value),
PrismaValue::Decimal(value) => Self::Float(value),
PrismaValue::Float(value) => Self::Float(BigDecimal::from_f64(value).unwrap()),
PrismaValue::Decimal(value) => {
Self::Float(bigdecimal_03::BigDecimal::from_str(&value.to_string()).unwrap())
}
PrismaValue::Float(value) => {
Self::Float(bigdecimal_03::BigDecimal::from_f64(value.to_f64().unwrap()).unwrap())
}
PrismaValue::BigInt(value) => Self::BigInt(value),
PrismaValue::Bytes(value) => Self::Bytes(value),
}
Expand Down
10 changes: 4 additions & 6 deletions crates/lib/src/queries/count.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use prisma_models::PrismaValue;
use query_core::{Operation, Selection};
use serde::Deserialize;

use crate::{
merge_fields, ModelOperation, ModelQuery, ModelReadOperation, ModelTypes, OrderByQuery,
PaginatedQuery, PrismaClientInternals, Query, QueryConvert, SerializedWhereInput, WhereInput,
PaginatedQuery, PrismaClientInternals, PrismaValue, Query, QueryConvert, WhereInput,
WhereQuery,
};

Expand Down Expand Up @@ -112,14 +111,13 @@ impl<'a, Actions: ModelTypes> Query<'a> for Count<'a, Actions> {
.map(WhereInput::serialize)
.map(|s| (s.field, s.value.into()))
.collect(),
)
.into(),
),
)
}),
self.skip
.map(|skip| ("skip".to_string(), PrismaValue::Int(skip as i64).into())),
.map(|skip| ("skip".to_string(), PrismaValue::Int(skip))),
self.take
.map(|take| ("take".to_string(), PrismaValue::Int(take as i64).into())),
.map(|take| ("take".to_string(), PrismaValue::Int(take))),
]
.into_iter()
.flatten(),
Expand Down
8 changes: 3 additions & 5 deletions crates/lib/src/queries/create.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use prisma_models::PrismaValue;
use query_core::{Operation, Selection};

use crate::{
merge_fields, Include, IncludeType, ModelOperation, ModelQuery, ModelTypes,
ModelWriteOperation, PrismaClientInternals, Query, QueryConvert, Select, SelectType, SetQuery,
WithQuery,
ModelWriteOperation, PrismaClientInternals, PrismaValue, Query, QueryConvert, Select,
SelectType, SetQuery, WithQuery,
};

pub struct Create<'a, Actions: ModelTypes> {
Expand Down Expand Up @@ -36,8 +35,7 @@ impl<'a, Actions: ModelTypes> Create<'a, Actions> {
"data".to_string(),
PrismaValue::Object(merge_fields(
set_params.into_iter().map(Into::into).collect(),
))
.into(),
)),
)]
.into_iter(),
nested_selections,
Expand Down
6 changes: 2 additions & 4 deletions crates/lib/src/queries/create_many.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use prisma_models::PrismaValue;
use query_core::{Operation, Selection};

use crate::{
merge_fields, BatchResult, ModelOperation, ModelQuery, ModelTypes, ModelWriteOperation,
PrismaClientInternals, Query, QueryConvert,
PrismaClientInternals, PrismaValue, Query, QueryConvert,
};

pub struct CreateMany<'a, Actions: ModelTypes> {
Expand Down Expand Up @@ -48,8 +47,7 @@ impl<'a, Actions: ModelTypes> CreateMany<'a, Actions> {
))
})
.collect(),
)
.into(),
),
)),
_skip_duplicates.then(|| {
(
Expand Down
8 changes: 3 additions & 5 deletions crates/lib/src/queries/create_unchecked.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use prisma_models::PrismaValue;
use query_core::{Operation, Selection};

use crate::{
merge_fields, Include, IncludeType, ModelOperation, ModelQuery, ModelTypes,
ModelWriteOperation, PrismaClientInternals, Query, QueryConvert, Select, SelectType,
UncheckedSetQuery, WithQuery,
ModelWriteOperation, PrismaClientInternals, PrismaValue, Query, QueryConvert, Select,
SelectType, UncheckedSetQuery, WithQuery,
};

pub struct CreateUnchecked<'a, Actions: ModelTypes> {
Expand Down Expand Up @@ -36,8 +35,7 @@ impl<'a, Actions: ModelTypes> CreateUnchecked<'a, Actions> {
"data".to_string(),
PrismaValue::Object(merge_fields(
set_params.into_iter().map(Into::into).collect(),
))
.into(),
)),
)]
.into_iter(),
nested_selections,
Expand Down
6 changes: 3 additions & 3 deletions crates/lib/src/queries/delete.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use prisma_models::PrismaValue;
use query_core::{Operation, Selection};

use crate::{
Include, IncludeType, ModelOperation, ModelQuery, ModelTypes, ModelWriteOperation,
PrismaClientInternals, Query, QueryConvert, Select, SelectType, WhereInput, WithQuery,
PrismaClientInternals, PrismaValue, Query, QueryConvert, Select, SelectType, WhereInput,
WithQuery,
};

pub struct Delete<'a, Actions: ModelTypes> {
Expand Down Expand Up @@ -37,7 +37,7 @@ impl<'a, Actions: ModelTypes> Delete<'a, Actions> {
Self::base_selection(
[(
"where".to_string(),
PrismaValue::Object(vec![where_param.serialize().transform_equals()]).into(),
PrismaValue::Object(vec![where_param.serialize().transform_equals()]),
)],
nested_selections,
)
Expand Down
6 changes: 2 additions & 4 deletions crates/lib/src/queries/delete_many.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ use query_core::Operation;

use crate::{
merge_fields, BatchResult, ModelOperation, ModelQuery, ModelTypes, ModelWriteOperation,
PrismaClientInternals, Query, QueryConvert, WhereInput, WhereQuery,
PrismaClientInternals, PrismaValue, Query, QueryConvert, WhereInput, WhereQuery,
};
use prisma_models::PrismaValue;

pub struct DeleteMany<'a, Actions: ModelTypes> {
client: &'a PrismaClientInternals,
Expand Down Expand Up @@ -50,8 +49,7 @@ impl<'a, Actions: ModelTypes> Query<'a> for DeleteMany<'a, Actions> {
.map(WhereInput::serialize)
.map(|s| (s.field, s.value.into()))
.collect(),
))
.into(),
)),
)
})]
.into_iter()
Expand Down
14 changes: 5 additions & 9 deletions crates/lib/src/queries/find_first.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use prisma_models::PrismaValue;
use query_core::{Operation, Selection};

use crate::{
merge_fields, Include, IncludeType, ModelOperation, ModelQuery, ModelReadOperation, ModelTypes,
OrderByQuery, PaginatedQuery, PrismaClientInternals, Query, QueryConvert, Select, SelectType,
WhereInput, WhereQuery, WithQuery,
OrderByQuery, PaginatedQuery, PrismaClientInternals, PrismaValue, Query, QueryConvert, Select,
SelectType, WhereInput, WhereQuery, WithQuery,
};

pub struct FindFirst<'a, Actions: ModelTypes> {
Expand Down Expand Up @@ -74,8 +73,7 @@ impl<'a, Actions: ModelTypes> FindFirst<'a, Actions> {
.map(WhereInput::serialize)
.map(|s| (s.field, s.value.into()))
.collect(),
))
.into(),
)),
)
}),
(!order_by_params.is_empty()).then(|| {
Expand All @@ -86,8 +84,7 @@ impl<'a, Actions: ModelTypes> FindFirst<'a, Actions> {
.into_iter()
.map(|p| PrismaValue::Object(vec![p.into()]))
.collect(),
)
.into(),
),
)
}),
(!cursor_params.is_empty()).then(|| {
Expand All @@ -99,8 +96,7 @@ impl<'a, Actions: ModelTypes> FindFirst<'a, Actions> {
.map(WhereInput::serialize)
.map(|s| (s.field, s.value.into()))
.collect(),
)
.into(),
),
)
}),
skip.map(|skip| ("skip".to_string(), PrismaValue::Int(skip as i64).into())),
Expand Down
15 changes: 6 additions & 9 deletions crates/lib/src/queries/find_many.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
use prisma_models::PrismaValue;
use query_core::{ArgumentValue, Operation, Selection};

use crate::{
merge_fields, Include, IncludeType, ModelOperation, ModelQuery, ModelReadOperation, ModelTypes,
OrderByQuery, PaginatedQuery, PrismaClientInternals, Query, QueryConvert, Select, SelectType,
WhereInput, WhereQuery, WithQuery,
OrderByQuery, PaginatedQuery, PrismaClientInternals, PrismaValue, Query, QueryConvert, Select,
SelectType, WhereInput, WhereQuery, WithQuery,
};

use super::SerializedWhereInput;

pub struct FindMany<'a, Actions: ModelTypes> {
client: &'a PrismaClientInternals,
pub where_params: Vec<Actions::Where>,
Expand Down Expand Up @@ -303,17 +300,17 @@ impl<Actions: ModelTypes> ManyArgs<Actions> {
.map(WhereInput::serialize)
.map(|s| (s.field, s.value.into()))
.collect(),
)
.into(),
),
)
}),
self.skip
.map(|skip| ("skip".to_string(), PrismaValue::Int(skip).into())),
.map(|skip| ("skip".to_string(), PrismaValue::Int(skip))),
self.take
.map(|take| ("take".to_string(), PrismaValue::Int(take).into())),
.map(|take| ("take".to_string(), PrismaValue::Int(take))),
]
.into_iter()
.flatten()
.map(|(k, v)| (k, prisma_models::PrismaValue::from(v).into()))
.collect();

let nested_selections = (self.with_params.len() > 0)
Expand Down
6 changes: 3 additions & 3 deletions crates/lib/src/queries/find_unique.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::marker::PhantomData;

use prisma_models::PrismaValue;
use query_core::{Operation, Selection};

use crate::{
Include, IncludeType, ModelOperation, ModelQuery, ModelReadOperation, ModelTypes,
PrismaClientInternals, Query, QueryConvert, Select, SelectType, WhereInput, WithQuery,
PrismaClientInternals, PrismaValue, Query, QueryConvert, Select, SelectType, WhereInput,
WithQuery,
};

pub struct FindUnique<'a, Actions: ModelTypes> {
Expand Down Expand Up @@ -37,7 +37,7 @@ impl<'a, Actions: ModelTypes> FindUnique<'a, Actions> {
Self::base_selection(
[(
"where".to_string(),
PrismaValue::Object(vec![where_param.serialize().transform_equals()]).into(),
PrismaValue::Object(vec![where_param.serialize().transform_equals()]),
)],
nested_selections,
)
Expand Down
26 changes: 13 additions & 13 deletions crates/lib/src/queries/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,19 @@ use serde::de::IntoDeserializer;
use serde::Deserialize;
use std::future::Future;

use crate::ExecutionEngine;
use crate::{ExecutionEngine, PrismaValue};

pub enum SerializedWhereValue {
Object(Vec<(String, prisma_models::PrismaValue)>),
List(Vec<prisma_models::PrismaValue>),
Value(prisma_models::PrismaValue),
Object(Vec<(String, PrismaValue)>),
List(Vec<PrismaValue>),
Value(PrismaValue),
}

impl Into<prisma_models::PrismaValue> for SerializedWhereValue {
fn into(self) -> prisma_models::PrismaValue {
impl Into<PrismaValue> for SerializedWhereValue {
fn into(self) -> PrismaValue {
match self {
Self::Object(v) => prisma_models::PrismaValue::Object(v),
Self::List(v) => prisma_models::PrismaValue::List(v),
Self::Object(v) => PrismaValue::Object(v),
Self::List(v) => PrismaValue::List(v),
Self::Value(v) => v,
}
}
Expand All @@ -82,7 +82,7 @@ impl SerializedWhereInput {
/// If the parameter is an 'equals' parameter, collapses the value provided directly
/// into the where clause. This is necessary for unique queries that have no filters,
/// only direct value comparisons.
pub fn transform_equals(self) -> (String, prisma_models::PrismaValue) {
pub fn transform_equals(self) -> (String, PrismaValue) {
let Self { field, value } = self;

(
Expand All @@ -94,17 +94,17 @@ impl SerializedWhereInput {
.map(|i| params.swap_remove(i))
{
Some((_, value)) => value,
None => prisma_models::PrismaValue::Object(params),
None => PrismaValue::Object(params),
},
SerializedWhereValue::List(values) => prisma_models::PrismaValue::List(values),
SerializedWhereValue::List(values) => PrismaValue::List(values),
SerializedWhereValue::Value(v) => v,
},
)
}
}

impl Into<(String, prisma_models::PrismaValue)> for SerializedWhereInput {
fn into(self) -> (String, prisma_models::PrismaValue) {
impl Into<(String, PrismaValue)> for SerializedWhereInput {
fn into(self) -> (String, PrismaValue) {
let SerializedWhereInput { field, value } = self;
(field, value.into())
}
Expand Down
Loading

0 comments on commit 9259b47

Please sign in to comment.