metaorm

package module
v1.6.8 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2025 License: MIT Imports: 13 Imported by: 0

README

metaorm v1

MetaORM is a Go package that provides a simplified database interaction layer with support for MySQL, PostgreSQL, and SQLite databases. It includes features for connection management, query building, encryption, environment variable handling, and pagination/sorting utilities.

Installation

go get -u github.com/metadiv-io/metaorm

Documentation

metaorm v1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	METAORM_HOST        = env.New("METAORM_HOST", false)
	METAORM_PORT        = env.New("METAORM_PORT", false)
	METAORM_USERNAME    = env.New("METAORM_USERNAME", false)
	METAORM_PASSWORD    = env.New("METAORM_PASSWORD", false)
	METAORM_DATABASE    = env.New("METAORM_DATABASE", false)
	METAORM_ENCRYPT_KEY = env.New("METAORM_ENCRYPT_KEY", false)
)

Functions

func Decrypt

func Decrypt(cipherText []byte, decryptKey string) string

Decrypt decrypts the given cipher text using the given decrypt key.

func DefaultDecrypt added in v1.1.0

func DefaultDecrypt(cipherText []byte) string

DefaultDecrypt decrypts the given cipher text using the METAORM_ENCRYPT_KEY environment variable.

func DefaultEncrypt added in v1.1.0

func DefaultEncrypt(text string) []byte

DefaultEncrypt encrypts the given text using the METAORM_ENCRYPT_KEY environment variable.

func Encrypt

func Encrypt(text string, encryptKey string) []byte

Encrypt encrypts the given text using the given encrypt key.

func NewAndQueryBuilder

func NewAndQueryBuilder() *builder.QueryBuilder

NewAndQueryBuilder returns a new and query builder. The default operator is and.

func NewOrQueryBuilder

func NewOrQueryBuilder() *builder.QueryBuilder

NewOrQueryBuilder returns a new or query builder. The default operator is or.

Types

type DB added in v1.1.0

type DB struct {
	GORM *gorm.DB
}

func DefaultMySQL

func DefaultMySQL() (*DB, error)

DefaultMySQL returns a new database connection for MySQL using the environment variables.

func DefaultPostgres

func DefaultPostgres() (*DB, error)

DefaultPostgres returns a new database connection for Postgres using the environment variables.

func MySQL

func MySQL(host, port, username, password, databaseName string, silent ...bool) (*DB, error)

MySQL returns a new database connection for MySQL.

func NewDB added in v1.1.0

func NewDB(db *gorm.DB) *DB

func Postgres

func Postgres(host, port, username, password, databaseName string, silent ...bool) (*DB, error)

Postgres returns a new database connection for Postgres.

func SQLite

func SQLite(path string, silent ...bool) (*DB, error)

SQLite returns a new database connection for SQLite.

func SqliteMemory

func SqliteMemory() (*DB, error)

SqliteMemory returns a new database connection for SQLite in-memory.

func (*DB) And added in v1.1.0

func (db *DB) And(queries ...*query.Query) *query.Query

And returns a new query with the and operator for the given queries.

func (*DB) AutoMigrate added in v1.1.0

func (db *DB) AutoMigrate(models ...any) error

AutoMigrate runs the auto migration for the given models. It returns an error if the migration fails.

func (*DB) Begin added in v1.1.0

func (db *DB) Begin() *DB

Begin starts a new transaction.

func (*DB) Commit added in v1.1.0

func (db *DB) Commit() *DB

Commit commits the transaction.

func (*DB) Eq added in v1.1.0

func (db *DB) Eq(field string, value any) *query.Query

Eq returns a new query with the equal operator for the given field and value. If the field is prefixed with a *, it will be decrypted.

func (*DB) Gt added in v1.1.0

func (db *DB) Gt(field string, value any) *query.Query

Gt returns a new query with the greater than operator for the given field and value.

func (*DB) Gte added in v1.1.0

