coal

package
v0.17.3 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2019 License: MIT Imports: 9 Imported by: 2

Documentation

Overview

Package coal provides a mini ORM for mongoDB.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func A

func A(m Model, field string) string

A is a short-hand function to extract the attribute JSON key of a model field.

Note: A will panic if no field has been found.

func C

func C(m Model) string

C is a short-hand function to extract the collection of a model.

func Contains added in v0.15.0

func Contains(list []bson.ObjectId, id bson.ObjectId) bool

Contains returns true if a list of object ids contains the specified id.

func F

func F(m Model, field string) string

F is a short-hand function to extract the database BSON field name of a model field. Additionally, it supports the "-" prefix for retrieving sort keys.

Note: F will panic if no field has been found.

func Includes added in v0.15.1

func Includes(all, subset []bson.ObjectId) bool

Includes returns true if a list of object ids includes another list of object ids.

func N added in v0.14.0

func N() *bson.ObjectId

N is a short-hand function to get a typed nil object id pointer.

func P added in v0.8.4

func P(id bson.ObjectId) *bson.ObjectId

P is a short-hand function to get a pointer of the passed object id.

func R added in v0.11.2

func R(m Model, field string) string

R is a short-hand function to extract the relationship name of a model field.

Note: R will panic if no field has been found.

func T added in v0.17.0

func T(t time.Time) *time.Time

T is a short-hand function to get a pointer of a timestamp.

func Unique added in v0.10.2

func Unique(ids []bson.ObjectId) []bson.ObjectId

Unique is a helper to get a unique list of object ids.

Types

type Base

type Base struct {
	DocID bson.ObjectId `json:"-" bson:"_id"`
	// contains filtered or unexported fields
}

Base is the base for every coal model.

func (*Base) ID

func (b *Base) ID() bson.ObjectId

ID returns the models id.

func (*Base) Meta

func (b *Base) Meta() *Meta

Meta returns the models Meta structure.

func (*Base) MustGet

func (b *Base) MustGet(name string) interface{}

MustGet returns the value of the given field. MustGet will panic if no field has been found.

func (*Base) MustSet

func (b *Base) MustSet(name string, value interface{})

MustSet will set the given field to the the passed valued. MustSet will panic if no field has been found.

type Catalog added in v0.8.8

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

A Catalog provides a mechanism for models to access each others meta data.

func NewCatalog added in v0.8.8

func NewCatalog(models ...Model) *Catalog

NewCatalog will create a new catalog.

func (*Catalog) Add added in v0.8.8

func (c *Catalog) Add(models ...Model)

Add will add the specified models to the catalog.

func (*Catalog) All added in v0.17.0

func (c *Catalog) All() []Model

All returns a list of all registered models.

func (*Catalog) Find added in v0.8.8

func (c *Catalog) Find(pluralName string) Model

Find will return a model with the specified plural name.

func (*Catalog) Visualize added in v0.13.3

func (c *Catalog) Visualize(title string) string

Visualize emits a string in dot format which when rendered with graphviz visualizes the models and their relationships.

type Event added in v0.17.1

type Event string

Event defines the event type.

const (
	// Created is emitted when a document has been created.
	Created Event = "created"

	// Updated is emitted when a document has been updated.
	Updated Event = "updated"

	// Deleted is emitted when a document has been deleted.
	Deleted Event = "deleted"
)

type Field

type Field struct {
	// The struct field name e.g. "TireSize".
	Name string

	// The struct field type and kind.
	Type reflect.Type
	Kind reflect.Kind

	// The JSON object key name e.g. "tire-size".
	JSONKey string

	// The BSON document field e.g. "tire_size".
	BSONField string

	// Whether the field is a pointer and thus optional.
	Optional bool

	// The relationship status.
	ToOne   bool
	ToMany  bool
	HasOne  bool
	HasMany bool

	// The relationship information.
	RelName    string
	RelType    string
	RelInverse string
	// contains filtered or unexported fields
}

A Field contains the meta information about a single field of a model.

type HasMany

type HasMany struct{}

The HasMany type denotes a has-many relationship in a model declaration.

type HasOne added in v0.8.3

type HasOne struct{}

The HasOne type denotes a has-one relationship in a model declaration.

Has-one relationships requires that the referencing side is ensuring that the reference is unique. In fire this should be done using a uniqueness validator and a unique index on the collection.

type Indexer

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

An Indexer can be used to manage indexes for models.

func NewIndexer

func NewIndexer() *Indexer

NewIndexer returns a new indexer.

func (*Indexer) Add

func (i *Indexer) Add(model Model, unique bool, expireAfter time.Duration, fields ...string)

Add will add an index to the internal index list. Fields that are prefixed with a dash will result in an descending index. See the MongoDB documentation for more details.

func (*Indexer) AddPartial added in v0.17.1

func (i *Indexer) AddPartial(model Model, unique bool, expireAfter time.Duration, fields []string, filter bson.M)

AddPartial is similar to Add except that it adds a partial index.

func (*Indexer) AddRaw

func (i *Indexer) AddRaw(coll string, idx mgo.Index)

AddRaw will add a raw mgo.Index to the internal index list.

func (*Indexer) Ensure

