-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.go
More file actions
104 lines (90 loc) · 5.35 KB
/
Copy pathdatabase.go
File metadata and controls
104 lines (90 loc) · 5.35 KB
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
package mongokit
import (
"context"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo"
"go.mongodb.org/mongo-driver/v2/mongo/options"
)
// IRepository defines the interface for database operations
type IRepository[T Document] interface {
Collection() *mongo.Collection
InsertOne(ctx context.Context, document T, opts ...options.Lister[options.InsertOneOptions]) (T, error)
InsertMany(ctx context.Context, documents []T, opts ...options.Lister[options.InsertManyOptions]) ([]any, error)
Find(ctx context.Context, filter any, opts ...options.Lister[options.FindOptions]) (*mongo.Cursor, error)
FindOneRaw(ctx context.Context, filter any, opts ...options.Lister[options.FindOneOptions]) *mongo.SingleResult
FindDecoded(ctx context.Context, filter any, opts ...options.Lister[options.FindOptions]) ([]T, error)
FindDecodedWithTotal(ctx context.Context, filter any, opts ...options.Lister[options.FindOptions]) ([]T, int64, error)
FindPaginated(ctx context.Context, filter any, page, pageSize int64) ([]T, error)
FindPaginatedWithTotal(ctx context.Context, filter any, page, pageSize int64) ([]T, int64, error)
FindCursorPaginated(ctx context.Context, filter any, pagination *CursorPagination, sort any, cursorEncoder func(T) (string, error)) (*CursorPaginationResult[T], error)
FindOne(ctx context.Context, filter any, opts ...options.Lister[options.FindOneOptions]) (T, error)
FindByID(ctx context.Context, id any, opts ...options.Lister[options.FindOneOptions]) (T, error)
FindOneAndUpdate(ctx context.Context, filter any, update any, opts ...options.Lister[options.FindOneAndUpdateOptions]) (T, error)
FindOneAndUpdateByID(ctx context.Context, id, update any, opts ...options.Lister[options.FindOneAndUpdateOptions]) (T, error)
FindOneAndDelete(ctx context.Context, filter any, opts ...options.Lister[options.FindOneAndDeleteOptions]) (T, error)
UpdateOne(ctx context.Context, filter any, update any, opts ...options.Lister[options.UpdateOneOptions]) (*mongo.UpdateResult, error)
UpdateByID(ctx context.Context, id any, update any, opts ...options.Lister[options.UpdateOneOptions]) (*mongo.UpdateResult, error)
UpdateMany(ctx context.Context, filter any, update any, opts ...options.Lister[options.UpdateManyOptions]) (*mongo.UpdateResult, error)
DeleteOne(ctx context.Context, filter any, opts ...options.Lister[options.DeleteOneOptions]) error
DeleteByID(ctx context.Context, id any, opts ...options.Lister[options.DeleteOneOptions]) error
DeleteMany(ctx context.Context, filter any, opts ...options.Lister[options.DeleteManyOptions]) (int64, error)
EstimatedCount(ctx context.Context, opts ...options.Lister[options.EstimatedDocumentCountOptions]) (int64, error)
CountDocuments(ctx context.Context, filter any, opts ...options.Lister[options.CountOptions]) (int64, error)
Aggregate(ctx context.Context, pipeline any, opts ...options.Lister[options.AggregateOptions]) ([]bson.M, error)
AggregateTyped(ctx context.Context, pipeline any, opts ...options.Lister[options.AggregateOptions]) ([]T, error)
Distinct(ctx context.Context, fieldName string, filter any, opts ...options.Lister[options.DistinctOptions]) ([]any, error)
Transaction(ctx context.Context, fn func(sessCtx context.Context) error, opts ...options.Lister[options.SessionOptions]) error
EnsureIndexes(ctx context.Context, indexes []mongo.IndexModel, opts ...options.Lister[options.CreateIndexesOptions]) error
GetIndexes(ctx context.Context, opts ...options.Lister[options.ListIndexesOptions]) ([]bson.M, error)
BulkWrite(ctx context.Context, models []mongo.WriteModel, opts ...options.Lister[options.BulkWriteOptions]) (*mongo.BulkWriteResult, error)
Watch(ctx context.Context, pipeline any, opts ...options.Lister[options.ChangeStreamOptions]) (*mongo.ChangeStream, error)
}
// Document represents an interface for common document operations.
type Document interface {
SetID(id bson.ObjectID)
BeforeInsert()
BeforeUpdate()
}
// ICollectionName is implemented by models to declare their collection name.
type ICollectionName interface {
CollectionName() string
}
// Repository implements IRepository interface for MongoDB
type Repository[T Document] struct {
collection *mongo.Collection
}
// NewRepository creates a new MongoDB repository.
// Collection name is taken from T.CollectionName() via the ICollectionName interface.
// If T implements IIndex, indexes are ensured automatically.
// T methods must use pointer receivers to avoid nil pointer panics.
//
// Panics if:
// - T does not implement ICollectionName
// - CollectionName() returns an empty string
// - index creation fails
func NewRepository[T Document](ctx context.Context, database *mongo.Database) *Repository[T] {
var zero T
namer, ok := any(zero).(ICollectionName)
if !ok {
panic("mongokit: model must implement CollectionName() string")
}
name := namer.CollectionName()
if name == "" {
panic("mongokit: CollectionName() must return a non-empty string")
}
validateBaseFieldInline(zero)
repo := &Repository[T]{
collection: database.Collection(name),
}
if indexer, ok := any(zero).(IIndex); ok {
indexes := indexer.Indexes()
if len(indexes) > 0 {
if err := repo.EnsureIndexes(ctx, indexes); err != nil {
panic("mongokit: failed to ensure indexes for " + name + ": " + err.Error())
}
}
}
return repo
}
// compile-time check that Repository implements IRepository
var _ IRepository[*BaseField] = (*Repository[*BaseField])(nil)