mrest

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2023 License: BSD-3-Clause Imports: 21 Imported by: 0

README

MRest

GitHub Workflow Status Coveralls github GitHub go.mod Go version Go Report Card Release GitHub license

RestFul based on MongoDB and Nats JetStream provides general CRUD, data formatting, message transaction compensation and dynamic control for low-code.

License

BSD-3-Clause License

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCollectionForbidden = errors.NewPublic("the collection is forbidden")
View Source
var ErrTxnNotExist = errors.NewPublic("the txn does not exist")
View Source
var ErrTxnTimeOut = errors.NewPublic("the transaction has timed out")

Functions

This section is empty.

Types

type BulkCreateDto

type BulkCreateDto struct {
	Collection string `path:"collection,required" vd:"regexp('^[a-z_]+$');msg:'the collection name must be lowercase letters with underscores'"`
	Data       []M    `json:"data,required" vd:"len($)>0 && range($,len(#v)>0);msg:'batch documents cannot have empty data'"`
	Xdata      M      `json:"xdata"`
	Txn        string `json:"txn"`
}

type BulkDeleteDto

type BulkDeleteDto struct {
	Collection string `path:"collection,required" vd:"regexp('^[a-z_]+$');msg:'the collection name must be lowercase letters with underscores'"`
	Filter     M      `json:"filter,required" vd:"len($)>0;msg:'the filter cannot be empty'"`
	Xfilter    M      `json:"xfilter"`
	Txn        string `json:"txn"`
}

type CommitDto

type CommitDto struct {
	Txn string `json:"txn,required"`
}

type Controller

type Controller struct {
	*Service
}

func (*Controller) BulkCreate

func (x *Controller) BulkCreate(ctx context.Context, c *app.RequestContext)

BulkCreate @router /:collection/bulk_create [POST]

func (*Controller) BulkDelete

func (x *Controller) BulkDelete(ctx context.Context, c *app.RequestContext)

BulkDelete @router /:collection/bulk_delete [POST]

func (*Controller) Commit

func (x *Controller) Commit(ctx context.Context, c *app.RequestContext)

Commit @router /commit [POST]

func (*Controller) Create

func (x *Controller) Create(ctx context.Context, c *app.RequestContext)

Create @router /:collection/create [POST]

func (*Controller) Delete

func (x *Controller) Delete(ctx context.Context, c *app.RequestContext)

Delete @router /:collection/:id [DELETE]

func (*Controller) Find

func (x *Controller) Find(ctx context.Context, c *app.RequestContext)

Find @router /:collection/find [POST]

func (*Controller) FindById

func (x *Controller) FindById(ctx context.Context, c *app.RequestContext)

FindById @router /:collection/:id [GET]

func (*Controller) FindOne

func (x *Controller) FindOne(ctx context.Context, c *app.RequestContext)

FindOne @router /:collection/fine_one [POST]

func (*Controller) Replace

func (x *Controller) Replace(ctx context.Context, c *app.RequestContext)

Replace @router /:collection/:id [PUT]

func (*Controller) Size

func (x *Controller) Size(ctx context.Context, c *app.RequestContext)

Size @router /:collection/size [POST]

func (*Controller) Sort

func (x *Controller) Sort(ctx context.Context, c *app.RequestContext)

Sort @router /:collection/sort [POST]

func (*Controller) Transaction

func (x *Controller) Transaction(ctx context.Context, c *app.RequestContext)

Transaction @router /transaction [POST]

func (*Controller) Update

func (x *Controller) Update(ctx context.Context, c *app.RequestContext)

Update @router /:collection/update [POST]

func (*Controller) UpdateById

func (x *Controller) UpdateById(ctx context.Context, c *app.RequestContext)

UpdateById @router /:collection/:id [PATCH]

type CreateDto

type CreateDto struct {
	Collection string `path:"collection" vd:"regexp('^[a-z_]+$');msg:'the collection name must be lowercase letters with underscores'"`
	Data       M      `json:"data,required" vd:"len($)>0;msg:'document cannot be empty data'"`
	Xdata      M      `json:"xdata"`
	Txn        string `json:"txn"`
}

type DeleteDto

