Gorm: proposal: generate structs from schema info

Created on 21 Jul 2015  路  11Comments  路  Source: go-gorm/gorm

Hi,
I'm started to write my own orm like package for learning proposes,
it's not fully featured as gorm (not even close) but It has a command line tool that can generate files with structs from database information. This saves a lot of time when working with big databases (hundreds of tables).
Are you interested to have this feature in gorm? If so, I can port and adapt my existing code to a PR.
The use case would be

go get -u github.com/jinzhu/gorm
gorm generate -d dbname -o outputpath

gorm generate could generate all structs needed under outputpath with gorm tags included.
Another option is that I add the option to generate gorm ready structs to my existing yao gen command.

Thoughts ?

Most helpful comment

Any updates? really needs this feature!

All 11 comments

Hi @alfonsodev

Sure, it is good to have a tool to generate models from database schema, but I don't really want to integrate it into gorm itself.

Just want gorm focus on the ORM related things, but tools like this could be third party tools.

This is a start: https://gist.github.com/rsperl/f0b9d48f38cd6aeccc81 . You'll have to set env vars DBNAME, DBUSER, DBPASS, DBHOST for db info.

./gorm-generate-types.pl --package mypackage --filename MyTypes.go

The types map needs some work, but this should provide a pretty good start for generating types for database DBNAME.

I actually think this would be a GREAT idea, similar to what dbicdump does for perl's DBIx::Class, but to be feasible it should be written in GO, and should have an API to allow for custom database types to plugin, so it can work with different drivers and DB backends. I'm going to try to build one.

I'd like to hear more from this.

Any updates?

Any updates? really needs this feature!

Any updates? really needs this feature!

So, there is no even third-party tools for this?

good feature, it will be helpful for freelance programmers to cut sort the coding time a lot when it comes to handling database in Golang.

Wish authro can provide this feature, thrid-party hard to compatibility perfact.

Hi, look at this repository
https://github.com/opannapo/db-to-struct

Execute JAR file :
Create one -> --init ,, --tbl
Create All -> --init ,, --tbl all

For example :
java -cp ./db-to-struct.jar Main --init napouser,napouser,MyDataBase --tbl tbl_user

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rfyiamcool picture rfyiamcool  路  3Comments

fieryorc picture fieryorc  路  3Comments

koalacxr picture koalacxr  路  3Comments

Ganitzsh picture Ganitzsh  路  3Comments

leebrooks0 picture leebrooks0  路  3Comments