package
0.0.0-20240819013456-0d062cb8a7db
Repository: https://github.com/cosys-io/cosys.git
Documentation: pkg.go.dev

# README

Cosys - Common

The package to be imported in cosys projects.

Documentation

type Cosys

Cosys is the main app, where the database, module service, server, models, services, configs can be accessed from. Models and services are accessed via their uids module_name.model_name, module_name.service_name respectively.

When the Cosys app is run, the Cosys struct undergoes the following procedure.

  1. Import - The database, module service and server are registered using the blank import method (init function).
  2. Initialization - The Cosys struct is initialized, the config yaml files are parsed and loaded into the struct.
  3. Registration - The modules are registered. The models and services from each module are loaded into the Cosys struct. The 'OnRegister' function of each module is run.
  4. Listening - The server from the Cosys struct is started.
  5. Destruction - Clean-up processes before the app is shutdown. The 'OnDestroy' function of each module is run.

func Database

func (c Cosys) Database() Database

Returns the Database struct.

func ModuleService

func (c Cosys) ModuleService() ModuleService

Returns the ModuleService struct

func Server

func (c Cosys) Server() Server

Returns the Server struct

func Register

func (c Cosys) Register(modules map[string]*Module) (*Cosys, error)

Register the modules. The models and services of each module are loaded into the Cosys struct and the ‘OnRegister’ functions of each module are run.

func Start

func (c Cosys) Start() error

Starts the server.

func Destroy

func (c Cosys) Destroy() (*Cosys, error)

Runs clean-up processes. The ‘OnDestroy’ functions of each module are run.

type Module

type Module struct {
	Routes      []*Route
	Controllers map[string]*Controller
	Middlewares map[string]Middleware
	Policies    map[string]Policy

	Models   map[string]Model
	Services map[string]Service
	
	OnRegister func(Cosys) error
	OnDestroy  func(Cosys) error
}

The entrypoint into modules.

Routes, Controllers, Middlewares, Policies are used by the server to create API endpoints.

Models and Services from the model can be accessed by any other module through the Cosys struct.

OnRegister and OnDestroy are called when the app is started and closed respectively.

type Database

type Database interface {
	FindOne(uid string, params DBParams) (Entity, error)
	FindMany(uid string, params DBParams) ([]Entity, error)
	Create(uid string, data Entity, params DBParams) (Entity, error)
	CreateMany(uid string, data []Entity, params DBParams) ([]Entity, error)
	Update(uid string, data Entity, params DBParams) (Entity, error)
	UpdateMany(uid string, data Entity, params DBParams) ([]Entity, error)
	Delete(uid string, params DBParams) (Entity, error)
	DeleteMany(uid string, params DBParams) ([]Entity, error)
}

Database provides an API to interact with the database layer directly.

Database will be registered using blank imports (call RegisterDatabase in init function) in a mandatory Database module.

type ModuleService

type ModuleService interface {
	FindOne(uid string, id int, params MSParams) (Entity, error)
	FindMany(uid string, params MSParams) ([]Entity, error)
	Create(uid string, data Entity, params MSParams) (Entity, error)
	Update(uid string, data Entity, id int, params MSParams) (Entity, error)
	Delete(uid string, id int, params MSParams) (Entity, error)
}

ModuleService will be registered using blank imports (call RegisterModuleService in init function) in a mandatory ModuleService module.

type Server

type Server interface {
	Start(port string) error
}

Server will be registered using blank imports (call RegisterServer in init function) in a mandatory Server module.

type Controller

type Controller map[string]Action

Controller is a grouping of similar actions.

type Action

type Action func(Cosys) http.HandlerFunc

Action handles incoming server requests.

type Middleware

type Middleware func(Cosys) func(http.HandlerFunc) http.HandlerFunc

Middleware runs processes before and after the controller handles incoming requests.

type Policy

type Policy func(Cosys, *http.Request) bool

Policy checks specific conditions before the controller handles incoming requests.

type Route

type Route struct {
	Method      string
	Regex       *regexp.Regexp
	Action      string
	Middlewares []string
	Policies    []string
}

Route specifies which controller and action should handle an incoming request based on its path and method, and can be configured with policies and middleware.

Request paths are specified using regex, and actions are specified using their uid controller_name.action_name.

type Lifecycle

type Lifecycle map[string]LifecycleFunc

type LifecycleFunc func(params DBParams, result any, state any) (afterState any, err error)

Lifecycle is the collection of lifecycle functions of a content type. LifecycleFunc is a function called before or after certain actions are performed on a content type. LifecycleFunc takes in the DBParams passed to the Database, the result of the Database call (only available for “after” actions, set to nil for “before” actions), and a state variable that is initialised as nil and will be passed from the “before” to the “after” action.

type Model

type Model interface {
	// Returns an entity of the corresponding content type with zero values.
	New_() Entity
	
	// Returns an array containing all the attributes of the content type.
	All_() []Attribute
	
	// Returns the id attribute of the content type.
	Id_() *IntAttribute
	
	// Returns the schema name of the content type.
	Name_() string
	
	// Returns the schema of the content type.
	Schema_() *ModelSchema
	
	// Returns the lifecycle of the content type.
	Lifecycle_() Lifecycle
}

Model represents a content type.

