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

feat: add option to choose repository methods #2

Merged
merged 1 commit into from
Nov 22, 2024
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
185 changes: 114 additions & 71 deletions src/code_gen.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::parser::{Field, Model};
use core::fmt;
use std::fmt::Write as FmtWrite;
use std::io::Write as IoWrite;
use std::{fs, path::Path};
Expand All @@ -8,11 +9,11 @@ const MAPPER_PATH: &str = "infra/database/prisma/mappers";
const REPOSITORY_PATH: &str = "app/repositories";
const PRISMA_REPOSITORY_PATH: &str = "infra/database/prisma";

#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Eq)]
pub enum ModuleType {
Entity,
Mapper,
Repository,
Repository(Option<Vec<RepositoryOperations>>),
PrismaRepository,
}

Expand All @@ -21,7 +22,7 @@ impl From<&str> for ModuleType {
match value {
"Entity" => ModuleType::Entity,
"Mapper" => ModuleType::Mapper,
"Repository" => ModuleType::Repository,
"Repository" => ModuleType::Repository(None),
"Prisma repository" => ModuleType::PrismaRepository,
_ => unreachable!(),
}
Expand All @@ -33,7 +34,7 @@ impl From<ModuleType> for &str {
match value {
ModuleType::Entity => "Entity",
ModuleType::Mapper => "Mapper",
ModuleType::Repository => "Repository",
ModuleType::Repository(_) => "Repository",
ModuleType::PrismaRepository => "Prisma repository",
}
}
Expand All @@ -47,20 +48,39 @@ fn lowercase_first_char(s: &str) -> String {
}
}

enum RepositoryOperations {
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RepositoryOperations {
Create,
Find,
FindMany,
Delete,
Update,
}

fn build_repository_methods(model: &Model, has_mapper: bool, op: RepositoryOperations) -> String {
impl fmt::Display for RepositoryOperations {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match *self {
RepositoryOperations::Create => write!(f, "create"),
RepositoryOperations::Find => write!(f, "find"),
RepositoryOperations::FindMany => write!(f, "findMany"),
RepositoryOperations::Delete => write!(f, "delete"),
RepositoryOperations::Update => write!(f, "update"),
}
}
}

fn build_repository_methods(
model_name: &str,
input_type: &str,
return_type: &str,
has_mapper: bool,
op: &RepositoryOperations,
) -> String {
match op {
RepositoryOperations::Create => {
let mut method = format!(
"async create(data: {}): Promise<{}> {{\n",
model.name, model.name
input_type, return_type
);
if has_mapper {
write!(
Expand All @@ -71,8 +91,8 @@ fn build_repository_methods(model: &Model, has_mapper: bool, op: RepositoryOpera

return {}Mapper.toDomain(result)
}}"#,
lowercase_first_char(&model.name),
model.name
lowercase_first_char(model_name),
model_name
)
.unwrap();

Expand All @@ -85,7 +105,7 @@ fn build_repository_methods(model: &Model, has_mapper: bool, op: RepositoryOpera
data,
}})
}}"#,
lowercase_first_char(&model.name)
lowercase_first_char(model_name)
)
.unwrap();

Expand All @@ -102,12 +122,12 @@ fn build_repository_methods(model: &Model, has_mapper: bool, op: RepositoryOpera
}},
}})
}}"#,
lowercase_first_char(&model.name)
lowercase_first_char(model_name)
),
RepositoryOperations::Find => {
let mut method = format!(
"async find(data: Partial<{}>): Promise<{}> {{\n",
model.name, model.name
"async find(data: {}): Promise<{}> {{\n",
input_type, return_type
);

if has_mapper {
Expand All @@ -119,8 +139,8 @@ fn build_repository_methods(model: &Model, has_mapper: bool, op: RepositoryOpera

return {}Mapper.toDomain(result)
}}"#,
lowercase_first_char(&model.name),
model.name
lowercase_first_char(model_name),
model_name
)
.unwrap();

Expand All @@ -133,16 +153,16 @@ fn build_repository_methods(model: &Model, has_mapper: bool, op: RepositoryOpera
where: data,
}})
}}"#,
lowercase_first_char(&model.name)
lowercase_first_char(model_name)
)
.unwrap();

