Update MongoDB Driver and Configuration for Company Entities
- Updated the MongoDB driver from v1.17.4 to v2.3.0, ensuring compatibility with the latest features and improvements. - Refactored company-related entity and repository files to utilize the new driver, replacing `primitive.ObjectID` with `bson.ObjectID` for ID handling. - Adjusted the configuration in `.drone.yml` to include the `main` branch in the trigger settings, enhancing CI/CD pipeline flexibility.
This commit is contained in:
@@ -5,10 +5,10 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"go.mongodb.org/mongo-driver/mongo/readpref"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/readpref"
|
||||
)
|
||||
|
||||
// ConnectionConfig holds MongoDB connection configuration
|
||||
@@ -62,7 +62,7 @@ func NewConnectionManager(config ConnectionConfig, logger Logger) (*ConnectionMa
|
||||
ctx, cancel := context.WithTimeout(context.Background(), config.ConnectTimeout)
|
||||
defer cancel()
|
||||
|
||||
client, err := mongo.Connect(ctx, clientOptions)
|
||||
client, err := mongo.Connect(clientOptions)
|
||||
if err != nil {
|
||||
logger.Error("Failed to connect to MongoDB", map[string]interface{}{
|
||||
"uri": config.URI,
|
||||
@@ -144,7 +144,7 @@ func (cm *ConnectionManager) DropIndexes(collectionName string, indexNames []str
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
_, err := collection.Indexes().DropOne(ctx, indexName)
|
||||
err := collection.Indexes().DropOne(ctx, indexName)
|
||||
if err != nil {
|
||||
cm.logger.Error("Failed to drop index", map[string]interface{}{
|
||||
"collection": collectionName,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package mongo
|
||||
|
||||
import (
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
)
|
||||
|
||||
// Logger defines the interface for logging operations
|
||||
@@ -32,7 +32,7 @@ type IDSetter interface {
|
||||
|
||||
// Model provides a common base for MongoDB documents
|
||||
type Model struct {
|
||||
ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
|
||||
ID bson.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
|
||||
CreatedAt int64 `bson:"created_at" json:"created_at"`
|
||||
UpdatedAt int64 `bson:"updated_at" json:"updated_at"`
|
||||
}
|
||||
@@ -44,7 +44,7 @@ func (b *Model) GetID() string {
|
||||
|
||||
// SetID sets the document ID
|
||||
func (b *Model) SetID(id string) {
|
||||
b.ID, _ = primitive.ObjectIDFromHex(id)
|
||||
b.ID, _ = bson.ObjectIDFromHex(id)
|
||||
}
|
||||
|
||||
// SetCreatedAt sets the creation timestamp
|
||||
|
||||
+10
-11
@@ -7,10 +7,9 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||
)
|
||||
|
||||
// Common errors
|
||||
@@ -83,7 +82,7 @@ func NewRepository[T any](collection *mongo.Collection, logger Logger) Repositor
|
||||
|
||||
// FindByID retrieves a document by its ID
|
||||
func (r *repository[T]) FindByID(ctx context.Context, id string) (*T, error) {
|
||||
objectID, err := primitive.ObjectIDFromHex(id)
|
||||
objectID, err := bson.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
r.logger.Error("Invalid ObjectID format", map[string]interface{}{
|
||||
"id": id,
|
||||
@@ -228,7 +227,7 @@ func (r *repository[T]) Create(ctx context.Context, model *T) error {
|
||||
|
||||
// Set the ID if the model implements IDSetter
|
||||
if idSetter, ok := any(model).(IDSetter); ok {
|
||||
if oid, ok := result.InsertedID.(primitive.ObjectID); ok {
|
||||
if oid, ok := result.InsertedID.(bson.ObjectID); ok {
|
||||
idSetter.SetID(oid.Hex())
|
||||
}
|
||||
}
|
||||
@@ -253,7 +252,7 @@ func (r *repository[T]) Update(ctx context.Context, model *T) error {
|
||||
return err
|
||||
}
|
||||
|
||||
objectID, err := primitive.ObjectIDFromHex(id)
|
||||
objectID, err := bson.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid id format: %w", err)
|
||||
}
|
||||
@@ -283,7 +282,7 @@ func (r *repository[T]) Update(ctx context.Context, model *T) error {
|
||||
|
||||
// Delete removes a document by ID
|
||||
func (r *repository[T]) Delete(ctx context.Context, id string) error {
|
||||
objectID, err := primitive.ObjectIDFromHex(id)
|
||||
objectID, err := bson.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid id format: %w", err)
|
||||
}
|
||||
@@ -422,7 +421,7 @@ func (r *repository[T]) buildCursorFilter(cursor, sortField string, sortOrder in
|
||||
|
||||
// Try to parse as ObjectID first
|
||||
if sortField == "_id" {
|
||||
if objectID, err := primitive.ObjectIDFromHex(string(cursorBytes)); err == nil {
|
||||
if objectID, err := bson.ObjectIDFromHex(string(cursorBytes)); err == nil {
|
||||
cursorValue = objectID
|
||||
} else {
|
||||
return nil, fmt.Errorf("%w: invalid ObjectID in cursor", ErrInvalidCursor)
|
||||
@@ -466,7 +465,7 @@ func (r *repository[T]) generateCursor(doc T, sortField string) (string, error)
|
||||
// Convert to string for encoding
|
||||
var cursorStr string
|
||||
switch v := sortValue.(type) {
|
||||
case primitive.ObjectID:
|
||||
case bson.ObjectID:
|
||||
cursorStr = v.Hex()
|
||||
case string:
|
||||
cursorStr = v
|
||||
@@ -500,7 +499,7 @@ func (r *repository[T]) getModelID(model *T) (string, error) {
|
||||
}
|
||||
|
||||
if id, exists := docMap["_id"]; exists {
|
||||
if oid, ok := id.(primitive.ObjectID); ok {
|
||||
if oid, ok := id.(bson.ObjectID); ok {
|
||||
return oid.Hex(), nil
|
||||
}
|
||||
}
|
||||
|
||||
+3
-8
@@ -1,15 +1,15 @@
|
||||
package mongo
|
||||
|
||||
import (
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||
)
|
||||
|
||||
// Index represents a MongoDB index
|
||||
type Index struct {
|
||||
Name string `json:"name"`
|
||||
Keys bson.D `json:"keys"`
|
||||
Options *options.IndexOptions `json:"options"`
|
||||
Options *options.IndexOptionsBuilder `json:"options"`
|
||||
}
|
||||
|
||||
// NewIndex creates a new index with default options
|
||||
@@ -33,11 +33,6 @@ func (i *Index) WithSparse(sparse bool) *Index {
|
||||
return i
|
||||
}
|
||||
|
||||
// WithBackground sets the background option for the index
|
||||
func (i *Index) WithBackground(background bool) *Index {
|
||||
i.Options.SetBackground(background)
|
||||
return i
|
||||
}
|
||||
|
||||
// WithExpireAfterSeconds sets the TTL for the index
|
||||
func (i *Index) WithExpireAfterSeconds(seconds int32) *Index {
|
||||
|
||||
Reference in New Issue
Block a user