Documentation
¶
Overview ¶
Package controller contains root neuron structure. It is responsible for getting access to registered models and their repositories. It contains the configuration, validators and default query processor.
Index ¶
- Constants
- Variables
- func CtxStore(parent context.Context, c *Controller) context.Context
- type Closer
- type Controller
- func (c *Controller) CloseAll(ctx context.Context) error
- func (c *Controller) DialAll(ctx context.Context) error
- func (c *Controller) GetRepository(model mapping.Model) (repository.Repository, error)
- func (c *Controller) GetRepositoryByModelStruct(mStruct *mapping.ModelStruct) (repository.Repository, error)
- func (c *Controller) HealthCheck(ctx context.Context) (*repository.HealthResponse, error)
- func (c *Controller) ListModels() []*mapping.ModelStruct
- func (c *Controller) MapRepositoryModels(r repository.Repository, models ...mapping.Model) (err error)
- func (c *Controller) MigrateModels(ctx context.Context, models ...mapping.Model) error
- func (c *Controller) ModelStruct(model mapping.Model) (*mapping.ModelStruct, error)
- func (c *Controller) MustModelStruct(model mapping.Model) *mapping.ModelStruct
- func (c *Controller) Now() time.Time
- func (c *Controller) RegisterModels(models ...mapping.Model) (err error)
- func (c *Controller) RegisterRepositories(repositories ...repository.Repository)
- func (c *Controller) RegisterRepositoryModels() error
- func (c *Controller) RegisterStore(name string, s store.Store) error
- func (c *Controller) SetDefaultRepository(r repository.Repository) error
- func (c *Controller) SetDefaultStore(s store.Store) error
- func (c *Controller) SetUnmappedModelRepositories() error
- type Dialer
- type Initializer
- type Options
Constants ¶
const DefaultStoreName = "nrn_default_store"
DefaultStoreName is the name of the default store.
Variables ¶
var ( // ErrController defines major classification for the controller. ErrController = errors.New("controller") // ErrRepository is an error related with the repository. ErrRepository = errors.Wrap(ErrController, "repository") // ErrRepositoryNotFound is an error when repository is not found. ErrRepositoryNotFound = errors.Wrap(ErrRepository, "not found") // ErrRepositoryAlreadyRegistered class of errors when repository is already registered. ErrRepositoryAlreadyRegistered = errors.Wrap(ErrRepository, "already registered") // ErrStoreAlreadySet is an error when the store is already set at some name. ErrStoreAlreadySet = errors.Wrap(ErrController, "store already set") )
Functions ¶
Types ¶
type Closer ¶ added in v0.17.0
Closer is an interface that closes all connection for given instance.
type Controller ¶
type Controller struct {
// Config is the configuration struct for the controller.
Options *Options
// ModelMap is a mapping for the model's structures.
ModelMap *mapping.ModelMap
// Repositories are the controller registered repositories.
Repositories map[string]repository.Repository
// ModelRepositories is the mapping between the model and related repositories.
ModelRepositories map[*mapping.ModelStruct]repository.Repository
// DefaultService is the default service or given controller.
DefaultRepository repository.Repository
// Stores is the mapping of the stores with their names.
Stores map[string]store.Store
// DefaultStore is the default key-value store used by this controller.
DefaultStore store.Store
}
Controller is the structure that contains, initialize and control the flow of the application. It contains repositories, model definitions.
func CtxGet ¶ added in v0.16.0
func CtxGet(ctx context.Context) (*Controller, bool)
CtxGet gets the controller from the context.
func NewDefault ¶
func NewDefault() *Controller
NewDefault creates new default controller based on the default config.
func (*Controller) CloseAll ¶ added in v0.15.0
func (c *Controller) CloseAll(ctx context.Context) error
CloseAll gently closes repository connections.
func (*Controller) DialAll ¶ added in v0.15.0
func (c *Controller) DialAll(ctx context.Context) error
DialAll establish connections for all repositories.
func (*Controller) GetRepository ¶ added in v0.15.0
func (c *Controller) GetRepository(model mapping.Model) (repository.Repository, error)
GetRepository gets the repository for the provided model.
func (*Controller) GetRepositoryByModelStruct ¶ added in v0.16.0
func (c *Controller) GetRepositoryByModelStruct(mStruct *mapping.ModelStruct) (repository.Repository, error)
GetRepositoryByModelStruct gets the service by model struct.
func (*Controller) HealthCheck ¶ added in v0.15.0
func (c *Controller) HealthCheck(ctx context.Context) (*repository.HealthResponse, error)
HealthCheck checks all repositories health.
func (*Controller) ListModels ¶ added in v0.15.0
func (c *Controller) ListModels() []*mapping.ModelStruct
ListModels returns a list of registered models for given controller.
func (*Controller) MapRepositoryModels ¶ added in v0.16.0
func (c *Controller) MapRepositoryModels(r repository.Repository, models ...mapping.Model) (err error)
MapRepositoryModels maps models to their repositories. If the model doesn't have repository name mapped then the controller would match default repository.
func (*Controller) MigrateModels ¶ added in v0.15.0
MigrateModels updates or creates provided models representation in their related repositories. A representation of model might be a database table, collection etc. Model's repository must implement repository.Migrator.
func (*Controller) ModelStruct ¶
func (c *Controller) ModelStruct(model mapping.Model) (*mapping.ModelStruct, error)
ModelStruct gets the model struct on the base of the provided model
func (*Controller) MustModelStruct ¶ added in v0.15.0
func (c *Controller) MustModelStruct(model mapping.Model) *mapping.ModelStruct
MustModelStruct gets the model struct from the cached model Map. Panics if the model does not exists in the map.
func (*Controller) Now ¶ added in v0.15.0
func (c *Controller) Now() time.Time
Now gets and returns current timestamp. If the configs specify the function might return UTC timestamp.
func (*Controller) RegisterModels ¶
func (c *Controller) RegisterModels(models ...mapping.Model) (err error)
RegisterModels registers provided models within the context of the provided Controller.
func (*Controller) RegisterRepositories ¶ added in v0.16.0
func (c *Controller) RegisterRepositories(repositories ...repository.Repository)
RegisterRepositories registers provided repositories.
func (*Controller) RegisterRepositoryModels ¶ added in v0.17.0
func (c *Controller) RegisterRepositoryModels() error
RegisterRepositoryModels registers models in the repositories that implements repository.ModelRegistrar.
func (*Controller) RegisterStore ¶ added in v0.17.0
func (c *Controller) RegisterStore(name string, s store.Store) error
RegisterStore registers store 's' at 'name'.
func (*Controller) SetDefaultRepository ¶ added in v0.16.0
func (c *Controller) SetDefaultRepository(r repository.Repository) error
SetDefaultRepository sets the default repository for given controller.
func (*Controller) SetDefaultStore ¶ added in v0.17.0
func (c *Controller) SetDefaultStore(s store.Store) error
SetDefaultStore sets the default store.in the controller.
func (*Controller) SetUnmappedModelRepositories ¶ added in v0.17.0
func (c *Controller) SetUnmappedModelRepositories() error
SetUnmappedModelRepositories checks if all models have their repositories mapped.
type Initializer ¶ added in v0.17.0
type Initializer interface {
Initialize(c *Controller) error
}
Initializer is an interface used to initialize services, repositories etc.
type Options ¶ added in v0.16.0
type Options struct {
// NamingConvention is the naming convention used while preparing the models.
// Allowed values:
// - camel
// - lower_camel
// - snake
// - kebab
NamingConvention mapping.NamingConvention
// SynchronousConnections defines if the query relation includes would be taken concurrently.
SynchronousConnections bool
// UTCTimestamps is the flag that defines the format of the timestamps.
UTCTimestamps bool
// DefaultNotNullFields defines if the model non-pointer fields should be marked as not null by default.
DefaultNotNullFields bool
// ModelNotNullFields defines not null fields for specified model only.
ModelNotNullFields map[mapping.Model]struct{}
}
Options defines the configuration for the Options.