diff --git a/README.md b/README.md index 5fd6d57..8ffd4c0 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # Colt -The mongodb ODM for Go i've always wanted +The [MongoDB](https://www.mongodb.com) ODM for [Go](https://go.dev) i've always wanted. ![Build & Tests](https://github.com/jensteichert/webvitals_exporter/workflows/Build/badge.svg) ![CodeQL](https://github.com/jensteichert/colt/workflows/CodeQL/badge.svg) @@ -8,7 +8,11 @@ The mongodb ODM for Go i've always wanted [![Go Report Card](https://goreportcard.com/badge/github.com/jensteichert/colt)](https://goreportcard.com/report/github.com/jensteichert/colt) [![Coverage Status](https://coveralls.io/repos/github/jensteichert/colt/badge.svg?branch=main)](https://coveralls.io/github/jensteichert/colt?branch=main) -Colt leverages Generics to provide type-safe methods and decoding of documents. It therefor requires [Go 1.18+](https://tip.golang.org/doc/go1.18). +Colt is a wrapper around 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`: ``` @@ -81,13 +85,22 @@ func(t *Todo) BeforeUpdate() error { } ``` +#### ``NewID`` Hook +Can be used to generate custom ids for documents within a collection +```golang +func(t *Todo) BeforeUpdate() error { + t.DocWithTimestamps.BeforeUpdate() + + // Do something with t here + return nil +} +``` ### ToDo - [x] CRUD - [x] Hooks - [x] Disconnect - [ ] Context -- [ ] Aggregations - [ ] Transactions diff --git a/collection.go b/collection.go index 34fd0f4..b28c22f 100644 --- a/collection.go +++ b/collection.go @@ -14,7 +14,7 @@ type Collection[T Document] struct { func (repo *Collection[T]) Insert(model T) (T, error) { if model.GetID() == "" { - model.SetID(repo.NewId().Hex()) + model.SetID(model.NewID()) } if hook, ok := any(model).(BeforeInsertHook); ok { diff --git a/document.go b/document.go index cf0cad9..16da591 100644 --- a/document.go +++ b/document.go @@ -1,10 +1,15 @@ package colt -import "time" +import ( + "go.mongodb.org/mongo-driver/bson/primitive" + "time" +) type Document interface { SetID(id string) GetID() string + + NewID() string //CastID(id interface{}) (interface{}, error) } @@ -12,6 +17,10 @@ type Doc struct { ID string `bson:"_id,omitempty" json:"_id,omitempty"` } +func (doc *Doc) NewID() string { + return primitive.NewObjectID().Hex() +} + func (doc *Doc) SetID(id string) { doc.ID = id } diff --git a/document_test.go b/document_test.go index 0b549ec..f8fe0d4 100644 --- a/document_test.go +++ b/document_test.go @@ -2,6 +2,8 @@ package colt import ( "github.com/stretchr/testify/assert" + "go.mongodb.org/mongo-driver/bson/primitive" + "strings" "testing" ) @@ -24,4 +26,28 @@ func TestCDocument_GetID(t *testing.T) { doc.SetID("638cda03871d719a9020c855") assert.Equal(t, doc.ID, "638cda03871d719a9020c855") +} + +func TestCDocument_NewID(t *testing.T) { + doc := todoWithCustomId{} + assert.Empty(t, doc.GetID()) + + assert.NotEmpty(t, doc.NewID()) +} + +type todoWithCustomId struct { + Doc `bson:",inline"` + Title string `bson:"title" json:"title"` +} + +func (t *todoWithCustomId) NewID() string { + return "td_" + primitive.NewObjectID().Hex() +} + +func TestCDocument_NewID_Custom(t *testing.T) { + doc := todoWithCustomId{} + assert.Empty(t, doc.GetID()) + + assert.NotEmpty(t, doc.NewID()) + assert.True(t, strings.HasPrefix(doc.NewID(), "td_")) } \ No newline at end of file