Documentation ¶
Overview ¶
Package db A simple library to persist structs in json file and perform queries and CRUD operations
Index ¶
- Variables
- type Driver
- func (d *Driver) AsEntity(output interface{}) (err error)
- func (d *Driver) Delete(entity Entity) (err error)
- func (d *Driver) Errors() []error
- func (d *Driver) First() *Driver
- func (d *Driver) Get() *Driver
- func (d *Driver) GetNextNumericID() int
- func (d *Driver) GetNextStringID() string
- func (d *Driver) Insert(entity Entity) (err error)
- func (d *Driver) Open(entity Entity) *Driver
- func (d *Driver) Raw() interface{}
- func (d *Driver) RawArray() []interface{}
- func (d *Driver) Update(entity Entity) (err error)
- func (d *Driver) Where(key, cond string, val interface{}) *Driver
- type Entity
- type QueryFunc
Constants ¶
This section is empty.
Variables ¶
var ErrTooManyRecords = fmt.Errorf("Too many records returned when 1 was expected")
var ErrZeroRecords = fmt.Errorf("No records found")
Functions ¶
This section is empty.
Types ¶
type Driver ¶
type Driver struct {
// contains filtered or unexported fields
}
Driver contains all the state of the db.
func New ¶
New creates a new database driver. Accepts the directory name to store the db files. If the passed directory not exist then will create one. driver, err:=db.New("customer")
func (*Driver) AsEntity ¶
AsEntity will converts the map to the passed structure pointer. should call this function after calling Get() or First(). This function will convert the result of Get or First operation to the passed structure type 'output' variable should be a pointer to a structure or stucture array. Function returns error in case of any errors in conversion.
First()
var custOut Customer err:=driver.Open(Customer{}).First().AsEntity(&custOut) fmt.Printf("%#v", custOut) this function will fill the custOut with the values from the map
Get()
var customers []Customer err:=driver.Open(Customer{}).Get().AsEntity(&customers)
func (*Driver) Delete ¶
Delete the record from the json db based on the id field/value pair
custToDelete:=Customer { CustID:"CUST1", } err:=driver.Delete(custToDelete)
func (*Driver) First ¶
First return the first record matching the condtion.
driver.Open(Customer{}).Where("custid","=","CUST1").First()
func (*Driver) Get ¶
Get the result from the json db as an array. If no where condition then return all the data from json db
Get based on a where condition
driver.Open(Customer{}).Where("name","=","sarouje").Get()
Get all records
driver.Open(Customer{}).Get()
func (*Driver) GetNextNumericID ¶ added in v1.0.2
func (*Driver) GetNextStringID ¶ added in v1.0.2
func (*Driver) Insert ¶
Insert the entity to the json db. Insert will identify the type of the entity and insert the entity to the specific json file based on the type of the entity. If the db file not exist then will create a new db file
customer:=Customer { CustID:"CUST1", Name:"sarouje", Address: "address", Contact: Contact { Phone:"45533355", Email:"someone@gmail.com", }, } err:=driver.Insert(customer)
func (*Driver) Open ¶
Open will open the json db based on the entity passed. Once the file is open you can apply where conditions or get operation. driver.Open(Customer{}) Open returns a pointer to Driver, so you can chain methods like Where(), Get(), etc
func (*Driver) RawArray ¶
func (d *Driver) RawArray() []interface{}
RawArray will return the data in map array type
type Entity ¶
type Entity interface {
ID() (jsonField string, value interface{})
}
* Entity any structure wanted to persist to json db should implement this interface function ID(). * ID and Field will be used while doing update and delete operation. * ID() return the id value and field name that stores the id * * Sample Struct * type Customer struct { * CustID string `json:"custid"` * Name string `json:"name"` * Address string `json:"address"` * Contact Contact * } * type Contact struct { * Phone string `json:"phone"` * Email string `json:"email"` * } * func (c Customer) ID() (jsonField string, value interface{}) { * value=c.CustID * jsonField="custid" * return * }