data

package module
v0.28.4 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2024 License: Apache-2.0 Imports: 3 Imported by: 10

README

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! 📚

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 {
	Count(criteria exp.Expression, options ...option.Option) (int64, error)
	Query(target any, criteria exp.Expression, options ...option.Option) error
	Iterator(criteria exp.Expression, options ...option.Option) (Iterator, error)
	Load(criteria exp.Expression, target Object) error
	Save(object Object, note string) error
	Delete(object Object, note string) error
	HardDelete(criteria exp.Expression) 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(any) bool
	Error() error
	Count() int
	Close() error
}

Iterator interface allows callers to iterator over a large number of items in a data structure

type Object

type Object interface {

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

	// Unix epoch time when this object was created
	Created() int64

	// Unix epoch time when this object was updated
	Updated() int64

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

	// IsDeleted returns TRUE if the object has been virtually deleted
	IsDeleted() 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)

	// ETag returns the signature or revision number of the object
	ETag() 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