Skip to content

Commit

Permalink
docs(examples): show some quick start examples
Browse files Browse the repository at this point in the history
  • Loading branch information
tr1v3r committed Nov 30, 2021
1 parent 098f259 commit c9f14cc
Show file tree
Hide file tree
Showing 16 changed files with 680 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ _testmain.go
/.vscode
__debug_bin
/test/

*.db
7 changes: 7 additions & 0 deletions examples/README.md
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.
45 changes: 45 additions & 0 deletions examples/biz/query.go
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)
}
}
26 changes: 26 additions & 0 deletions examples/cmd/gen/generate.go
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()
}
19 changes: 19 additions & 0 deletions examples/cmd/gen/prepare.go
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)
}
55 changes: 55 additions & 0 deletions examples/cmd/sync_table/generate.go
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()
}
19 changes: 19 additions & 0 deletions examples/cmd/sync_table/prepare.go
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)
}
39 changes: 39 additions & 0 deletions examples/cmd/ultimate/generate.go
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()
}
19 changes: 19 additions & 0 deletions examples/cmd/ultimate/prepare.go
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)
}
5 changes: 5 additions & 0 deletions examples/conf/mysql.go
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"
20 changes: 20 additions & 0 deletions examples/dal/model/mytables.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions examples/dal/mysql.go
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
}
80 changes: 80 additions & 0 deletions examples/dal/query/gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c9f14cc

Please sign in to comment.