Skip to content

lxnre-codes/mgs

Repository files navigation

Mgs

CodeQL Build & Tests GitHub release (with filter) Go MongoDB Go Report Card Go Reference Coverage Status

Mgs is a mongoose-like go mongodb odm. If you've used mongoose.js, or you're looking for a dev friendly mongodb odm for golang, then this is for you.


Features

  • Perform left join (lookup) on find operations without having to define aggregation pipelines for each query.
  • Register hooks on predefined collection schemas for CRUD operations.
  • Type safe schema & model validations.
  • Atomic WRITE operations (documents are not written to database if one or more hooks return errors).

Requirements

  • Go >=1.18 (for generics)
  • MongoDB >=4.4 (for atomic transactions)

Installation

go get github.com/0x-buidl/mgs@latest

Usage

To get started, establish a connection to mongodb client like normal.

import (
    "context"
    "time"

    "github.com/0x-buidl/mgs"
    "go.mongodb.org/mongo-driver/mongo"
    "go.mongodb.org/mongo-driver/mongo/options"
    "go.mongodb.org/mongo-driver/mongo/readpref"
)

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017"))
if err != nil {
	log.Fatal(err)
}

defer func() {
    if err = client.Disconnect(ctx); err != nil {
        panic(err)
    }
}()

err = client.Ping(ctx, nil)
if err != nil {
	log.Fatal(err)
}

db := client.Database("test")

coll := db.Collection("test_coll")

Define your collection schema and hooks.

NOTE: Do not modify hook receivers, doing so may lead to unexpected behaviours. To avoid errors, ensure hook receivers are not pointers.

type Book struct {
	Title  string      `json:"title"  bson:"title"`
	Author interface{} `json:"author" bson:"author"`
}

// For simplicity. You can also define your custom default schema that implements mgs.IDefaultSchema
type BookModel = mgs.Model[Book,*mgs.DefaultSchema]
type BookDoc = mgs.Document[Book]

func NewBookModel(coll *mongo.Collection) *BookModel {
    return mgs.NewModel[Book,*mgs.DefaultSchema](coll)
}

func (book Book) Validate(ctx context.Context, arg *mgs.HookArg[Book]) error {
	return nil
}

func (book Book) BeforeValidate(ctx context.Context, arg *mgs.HookArg[Book]) error {
	return nil
}

func (book Book) AfterValidate(ctx context.Context, arg *mgs.HookArg[Book]) error {
	return nil
}

func (book Book) BeforeCreate(ctx context.Context, arg *mgs.HookArg[Book]) error {
	return nil
}

func (book Book) AfterCreate(ctx context.Context, arg *mgs.HookArg[Book]) error {
	return nil
}

func (book Book) BeforeUpdate(ctx context.Context, arg *mgs.HookArg[Book]) error {
	return nil
}

func (book Book) AfterUpdate(ctx context.Context, arg *mgs.HookArg[Book]) error {
	return nil
}

func (book Book) BeforeDelete(ctx context.Context, arg *mgs.HookArg[Book]) error {
	return nil
}

func (book Book) AfterDelete(ctx context.Context, arg *mgs.HookArg[Book]) error {
	return nil
}

func (book Book) BeforeFind(ctx context.Context, arg *mgs.HookArg[Book]) error {
	return nil
}

func (book Book) AfterFind(ctx context.Context, arg *mgs.HookArg[Book]) error {
	return nil
}

Additional examples and usage guides can be found under the examples directory and mgs go docs.

License

This package is licensed under the Apache License.