fraenky8 / tables-to-go

convert your database tables to structs easily
MIT License
233 stars 42 forks source link

Specify table(s) and generated filename #56

Open guhkun13 opened 1 year ago

guhkun13 commented 1 year ago

Hi, thanks for great repo.

I wonder if it is possible if we can specify which table(s) that we'll be convert into struct. maybe using flag table ? and also option to define the generated filename, such as model.go

example 1: tables-to-go -d booking_app -table invoice -of /internal/domain/invoice -o model
--> will create file /internal/domain/invoice/model.go

example 2: tables-to-go -d booking_app -table order -of /internal/domain/order -o model
--> will create file /internal/domain/order/model.go

I think this would allow us to be more selective on generating the struct and saving it into each domain folder with specified filename.

fraenky8 commented 1 year ago

Hi!

I like the idea to filter by tables via (multiple) -table flags 👍

For the file name we need to decide what behavior we want:

1) If multiple tables are specified then also multiple file-names (in same order) needs to be specified? 2) Or we restrict the usage of only one -table flag and one -o flag at the same time? 3) ???

Tbh I would like to keep the scope of the tool small, don't make it more complex as it is already. We can start on the -table flag first before moving on to the file-name output? Because I think these are 2 different new features.

blackfyre commented 6 days ago

Hey :smile:

My 2 cents: I'm fine with the filtering only and with keeping the existing rules around filenames

fraenky8 commented 2 days ago

Hey guys!

I decided to split this issue into two parts:

1) Add the -table flag to filter for specific tables:

Please checkout PR https://github.com/fraenky8/tables-to-go/pull/60 and give it a try. The new -table flag is applied on SQL level to filter for the table names. If everything looks good, will merge 👍 LMK!

2) Add support for the -of flag to accept a filename in addition to the a path.

I have multiple ideas on this: First, if a file-name is given (rule will be likely *.go file) - then only one -table flag has to be given. But thinking about it further, maybe we can provide a placeholder in the path like %s which gets substituted by the table names in the order specified and hence, different directories but same file name will be possible:

tables-to-go -d booking_app -table invoice,order -of /internal/domain/%s/model.go

Will produce:

/internal/domain/invoice/model.go
/internal/domain/order/model.go

If there is no placeholder but a file-ending given in -of then the CLI will error.

Another approach would be to support multiple -of flags as well which apply in same order as the table flag.

LMK WDYT

blackfyre commented 1 day ago

@fraenky8

Thanks for taking this up! I really like the approach! Albeit there's 1 problem :sweat_smile: I wasn't able to make it work... I've downloaded the repo, switched over to the PR branch, built it, added the -table flag with a comma separated list of table names but it still dumps all the table in the DB. The mysql 5.7 DB I'm working on contains 200+ tables with varied naming conventions (snake, camel, you name it...) applied to the table names.

fraenky8 commented 1 day ago

OMG I was so focused to add the new flag and integrate it into the SQL query that I forgot to actually pass it down 🤦‍♂️

@blackfyre I added one more commit, please update your local branch, run make install and try again! I tested on my side and it seems to work 🤞

blackfyre commented 1 day ago

Can confirm, it's working :+1:

fraenky8 commented 1 day ago

Awesome, glad to hear! I will test with other databases later this week and if everything appears to be good, will merge into master 👍