Skip to content

Commit

Permalink
add method to return collection name and refactor database validator
Browse files Browse the repository at this point in the history
  • Loading branch information
victorguarana committed Apr 30, 2024
1 parent ad23bb2 commit 9ac0a7a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
9 changes: 8 additions & 1 deletion gomongo/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,16 @@ type Collection[T any] interface {
ListIndexes(ctx context.Context) ([]Index, error)

Drop(ctx context.Context) error

Name() string
}

type collection[T any] struct {
mongoCollection *mongo.Collection
}

func NewCollection[T any](database *Database, collectionName string) (Collection[T], error) {
if database == nil || database.mongoDatabase == nil {
if err := validateDatabase(database); err != nil {
return nil, ErrConnectionNotInitialized
}

Expand Down Expand Up @@ -196,6 +198,11 @@ func (c *collection[T]) Drop(ctx context.Context) error {
return drop(ctx, c.mongoCollection)
}

// Name returns the name of a collection
func (c *collection[T]) Name() string {
return c.mongoCollection.Name()
}

func validateReceivedID(id ID) error {
if id == nil {
return ErrEmptyID
Expand Down
8 changes: 8 additions & 0 deletions gomongo/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,11 @@ func pingMongoServer(cs *ConnectionSettings, mongoClient *mongo.Client, ctx cont

return mongoClient.Ping(ctx, nil)
}

func validateDatabase(database *Database) error {
if database == nil || database.mongoDatabase == nil {
return ErrConnectionNotInitialized
}

return nil
}

0 comments on commit 9ac0a7a

Please sign in to comment.