Documentation
¶
Overview ¶
Package coal provides a mini ORM for mongoDB.
Index ¶
- func A(m Model, field string) string
- func C(m Model) string
- func F(m Model, field string) string
- func P(id bson.ObjectId) *bson.ObjectId
- func R(m Model, field string) string
- func Unique(ids []bson.ObjectId) []bson.ObjectId
- func Validate(m Model) error
- func ValidateTimestamps(m Model, createdAtField, updatedAtField string)
- type Base
- type Catalog
- type Field
- type HasMany
- type HasOne
- type Indexer
- type Meta
- type Model
- type Store
- type SubStore
- type ValidatableModel
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func R ¶ added in v0.11.2
R is a short-hand function to extract the relationship name of a model attribute.
func Validate ¶
Validate uses the govalidator package to validate the model based on the "valid" struct tags. If the passed model also implements the ValidatableModel interface, Validate method will be invoked after the struct validation.
func ValidateTimestamps ¶
ValidateTimestamps is a helper function that can be used inside a models Validate method to maintain "created-at" and "updated-at" timestamps.
Note: You can pass an empty string to disable certain timestamps.
Types ¶
type Base ¶
type Base struct {
DocID bson.ObjectId `json:"-" bson:"_id" valid:"required,object-id"`
// contains filtered or unexported fields
}
Base is the base for every coal model.
func (*Base) MustGet ¶
MustGet returns the value of the given field.
Note: MustGet will return the value of the first field that has a matching Name, JSONName, or BSONName and will panic if no field can be found.
func (*Base) MustSet ¶
MustSet will set the given field to the the passed valued.
Note: MustSet will set the value of the first field that has a matching Name, JSONName, or BSONName and will panic if no field can been found. The method will also panic if the type of the field and the passed value do not match.
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
NewCatalog will create a new catalog.
func (*Catalog) VisualizeModels ¶ added in v0.13.2
VisualizeModels emits a string in dot format that when put through graphviz visualizes the model dependencies.
Example ¶
catalog := NewCatalog(&postModel{}, &commentModel{}, &selectionModel{}, ¬eModel{})
fmt.Print(catalog.VisualizeModels("Test"))
Output: digraph G { rankdir="LR"; ranksep="0.5"; nodesep="0.4"; pad="0.4,0.4"; margin="0,0"; concentrate="true"; labelloc="t"; fontsize="13"; fontname="Arial BoldMT"; splines="spline"; label="Test"; "coal.commentModel" [ label=<<table border="0" align="center" cellspacing="0.5" cellpadding="0" width="134"><tr><td align="center" valign="bottom" width="130"><font face="Arial BoldMT" point-size="11">coal.commentModel</font></td></tr></table>|<table border="0" align="left" cellspacing="2" cellpadding="0" width="134"><tr><td align="left" width="130" port="Message">Message<font face="Arial ItalicMT" color="grey60"> string</font></td></tr><tr><td align="left" width="130" port="Parent">Parent<font face="Arial ItalicMT" color="grey60"> *bson.ObjectId</font></td></tr><tr><td align="left" width="130" port="Post">Post<font face="Arial ItalicMT" color="grey60"> bson.ObjectId</font></td></tr></table>>, shape=Mrecord, fontsize=10, fontname="ArialMT", margin="0.07,0.05", penwidth="1.0" ]; "coal.noteModel" [ label=<<table border="0" align="center" cellspacing="0.5" cellpadding="0" width="134"><tr><td align="center" valign="bottom" width="130"><font face="Arial BoldMT" point-size="11">coal.noteModel</font></td></tr></table>|<table border="0" align="left" cellspacing="2" cellpadding="0" width="134"><tr><td align="left" width="130" port="Title">Title<font face="Arial ItalicMT" color="grey60"> string</font></td></tr><tr><td align="left" width="130" port="CreatedAt">CreatedAt<font face="Arial ItalicMT" color="grey60"> time.Time</font></td></tr><tr><td align="left" width="130" port="UpdatedAt">UpdatedAt<font face="Arial ItalicMT" color="grey60"> time.Time</font></td></tr><tr><td align="left" width="130" port="Post">Post<font face="Arial ItalicMT" color="grey60"> bson.ObjectId</font></td></tr></table>>, shape=Mrecord, fontsize=10, fontname="ArialMT", margin="0.07,0.05", penwidth="1.0" ]; "coal.postModel" [ label=<<table border="0" align="center" cellspacing="0.5" cellpadding="0" width="134"><tr><td align="center" valign="bottom" width="130"><font face="Arial BoldMT" point-size="11">coal.postModel</font></td></tr></table>|<table border="0" align="left" cellspacing="2" cellpadding="0" width="134"><tr><td align="left" width="130" port="Title">Title<font face="Arial ItalicMT" color="grey60"> string</font></td></tr><tr><td align="left" width="130" port="Published">Published<font face="Arial ItalicMT" color="grey60"> bool</font></td></tr><tr><td align="left" width="130" port="TextBody">TextBody<font face="Arial ItalicMT" color="grey60"> string</font></td></tr><tr><td align="left" width="130" port="Comments">Comments<font face="Arial ItalicMT" color="grey60"> coal.HasMany</font></td></tr><tr><td align="left" width="130" port="Selections">Selections<font face="Arial ItalicMT" color="grey60"> coal.HasMany</font></td></tr><tr><td align="left" width="130" port="Note">Note<font face="Arial ItalicMT" color="grey60"> coal.HasOne</font></td></tr></table>>, shape=Mrecord, fontsize=10, fontname="ArialMT", margin="0.07,0.05", penwidth="1.0" ]; "coal.selectionModel" [ label=<<table border="0" align="center" cellspacing="0.5" cellpadding="0" width="134"><tr><td align="center" valign="bottom" width="130"><font face="Arial BoldMT" point-size="11">coal.selectionModel</font></td></tr></table>|<table border="0" align="left" cellspacing="2" cellpadding="0" width="134"><tr><td align="left" width="130" port="Name">Name<font face="Arial ItalicMT" color="grey60"> string</font></td></tr><tr><td align="left" width="130" port="Posts">Posts<font face="Arial ItalicMT" color="grey60"> []bson.ObjectId</font></td></tr></table>>, shape=Mrecord, fontsize=10, fontname="ArialMT", margin="0.07,0.05", penwidth="1.0" ]; "coal.commentModel"->"coal.commentModel"[ fontname="ArialMT", fontsize=7, dir=both, arrowsize="0.9", penwidth="0.9", labelangle=32, labeldistance="1.8", style=dotted, arrowhead=none, arrowtail=none ]; "coal.commentModel"->"coal.postModel"[ fontname="ArialMT", fontsize=7, dir=both, arrowsize="0.9", penwidth="0.9", labelangle=32, labeldistance="1.8", style=solid, arrowhead=normal, arrowtail=none ]; "coal.noteModel"->"coal.postModel"[ fontname="ArialMT", fontsize=7, dir=both, arrowsize="0.9", penwidth="0.9", labelangle=32, labeldistance="1.8", style=solid, arrowhead=none, arrowtail=none ]; "coal.selectionModel"->"coal.postModel"[ fontname="ArialMT", fontsize=7, dir=both, arrowsize="0.9", penwidth="0.9", labelangle=32, labeldistance="1.8", style=solid, arrowhead=normal, arrowtail=normal ]; }
type Field ¶
type Field struct {
Name string
Type reflect.Type
Kind reflect.Kind
JSONName string
BSONName string
Optional bool
ToOne bool
ToMany bool
HasOne bool
HasMany bool
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.
Note: In fire has-many relationships will be fetched without authorization. This means that the query could return ids to resources that would normally not be accessible.
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.
Note: In fire has-one relationships will be fetched without authorization. This means that the query could return ids to resources that would normally not be accessible.
type Indexer ¶
type Indexer struct {
// contains filtered or unexported fields
}
An Indexer can be used to ensure indexes for models.
func (*Indexer) Add ¶
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.
type Meta ¶
type Meta struct {
Name string
PluralName string
Collection string
Fields []Field
// contains filtered or unexported fields
}
Meta stores extracted meta data from a model.
func NewMeta ¶
NewMeta returns the Meta structure for the passed Model.
Note: This method panics if the passed Model has invalid fields and tags.
func (*Meta) FindField ¶
FindField returns the first field that has a matching Name, JSONName, BSONName or RelName. FindField will return nil if no field has been found.
func (*Meta) Make ¶
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.
func (*Meta) MustFindField ¶
MustFindField returns the first field that has a matching Name, JSONName, BSONName or RelName. MustFindField will panic if no field has been found.
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.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
A Store manages the usage of database connections.
func CreateStore ¶
CreateStore will dial the passed database and return a new store. It will return an error if the initial connection failed
func MustCreateStore ¶
MustCreateStore will dial the passed database and return a new store. It will panic if the initial connection failed.
type SubStore ¶
type SubStore struct {
// contains filtered or unexported fields
}
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.
type ValidatableModel ¶
type ValidatableModel interface {
Model
// The Validate method that should return normal errors about invalid fields.
Validate() error
}
The ValidatableModel interface can be additionally implemented to provide a custom validation method that is used by the Validate function.