init
This commit is contained in:
commit
1d2716ae1d
|
@ -0,0 +1,19 @@
|
|||
module git.nefrace.ru/nefrace/tongo
|
||||
|
||||
go 1.19
|
||||
|
||||
require (
|
||||
github.com/golang/snappy v0.0.1 // indirect
|
||||
github.com/joho/godotenv v1.4.0 // indirect
|
||||
github.com/klauspost/compress v1.13.6 // indirect
|
||||
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
|
||||
github.com/xdg-go/scram v1.1.1 // indirect
|
||||
github.com/xdg-go/stringprep v1.0.3 // indirect
|
||||
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
|
||||
go.mongodb.org/mongo-driver v1.11.1
|
||||
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect
|
||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
|
||||
golang.org/x/text v0.3.7 // indirect
|
||||
)
|
|
@ -0,0 +1,48 @@
|
|||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
|
||||
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
||||
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
|
||||
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||
github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc=
|
||||
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe h1:iruDEfMl2E6fbMZ9s0scYfZQ84/6SPL6zC8ACM2oIL0=
|
||||
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
|
||||
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
|
||||
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
|
||||
github.com/xdg-go/scram v1.1.1 h1:VOMT+81stJgXW3CpHyqHN3AXDYIMsx56mEFrB37Mb/E=
|
||||
github.com/xdg-go/scram v1.1.1/go.mod h1:RaEWvsqvNKKvBPvcKeFjrG2cJqOkHTiyTpzz23ni57g=
|
||||
github.com/xdg-go/stringprep v1.0.3 h1:kdwGpVNwPFtjs98xCGkHjQtGKh86rDcRZN17QEMCOIs=
|
||||
github.com/xdg-go/stringprep v1.0.3/go.mod h1:W3f5j4i+9rC0kuIEJL0ky1VpHXQU3ocBgklLGvcBnW8=
|
||||
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d h1:splanxYIlg+5LfHAM6xpdFEAYOk8iySO56hMFq6uLyA=
|
||||
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA=
|
||||
go.mongodb.org/mongo-driver v1.11.1 h1:QP0znIRTuL0jf1oBQoAoM0C6ZJfBK4kx0Uumtv1A7w8=
|
||||
go.mongodb.org/mongo-driver v1.11.1/go.mod h1:s7p5vEtfbeR1gYi6pnj3c3/urpbLv2T5Sfd6Rp2HBB8=
|
||||
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d h1:sK3txAijHtOK88l68nt020reeT1ZdKLIYetKl95FzVY=
|
||||
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
||||
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
|
||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
|
@ -0,0 +1,22 @@
|
|||
package tongo
|
||||
|
||||
import "go.mongodb.org/mongo-driver/bson/primitive"
|
||||
|
||||
type Collectable interface {
|
||||
Coll() string
|
||||
GetID() primitive.ObjectID
|
||||
}
|
||||
|
||||
type Item struct {
|
||||
Id primitive.ObjectID `bson:"_id" json:"Id,omitempty"`
|
||||
}
|
||||
|
||||
func NewID() Item {
|
||||
return Item{
|
||||
Id: primitive.NewObjectID(),
|
||||
}
|
||||
}
|
||||
|
||||
func (i Item) GetID() primitive.ObjectID {
|
||||
return i.Id
|
||||
}
|
|
@ -0,0 +1,125 @@
|
|||
package tongo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
type Database struct {
|
||||
db *mongo.Client
|
||||
}
|
||||
|
||||
func (d *Database) Collection(col string) *mongo.Collection {
|
||||
return d.db.Database("eeee").Collection(col)
|
||||
}
|
||||
|
||||
func getFilter(f *bson.D) bson.D {
|
||||
if f == nil {
|
||||
return bson.D{}
|
||||
}
|
||||
return *f
|
||||
}
|
||||
|
||||
func QueryFilter(q *url.Values) bson.D {
|
||||
filter := bson.D{}
|
||||
for k, v := range *q {
|
||||
var f bson.E
|
||||
q := strings.Split(k, "_")
|
||||
if len(q) == 1 { // If param is like "name" or "url" we're using exact matching
|
||||
f = bson.E{Key: k, Value: v[0]}
|
||||
} else {
|
||||
if q[1] == "like" { // If it's like "name_like", we're using regex
|
||||
f = bson.E{Key: q[0], Value: bson.D{{Key: "$regex", Value: v[0]}}}
|
||||
}
|
||||
}
|
||||
filter = append(filter, f) // Applying query filters to user filter
|
||||
}
|
||||
return filter
|
||||
}
|
||||
|
||||
func NewStore[T Collectable](db *mongo.Client) Store[T] {
|
||||
var item T
|
||||
coll := item.Coll()
|
||||
return Store[T]{
|
||||
Db: db,
|
||||
Coll: db.Database("instagetter").Collection(coll),
|
||||
}
|
||||
}
|
||||
|
||||
type Store[T Collectable] struct {
|
||||
Db *mongo.Client
|
||||
Coll *mongo.Collection
|
||||
}
|
||||
|
||||
func (s Store[T]) InsertOne(ctx context.Context, item *T) (primitive.ObjectID, error) {
|
||||
result, err := s.Coll.InsertOne(ctx, item)
|
||||
if err != nil {
|
||||
if e, ok := err.(mongo.WriteError); ok {
|
||||
log.Printf("Error writing new item of type %v: %v ", reflect.TypeOf(item), e)
|
||||
return primitive.NilObjectID, e
|
||||
}
|
||||
log.Printf("Error creating new item of type %v: %v ", reflect.TypeOf(item), err)
|
||||
return primitive.NilObjectID, err
|
||||
}
|
||||
rid, _ := result.InsertedID.(primitive.ObjectID)
|
||||
return rid, nil
|
||||
}
|
||||
|
||||
func (s Store[T]) GetById(ctx context.Context, id primitive.ObjectID) (*T, error) {
|
||||
res := s.Coll.FindOne(ctx, bson.D{{Key: "_id", Value: id}})
|
||||
if res.Err() != nil {
|
||||
return nil, res.Err()
|
||||
}
|
||||
var item T
|
||||
res.Decode(&item)
|
||||
return &item, nil
|
||||
}
|
||||
|
||||
func (s Store[T]) GetOne(ctx context.Context, filter *bson.D) (*T, error) {
|
||||
f := getFilter(filter)
|
||||
res := s.Coll.FindOne(ctx, f)
|
||||
if res.Err() != nil {
|
||||
return nil, res.Err()
|
||||
}
|
||||
var item T
|
||||
res.Decode(&item)
|
||||
return &item, nil
|
||||
}
|
||||
|
||||
func (s Store[T]) GetMany(ctx context.Context, filter *bson.D) ([]*T, error) {
|
||||
f := getFilter(filter)
|
||||
cur, err := s.Coll.Find(ctx, f)
|
||||
if err != nil {
|
||||
log.Println("Error fetching items: ", err)
|
||||
return nil, err
|
||||
}
|
||||
res := []*T{}
|
||||
err = cur.All(ctx, &res)
|
||||
if err != nil {
|
||||
log.Println("Error collecting items to slice: ", err)
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (s Store[T]) DeleteByID(ctx context.Context, id primitive.ObjectID) error {
|
||||
_, err := s.Coll.DeleteOne(ctx, bson.D{{Key: "_id", Value: id}})
|
||||
return err
|
||||
}
|
||||
|
||||
func (s Store[T]) ReplaceItem(ctx context.Context, item T, upsert bool) error {
|
||||
_, err := s.Coll.ReplaceOne(ctx, bson.D{{Key: "_id", Value: item.GetID()}}, item, options.Replace().SetUpsert(upsert))
|
||||
return err
|
||||
}
|
||||
|
||||
func (s Store[T]) Count(ctx context.Context, filter *bson.D) (int64, error) {
|
||||
return s.Coll.CountDocuments(ctx, filter)
|
||||
}
|
Loading…
Reference in New Issue