diff --git a/Cargo.lock b/Cargo.lock index 0f5f28572..a2fdc3461 100644 Binary files a/Cargo.lock and b/Cargo.lock differ diff --git a/Cargo.toml b/Cargo.toml index c51015198..112155006 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,21 +13,21 @@ members = [ ] [workspace.dependencies] -prisma-client-rust = { git = "https://github.com/Brendonovich/prisma-client-rust", tag = "0.6.8", features = [ +prisma-client-rust = { git = "https://github.com/Brendonovich/prisma-client-rust", rev = "3c846422055032ca9b2c8ce969ab9c8d18fde013", features = [ "rspc", "sqlite-create-many", "migrations", "sqlite", -], default-features = false } -prisma-client-rust-cli = { git = "https://github.com/Brendonovich/prisma-client-rust", tag = "0.6.8", features = [ +] } +prisma-client-rust-cli = { git = "https://github.com/Brendonovich/prisma-client-rust", rev = "3c846422055032ca9b2c8ce969ab9c8d18fde013", features = [ "rspc", "sqlite-create-many", "migrations", "sqlite", -], default-features = false } -prisma-client-rust-sdk = { git = "https://github.com/Brendonovich/prisma-client-rust", tag = "0.6.8", features = [ +] } +prisma-client-rust-sdk = { git = "https://github.com/Brendonovich/prisma-client-rust", rev = "3c846422055032ca9b2c8ce969ab9c8d18fde013", features = [ "sqlite", -], default-features = false } +] } rspc = { version = "0.1.4" } specta = { version = "1.0.3" } diff --git a/crates/sync-generator/src/attribute/mod.rs b/crates/sync-generator/src/attribute/mod.rs index 48bba6189..6dbb32a4f 100644 --- a/crates/sync-generator/src/attribute/mod.rs +++ b/crates/sync-generator/src/attribute/mod.rs @@ -1,4 +1,4 @@ -use prisma_client_rust_sdk::prelude::*; +use prisma_client_rust_sdk::prisma::prisma_models::{ast::WithDocumentation, walkers::ModelWalker}; mod parser; @@ -41,9 +41,10 @@ impl<'a> Attribute<'a> { } } -pub fn model_attributes(model: &dml::Model) -> Vec { +pub fn model_attributes(model: ModelWalker) -> Vec { model - .documentation + .ast_model() + .documentation() .as_ref() .map(|docs| docs.lines().flat_map(Attribute::parse).collect()) .unwrap_or_default() diff --git a/crates/sync-generator/src/lib.rs b/crates/sync-generator/src/lib.rs index d267850fa..f8fa830b3 100644 --- a/crates/sync-generator/src/lib.rs +++ b/crates/sync-generator/src/lib.rs @@ -2,7 +2,10 @@ mod attribute; use attribute::*; -use prisma_client_rust_sdk::prelude::*; +use prisma_client_rust_sdk::{ + prelude::*, + prisma::prisma_models::walkers::{FieldWalker, ModelWalker, RefinedFieldWalker}, +}; #[derive(Debug, serde::Serialize, thiserror::Error)] enum Error {} @@ -10,10 +13,10 @@ enum Error {} #[derive(serde::Deserialize)] struct SDSyncGenerator {} -type FieldVec<'a> = Vec<&'a dml::Field>; +type FieldVec<'a> = Vec>; -#[derive(Debug)] #[allow(unused)] +#[derive(Clone)] enum ModelSyncType<'a> { Local { id: FieldVec<'a>, @@ -31,7 +34,7 @@ enum ModelSyncType<'a> { } impl<'a> ModelSyncType<'a> { - fn from_attribute(attr: Attribute, model: &'a dml::Model) -> Option { + fn from_attribute(attr: Attribute, model: ModelWalker<'a>) -> Option { let id = attr .field("id") .map(|field| match field { @@ -40,16 +43,15 @@ impl<'a> ModelSyncType<'a> { }) .unwrap_or_else(|| { model - .primary_key + .primary_key() .as_ref() .unwrap() - .fields - .iter() - .map(|f| f.name.as_str()) + .fields() + .map(|f| f.name()) .collect() }) .into_iter() - .flat_map(|name| model.find_field(name)) + .flat_map(|name| model.fields().find(|f| f.name() == name)) .collect(); Some(match attr.name { @@ -60,7 +62,7 @@ impl<'a> ModelSyncType<'a> { }) } - fn sync_id(&self) -> Vec<&dml::Field> { + fn sync_id(&self) -> Vec { match self { Self::Owned { id } => id.clone(), Self::Local { id } => id.clone(), @@ -90,21 +92,22 @@ impl PrismaGenerator for SDSyncGenerator { type Error = Error; fn generate(self, args: GenerateArgs) -> Result { - let models_with_attributes = args - .dml - .models() - .map(|model| (model, model_attributes(model))); + let db = &args.schema.db; - let models_with_sync_types = models_with_attributes.map(|(model, attributes)| { - let sync_type = attributes - .into_iter() - .find_map(|a| ModelSyncType::from_attribute(a, model)); + let models_with_sync_types = db + .walk_models() + .map(|model| (model, model_attributes(model))) + .map(|(model, attributes)| { + let sync_type = attributes + .into_iter() + .find_map(|a| ModelSyncType::from_attribute(a, model)); - (model, sync_type) - }); + (model, sync_type) + }) + .collect::>(); - let model_modules = models_with_sync_types.clone().map(|(model, sync_type)| { - let model_name_snake = snake_ident(&model.name); + let model_modules = models_with_sync_types.clone().into_iter().map(|(model, sync_type)| { + let model_name_snake = snake_ident(model.name()); let sync_id = sync_type.as_ref() .map(|sync_type| { @@ -112,15 +115,14 @@ impl PrismaGenerator for SDSyncGenerator { let fields = fields.iter().flat_map(|field| { let name_snake = snake_ident(field.name()); - let typ = match field { - dml::Field::ScalarField(_) => { + let typ = match field.refine() { + RefinedFieldWalker::Scalar(_) => { field.type_tokens("e!(self)) }, - dml::Field::RelationField(relation)=> { - let relation_model_name_snake = snake_ident(&relation.relation_info.referenced_model); + RefinedFieldWalker::Relation(relation)=> { + let relation_model_name_snake = snake_ident(relation.related_model().name()); Some(quote!(super::#relation_model_name_snake::SyncId)) }, - _ => return None }; Some(quote!(pub #name_snake: #typ)) @@ -149,32 +151,33 @@ impl PrismaGenerator for SDSyncGenerator { let field_name_snake_str = field_name_snake.to_string(); - match field { - dml::Field::ScalarField(_) => { + match field.refine() { + RefinedFieldWalker::Scalar(_) => { Some(quote! { #field_name_snake_str => #model_name_snake::#field_name_snake::set(::serde_json::from_value(val).unwrap()), }) }, - dml::Field::RelationField(relation_field) => { - let relation_model_name_snake = snake_ident(&relation_field.relation_info.referenced_model); + RefinedFieldWalker::Relation(relation_field) => { + let relation_model_name_snake = snake_ident(relation_field.related_model().name()); - match &relation_field.relation_info.references[..] { - [_] => { - Some(quote! { - #field_name_snake_str => { - let val: std::collections::HashMap = ::serde_json::from_value(val).unwrap(); - let val = val.into_iter().next().unwrap(); + match relation_field.referenced_fields() { + Some(i) => { + if i.count() == 1 { + Some(quote! { + #field_name_snake_str => { + let val: std::collections::HashMap = ::serde_json::from_value(val).unwrap(); + let val = val.into_iter().next().unwrap(); - #model_name_snake::#field_name_snake::connect( - #relation_model_name_snake::UniqueWhereParam::deserialize(&val.0, val.1).unwrap() - ) - }, - }) + #model_name_snake::#field_name_snake::connect( + #relation_model_name_snake::UniqueWhereParam::deserialize(&val.0, val.1).unwrap() + ) + }, + }) + } else { None } }, _ => None } }, - _ => None } }); @@ -195,11 +198,10 @@ impl PrismaGenerator for SDSyncGenerator { let unique_param_impl = { let field_matches = model - .loose_unique_criterias() - .iter() - .flat_map(|criteria| match &criteria.fields[..] { - [field] => { - let unique_field_name_str = &field.name; + .unique_criterias() + .flat_map(|criteria| match &criteria.fields().next() { + Some(field) if criteria.fields().len() == 1 => { + let unique_field_name_str = field.name(); let unique_field_name_snake = snake_ident(unique_field_name_str); Some(quote!(#unique_field_name_str => @@ -242,10 +244,11 @@ impl PrismaGenerator for SDSyncGenerator { let model_sync_data = { let (variants, matches): (Vec<_>, Vec<_>) = models_with_sync_types + .into_iter() .filter_map(|(model, sync_type)| { - let model_name_snake = snake_ident(&model.name); - let model_name_pascal = pascal_ident(&model.name); - let model_name_str = &model.name; + let model_name_snake = snake_ident(model.name()); + let model_name_pascal = pascal_ident(model.name()); + let model_name_str = model.name(); sync_type.and_then(|a| { let data_type = match a {