Skip to content

Commit 0eaeb0f

Browse files
author
Amit
committed
added create index and created unique index functions to create indexes in mongodb, modified query for getting data from db
1 parent e58efd8 commit 0eaeb0f

File tree

6 files changed

+50
-17
lines changed

6 files changed

+50
-17
lines changed

mongodb_client/mongodb-client.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,36 @@ func CreateCollections(ctx context.Context, dbName string, collectionName string
7474
log.Printf("✅ Collection already present: %s", collectionName)
7575
}
7676
}
77+
78+
func CreateIndex(dbName string, collectionName string, indexModel mongo.IndexModel) {
79+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
80+
defer cancel()
81+
82+
_, err := GetDb(dbName).Collection(collectionName).Indexes().CreateOne(ctx, indexModel)
83+
84+
if err != nil {
85+
log.Fatalf("❌ Failed to create index for collection: %s in db: %s", collectionName, dbName)
86+
log.Fatalf("❌ Error while creating creating index: %v", err)
87+
} else {
88+
log.Println("✅ Index Created Successfully")
89+
}
90+
}
91+
92+
func CreateUniqueIndex(dbName string, collectionName string, field string) {
93+
indexModel := mongo.IndexModel{
94+
Keys: bson.M{field: 1},
95+
Options: options.Index().SetUnique(true),
96+
}
97+
98+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
99+
defer cancel()
100+
101+
_, err := GetDb(dbName).Collection(collectionName).Indexes().CreateOne(ctx, indexModel)
102+
103+
if err != nil {
104+
log.Fatalf("❌ Failed to create index for collection: %s in db: %s", collectionName, dbName)
105+
log.Fatalf("❌ Error while creating creating index: %v", err)
106+
} else {
107+
log.Println("✅ Index Created Successfully")
108+
}
109+
}

mongodb_domain/base.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package mongodb_domain
22

33
type BaseDmlModel interface {
4+
GetId() string
5+
SetId(id string)
46
SetCreatedBy(by string)
57
SetCreatedOn(on string)
68
SetModifiedBy(by string)
79
SetModifiedOn(on string)
8-
GetId() string
9-
SetId(id string)
1010
}
1111

