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:
n.nakhostin
2025-09-09 18:53:39 +03:30
parent 57c29dc58f
commit 5d721705b7
24 changed files with 64 additions and 130 deletions
+10 -11
View File
@@ -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
}
}