borm

package module
v0.0.0-...-7284aed Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2015 License: MIT Imports: 13 Imported by: 0

README

borm

boltdb tiny orm for processing model structures.

######Model structure:

type Person struct {
  // required
  borm.Model
  
  // Model fields
  Name    string
  Age     int

  // optional. used to add Created field into model. Will be set automaticaly on creation
  borm.CreateTime
  
  // optional. used to add Updated field into model. Will be updated automaticaly on each model save
  borm.UpdateTime
}

######Usage example

package main

import (
	"fmt"
	"log"

	"github.com/vtg/borm"
)

type Person struct {
	borm.Model

	Name string
	Age  int

	borm.CreateTime
	borm.UpdateTime
}

func main() {
	db, err := borm.Open("database.db")
	if err != nil {
		log.Fatal(err)
	}
	db.Log = true
	defer db.Close()

	// the bucket that will store Person
	bucket := []string{"people"}

	// creating Person
	p := Person{Name: "John Doe"}
	db.Save(bucket, &p)
	fmt.Println(p.ID, p.Name, p.Age)

	//updating Person
	p.Age = 10
	db.Save(bucket, &p)
	fmt.Println(p.ID, p.Name, p.Age)

	// get Person from database
	p1 := Person{}
	db.Find(bucket, p.ID, &p1)
	fmt.Println(p1.ID, p1.Name, p1.Age)

	// list people
	people := []Person{}
	db.List(bucket, &people)
	fmt.Println(people)

	// delete Person from database
	db.Delete(bucket, &p)

	// count peoples
	fmt.Println(db.Count(bucket))
}

######Events borm has events subscription support.
There are 3 types of Events "Created", "Updated" and "Deleted".
Event names prefixed with model type name.

//Subscribing to Person creation event
borm.Events.Sub("PersonCreated", func(e *pubsub.Event) {
	fmt.Println(e.Name, e.Objects[0].(*Person))
})

GoDoc https://godoc.org/github.com/vtg/borm

#####Author

VTG - http://github.com/vtg

License

Released under the MIT License.

GoDoc

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Events = *pubsub.Hub

Events listener

Functions

This section is empty.

Types

type DB

type DB struct {
	File string
	Log  bool
	// contains filtered or unexported fields
}

DB

func Open

func Open(dbfile string) (db DB, err error)

Open opens database

func (*DB) Close

func (db *DB) Close()

Close closing database

func (*DB) Count

func (db *DB) Count(path []string) int

Count returns number of records in bucket

func (*DB) Delete

func (db *DB) Delete(path []string, m mod) error

Delete deletes model from database

m := Model{}
db.Find([]string{"bucket"}, &m)
db.Delete([]string{"bucket"}, &m)

func (*DB) DeleteBuckets

func (db *DB) DeleteBuckets(path []string, keys []string) error

DeleteBuckets deletes records from database by keys

db.DeleteBuckets([]string{"bucket"}, []string{"1","2","3"})

func (*DB) DeleteKeys

func (db *DB) DeleteKeys(path []string, keys []string) error

DeleteKeys deletes records from database by keys

db.DeleteKeys([]string{"bucket"}, []string{"1","2","3"})

func (*DB) Find

func (db *DB) Find(path []string, id string, i interface{}) error

Find returns model from database

m := Model{}
db.Find([]string{"bucket"}, &m)

func (*DB) Get

func (db *DB) Get(path []string, key string) ([]byte, error)

GET returns value by key

val, err := db.FindValue([]string{"bucket"}, "1")

func (*DB) List

func (db *DB) List(path []string, dest interface{}, params ...Params) error

List fills models slice with records from database

m := []Model{}
db.List([]string{"bucket"}, &m)

load with params

db.List([]string{"bucket"}, &m, Params{Offset: 10, Limit: 30})

func (*DB) ListItems

func (db *DB) ListItems(path []string, params ...Params) (map[string][]byte, error)

ListItems returns raw records from database

func (*DB) ListKeys

func (db *DB) ListKeys(path []string, keys [][]byte, dest interface{}) error

ListKeys fills models slice with records by keys provided

m := []Model{}
db.ListKeys([]string{"bucket"}, [][]byte{[]byte("1"),[]byte("2")}, &m)

func (*DB) Save

func (db *DB) Save(path []string, m mod) error

Save saves model into database

m := Model{Name: "Model Name"}
db.Save([]string{"bucket"}, &m)

func (*DB) SaveValue

func (db *DB) SaveValue(path []string, id string, val []byte) error

SaveValue saves key/value pair into database

func (*DB) Values

func (db *DB) Values(path []string, params ...Params) ([][]byte, error)

Values returns values from bucket

type Errors

type Errors map[string][]string

errors errors type

type MID

type MID struct {
	ID string
}

MID

func (*MID) Created

func (i *MID) Created() time.Time

Created returns creation time

func (*MID) GetID

func (i *MID) GetID() string

GetID returns id of model

type Model

type Model struct {
	MID
	// contains filtered or unexported fields
}

Model

func (*Model) AddError

func (m *Model) AddError(f string, t string)

AddError adding error to record

func (*Model) GetErrors

func (m *Model) GetErrors() Errors

GetErrors returns record errors

func (*Model) ResetErrors

func (m *Model) ResetErrors()

ResetErrors clean all model errors

func (*Model) SetErrors

func (m *Model) SetErrors(e Errors)

SetErrors set record errors

func (*Model) Valid

func (m *Model) Valid() bool

Valid returns true if no errors found in model

func (*Model) ValidateFloat32

func (m *Model) ValidateFloat32(f string, v, min, max float32)

ValidateFloat32 validates float32 min, max. -1 for any

m.ValidateFloat32("number", 10.2, -1, 11)

func (*Model) ValidateFloat64

func (m *Model) ValidateFloat64(f string, v, min, max float64)

ValidateFloat64 validates float64 min, max. -1 for any

m.ValidateFloat64("number", 10.2, -1, 11)

func (*Model) ValidateFormat

func (m *Model) ValidateFormat(f, v, reg string)

ValidateFormat validates string format with regex string

m.ValidateFormat("ip address", u.IP, `\A(\d{1,3}\.){3}\d{1,3}\z`)

func (*Model) ValidateInt

func (m *Model) ValidateInt(f string, v, min, max int)

ValidateInt validates int min, max. -1 for any

m.ValidateInt("number", 10, -1, 11)  // max 18

func (*Model) ValidateInt64

func (m *Model) ValidateInt64(f string, v, min, max int64)

ValidateInt64 validates int64 min, max. -1 for any

m.ValidateInt64("number", 10, 6, -1) // min 6

func (*Model) ValidateLength

func (m *Model) ValidateLength(f, v string, min, max int)

ValidateLength validates string min, max length. -1 for any

m.ValidateLength("password", m.Password, 6, 18) // min 6, max 18

func (*Model) ValidatePresence

func (m *Model) ValidatePresence(f, v string)

ValidatePresence validates string for presence

m.ValidatePresence("Name", m.Name)

type Params

type Params struct {
	Offset  int
	Limit   int
	Reverse bool
}

Params for List query

type UpdateTime

type UpdateTime struct {
	Updated time.Time
}

UpdateTime struct for managing update time

Jump to

Keyboard shortcuts

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