Skip to content

Commit f455dd1

Browse files
committed
hello mongo
1 parent b1a6769 commit f455dd1

6 files changed

Lines changed: 1040 additions & 0 deletions

File tree

go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/gin-gonic/gin v1.11.0
77
github.com/go-redis/redismock/v9 v9.2.0
88
github.com/redis/go-redis/v9 v9.14.0
9+
go.mongodb.org/mongo-driver v1.17.4
910
gorm.io/driver/sqlite v1.6.0
1011
gorm.io/gorm v1.31.0
1112
)
@@ -15,9 +16,16 @@ require (
1516
github.com/cespare/xxhash/v2 v2.3.0 // indirect
1617
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
1718
github.com/goccy/go-yaml v1.18.0 // indirect
19+
github.com/golang/snappy v0.0.4 // indirect
20+
github.com/klauspost/compress v1.16.7 // indirect
21+
github.com/montanaflynn/stats v0.7.1 // indirect
1822
github.com/onsi/gomega v1.38.2 // indirect
1923
github.com/quic-go/qpack v0.5.1 // indirect
2024
github.com/quic-go/quic-go v0.54.0 // indirect
25+
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
26+
github.com/xdg-go/scram v1.1.2 // indirect
27+
github.com/xdg-go/stringprep v1.0.4 // indirect
28+
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect
2129
go.uber.org/mock v0.5.0 // indirect
2230
golang.org/x/mod v0.28.0 // indirect
2331
golang.org/x/sync v0.17.0 // indirect

go.sum

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4=
3939
github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
4040
github.com/goccy/go-yaml v1.18.0 h1:8W7wMFS12Pcas7KU+VVkaiCng+kG8QiFeFwzFb+rwuw=
4141
github.com/goccy/go-yaml v1.18.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
42+
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
43+
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
4244
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
4345
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
4446
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
@@ -48,6 +50,8 @@ github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
4850
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
4951
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
5052
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
53+
github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I=
54+
github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
5155
github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y=
5256
github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
5357
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
@@ -61,6 +65,8 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w
6165
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
6266
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
6367
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
68+
github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8eaE=
69+
github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
6470
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
6571
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
6672
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
@@ -90,29 +96,63 @@ github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS
9096
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
9197
github.com/ugorji/go/codec v1.3.0 h1:Qd2W2sQawAfG8XSvzwhBeoGq71zXOC/Q1E9y/wUcsUA=
9298
github.com/ugorji/go/codec v1.3.0/go.mod h1:pRBVtBSKl77K30Bv8R2P+cLSGaTtex6fsA2Wjqmfxj4=
99+
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
100+
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
101+
github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY=
102+
github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4=
103+
github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6c8=
104+
github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM=
105+
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 h1:ilQV1hzziu+LLM3zUTJ0trRztfwgjqKnBWNtSRkbmwM=
106+
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78/go.mod h1:aL8wCCfTfSfmXjznFBSZNN13rSJjlIOI1fUNAtF7rmI=
107+
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
108+
go.mongodb.org/mongo-driver v1.17.4 h1:jUorfmVzljjr0FLzYQsGP8cgN/qzzxlY9Vh0C9KFXVw=
109+
go.mongodb.org/mongo-driver v1.17.4/go.mod h1:Hy04i7O2kC4RS06ZrhPRqj/u4DTYkFDAAccj+rVKqgQ=
93110
go.uber.org/mock v0.5.0 h1:KAMbZvZPyBPWgD14IrIQ38QCyjwpvVVV6K/bHl1IwQU=
94111
go.uber.org/mock v0.5.0/go.mod h1:ge71pBPLYDk7QIi1LupWxdAykm7KIEFchiOqd6z7qMM=
95112
go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc=
96113
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
97114
golang.org/x/arch v0.21.0 h1:iTC9o7+wP6cPWpDWkivCvQFGAHDQ59SrSxsLPcnkArw=
98115
golang.org/x/arch v0.21.0/go.mod h1:dNHoOeKiyja7GTvF9NJS1l3Z2yntpQNzgrjh1cU103A=
116+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
117+
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
99118
golang.org/x/crypto v0.42.0 h1:chiH31gIWm57EkTXpwnqf8qeuMUi0yekh6mT2AvFlqI=
100119
golang.org/x/crypto v0.42.0/go.mod h1:4+rDnOTJhQCx2q7/j6rAN5XDw8kPjeaXEUR2eL94ix8=
101120
golang.org/x/exp v0.0.0-20250911091902-df9299821621 h1:2id6c1/gto0kaHYyrixvknJ8tUK/Qs5IsmBtrc+FtgU=
102121
golang.org/x/exp v0.0.0-20250911091902-df9299821621/go.mod h1:TwQYMMnGpvZyc+JpB/UAuTNIsVJifOlSkrZkhcvpVUk=
122+
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
103123
golang.org/x/mod v0.28.0 h1:gQBtGhjxykdjY9YhZpSlZIsbnaE2+PgjfLWUQTnoZ1U=
104124
golang.org/x/mod v0.28.0/go.mod h1:yfB/L0NOf/kmEbXjzCPOx1iK1fRutOydrCMsqRhEBxI=
125+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
126+
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
127+
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
105128
golang.org/x/net v0.44.0 h1:evd8IRDyfNBMBTTY5XRF1vaZlD+EmWx6x8PkhR04H/I=
106129
golang.org/x/net v0.44.0/go.mod h1:ECOoLqd5U3Lhyeyo/QDCEVQ4sNgYsqvCZ722XogGieY=
130+
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
131+
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
107132
golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug=
108133
golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
134+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
135+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
136+
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
137+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
138+
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
109139
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
110140
golang.org/x/sys v0.36.0 h1:KVRy2GtZBrk1cBYA7MKu5bEZFxQk4NIDV6RLVcC8o0k=
111141
golang.org/x/sys v0.36.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
142+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
143+
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
144+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
145+
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
146+
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
147+
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
112148
golang.org/x/text v0.29.0 h1:1neNs90w9YzJ9BocxfsQNHKuAT4pkghyXc4nhZ6sJvk=
113149
golang.org/x/text v0.29.0/go.mod h1:7MhJOA9CD2qZyOKYazxdYMF85OwPdEr9jTtBpO7ydH4=
150+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
151+
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
152+
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
114153
golang.org/x/tools v0.37.0 h1:DVSRzp7FwePZW356yEAChSdNcQo6Nsp+fex1SUW09lE=
115154
golang.org/x/tools v0.37.0/go.mod h1:MBN5QPQtLMHVdvsbtarmTNukZDdgwdwlO5qGacAzF0w=
155+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
116156
google.golang.org/protobuf v1.36.9 h1:w2gp2mA27hUeUzj9Ex9FBjsBm40zfaDtEWow293U7Iw=
117157
google.golang.org/protobuf v1.36.9/go.mod h1:fuxRtAxBytpl4zzqUh6/eyUujkJdNiuEkXntxiD/uRU=
118158
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
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+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package mongorepository
2+
3+
import (
4+
"context"
5+
6+
"github.com/philiphil/restman/orm/entity"
7+
)
8+
9+
func (r *MongoRepository[M, E]) Create(entities []*E) error {
10+
return r.BatchInsert(context.Background(), entities)
11+
}
12+
13+
func (r *MongoRepository[M, E]) Update(entities []*E) error {
14+
return r.BatchUpdate(context.Background(), entities)
15+
}
16+
17+
func (r *MongoRepository[M, E]) Read(ids []entity.ID) ([]*E, error) {
18+
return r.FindByIDs(context.Background(), ids)
19+
}
20+
21+
func (r *MongoRepository[M, E]) Delete(entities []*E) error {
22+
return r.BatchDelete(context.Background(), entities)
23+
}
24+
25+
func (r *MongoRepository[M, E]) List(limit int, offset int, order map[string]string) ([]E, error) {
26+
orderSpecifications := make([]Specification, 0, len(order))
27+
for k, v := range order {
28+
orderSpecifications = append(orderSpecifications, OrderBy(k, v))
29+
}
30+
return r.FindWithLimit(context.Background(), limit, offset, orderSpecifications...)
31+
}
32+
33+
func (r *MongoRepository[M, E]) New() E {
34+
return r.NewEntity()
35+
}
36+
37+
func (r *MongoRepository[M, E]) Count() (int64, error) {
38+
return r.collection.CountDocuments(context.Background(), map[string]interface{}{})
39+
}

0 commit comments

Comments
 (0)