🏗️ update

This commit is contained in:
2024-12-24 18:01:54 +08:00
parent 89d64336f5
commit e31f95b943
168 changed files with 1337 additions and 14789 deletions

View 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)
}

View 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
}