Documentation ¶
Overview ¶
Package db A simple library to persist structs in json file and perform queries and CRUD operations
Index ¶
- Constants
- Variables
- type Driver
- func (d *Driver) AsEntity(output interface{}) (err error)
- func (d *Driver) Delete(entity elemental.Identifiable) (err error)
- func (d *Driver) Errors() []error
- func (d *Driver) First() *Driver
- func (d *Driver) Get() *Driver
- func (d *Driver) Insert(entity elemental.Identifiable) (err error)
- func (d *Driver) Open(entity elemental.Identifiable) *Driver
- func (d *Driver) Raw() interface{}
- func (d *Driver) RawArray() []interface{}
- func (d *Driver) Update(entity elemental.Identifiable) (err error)
- func (d *Driver) Upsert(entity elemental.Identifiable) (err error)
- func (d *Driver) Where(key, cond string, val interface{}) *Driver
- type QueryFunc
Constants ¶
const (
IdentifierKey = "ID"
)
Variables ¶
var ErrRecordNotFound = errors.New("record not found")
var ErrUpdateFailed = errors.New("update failed, no record(s) to update")
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 ¶
func (d *Driver) Delete(entity elemental.Identifiable) (err error)
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) Insert ¶
func (d *Driver) Insert(entity elemental.Identifiable) (err error)
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 ¶
func (d *Driver) Open(entity elemental.Identifiable) *Driver
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
func (*Driver) Update ¶
func (d *Driver) Update(entity elemental.Identifiable) (err error)
Update the json data based on the id field/value pair
customerToUpdate:=driver.Open(Customer{}).Where("custid","=","CUST1").First() customerToUpdate.Name="Sony Arouje" err:=driver.Update(customerToUpdate)
Should not change the ID field when updating the record.
func (*Driver) Upsert ¶
func (d *Driver) Upsert(entity elemental.Identifiable) (err error)
Upsert function will try updating the passed entity. If no records to update then do the Insert operation.
customer := Customer{ CustID: "CU4", Name: "Sony Arouje", Address: "address", Contact: Contact{ Phone: "45533355", Email: "someone@gmail.com", }, } driver.Upsert(customer)