forked from go-gorm/gen
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(examples): show some quick start examples
- Loading branch information
Showing
16 changed files
with
680 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,3 +33,5 @@ _testmain.go | |
/.vscode | ||
__debug_bin | ||
/test/ | ||
|
||
*.db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# examples | ||
|
||
***Run generate.sh*** | ||
|
||
一个简单的`GEN`最佳实践。你可以通过配置`generate.sh`中的`TARGET_DIR`值指定执行不同的代码生成命令。 | ||
|
||
A simple best practice of `GEN`. You can configure `TARGET_DIR` value in `generate.sh` to generate different code. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package biz | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"gorm.io/gen/examples/dal/query" | ||
) | ||
|
||
var q = query.Q | ||
|
||
func Query(ctx context.Context) { | ||
t := q.Mytable | ||
do := t.WithContext(context.Background()) | ||
|
||
data, err := do.Take() | ||
catchError("Take", err) | ||
fmt.Printf("got %+v\n", data) | ||
|
||
dataArray, err := do.Find() | ||
catchError("Find", err) | ||
fmt.Printf("got %+v\n", dataArray) | ||
|
||
data, err = do.Where(t.ID.Eq(1)).Take() | ||
catchError("Take", err) | ||
fmt.Printf("got %+v\n", data) | ||
|
||
dataArray, err = do.Where(t.Age.Gt(18)).Order(t.Username).Find() | ||
catchError("Find", err) | ||
fmt.Printf("got %+v\n", dataArray) | ||
|
||
dataArray, err = do.Select(t.ID, t.Username).Order(t.Age.Desc()).Find() | ||
catchError("Find", err) | ||
fmt.Printf("got %+v\n", dataArray) | ||
|
||
info, err := do.Where(t.ID.Eq(1)).UpdateSimple(t.Age.Add(1)) | ||
catchError("Update", err) | ||
fmt.Printf("got %+v\n", info) | ||
} | ||
|
||
func catchError(detail string, err error) { | ||
if err != nil { | ||
fmt.Printf("%s: %v\n", detail, err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package main | ||
|
||
import ( | ||
"gorm.io/gen" | ||
"gorm.io/gen/examples/conf" | ||
"gorm.io/gen/examples/dal" | ||
) | ||
|
||
func init() { | ||
dal.DB = dal.ConnectDB(conf.MySQLDSN) | ||
|
||
prepare(dal.DB) // prepare table for generate | ||
} | ||
|
||
func main() { | ||
g := gen.NewGenerator(gen.Config{ | ||
OutPath: "../../dal/query", | ||
}) | ||
|
||
g.UseDB(dal.DB) | ||
|
||
// generate all table from database | ||
g.ApplyBasic(g.GenerateAllTable()...) | ||
|
||
g.Execute() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package main | ||
|
||
import ( | ||
"gorm.io/gorm" | ||
) | ||
|
||
// prepare table for test | ||
|
||
const mytableSQL = "CREATE TABLE IF NOT EXISTS `mytables` (" + | ||
" `ID` int(11) NOT NULL," + | ||
" `username` varchar(16) DEFAULT NULL," + | ||
" `age` int(8) NOT NULL," + | ||
" `phone` varchar(11) NOT NULL," + | ||
" INDEX `idx_username` (`username`)" + | ||
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;" | ||
|
||
func prepare(db *gorm.DB) { | ||
db.Exec(mytableSQL) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package main | ||
|
||
import ( | ||
"strings" | ||
|
||
"gorm.io/gen" | ||
"gorm.io/gen/examples/conf" | ||
"gorm.io/gen/examples/dal" | ||
) | ||
|
||
func init() { | ||
dal.DB = dal.ConnectDB(conf.MySQLDSN) | ||
|
||
prepare(dal.DB) // prepare table for generate | ||
} | ||
|
||
// dataMap mapping relationship | ||
var dataMap = map[string]func(detailType string) (dataType string){ | ||
// int mapping | ||
"int": func(detailType string) (dataType string) { return "int32" }, | ||
|
||
// bool mapping | ||
"tinyint": func(detailType string) (dataType string) { | ||
if strings.HasPrefix(detailType, "tinyint(1)") { | ||
return "bool" | ||
} | ||
return "byte" | ||
}, | ||
} | ||
|
||
func main() { | ||
g := gen.NewGenerator(gen.Config{ | ||
OutPath: "../../dal/query", | ||
ModelPkgPath: "../../dal/model", | ||
|
||
// generate model global configuration | ||
FieldNullable: true, // generate pointer when field is nullable | ||
FieldWithIndexTag: true, // generate with gorm index tag | ||
FieldWithTypeTag: true, // generate with gorm column type tag | ||
}) | ||
|
||
g.UseDB(dal.DB) | ||
|
||
// specify diy mapping relationship | ||
g.WithDataTypeMap(dataMap) | ||
|
||
// generate all field with json tag end with "_example" | ||
g.WithJSONTagNameStrategy(func(c string) string { return c + "_example" }) | ||
|
||
mytable := g.GenerateModel("mytables") | ||
g.ApplyBasic(mytable) | ||
// g.ApplyBasic(g.GenerateAllTable()...) // generate all table in db server | ||
|
||
g.Execute() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package main | ||
|
||
import ( | ||
"gorm.io/gorm" | ||
) | ||
|
||
// prepare table for test | ||
|
||
const mytableSQL = "CREATE TABLE IF NOT EXISTS `mytables` (" + | ||
" `ID` int(11) NOT NULL," + | ||
" `username` varchar(16) DEFAULT NULL," + | ||
" `age` int(8) NOT NULL," + | ||
" `phone` varchar(11) NOT NULL," + | ||
" INDEX `idx_username` (`username`)" + | ||
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;" | ||
|
||
func prepare(db *gorm.DB) { | ||
db.Exec(mytableSQL) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main | ||
|
||
import ( | ||
"gorm.io/gen" | ||
"gorm.io/gen/examples/conf" | ||
"gorm.io/gen/examples/dal" | ||
) | ||
|
||
func init() { | ||
dal.DB = dal.ConnectDB(conf.MySQLDSN) | ||
|
||
prepare(dal.DB) // prepare table for generate | ||
} | ||
|
||
var dataMap = map[string]func(detailType string) (dataType string){ | ||
"int": func(detailType string) (dataType string) { return "int64" }, | ||
"json": func(string) string { return "json.RawMessage" }, | ||
} | ||
|
||
func main() { | ||
g := gen.NewGenerator(gen.Config{ | ||
OutPath: "../../dal/query", | ||
Mode: gen.WithDefaultQuery, | ||
|
||
WithUnitTest: true, | ||
|
||
FieldNullable: true, | ||
FieldWithIndexTag: true, | ||
}) | ||
|
||
g.UseDB(dal.DB) | ||
|
||
g.WithDataTypeMap(dataMap) | ||
g.WithJSONTagNameStrategy(func(c string) string { return "-" }) | ||
|
||
g.ApplyBasic(g.GenerateAllTable()...) | ||
|
||
g.Execute() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package main | ||
|
||
import ( | ||
"gorm.io/gorm" | ||
) | ||
|
||
// prepare table for test | ||
|
||
const mytableSQL = "CREATE TABLE IF NOT EXISTS `mytables` (" + | ||
" `ID` int(11) NOT NULL," + | ||
" `username` varchar(16) DEFAULT NULL," + | ||
" `age` int(8) NOT NULL," + | ||
" `phone` varchar(11) NOT NULL," + | ||
" INDEX `idx_username` (`username`)" + | ||
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;" | ||
|
||
func prepare(db *gorm.DB) { | ||
db.Exec(mytableSQL) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package conf | ||
|
||
const MySQLDSN = "root:local_maridb_test@tcp(localhost:3306)/test?charset=utf8mb4&parseTime=True" | ||
|
||
const SQLiteDBName = "gen_sqlite.db" |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package dal | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"gorm.io/driver/mysql" | ||
"gorm.io/driver/sqlite" | ||
"gorm.io/gorm" | ||
) | ||
|
||
var DB *gorm.DB | ||
|
||
func ConnectDB(dsn string) *gorm.DB { | ||
var db *gorm.DB | ||
var err error | ||
|
||
if strings.HasSuffix(dsn, "sqlite.db") { | ||
db, err = gorm.Open(sqlite.Open(dsn), &gorm.Config{}) | ||
} else { | ||
db, err = gorm.Open(mysql.Open(dsn)) | ||
} | ||
|
||
if err != nil { | ||
panic(fmt.Errorf("connect db fail: %s", err)) | ||
} | ||
|
||
return db | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.