-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepository.go
377 lines (339 loc) · 15.6 KB
/
repository.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package mongorepository
import (
"context"
"errors"
"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"
)
// Repository is an interface that defines the methods for interacting with a MongoDB collection.
type Repository[T any] interface {
// CreateIndex creates an index in the MongoDB collection based on the specified key and options.
// It takes a context.Context as the first argument, the key for the index as the second argument,
// and optional IndexOption(s) as the third argument(s).
// The function returns an error if the index creation fails.
CreateIndex(ctx context.Context, key interface{}, opts ...IndexOption) error
// Create inserts a new document into the MongoDB collection.
// It takes a context.Context and a model of type T as input parameters.
// It returns the ID of the newly created document as a string and an error, if any.
Create(ctx context.Context, model T) (string, error)
// FindByID retrieves a document from the MongoDB collection by its ID.
// It takes a context.Context and the ID of the document as parameters.
// It returns the retrieved document of type T and an error, if any.
FindByID(ctx context.Context, id string) (T, error)
// FindByIDs retrieves multiple documents from the MongoDB collection by their IDs.
// It takes a context.Context and a slice of IDs as parameters.
// It returns a slice of documents of type T and an error, if any.
FindByIDs(ctx context.Context, ids ...string) ([]T, error)
// Update updates a document in the MongoDB collection with the specified ID.
// It takes a context, ID string, and model as input parameters.
// It returns the number of modified documents and an error, if any.
Update(ctx context.Context, id string, model T) (int64, error)
// UpdateMany updates multiple documents in the MongoDB collection based on the provided filters.
// It takes a context.Context, a map of update fields, and optional filter functions as parameters.
// The update fields specify the changes to be made to the documents.
// The filter functions are used to build the filter for selecting the documents to be updated.
// It returns the number of documents modified and an error if any.
UpdateMany(ctx context.Context, update map[string]interface{}, filters ...FilterFunc) (int64, error)
// Delete deletes a document from the MongoDB collection based on the provided ID.
// It returns the number of deleted documents and an error, if any.
Delete(ctx context.Context, id string) (int64, error)
// DeleteMany deletes multiple documents from the MongoDB collection based on the provided filters.
// It returns the number of deleted documents and an error, if any.
DeleteMany(ctx context.Context, filters ...FilterFunc) (int64, error)
// FindManyByFilter retrieves multiple documents from the collection based on the provided filters.
// It allows skipping a certain number of documents and limiting the number of documents to be returned.
// The filters are applied in the order they are passed.
// If no documents match the filters, it returns an error with the ErrNotFound error code.
// If an error occurs during the retrieval process, it returns an error with the ErrFailedToFindManyByFilter error code.
// The function returns a slice of documents of type T and an error.
FindManyByFilter(ctx context.Context, skip int64, limit int64, filters ...FilterFunc) ([]T, error)
// FindOneByFilter finds a single document in the collection based on the provided filters.
// It accepts one or more FilterFunc functions that modify the filter criteria.
// The function returns the found document of type T and an error, if any.
// If no document is found, it returns an error of type ErrNotFound.
// If an error occurs during the find operation, it returns the error.
FindOneByFilter(ctx context.Context, filters ...FilterFunc) (T, error)
// Exists checks if a document exists in the collection based on the provided filters.
// It accepts one or more FilterFunc functions that modify the filter criteria.
// The function returns true if a document exists and false otherwise.
// If an error occurs during the find operation, it returns the error.
Exists(ctx context.Context, filters ...FilterFunc) (bool, error)
// Count returns the number of documents in the collection based on the provided filters.
// It accepts one or more FilterFunc functions that modify the filter criteria.
// The function returns the number of documents and an error, if any.
Count(ctx context.Context, filters ...FilterFunc) (int64, error)
}
// mongoRepository is a generic struct that represents a MongoDB repository.
// It holds a reference to a mongo.Collection, which is used to interact with the MongoDB database.
type mongoRepository[T any] struct {
collection *mongo.Collection
}
// NewMongoRepository creates a new instance of the mongoRepository[T] struct.
// It takes a mongo.Database and a collectionName as parameters and returns a pointer to the mongoRepository[T] struct.
// The mongoRepository[T] struct represents a repository for working with a specific MongoDB collection.
// The collection field of the struct is initialized with the specified collectionName from the provided database.
func NewMongoRepository[T any](db *mongo.Database, collectionName string) *mongoRepository[T] {
return &mongoRepository[T]{collection: db.Collection(collectionName)}
}
// CreateIndex creates an index in the MongoDB collection based on the specified key and options.
// It takes a context.Context as the first argument, the key for the index as the second argument,
// and optional IndexOption(s) as the third argument(s).
// The function returns an error if the index creation fails.
func (r *mongoRepository[T]) CreateIndex(ctx context.Context, key string, opts ...IndexOption) error {
indexOpts := options.Index()
for _, opt := range opts {
opt(indexOpts)
}
indexModel := mongo.IndexModel{
Keys: bson.D{{Key: key, Value: 1}},
Options: indexOpts,
}
if _, err := r.collection.Indexes().CreateOne(ctx, indexModel); err != nil {
return errors.Join(ErrFailedToCreateIndex, err)
}
return nil
}
// Create inserts a new document into the MongoDB collection.
// It takes a context.Context and a model of type T as input parameters.
// It returns the ID of the newly created document as a string and an error, if any.
func (r *mongoRepository[T]) Create(ctx context.Context, model T) (string, error) {
result, err := r.collection.InsertOne(ctx, model)
if err != nil {
// Handle duplicate key error
if mongo.IsDuplicateKeyError(err) {
return "", errors.Join(ErrFailedToCreate, ErrDuplicate, err)
}
return "", errors.Join(ErrFailedToCreate, err)
}
oid, ok := result.InsertedID.(primitive.ObjectID)
if !ok {
return "", errors.Join(ErrInvalidDocumentID, err)
}
return oid.Hex(), nil
}
// FindByID retrieves a document from the MongoDB collection by its ID.
// It takes a context.Context and the ID of the document as parameters.
// It returns the retrieved document of type T and an error, if any.
func (r *mongoRepository[T]) FindByID(ctx context.Context, id string) (T, error) {
var result T
objID, err := primitive.ObjectIDFromHex(id)
if err != nil {
return result, errors.Join(ErrFailedToFindByID, ErrInvalidDocumentID, err)
}
filter := bson.M{"_id": objID}
if err := r.collection.FindOne(ctx, filter).Decode(&result); err != nil {
if errors.Is(err, mongo.ErrNoDocuments) {
return result, errors.Join(ErrFailedToFindByID, ErrNotFound, err)
}
return result, errors.Join(ErrFailedToFindByID, err)
}
return result, nil
}
// FindByIDs retrieves multiple documents from the MongoDB collection by their IDs.
// It takes a context.Context and a slice of IDs as parameters.
// It returns a slice of documents of type T and an error, if any.
func (r *mongoRepository[T]) FindByIDs(ctx context.Context, ids ...string) ([]T, error) {
// Convert string IDs to ObjectIDs
objIDs := make([]primitive.ObjectID, len(ids))
for i, id := range ids {
objID, err := primitive.ObjectIDFromHex(id)
if err != nil {
return nil, errors.Join(ErrFailedToFindByIDs, ErrInvalidDocumentID, err)
}
objIDs[i] = objID
}
// Build the query filter
filter := bson.M{"_id": bson.M{"$in": objIDs}}
// Find documents
cursor, err := r.collection.Find(ctx, filter, options.Find())
if err != nil {
if errors.Is(err, mongo.ErrNoDocuments) {
return nil, errors.Join(ErrFailedToFindByIDs, ErrNotFound, err)
}
return nil, errors.Join(ErrFailedToFindByIDs, err)
}
defer cursor.Close(ctx)
var results []T
for cursor.Next(ctx) {
var element T
if err := cursor.Decode(&element); err != nil {
return nil, errors.Join(ErrFailedToFindByIDs, err)
}
results = append(results, element)
}
if err := cursor.Err(); err != nil {
return nil, errors.Join(ErrFailedToFindByIDs, err)
}
if len(results) == 0 {
return nil, errors.Join(ErrFailedToFindByIDs, ErrNotFound)
}
return results, nil
}
// Update updates a document in the MongoDB collection with the specified ID.
// It takes a context, ID string, and model as input parameters.
// It returns the number of modified documents and an error, if any.
func (r *mongoRepository[T]) Update(ctx context.Context, id string, model T) (int64, error) {
objID, err := primitive.ObjectIDFromHex(id)
if err != nil {
return 0, errors.Join(ErrFailedToFindByID, ErrInvalidDocumentID, err)
}
update := bson.M{"$set": model}
result, err := r.collection.UpdateByID(ctx, objID, update)
if err != nil {
if errors.Is(err, mongo.ErrNoDocuments) {
return 0, errors.Join(ErrFailedToUpdate, ErrNotFound, err)
}
return 0, errors.Join(ErrFailedToUpdate, err)
}
if result.MatchedCount == 0 {
return 0, errors.Join(ErrFailedToUpdate, ErrNotFound)
}
return result.ModifiedCount, nil
}
// UpdateMany updates multiple documents in the MongoDB collection based on the provided filters.
// It takes a context.Context, a map of update fields, and optional filter functions as parameters.
// The update fields specify the changes to be made to the documents.
// The filter functions are used to build the filter for selecting the documents to be updated.
// It returns the number of documents modified and an error if any.
func (r *mongoRepository[T]) UpdateMany(ctx context.Context, update map[string]interface{}, filters ...FilterFunc) (int64, error) {
// Build the filter
filter := bson.D{}
for _, f := range filters {
filter = f(filter)
}
// Prepare the update document
updateDoc := bson.M{"$set": update}
// Perform the update
result, err := r.collection.UpdateMany(ctx, filter, updateDoc)
if err != nil {
if errors.Is(err, mongo.ErrNoDocuments) {
return 0, errors.Join(ErrFailedToUpdateMany, ErrNotFound, err)
}
return 0, errors.Join(ErrFailedToUpdateMany, err)
}
return result.ModifiedCount, nil
}
// Delete deletes a document from the MongoDB collection based on the provided ID.
// It returns the number of deleted documents and an error, if any.
func (r *mongoRepository[T]) Delete(ctx context.Context, id string) (int64, error) {
objID, err := primitive.ObjectIDFromHex(id)
if err != nil {
return 0, errors.Join(ErrFailedToFindByID, ErrInvalidDocumentID, err)
}
result, err := r.collection.DeleteOne(ctx, bson.M{"_id": objID})
if err != nil {
if errors.Is(err, mongo.ErrNoDocuments) {
return 0, errors.Join(ErrFailedToDelete, ErrNotFound, err)
}
return 0, errors.Join(ErrFailedToDelete, err)
}
if result.DeletedCount == 0 {
return 0, errors.Join(ErrFailedToDelete, ErrNotFound)
}
return result.DeletedCount, nil
}
// DeleteMany deletes multiple documents from the MongoDB collection based on the provided filters.
// It returns the number of deleted documents and an error, if any.
func (r *mongoRepository[T]) DeleteMany(ctx context.Context, filters ...FilterFunc) (int64, error) {
filter := bson.D{}
for _, f := range filters {
filter = f(filter)
}
result, err := r.collection.DeleteMany(ctx, filter)
if err != nil {
if errors.Is(err, mongo.ErrNoDocuments) {
return 0, errors.Join(ErrFailedToDeleteMany, ErrNotFound, err)
}
return 0, errors.Join(ErrFailedToDeleteMany, err)
}
return result.DeletedCount, nil
}
// FindManyByFilter retrieves multiple documents from the collection based on the provided filters.
// It allows skipping a certain number of documents and limiting the number of documents to be returned.
// The filters are applied in the order they are passed.
// If no documents match the filters, it returns an error with the ErrNotFound error code.
// If an error occurs during the retrieval process, it returns an error with the ErrFailedToFindManyByFilter error code.
// The function returns a slice of documents of type T and an error.
func (r *mongoRepository[T]) FindManyByFilter(ctx context.Context, skip int64, limit int64, filters ...FilterFunc) ([]T, error) {
filter := bson.D{}
for _, f := range filters {
filter = f(filter)
}
if limit == 0 {
limit = 10
}
findOptions := options.Find().SetSkip(skip).SetLimit(limit)
cursor, err := r.collection.Find(ctx, filter, findOptions)
if err != nil {
if errors.Is(err, mongo.ErrNoDocuments) {
return nil, errors.Join(ErrFailedToFindManyByFilter, ErrNotFound, err)
}
return nil, errors.Join(ErrFailedToFindManyByFilter, err)
}
defer cursor.Close(ctx)
var results []T
for cursor.Next(ctx) {
var element T
if err := cursor.Decode(&element); err != nil {
return nil, errors.Join(ErrFailedToFindManyByFilter, err)
}
results = append(results, element)
}
if err := cursor.Err(); err != nil {
return nil, errors.Join(ErrFailedToFindManyByFilter, err)
}
if len(results) == 0 {
return nil, errors.Join(ErrFailedToFindManyByFilter, ErrNotFound)
}
return results, nil
}
// FindOneByFilter finds a single document in the collection based on the provided filters.
// It accepts one or more FilterFunc functions that modify the filter criteria.
// The function returns the found document of type T and an error, if any.
// If no document is found, it returns an error of type ErrNotFound.
// If an error occurs during the find operation, it returns the error.
func (r *mongoRepository[T]) FindOneByFilter(ctx context.Context, filters ...FilterFunc) (T, error) {
filter := bson.D{}
for _, f := range filters {
filter = f(filter)
}
var result T
if err := r.collection.FindOne(ctx, filter).Decode(&result); err != nil {
if errors.Is(err, mongo.ErrNoDocuments) {
return result, errors.Join(ErrFailedToFindOneByFilter, ErrNotFound, err)
}
return result, errors.Join(ErrFailedToFindOneByFilter, err)
}
return result, nil
}
// Exists checks if a document exists in the collection based on the provided filters.
// It accepts one or more FilterFunc functions that modify the filter criteria.
// The function returns true if a document exists and false otherwise.
// If an error occurs during the find operation, it returns the error.
func (r *mongoRepository[T]) Exists(ctx context.Context, filters ...FilterFunc) (bool, error) {
filter := bson.D{}
for _, f := range filters {
filter = f(filter)
}
count, err := r.collection.CountDocuments(ctx, filter)
if err != nil {
return false, errors.Join(ErrFailedToFindOneByFilter, err)
}
return count > 0, nil
}
// Count returns the number of documents in the collection based on the provided filters.
// It accepts one or more FilterFunc functions that modify the filter criteria.
// The function returns the number of documents and an error, if any.
func (r *mongoRepository[T]) Count(ctx context.Context, filters ...FilterFunc) (int64, error) {
filter := bson.D{}
for _, f := range filters {
filter = f(filter)
}
count, err := r.collection.CountDocuments(ctx, filter)
if err != nil {
return 0, errors.Join(ErrFailedToFindOneByFilter, err)
}
return count, nil
}