grimoire: github.com/Fs02/grimoire Index | Examples | Files | Directories

package grimoire

import "github.com/Fs02/grimoire"

Package grimoire is a data access layer and validation for go.

Grimoire is a database access layer inspired by Ecto's changeset system. It features flexible query API and builtin validation. It currently supports MySQL, PostgreSQL and SQLite3 but a custom adapter can be implemented easily using the Adapter interface.

Code:

package main

import (
    "time"

    "github.com/Fs02/grimoire"
    "github.com/Fs02/grimoire/adapter/mysql"
    "github.com/Fs02/grimoire/changeset"
    "github.com/Fs02/grimoire/params"
)

type Product struct {
    ID        int
    Name      string
    Price     int
    CreatedAt time.Time
    UpdatedAt time.Time
}

// ChangeProduct prepares data before database operation.
// Such as casting value to appropriate types and perform validations.
func ChangeProduct(product interface{}, params params.Params) *changeset.Changeset {
    ch := changeset.Cast(product, params, []string{"name", "price"})
    changeset.ValidateRequired(ch, []string{"name", "price"})
    changeset.ValidateMin(ch, "price", 100)
    return ch
}

func main() {
    // initialize mysql adapter.
    adapter, err := mysql.Open("root@(127.0.0.1:3306)/db?charset=utf8&parseTime=True&loc=Local")
    if err != nil {
        panic(err)
    }
    defer adapter.Close()

    // initialize grimoire's repo.
    repo := grimoire.New(adapter)

    var product Product

    // Inserting Products.
    // Changeset is used when creating or updating your data.
    ch := ChangeProduct(product, params.Map{
        "name":  "shampoo",
        "price": 1000,
    })

    if ch.Error() != nil {
        // handle error
    }

    // Changeset can also be created directly from json string.
    jsonch := ChangeProduct(product, params.ParseJSON(`{
		"name":  "soap",
		"price": 2000,
	}`))

    // Create products with changeset and return the result to &product,
    if err = repo.From("products").Insert(&product, ch); err != nil {
        // handle error
    }

    // or panic when insertion pailed
    repo.From("products").MustInsert(&product, jsonch)

    // Querying Products.
    // Find a product with id 1.
    repo.From("products").Find(1).MustOne(&product)

    // Updating Products.
    // Update products with id=1.
    repo.From("products").Find(1).MustUpdate(&product, ch)

    // Deleting Products.
    // Delete Product with id=1.
    repo.From("products").Find(1).MustDelete()
}

Index

Examples

Package Files

adapter.go grimoire.go logger.go query.go repo.go

func DefaultLogger Uses

func DefaultLogger(query string, duration time.Duration, err error)

DefaultLogger log query suing standard log library.

func Log Uses

func Log(logger []Logger, statement string, duration time.Duration, err error)

Log using multiple logger. This function intended to be used within adapter.

type Adapter Uses

type Adapter interface {
    All(Query, interface{}, ...Logger) (int, error)
    Aggregate(Query, interface{}, ...Logger) error
    Insert(Query, map[string]interface{}, ...Logger) (interface{}, error)
    InsertAll(Query, []string, []map[string]interface{}, ...Logger) ([]interface{}, error)
    Update(Query, map[string]interface{}, ...Logger) error
    Delete(Query, ...Logger) error

    Begin() (Adapter, error)
    Commit() error
    Rollback() error
}

Adapter interface

type Logger Uses

type Logger func(string, time.Duration, error)

Logger defines function signature for custom logger.

type Query Uses

type Query struct {
    Collection      string
    Fields          []string
    AggregateField  string
    AggregateMode   string
    AsDistinct      bool
    JoinClause      []c.Join
    Condition       c.Condition
    GroupFields     []string
    HavingCondition c.Condition
    OrderClause     []c.Order
    OffsetResult    int
    LimitResult     int
    LockClause      string
    Changes         map[string]interface{}
    // contains filtered or unexported fields
}

Query defines information about query generated by query builder.

func (Query) Aggregate Uses

func (query Query) Aggregate(mode string, field string, out interface{}) error

Aggregate calculate aggregate over the given field.

func (Query) All Uses

func (query Query) All(record interface{}) error

All retrieves all results that match the query.

func (Query) Count Uses

func (query Query) Count() (int, error)

Count retrieves count of results that match the query.

func (Query) Delete Uses

func (query Query) Delete() error

Delete deletes all results that match the query.

func (Query) Distinct Uses

func (query Query) Distinct() Query

Distinct add distinct option to select query.

func (Query) Find Uses

func (query Query) Find(id interface{}) Query

Find adds where id=? into query. This is short cut for Where(Eq(I("id"), 1))

func (Query) FindBy Uses

func (query Query) FindBy(col string, val interface{}) Query

FindBy adds where col=? into query.

func (Query) Group Uses

func (query Query) Group(fields ...string) Query

Group query using fields.

func (Query) Having Uses

func (query Query) Having(condition ...c.Condition) Query

Having adds condition for group query.

func (Query) Insert Uses

func (query Query) Insert(record interface{}, chs ...*changeset.Changeset) error

