mirror of
https://github.com/mainnika/mongox-go-driver.git
synced 2026-05-22 15:53:36 +00:00
Merge common functions into database interface
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox/query"
|
||||
)
|
||||
|
||||
// Count function counts documents in the database by query
|
||||
// target is used only to get collection by tag so it'd be better to use nil ptr here
|
||||
func (d *Database) Count(target interface{}, filters ...interface{}) (int64, error) {
|
||||
|
||||
collection := d.GetCollectionOf(target)
|
||||
opts := options.Count()
|
||||
composed := query.Compose(filters...)
|
||||
|
||||
opts.Limit = composed.Limiter()
|
||||
opts.Skip = composed.Skipper()
|
||||
|
||||
result, err := collection.CountDocuments(d.Context(), composed.M(), opts)
|
||||
if err == mongo.ErrNoDocuments {
|
||||
return 0, err
|
||||
}
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("can't decode desult: %w", err)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
@@ -4,10 +4,15 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox"
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox/query"
|
||||
)
|
||||
|
||||
// Database handler
|
||||
@@ -80,3 +85,150 @@ func (d *Database) GetCollectionOf(document interface{}) mongox.MongoCollection
|
||||
|
||||
panic(fmt.Errorf("document %v does not have a collection tag", document))
|
||||
}
|
||||
|
||||
func (d *Database) createSimpleLoad(target interface{}, composed *query.Query) (cursor *mongo.Cursor, err error) {
|
||||
|
||||
collection := d.GetCollectionOf(target)
|
||||
opts := options.Find()
|
||||
|
||||
opts.Sort = composed.Sorter()
|
||||
opts.Limit = composed.Limiter()
|
||||
opts.Skip = composed.Skipper()
|
||||
|
||||
return collection.Find(d.Context(), composed.M(), opts)
|
||||
}
|
||||
|
||||
func (d *Database) createAggregateLoad(target interface{}, composed *query.Query) (cursor *mongo.Cursor, err error) {
|
||||
|
||||
collection := d.GetCollectionOf(target)
|
||||
opts := options.Aggregate()
|
||||
|
||||
pipeline := primitive.A{}
|
||||
|
||||
if !composed.Empty() {
|
||||
pipeline = append(pipeline, primitive.M{"$match": primitive.M{"$expr": composed.M()}})
|
||||
}
|
||||
if composed.Sorter() != nil {
|
||||
pipeline = append(pipeline, primitive.M{"$sort": composed.Sorter()})
|
||||
}
|
||||
if composed.Skipper() != nil {
|
||||
pipeline = append(pipeline, primitive.M{"$skip": *composed.Skipper()})
|
||||
}
|
||||
if composed.Limiter() != nil {
|
||||
pipeline = append(pipeline, primitive.M{"$limit": *composed.Limiter()})
|
||||
}
|
||||
|
||||
el := reflect.ValueOf(target).Elem()
|
||||
elType := el.Type()
|
||||
numField := elType.NumField()
|
||||
_, preloads := composed.Preloader()
|
||||
|
||||
for i := 0; i < numField; i++ {
|
||||
|
||||
field := elType.Field(i)
|
||||
tag := field.Tag
|
||||
|
||||
preloadTag, ok := tag.Lookup("preload")
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
jsonTag, ok := tag.Lookup("json")
|
||||
if jsonTag == "-" {
|
||||
return nil, fmt.Errorf("preload private field is impossible")
|
||||
}
|
||||
|
||||
jsonData := strings.SplitN(jsonTag, ",", 2)
|
||||
jsonName := field.Name
|
||||
if len(jsonData) > 0 {
|
||||
jsonName = strings.TrimSpace(jsonData[0])
|
||||
}
|
||||
|
||||
preloadData := strings.Split(preloadTag, ",")
|
||||
if len(preloadData) == 0 {
|
||||
continue
|
||||
}
|
||||
if len(preloadData) == 1 {
|
||||
panic("there is no foreign field")
|
||||
}
|
||||
|
||||
localField := strings.TrimSpace(preloadData[0])
|
||||
if len(localField) == 0 {
|
||||
localField = "_id"
|
||||
}
|
||||
|
||||
foreignField := strings.TrimSpace(preloadData[1])
|
||||
if len(foreignField) == 0 {
|
||||
panic("there is no foreign field")
|
||||
}
|
||||
|
||||
preloadLimiter := 100
|
||||
preloadReversed := false
|
||||
if len(preloadData) > 2 {
|
||||
stringLimit := strings.TrimSpace(preloadData[2])
|
||||
intLimit := preloadLimiter
|
||||
|
||||
preloadReversed = strings.HasPrefix(stringLimit, "-")
|
||||
if preloadReversed {
|
||||
stringLimit = stringLimit[1:]
|
||||
}
|
||||
|
||||
intLimit, err = strconv.Atoi(stringLimit)
|
||||
if err == nil {
|
||||
preloadLimiter = intLimit
|
||||
}
|
||||
}
|
||||
|
||||
for _, preload := range preloads {
|
||||
if preload != jsonName {
|
||||
continue
|
||||
}
|
||||
|
||||
isSlice := el.Field(i).Kind() == reflect.Slice
|
||||
|
||||
typ := el.Field(i).Type()
|
||||
if typ.Kind() == reflect.Slice {
|
||||
typ = typ.Elem()
|
||||
}
|
||||
if typ.Kind() != reflect.Ptr {
|
||||
panic("preload field should have ptr type")
|
||||
}
|
||||
|
||||
lookupCollection := d.GetCollectionOf(reflect.Zero(typ).Interface())
|
||||
lookupVars := primitive.M{"selector": "$" + localField}
|
||||
lookupPipeline := primitive.A{
|
||||
primitive.M{"$match": primitive.M{"$expr": primitive.M{"$eq": primitive.A{"$" + foreignField, "$$selector"}}}},
|
||||
}
|
||||
|
||||
if preloadReversed {
|
||||
lookupPipeline = append(lookupPipeline, primitive.M{"$sort": primitive.M{"_id": -1}})
|
||||
}
|
||||
if isSlice && preloadLimiter > 0 {
|
||||
lookupPipeline = append(lookupPipeline, primitive.M{"$limit": preloadLimiter})
|
||||
} else if !isSlice {
|
||||
lookupPipeline = append(lookupPipeline, primitive.M{"$limit": 1})
|
||||
}
|
||||
|
||||
pipeline = append(pipeline, primitive.M{
|
||||
"$lookup": primitive.M{
|
||||
"from": lookupCollection.Name(),
|
||||
"let": lookupVars,
|
||||
"pipeline": lookupPipeline,
|
||||
"as": jsonName,
|
||||
},
|
||||
})
|
||||
|
||||
if isSlice {
|
||||
continue
|
||||
}
|
||||
|
||||
pipeline = append(pipeline, primitive.M{
|
||||
"$unwind": primitive.M{
|
||||
"preserveNullAndEmptyArrays": true,
|
||||
"path": "$" + jsonName,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return collection.Aggregate(d.Context(), pipeline, opts)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox/base"
|
||||
)
|
||||
|
||||
// DeleteArray removes documents list from a database by their ids
|
||||
func (d *Database) DeleteArray(target interface{}) error {
|
||||
|
||||
targetV := reflect.ValueOf(target)
|
||||
targetT := targetV.Type()
|
||||
|
||||
targetK := targetV.Kind()
|
||||
if targetK != reflect.Ptr {
|
||||
panic(fmt.Errorf("target is not a ptr"))
|
||||
}
|
||||
|
||||
targetSliceV := targetV.Elem()
|
||||
targetSliceT := targetT.Elem()
|
||||
if targetSliceT.Kind() != reflect.Slice {
|
||||
panic(fmt.Errorf("target should be a ptr to a slice"))
|
||||
}
|
||||
|
||||
targetSliceElemT := targetSliceT.Elem()
|
||||
if targetSliceElemT.Kind() != reflect.Ptr {
|
||||
panic(fmt.Errorf("target slice should contain ptrs"))
|
||||
}
|
||||
|
||||
zeroElem := reflect.Zero(targetSliceElemT)
|
||||
targetLen := targetSliceV.Len()
|
||||
collection := d.GetCollectionOf(zeroElem.Interface())
|
||||
opts := options.Delete()
|
||||
ids := primitive.A{}
|
||||
|
||||
for i := 0; i < targetLen; i++ {
|
||||
elem := targetSliceV.Index(i)
|
||||
ids = append(ids, base.GetID(elem.Interface()))
|
||||
}
|
||||
|
||||
if len(ids) == 0 {
|
||||
return fmt.Errorf("can't delete zero elements")
|
||||
}
|
||||
|
||||
result, err := collection.DeleteMany(d.Context(), primitive.M{"_id": primitive.M{"$in": ids}}, opts)
|
||||
if err != nil {
|
||||
return fmt.Errorf("can't create find and delete result: %w", err)
|
||||
}
|
||||
if result.DeletedCount != int64(targetLen) {
|
||||
return fmt.Errorf("can't verify delete result: removed count mismatch %d != %d", result.DeletedCount, targetLen)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox/base"
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox/query"
|
||||
)
|
||||
|
||||
// DeleteOne removes a document from a database and then returns it into target
|
||||
func (d *Database) DeleteOne(target interface{}, filters ...interface{}) error {
|
||||
|
||||
collection := d.GetCollectionOf(target)
|
||||
opts := &options.FindOneAndDeleteOptions{}
|
||||
composed := query.Compose(filters...)
|
||||
protected := base.GetProtection(target)
|
||||
|
||||
opts.Sort = composed.Sorter()
|
||||
|
||||
if target != nil {
|
||||
composed.And(primitive.M{"_id": base.GetID(target)})
|
||||
}
|
||||
|
||||
if protected != nil {
|
||||
query.Push(composed, protected)
|
||||
protected.X = primitive.NewObjectID()
|
||||
protected.V = time.Now().Unix()
|
||||
}
|
||||
|
||||
result := collection.FindOneAndDelete(d.Context(), composed.M(), opts)
|
||||
if result.Err() != nil {
|
||||
return fmt.Errorf("can't create find one and delete result: %w", result.Err())
|
||||
}
|
||||
|
||||
err := result.Decode(target)
|
||||
if err == mongo.ErrNoDocuments {
|
||||
return err
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("can't decode result: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox/base"
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox/query"
|
||||
)
|
||||
|
||||
// LoadArray loads an array of documents from the database by query
|
||||
func (d *Database) LoadArray(target interface{}, filters ...interface{}) error {
|
||||
|
||||
targetV := reflect.ValueOf(target)
|
||||
targetT := targetV.Type()
|
||||
|
||||
targetK := targetV.Kind()
|
||||
if targetK != reflect.Ptr {
|
||||
panic(fmt.Errorf("target is not a ptr"))
|
||||
}
|
||||
|
||||
targetSliceV := targetV.Elem()
|
||||
targetSliceT := targetT.Elem()
|
||||
if targetSliceT.Kind() != reflect.Slice {
|
||||
panic(fmt.Errorf("target should be a ptr to a slice"))
|
||||
}
|
||||
|
||||
targetSliceElemT := targetSliceT.Elem()
|
||||
if targetSliceElemT.Kind() != reflect.Ptr {
|
||||
panic(fmt.Errorf("target slice should contain ptrs"))
|
||||
}
|
||||
|
||||
composed := query.Compose(filters...)
|
||||
zeroElem := reflect.Zero(targetSliceElemT)
|
||||
hasPreloader, _ := composed.Preloader()
|
||||
|
||||
var result *mongo.Cursor
|
||||
var err error
|
||||
|
||||
if hasPreloader {
|
||||
result, err = d.createAggregateLoad(zeroElem.Interface(), composed)
|
||||
} else {
|
||||
result, err = d.createSimpleLoad(zeroElem.Interface(), composed)
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("can't create find result: %w", err)
|
||||
}
|
||||
|
||||
defer result.Close(d.Context())
|
||||
var i int
|
||||
|
||||
for i = 0; result.Next(d.Context()); {
|
||||
if targetSliceV.Len() == i {
|
||||
elem := reflect.New(targetSliceElemT.Elem())
|
||||
if err = result.Decode(elem.Interface()); err == nil {
|
||||
targetSliceV = reflect.Append(targetSliceV, elem)
|
||||
} else {
|
||||
continue
|
||||
}
|
||||
} else {
|
||||
elem := targetSliceV.Index(i).Interface()
|
||||
base.Reset(elem)
|
||||
if err = result.Decode(elem); err != nil {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
i++
|
||||
}
|
||||
|
||||
targetSliceV = targetSliceV.Slice(0, i)
|
||||
targetV.Elem().Set(targetSliceV)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox/base"
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox/query"
|
||||
)
|
||||
|
||||
// LoadOne function loads a first single target document by a query
|
||||
func (d *Database) LoadOne(target interface{}, filters ...interface{}) error {
|
||||
|
||||
composed := query.Compose(append(filters, query.Limit(1))...)
|
||||
hasPreloader, _ := composed.Preloader()
|
||||
|
||||
var result *mongo.Cursor
|
||||
var err error
|
||||
|
||||
if hasPreloader {
|
||||
result, err = d.createAggregateLoad(target, composed)
|
||||
} else {
|
||||
result, err = d.createSimpleLoad(target, composed)
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("can't create find result: %w", err)
|
||||
}
|
||||
|
||||
hasNext := result.Next(d.Context())
|
||||
if result.Err() != nil {
|
||||
return err
|
||||
}
|
||||
if !hasNext {
|
||||
return mongo.ErrNoDocuments
|
||||
}
|
||||
|
||||
base.Reset(target)
|
||||
|
||||
return result.Decode(target)
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox"
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox/query"
|
||||
)
|
||||
|
||||
// LoadStream function loads documents one by one into a target channel
|
||||
func (d *Database) LoadStream(target interface{}, filters ...interface{}) (mongox.StreamLoader, error) {
|
||||
|
||||
var cursor *mongo.Cursor
|
||||
var err error
|
||||
|
||||
composed := query.Compose(filters...)
|
||||
hasPreloader, _ := composed.Preloader()
|
||||
|
||||
if hasPreloader {
|
||||
cursor, err = d.createAggregateLoad(target, composed)
|
||||
} else {
|
||||
cursor, err = d.createSimpleLoad(target, composed)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("can't create find result: %w", err)
|
||||
}
|
||||
|
||||
l := &StreamLoader{Cursor: cursor, ctx: d.Context(), target: target}
|
||||
|
||||
return l, nil
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox/base"
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox/query"
|
||||
)
|
||||
|
||||
// SaveOne saves a single source document to the database
|
||||
func (d *Database) SaveOne(source interface{}) error {
|
||||
|
||||
collection := d.GetCollectionOf(source)
|
||||
opts := options.FindOneAndReplace()
|
||||
id := base.GetID(source)
|
||||
protected := base.GetProtection(source)
|
||||
composed := query.Compose(bson.M{"_id": id})
|
||||
|
||||
opts.SetUpsert(true)
|
||||
opts.SetReturnDocument(options.After)
|
||||
|
||||
if protected != nil {
|
||||
query.Push(composed, protected)
|
||||
protected.X = primitive.NewObjectID()
|
||||
protected.V = time.Now().Unix()
|
||||
}
|
||||
|
||||
result := collection.FindOneAndReplace(d.Context(), composed.M(), source, opts)
|
||||
if result.Err() != nil {
|
||||
return result.Err()
|
||||
}
|
||||
|
||||
return result.Decode(source)
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox/base"
|
||||
)
|
||||
|
||||
// StreamLoader is a controller for a database cursor
|
||||
type StreamLoader struct {
|
||||
*mongo.Cursor
|
||||
ctx context.Context
|
||||
target interface{}
|
||||
}
|
||||
|
||||
// DecodeNext loads next documents to a target or returns an error
|
||||
func (l *StreamLoader) DecodeNext() error {
|
||||
|
||||
hasNext := l.Cursor.Next(l.ctx)
|
||||
|
||||
if l.Cursor.Err() != nil {
|
||||
return l.Cursor.Err()
|
||||
}
|
||||
if !hasNext {
|
||||
return mongo.ErrNoDocuments
|
||||
}
|
||||
|
||||
base.Reset(l.target)
|
||||
|
||||
err := l.Cursor.Decode(l.target)
|
||||
if err != nil {
|
||||
return fmt.Errorf("can't decode desult: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *StreamLoader) Decode() error {
|
||||
|
||||
base.Reset(l.target)
|
||||
|
||||
err := l.Cursor.Decode(l.target)
|
||||
if err != nil {
|
||||
return fmt.Errorf("can't decode desult: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Next loads next documents but doesn't perform decoding
|
||||
func (l *StreamLoader) Next() error {
|
||||
|
||||
hasNext := l.Cursor.Next(l.ctx)
|
||||
|
||||
if l.Cursor.Err() != nil {
|
||||
return l.Cursor.Err()
|
||||
}
|
||||
if !hasNext {
|
||||
return mongo.ErrNoDocuments
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Close cursor
|
||||
func (l *StreamLoader) Close() error {
|
||||
|
||||
return l.Cursor.Close(l.ctx)
|
||||
}
|
||||
Reference in New Issue
Block a user