Godex

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2025 License: GPL-3.0 Imports: 3 Imported by: 0

README

Godex

Godex provides a simple utility for creating and storing reusable queries

logo
Example
Creating a Codex
q := Codex{
    Table: "posts",
    DefaultQueries: DefaultQueries{
        SelectById: "SELECT * FROM posts WHERE id = :id",
        SelectOne:  "SELECT * FROM posts WHERE id = :id and post_title = :post_title",
        Select:     "SELECT * FROM posts WHERE created_at > '2020-05-10 12:23:43'",
        Insert:     "INSERT INTO posts(post_id) VALUES (:post_id)",
        Update:     "UPDATE posts SET is_active=:is_active WHERE id=:id",
        Delete:     "DELETE FROM posts WHERE id=:id",
        SoftDelete: "UPDATE posts SET deleted_at=CURRENT_TIMESTAMP() WHERE id=:id",
    },
    Queries: map[string]string{
        "SelectUsersByFirstName": "SELECT * FROM users WHERE first_name = :first_name",
    },
}
Default Queries
res, err := q.SelectOne(CxArgs{"id": 154, "post_title": "Hello World"})
if err != nil {
	panic(err)
}
fmt.Println(res.postId)
Custom Queries
_, err := q.RawQuery(q.Queries["SelectUsersByFirstName"], CxArgs{"first_name": "John"})
if err != nil {
    return
}
Roadmap
  1. Integrate Migrations into Godex (a feature which exists within Sphire's Codex utility)
  2. Unit tests
  3. Github Actions

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Pretty

func Pretty(str string) string

Types

type Codex

type Codex struct {
	Table          string            `json:"table,omitempty"`
	DefaultQueries DefaultQueries    `json:"default_queries,omitempty"`
	Queries        map[string]string `json:"queries"`
	DB             mantisDb.MySQL    `json:"db,omitempty"`
	// contains filtered or unexported fields
}

Codex provides an easy query store

func CreateGodex

func CreateGodex(
	db mantisDb.MySQL,
	table string,
	SelectById string,
	SelectOne string,
	Select string,
	Insert string,
	Update string,
	Delete string,
	SoftDelete string,
) Codex

CreateGodex creates a new instance of Codex

func (*Codex) Delete

func (c *Codex) Delete() error

Delete a specific record

func (*Codex) Insert

func (c *Codex) Insert() (int64, error)

Insert a new record

func (*Codex) RawQuery

func (c *Codex) RawQuery(query string, args ...any) (any, error)

RawQuery performs a query with given args

func (*Codex) Select

func (c *Codex) Select(args ...any) ([]any, error)

Select one

func (*Codex) SelectById

func (c *Codex) SelectById(args ...any) (any, error)

SelectById one item

func (*Codex) SelectOne

func (c *Codex) SelectOne(args ...any) (any, error)

SelectOne item

func (*Codex) SoftDelete

func (c *Codex) SoftDelete() (int64, error)

SoftDelete a specific record

func (*Codex) String

func (c *Codex) String() string

String returns a string representation of Codex

func (*Codex) Update

func (c *Codex) Update() (int64, error)

Update a specific record

type CxArgs

type CxArgs map[string]any

CxArgs provides an alias for map[string]any for arguments passed to the named queries

type DefaultQueries

type DefaultQueries struct {
	SelectById string `json:"selectById,omitempty"`
	SelectOne  string `json:"selectOne,omitempty"`
	Select     string `json:"select,omitempty"`
	Insert     string `json:"insert,omitempty"`
	Update     string `json:"update,omitempty"`
	Delete     string `json:"delete,omitempty"`
	SoftDelete string `json:"softDelete,omitempty"`
}

DefaultQueries stores specific queries to be used by Codex

Jump to

Keyboard shortcuts

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