Mongrator
Mongrator is a Go package that automatically runs MongoDB schema migrations at application startup.
Prerequisites
- Use
bson
tags in your struct fields to define schema mappings.
- Schemas must be defined as Go structs (not other types).
Limitations
- Pointer Support: This package does not handle pointers in schemas. If your schema uses pointers, Mongrator might not work as expected.
Installation
To install Mongrator, run the following command:
go get github.com/RonanzinDev/mongrator
Usage
package main
import (
"context"
"time"
"github.com/RonanzinDev/mongrator"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type User struct {
Name string `json:"name" bson:"name"`
Addrees Addrees `json:"address" bson:"address"`
Contacts []Contact `json:"contacts" bson:"contacts"`
}
type Addrees struct {
Street string `json:"street" bson:"street"`
Number int `json:"number" bson:"number"`
}
type Contact struct {
Email string `json:"email" bson:"email"`
Number float64 `json:"number" bson:"number"`
}
func main() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) // Adjust the context as needed
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017"))
if err != nil {
panic(err)
}
db := client.Database("YOUR_DATABASE")
migrator := mongrator.New(db)
migrator.RegisterSchema("users", User{})
migrator.RunMigrations()
}
Configuration
You can customize Mongrator's behavior with optional configurations:
migrator := mongrator.New(db).ShouldCreateCollection().ShouldSaveMigrations()
Avaliable options
ShouldCreateCollection
: Determines whether a collection should be created when registering a schema
ShouldSaveMigrations
: // Enables saving the migration log in the database
Contributing
If you encounter any issues, feel free to open an Issue :)