1212
type DmlModel struct {
@@ -18,9 +18,9 @@ type DmlModel struct {
1818
}
1919

2020
// Implement Auditable interface
21+
func (d *DmlModel) GetId() string { return d.Id }
22+
func (d *DmlModel) SetId(id string) { d.Id = id }
2123
func (d *DmlModel) SetCreatedBy(by string) { d.CreatedBy = by }
2224
func (d *DmlModel) SetCreatedOn(on string) { d.CreatedOn = on }
2325
func (d *DmlModel) SetModifiedBy(by string) { d.ModifiedBy = by }
2426
func (d *DmlModel) SetModifiedOn(on string) { d.ModifiedOn = on }
25-
func (d *DmlModel) GetId() string { return d.Id }
26-
func (d *DmlModel) SetId(id string) { d.Id = id }

mongodb_repositories/cmd/create.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
type ICreateRepository[T mongodb_domain.BaseDmlModel] interface {
14-
Create(data T, dbName string, collectionName string, username string) (*T, error)
14+
Create(data T, dbName string, collectionName string, createdBy string) (*T, error)
1515
}
1616

1717
type CreateRepositoryImpl[T mongodb_domain.BaseDmlModel] struct{}
@@ -21,16 +21,16 @@ func CreateRepository[T mongodb_domain.BaseDmlModel]() ICreateRepository[T] {
2121
}
2222

2323
// Create implements ICreateRepository.
24-
func (g *CreateRepositoryImpl[T]) Create(data T, dbName string, collectionName string, username string) (*T, error) {
24+
func (g *CreateRepositoryImpl[T]) Create(data T, dbName string, collectionName string, createdBy string) (*T, error) {
2525
bgCtx := context.Background()
2626
ctx, cancel := context.WithTimeout(bgCtx, 5*time.Second)
2727
defer cancel()
2828

2929
now := time.Now().UTC().Format(time.RFC3339)
3030

31-
data.SetCreatedBy(username)
31+
data.SetCreatedBy(createdBy)
3232
data.SetCreatedOn(now)
33-
data.SetModifiedBy(username)
33+
data.SetModifiedBy(createdBy)
3434
data.SetModifiedOn(now)
3535

3636
collection := mongodb_client.GetDb(dbName).Collection(collectionName)

mongodb_repositories/cmd/update.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
)
1515

1616
type IUpdateRepository[T mongodb_domain.BaseDmlModel] interface {
17-
Update(data T, dbName string, collectionName string, username string) (*T, error)
17+
Update(data T, dbName string, collectionName string, createdBy string) (*T, error)
1818
}
1919

2020
type UpdateRepositoryImpl[T mongodb_domain.BaseDmlModel] struct{}
@@ -24,14 +24,14 @@ func UpdateRepository[T mongodb_domain.BaseDmlModel]() IUpdateRepository[T] {
2424
}
2525

2626
// Update implements IUpdateRepository.
27-
func (g *UpdateRepositoryImpl[T]) Update(data T, dbName string, collectionName string, username string) (*T, error) {
27+
func (g *UpdateRepositoryImpl[T]) Update(data T, dbName string, collectionName string, createdBy string) (*T, error) {
2828
var updatedDoc T
2929

3030
bgCtx := context.Background()
3131
ctx, cancel := context.WithTimeout(bgCtx, 5*time.Second)
3232
defer cancel()
3333

34-
data.SetModifiedBy(username)
34+
data.SetModifiedBy(createdBy)
3535
data.SetModifiedOn(time.Now().UTC().Format(time.RFC3339))
3636

3737
objectId, err := primitive.ObjectIDFromHex(data.GetId())

mongodb_repositories/query/get-all.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
)
1414

1515
type IGetAllRepository[T mongodb_domain.BaseDmlModel] interface {
16-
GetAll(dbName string, collectionName string) ([]T, error)
16+
GetAll(dbName string, collectionName string, createdBy string) ([]T, error)
1717
}
1818

1919
type GetAllRepositoryImpl[T mongodb_domain.BaseDmlModel] struct{}
@@ -22,14 +22,14 @@ func GetAllRepository[T mongodb_domain.BaseDmlModel]() IGetAllRepository[T] {
2222
return &GetAllRepositoryImpl[T]{}
2323
}
2424

25-
func (g *GetAllRepositoryImpl[T]) GetAll(dbName string, collectionName string) ([]T, error) {
25+
func (g *GetAllRepositoryImpl[T]) GetAll(dbName string, collectionName string, createdBy string) ([]T, error) {
2626
var results []T = make([]T, 0)
2727

2828
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
2929
defer cancel()
3030

3131
collection := mongodb_client.GetDb(dbName).Collection(collectionName)
32-
cursor, err := collection.Find(ctx, bson.M{})
32+
cursor, err := collection.Find(ctx, bson.M{"createdBy": createdBy})
3333

3434
if err != nil {
3535
log.Printf("❌ [GetAll] error while getting data: %v", err)

mongodb_repositories/query/get-by-id.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
)
1515

1616
type IGetByIdRepository[T mongodb_domain.BaseDmlModel] interface {
17-
GetById(id string, dbName string, collectionName string) (*T, error)
17+
GetById(id string, dbName string, collectionName string, createdBy string) (*T, error)
1818
}
1919

2020
type GetByIdRepositoryImpl[T mongodb_domain.BaseDmlModel] struct{}
@@ -24,7 +24,7 @@ func GetByIdRepository[T mongodb_domain.BaseDmlModel]() IGetByIdRepository[T] {
2424
}
2525

2626
// GetById implements GetByIdRepositoryImpl.
27-
func (g *GetByIdRepositoryImpl[T]) GetById(id string, dbName string, collectionName string) (*T, error) {
27+
func (g *GetByIdRepositoryImpl[T]) GetById(id string, dbName string, collectionName string, createdBy string) (*T, error) {
2828
bgCtx := context.Background()
2929
ctx, cancel := context.WithTimeout(bgCtx, 5*time.Second)
3030
defer cancel()
@@ -37,7 +37,7 @@ func (g *GetByIdRepositoryImpl[T]) GetById(id string, dbName string, collectionN
3737
return nil, err
3838
}
3939

40-
filter := bson.M{"_id": objectId}
40+
filter := bson.M{"_id": objectId, "createdBy": createdBy}
4141
collection := mongodb_client.GetClient().Database(dbName).Collection(collectionName)
4242

4343
var result T

0 commit comments

Comments
 (0)