this is a module for eav module for go gorm , just support mysql nowgo gorm 的eav 模型, 目前只支持mysql
// define connectioin name// 定義鏈接名字vartestConnectionNamestring// define struct// 定義模型結構體typeUserTeststruct {EntityIduint64NamestringAgeuint32}// define table of db// 定義數據庫名字typeUserTeststruct {EntityIduint64NamestringAgeuint32CreatedAt time.TimeUpdatedAt time.Time}// 返回主表名字func (e*UserTest)GetTableName()string {return"user"}// 返回字段 , 會通過反射 reflect 設置 struct 字段/** Name: struct 的字段名字 必須是public 的字段 IsEav: 是否為eav 字段 DbType: 跟struct 字段一樣類型 EavType: 會根據主表名字+ EavType 來確定 eav 的value 表 Autocreated: 只爲 time.Time 類型處理; 首次創建時會生成 Autoupdate: 只爲 time.Time 類型處理; 每次保存時會生成*/func (e*UserTest)GetTableFields()map[string]Field {fields:=map[string]Field{"entity_id": {Name:"EntityId",IsEav:false,DbType:"uint64"},"name": {Name:"Name",IsEav:true,DbType:"string",EavType:"varchar"},"age": {Name:"Age",IsEav:false,DbType:"uint32"},"created_at": {Name:"CreatedAt",IsEav:false,DbType:"time.Time",Autocreate:true},"updated_at": {Name:"UpdatedAt",IsEav:false,DbType:"time.Time",Autocreate:true,Autoupdate:true},}returnfields}// 主表的 primary keyfunc (e*UserTest)GetPrimaryFieldName()string {return"entity_id"}
// 將 Basictablemodelinterface 轉化為對應類型funcConvertModelToUserTest(tableModelBasictablemodelinterface)*UserTest {model:=tableModel.GetModel()ifm,ok:=model.(*UserTest);ok {returnm}return&UserTest{}}// 獲取 collectionfuncGetUserTestCollectionFactory(localestring,defaultLocalestring)CollectionInterface {callback:=func()Basictablemodelinterface {model:=&Basictablemodel{Model:&UserTest{},Connection:testConnectionName,Locale:locale,DefaultLocale:defaultLocale}returnmodel}returnCollectionFactory(callback)}// 獲取 BasictablemodelinterfacefuncGetUserTestFactory(localestring,defaultLocalestring)Basictablemodelinterface {callback:=func()Basictablemodelinterface {model:=&Basictablemodel{Model:&UserTest{},Connection:testConnectionName,Locale:locale,DefaultLocale:defaultLocale}returnmodel}returnModelFactory(callback)}