type Entity

type Entity interface {
}

Entity represents an instance of a content type.

type Attribute

type Attribute interface {
	// Returns the schema name of the attribute.
	Name() string
	
	// Returns the name of the field in the corresponding entity struct.
	FieldName() string

	// Returns the "ascending" order-by condition for this attribute. 
	// To be used in DBParams or MSParams.
	Asc() *Order
	
	// Returns the "descending" order-by condition for this attribute. 
	// To be used in DBParams or MSParams.
	Desc() *Order

	// Returns the "is null" where condition for this attribute. 
	// To be used in DBParams or MSParams.
	Null() Condition
	
	// Returns the "is not null" where condition for this attribute. 
	// To be used in DBParams or MSParams.
	NotNull() Condition
}

Attribute represents an attribute of a content-type.

# Functions

Default specifies that an attribute has the given default value.
Enum specifies that an attribute is can only take the given values.
GetAction returns the actionFunc based on the actionFunc added to the cosys instance under the given uid.
GetMiddlewares adds middlewareFuncs to the route, based on the middlewareFuncs added to the cosys instance under the given uids.
No description provided by the author
GetPolicies adds policyFuncs to the route, based on the policyFuncs added to the cosys instance under the given uids.
GetService returns the singleton Service map Unsafe for concurrent use as it exposes the underlying singleService object to everyone.
No description provided by the author
Max specifies that an attribute has the given maximum value.
MaxLength specifies that an attribute has the given maximum length.
Min specifies that an attribute has the given minimum value.
MinLength specifies that an attribute has the given minimum length.
New returns a new cosys instance, with modules registered.
NewAction returns a new action with the given uid and actionFunc.
NewAttrSchema returns a new attribute schema from the given name, types and configurations.
NewBoolAttribute returns a new boolean attribute with the given name.
NewController returns a new controller with the given uid and actions, or throws an error if multiple actions have the same uid.
NewDBParams returns a new DBParams with default conditions.
NewDBParamsBuilder returns a new DBParamsBuilder with default conditions.
NewIntAttribute returns a new integer attribute with the given name.
NewLifecycle returns a new lifecycle.
NewMiddleware returns a new middleware with the given uid and middlewareFunc.
NewModel returns a new model that is built upon ModelBase.
NewModelSchema returns a new model schema from the given names and attribute schemas.
NewPolicy returns a new policy with the given uid and policyFunc.
NewRoute returns a new route with configurations.
NewStringAttribute returns a new string attribute with the given name.
RegisterModule registers a module to the cosys app.
RegisterService registers a service to the singleton Service map RegisterService will return an error if the service name already exists in the singleton Service map Safe for concurrent use.
RegisterServices registers a map of services to the singleton Service map RegisterServices will return an error if a service name already exists in the singleton Service map Safe for concurrent use.
UseMiddlewares adds middlewareFuncs to the route.
UsePolicies adds policyFuncs to the route.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Variables

IdSchema is the schema for the id attribute.
NotEditable specifies that an attribute is not editable.
NotNullable specifies that an attribute is cannot be null.
Private specifies that an attribute is private.
Required specifies that an attribute is required.
Unique specifies that an attribute is unique.
UuidSchema is the schema for the uuid attribute.

# Structs

Action is a wrapper around ActionFunc that allows them to be identifiable by uid.
BoolAttribute is an attribute of boolean datatype.
Controller is a group of actions.
Cosys is the cosys app.
DBParams are query conditions.
DBParamsBuilder is a builder for DBParams.
EventQuery is the query data associated with a lifecycle event.
ExpressionCondition is a condition formed by performing operations a value.
IntAttribute is an attribute of integer datatype.
Lifecycle is a group of lifecycle hooks associated with a model.
Middleware is a wrapper around MiddlewareFunc that allows them to be identifiable by uid.
ModelBase pointers can be embedded into structs to provide them with the methods to implement the Model interface.
NestedCondition is a where condition formed by performing the "not", "and", or "or" operations on other condition/s.
Order is an order-by condition.
Policy is a wrapper around PolicyFunc that allows them to be identifiable by uid.
Route specifies an api endpoint.
StringAttribute is an attribute of string datatype.

# Interfaces

Attribute is a field of a model.
AttributeSchema is a schema for a model attribute.
Condition is a where condition.
Database is a core service for interacting with the relational database.
Entity is a record/document.
Logger is a core service for logging.
Model is a content model.
ModelSchema is a schema for a model.
Server is a core service for the external API.

# Type aliases

ActionFunc takes in cosys instance and returns a handler.
AttrOption is a configuration for an attribute schema.
BootstrapHook is a hook called during the bootstrap stage of the cosys app.
CleanupHook is a hook called during the cleanup stage of the cosys app.
Command takes in a cosys instance and returns a command.
Environment specifies which environment the app is running in.
No description provided by the author
LifecycleHook is a hook that is called when a model's lifecycle event happens.
LogLevel is the alert level of a log message.
MiddlewareFunc takes in a cosys instance and returns a middleware.
Module is a hook that is called during the registration stage.
No description provided by the author
No description provided by the author
PolicyFunc takes in a cosys instance and returns a policy.
RouteOption is a route configuration.
No description provided by the author
State specifies which stage the cosys app is in.