🏗️ update
This commit is contained in:
12
app/auth/model/mongodb/collection.go
Normal file
12
app/auth/model/mongodb/collection.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package mongodb
|
||||
|
||||
import (
|
||||
"github.com/chenmingyong0423/go-mongox/v2"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||
)
|
||||
|
||||
// MustNewCollection creates a new Collection instance with the given name.
|
||||
func MustNewCollection[T any](mongoClient *mongo.Database, collectionName string) *mongox.Collection[T] {
|
||||
collection := mongoClient.Collection(collectionName)
|
||||
return mongox.NewCollection[T](collection)
|
||||
}
|
26
app/auth/model/mongodb/mongodb.go
Normal file
26
app/auth/model/mongodb/mongodb.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package mongodb
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/readpref"
|
||||
)
|
||||
|
||||
// NewMongoDB initializes the MongoDB connection and returns the database object
|
||||
func NewMongoDB(uri string, username string, password string, authSource string, database string) *mongo.Database {
|
||||
client, err := mongo.Connect(options.Client().ApplyURI(uri).SetAuth(options.Credential{
|
||||
Username: username,
|
||||
Password: password,
|
||||
AuthSource: authSource,
|
||||
}))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
err = client.Ping(context.Background(), readpref.Primary())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
db := client.Database(database)
|
||||
return db
|
||||
}
|
Reference in New Issue
Block a user