sea-orm icon indicating copy to clipboard operation
sea-orm copied to clipboard

Support to Programmatically Generate Entities

Open LeoBorai opened this issue 3 years ago • 9 comments
trafficstars

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.

LeoBorai avatar Sep 17 '22 19:09 LeoBorai

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

frederikhors avatar Sep 17 '22 21:09 frederikhors

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

LeoBorai avatar Sep 17 '22 23:09 LeoBorai

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

billy1624 avatar Sep 19 '22 10:09 billy1624

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:

JonasCir avatar Sep 20 '22 15:09 JonasCir

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.

LeoBorai avatar Sep 20 '22 15:09 LeoBorai

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

billy1624 avatar Sep 21 '22 06:09 billy1624

Hey @billy1624, thank you so much for your work and the prompt reply :+1:

JonasCir avatar Sep 21 '22 07:09 JonasCir

Awesome @billy1624!

LeoBorai avatar Sep 21 '22 11:09 LeoBorai

Hey everyone. please check https://github.com/SeaQL/sea-orm/pull/1054

billy1624 avatar Sep 21 '22 13:09 billy1624

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 avatar Dec 02 '22 09:12 billy1624

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?

LeoBorai avatar Dec 02 '22 12:12 LeoBorai

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

JonasCir avatar Dec 04 '22 14:12 JonasCir