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

Support to Programmatically Generate Entities #1047

Closed
EstebanBorai opened this issue Sep 17, 2022 · 12 comments
Closed

Support to Programmatically Generate Entities #1047

EstebanBorai opened this issue Sep 17, 2022 · 12 comments

Comments

@EstebanBorai
Copy link
Contributor

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.

@frederikhors
Copy link
Contributor

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();
}

@EstebanBorai
Copy link
Contributor Author

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. 😄

@billy1624
Copy link
Member

billy1624 commented Sep 19, 2022

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

@JonasCir
Copy link

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 🚀

@EstebanBorai
Copy link
Contributor Author

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

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.

@billy1624
Copy link
Member

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).

@JonasCir
Copy link

Hey @billy1624, thank you so much for your work and the prompt reply 👍

@EstebanBorai
Copy link
Contributor Author

Awesome @billy1624!

@billy1624
Copy link
Member

Hey everyone. please check #1054

@billy1624
Copy link
Member

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.

@billy1624 billy1624 closed this as not planned Won't fix, can't repro, duplicate, stale Dec 2, 2022
@EstebanBorai
Copy link
Contributor Author

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.

Hi @billy1624! Do you have an example of how it would be?

@JonasCir
Copy link

JonasCir commented Dec 4, 2022

@billy1624 Totally fine with me. One question though: Will all (current and future) CLI options be mirrored in the API of sea-orm-codegen?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants