meth

package module
v0.0.0-...-227ffba Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2016 License: MIT Imports: 4 Imported by: 0

README

METH

M​aladroit E​lusive T​ransaction H​ub

A wrapper around upper.io/db to ease some repetitive tasks.

Reasoning Behind

Working with upper.io/db sometimes becomes a little bit repetitive, so METH is an attempt to minimize the scaffolding needed in some basic tasks [fetching by id, by certain conditions].

Badges

GoDoc Build Status Go Report Card

Usage

Install
go get github.com/worg/meth

Any type must implement Persistent interface and return an initalized and valid db.Collection in order to use METH functions.

Example
package person

import (
    "upper.io/db"
    "upper.io/db/some_driver"
    
)

type Person struct {
    ID      int    `db:"id"`
    Name    string `db:"name"`
    Address string `db:"address"`
}

var (
    collection db.Collection
)

func init() {
    //init DB and set collection
}

func (p *Person) Collection() db.Collection {
    return collection
}

func (p *Person) ById(id int) {
    if err := meth.One(&p); err := nil {
        // handle err
    }
    // by this point p should've been filled with db data
}

Documentation

Overview

Package meth is a thin wrapper around upper.io/db to ease some repettitive tasks

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoID    = errors.New(`no id field found`)
	ErrNoSlice = errors.New(`slice expected`)
)

Shared error codes

Functions

func All

func All(p Persistent, rows interface{}, cond ...interface{}) error

All works as an alias for AllBy

func AllBy

func AllBy(p Persistent, rows interface{}, cond ...interface{}) error

AllBy fills rows with data from the result set matching the given conditions

func AllOp

func AllOp(p Persistent, operation resFunc, rows interface{}, cond ...interface{}) error

AllOp works like All but applies an operation in form of:

func(db.Result)

to work with the result set [for things like select, group, limit]

func Exists

func Exists(p Persistent, cond ...interface{}) bool

Exists returns true if a record exists matching either id or the given conditions

func Group

func Group(i ...interface{}) resFunc

Group is used to group results that have the same value in the same column or columns.

func Limit

func Limit(u uint) resFunc

Limit defines the maximum number of results returns a resFunc

func One

func One(p Persistent) error

One fills p with one element based on the id field

func OneBy

func OneBy(p Persistent, cond ...interface{}) error

OneBy fills p with one element matching the given conditions

func OneOp

func OneOp(p Persistent, operation resFunc, row interface{}, cond ...interface{}) error

OneOp works like AllOp but returns only one row

func Paginate

func Paginate(limit, skip uint) resFunc

Paginate applies a limit and skip to a result set

func Select

func Select(i ...interface{}) resFunc

Select defines specific fields to be fulfilled on results in this result set.

func Skip

func Skip(n uint) resFunc

Skip ignores the first *n* results returns a resFunc

func Sort

func Sort(i ...interface{}) resFunc

Sort receives field names that define the order in which elements will be returned in a query, field names may be prefixed with a minus sign (-) indicating descending order; ascending order would be used by default.

func Where

func Where(i ...interface{}) resFunc

Where discards the initial filtering conditions and sets new ones.

Types

type Persistent

type Persistent interface {
	Collection() db.Collection
}

Persistent is the interface that allows us to communicate with upper.io/db collection

Jump to

Keyboard shortcuts

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