mgmrel

package module
v0.0.0-...-5cb3569 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2020 License: Apache-2.0 Imports: 10 Imported by: 1

README

Mongo Go Models(mgm) relation implements has-one,has-many relations for the Mongo Go Models (mgm) package.

Hooks

  • Syncing () error : calls before sync. if return error, ew cancel sync and return that error to the caller.
  • Synced () error : calls after sync. if return error, we return that error to the caller.

Important Notes:

  • This package use Mongo Go Models native methods, so you can not expect to have behavior of mgn (like set ID on the model, or update created_at,updated_at fields...).
    You can write your sync hooks or use default mgm-relation implementation of sync hooks to handle it.

TODO

  • We can also automatically set the foreign key field on each model before saving it. implement it if you need(find foreign key field on the related model by bson tag's value).

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HasManyRelation

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

func HasMany

func HasMany(model mgm.Model, related mgm.Model) *HasManyRelation

HasMany returns new instance of the "has many" relation ship.

func HasManyWithOptions

func HasManyWithOptions(model mgm.Model, related mgm.Model, foreignKey string) *HasManyRelation

HasManyWithOptions gets HasManyRelation options and returns new instance of it.

func (*HasManyRelation) Get

func (r *HasManyRelation) Get(results interface{}, sort string, skip, limit int64) error

Get method get the list of related models with provided filter,limit,... if not found, returns the Mongo Go driver not found error.

func (*HasManyRelation) GetWithOptions

func (r *HasManyRelation) GetWithOptions(results interface{}, options ...*options.FindOptions) error

Get method get the list of related models with provided filter,limit,... if not found, returns the Mongo Go driver not found error.

func (*HasManyRelation) SimpleGet

func (r *HasManyRelation) SimpleGet(results interface{}, limit int64) error

SimpleGet method get the list of related models if not found, returns the Mongo Go driver not found error. sort is the sort field. you can sort descending by adding a `-` to the sort field. e.g `-created_at`

func (*HasManyRelation) Sync

func (r *HasManyRelation) Sync(docs interface{}) error

Sync method sync the relations: If provided models is nil(or length is zero): it remove the related models in the DB. If provided models is not nil and length is not zero: udpate new items, and remove items that are not in the provided list. Use sync just when your 1-m model contains just few m mdoel. otherwise use SyncWithoutRemove

func (*HasManyRelation) SyncWithoutRemove

func (r *HasManyRelation) SyncWithoutRemove(docs interface{}) error

SyncWithoutRemove method sync the relations without removing items that are not in the provided list.

type HasOneRelation

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

func HasOne

func HasOne(model mgm.Model, related mgm.Model) *HasOneRelation

HasOne returns new instance of the "has one" relation ship.

func HasOneByOptions

func HasOneByOptions(model mgm.Model, related mgm.Model, foreignKey string) *HasOneRelation

HasOneByOptions gets HasOneRelation options and returns new instance of it.

func (*HasOneRelation) Get

func (r *HasOneRelation) Get(m mgm.Model) error

SimpleGet method get the single related model. if not found, returns the Mongo Go driver not found error.

func (*HasOneRelation) Sync

func (r *HasOneRelation) Sync(model mgm.Model) error

Sync method sync the relations: If provided model is nil: it remove the related model in the DB. If provided model is not nil: sync it. insert new model, otherwise upsert provided model.

type IDField

type IDField struct {
	ID primitive.ObjectID `json:"id" bson:"_id,omitempty"`
}

IDField struct contain model's ID field. it also implements the SyncingHook to set id before sync the model. you can use this IDField instead of the mgm IDField.

func (*IDField) GetID

func (f *IDField) GetID() interface{}

GetID method return model's id

func (*IDField) PrepareID

func (f *IDField) PrepareID(id interface{}) (interface{}, error)

PrepareID method prepare id value to using it as id in filtering,... e.g convert hex-string id value to bson.ObjectId

func (*IDField) SetID

func (f *IDField) SetID(id interface{})

SetID set id value of model's id field.

func (*IDField) Syncing

func (f *IDField) Syncing() error

Syncing set the ID if it's zero(empty ID).

type SyncedHook

type SyncedHook interface {
	Synced() error
}

SyncedHook is the interface to implement hook to call after sync your model.

type SyncingHook

type SyncingHook interface {
	Syncing() error
}

SyncingHook is the interface to implement hook to call before sync your model.

Jump to

Keyboard shortcuts

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