wrappgo

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2023 License: MIT Imports: 6 Imported by: 0

README

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
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IMongoStore

type IMongoStore[T any] interface {
	FindOneBy(ctx context.Context, filter interface{}) (*T, error)
	FindBy(ctx context.Context, filter interface{}) (*[]T, error)
	Get(ctx context.Context) (*[]T, error)
	Delete(ctx context.Context, filter interface{}) error
	Create(ctx context.Context, obj T) error
}

func New

func New[T any](ctx context.Context, uri string) (IMongoStore[T], error)

type MongoStore

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

func (MongoStore[T]) Create

func (s MongoStore[T]) Create(ctx context.Context, obj T) error

func (MongoStore[T]) Delete

func (s MongoStore[T]) Delete(ctx context.Context, filter interface{}) error

func (MongoStore[T]) FindBy

func (s MongoStore[T]) FindBy(ctx context.Context, filter interface{}) (*[]T, error)

func (MongoStore[T]) FindOneBy

func (s MongoStore[T]) FindOneBy(ctx context.Context, filter interface{}) (*T, error)

func (MongoStore[T]) Get

func (s MongoStore[T]) Get(ctx context.Context) (*[]T, error)

func (MongoStore[T]) Update

func (s MongoStore[T]) Update(ctx context.Context, filter interface{}, obj T) error

type StoreFilter

type StoreFilter = bson.M

Jump to

Keyboard shortcuts

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