|
| 1 | +package mongorepository |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/philiphil/restman/orm/entity" |
| 7 | + "go.mongodb.org/mongo-driver/bson" |
| 8 | + "go.mongodb.org/mongo-driver/mongo" |
| 9 | + "go.mongodb.org/mongo-driver/mongo/options" |
| 10 | +) |
| 11 | + |
| 12 | +func NewRepository[M entity.DatabaseModel[E], E entity.Entity](collection *mongo.Collection) *MongoRepository[M, E] { |
| 13 | + return &MongoRepository[M, E]{ |
| 14 | + collection: collection, |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +type MongoRepository[M entity.DatabaseModel[E], E entity.Entity] struct { |
| 19 | + collection *mongo.Collection |
| 20 | +} |
| 21 | + |
| 22 | +func (r *MongoRepository[M, E]) Insert(ctx context.Context, entity *E) error { |
| 23 | + var start M |
| 24 | + model := start.FromEntity(*entity).(M) |
| 25 | + |
| 26 | + result, err := r.collection.InsertOne(ctx, model) |
| 27 | + if err != nil { |
| 28 | + return err |
| 29 | + } |
| 30 | + |
| 31 | + (*entity).SetId(result.InsertedID) |
| 32 | + |
| 33 | + return nil |
| 34 | +} |
| 35 | + |
| 36 | +func (r *MongoRepository[M, E]) DeleteByID(ctx context.Context, id entity.ID) error { |
| 37 | + filter := bson.M{"_id": id} |
| 38 | + _, err := r.collection.DeleteOne(ctx, filter) |
| 39 | + return err |
| 40 | +} |
| 41 | + |
| 42 | +func (r *MongoRepository[M, E]) Upsert(ctx context.Context, entity *E) error { |
| 43 | + var start M |
| 44 | + model := start.FromEntity(*entity).(M) |
| 45 | + |
| 46 | + id := (*entity).GetId() |
| 47 | + filter := bson.M{"_id": id} |
| 48 | + opts := options.Replace().SetUpsert(true) |
| 49 | + |
| 50 | + _, err := r.collection.ReplaceOne(ctx, filter, model, opts) |
| 51 | + if err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + |
| 55 | + return nil |
| 56 | +} |
| 57 | + |
| 58 | +func (r *MongoRepository[M, E]) FindByID(ctx context.Context, id entity.ID) (E, error) { |
| 59 | + var model M |
| 60 | + filter := bson.M{"_id": id} |
| 61 | + |
| 62 | + err := r.collection.FindOne(ctx, filter).Decode(&model) |
| 63 | + if err != nil { |
| 64 | + return *new(E), err |
| 65 | + } |
| 66 | + |
| 67 | + return model.ToEntity(), nil |
| 68 | +} |
| 69 | + |
| 70 | +func (r *MongoRepository[M, E]) Find(ctx context.Context, specifications ...Specification) ([]E, error) { |
| 71 | + return r.FindWithLimit(ctx, -1, -1, specifications...) |
| 72 | +} |
| 73 | + |
| 74 | +func (r *MongoRepository[M, E]) buildFilter(specifications []Specification) bson.M { |
| 75 | + filters := make([]bson.M, 0) |
| 76 | + |
| 77 | + for _, spec := range specifications { |
| 78 | + if filter := spec.GetFilter(); filter != nil { |
| 79 | + filters = append(filters, filter) |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + if len(filters) == 0 { |
| 84 | + return bson.M{} |
| 85 | + } |
| 86 | + |
| 87 | + if len(filters) == 1 { |
| 88 | + return filters[0] |
| 89 | + } |
| 90 | + |
| 91 | + return bson.M{"$and": filters} |
| 92 | +} |
| 93 | + |
| 94 | +func (r *MongoRepository[M, E]) buildOptions(limit int, offset int, specifications []Specification) *options.FindOptions { |
| 95 | + opts := options.Find() |
| 96 | + |
| 97 | + if limit > 0 { |
| 98 | + opts.SetLimit(int64(limit)) |
| 99 | + } |
| 100 | + |
| 101 | + if offset > 0 { |
| 102 | + opts.SetSkip(int64(offset)) |
| 103 | + } |
| 104 | + |
| 105 | + for _, spec := range specifications { |
| 106 | + if sort := spec.GetSort(); sort != nil { |
| 107 | + opts.SetSort(sort) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return opts |
| 112 | +} |
| 113 | + |
| 114 | +func (r *MongoRepository[M, E]) FindWithLimit(ctx context.Context, limit int, offset int, specifications ...Specification) ([]E, error) { |
| 115 | + filter := r.buildFilter(specifications) |
| 116 | + opts := r.buildOptions(limit, offset, specifications) |
| 117 | + |
| 118 | + cursor, err := r.collection.Find(ctx, filter, opts) |
| 119 | + if err != nil { |
| 120 | + return nil, err |
| 121 | + } |
| 122 | + defer cursor.Close(ctx) |
| 123 | + |
| 124 | + var models []M |
| 125 | + if err := cursor.All(ctx, &models); err != nil { |
| 126 | + return nil, err |
| 127 | + } |
| 128 | + |
| 129 | + result := make([]E, 0, len(models)) |
| 130 | + for _, model := range models { |
| 131 | + result = append(result, model.ToEntity()) |
| 132 | + } |
| 133 | + |
| 134 | + return result, nil |
| 135 | +} |
| 136 | + |
| 137 | +func (r *MongoRepository[M, E]) FindAll(ctx context.Context, specification ...Specification) ([]E, error) { |
| 138 | + return r.FindWithLimit(ctx, -1, -1, specification...) |
| 139 | +} |
| 140 | + |
| 141 | +func (r *MongoRepository[M, E]) FindByIDs(ctx context.Context, ids []entity.ID) ([]*E, error) { |
| 142 | + filter := bson.M{"_id": bson.M{"$in": ids}} |
| 143 | + |
| 144 | + cursor, err := r.collection.Find(ctx, filter) |
| 145 | + if err != nil { |
| 146 | + return nil, err |
| 147 | + } |
| 148 | + defer cursor.Close(ctx) |
| 149 | + |
| 150 | + var models []M |
| 151 | + if err := cursor.All(ctx, &models); err != nil { |
| 152 | + return nil, err |
| 153 | + } |
| 154 | + |
| 155 | + result := make([]*E, 0, len(models)) |
| 156 | + for _, model := range models { |
| 157 | + entity := model.ToEntity() |
| 158 | + result = append(result, &entity) |
| 159 | + } |
| 160 | + |
| 161 | + return result, nil |
| 162 | +} |
| 163 | + |
| 164 | +func (r *MongoRepository[M, E]) DeleteByIDs(ctx context.Context, ids []entity.ID) error { |
| 165 | + filter := bson.M{"_id": bson.M{"$in": ids}} |
| 166 | + _, err := r.collection.DeleteMany(ctx, filter) |
| 167 | + return err |
| 168 | +} |
| 169 | + |
| 170 | +func (r *MongoRepository[M, E]) BatchDelete(ctx context.Context, entities []*E) error { |
| 171 | + ids := make([]entity.ID, 0, len(entities)) |
| 172 | + for _, entity := range entities { |
| 173 | + ids = append(ids, (*entity).GetId()) |
| 174 | + } |
| 175 | + return r.DeleteByIDs(ctx, ids) |
| 176 | +} |
| 177 | + |
| 178 | +func (r *MongoRepository[M, E]) BatchUpdate(ctx context.Context, entities []*E) error { |
| 179 | + for _, entity := range entities { |
| 180 | + if err := r.Upsert(ctx, entity); err != nil { |
| 181 | + return err |
| 182 | + } |
| 183 | + } |
| 184 | + return nil |
| 185 | +} |
| 186 | + |
| 187 | +func (r *MongoRepository[M, E]) BatchInsert(ctx context.Context, entities []*E) error { |
| 188 | + if len(entities) == 0 { |
| 189 | + return nil |
| 190 | + } |
| 191 | + |
| 192 | + docs := make([]interface{}, 0, len(entities)) |
| 193 | + for _, entity := range entities { |
| 194 | + var start M |
| 195 | + model := start.FromEntity(*entity).(M) |
| 196 | + docs = append(docs, model) |
| 197 | + } |
| 198 | + |
| 199 | + results, err := r.collection.InsertMany(ctx, docs) |
| 200 | + if err != nil { |
| 201 | + return err |
| 202 | + } |
| 203 | + |
| 204 | + for i, insertedID := range results.InsertedIDs { |
| 205 | + (*entities[i]).SetId(insertedID) |
| 206 | + } |
| 207 | + |
| 208 | + return nil |
| 209 | +} |
| 210 | + |
| 211 | +func (r *MongoRepository[M, E]) GetCollection() *mongo.Collection { |
| 212 | + return r.collection |
| 213 | +} |
| 214 | + |
| 215 | +func (r *MongoRepository[M, E]) NewEntity() E { |
| 216 | + var entity E |
| 217 | + return entity |
| 218 | +} |
| 219 | + |
| 220 | +func (r *MongoRepository[M, E]) CountWithSpecifications(ctx context.Context, specifications ...Specification) (int64, error) { |
| 221 | + filter := r.buildFilter(specifications) |
| 222 | + return r.collection.CountDocuments(ctx, filter) |
| 223 | +} |
0 commit comments