Insert records to database.

func (Query) Join Uses

func (query Query) Join(collection string, condition ...c.Condition) Query

Join current collection with other collection.

func (Query) JoinWith Uses

func (query Query) JoinWith(mode string, collection string, condition ...c.Condition) Query

JoinWith current collection with other collection with custom join mode.

func (Query) Limit Uses

func (query Query) Limit(limit int) Query

Limit result returned by database.

func (Query) Lock Uses

func (query Query) Lock(lock ...string) Query

Lock query using pessimistic locking. Lock expression can be specified as first parameter, default to FOR UPDATE.

func (Query) MustAggregate Uses

func (query Query) MustAggregate(mode string, field string, out interface{})

MustAggregate calculate aggregate over the given field. It'll panic if any error eccured.

func (Query) MustAll Uses

func (query Query) MustAll(record interface{})

MustAll retrieves all results that match the query. It'll panic if any error eccured.

func (Query) MustCount Uses

func (query Query) MustCount() int

MustCount retrieves count of results that match the query. It'll panic if any error eccured.

func (Query) MustDelete Uses

func (query Query) MustDelete()

MustDelete deletes all results that match the query. It'll panic if any error eccured.

func (Query) MustInsert Uses

func (query Query) MustInsert(record interface{}, chs ...*changeset.Changeset)

MustInsert records to database. It'll panic if any error occurred.

func (Query) MustOne Uses

func (query Query) MustOne(record interface{})

MustOne retrieves one result that match the query. If no result found, it'll panic.

func (Query) MustPreload Uses

func (query Query) MustPreload(record interface{}, field string)

MustPreload loads association with given query. It'll panic if any error occurred.

func (Query) MustSave Uses

func (query Query) MustSave(record interface{})

MustSave puts a record to database. It'll panic if any error eccured.

func (Query) MustUpdate Uses

func (query Query) MustUpdate(record interface{}, chs ...*changeset.Changeset)

MustUpdate records in database. It'll panic if any error occurred.

func (Query) Offset Uses

func (query Query) Offset(offset int) Query

Offset the result returned by database.

func (Query) One Uses

func (query Query) One(record interface{}) error

One retrieves one result that match the query. If no result found, it'll return not found error.

func (Query) OrHaving Uses

func (query Query) OrHaving(condition ...c.Condition) Query

OrHaving behaves exactly the same as having except it combines with any previous expression by using an OR.

func (Query) OrWhere Uses

func (query Query) OrWhere(condition ...c.Condition) Query

OrWhere behaves exactly the same as where except it combines with any previous expression by using an OR.

func (Query) Order Uses

func (query Query) Order(order ...c.Order) Query

Order the result returned by database.

func (Query) Preload Uses

func (query Query) Preload(record interface{}, field string) error

Preload loads association with given query.

func (Query) Save Uses

func (query Query) Save(record interface{}) error

Save a record to database. If condition exist, it will try to update the record, otherwise it'll insert it. Save ignores id from record.

func (Query) Select Uses

func (query Query) Select(fields ...string) Query

Select filter fields to be selected from database.

func (Query) Set Uses

func (query Query) Set(field string, value interface{}) Query

Set value for insert or update operation that will replace changeset value.

func (Query) Update Uses

func (query Query) Update(record interface{}, chs ...*changeset.Changeset) error

Update records in database. It'll panic if any error occurred.

func (Query) Where Uses

func (query Query) Where(condition ...c.Condition) Query

Where expressions are used to filter the result set. If there is more than one where expression, they are combined with an and operator.

type Repo Uses

type Repo struct {
    // contains filtered or unexported fields
}

Repo defines grimoire repository.

func New Uses

func New(adapter Adapter) Repo

New create new repo using adapter.

func (*Repo) Adapter Uses

func (repo *Repo) Adapter() Adapter

Adapter returns adapter of repo.

func (Repo) From Uses

func (repo Repo) From(collection string) Query

From initiates a query for a collection.

func (*Repo) SetLogger Uses

func (repo *Repo) SetLogger(logger ...Logger)

SetLogger replace default logger with custom logger.

func (Repo) Transaction Uses

func (repo Repo) Transaction(fn func(Repo) error) error

Transaction performs transaction with given function argument.

Directories

PathSynopsis
adapter/mysqlPackage mysql wraps mysql driver as an adapter for grimoire.
adapter/postgresPackage postgres wraps postgres (pq) driver as an adapter for grimoire.
adapter/specsPackage specs defines test specifications for grimoire's adapter.
adapter/sqlPackage sql is general sql adapter that wraps database/sql.
adapter/sqlite3Package sqlite3 wraps go-sqlite3 driver as an adapter for grimoire.
cPackage c defines function for building condition in query.
changesetPackage changeset used to cast and validate data before saving it to the database.
errorsPackage errors wraps driver and changeset error as a grimoire's error.
internal
paramsPackage params defines different types of params used for changeset input.
schema

Package grimoire imports 9 packages (graph) and is imported by 6 packages. Updated 2019-11-12. Refresh now. Tools for package owners.