Skip to content

Commit

Permalink
Merge pull request #15 from jensteichert/f/custom-ids
Browse files Browse the repository at this point in the history
support custom doc ids
  • Loading branch information
jensteichert committed Jul 8, 2023
2 parents e07078c + ff14be2 commit c692826
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 5 deletions.
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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`:
```
Expand Down Expand Up @@ -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


2 changes: 1 addition & 1 deletion collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 10 additions & 1 deletion document.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
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)
}

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
}
Expand Down
26 changes: 26 additions & 0 deletions document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package colt

import (
"github.com/stretchr/testify/assert"
"go.mongodb.org/mongo-driver/bson/primitive"
"strings"
"testing"
)

Expand All @@ -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_"))
}

0 comments on commit c692826

Please sign in to comment.