colt

package module
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2024 License: MIT Imports: 9 Imported by: 1

README

Colt

Coverage Status Go.Dev reference CodeQL Go Report Card

The MongoDB ODM for Go i've always wanted. Inspired by Mongoose.

Colt wraps the official mongo-go-driver.

Requirements
  • Go 1.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
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

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

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

func (t *Todo) NewID() string {
    return "td_" + primitive.NewObjectID().Hex()
}
ToDo
  • CRUD
  • Hooks
  • Disconnect
  • Context
  • Transactions

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultContext

func DefaultContext() context.Context

Types

type BeforeInsertHook

type BeforeInsertHook interface {
	BeforeInsert() error
}

type BeforeUpdateHook added in v0.0.3

type BeforeUpdateHook interface {
	BeforeUpdate() error
}

type Collection

type Collection[T Document] struct {
	// contains filtered or unexported fields
}

func GetCollection

func GetCollection[T Document](db *Database, collectionName string) *Collection[T]

func (*Collection[T]) CountDocuments

func (repo *Collection[T]) CountDocuments(filter interface{}) (int64, error)

func (*Collection[T]) CreateIndex

func (repo *Collection[T]) CreateIndex(keys bson.D) error

func (*Collection[T]) DeleteById

func (repo *Collection[T]) DeleteById(id string) error

func (*Collection[T]) Find

func (repo *Collection[T]) Find(filter interface{}, opts ...*options.FindOptions) ([]T, error)

func (*Collection[T]) FindById

func (repo *Collection[T]) FindById(id interface{}) (T, error)

func (*Collection[T]) FindOne

func (repo *Collection[T]) FindOne(filter interface{}) (T, error)

func (*Collection[T]) Insert

func (repo *Collection[T]) Insert(model T) (T, error)

func (*Collection[T]) NewId

func (repo *Collection[T]) NewId() primitive.ObjectID

func (*Collection[T]) UpdateById

func (repo *Collection[T]) UpdateById(id string, model T) error

func (*Collection[T]) UpdateMany

func (repo *Collection[T]) UpdateMany(filter interface{}, doc bson.M) error

func (*Collection[T]) UpdateOne

func (repo *Collection[T]) UpdateOne(filter interface{}, model T) error

type Database

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

func (*Database) Connect

func (db *Database) Connect(connectionString string, dbName string) error

func (*Database) Disconnect added in v0.0.8

func (db *Database) Disconnect() error

type Doc

type Doc struct {
	ID string `bson:"_id,omitempty" json:"_id,omitempty"`
}

func (*Doc) GetID

func (doc *Doc) GetID() string

func (*Doc) NewID added in v0.0.9

func (doc *Doc) NewID() string

func (*Doc) SetID

func (doc *Doc) SetID(id string)

type DocWithTimestamps

type DocWithTimestamps struct {
	Doc       `bson:",inline"`
	CreatedAt time.Time  `json:"created_at" bson:"created_at"`
	UpdatedAt *time.Time `json:"updated_at,omitempty" bson:"updated_at,omitempty"`
}

func (*DocWithTimestamps) BeforeInsert

func (doc *DocWithTimestamps) BeforeInsert() error

func (*DocWithTimestamps) BeforeUpdate

func (doc *DocWithTimestamps) BeforeUpdate() error

type Document

type Document interface {
	SetID(id string)
	GetID() string

	NewID() string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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