https://github.com/jensteichert/colt
A productive and type-safe MongoDB ODM for Go. Based on the official MongoDB driver but elegant to use.
https://github.com/jensteichert/colt
database generics go golang mongodb mongodb-driver mongoose odm
Last synced: 18 days ago
JSON representation
A productive and type-safe MongoDB ODM for Go. Based on the official MongoDB driver but elegant to use.
- Host: GitHub
- URL: https://github.com/jensteichert/colt
- Owner: jensteichert
- License: mit
- Created: 2022-08-14T15:20:33.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-03-03T19:33:34.000Z (10 months ago)
- Last Synced: 2025-03-03T20:32:24.338Z (10 months ago)
- Topics: database, generics, go, golang, mongodb, mongodb-driver, mongoose, odm
- Language: Go
- Homepage: https://pkg.go.dev/github.com/jensteichert/colt
- Size: 98.6 KB
- Stars: 46
- Watchers: 4
- Forks: 6
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Colt

[](https://coveralls.io/github/jensteichert/colt?branch=main)
[](https://pkg.go.dev/github.com/jensteichert/colt)

[](https://goreportcard.com/report/github.com/jensteichert/colt)

The [MongoDB](https://www.mongodb.com) ODM for [Go](https://go.dev) i've always wanted. Inspired by [Mongoose](https://github.com/Automattic/mongoose).
Colt wraps the official [mongo-go-driver](https://github.com/mongodb/mongo-go-driver).
### Requirements
- [Go 1.18](https://tip.golang.org/doc/go1.18) or higher. Colt leverages Generics to provide type-safe methods and decoding of documents.
### Installation
To install Colt, use `go get`:
```
go get github.com/jensteichert/colt
```
### Quick Start
```golang
package main
import (
"fmt"
"github.com/jensteichert/colt"
"go.mongodb.org/mongo-driver/bson"
)
type Database struct {
Todos *colt.Collection[*Todo]
}
type Todo struct {
colt.DocWithTimestamps `bson:",inline"`
Title string `bson:"title" json:"title"`
}
func main() {
db := colt.Database{}
db.Connect("mongodb://...", "myDatabaseName")
database := Database{
Todos: colt.GetCollection[*Todo](&db, "todos"),
}
newTodo := Todo{Title: "Hello"}
todo, _ := database.Todos.Insert(&newTodo) // Will return a Todo
insertedTodo, _ := database.Todos.FindById(todo.ID)
allTodos, _ := database.Todos.Find(bson.M{"title": "Hello"})
}
```
## Features
### Hooks
#### ``BeforeInsert`` Hook
Triggers before a document will be inserted
```golang
type Todo struct {
colt.DocWithTimestamps `bson:",inline"`
}
func(t *Todo) BeforeInsert() error {
t.DocWithTimestamps.BeforeInsert()
// Do something with t here
return nil
}
```
#### ``BeforeUpdate`` Hook
Triggers before a document will be updated
```golang
func(t *Todo) BeforeUpdate() error {
t.DocWithTimestamps.BeforeUpdate()
// Do something with t here
return nil
}
```
#### ``NewID`` Hook
Can be used to generate custom ids for documents within a collection
```golang
func (t *Todo) NewID() string {
return "td_" + primitive.NewObjectID().Hex()
}
```
### ToDo
- [x] CRUD
- [x] Hooks
- [x] Disconnect
- [ ] Context
- [ ] Transactions