diff --git a/.gitignore b/.gitignore index 117f92f5..912d58f7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +TODO documents coverage.txt _book diff --git a/go.mod b/go.mod index 0b3e3065..d0a110ba 100644 --- a/go.mod +++ b/go.mod @@ -1 +1,3 @@ module github.com/jinzhu/gorm + +go 1.13 diff --git a/gorm.go b/gorm.go new file mode 100644 index 00000000..274f4c62 --- /dev/null +++ b/gorm.go @@ -0,0 +1,46 @@ +package gorm + +import ( + "time" + + "github.com/jinzhu/gorm/logger" +) + +// Config GORM config +type Config struct { + // Set true to use singular table name, by default, GORM will pluralize your struct's name as table name + // Refer https://github.com/jinzhu/inflection for inflection rules + SingularTable bool + + // GORM perform single create, update, delete operations in transactions by default to ensure database data integrity + // You can cancel it by setting `SkipDefaultTransaction` to true + SkipDefaultTransaction bool + + // Logger + Logger logger.Interface + + // NowFunc the function to be used when creating a new timestamp + NowFunc func() time.Time +} + +// Model a basic GoLang struct which includes the following fields: ID, CreatedAt, UpdatedAt, DeletedAt +// It may be embeded into your model or you may build your own model without it +// type User struct { +// gorm.Model +// } +type Model struct { + ID uint `gorm:"primary_key"` + CreatedAt time.Time + UpdatedAt time.Time + DeletedAt *time.Time `gorm:"index"` +} + +// Dialector GORM database dialector +type Dialector interface { + Migrator() Migrator +} + +// DB GORM DB definition +type DB struct { + *Config +} diff --git a/logger/logger.go b/logger/logger.go new file mode 100644 index 00000000..87b71013 --- /dev/null +++ b/logger/logger.go @@ -0,0 +1,5 @@ +package logger + +// Interface logger interface +type Interface interface { +} diff --git a/migrator.go b/migrator.go new file mode 100644 index 00000000..c21cda42 --- /dev/null +++ b/migrator.go @@ -0,0 +1,44 @@ +package gorm + +import ( + "database/sql" +) + +// ViewOption view option +type ViewOption struct { + Replace bool + CheckOption string + Query *DB +} + +type Migrator interface { + // AutoMigrate + AutoMigrate(dst ...interface{}) error + + // Tables + CreateTable(dst ...interface{}) error + DropTable(dst ...interface{}) error + HasTable(dst ...interface{}) error + RenameTable(oldName, newName string) error + + // Columns + AddColumn(dst interface{}, field string) error + DropColumn(dst interface{}, field string) error + AlterColumn(dst interface{}, field string) error + RenameColumn(dst interface{}, oldName, field string) error + ColumnTypes(dst interface{}) ([]*sql.ColumnType, error) + + // Views + CreateView(name string, option ViewOption) error + DropView(name string) error + + // Constraints + CreateConstraint(dst interface{}, name string) error + DropConstraint(dst interface{}, name string) error + + // Indexes + CreateIndex(dst interface{}, name string) error + DropIndex(dst interface{}, name string) error + HasIndex(dst interface{}, name string) error + RenameIndex(dst interface{}, oldName, newName string) error +} diff --git a/migrator/migrator.go b/migrator/migrator.go new file mode 100644 index 00000000..0ff83ac1 --- /dev/null +++ b/migrator/migrator.go @@ -0,0 +1,14 @@ +package migrator + +import "github.com/jinzhu/gorm" + +// Migrator migrator struct +type Migrator struct { + *Config +} + +// Config schema config +type Config struct { + CheckExistsBeforeDropping bool + DB *gorm.DB +}