db

package
v0.0.1-beta Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ObjectIdField  = "_id"
	ExpiresAtField = "_expiresAt"
)

Variables

View Source
var (
	ErrCollectionExists = errors.New("collection already exists")
	ErrIdNotFound       = errors.New("field not found")
	ErrInvalidId        = errors.New("invalid id type")
	ErrUnmarshallable   = errors.New("provided object is not a map or a struct")
)

Functions

This section is empty.

Types

type DB

type DB struct {
	// contains filtered or unexported fields
}

func (*DB) CreateCollection

func (db *DB) CreateCollection(name string) error

Create a collection in the database

func (*DB) DropCollection

func (db *DB) DropCollection(name string) error

Remove a collection from the database, removing all documents

func (*DB) Transact

func (db *DB) Transact(isWrite bool, do TransactionFunc) error

Perform actions inside a transaction

type Document

type Document struct {
	// contains filtered or unexported fields
}

Represents a document in a collection

func NewDocument

func NewDocument() *Document

Create a new empty document. User must add _id and _expiresAt themselves.

It will be validated when being used

func NewDocumentFrom

func NewDocumentFrom(from interface{}) (*Document, error)

Create a new document initialized from an object.

Please use maps, or else it will return an error

func (*Document) Decode

func (d *Document) Decode(data []byte) error

Decodes the byte arrays into the document.

Will resets all of the document's existing fields and values

func (*Document) Encode

func (d *Document) Encode() ([]byte, error)

Encodes the document into bytes array using MessagePack encoding

func (*Document) ExpiresAt

func (d *Document) ExpiresAt() *time.Time

Returns the time at which this document is expired

func (*Document) Fields

func (d *Document) Fields(withSubFields bool) []string

Returns a list of sorted keys inside the document.

func (*Document) Get

func (d *Document) Get(key string) interface{}

Returns the value associated with a key inside the document. It returns nil if the key is not found, or the value is nil

func (*Document) Has

func (d *Document) Has(key string) bool

Check if the provided key exists inside the document

func (*Document) IsValid

func (d *Document) IsValid() error

Check for if the document has valid _id and _expiresAt

func (*Document) Map

func (d *Document) Map() map[string]interface{}

Returns the document as a map

func (*Document) Marshal

func (d *Document) Marshal(from interface{}) error

Takes in an interface and marshal it into the current document

Will resets all of the document's existing fields and values

func (*Document) ObjectId

func (d *Document) ObjectId() (string, error)

Returns the document ID.

  • ErrIdNotFound if the ID is not found (key is "_id").
  • ErrInvalidId if the ID has an invalid type (must be string)

func (*Document) Set

func (d *Document) Set(key string, val interface{}) error

Set a value to a key inside the document. By default, it overrides the existing value associated to the key and also add a new key if key does not exist

func (*Document) SetExpiresAt

func (d *Document) SetExpiresAt(exp time.Time) error

Set the expiration date of this document

func (*Document) Unmarshal

func (d *Document) Unmarshal(to interface{}) error

Unpacks the document values into a struct

func (*Document) Update

func (d *Document) Update(updates map[string]interface{}, upsert bool) error

Update the fields inside the document. Set upsert to true if new fields are meant to be inserted into the document.

Upsert true is equal to calling Set on all fields

type TransactionFunc

type TransactionFunc = func(tx store.Transaction) error

Jump to

Keyboard shortcuts

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