WrappGo
WrappGo is a lightweight and user-friendly Go module that serves as a simple wrapper for MongoDB. It provides easy and basic repository functionalities, allowing developers to interact with MongoDB collections in a straightforward manner.
Features
-
Simplified MongoDB Interactions: WrappGo simplifies the process of working with MongoDB by abstracting away the complexities of low-level operations. It provides a clean and intuitive interface for common CRUD (Create, Read, Update, Delete) operations.
-
Repository Pattern: WrappGo implements the repository pattern, offering a structured and organized approach to accessing MongoDB collections. It provides convenient methods for creating, retrieving, updating, and deleting documents within the repository.
Installation
To use WrappGo in your Go project, simply install it as a dependency using the following command:
go get github.com/drzombey/wrappgo
import the module in your go project:
import "github.com/drzombey/wrappgo"
Getting Started
To get started with WrappGo, follow the examples and code snippets provided here.
Create a new MongoStore
type MyStruct struct {}
func NewWrappGoStoreExample() {
cfg := &MongoDbConfig{
Host: "your-host",
Port: 12345,
User: "your-username"
Password: "your-secret-password"
Name: "your-database-name"
}
store, err := wrappgo.New[MyStruct](ctx, cfg)
if err != nil {
panic(err)
}
}
FindBy filter
filter := wrappgo.StoreFilter{
"id": id,
"creationDate": date
}
items, err := store.FindBy(ctx, filter)
FindOneBy filter
filter := wrappgo.StoreFilter{
"id": id,
"creationDate": date
}
item, err := store.FindOneBy(ctx, filter)
Get list of items
items, err := store.Get(ctx, filter)
Create item
itemToSave := &MyStruct{
name: "AttributeName"
}
err := store.Create(ctx, itemToSave)
Delete item
filter := wrappgo.StoreFilter{
"id": id,
"creationDate": date
}
item, err := store.FindOneBy(ctx, filter)
err := store.Create(ctx, item)
Repository pattern example
type MyCollectionStruct struct {
name string,
date string
}
type MyRepo struct {
store wrappgo.IMongoStore[MyCollectionStruct]
}
func NewMyRepo() *IMongoStore {
cfg := &MongoDbConfig{
Host: "your-host",
Port: 12345,
User: "your-username"
Password: "your-secret-password"
Name: "your-database-name"
}
myStore, err := wrappgo.New[MyStruct](ctx, cfg)
if err != nil {
panic(err)
}
return &MyRepo {
store: myStore
}
}
func (s MyRepo) FindMyCollectionStructList(ctx context.Context) *[]MyCollectionStruct {
filter := wrappgo.StoreFilter{
"id": id,
}
items, err := s.store.FindBy(ctx, filter)
if err != nil {
panic(err)
}
return items
}