Documentation ¶
Index ¶
- func ClaimEntity[T interface{}](db *Database, entity T) T
- func DeleteEntity[T interface{}](db *Database, entity T) T
- func GetProperties(table interface{}, format string, is_defined_only bool) []string
- func JoinMapValues(input map[string]string, sep string) string
- func Populate[T interface{}](db *Database, table T, is_recursive bool) (T, bool)
- func RegisterTables(tables ...interface{})
- type Collection
- type Database
- type Table
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ClaimEntity ¶
func ClaimEntity[T interface{}](db *Database, entity T) T
Creates a row if it doesn't exist. Uses the PRIMARY KEY to find row inside of database.
func DeleteEntity ¶
func DeleteEntity[T interface{}](db *Database, entity T) T
func GetProperties ¶
Get all fields that aren't equal to default value. Meanining they were explicitly set.
func Populate ¶
Fills all undefined entity fields from database. Only works for a single entity. To query multiple entities use orm.Collection
Example: orm.Populate(db, MyTable{Id:"hello"})
is_recursive tells whether to query all underlying foreign objects. Can reduce unnecessary database queries.
func RegisterTables ¶
func RegisterTables(tables ...interface{})
Register tables before you use them. It will NOT perform any operations on the database. It simply creates a local map of all existing tables.
Types ¶
type Collection ¶
type Collection struct {
Table *Table
}
A collection of tables. Use this instead of []orm.Table because it implements the correct SQL queries for getting and managing rows.
type Database ¶
Proxy for sql.DB that attaches custom methods
func NewDatabase ¶
Creates a new Database after connecting to MySQL using DSN. Panics if driver fails to connect or fails to ping the database.
Example: db := orm.NewDatabase(os.Getenv("DSN")) defer db.SQL.Close()
func (*Database) CreateTable ¶
Creates an SQL table. Does not check if table already exists. Refer to Database.SyncTables() for syncronization instead.
func (*Database) SyncTables ¶
func (db *Database) SyncTables()
Synchronizes tables with tables on the SQL server. It creates tables that don't exist and alters tables that don't match the structure.
Make sure to orm.RegisterTables() first.
type Table ¶
type Table struct {
DB *Database
}
Represents a single table/entity.
Automatically sets PRIMARY KEY to "Id" field. The "Id" field is transformed into <table_name>_id in database. Foreign keys should start with "Fk" followed by the table struct name and have the type of the struct.
Example:
type MyTable struct { *orm.Table Id string `type:"char(32)" with:""` IsCool bool `type:"boolean" with:"NOT NULL"` FkAnimal Animal `type:"" with:"NOT NULL"` }