Categorygithub.com/custompbx/customorm
modulepackage
0.0.0-20240604205339-ef13a8616d01
Repository: https://github.com/custompbx/customorm.git
Documentation: pkg.go.dev

# README

Here's an improved version of your README:

CustomORM

CustomORM is not a full-fledged ORM but rather a collection of functions designed to generate and execute SQL queries based on specific struct definitions. It is tailored for PostgreSQL databases.

Features

  • Generate and execute SQL queries based on struct definitions
  • Designed to work seamlessly with PostgreSQL databases
  • Support for table creation, insertion, updating, deletion, and querying

Usage

Struct Definitions

Define your structs with tags specifying SQL constraints and properties:

type Domain struct {
	Id      int64  `json:"id" customsql:"pkey:id;check(id <> 0)"`
	Enabled bool   `json:"enabled" customsql:"enabled;default=TRUE"`
	Name    string `json:"name" customsql:"name;unique;check(name <> '')"`
}

func (d *Domain) GetTableName() string {
    return "domains"
}

type DomainUser struct {
	Id      int64    `json:"id" customsql:"pkey:id;check(id <> 0)"`
	Position int64    `json:"position" customsql:"position;position"`
	Enabled bool     `json:"enabled" customsql:"enabled;default=TRUE"`
	Name    string   `json:"name" customsql:"name;unique_1;check(name <> '')"`
	Parent  *Domain  `json:"parent" customsql:"fkey:parent_id;unique_1;check(parent_id <> 0)"`
}

func (d *DomainUser) GetTableName() string {
    return "domain_users"
}

The tags unique and index can be grouped using an underscore, such as unique_1. To set the order within a group, add a suffix after a hyphen, for example, index_1-1 and index_1-2.

Creating Tables

corm := customorm.Init(db)
corm.CreateTable(&Domain{})     // Returns bool
corm.CreateTable(&DomainUser{}) // Returns bool

Inserting Rows

corm := customorm.Init(db)
corm.InsertRow(&DomainUser{
    Name:    "UserName",
    Parent:  &altStruct.Domain{Id: parentId},
    Enabled: true,
}) // Returns int64, error

Updating Rows

corm := customorm.Init(db)
corm.UpdateRow(&DomainUser{
    Id:   1,
    Name: "NewUserName",
}, true, map[string]bool{"Name": true}) // Returns error

Deleting Rows

corm := customorm.Init(db)
corm.DeleteRowById(&DomainUser{Id: 1})           // Returns error
corm.DeleteRowByArgId(&DomainUser{}, 1)          // Returns error
corm.DeleteRows(&DomainUser{Enabled: false}, map[string]bool{"Enabled": true}) // Returns error

Querying Rows

corm := customorm.Init(db)
corm.GetDataAll(&DomainUser{}, true) // Returns interface{}, error

filter := customOrm.Filters{
    Fields: map[string]FilterFields{
        "Name": {
            Flag:      true, // Use this filter
            UseValue:  false, // Use value from struct or from value field below
            Value:     nil, // Can use this value instead of value from struct
            Operand:   "", // Operand to compare with value of (<,>,=,CONTAINS,IN) default "="
        },
    },
    Order: customOrm.Order{
        Desc:   true, // Sort DESC
        Fields: []string{"Id"}, // Sort by field names
    },
    Limit:  10,
    Offset: 0,
    Count:  false, // If true, returns count(*) instead of fields
}

corm.GetDataByValue(&DomainUser{Id: 1}, filter, false) // Returns interface{}, error

Feel free to adjust and expand upon these examples to suit your specific use cases.

# Functions

GetTableName retrieves the table name from the provided instance.
No description provided by the author
Init initializes the CORM instance with a database connection.
ToSnakeCase converts a string to snake case.
ValuesEqualPlaceholders generates equal placeholders for SQL values.
ValuesEqualPlaceholdersAnd generates equal placeholders separated by "AND" for SQL values.
ValuesPlaceholders generates placeholders for SQL values.

# Constants

Constants defining various tags and operands.
Constants defining various tags and operands.
Constants defining various tags and operands.
Constants defining various tags and operands.
Constants defining various tags and operands.
Constants defining various tags and operands.

# Structs

Column struct representing a column in a database table.
CompositeFields struct for field names could be composed wth others.
CORM is the main struct for Custom ORM.
FilterFields struct defining individual filtering criteria.
Filters struct to hold filtering criteria for querying.
FKey struct representing a foreign key constraint.
Order struct defining ordering criteria.
Table struct representing a database table.

# Interfaces

Row interface to get table name.