benpate / data

Swappable database adapters for Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Data ๐Ÿ“š

GoDoc Version Build Status Go Report Card Codecov

Swappable Database Adapters for Go

This library provides a common interface for making simple database calls. The goal of this package is to provide simple CRUD operations only, so each database will support many advanced features that are not available through this library. Other modules (such as data-mongo and data-mock) implement specific database adapters.

The "Object" interface

The data library works with any object that implements the Object interface. To implement this quickly in existing data models, you can just attach the journal.Journal object to your domain objects, and most of your work is already done.

// Object wraps all of the methods that a Domain Object must provide to Presto
type Object interface {

    // ID returns the primary key of the object
    ID() string

    // IsNew returns TRUE if the object has not yet been saved to the database
    IsNew() bool

    // SetCreated stamps the CreateDate and UpdateDate of the object, and adds a note to the Journal.
    SetCreated(comment string)

    // SetUpdated stamps the UpdateDate of the object, and adds a note to the Journal.
    SetUpdated(comment string)

    // SetDeleted marks the object virtually "deleted", and adds a note to the Journal.
    SetDeleted(comment string)
}

Datasource Interface

// Configure your database
ds := mongodb.New(uri, dbname)

// Create a new session, one per server request
session := db.Session()
defer session.Close()

// Get a reference to the table/collection you're working with
collection := session.Collection("Person")

// LOAD from a person object from the database
err := collection.Load(criteria, &person)

// INSERT/UPDATE a person object in the database - writing a "note" to the journal.
err := collection.Save(person, note)

// DELETE a person from the database. This is a "soft" delete that marks values as deleted but leaves them in the database.
err := collection.Delete(person, note)

// HARD DELETE a person, actually removing the record from the database.
err := collection.HardDelete(criteria)

// QUERY many records from the database, by populating a slice of results
err := collection.Query(criteria, options...)

// ITERATE over many records, using a more memory-friendly iterator to loop through a very large dataset.
it, err := collection.Query(criteria, options...)

for it.Next(&person) {
    // do stuff with person.
}

// COUNT the number of records that match a particular criteria
count, err := collection.Count(criteria)

Expression Builder

Every database has its own query language, so this library uses the exp module to represent query expressions in an efficient intermediate format that should be easy to convert into whatever specific language you need to use.

// build single predicate expressions
c := exp.Equal("name", "John Connor")

// or chain logical expressions together
c := exp.Equal("name", "John Connor").OrEqual("name", "Sarah Connor")

Query Options

There's a package for managing optional query arguments, such as sorting and pagination. These options just encapsulate data. It is the responsibilty of each database adapter to implement each of these in its own query engine.

// get a new iterator.  Sort results by first name.  Return only the first 100 rows.
it := session.List(collection, criteria, options.SortAsc("name"), options.MaxRows(100))

SortAsc(fieldname) tells the database to sort by a particular field, in ascending order

SortDesc(fieldname) tells the database to sort by a particular field, in descending order

FirstRow(count) tells the database to start returning records at the provided row number

MaxRows(count) tells the database to limit the number of records to the designated number of rows.

Pull Requests Welcome

This library is a work in progress, and will benefit from your experience reports, use cases, and contributions. If you have an idea for making this library better, send in a pull request. We're all in this together! ๐Ÿ“š

About

Swappable database adapters for Go

License:Apache License 2.0


Languages

Language:Go 100.0%