Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultErrorHandler = func(ctx iris.Context, err error) bool { if _, ok := pg.IsErrDuplicate(err); ok { errors.AlreadyExists.Details(ctx, "resource already exists", err.Error()) } else if _, ok = pg.IsErrInputSyntax(err); ok { errors.InvalidArgument.Err(ctx, err) } else if errors.Is(err, pg.ErrNoRows) { errors.NotFound.Details(ctx, "resource not found", err.Error()) } else if _, ok = pg.IsErrForeignKey(err); ok { errors.InvalidArgument.Message(ctx, "reference entity does not exist") } else if errors.Is(err, strconv.ErrSyntax) { errors.InvalidArgument.Err(ctx, err) } else if _, ok = pg.IsErrInputSyntax(err); ok { errors.InvalidArgument.Err(ctx, err) } else if errMsg := err.Error(); strings.Contains(errMsg, "syntax error in") || strings.Contains(errMsg, "invalid input syntax") { if strings.Contains(errMsg, "invalid input syntax for type uuid") { errors.InvalidArgument.Err(ctx, err) } else { errors.InvalidArgument.Details(ctx, "invalid syntax", errMsg) } } else { errors.Internal.Err(ctx, err) } return true }
DefaultErrorHandler is the default error handler for the PG middleware.
var ErrNoRows = pg.ErrNoRows
ErrNoRows is a type alias of pg.ErrNoRows.
var NewSchema = pg.NewSchema
NewSchema returns a new Schema instance, it's a shortcut of pg.NewSchema.
Functions ¶
func IsErrNoRows ¶
IsErrNoRows reports whether the error is of type pg.ErrNoRows.
func Repository ¶
func Repository[T any](ctx iris.Context) *pg.Repository[T]
Repository returns a new Repository of T type by the database instance binded to the request Context.
Types ¶
type EntityController ¶
type EntityController[T any] struct { iris.Singleton // GetID returns the entity ID for GET/{id} and DELETE/{id} paths from the request Context. GetID func(ctx iris.Context) any // ErrorHandler defaults to the PG's error handler. It can be customized for this controller. // Setting this to nil will panic the application on the first error. ErrorHandler func(ctx iris.Context, err error) bool // AfterPayloadRead is called after the payload is read. // It can be used to validate the payload or set default fields based on the request Context. AfterPayloadRead func(ctx iris.Context, payload T) (T, bool) // contains filtered or unexported fields }
EntityController is a controller for a single entity. It can be used to create a RESTful API for a single entity. It is a wrapper around the pg.Repository. It can be used as a base controller for a custom controller. The T is the entity type (e.g. a custom type, Customer).
The controller registers the following routes: - GET /schema - returns the entity's JSON schema. - POST / - creates a new entity. - PUT / - updates an existing entity. - GET /{id} - gets an entity by ID. - DELETE /{id} - deletes an entity by ID. The {id} parameter is the entity ID. It can be a string, int, uint, uuid, etc.
func NewEntityController ¶
func NewEntityController[T any](middleware *PG) *EntityController[T]
NewEntityController returns a new EntityController[T]. The T is the entity type (e.g. a custom type, Customer).
Read the type's documentation for more information.
func (*EntityController[T]) Configure ¶
func (c *EntityController[T]) Configure(r iris.Party)
Configure registers the controller's routes. It is called automatically by the Iris API Builder when registered to the Iris Application.
func (*EntityController[T]) WithoutSchemaRoute ¶
func (c *EntityController[T]) WithoutSchemaRoute() *EntityController[T]
WithoutSchemaRoute disables the GET /schema route.
type Options ¶
type Options struct { // Connection options. Host string `yaml:"Host"` Port int `yaml:"Port"` User string `yaml:"User"` Password string `yaml:"Password"` Schema string `yaml:"Schema"` DBName string `yaml:"DBName"` SSLMode string `yaml:"SSLMode"` // Trace bool `yaml:"Trace"` // If true then database tracer with Logger will be registered. // Transactional bool `yaml:"Transactional"` // If true then all requests will be executed in transaction. CreateSchema bool `yaml:"CreateSchema"` // If true then schema will be created if not exists. CheckSchema bool `yaml:"CheckSchema"` // If true then check the schema for missing tables and columns. // // The error handler for the middleware. // The implementation can ignore the error and return false to continue to the default error handler. ErrorHandler func(ctx iris.Context, err error) bool }
Options is the configuration for the PG middleware. It is used to customize the connection to the database.
See https://pkg.go.dev/github.com/kataras/pg for more information.