type DeleteDto struct {
	Collection string `path:"collection,required" vd:"regexp('^[a-z_]+$');msg:'the collection name must be lowercase letters with underscores'"`
	Id         string `path:"id,required" vd:"mongoId($);msg:'the document id must be an ObjectId'"`
	Txn        string `query:"txn"`
}

type FindByIdDto

type FindByIdDto struct {
	Collection string   `path:"collection,required" vd:"regexp('^[a-z_]+$');msg:'the collection name must be lowercase letters with underscores'"`
	Id         string   `path:"id,required" vd:"mongoId($);msg:'the document id must be an ObjectId'"`
	Keys       []string `query:"keys" vd:"range($,regexp('^[a-z_]+$',#v));msg:'the projection rules are not standardized'"`
}

type FindDto

type FindDto struct {
	Collection string   `path:"collection,required" vd:"regexp('^[a-z_]+$');msg:'the collection name must be lowercase letters with underscores'"`
	Pagesize   int64    `header:"x-pagesize" vd:"$>=0 && $<=1000;msg:'the number of pages must be between 1 and 1000'"`
	Page       int64    `header:"x-page" vd:"$>=0;msg:'the page number must be greater than 0'"`
	Filter     M        `json:"filter,required"`
	Xfilter    M        `json:"xfilter"`
	Sort       []string `query:"sort" vd:"range($,regexp('^[a-z_]+:(-1|1)$',#v)));msg:'the collation is not standardized'"`
	Keys       []string `query:"keys" vd:"range($,regexp('^[a-z_]+$',#v));msg:'the projection rules are not standardized'"`
}

type FindOneDto

type FindOneDto struct {
	Collection string   `path:"collection,required" vd:"regexp('^[a-z_]+$');msg:'the collection name must be lowercase letters with underscores'"`
	Filter     M        `json:"filter,required" vd:"len($)>0;msg:'the filter cannot be empty'"`
	Xfilter    M        `json:"xfilter"`
	Keys       []string `query:"keys" vd:"range($,regexp('^[a-z_]+$',#v));msg:'the projection rules are not standardized'"`
}

type M

type M = map[string]interface{}

type Option

type Option func(x *Service)

func SetDatabase

func SetDatabase(v *mongo.Database) Option

func SetDynamicValues

func SetDynamicValues(v *values.DynamicValues) Option

func SetJetStream

func SetJetStream(v nats.JetStreamContext) Option

func SetKeyValue

func SetKeyValue(v nats.KeyValue) Option

func SetMongoClient

func SetMongoClient(v *mongo.Client) Option

func SetNamespace

func SetNamespace(v string) Option

func SetRedis

func SetRedis(v *redis.Client) Option

type PendingDto

type PendingDto struct {
	Action string             `json:"action"`
	Name   string             `json:"name"`
	Id     primitive.ObjectID `json:"id,omitempty"`
	Filter M                  `json:"filter,omitempty"`
	Data   interface{}        `json:"data,omitempty"`
}

type PublishDto

type PublishDto struct {
	Action string      `json:"action"`
	Id     string      `json:"id,omitempty"`
	Filter M           `json:"filter,omitempty"`
	Data   interface{} `json:"data,omitempty"`
	Result interface{} `json:"result"`
}

type ReplaceDto

type ReplaceDto struct {
	Collection string `path:"collection,required" vd:"regexp('^[a-z_]+$');msg:'the collection name must be lowercase letters with underscores'"`
	Id         string `path:"id,required" vd:"mongoId($);msg:'the document id must be an ObjectId'"`
	Data       M      `json:"data,required" vd:"len($)>0;msg:'document cannot be empty data'"`
	Xdata      M      `json:"xdata"`
	Txn        string `json:"txn"`
}

type Service

type Service struct {
	Namespace string
	Mgo       *mongo.Client
	Db        *mongo.Database
	RDb       *redis.Client
	JetStream nats.JetStreamContext
	KeyValue  nats.KeyValue
	Values    *values.DynamicValues
}

func New

func New(options ...Option) *Service

func (*Service) BulkCreate

func (x *Service) BulkCreate(ctx context.Context, name string, docs []interface{}) (r interface{}, err error)

func (*Service) BulkDelete