func (i *Indexer) Ensure(store *Store) error

Ensure will ensure that the required indexes exist. It may fail early if some of the indexes are already existing and do not match the supplied index.

type Meta

type Meta struct {
	// The struct type name e.g. "models.CarWheel".
	Name string

	// The plural resource name e.g. "car-wheels".
	PluralName string

	// The collection name e.g. "car_wheels".
	Collection string

	// The struct fields.
	Fields map[string]*Field

	// The struct fields ordered.
	OrderedFields []*Field

	// The database fields.
	DatabaseFields map[string]*Field

	// The attributes.
	Attributes map[string]*Field

	// The relationships.
	Relationships map[string]*Field
	// contains filtered or unexported fields
}

Meta stores extracted meta data from a model.

func NewMeta

func NewMeta(model Model) *Meta

NewMeta returns the Meta structure for the passed Model.

Note: This method panics if the passed Model has invalid fields and tags.

func (*Meta) Make

func (m *Meta) Make() Model

Make returns a pointer to a new zero initialized model e.g. *Post.

Note: Other libraries like mgo might replace the pointer content with a new structure, therefore the model eventually needs to be initialized again using Init().

func (*Meta) MakeSlice

func (m *Meta) MakeSlice() interface{}

MakeSlice returns a pointer to a zero length slice of the model e.g. *[]*Post.

Note: Don't forget to initialize the slice using InitSlice() after adding elements with libraries like mgo.

type Model

type Model interface {
	ID() bson.ObjectId
	Meta() *Meta

	MustGet(string) interface{}
	MustSet(string, interface{})
	// contains filtered or unexported methods
}

Model is the main interface implemented by every coal model embedding Base.

func Init

func Init(model Model) Model

Init initializes the internals of a model and should be called before using a newly created Model.

func InitSlice

func InitSlice(ptr interface{}) []Model

InitSlice initializes all models in a slice of the form *[]*Post and returns a new slice that contains all initialized models.

type Receiver added in v0.17.1

type Receiver func(Event, bson.ObjectId, Model)

Receiver is a callback that receives stream events.

type Store

type Store struct {
	// The session used by the store.
	Session *mgo.Session
}

A Store manages the usage of database connections.

func CreateStore

func CreateStore(uri string) (*Store, error)

CreateStore will dial the passed database and return a new store. It will return an error if the initial connection failed

func MustCreateStore

func MustCreateStore(uri string) *Store

MustCreateStore will dial the passed database and return a new store. It will panic if the initial connection failed.

func NewStore

func NewStore(session *mgo.Session) *Store

NewStore returns a Store that uses the passed session and its default database.

func (*Store) Close

func (s *Store) Close()

Close will close the store and its associated session.

func (*Store) Copy

func (s *Store) Copy() *SubStore

Copy will make a copy of the store and the underlying session. Copied stores that are not used anymore must be closed.

type Stream added in v0.17.1

type Stream struct {

	// Reporter is called with errors.
	Reporter func(error)
	// contains filtered or unexported fields
}

Stream simplifies the handling of change streams to receives changes to documents.

func NewStream added in v0.17.1

func NewStream(store *Store, model Model) *Stream

NewStream creates and returns a new stream.

func (*Stream) Close added in v0.17.1

func (s *Stream) Close()

Close will close the stream.

func (*Stream) Open added in v0.17.1

func (s *Stream) Open(rec Receiver, open func())

Open will open the stream and continuously forward events to the specified receiver until the stream is closed. The provided open function is called when the stream has been opened the first time.

type SubStore

type SubStore struct {
	// The session used by the store.
	Session *mgo.Session
}

A SubStore allows access to the database.

func (*SubStore) C

func (s *SubStore) C(model Model) *mgo.Collection

C will return the collection associated to the passed model.

func (*SubStore) Close

func (s *SubStore) Close()

Close will close the store and its associated session.

func (*SubStore) DB

func (s *SubStore) DB() *mgo.Database

DB returns the database used by this store.

type Tester added in v0.17.1

type Tester struct {
	// The store to use for cleaning the database.
	Store *Store

	// The registered models.
	Models []Model
}

A Tester provides facilities to the test a fire API.

func NewTester added in v0.17.1

func NewTester(store *Store, models ...Model) *Tester

NewTester returns a new tester.

func (*Tester) Clean added in v0.17.1

func (t *Tester) Clean()

Clean will remove the collections of models that have been registered and reset the header map.

func (*Tester) Delete added in v0.17.1

func (t *Tester) Delete(model Model)

Delete will delete the specified model.

func (*Tester) Fetch added in v0.17.1

func (t *Tester) Fetch(model Model, id bson.ObjectId) Model

Fetch will return the saved model.

func (*Tester) FindAll added in v0.17.1

func (t *Tester) FindAll(model Model) interface{}

FindAll will return all saved models.

func (*Tester) FindLast added in v0.17.1

func (t *Tester) FindLast(model Model) Model

FindLast will return the last saved model.

func (*Tester) Save added in v0.17.1

func (t *Tester) Save(model Model) Model

Save will save the specified model.

func (*Tester) Update added in v0.17.1

func (t *Tester) Update(model Model) Model

Update will update the specified model.

Jump to

Keyboard shortcuts

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