func (db *DB) Gte(field string, value any) *query.Query

Gte returns a new query with the greater than or equal operator for the given field and value.

func (*DB) In added in v1.1.0

func (db *DB) In(field string, value []any) *query.Query

In returns a new query with the in operator for the given field and value. If the field is prefixed with a *, it will be decrypted.

func (*DB) IsNotNull added in v1.1.0

func (db *DB) IsNotNull(field string) *query.Query

IsNotNull returns a new query with the is not null operator for the given field.

func (*DB) IsNull added in v1.1.0

func (db *DB) IsNull(field string) *query.Query

IsNull returns a new query with the is null operator for the given field.

func (*DB) Joins added in v1.1.0

func (db *DB) Joins(associations ...string) *DB

Joins joins the given associations.

func (*DB) Like added in v1.1.0

func (db *DB) Like(field string, value any) *query.Query

Like returns a new query with the like operator for the given field and value. If the field is prefixed with a *, it will be decrypted.

func (*DB) Lt added in v1.1.0

func (db *DB) Lt(field string, value any) *query.Query

Lt returns a new query with the less than operator for the given field and value.

func (*DB) Lte added in v1.1.0

func (db *DB) Lte(field string, value any) *query.Query

Lte returns a new query with the less than or equal operator for the given field and value.

func (*DB) Neq added in v1.1.0

func (db *DB) Neq(field string, value any) *query.Query

Neq returns a new query with the not equal operator for the given field and value. If the field is prefixed with a *, it will be decrypted.

func (*DB) NotIn added in v1.1.0

func (db *DB) NotIn(field string, value []any) *query.Query

NotIn returns a new query with the not in operator for the given field and value. If the field is prefixed with a *, it will be decrypted.

func (*DB) NotLike added in v1.1.0

func (db *DB) NotLike(field string, value any) *query.Query

NotLike returns a new query with the not like operator for the given field and value. If the field is prefixed with a *, it will be decrypted.

func (*DB) NotSimilar added in v1.1.0

func (db *DB) NotSimilar(field string, value any) *query.Query

NotSimilar returns a new query with the not similar operator for the given field and value. If the field is prefixed with a *, it will be decrypted.

func (*DB) Omit added in v1.1.0

func (db *DB) Omit(fields ...string) *DB

Omit omits the given fields.

func (*DB) Or added in v1.1.0

func (db *DB) Or(queries ...*query.Query) *query.Query

Or returns a new query with the or operator for the given queries.

func (*DB) Preload added in v1.1.0

func (db *DB) Preload(associations ...string) *DB

Preload preloads the given associations.

func (*DB) Query added in v1.1.0

func (d *DB) Query(query *query.Query) *DB

Query consumes the query and returns a new DB instance with the where clause applied.

func (*DB) Rollback added in v1.1.0

func (db *DB) Rollback() *DB

Rollback rolls back the transaction.

func (*DB) Similar added in v1.1.0

func (db *DB) Similar(field string, value any) *query.Query

Similar returns a new query with the similar operator for the given field and value. If the field is prefixed with a *, it will be decrypted.

func (*DB) Unscoped added in v1.1.0

func (db *DB) Unscoped() *DB

Unscoped returns a new DB instance with the unscoped mode.

type Page

type Page struct {
	Page  int `json:"page" form:"page"`
	Size  int `json:"size" form:"size"`
	Total int `json:"total" form:"-"`
}

Page is a pagination model.

func (*Page) Consume

func (p *Page) Consume(db *DB) *DB

Consume consumes the page and returns a new DB instance with the offset and limit applied.

type Sort

type Sort struct {
	Field string `json:"field" form:"field"`
	Asc   bool   `json:"asc" form:"asc"`
}

Sort is a sort model. add "*" to the field to sort by encrypted field

func (*Sort) Consume

func (s *Sort) Consume(db *DB) *DB

Consume consumes the sort and returns a new DB instance with the order applied.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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