data

package module
v0.10.2 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2021 License: Apache-2.0 Imports: 3 Imported by: 10

README

Data 📚

GoDoc Go Report Card Build Status Codecov Version

Swappable Database Adapters for Go

This library helps to make simple database calls as easily as possible, by providing a simple, common interface that we can implement for every database we need. 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.

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 := ds.Session()
defer session.Close()

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

// INSERT/UPDATE a person object in the database
err := session.Save("Person", person)

// DELETE a person from the database
err := session.Delete("Person", person)


// LIST many records from the database, by using an iterator that will loop through all records that match the provided criteria.
it, err := session.List("Person", criteria, options...)

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

This adapter implements the data interface for an in-memory datastore. It is the world's worst database, and should only be used for creating unit tests. If you use this "database" in production (hell, or even as a proof-of-concept demo) then you deserve the merciless mockery that fate holds for you.

data.mongodb

This adapter implements the data interface for MongoDB. It uses the standard MongoDB driver. It is currently a work-in-progress, and not ready for use by anyone, for any reason. It is very likely to blow up your server, launch the nukes, and unleash the plague on you, your customers, and your family.

Retrieving Record Sets

This library also includes an "iterator" interface, for retrieving large sets of data from the datasource efficiently.


// Create an object for the iterator to populate
person := Person{}

// Create the iterator.  Requires a collection name, criteria expression (below), and options (also below, such as sorting and pagination)
it := session.List(CollectionName, CriteriaExpression, Options)

for it.Next(&person) {

    // person.Name...
    // person.Email...
}
Expression Builder

Every database has its own query language, so this library provides in intermediate format that should be easy to convert into whatever specific language you need to use. Check out the README.md in the expression folder for more details and examples.

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

// or chain logical expressions together
c := expression.New("name", "=", "John Connor").Or("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! 📚

Documentation

Overview

Package data provides a data structure for defining simple database filters. This is not able to represent every imaginable query criteria, but it does a good job of making common criteria simple to format and pass around in your application.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Collection added in v0.3.8

type Collection interface {
	List(filter expression.Expression, options ...option.Option) (Iterator, error)
	Load(filter expression.Expression, target Object) error
	Save(object Object, note string) error
	Delete(object Object, note string) error
}

Collection represents a single database collection (or table) that is opened to support a single transactional request, and then closed when this transaction is complete

type Iterator added in v0.2.5

type Iterator interface {
	Next(Object) bool
	Count() int
	Close() error
}

Iterator interface allows callers to iterator over a large number of items in an array/slice

type Object

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 makes a note
	SetCreated(comment string)

	// SetUpdated stamps the UpdateDate of the object, and makes a note
	SetUpdated(comment string)

	// SetDeleted marks the object virtually "deleted", and makes a note
	SetDeleted(comment string)
}

Object interface defines all of the methods that a Domain Object must provide to Presto

type Server added in v0.3.6

type Server interface {
	Session(context.Context) (Session, error)
}

Server is an abstract representation of a database and its connection information.

type Session

type Session interface {
	Collection(collection string) Collection
	Close()
}

Session represents a single database session, that is opened to support a single transactional request, and then closed when this transaction is complete

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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