Documentation ¶
Index ¶
- Variables
- func Field(name string) *field
- func NewObjectId() string
- type Config
- type Criteria
- type DB
- func (db *DB) Close() error
- func (db *DB) CreateCollection(name string) error
- func (db *DB) DropCollection(name string) error
- func (db *DB) ExportCollection(collectionName string, exportPath string) error
- func (db *DB) HasCollection(name string) (bool, error)
- func (db *DB) ImportCollection(collectionName string, importPath string) error
- func (db *DB) Insert(collectionName string, docs ...*Document) error
- func (db *DB) InsertOne(collectionName string, doc *Document) (string, error)
- func (db *DB) ListCollections() ([]string, error)
- func (db *DB) Query(name string) *Query
- func (db *DB) Save(collectionName string, doc *Document) error
- type Document
- func (doc *Document) Copy() *Document
- func (doc *Document) Get(name string) interface{}
- func (doc *Document) Has(name string) bool
- func (doc *Document) ObjectId() string
- func (doc *Document) Set(name string, value interface{})
- func (doc *Document) SetAll(values map[string]interface{})
- func (doc *Document) Unmarshal(v interface{}) error
- type Option
- type Query
- func (q *Query) Count() (int, error)
- func (q *Query) Delete() error
- func (q *Query) DeleteById(id string) error
- func (q *Query) Exists() (bool, error)
- func (q *Query) FindAll() ([]*Document, error)
- func (q *Query) FindById(id string) (*Document, error)
- func (q *Query) FindFirst() (*Document, error)
- func (q *Query) ForEach(consumer func(_ *Document) bool) error
- func (q *Query) Limit(n int) *Query
- func (q *Query) MatchPredicate(p func(doc *Document) bool) *Query
- func (q *Query) ReplaceById(docId string, doc *Document) error
- func (q *Query) Skip(n int) *Query
- func (q *Query) Sort(opts ...SortOption) *Query
- func (q *Query) Update(updateMap map[string]interface{}) error
- func (q *Query) UpdateById(docId string, updateMap map[string]interface{}) error
- func (q *Query) Where(c *Criteria) *Query
- type SortOption
- type StorageEngine
Constants ¶
This section is empty.
Variables ¶
var ( ErrCollectionExist = errors.New("collection already exist") ErrCollectionNotExist = errors.New("no such collection") )
Collection creation errors
var ErrDocumentNotExist = errors.New("no such document")
var ErrDuplicateKey = errors.New("duplicate key")
Functions ¶
func Field ¶
func Field(name string) *field
Field represents a document field. It is used to create a new criteria.
func NewObjectId ¶ added in v1.1.0
func NewObjectId() string
Types ¶
type Config ¶ added in v1.1.0
type Config struct { InMemory bool Storage StorageEngine }
Config contains clover configuration parameters
type Criteria ¶
type Criteria struct {
// contains filtered or unexported fields
}
Criteria represents a predicate for selecting documents. It follows a fluent API style so that you can easily chain together multiple criteria.
func (*Criteria) And ¶
And returns a new Criteria obtained by combining the predicates of the provided criteria with the AND logical operator.
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB represents the entry point of each clover database.
func Open ¶
Open opens a new clover database on the supplied path. If such a folder doesn't exist, it is automatically created.
func (*DB) Close ¶
Close releases all the resources and closes the database. After the call, the instance will no more be usable.
func (*DB) CreateCollection ¶
CreateCollection creates a new empty collection with the given name.
func (*DB) DropCollection ¶
DropCollection removes the collection with the given name, deleting any content on disk.
func (*DB) ExportCollection ¶ added in v1.1.0
ExportCollection exports an existing collection to a JSON file.
func (*DB) HasCollection ¶
HasCollection returns true if and only if the database contains a collection with the given name.
func (*DB) ImportCollection ¶ added in v1.1.0
ImportCollection imports a collection from a JSON file.
func (*DB) InsertOne ¶
InsertOne inserts a single document to an existing collection. It returns the id of the inserted document.
func (*DB) ListCollections ¶ added in v1.1.0
ListCollections returns a slice of strings containing the name of each collection stored in the db.
type Document ¶
type Document struct {
// contains filtered or unexported fields
}
Document represents a document as a map.
func NewDocumentOf ¶
func NewDocumentOf(o interface{}) *Document
NewDocumentOf creates a new document and initializes it with the content of the provided object. It returns nil if the object cannot be converted to a valid Document.
func (*Document) Has ¶
Has tells returns true if the document contains a field with the supplied name.
func (*Document) ObjectId ¶
ObjectId returns the id of the document, provided that the document belongs to some collection. Otherwise, it returns the empty string.
type Option ¶ added in v1.1.0
Option is a function that takes a config struct and modifies it
func InMemoryMode ¶ added in v1.1.0
InMemoryMode allows to enable/disable in-memory mode.
func WithStorageEngine ¶ added in v1.1.0
func WithStorageEngine(engine StorageEngine) Option
WithStorageEngine allows to specify a custom storage engine.
type Query ¶
type Query struct {
// contains filtered or unexported fields
}
Query represents a generic query which is submitted to a specific collection.
func (*Query) Count ¶
Count returns the number of documents which satisfy the query (i.e. len(q.FindAll()) == q.Count()).
func (*Query) Delete ¶
Delete removes all the documents selected by q from the underlying collection.
func (*Query) DeleteById ¶
DeleteById removes the document with the given id from the underlying collection, provided that such a document exists and satisfies the underlying query.
func (*Query) Exists ¶ added in v1.1.0
Exists returns true if and only if the query result set is not empty.
func (*Query) FindById ¶
FindById returns the document with the given id, if such a document exists and satisfies the underlying query, or null.
func (*Query) FindFirst ¶ added in v1.1.0
FindFirst returns the first document (if any) satisfying the query.
func (*Query) ForEach ¶ added in v1.1.0
ForEach runs the consumer function for each document matching the provied query. If false is returned from the consumer function, then the iteration is stopped.
func (*Query) Limit ¶ added in v1.1.0
Limit sets the query q to consider at most n records. As a consequence, the FindAll() method will output at most n documents, and any integer m returned by Count() will satisfy the condition m <= n.
func (*Query) MatchPredicate ¶
MatchPredicate selects all the documents which satisfy the supplied predicate function.
func (*Query) ReplaceById ¶ added in v1.1.0
ReplaceById replaces the document with the specified id with the one provided. If no document exists, an ErrDocumentNotExist is returned.
func (*Query) Sort ¶ added in v1.1.0
func (q *Query) Sort(opts ...SortOption) *Query
Sort sets the query so that the returned documents are sorted according list of options.
func (*Query) Update ¶
Update updates all the document selected by q using the provided updateMap. Each update is specified by a mapping fieldName -> newValue.
func (*Query) UpdateById ¶ added in v1.1.0
UpdateById updates the document with the specified id using the supplied update map. If no document with the specified id exists, an ErrDocumentNotExist is returned.
type SortOption ¶ added in v1.1.0
SortOption is used to specify sorting options to the Sort method. It consists of a field name and a sorting direction (1 for ascending and -1 for descending). Any other positive of negative value (except from 1 and -1) will be equivalent, respectively, to 1 or -1. A direction value of 0 (which is also the default value) is assumed to be ascending.
type StorageEngine ¶
type StorageEngine interface { Open(path string) error Close() error CreateCollection(name string) error ListCollections() ([]string, error) DropCollection(name string) error HasCollection(name string) (bool, error) FindAll(q *Query) ([]*Document, error) FindById(collectionName string, id string) (*Document, error) UpdateById(collectionName string, docId string, updater func(doc *Document) *Document) error DeleteById(collectionName string, id string) error IterateDocs(q *Query, consumer docConsumer) error Insert(collection string, docs ...*Document) error Update(q *Query, updater func(doc *Document) *Document) error Delete(q *Query) error }
StorageEngine represents the persistance layer and abstracts how collections are stored.