Skip to content

Commit

Permalink
changed ID field from interface to string
Browse files Browse the repository at this point in the history
  • Loading branch information
Danushka96 committed Dec 1, 2024
1 parent 12b6c73 commit ca8e743
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 75 deletions.
54 changes: 11 additions & 43 deletions dynamo_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,21 @@ func NewDynamoRepository[T Document](client *dynamodb.Client, tableName string)
}
}

func (r *DynamoRepository[T]) FindById(id interface{}) (T, error) {
func (r *DynamoRepository[T]) FindById(id string) (T, error) {
var result T
key, err := attributevalue.Marshal(id)
if err != nil {
return result, err
}

collection := result.GetCollectionName()
collectionKey, err := attributevalue.Marshal(collection)
if err != nil {
return result, err
key := map[string]types.AttributeValue{
"_id": &types.AttributeValueMemberS{Value: id},
}

input := &dynamodb.GetItemInput{
TableName: aws.String(r.tableName),
Key: map[string]types.AttributeValue{
"collection": collectionKey,
"id": key,
},
Key: key,
}

output, err := r.client.GetItem(context.Background(), input)
if err != nil {
return result, err
}

if output.Item == nil {
return result, fmt.Errorf("item not found")
}
Expand All @@ -63,24 +52,16 @@ func (r *DynamoRepository[T]) FindById(id interface{}) (T, error) {
return result, err
}

func (r *DynamoRepository[T]) FindAllById(ids []interface{}) ([]T, error) {
func (r *DynamoRepository[T]) FindAllById(ids []string) ([]T, error) {
var results []T
var doc T
collection := doc.GetCollectionName()
collectionKey, err := attributevalue.Marshal(collection)
if err != nil {
return nil, err
if len(ids) == 0 {
return results, nil
}

keys := make([]map[string]types.AttributeValue, len(ids))
for i, id := range ids {
key, err := attributevalue.Marshal(id)
if err != nil {
return nil, err
}
keys[i] = map[string]types.AttributeValue{
"collection": collectionKey,
"id": key,
"_id": &types.AttributeValueMemberS{Value: id},
}
}

Expand Down Expand Up @@ -167,28 +148,15 @@ func (r *DynamoRepository[T]) Update(doc T) error {
return r.Save(doc)
}

func (r *DynamoRepository[T]) Delete(id interface{}) error {
var doc T
collection := doc.GetCollectionName()
collectionKey, err := attributevalue.Marshal(collection)
if err != nil {
return err
}

key, err := attributevalue.Marshal(id)
if err != nil {
return err
}

func (r *DynamoRepository[T]) Delete(id string) error {
input := &dynamodb.DeleteItemInput{
TableName: aws.String(r.tableName),
Key: map[string]types.AttributeValue{
"collection": collectionKey,
"id": key,
"_id": &types.AttributeValueMemberS{Value: id},
},
}

_, err = r.client.DeleteItem(context.Background(), input)
_, err := r.client.DeleteItem(context.Background(), input)
return err
}

Expand Down
44 changes: 40 additions & 4 deletions generic_repository.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,58 @@
package ginboot

type GenericRepository[T Document] interface {
FindById(id interface{}) (T, error)
FindAllById(ids []interface{}) ([]T, error)
// GenericRepository defines the interface for a generic repository with string IDs
type GenericRepository[T any] interface {
// FindById finds a document by its string ID
FindById(id string) (T, error)

// FindAllById finds all documents with the given string IDs
FindAllById(ids []string) ([]T, error)

// Save saves a document
Save(doc T) error

// SaveOrUpdate saves or updates a document
SaveOrUpdate(doc T) error

// SaveAll saves multiple documents
SaveAll(docs []T) error

// Update updates an existing document
Update(doc T) error
Delete(id interface{}) error

// Delete deletes a document by its string ID
Delete(id string) error

// FindOneBy finds a document by a field value
FindOneBy(field string, value interface{}) (T, error)

// FindOneByFilters finds a document by multiple filters
FindOneByFilters(filters map[string]interface{}) (T, error)

// FindBy finds documents by a field value
FindBy(field string, value interface{}) ([]T, error)

// FindByFilters finds documents by multiple filters
FindByFilters(filters map[string]interface{}) ([]T, error)

// FindAll finds all documents
FindAll(options ...interface{}) ([]T, error)

// FindAllPaginated finds all documents with pagination
FindAllPaginated(pageRequest PageRequest) (PageResponse[T], error)

// FindByPaginated finds documents by filters with pagination
FindByPaginated(pageRequest PageRequest, filters map[string]interface{}) (PageResponse[T], error)

// CountBy counts documents by a field value
CountBy(field string, value interface{}) (int64, error)

// CountByFilters counts documents by multiple filters
CountByFilters(filters map[string]interface{}) (int64, error)

// ExistsBy checks if a document exists by a field value
ExistsBy(field string, value interface{}) (bool, error)

// ExistsByFilters checks if a document exists by multiple filters
ExistsByFilters(filters map[string]interface{}) (bool, error)
}
17 changes: 8 additions & 9 deletions mongo_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func NewMongoRepository[T Document](db *mongo.Database) *MongoRepository[T] {
}
}

func (r *MongoRepository[T]) FindById(id interface{}) (T, error) {
func (r *MongoRepository[T]) FindById(id string) (T, error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

Expand All @@ -33,7 +33,7 @@ func (r *MongoRepository[T]) FindById(id interface{}) (T, error) {
return result, nil
}

func (r *MongoRepository[T]) FindAllById(ids []interface{}) ([]T, error) {
func (r *MongoRepository[T]) FindAllById(ids []string) ([]T, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
filter := bson.M{"_id": bson.M{"$in": ids}}
Expand All @@ -59,8 +59,7 @@ func (r *MongoRepository[T]) Save(doc T) error {
func (r *MongoRepository[T]) SaveOrUpdate(doc T) error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
opts := options.Replace().SetUpsert(true)
_, err := r.collection.ReplaceOne(ctx, bson.M{"_id": doc.GetID()}, doc, opts)
_, err := r.collection.ReplaceOne(ctx, bson.M{"_id": doc.GetID()}, doc, options.Replace().SetUpsert(true))
return err
}

Expand All @@ -70,12 +69,12 @@ func (r *MongoRepository[T]) SaveAll(docs []T) error {
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

var items []interface{}
var operations []mongo.WriteModel
for _, doc := range docs {
items = append(items, doc)
operation := mongo.NewReplaceOneModel().SetFilter(bson.M{"_id": doc.GetID()}).SetReplacement(doc).SetUpsert(true)
operations = append(operations, operation)
}
_, err := r.collection.InsertMany(ctx, items)
_, err := r.collection.BulkWrite(ctx, operations)
return err
}

Expand All @@ -86,7 +85,7 @@ func (r *MongoRepository[T]) Update(doc T) error {
return err
}

func (r *MongoRepository[T]) Delete(id interface{}) error {
func (r *MongoRepository[T]) Delete(id string) error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_, err := r.collection.DeleteOne(ctx, bson.M{"_id": id})
Expand Down
Loading

0 comments on commit ca8e743

Please sign in to comment.