method
}
RepositoryOperations::FindMany => {
let mut method = format!(
"async findMany(data: Partial<{}>): Promise<{}[]> {{\n",
model.name, model.name
"async findMany(data: {}): Promise<{}[]> {{\n",
input_type, return_type
);

if has_mapper {
Expand All @@ -154,8 +174,8 @@ fn build_repository_methods(model: &Model, has_mapper: bool, op: RepositoryOpera

return result.map({}Mapper.toDomain)
}}"#,
lowercase_first_char(&model.name),
model.name
lowercase_first_char(model_name),
model_name
)
.unwrap();

Expand All @@ -168,16 +188,16 @@ fn build_repository_methods(model: &Model, has_mapper: bool, op: RepositoryOpera
where: data,
}})
}}"#,
lowercase_first_char(&model.name)
lowercase_first_char(model_name)
)
.unwrap();

method
}
RepositoryOperations::Update => {
let mut method = format!(
"async update(id: string, data: Partial<{}>): Promise<{}> {{\n",
model.name, model.name
"async update(id: string, data: {}): Promise<{}> {{\n",
input_type, return_type
);

if has_mapper {
Expand All @@ -192,8 +212,8 @@ fn build_repository_methods(model: &Model, has_mapper: bool, op: RepositoryOpera

return {}Mapper.toDomain(result)
}}"#,
lowercase_first_char(&model.name),
model.name
lowercase_first_char(model_name),
model_name
)
.unwrap();

Expand All @@ -206,7 +226,7 @@ fn build_repository_methods(model: &Model, has_mapper: bool, op: RepositoryOpera
where: data,
}})
}}"#,
lowercase_first_char(&model.name)
lowercase_first_char(model_name)
)
.unwrap();

Expand All @@ -215,52 +235,71 @@ fn build_repository_methods(model: &Model, has_mapper: bool, op: RepositoryOpera
}
}

