Documentation ¶
Index ¶
- Constants
- func FillRelation(rValF reflect.Value, relName string)
- func Scan(rows *sql.Rows, col schema.Collection, query *schema.Query) error
- type Adapter
- type BaseAdapter
- func (o *BaseAdapter) Exec(q string, bind ...any) (err error)
- func (o *BaseAdapter) GetMigrationSQL() string
- func (o *BaseAdapter) InitMigrations(table string) error
- func (o *BaseAdapter) Open(dsn string) error
- func (o *BaseAdapter) Rows(q string, bind ...any) (err error)
- func (o *BaseAdapter) ScanCollection(col schema.Collection, query *schema.Query) (err error)
- func (o *BaseAdapter) ScanModel(m schema.Model, query *schema.Query) (mOut schema.Model, err error)
- func (o *BaseAdapter) ScanRelations(sch *schema.Schema, col schema.Collection, query *schema.Query) (err error)
- func (o *BaseAdapter) Type() string
- func (o *BaseAdapter) Values(q string, bind ...any) (ss []string, err error)
- type DB
- type DBSeeder
- type Filter
- type Migrate
- func (o *Migrate) AbsDir() (string, error)
- func (o *Migrate) Apply(m *Migration) error
- func (o *Migrate) CreateMigration(name string) (m *Migration, err error)
- func (o *Migrate) EnsureTable() error
- func (o *Migrate) Files() ([]string, error)
- func (o *Migrate) FilteredFiles(filter Filter) ([]string, error)
- func (o *Migrate) Init() (err error)
- func (o *Migrate) Migrate() (err error)
- func (o *Migrate) Migrated() (ss []string, err error)
- func (o *Migrate) MigrationExists(name string) (bool, error)
- func (o *Migrate) Path(p string) (string, error)
- func (o *Migrate) RecordMigrated(name string, batch uint32) error
- func (o *Migrate) SetConfig(cfg *config.MigrateConfig)
- type Migration
- func (m *Migration) Create() (err error)
- func (o *Migration) Exists() (bool, error)
- func (m *Migration) ExtractTableName(alt string) (t string)
- func (o *Migration) FullPath() (string, error)
- func (o *Migration) Read() (c string, err error)
- func (o *Migration) Remove() (err error)
- func (o *Migration) Write(sql string) (err error)
- type Migrator
- type MySqlAdapter
- func (o *MySqlAdapter) Exec(q string, bind ...any) (err error)
- func (o *MySqlAdapter) GetMigrationSQL() string
- func (o *MySqlAdapter) InitMigrations(table string) error
- func (o *MySqlAdapter) Open(dsn string) error
- func (o *MySqlAdapter) Rows(q string, bind ...any) (err error)
- func (o *MySqlAdapter) ScanCollection(col schema.Collection, query *schema.Query) (err error)
- func (o *MySqlAdapter) ScanModel(m schema.Model, query *schema.Query) (mOut schema.Model, err error)
- func (o *MySqlAdapter) ScanRelations(sch *schema.Schema, col schema.Collection, query *schema.Query) (err error)
- func (o *MySqlAdapter) Type() string
- func (o *MySqlAdapter) Values(q string, bind ...any) (ss []string, err error)
- type Session
- type SqliteAdapter
- func (o *SqliteAdapter) Exec(q string, bind ...any) (err error)
- func (o *SqliteAdapter) GetMigrationSQL() string
- func (o *SqliteAdapter) InitMigrations(table string) error
- func (o *SqliteAdapter) Open(dsn string) error
- func (o *SqliteAdapter) Rows(q string, bind ...any) (err error)
- func (o *SqliteAdapter) ScanCollection(col schema.Collection, query *schema.Query) (err error)
- func (o *SqliteAdapter) ScanModel(m schema.Model, query *schema.Query) (mOut schema.Model, err error)
- func (o *SqliteAdapter) ScanRelations(sch *schema.Schema, col schema.Collection, query *schema.Query) (err error)
- func (o *SqliteAdapter) Type() string
- func (o *SqliteAdapter) Values(q string, bind ...any) (ss []string, err error)
Constants ¶
const ( StatusMigrated = -1 StatusNotMigrated = 1 )
Migration statuses @see Filter.Status
Variables ¶
This section is empty.
Functions ¶
func FillRelation ¶
Types ¶
type Adapter ¶
type Adapter interface { // For default behaviour, see DefaultAdapter.Open Open(string) error // For default behaviour, see DefaultAdapter.Exec Exec(string, ...any) error // For default behaviour, see DefaultAdapter.Rows Rows(string, ...any) error // ScanRow takes the first arg as object // and tries to scan Row values from database source into it. ScanModel(schema.Model, *schema.Query) (mOut schema.Model, err error) // ScanRows takes the first arg as objects // and tries to scan Rows from database source into it // based on the Query we passed // E.g. // var menuCol MenuCollection // ScanRows(menuCol, NewQuery("menu", menuCol.BaseFIelds())) ScanCollection(schema.Collection, *schema.Query) error // ScanRelations looks for any given relations set in Query, // and queries the DB fro those and than populates into the Collection ScanRelations(*schema.Schema, schema.Collection, *schema.Query) error // Values TODO Generics (to replace "string")! Values(string, ...any) ([]string, error) // Type is one of DBAdapterMySql, DBAdapterSqlite, etc Type() string // InitMigrations creates migrations table in DB InitMigrations(table string) error }
func AdapterFactory ¶
AdapterFactory creates new Adapter implementation based on adapter name (mysql, sqlite, etc)
type BaseAdapter ¶
BaseAdapter
func NewBaseAdapter ¶
func NewBaseAdapter(parent string) *BaseAdapter
func (*BaseAdapter) Exec ¶
func (o *BaseAdapter) Exec(q string, bind ...any) (err error)
Exec implements Adatpter interface. It only executes sql query
func (*BaseAdapter) GetMigrationSQL ¶
func (o *BaseAdapter) GetMigrationSQL() string
Type returns the type name of the adapter: "mysql", "sqlite", etc
func (*BaseAdapter) InitMigrations ¶
func (o *BaseAdapter) InitMigrations(table string) error
// InitMigrations creates the migrations table in the DB
func (*BaseAdapter) Open ¶
func (o *BaseAdapter) Open(dsn string) error
Open implements Adatpter interface
func (*BaseAdapter) Rows ¶
func (o *BaseAdapter) Rows(q string, bind ...any) (err error)
Rows implements Adatpter interface. It returns rows typical for SELECT statement
func (*BaseAdapter) ScanCollection ¶
func (o *BaseAdapter) ScanCollection(col schema.Collection, query *schema.Query) (err error)
ScanRows implements db.Adapter.ScanRows
func (*BaseAdapter) ScanModel ¶
ScanModel implements db.Adapter.ScanModel While ScanCollection scans into the passed collection, this fnction returns new model instance
func (*BaseAdapter) ScanRelations ¶
func (o *BaseAdapter) ScanRelations(sch *schema.Schema, col schema.Collection, query *schema.Query) (err error)
ScanRows implements db.Adapter.ScanRelations
func (*BaseAdapter) Type ¶
func (o *BaseAdapter) Type() string
Type returns the type name of the adapter: "mysql", "sqlite", etc
type DB ¶
type DB struct { Config *config.DBConfig Migrator Migrator Logger log.Logger Adapter Adapter Schema *schema.Schema // Models is the holder for all models that we want to configue // along with default code models that are configured automativally Models []schema.Model Seeders []DBSeeder }
func DBWithConfig ¶
WithConfig returns new DB instance with specified db config
func (*DB) Init ¶
Init checks needed data and tries to connect to database also, tries to migrate any migrations available
func (*DB) RegisterModels ¶
RegisterModels allows custom models registering that will be part of complete db Schema along with core models.
func (*DB) RegisterSeeder ¶
RegisterSeeder keeps track aff all handler functions that we need to call with DB object, so they can all seed thir parts to the DB
type Filter ¶
type Filter struct { // current // -1 <= | => 1 // M1 | M2 | M3 | M4 | M5 | M6 | // -1 migrated (left) , 1 unmigrated ( right ) Status uint8 }
Filter helps to filter all migration files available
type Migrate ¶
type Migrate struct { Config *config.MigrateConfig DB *DB // contains filtered or unexported fields }
func NewMigrate ¶
NewMigrate craetes new instance of the Migrate (Manager) MIgrator also requeres MigrateConfig, to create with config see NewMigrateWithConfig
func NewMigrateWithConfig ¶
func NewMigrateWithConfig(db *DB, cfg *config.MigrateConfig) (m *Migrate)
NewMigrateWithConfig creates new instance of Migrate, and immediately sets the proper config of MigrateConfig
func (*Migrate) CreateMigration ¶
CreateMigration accepts name like "create_users_table" or "Create Users Table" and creates migration file
func (*Migrate) EnsureTable ¶
EnsureTable ensures that the migrations table in the DB exists
func (*Migrate) Files ¶
Files returns all migration files available, no matter if they are already executed or not
func (*Migrate) FilteredFiles ¶
Returns slice of files filtered by status, etc.
func (*Migrate) Migrate ¶
Migrate executes all migrations that are not already executed in up direction
func (*Migrate) Migrated ¶
Migrated returns all migrated migrations (string names) from the database
func (*Migrate) MigrationExists ¶
MigrationExists checks if the migration file exists
in the migrations directory
func (*Migrate) RecordMigrated ¶
Migrated returns all migrated migrations (string names) from the database
func (*Migrate) SetConfig ¶
func (o *Migrate) SetConfig(cfg *config.MigrateConfig)
In some cases, we want to update the migrator config, so this func provides a way to do it
type Migration ¶
type Migration struct { // Pointer to the Migrate (Manager) Migrate *Migrate // Id mirror in database Id schema.ObjectId // Name is migration file name (no dir) and also database name Name string // While Name can be changed with adding timestamp and extension, // OrigName is the name we used initially to create the migration OrigName string }
Migration is mirror to single migration file
func FromName ¶
FromName retruns new Migration instance based on the (file) name. This is reversing of the situation when we want to get file name based on some string like "Create Users Table". Here we want to get a Migration from e.g. "200000000000_create_users_table.sql"
func NewMigration ¶
NewMigration creates new migration struct without ceateing any file in the system. It takes migration manager (mm) as the argument, so it has access to the config. To create migration use: Migrate.CreateMigration
func (*Migration) ExtractTableName ¶
ExtractTableName tries to extract table name from the migration name. e.g. '20220712121033_create_users_table.sql' will extract 'users'
type MySqlAdapter ¶
MySqlAdapter
func (*MySqlAdapter) Exec ¶
func (o *MySqlAdapter) Exec(q string, bind ...any) (err error)
Exec implements Adatpter interface. It only executes sql query
func (*MySqlAdapter) GetMigrationSQL ¶
func (o *MySqlAdapter) GetMigrationSQL() string
Type returns the type name of the adapter: "mysql", "sqlite", etc
func (*MySqlAdapter) InitMigrations ¶
func (o *MySqlAdapter) InitMigrations(table string) error
// InitMigrations creates the migrations table in the DB
func (*MySqlAdapter) Open ¶
func (o *MySqlAdapter) Open(dsn string) error
Open implements Adatpter interface
func (*MySqlAdapter) Rows ¶
func (o *MySqlAdapter) Rows(q string, bind ...any) (err error)
Rows implements Adatpter interface. It returns rows typical for SELECT statement
func (*MySqlAdapter) ScanCollection ¶
func (o *MySqlAdapter) ScanCollection(col schema.Collection, query *schema.Query) (err error)
ScanRows implements db.Adapter.ScanRows
func (*MySqlAdapter) ScanModel ¶
func (o *MySqlAdapter) ScanModel(m schema.Model, query *schema.Query) (mOut schema.Model, err error)
ScanModel implements db.Adapter.ScanModel While ScanCollection scans into the passed collection, this fnction returns new model instance
func (*MySqlAdapter) ScanRelations ¶
func (o *MySqlAdapter) ScanRelations(sch *schema.Schema, col schema.Collection, query *schema.Query) (err error)
ScanRows implements db.Adapter.ScanRelations
func (*MySqlAdapter) Type ¶
func (o *MySqlAdapter) Type() string
Type returns the type name of the adapter: "mysql", "sqlite", etc
type Session ¶
type Session struct { DB *DB // contains filtered or unexported fields }
func (*Session) All ¶
func (s *Session) All() (schema.Collection, error)
All returns all models from the database
func (*Session) Get ¶
func (s *Session) Get() (schema.Collection, error)
All returns all models from the database
type SqliteAdapter ¶
SqliteAdapter
func (*SqliteAdapter) Exec ¶
func (o *SqliteAdapter) Exec(q string, bind ...any) (err error)
Exec implements Adatpter interface. It only executes sql query
func (*SqliteAdapter) GetMigrationSQL ¶
func (o *SqliteAdapter) GetMigrationSQL() string
Type returns the type name of the adapter: "mysql", "sqlite", etc
func (*SqliteAdapter) InitMigrations ¶
func (o *SqliteAdapter) InitMigrations(table string) error
// InitMigrations creates the migrations table in the DB
func (*SqliteAdapter) Open ¶
func (o *SqliteAdapter) Open(dsn string) error
Open implements Adatpter interface
func (*SqliteAdapter) Rows ¶
func (o *SqliteAdapter) Rows(q string, bind ...any) (err error)
Rows implements Adatpter interface. It returns rows typical for SELECT statement
func (*SqliteAdapter) ScanCollection ¶
func (o *SqliteAdapter) ScanCollection(col schema.Collection, query *schema.Query) (err error)
ScanRows implements db.Adapter.ScanRows
func (*SqliteAdapter) ScanModel ¶
func (o *SqliteAdapter) ScanModel(m schema.Model, query *schema.Query) (mOut schema.Model, err error)
ScanModel implements db.Adapter.ScanModel While ScanCollection scans into the passed collection, this fnction returns new model instance
func (*SqliteAdapter) ScanRelations ¶
func (o *SqliteAdapter) ScanRelations(sch *schema.Schema, col schema.Collection, query *schema.Query) (err error)
ScanRows implements db.Adapter.ScanRelations
func (*SqliteAdapter) Type ¶
func (o *SqliteAdapter) Type() string
Type returns the type name of the adapter: "mysql", "sqlite", etc