func (x *Service) BulkDelete(ctx context.Context, name string, filter M) (r interface{}, err error)

func (*Service) Commit

func (x *Service) Commit(ctx context.Context, txn string) (_ interface{}, err error)

func (*Service) Create

func (x *Service) Create(ctx context.Context, name string, doc M) (r interface{}, err error)

func (*Service) Delete

func (x *Service) Delete(ctx context.Context, name string, id primitive.ObjectID) (r interface{}, err error)

func (*Service) Find

func (x *Service) Find(ctx context.Context, name string, filter M, option *options.FindOptions) (data []M, err error)

func (*Service) FindOne

func (x *Service) FindOne(ctx context.Context, name string, filter M, option *options.FindOneOptions) (data M, err error)

func (*Service) Invoke

func (x *Service) Invoke(ctx context.Context, dto PendingDto) (_ interface{}, _ error)

func (*Service) IsForbid

func (x *Service) IsForbid(name string) bool

func (*Service) Pending

func (x *Service) Pending(ctx context.Context, txn string, dto PendingDto) (err error)

func (*Service) Pipe

func (x *Service) Pipe(input M, paths []string, kind interface{}) (err error)

func (*Service) Projection

func (x *Service) Projection(name string, keys []string) (result bson.M)

func (*Service) Publish

func (x *Service) Publish(ctx context.Context, name string, dto PublishDto) (err error)

func (*Service) Replace

func (x *Service) Replace(ctx context.Context, name string, id primitive.ObjectID, doc M) (r interface{}, err error)

func (*Service) Size

func (x *Service) Size(ctx context.Context, name string, filter M) (_ int64, err error)

func (*Service) Sort

func (x *Service) Sort(ctx context.Context, name string, key string, ids []primitive.ObjectID) (r interface{}, err error)

func (*Service) Transaction

func (x *Service) Transaction(ctx context.Context, txn string)

func (*Service) Transform

func (x *Service) Transform(data M, rules M) (err error)

func (*Service) TxnNotExists

func (x *Service) TxnNotExists(ctx context.Context, key string) (err error)

func (*Service) Update

func (x *Service) Update(ctx context.Context, name string, filter M, update M) (r interface{}, err error)

func (*Service) UpdateById

func (x *Service) UpdateById(ctx context.Context, name string, id primitive.ObjectID, update M) (r interface{}, err error)

type SizeDto

type SizeDto struct {
	Collection string `path:"collection,required" vd:"regexp('^[a-z_]+$');msg:'the collection name must be lowercase letters with underscores'"`
	Filter     M      `json:"filter,required"`
	Xfilter    M      `json:"xfilter"`
}

type SortDto

type SortDto struct {
	Collection string      `path:"collection,required" vd:"regexp('^[a-z_]+$');msg:'the collection name must be lowercase letters with underscores'"`
	Data       SortDtoData `json:"data,required"`
	Txn        string      `json:"txn"`
}

type SortDtoData

type SortDtoData struct {
	Key    string               `json:"key,required"`
	Values []primitive.ObjectID `json:"values,required" vd:"len($)>0;msg:'the submission data must be an array of ObjectId'"`
}

type UpdateByIdDto

type UpdateByIdDto struct {
	Collection string `path:"collection,required" vd:"regexp('^[a-z_]+$');msg:'the collection name must be lowercase letters with underscores'"`
	Id         string `path:"id,required" vd:"mongoId($);msg:'the document id must be an ObjectId'"`
	Data       M      `json:"data,required" vd:"len($)>0;msg:'the update cannot be empty'"`
	Xdata      M      `json:"xdata"`
	Txn        string `json:"txn"`
}

type UpdateDto

type UpdateDto struct {
	Collection string `path:"collection" vd:"regexp('^[a-z_]+$');msg:'the collection name must be lowercase letters with underscores'"`
	Filter     M      `json:"filter,required" vd:"len($)>0;msg:'the filter cannot be empty'"`
	Xfilter    M      `json:"xfilter"`
	Data       M      `json:"data,required" vd:"len($)>0;msg:'the update cannot be empty'"`
	Xdata      M      `json:"xdata"`
	Txn        string `json:"txn"`
}

Jump to

Keyboard shortcuts

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