fn create_repository(model: &Model, has_mapper: bool) -> (String, String) {
let abstract_repository = format!(
r#"export abstract class {}Repository {{
abstract create(data: {}): Promise<{}>

abstract find(data: Partial<{}>): Promise<{}>

abstract findMany(data: Partial<{}>): Promise<{}[]>

abstract update(id: string, data: Partial<{}>): Promise<{}>

abstract delete(id: string): Promise<void>
}}"#,
model.name,
model.name,
model.name,
model.name,
model.name,
model.name,
model.name,
model.name,
model.name
fn create_repository(
model: &Model,
methods: Option<Vec<RepositoryOperations>>,
has_mapper: bool,
has_entity: bool,
) -> (String, String) {
let mut abstract_repository = format!("export abstract class {}Repository {{", model.name);
let mut prisma_repository = format!(
r#"@Injectable()
export class Prisma{}Repository implements {}Repository {{
constructor(private readonly prisma: PrismaService) {{}}"#,
model.name, model.name
);

let prisma_repository = format!(
r#"export class Prisma{}Repository implements {}Repository {{
constructor(private readonly prisma: PrismaService) {{}}

{}
let (input_type, return_type) = if has_entity {
(format!("Partial<{}>", model.name), model.name.clone())
} else {
("any".to_string(), "any".to_string())
};

{}
let methods = methods.unwrap_or_default();

{}
for method in &methods {
match method {
RepositoryOperations::Create => write!(
abstract_repository,
"\n\t\tabstract create(data: {}): Promise<{}>",
input_type, return_type
)
.unwrap(),
RepositoryOperations::Find => write!(
abstract_repository,
"\n\t\tabstract find(data: {}): Promise<{}>",
input_type, return_type
)
.unwrap(),
RepositoryOperations::FindMany => write!(
abstract_repository,
"\n\t\tabstract findMany(data: {}): Promise<{}[]>",
input_type, return_type
)
.unwrap(),
RepositoryOperations::Update => write!(
abstract_repository,
"\n\t\tabstract update(id: string, data: {}): Promise<{}>",
input_type, return_type
)
.unwrap(),
RepositoryOperations::Delete => write!(
abstract_repository,
"\n\t\tabstract delete(id: string): Promise<void>"
)
.unwrap(),
}

{}
write!(
prisma_repository,
"\n\t\t{}",
build_repository_methods(&model.name, &input_type, &return_type, has_mapper, method)
)
.unwrap();
}

{}
}}"#,
model.name,
model.name,
build_repository_methods(model, has_mapper, RepositoryOperations::Create),
build_repository_methods(model, has_mapper, RepositoryOperations::Find),
build_repository_methods(model, has_mapper, RepositoryOperations::FindMany),
build_repository_methods(model, has_mapper, RepositoryOperations::Update),
build_repository_methods(model, has_mapper, RepositoryOperations::Delete)
);
write!(abstract_repository, "\n}}").unwrap();
write!(prisma_repository, "\n}}").unwrap();

(abstract_repository, prisma_repository)
}
Expand Down Expand Up @@ -407,7 +446,7 @@ fn build_path(dir: &Path, module_path: &str, module_type: ModuleType, model_name
let (path, file_name) = match module_type {
ModuleType::Entity => (ENTITY_PATH, format!("{}.entity.ts", kebab_model_name)),
ModuleType::Mapper => (MAPPER_PATH, format!("{}.mapper.ts", kebab_model_name)),
ModuleType::Repository => (
ModuleType::Repository(_) => (
REPOSITORY_PATH,
format!("{}.repository.ts", kebab_model_name),
),
Expand Down Expand Up @@ -444,12 +483,16 @@ pub fn write_modules(modules: Vec<ModuleType>, dir: &Path, module_path: &str, mo
create_mapper(model),
)
.unwrap(),
ModuleType::Repository => {
let (abstract_repository, prisma_repository) =
create_repository(model, modules.contains(&ModuleType::Mapper));
ModuleType::Repository(methods) => {
let (abstract_repository, prisma_repository) = create_repository(
model,
methods.clone(),
modules.contains(&ModuleType::Mapper),
modules.contains(&ModuleType::Entity),
);

write_to_module(
build_path(dir, module_path, ModuleType::Repository, &model.name),
build_path(dir, module_path, ModuleType::Repository(None), &model.name),
abstract_repository,
)
.unwrap();
Expand Down
35 changes: 32 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use code_gen::{write_modules, ModuleType};
use code_gen::{write_modules, ModuleType, RepositoryOperations};
use dialoguer::{theme::ColorfulTheme, FuzzySelect, MultiSelect};
use parser::{get_schemas, parse_schema, TsConfig};
use std::{
Expand Down Expand Up @@ -79,7 +79,7 @@ fn main() {
let multiselected: &[&str; 3] = &[
ModuleType::Entity.into(),
ModuleType::Mapper.into(),
ModuleType::Repository.into(),
ModuleType::Repository(None).into(),
];

let defaults = &[true, false, false];
Expand All @@ -91,10 +91,39 @@ fn main() {
.interact()
.unwrap();

let selected_modules: Vec<ModuleType> = selections
let mut selected_modules: Vec<ModuleType> = selections
.iter()
.map(|i| ModuleType::from(*multiselected.get(*i).unwrap()))
.collect();

if selected_modules.contains(&ModuleType::Repository(None)) {
let methods: &[RepositoryOperations; 5] = &[
RepositoryOperations::Find,
RepositoryOperations::FindMany,
RepositoryOperations::Create,
RepositoryOperations::Delete,
RepositoryOperations::Update,
];

let selections = MultiSelect::with_theme(&ColorfulTheme::default())
.with_prompt("Select which repository methods to create")
.items(&methods[..])
.defaults(&defaults[..])
.interact()
.unwrap();

let selected_repositories: Vec<RepositoryOperations> = selections
.iter()
.map(|&i| methods.get(i).unwrap().clone())
.collect();

let index = selected_modules
.iter()
.position(|item| *item == ModuleType::Repository(None))
.unwrap();

selected_modules[index] = ModuleType::Repository(Some(selected_repositories))
};

write_modules(selected_modules, &dir, &module_path, selected_model)
}