orm

package
v0.0.0-...-47bdc06 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 17, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

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

func GetProperties(table interface{}, format string, is_defined_only bool) []string

Get all fields that aren't equal to default value. Meanining they were explicitly set.

func JoinMapValues

func JoinMapValues(input map[string]string, sep string) string

func Populate

func Populate[T interface{}](db *Database, table T, is_recursive bool) (T, bool)

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

type Database struct {
	SQL *sql.DB
}

Proxy for sql.DB that attaches custom methods

func NewDatabase

func NewDatabase(dsn string) *Database

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

func (db *Database) CreateTable(src source.Source)

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.

func (*Database) VoidExec

func (db *Database) VoidExec(query string, args ...any)

Executes a query and completely discards the result. Panics if there has been an error performing db.Exec().

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"`
	}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL