第二期Go開源說實錄:GORM 剖析與最佳實踐

關(guān)聯(lián):一對一、一對多、單表自關(guān)聯(lián)、多態(tài)關(guān)聯(lián);Preload、Joins 預(yù)加載;關(guān)聯(lián)模式
事務(wù):嵌套事務(wù), Save Point
Hooks、Callbacks 自由擴展
多數(shù)據(jù)庫、讀寫分離、Prometheus、Prepared Stmt、查詢優(yōu)化器、批量數(shù)據(jù)處理、代碼共享、子查詢、DryRun
SQL Builder、Smart Migration、復(fù)合主鍵、自定義類型 (JSON等)、SQL 表達式查詢創(chuàng)建更新、虛擬字段…
真 ? 跨數(shù)據(jù)庫兼容等功能

Method Chain 的線程安全
https://gorm.cn/zh_CN/docs/method_chaining.html
防止 SQL 注入
https://gorm.cn/zh_CN/docs/security.html
錯誤處理
https://gorm.cn/zh_CN/docs/error_handling.html
GORM 優(yōu)于配置的一些約定
https://gorm.cn/zh_CN/docs/models.html#Conventions
如何給字段來配置讀寫權(quán)限
https://gorm.cn/zh_CN/docs/models.html#field_permission
給多字段配置時間追蹤
https://gorm.cn/zh_CN/docs/models.html#time_tracking
自關(guān)聯(lián)定義 (一對一,多對多,一對多,單表自關(guān)聯(lián),多態(tài))、自定義 foreign key, reference、復(fù)合外鍵、自定義 JoinTable
Query 的一些 API: Pluck, FirstOrInit, FirstOrCreate (Assign, Attrs)
https://gorm.cn/zh_CN/docs/advanced_query.html#FirstOrInit
Migrations
https://gorm.cn/zh_CN/docs/migration.html
字段的 Tags 特殊配置支持
https://gorm.cn/zh_CN/docs/models.html#tags
如何使用 Context
https://gorm.cn/zh_CN/docs/context.html
Transactions, Nested Transactions, Save Point, RollbackTo to Saved Point
https://gorm.cn/zh_CN/docs/transactions.html
Gorm Config (跳過默認事務(wù)、修改命名策略、修改當前時間函數(shù)、只生成 SQL 不執(zhí)行模式、Prepared Stmt 加速模式等等)
https://gorm.cn/zh_CN/docs/gorm_config.html
Session 模式概念及其配置 (如:跳過默認事務(wù)、修改命名策略、修改當前時間函數(shù)、只生成 SQL 不執(zhí)行模式、Prepared Stmt 加速模式等等)
https://gorm.cn/zh_CN/docs/session.html
定義索引、復(fù)合索引、優(yōu)先索引、約束等
https://gorm.cn/zh_CN/docs/indexes.html https://gorm.cn/zh_CN/docs/constraints.html
數(shù)據(jù)庫連接池配置
https://gorm.cn/zh_CN/docs/generic_interface.html
數(shù)據(jù)庫的不同連接參數(shù),以 mysql 為例子
https://github.com/go-gorm/mysql#gorm-mysql-driver
Hooks 介紹
https://gorm.cn/zh_CN/docs/hooks.html
讀寫分離 / 多數(shù)據(jù)庫
https://gorm.cn/zh_CN/docs/dbresolver.html
使用查詢優(yōu)化器,指定索引查詢
https://gorm.cn/zh_CN/docs/hints.html
Prometheus 集成
https://gorm.cn/zh_CN/docs/prometheus.html
使用復(fù)合主鍵
https://gorm.cn/zh_CN/docs/composite_primary_key.html
分庫分表 / 代碼共享
https://gorm.cn/zh_CN/docs/scopes.html
Embedded Struct 定義共享 struct (參考 embedded tag)
自定義數(shù)據(jù)類型(json 等數(shù)據(jù)類型支持)
https://gorm.cn/zh_CN/docs/data_types.html
如何提升性能
https://gorm.cn/zh_CN/docs/performance.html
多看源碼,貢獻社區(qū) ??
https://github.com/go-gorm/gorm
熟悉 Statement & Clause 概念
https://www2.slideshare.net/JinzhuZhang2/gorm-gopher-china
定制 Callbacks 插件
https://gorm.cn/zh_CN/docs/write_plugins.html
了解如何定制 driver 實現(xiàn)特殊需求 (各 driver 源碼,
https://gorm.cn/zh_CN/docs/write_driver.html)
如何定制 logger ?
https://gorm.cn/zh_CN/docs/logger.html
Set/Get/InstanceSet/InstanceGet Callback 傳遞參數(shù)
https://gorm.cn/zh_CN/docs/settings.html
1.如何設(shè)置表名前綴
db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{NamingStrategy: schema.NamingStrategy{TablePrefix: "t_", // NOTE 這里SingularTable: true,SingularTable: true,},})
默認的 NamingStrategy 可以實現(xiàn)一些常見的配置,如果不能滿足需求的話,可以選擇自定義 Namer 的 interface,例如:
type Namer interface {TableName(table string) stringColumnName(table, column string) stringJoinTableName(table string) stringRelationshipFKName(Relationship) stringCheckerName(table, column string) stringIndexName(table, column string) string}
import "github.com/DATA-DOG/go-sqlmock"mockdb, mock, err := sqlmock.New()
import ("gorm.io/gorm""gorm.io/driver/mysql")gormDB, err := gorm.Open(mysql.New(mysql.Config{Conn: mockdb,}), &gorm.Config{})
db.Statement 獲取當前的條件,然后根據(jù)這些條件從相應(yīng)的其它的數(shù)據(jù)庫、緩存數(shù)據(jù)庫中查詢出相應(yīng)數(shù)據(jù)并賦值回原對象中。

