crud

package
v0.0.0-...-2ef827d Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CreatedKey = "history.created"
	UpdatedKey = "history.updated"
	DeletedKey = "history.deleted"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ActionHistory

type ActionHistory struct {
	At     time.Time `bson:"at"`
	UserId string    `bson:"user_id"` // deprecated
}

func NewActionHistory

func NewActionHistory(at time.Time) *ActionHistory

type CrudDao

type CrudDao interface {
	// InitializeCompositeUniqueIndex will set a unique key tupling constraint to the collection.
	InitializeCompositeUniqueIndex(ctx context.Context, logger l.Logger, keys bson.D) l.Error

	// Create inserts the specified record into the database.
	Create(ctx context.Context, logger l.Logger, model interface{}) (modelId string, err l.Error)
	// CreateWithId inserts the specified record into the database with the given id.
	CreateWithId(ctx context.Context, logger l.Logger, id string, model interface{}) (modelId string, err l.Error)

	// Read retrieves the records specified by the parameters.
	Read(ctx context.Context, logger l.Logger, params bson.M, decodeModelCallback func(bsonBytes []byte, modelId string) (interface{}, error), opts ...*options.FindOptions) ([]CrudDaoModel, l.Error)
	// ReadById retrieves the record specified by the id.
	ReadById(ctx context.Context, logger l.Logger, id string, decodeModelCallback func(bsonBytes []byte, modelId string) (interface{}, error), opts ...*options.FindOptions) (model CrudDaoModel, found bool, err l.Error)

	// Update changes records based off the parameters.
	Update(ctx context.Context, logger l.Logger, params bson.M, model interface{}) l.Error
	// Update changes the record specified by the id.
	UpdateById(ctx context.Context, logger l.Logger, id string, model interface{}) l.Error

	// Delete removes one or more records from the database.
	Delete(ctx context.Context, logger l.Logger, params bson.M) l.Error
	// DeleteById removes the record matching the id.
	DeleteById(ctx context.Context, logger l.Logger, id string) l.Error

	// GetCollection returns the mongo collection being used.
	// Useful for database transactions not covered in the other actions of this struct.
	GetCollection() *mongo.Collection

	// GetConfiguration returns the configuration used by the dao.
	GetConfiguration() *CrudDaoConfig

	// SetRequiredParams is used to add required params for database calls.
	SetRequiredParams(params bson.M)
}

func NewCrudDao

func NewCrudDao(logger l.Logger, client *mongo.Client, config *CrudDaoConfig) (CrudDao, l.Error)

type CrudDaoConfig

type CrudDaoConfig struct {
	DatabaseName         string
	Namespace            string
	CollectionName       string
	SoftDelete           bool
	Timeout              time.Duration
	AllowUpsert          bool
	UniqueKeyColumnNames []string
}

type CrudDaoModel

type CrudDaoModel interface {
	GetModel() interface{}
	GetHistory() *ModelHistory
}

type ModelHistory

type ModelHistory struct {
	Created *ActionHistory `bson:"created"`
	Updated *ActionHistory `bson:"updated"`
	Deleted *ActionHistory `bson:"deleted"`
}

Jump to

Keyboard shortcuts

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