Skip to content

angrypufferfish/goodm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Goodm

  1. About The Project
  2. Installation
  3. Connection
  4. Define Document
  5. Save a Document
  6. Save and serialize output
  7. List documents
  8. Operators
  9. Usage
  10. Contributing
  11. License

About The Project

Golang ODM based on the official MongoDB driver.

Installation

go get github.com/angrypufferfish/goodm

Connection

import (
  "context"
  "github.com/angrypufferfish/goodm"
)

func Connect() {

  var iGoodm goodm.Goodm
  ctx := context.Background()

  client, err := iGoodm.Connect("mongodb://localhost:27017", 10000)

  ///!
  defer iGoodm.Disconnect()

  if err != nil {
    panic(err)
  }
  client.UseDatabase("test", &ctx)
}

Define Document

package example

import (
  "github.com/angrypufferfish/goodm/src/base"
)

type UserInfo struct {
  Address string `json:"address" bson:"address"`
}

type User struct {
  //Define collection name on goodm tag
  base.BaseDocument `json:",inline" bson:",inline" goodm:"users"`

  LastName  string     `json:"lastName" bson:"lastName"`
  FirstName string     `json:"firstName" bson:"firstName"`
  Info  UserInfo       `json:"info" bson:"info"`
}

Usage

Save a document

package example

import (
  "github.com/angrypufferfish/goodm"
)

user := &User{
	FirstName: "Mario",
	LastName:  "Rossi",
	Info: UserInfo{
		Address: "via campo",
	},
}

savedUser := goodm.Create[User](user)

Save and serialize output

package example

import (
  "github.com/angrypufferfish/goodm"
)

type UserName struct {
	FirstName string            `json:"firstName" bson:"firstName"`
	LastName string             `json:"lastName" bson:"lastName"`
}

user := &User{
	FirstName: "Mario",
	LastName:  "Rossi",
	Info: UserInfo{
		Address: "via campo",
	},
}

savedUser := goodm.CreateAndSerialize[User, UserName](user)
//output usr
//{
//  "firstName": "Mario",
//  "lastName": "Rossi",
//}

List

package example

import (
  "github.com/angrypufferfish/goodm"
)

type UserName struct {
	FirstName string            `json:"firstName" bson:"firstName"`
	LastName string             `json:"lastName" bson:"lastName"`
}

//Output users: []User
users := goodm.FindAll[User]()

//Output serializedUsers: []UserName
serializedUsers := goodm.FindAllAndSerialize[User, UserName]()

Operators

package example

import (
  "github.com/angrypufferfish/goodm"
  "github.com/angrypufferfish/goodm/src/query"
)


/// {"$and": [
///    {"lastName": {"$ne": "Doe"}},
///    {"firstName": {"$eq": "Mario"}}
///  ]}
users := goodm.Find[User](
  query.And(
    ///{"lastName": {"$ne": "Doe"}}
		query.Ne("lastName", "Doe"),
    ///{"firstName": {"$eq": "Mario"}}
		query.Eq("firstName", "Mario"),
	),
)

Contributing

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue.

License

Distributed under the APACHE LICENSE 2.0 See LICENSE.txt for more information.

Releases

No releases published

Packages

No packages published

Languages