mongox

package module
v2.7.1 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2025 License: Apache-2.0 Imports: 12 Imported by: 5

README

GitHub Repo stars GitHub issues GitHub License GitHub release (with filter) codecov Go Report Card go.dev reference All Contributors

English | 中文简体

go mongox

go mongox is a generic-based library that extends the official MongoDB framework. By using generics, it binds structs to MongoDB collections, aiming to provide type safety and simplified data operations. go mongox also introduces method chaining for smoother document operations and offers a rich set of BSON builders and built-in functions to simplify the construction of BSON data. Additionally, it supports plugin-based programming and provides various built-in hook functions, offering flexibility for custom logic before and after database operations, thus enhancing the scalability and maintainability of applications.

Feature Highlights

  • Generic MongoDB Collections
  • CRUD operations for documents
  • Aggregation operations
  • Built-in basic Model struct with automatic updates to default field values
  • BSON data construction support
  • Built-in Hooks
  • Plugin-based programming support

Install

go get github.com/chenmingyong0423/go-mongox/v2

Getting Started

Contributing

With your participation, go-mongox will become even more powerful!

Contributors

Thank you for contributing to the go mongox framework!

Community

Join our community to get support, share ideas, and collaborate with others!

Join Discord

Contributors

A heartfelt thank you to all the developers who have contributed to this project!
Contributors

Your support and efforts make this project.

Acknowledgements

JetBrains logo.

Thanks to JetBrains for supporting this project through their Open Source Licenses program.

License

© Mingyong Chen,2024-now

This project is licensed under the Apache License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client added in v2.2.0

type Client struct {
	// contains filtered or unexported fields
}

func NewClient added in v2.2.0

func NewClient(client *mongo.Client, config *Config) *Client

func (*Client) Client added in v2.2.0

func (c *Client) Client() *mongo.Client

Client returns the mongo client

func (*Client) Disconnect added in v2.2.0

func (c *Client) Disconnect(ctx context.Context) error

func (*Client) NewDatabase added in v2.2.0

func (c *Client) NewDatabase(database string) *Database

type Collection

type Collection[T any] struct {
	// contains filtered or unexported fields
}

func NewCollection

func NewCollection[T any](db *Database, collection string) *Collection[T]

func (*Collection[T]) Aggregator

func (c *Collection[T]) Aggregator() *aggregator.Aggregator[T]

func (*Collection[T]) Collection

func (c *Collection[T]) Collection() *mongo.Collection

func (*Collection[T]) Creator

func (c *Collection[T]) Creator() *creator.Creator[T]

func (*Collection[T]) Deleter

func (c *Collection[T]) Deleter() *deleter.Deleter[T]

func (*Collection[T]) Finder

func (c *Collection[T]) Finder() *finder.Finder[T]

func (*Collection[T]) Updater

func (c *Collection[T]) Updater() *updater.Updater[T]

type Config added in v2.2.0

type Config struct {
}

type Database added in v2.2.0

type Database struct {
	// contains filtered or unexported fields
}

func (*Database) Database added in v2.2.0

func (d *Database) Database() *mongo.Database

func (*Database) RegisterPlugin added in v2.2.0

func (d *Database) RegisterPlugin(name string, cb callback.CbFn, opType operation.OpType)

func (*Database) RemovePlugin added in v2.2.0

func (d *Database) RemovePlugin(name string, opType operation.OpType)

type Model

type Model struct {
	ID        bson.ObjectID `bson:"_id,omitempty" mongox:"autoID"`
	CreatedAt time.Time     `bson:"created_at"`
	UpdatedAt time.Time     `bson:"updated_at"`
	DeletedAt time.Time     `bson:"deleted_at,omitempty"`
}

Model is a base struct which includes the following fields: - ID: the primary key of the document - CreatedAt: the time when the document was created - UpdatedAt: the time when the document was last updated It may be embedded into a struct to provide these fields. Example:

type User struct {
	mongox.Model
}

func (*Model) DefaultCreatedAt

func (m *Model) DefaultCreatedAt() time.Time

func (*Model) DefaultId

func (m *Model) DefaultId() bson.ObjectID

func (*Model) DefaultUpdatedAt

func (m *Model) DefaultUpdatedAt() time.Time

Directories

Path Synopsis
builder
internal
Code generated by MockGen.
Code generated by MockGen.

Jump to

Keyboard shortcuts

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