Documentation ¶
Overview ¶
Package dbcommon provides common database functionality
Index ¶
- Constants
- Variables
- func EnumScan(src interface{}) (uint8, error)
- func EnumValue(enum EnumInter) (driver.Value, error)
- func GetGormLogger(zapLogger *log.ZapEventLogger) gormLogger.Interface
- func GetModelName(db *gorm.DB, model interface{}) (string, error)
- type DBType
- type Enum
- type EnumInter
- type Namer
Examples ¶
Constants ¶
const EnumDataType = "integer"
EnumDataType is exported here to be passed as GormDataType.
Variables ¶
var AllDBTypes []DBType
AllDBTypes is a list of all contract types. Since we use stringer and this is a testing library, instead of manually copying all these out we pull the names out of stringer. In order to make sure stringer is updated, we panic on any method called where the index is higher than the stringer array length.
Functions ¶
func EnumScan ¶ added in v0.0.37
EnumScan converts the enum to a value. the returned uint 8 should be used. Note this should only be used for a non-nullable enum. TODO: consider using generics to make this more type safe.
func GetGormLogger ¶
func GetGormLogger(zapLogger *log.ZapEventLogger) gormLogger.Interface
GetGormLogger gets a gorm logger at the correct level TODO investigate https://github.com/moul/zapgorm, we want to use the same write group.
Types ¶
type DBType ¶
type DBType int
DBType is the database driver to use.
func DBTypeFromString ¶
DBTypeFromString parses a database type from a string.
type Enum ¶ added in v0.0.37
Enum is a type used to define interfaces db enums must conform to. it is kept here so it can be used throughout the app.
Example ¶
ExampleEnum demonstrates example use of the enum interface. this implementation can be confusing, so there's an example below.
res, err := RunEnumExample(context.Background(), fmt.Sprintf("%s/sql.db", os.TempDir())) if err != nil { panic(err) } for _, res := range res { fmt.Printf("got result %s \n", res.Fruit.String()) }
Output:
type EnumInter ¶ added in v0.0.37
type EnumInter interface {
Int() uint8
}
EnumInter ensures an enum has a type to access an integer.
type Namer ¶
type Namer struct {
// contains filtered or unexported fields
}
Namer is used to pull consistent names for fields from models. This prevents inconsistency by panicing on breaking changes. It also allows us to avoid using xModelFieldName, yModelFieldName by taking advantage of introspection.
func (Namer) GetConsistentName ¶
GetConsistentName makes sure a `ColumnName` has a common column tag against all models we use this so we don't have to define bridgeRedeemModelFieldName, originChainFieldName, etc. panic's on an inconsistency. Note: this should only be called from init.
after this is called, you can safely call getGormFieldName(AnyModelWithField, fieldName) if all fields cannot make this guarantee, they should be separated out to avoid developer confusion.
this returns the result of getGormFieldName for a valid model. It also panics if no model is valid todo consider generating getters for all field names.