mongrator

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 29, 2025 License: Apache-2.0 Imports: 14 Imported by: 0

README

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,omitempty"`
}
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()
}
Options

You can define default type values:

mongrator.DefaultBooleanType(true),
mongrator.DefaultIntType(1),
mongrator.DefaultInt32Type(2),
mongrator.DefaultIntType(3),
mongrator.DefaultFloat32Type(4.5),
mongrator.DefaultFloat64Type(5.5),
mongrator.DefaultStringType("completed"),
mongrator.DefaultTimeType(time.Now()),
Contributing

If you encounter any issues, feel free to open an Issue :)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config added in v1.0.8

type Config struct {
	Types map[string]any
}

type Mongrator added in v1.0.6

type Mongrator struct {
	// contains filtered or unexported fields
}

func New

func New(database *mongo.Database, opts ...Option) *Mongrator

Initialize the migrator

func (*Mongrator) RegisterSchema added in v1.0.6

func (m *Mongrator) RegisterSchema(collection string, schema any)

Registers a schema and its corresponding collection for automatic migration during application startup.

func (*Mongrator) RunMigrations added in v1.0.6

func (m *Mongrator) RunMigrations()

type Option added in v1.0.8

type Option func(*Config)

func DefaultBooleanType added in v1.0.8

func DefaultBooleanType(defaultValue bool) Option

func DefaultFloat32Type added in v1.0.8

func DefaultFloat32Type(defaultValue float32) Option

func DefaultFloat64Type added in v1.0.8

func DefaultFloat64Type(defaultValue float64) Option

func DefaultInt32Type added in v1.0.8

func DefaultInt32Type(defaultValue int32) Option

func DefaultInt64Type added in v1.0.8

func DefaultInt64Type(defaultValue int64) Option

func DefaultIntType added in v1.0.8

func DefaultIntType(defaultValue int) Option

func DefaultStringType added in v1.0.8

func DefaultStringType(defaultValue string) Option

func DefaultTimeType added in v1.0.8

func DefaultTimeType(defaultValue time.Time) Option

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL