sea-orm
sea-orm copied to clipboard
Support to Programmatically Generate Entities
Motivation
It would be great to be able to generate entities similar to how migrations can be run programmatically. As of today we have to install the sea-orm-cli manually in order to have entities generated:
sea-orm-cli generate entity -h
This would improve DX by using a single command (cargo run) when running a solution
using Sea ORM on development.
Proposed Solutions
Having an API similar to the one using by Migrator could be a first approach.
use sea_orm::entity::Generator;
Generator::generate_entities()
I think we could reuse the code from the Sea ORM Cli similar to what we do for migrations nowadays.
Additional Information
My goal is to improve DX by allowing the user to provide custom scripts by taking advantage of the CLI as an API on runtime.
I can't believe you're doing the same I'm! :smile:
I opened https://github.com/SeaQL/sea-orm/issues/1046 just before you!
This is my code:
use sea_orm_cli::{run_generate_command, DateTimeCrate};
pub async fn gen() {
run_generate_command(
sea_orm_cli::GenerateSubcommands::Entity {
database_url: "...database_url here...",
output_dir: "./entities".to_string(),
compact_format: false,
expanded_format: false,
include_hidden_tables: false,
tables: None,
ignore_tables: vec!["".to_string()],
max_connections: 1,
database_schema: "public".to_string(),
with_serde: "none".to_string(),
with_copy_enums: false,
date_time_crate: DateTimeCrate::Time,
},
true,
)
.await
.unwrap();
}
I can't believe you're doing the same I'm! 😄
I opened #1046 just before you!
This is my code:
use sea_orm_cli::{run_generate_command, DateTimeCrate}; pub async fn gen() { run_generate_command( sea_orm_cli::GenerateSubcommands::Entity { database_url: "...database_url here...", output_dir: "./entities".to_string(), compact_format: false, expanded_format: false, include_hidden_tables: false, tables: None, ignore_tables: vec!["".to_string()], max_connections: 1, database_schema: "public".to_string(), with_serde: "none".to_string(), with_copy_enums: false, date_time_crate: DateTimeCrate::Time, }, true, ) .await .unwrap(); }
LOL! This is great! Yeah I'm working on adding Sea ORM to my project here: https://github.com/whizzes/linx/tree/feat/seaorm and I wanted to have something similar to: https://github.com/whizzes/linx/tree/feat/seaorm/migration for the entity crate. 😄
Hey @EstebanBorai, can you share more on what you want to achieve?
Would depends on sea-orm-codegen instead of sea-orm-cli be a good idea? Just like seaography
- https://github.com/SeaQL/seaography/blob/003770beb6f085dd711019b426aedf501a10ab5a/generator/src/sea_orm_codegen.rs#L9-L19
Hey I have a similar use case: Instead of wrapping the CLI in a Command call, I want to call a library function which behaves the same way sea-orm-cli generate entity does.
@frederikhors proposal looks just what I need/imagine :)
@billy1624 using codegen would probably work, however, controlling output_dir etc would be really nice for developer experience and flexibility :rocket:
Hey @EstebanBorai, can you share more on what you want to achieve?
Would depends on sea-orm-codegen instead of sea-orm-cli be a good idea? Just like seaography
- https://github.com/SeaQL/seaography/blob/003770beb6f085dd711019b426aedf501a10ab5a/generator/src/sea_orm_codegen.rs#L9-L19
Hi @billy1624!
My goal is to be able to run the entity generation through code, @frederikhors makes a good example on it. SQLx also has a similar approach on running database migrations here I have an example of what I did before:
https://github.com/whizzes/linx/blob/6acbed5b8236b57a9ed4f04d72f631b1456e81a0/src/context.rs#L32
I would like to have a function that I can run passing my entities crate and a database url, and have entities generated similar to when I do:
sea-orm-cli generate entity
I think this could help improve DX by not having to manually install the CLI and run the command manually.
Hello everyone, I'll try to draft a PR this afternoon.
I'm thinking to rewrite async fn run_generate_command Pull the logic of it out into another function. The function take a database_url and other optional options (with defaults).
Hey @billy1624, thank you so much for your work and the prompt reply :+1:
Awesome @billy1624!
Hey everyone. please check https://github.com/SeaQL/sea-orm/pull/1054
After second thought, I think sea-orm-cli should be used as a binary instead of as a library.
I suggest everyone to go one level dipper and programmatically generate entity based on sea-orm-codegen.
After second thought, I think
sea-orm-clishould be used as a binary instead of as a library.I suggest everyone to go one level dipper and programmatically generate entity based on
sea-orm-codegen.
Hi @billy1624! Do you have an example of how it would be?
@billy1624 Totally fine with me. One question though: Will all (current and future) CLI options be mirrored in the API of sea-orm-codegen?