Thanks to visit codestin.com
Credit goes to github.com

Skip to content

go-monarch/monarch

Repository files navigation

Monarch - ORM for mongodb

Introduction

Monarch is an ORM written for mongodb queries in go. While the current mongo driver works great, writing repository logic over and over again can get very tedious and repetitive. Monarch exists as a middle ground for developers, giving the ease of an ORM while not as bloated as a traditional one.

Usage

Download

go mod init my-app

go get -u github.com/go-monarch/monarch

Set up

package main

type User struct {
    ID string `monarch:"id,index"`
    Email string `monarch:"email"`
    Password string `monarch:"password"`
    Auto bool
}

func main(){
    c, err := monarch.Connect("mongodb://localhost")
	if err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	}
	m := monarch.New(c)

	u, err := monarch.RegisterCollection(m, User{})
	if err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	}
}

Monarch has a beatiful collection api that allows for seamless data queries.

Queries

//Find Many
d, err := u.Query(context.Background(), query.SetLimit(3)).FindMany()
	if err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	}

//Find One
d, err := u.Query(context.Background(), query.WithFilter("id", "user_id")).FindOne()
	if err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	}

//Update One
err := u.Query(context.Background(), query.WithFilter("id", "user_id")).UpdateOne(User{})
	if err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	}

//Update Many
err := u.Query(context.Background(), query.WithFilter("id", "user_id")).UpdateMany(User{})
	if err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	}

//Delete One
err := u.Query(context.Background(), query.WithFilter("id", "user_id")).DeleteOne()
	if err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	}

//Delete Many
err := u.Query(context.Background(), query.WithFilter("id", "user_id")).DeleteMany()
	if err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	}

Save

if err := u.Save(context.Background(), User{
		ID:        uuid.New(),
		Email:     "[email protected]",
	}); err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages