Skip to content

Commit 3b8ac1d

Browse files
committed
feat: Added MongoDB and Redis connection handlers and helper functions
* Added MongoDB and Redis connection handlers * Added MongoDB helper functions to create FindOne, Find and Update options, to create Collections and their Indexes (SingleFieldIndexes, CompoundIndexes, TTL Indexes) * Forked from 'github.com/pixel-plaza-dev/uru-databases-2-go-service-common'
1 parent f798784 commit 3b8ac1d

File tree

17 files changed

+673
-0
lines changed

17 files changed

+673
-0
lines changed

.gitignore

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# IDE folders
2+
/.idea
3+
/.vscode
4+
5+
# Environment variables
6+
/**/*.env
7+
8+
# Executable files
9+
/**/*.exe
10+
11+
# Private and public keys
12+
/**/*.ed
13+
/**/*.pub
14+
/**/*.pem
15+
16+
# Commands
17+
/**/*.cmd
18+
/**/*.sh
19+
/**/*.bat

errors.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package go_databases
2+
3+
import "errors"
4+
5+
var (
6+
AlreadyConnectedError = errors.New("connection to database already established")
7+
FailedToConnectError = errors.New("failed to connect to database")
8+
FailedToPingError = errors.New("failed to ping database")
9+
NotConnectedError = errors.New("connection to database not established")
10+
FailedToDisconnectError = errors.New("failed to disconnect from database")
11+
InternalServerError = errors.New("internal server error")
12+
)

go.mod

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module github.com/ralvarezdev/go-databases
2+
3+
go 1.23.4
4+
5+
require (
6+
github.com/go-redis/redis/v8 v8.11.5
7+
github.com/ralvarezdev/go-logger v0.1.0
8+
go.mongodb.org/mongo-driver v1.17.1
9+
golang.org/x/net v0.33.0
10+
)
11+
12+
require (
13+
github.com/cespare/xxhash/v2 v2.3.0 // indirect
14+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
15+
github.com/golang/snappy v0.0.4 // indirect
16+
github.com/klauspost/compress v1.13.6 // indirect
17+
github.com/montanaflynn/stats v0.7.1 // indirect
18+
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
19+
github.com/xdg-go/scram v1.1.2 // indirect
20+
github.com/xdg-go/stringprep v1.0.4 // indirect
21+
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect
22+
golang.org/x/crypto v0.31.0 // indirect
23+
golang.org/x/sync v0.10.0 // indirect
24+
golang.org/x/text v0.21.0 // indirect
25+
)

go.sum

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
2+
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
3+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
4+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
6+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
7+
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
8+
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
9+
github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI=
10+
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
11+
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
12+
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
13+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
14+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
15+
github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc=
16+
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
17+
github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8eaE=
18+
github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
19+
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
20+
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
21+
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
22+
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
23+
github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=
24+
github.com/onsi/gomega v1.18.1/go.mod h1:0q+aL8jAiMXy9hbwj2mr5GziHiwhAIQpFmmtT5hitRs=
25+
github.com/ralvarezdev/go-logger v0.1.0 h1:i2AI1nlxU6Hizvk75Vc8wtFydiVrqIeeRbJwiuO/69A=
26+
github.com/ralvarezdev/go-logger v0.1.0/go.mod h1:v5OvFrkS+wsYNTCVegXWiRhBtcYrQJr4LDMDntvpAos=
27+
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
28+
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
29+
github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY=
30+
github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4=
31+
github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6c8=
32+
github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM=
33+
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 h1:ilQV1hzziu+LLM3zUTJ0trRztfwgjqKnBWNtSRkbmwM=
34+
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78/go.mod h1:aL8wCCfTfSfmXjznFBSZNN13rSJjlIOI1fUNAtF7rmI=
35+
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
36+
go.mongodb.org/mongo-driver v1.17.1 h1:Wic5cJIwJgSpBhe3lx3+/RybR5PiYRMpVFgO7cOHyIM=
37+
go.mongodb.org/mongo-driver v1.17.1/go.mod h1:wwWm/+BuOddhcq3n68LKRmgk2wXzmF6s0SFOa0GINL4=
38+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
39+
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
40+
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
41+
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
42+
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
43+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
44+
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
45+
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
46+
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
47+
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
48+
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
49+
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
50+
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
51+
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
52+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
53+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
54+
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
55+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
56+
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
57+
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
58+
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
59+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
60+
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
61+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
62+
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
63+
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
64+
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
65+
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
66+
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
67+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
68+
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
69+
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
70+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
71+
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
72+
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
73+
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
74+
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

logger.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package go_databases
2+
3+
import (
4+
gologger "github.com/ralvarezdev/go-logger"
5+
gologgerstatus "github.com/ralvarezdev/go-logger/status"
6+
)
7+
8+
// Logger is the logger for the database connection
9+
type Logger struct {
10+
logger gologger.Logger
11+
}
12+
13+
// NewLogger is the logger for the database connection
14+
func NewLogger(logger gologger.Logger) (*Logger, error) {
15+
// Check if the logger is nil
16+
if logger == nil {
17+
return nil, gologger.NilLoggerError
18+
}
19+
20+
return &Logger{logger: logger}, nil
21+
}
22+
23+
// ConnectedToDatabase logs a success message when the server connects to the database
24+
func (l *Logger) ConnectedToDatabase() {
25+
l.logger.LogMessage(gologger.NewLogMessage("connected to database", gologgerstatus.StatusDebug, nil))
26+
}
27+
28+
// DisconnectedFromDatabase logs a success message when the server disconnects from the database
29+
func (l *Logger) DisconnectedFromDatabase() {
30+
l.logger.LogMessage(gologger.NewLogMessage("disconnected from database", gologgerstatus.StatusDebug, nil))
31+
}

mongodb/collection.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package mongodb
2+
3+
import (
4+
"fmt"
5+
"go.mongodb.org/mongo-driver/mongo"
6+
"golang.org/x/net/context"
7+
)
8+
9+
// Collection represents a MongoDB collection
10+
type Collection struct {
11+
Name string
12+
Indexes *[]*mongo.IndexModel
13+
}
14+
15+
// NewCollection creates a new MongoDB collection
16+
func NewCollection(
17+
name string,
18+
indexes *[]*mongo.IndexModel,
19+
) *Collection {
20+
return &Collection{
21+
Name: name,
22+
Indexes: indexes,
23+
}
24+
}
25+
26+
// CreateCollection creates the collection
27+
func (c *Collection) CreateCollection(database *mongo.Database) (
28+
collection *mongo.Collection, err error,
29+
) {
30+
// Get the collection
31+
collection = database.Collection(c.Name)
32+
33+
// Create the indexes
34+
if err = c.createIndexes(collection); err != nil {
35+
return nil, err
36+
}
37+
38+
return collection, nil
39+
}
40+
41+
// createIndexes creates the indexes for the collection
42+
func (c *Collection) createIndexes(collection *mongo.Collection) (err error) {
43+
if c.Indexes != nil {
44+
for _, index := range *c.Indexes {
45+
// Check if the index is nil
46+
if index == nil {
47+
continue
48+
}
49+
50+
// Create the index
51+
_, err = collection.Indexes().CreateOne(
52+
context.Background(), *index,
53+
)
54+
if err != nil {
55+
return fmt.Errorf(FailedToCreateIndexError, *index)
56+
}
57+
}
58+
}
59+
return nil
60+
}

mongodb/connection.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package mongodb
2+
3+
import (
4+
godatabases "github.com/ralvarezdev/go-databases"
5+
"go.mongodb.org/mongo-driver/mongo"
6+
"go.mongodb.org/mongo-driver/mongo/options"
7+
"golang.org/x/net/context"
8+
"time"
9+
)
10+
11+
type (
12+
// ConnectionHandler interface
13+
ConnectionHandler interface {
14+
Connect() (*mongo.Client, error)
15+
GetClient() (*mongo.Client, error)
16+
Disconnect()
17+
}
18+
19+
// Config struct
20+
Config struct {
21+
Uri string
22+
Timeout time.Duration
23+
}
24+
25+
// DefaultConnectionHandler struct
26+
DefaultConnectionHandler struct {
27+
Ctx context.Context
28+
Cancel context.CancelFunc
29+
ClientOptions *options.ClientOptions
30+
Client *mongo.Client
31+
}
32+
)
33+
34+
// NewDefaultConnectionHandler creates a new connection
35+
func NewDefaultConnectionHandler(config *Config) (*DefaultConnectionHandler, error) {
36+
// Check if the config is nil
37+
if config == nil {
38+
return nil, NilClientError
39+
}
40+
41+
// Set client options
42+
ctx, cancel := context.WithTimeout(context.Background(), config.Timeout)
43+
clientOptions := options.Client().ApplyURI(config.Uri)
44+
45+
return &DefaultConnectionHandler{
46+
Cancel: cancel,
47+
Ctx: ctx,
48+
ClientOptions: clientOptions,
49+
Client: nil,
50+
}, nil
51+
}
52+
53+
// Connect returns a new MongoDB client
54+
func (d *DefaultConnectionHandler) Connect() (*mongo.Client, error) {
55+
// Check if the connection is already established
56+
if d.Client != nil {
57+
return d.Client, godatabases.AlreadyConnectedError
58+
}
59+
60+
// Connect to MongoDB
61+
client, err := mongo.Connect(d.Ctx, d.ClientOptions)
62+
63+
// Create MongoDB Connection struct
64+
if err != nil {
65+
return nil, godatabases.FailedToConnectError
66+
}
67+
68+
// Check the connection
69+
err = client.Ping(context.Background(), nil)
70+
if err != nil {
71+
return nil, godatabases.FailedToPingError
72+
}
73+
74+
// Set client
75+
d.Client = client
76+
77+
return client, nil
78+
}
79+
80+
// GetClient returns the MongoDB client
81+
func (d *DefaultConnectionHandler) GetClient() (*mongo.Client, error) {
82+
// Check if the connection is established
83+
if d.Client == nil {
84+
return nil, godatabases.NotConnectedError
85+
}
86+
87+
return d.Client, nil
88+
}
89+
90+
// Disconnect closes the MongoDB client connection
91+
func (d *DefaultConnectionHandler) Disconnect() {
92+
defer func() {
93+
// Check if the connection is established
94+
if d.Client == nil {
95+
return
96+
}
97+
98+
// Close the connection
99+
d.Cancel()
100+
if err := d.Client.Disconnect(d.Ctx); err != nil {
101+
panic(godatabases.FailedToDisconnectError)
102+
}
103+
}()
104+
}

mongodb/errors.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package mongodb
2+
3+
import "errors"
4+
5+
var (
6+
FailedToCreateDocumentError = errors.New("failed to create document")
7+
FailedToStartSessionError = errors.New("failed to start session")
8+
FailedToCreateIndexError = "failed to create index: %v"
9+
NilConfigError = errors.New("mongodb connection config cannot be nil")
10+
NilClientError = errors.New("mongodb client cannot be nil")
11+
)

mongodb/index.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package mongodb
2+
3+
import (
4+
"go.mongodb.org/mongo-driver/bson"
5+
"go.mongodb.org/mongo-driver/mongo"
6+
"go.mongodb.org/mongo-driver/mongo/options"
7+
)
8+
9+
type FieldIndex struct {
10+
Name string
11+
Order Order
12+
}
13+
14+
// NewFieldIndex creates a new field index
15+
func NewFieldIndex(name string, order Order) *FieldIndex {
16+
return &FieldIndex{Name: name, Order: order}
17+
}
18+
19+
// NewUniqueIndex creates a new unique field index model
20+
func NewUniqueIndex(fieldIndex FieldIndex, unique bool) *mongo.IndexModel {
21+
return &mongo.IndexModel{
22+
Keys: bson.D{{fieldIndex.Name, fieldIndex.Order.OrderInt()}},
23+
Options: options.Index().SetUnique(unique),
24+
}
25+
}
26+
27+
// NewTTLIndex creates a new TTL index model
28+
func NewTTLIndex(fieldName string, expireAfterSeconds int32) *mongo.IndexModel {
29+
return &mongo.IndexModel{
30+
Keys: bson.D{{fieldName, 1}},
31+
Options: options.Index().SetExpireAfterSeconds(expireAfterSeconds),
32+
}
33+
}
34+
35+
// NewCompoundFieldIndex creates a new compound field index model
36+
func NewCompoundFieldIndex(
37+
fieldIndexes []*FieldIndex, unique bool,
38+
) *mongo.IndexModel {
39+
// Create the keys
40+
keys := bson.D{}
41+
for _, fieldIndex := range fieldIndexes {
42+
keys = append(
43+
keys,
44+
bson.E{Key: fieldIndex.Name, Value: fieldIndex.Order.OrderInt()},
45+
)
46+
}
47+
48+
// Create the index model
49+
return &mongo.IndexModel{
50+
Keys: keys,
51+
Options: options.Index().SetUnique(unique),
52+
}
53+
}

0 commit comments

Comments
 (0)