mirror of
https://github.com/mainnika/mongox-go-driver.git
synced 2026-05-23 00:03:36 +00:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6111341a3c | |||
| 9f647ca094 | |||
| eeb83daf4b | |||
| 6e6a042a16 | |||
| e0c26f770f | |||
| e7a05d94e1 |
@@ -6,6 +6,7 @@ import (
|
|||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
|
|
||||||
"github.com/mainnika/mongox-go-driver/v2/mongox"
|
"github.com/mainnika/mongox-go-driver/v2/mongox"
|
||||||
|
"github.com/mainnika/mongox-go-driver/v2/mongox/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetID returns source document id
|
// GetID returns source document id
|
||||||
@@ -62,7 +63,7 @@ func getObjectOrPanic(source mongox.JSONBased) (id primitive.D) {
|
|||||||
func getInterfaceOrPanic(source mongox.InterfaceBased) (id interface{}) {
|
func getInterfaceOrPanic(source mongox.InterfaceBased) (id interface{}) {
|
||||||
|
|
||||||
id = source.GetID()
|
id = source.GetID()
|
||||||
if id != nil {
|
if !utils.IsNil(id) {
|
||||||
return id
|
return id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -19,6 +19,9 @@ func GetProtection(source interface{}) *protection.Key {
|
|||||||
|
|
||||||
for i := 0; i < numField; i++ {
|
for i := 0; i < numField; i++ {
|
||||||
field := el.Field(i)
|
field := el.Field(i)
|
||||||
|
if !field.CanInterface() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
switch field.Interface().(type) {
|
switch field.Interface().(type) {
|
||||||
case *protection.Key:
|
case *protection.Key:
|
||||||
|
|||||||
@@ -3,9 +3,9 @@ package database
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
|
||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
|
|
||||||
|
"github.com/mainnika/mongox-go-driver/v2/mongox"
|
||||||
"github.com/mainnika/mongox-go-driver/v2/mongox/query"
|
"github.com/mainnika/mongox-go-driver/v2/mongox/query"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -21,7 +21,7 @@ func (d *Database) Count(target interface{}, filters ...interface{}) (int64, err
|
|||||||
opts.Skip = composed.Skipper()
|
opts.Skip = composed.Skipper()
|
||||||
|
|
||||||
result, err := collection.CountDocuments(d.Context(), composed.M(), opts)
|
result, err := collection.CountDocuments(d.Context(), composed.M(), opts)
|
||||||
if err == mongo.ErrNoDocuments {
|
if err == mongox.ErrNoDocuments {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
|
||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
|
|
||||||
"github.com/mainnika/mongox-go-driver/v2/mongox"
|
"github.com/mainnika/mongox-go-driver/v2/mongox"
|
||||||
@@ -17,13 +16,13 @@ import (
|
|||||||
|
|
||||||
// Database handler
|
// Database handler
|
||||||
type Database struct {
|
type Database struct {
|
||||||
client *mongo.Client
|
client *mongox.Client
|
||||||
dbname string
|
dbname string
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewDatabase function creates new database instance with mongo client and empty context
|
// NewDatabase function creates new database instance with mongo client and empty context
|
||||||
func NewDatabase(client *mongo.Client, dbname string) mongox.Database {
|
func NewDatabase(client *mongox.Client, dbname string) mongox.Database {
|
||||||
|
|
||||||
db := &Database{}
|
db := &Database{}
|
||||||
db.client = client
|
db.client = client
|
||||||
@@ -33,13 +32,19 @@ func NewDatabase(client *mongo.Client, dbname string) mongox.Database {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Client function returns a mongo client
|
// Client function returns a mongo client
|
||||||
func (d *Database) Client() *mongo.Client {
|
func (d *Database) Client() *mongox.Client {
|
||||||
return d.client
|
return d.client
|
||||||
}
|
}
|
||||||
|
|
||||||
// Context function returns a context
|
// Context function returns a context
|
||||||
func (d *Database) Context() context.Context {
|
func (d *Database) Context() (ctx context.Context) {
|
||||||
return d.ctx
|
|
||||||
|
ctx = d.ctx
|
||||||
|
if ctx == nil {
|
||||||
|
ctx = context.Background()
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Name function returns a database name
|
// Name function returns a database name
|
||||||
@@ -67,7 +72,7 @@ func (d *Database) New(ctx context.Context) mongox.Database {
|
|||||||
// base.ObjectID `bson:",inline" json:",inline" collection:"foobars"`
|
// base.ObjectID `bson:",inline" json:",inline" collection:"foobars"`
|
||||||
// ...
|
// ...
|
||||||
// Will panic if there is no «collection» tag
|
// Will panic if there is no «collection» tag
|
||||||
func (d *Database) GetCollectionOf(document interface{}) *mongo.Collection {
|
func (d *Database) GetCollectionOf(document interface{}) *mongox.Collection {
|
||||||
|
|
||||||
el := reflect.TypeOf(document).Elem()
|
el := reflect.TypeOf(document).Elem()
|
||||||
numField := el.NumField()
|
numField := el.NumField()
|
||||||
@@ -86,7 +91,7 @@ func (d *Database) GetCollectionOf(document interface{}) *mongo.Collection {
|
|||||||
panic(fmt.Errorf("document %v does not have a collection tag", document))
|
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) {
|
func (d *Database) createSimpleLoad(target interface{}, composed *query.Query) (cursor *mongox.Cursor, err error) {
|
||||||
|
|
||||||
collection := d.GetCollectionOf(target)
|
collection := d.GetCollectionOf(target)
|
||||||
opts := options.Find()
|
opts := options.Find()
|
||||||
@@ -98,7 +103,7 @@ func (d *Database) createSimpleLoad(target interface{}, composed *query.Query) (
|
|||||||
return collection.Find(d.Context(), composed.M(), opts)
|
return collection.Find(d.Context(), composed.M(), opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Database) createAggregateLoad(target interface{}, composed *query.Query) (cursor *mongo.Cursor, err error) {
|
func (d *Database) createAggregateLoad(target interface{}, composed *query.Query) (cursor *mongox.Cursor, err error) {
|
||||||
|
|
||||||
collection := d.GetCollectionOf(target)
|
collection := d.GetCollectionOf(target)
|
||||||
opts := options.Aggregate()
|
opts := options.Aggregate()
|
||||||
|
|||||||
@@ -5,11 +5,12 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
|
||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
|
|
||||||
|
"github.com/mainnika/mongox-go-driver/v2/mongox"
|
||||||
"github.com/mainnika/mongox-go-driver/v2/mongox/base"
|
"github.com/mainnika/mongox-go-driver/v2/mongox/base"
|
||||||
"github.com/mainnika/mongox-go-driver/v2/mongox/query"
|
"github.com/mainnika/mongox-go-driver/v2/mongox/query"
|
||||||
|
"github.com/mainnika/mongox-go-driver/v2/mongox/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DeleteOne removes a document from a database and then returns it into target
|
// DeleteOne removes a document from a database and then returns it into target
|
||||||
@@ -22,7 +23,7 @@ func (d *Database) DeleteOne(target interface{}, filters ...interface{}) error {
|
|||||||
|
|
||||||
opts.Sort = composed.Sorter()
|
opts.Sort = composed.Sorter()
|
||||||
|
|
||||||
if target != nil {
|
if !utils.IsNil(target) {
|
||||||
composed.And(primitive.M{"_id": base.GetID(target)})
|
composed.And(primitive.M{"_id": base.GetID(target)})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,7 +39,7 @@ func (d *Database) DeleteOne(target interface{}, filters ...interface{}) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
err := result.Decode(target)
|
err := result.Decode(target)
|
||||||
if err == mongo.ErrNoDocuments {
|
if err == mongox.ErrNoDocuments {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -0,0 +1,134 @@
|
|||||||
|
package database
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"text/template"
|
||||||
|
|
||||||
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
|
)
|
||||||
|
|
||||||
|
// IndexEnsure function ensures index in mongo collection of document
|
||||||
|
// `index:""` -- https://docs.mongodb.com/manual/indexes/#create-an-index
|
||||||
|
// `index:"-"` -- (descending)
|
||||||
|
// `index:"-,+foo,+-bar"` -- https://docs.mongodb.com/manual/core/index-compound
|
||||||
|
// `index:"-,+foo,+-bar,unique"` -- https://docs.mongodb.com/manual/core/index-unique
|
||||||
|
// `index:"-,+foo,+-bar,unique,allowNull"` -- https://docs.mongodb.com/manual/core/index-partial
|
||||||
|
// `index:"-,unique,allowNull,expireAfter=86400"` -- https://docs.mongodb.com/manual/core/index-ttl
|
||||||
|
// `index:"-,unique,allowNull,expireAfter={{.Expire}}"` -- evaluate index as a golang template with `cfg` arguments
|
||||||
|
func (d *Database) IndexEnsure(cfg interface{}, document interface{}) error {
|
||||||
|
|
||||||
|
el := reflect.ValueOf(document).Elem().Type()
|
||||||
|
numField := el.NumField()
|
||||||
|
documents := d.GetCollectionOf(document)
|
||||||
|
|
||||||
|
for i := 0; i < numField; i++ {
|
||||||
|
|
||||||
|
field := el.Field(i)
|
||||||
|
tag := field.Tag
|
||||||
|
|
||||||
|
indexTag, ok := tag.Lookup("index")
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
bsonTag, ok := tag.Lookup("bson")
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("bson tag is not defined for field:%v document:%v", field, document)
|
||||||
|
}
|
||||||
|
|
||||||
|
tmpBuffer := &bytes.Buffer{}
|
||||||
|
tpl, err := template.New("").Parse(indexTag)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Errorf("invalid prop template, %v", indexTag))
|
||||||
|
}
|
||||||
|
err = tpl.Execute(tmpBuffer, cfg)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Errorf("failed to evaluate prop template, %v", indexTag))
|
||||||
|
}
|
||||||
|
|
||||||
|
indexString := tmpBuffer.String()
|
||||||
|
indexValues := strings.Split(indexString, ",")
|
||||||
|
bsonValues := strings.Split(bsonTag, ",")
|
||||||
|
|
||||||
|
var f = false
|
||||||
|
var t = true
|
||||||
|
var key = bsonValues[0]
|
||||||
|
var name = fmt.Sprintf("%s_%s_", indexString, key)
|
||||||
|
|
||||||
|
if len(key) == 0 {
|
||||||
|
panic(fmt.Errorf("cannot evaluate index key"))
|
||||||
|
}
|
||||||
|
|
||||||
|
index := primitive.M{key: 1}
|
||||||
|
opts := &options.IndexOptions{
|
||||||
|
Background: &f,
|
||||||
|
Unique: &f,
|
||||||
|
Name: &name,
|
||||||
|
}
|
||||||
|
|
||||||
|
if indexValues[0] == "-" {
|
||||||
|
index[key] = -1
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, prop := range indexValues[1:] {
|
||||||
|
var left string
|
||||||
|
var right string
|
||||||
|
|
||||||
|
pair := strings.SplitN(prop, "=", 2)
|
||||||
|
left = pair[0]
|
||||||
|
if len(pair) > 1 {
|
||||||
|
right = pair[1]
|
||||||
|
}
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case left == "unique":
|
||||||
|
opts.Unique = &t
|
||||||
|
|
||||||
|
case left == "allowNull":
|
||||||
|
expression, isMap := opts.PartialFilterExpression.(primitive.M)
|
||||||
|
if !isMap || expression == nil {
|
||||||
|
expression = primitive.M{}
|
||||||
|
}
|
||||||
|
|
||||||
|
expression[key] = primitive.M{"$exists": true}
|
||||||
|
opts.PartialFilterExpression = expression
|
||||||
|
|
||||||
|
case left == "expireAfter":
|
||||||
|
expireAfter, err := strconv.Atoi(right)
|
||||||
|
if err != nil || expireAfter < 1 {
|
||||||
|
panic(fmt.Errorf("invalid expireAfter value, err: %w", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
expireAfterInt32 := int32(expireAfter)
|
||||||
|
opts.ExpireAfterSeconds = &expireAfterInt32
|
||||||
|
|
||||||
|
case len(left) > 0 && left[0] == '+':
|
||||||
|
compoundValue := left[1:]
|
||||||
|
if len(compoundValue) == 0 {
|
||||||
|
panic(fmt.Errorf("invalid compound value"))
|
||||||
|
}
|
||||||
|
|
||||||
|
if compoundValue[0] == '-' {
|
||||||
|
index[compoundValue[1:]] = -1
|
||||||
|
} else {
|
||||||
|
index[compoundValue] = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
panic(fmt.Errorf("unsupported flag:%q in tag:%q of type:%s", prop, tag, el))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = documents.Indexes().CreateOne(d.Context(), mongo.IndexModel{Keys: index, Options: opts})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,156 @@
|
|||||||
|
package database_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
|
"github.com/mainnika/mongox-go-driver/v2/mongox-testing/database"
|
||||||
|
"github.com/mainnika/mongox-go-driver/v2/mongox/base/oidbased"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestDatabase_Ensure(t *testing.T) {
|
||||||
|
|
||||||
|
db, err := database.NewEphemeral("mongodb://localhost")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
testvalues := []struct {
|
||||||
|
doc interface{}
|
||||||
|
settings map[string]interface{}
|
||||||
|
index map[string]interface{}
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
doc: &struct {
|
||||||
|
oidbased.Primary `bson:",inline" json:",inline" collection:"1"`
|
||||||
|
|
||||||
|
Foobar int `bson:"foobar" json:"foobar" index:"-,unique,allowNull,expireAfter=86400"`
|
||||||
|
Foo int `bson:"foo" json:"foo"`
|
||||||
|
Bar int `bson:"bar" json:"bar"`
|
||||||
|
}{},
|
||||||
|
index: map[string]interface{}{
|
||||||
|
"background": false,
|
||||||
|
"expireAfterSeconds": int32(86400),
|
||||||
|
"key": map[string]interface{}{
|
||||||
|
"foobar": int32(-1),
|
||||||
|
},
|
||||||
|
"name": "-,unique,allowNull,expireAfter=86400_foobar_",
|
||||||
|
"partialFilterExpression": map[string]interface{}{
|
||||||
|
"foobar": map[string]interface{}{"$exists": true},
|
||||||
|
},
|
||||||
|
"unique": true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
doc: &struct {
|
||||||
|
oidbased.Primary `bson:",inline" json:",inline" collection:"2"`
|
||||||
|
|
||||||
|
Foobar int `bson:"foobar" json:"foobar" index:",unique"`
|
||||||
|
}{},
|
||||||
|
index: map[string]interface{}{
|
||||||
|
"background": false,
|
||||||
|
"key": map[string]interface{}{
|
||||||
|
"foobar": int32(1),
|
||||||
|
},
|
||||||
|
"name": ",unique_foobar_",
|
||||||
|
"unique": true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
doc: &struct {
|
||||||
|
oidbased.Primary `bson:",inline" json:",inline" collection:"3"`
|
||||||
|
|
||||||
|
Foobar int `bson:"foobar" json:"foobar" index:"-,+foo,+-bar,unique,allowNull"`
|
||||||
|
Foo int `bson:"foo" json:"foo"`
|
||||||
|
Bar int `bson:"bar" json:"bar"`
|
||||||
|
}{},
|
||||||
|
index: map[string]interface{}{
|
||||||
|
"background": false,
|
||||||
|
"key": map[string]interface{}{
|
||||||
|
"foobar": int32(-1),
|
||||||
|
"foo": int32(1),
|
||||||
|
"bar": int32(-1),
|
||||||
|
},
|
||||||
|
"name": "-,+foo,+-bar,unique,allowNull_foobar_",
|
||||||
|
"partialFilterExpression": map[string]interface{}{
|
||||||
|
"foobar": map[string]interface{}{"$exists": true},
|
||||||
|
},
|
||||||
|
"unique": true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
doc: &struct {
|
||||||
|
oidbased.Primary `bson:",inline" json:",inline" collection:"4"`
|
||||||
|
|
||||||
|
Foobar int `bson:"foobar" json:"foobar" index:""`
|
||||||
|
Foo int `bson:"foo" json:"foo"`
|
||||||
|
Bar int `bson:"bar" json:"bar"`
|
||||||
|
}{},
|
||||||
|
index: map[string]interface{}{
|
||||||
|
"background": false,
|
||||||
|
"key": map[string]interface{}{
|
||||||
|
"foobar": int32(1),
|
||||||
|
},
|
||||||
|
"name": "_foobar_",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
doc: &struct {
|
||||||
|
oidbased.Primary `bson:",inline" json:",inline" collection:"5"`
|
||||||
|
|
||||||
|
Foobar int `bson:"foobar" json:"foobar" index:"-"`
|
||||||
|
Foo int `bson:"foo" json:"foo"`
|
||||||
|
Bar int `bson:"bar" json:"bar"`
|
||||||
|
}{},
|
||||||
|
index: map[string]interface{}{
|
||||||
|
"background": false,
|
||||||
|
"key": map[string]interface{}{
|
||||||
|
"foobar": int32(-1),
|
||||||
|
},
|
||||||
|
"name": "-_foobar_",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
doc: &struct {
|
||||||
|
oidbased.Primary `bson:",inline" json:",inline" collection:"1"`
|
||||||
|
|
||||||
|
Foobar int `bson:"foobar" json:"foobar" index:"-,unique,allowNull,expireAfter={{.Expire}}"`
|
||||||
|
Foo int `bson:"foo" json:"foo"`
|
||||||
|
Bar int `bson:"bar" json:"bar"`
|
||||||
|
}{},
|
||||||
|
settings: map[string]interface{}{
|
||||||
|
"Expire": 86400,
|
||||||
|
},
|
||||||
|
index: map[string]interface{}{
|
||||||
|
"background": false,
|
||||||
|
"expireAfterSeconds": int32(86400),
|
||||||
|
"key": map[string]interface{}{
|
||||||
|
"foobar": int32(-1),
|
||||||
|
},
|
||||||
|
"name": "-,unique,allowNull,expireAfter=86400_foobar_",
|
||||||
|
"partialFilterExpression": map[string]interface{}{
|
||||||
|
"foobar": map[string]interface{}{"$exists": true},
|
||||||
|
},
|
||||||
|
"unique": true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range testvalues {
|
||||||
|
err = db.IndexEnsure(tt.settings, tt.doc)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
indexes, _ := db.GetCollectionOf(tt.doc).Indexes().List(db.Context())
|
||||||
|
index := new(map[string]interface{})
|
||||||
|
|
||||||
|
indexes.Next(db.Context()) // skip _id_
|
||||||
|
indexes.Next(db.Context())
|
||||||
|
indexes.Decode(&index)
|
||||||
|
|
||||||
|
for k, v := range tt.index {
|
||||||
|
assert.Equal(t, v, (*index)[k])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,8 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"github.com/mainnika/mongox-go-driver/v2/mongox"
|
||||||
|
|
||||||
"github.com/mainnika/mongox-go-driver/v2/mongox/base"
|
"github.com/mainnika/mongox-go-driver/v2/mongox/base"
|
||||||
"github.com/mainnika/mongox-go-driver/v2/mongox/query"
|
"github.com/mainnika/mongox-go-driver/v2/mongox/query"
|
||||||
)
|
)
|
||||||
@@ -36,7 +35,7 @@ func (d *Database) LoadArray(target interface{}, filters ...interface{}) error {
|
|||||||
zeroElem := reflect.Zero(targetSliceElemT)
|
zeroElem := reflect.Zero(targetSliceElemT)
|
||||||
hasPreloader, _ := composed.Preloader()
|
hasPreloader, _ := composed.Preloader()
|
||||||
|
|
||||||
var result *mongo.Cursor
|
var result *mongox.Cursor
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
if hasPreloader {
|
if hasPreloader {
|
||||||
|
|||||||
@@ -3,8 +3,7 @@ package database
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"github.com/mainnika/mongox-go-driver/v2/mongox"
|
||||||
|
|
||||||
"github.com/mainnika/mongox-go-driver/v2/mongox/base"
|
"github.com/mainnika/mongox-go-driver/v2/mongox/base"
|
||||||
"github.com/mainnika/mongox-go-driver/v2/mongox/query"
|
"github.com/mainnika/mongox-go-driver/v2/mongox/query"
|
||||||
)
|
)
|
||||||
@@ -15,7 +14,7 @@ func (d *Database) LoadOne(target interface{}, filters ...interface{}) error {
|
|||||||
composed := query.Compose(append(filters, query.Limit(1))...)
|
composed := query.Compose(append(filters, query.Limit(1))...)
|
||||||
hasPreloader, _ := composed.Preloader()
|
hasPreloader, _ := composed.Preloader()
|
||||||
|
|
||||||
var result *mongo.Cursor
|
var result *mongox.Cursor
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
if hasPreloader {
|
if hasPreloader {
|
||||||
@@ -32,7 +31,7 @@ func (d *Database) LoadOne(target interface{}, filters ...interface{}) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if !hasNext {
|
if !hasNext {
|
||||||
return mongo.ErrNoDocuments
|
return mongox.ErrNoDocuments
|
||||||
}
|
}
|
||||||
|
|
||||||
base.Reset(target)
|
base.Reset(target)
|
||||||
|
|||||||
@@ -3,8 +3,6 @@ package database
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
|
||||||
|
|
||||||
"github.com/mainnika/mongox-go-driver/v2/mongox"
|
"github.com/mainnika/mongox-go-driver/v2/mongox"
|
||||||
"github.com/mainnika/mongox-go-driver/v2/mongox/query"
|
"github.com/mainnika/mongox-go-driver/v2/mongox/query"
|
||||||
)
|
)
|
||||||
@@ -12,7 +10,7 @@ import (
|
|||||||
// LoadStream function loads documents one by one into a target channel
|
// LoadStream function loads documents one by one into a target channel
|
||||||
func (d *Database) LoadStream(target interface{}, filters ...interface{}) (mongox.StreamLoader, error) {
|
func (d *Database) LoadStream(target interface{}, filters ...interface{}) (mongox.StreamLoader, error) {
|
||||||
|
|
||||||
var cursor *mongo.Cursor
|
var cursor *mongox.Cursor
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
composed := query.Compose(filters...)
|
composed := query.Compose(filters...)
|
||||||
@@ -27,7 +25,7 @@ func (d *Database) LoadStream(target interface{}, filters ...interface{}) (mongo
|
|||||||
return nil, fmt.Errorf("can't create find result: %w", err)
|
return nil, fmt.Errorf("can't create find result: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
l := &StreamLoader{Cursor: cursor, ctx: d.Context(), target: target}
|
l := &StreamLoader{cur: cursor, ctx: d.Context(), target: target}
|
||||||
|
|
||||||
return l, nil
|
return l, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,14 +4,13 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"github.com/mainnika/mongox-go-driver/v2/mongox"
|
||||||
|
|
||||||
"github.com/mainnika/mongox-go-driver/v2/mongox/base"
|
"github.com/mainnika/mongox-go-driver/v2/mongox/base"
|
||||||
)
|
)
|
||||||
|
|
||||||
// StreamLoader is a controller for a database cursor
|
// StreamLoader is a controller for a database cursor
|
||||||
type StreamLoader struct {
|
type StreamLoader struct {
|
||||||
*mongo.Cursor
|
cur *mongox.Cursor
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
target interface{}
|
target interface{}
|
||||||
}
|
}
|
||||||
@@ -19,18 +18,18 @@ type StreamLoader struct {
|
|||||||
// DecodeNext loads next documents to a target or returns an error
|
// DecodeNext loads next documents to a target or returns an error
|
||||||
func (l *StreamLoader) DecodeNext() error {
|
func (l *StreamLoader) DecodeNext() error {
|
||||||
|
|
||||||
hasNext := l.Cursor.Next(l.ctx)
|
hasNext := l.cur.Next(l.ctx)
|
||||||
|
|
||||||
if l.Cursor.Err() != nil {
|
if l.cur.Err() != nil {
|
||||||
return l.Cursor.Err()
|
return l.cur.Err()
|
||||||
}
|
}
|
||||||
if !hasNext {
|
if !hasNext {
|
||||||
return mongo.ErrNoDocuments
|
return mongox.ErrNoDocuments
|
||||||
}
|
}
|
||||||
|
|
||||||
base.Reset(l.target)
|
base.Reset(l.target)
|
||||||
|
|
||||||
err := l.Cursor.Decode(l.target)
|
err := l.cur.Decode(l.target)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("can't decode desult: %w", err)
|
return fmt.Errorf("can't decode desult: %w", err)
|
||||||
}
|
}
|
||||||
@@ -43,7 +42,7 @@ func (l *StreamLoader) Decode() error {
|
|||||||
|
|
||||||
base.Reset(l.target)
|
base.Reset(l.target)
|
||||||
|
|
||||||
err := l.Cursor.Decode(l.target)
|
err := l.cur.Decode(l.target)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("can't decode desult: %w", err)
|
return fmt.Errorf("can't decode desult: %w", err)
|
||||||
}
|
}
|
||||||
@@ -54,20 +53,27 @@ func (l *StreamLoader) Decode() error {
|
|||||||
// Next loads next documents but doesn't perform decoding
|
// Next loads next documents but doesn't perform decoding
|
||||||
func (l *StreamLoader) Next() error {
|
func (l *StreamLoader) Next() error {
|
||||||
|
|
||||||
hasNext := l.Cursor.Next(l.ctx)
|
hasNext := l.cur.Next(l.ctx)
|
||||||
|
|
||||||
if l.Cursor.Err() != nil {
|
if l.cur.Err() != nil {
|
||||||
return l.Cursor.Err()
|
return l.cur.Err()
|
||||||
}
|
}
|
||||||
if !hasNext {
|
if !hasNext {
|
||||||
return mongo.ErrNoDocuments
|
return mongox.ErrNoDocuments
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l *StreamLoader) Cursor() *mongox.Cursor {
|
||||||
|
return l.cur
|
||||||
|
}
|
||||||
|
|
||||||
// Close cursor
|
// Close cursor
|
||||||
func (l *StreamLoader) Close() error {
|
func (l *StreamLoader) Close() error {
|
||||||
|
return l.cur.Close(l.ctx)
|
||||||
return l.Cursor.Close(l.ctx)
|
}
|
||||||
|
|
||||||
|
func (l *StreamLoader) Err() error {
|
||||||
|
return l.cur.Err()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package mongox
|
||||||
|
|
||||||
|
import (
|
||||||
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Reexported mongo errors
|
||||||
|
var (
|
||||||
|
ErrMissingResumeToken = mongo.ErrMissingResumeToken
|
||||||
|
ErrNilCursor = mongo.ErrNilCursor
|
||||||
|
ErrUnacknowledgedWrite = mongo.ErrUnacknowledgedWrite
|
||||||
|
ErrClientDisconnected = mongo.ErrClientDisconnected
|
||||||
|
ErrNilDocument = mongo.ErrNilDocument
|
||||||
|
ErrEmptySlice = mongo.ErrEmptySlice
|
||||||
|
ErrInvalidIndexValue = mongo.ErrInvalidIndexValue
|
||||||
|
ErrNonStringIndexName = mongo.ErrNonStringIndexName
|
||||||
|
ErrMultipleIndexDrop = mongo.ErrMultipleIndexDrop
|
||||||
|
ErrWrongClient = mongo.ErrWrongClient
|
||||||
|
ErrNoDocuments = mongo.ErrNoDocuments
|
||||||
|
)
|
||||||
+11
-2
@@ -7,13 +7,20 @@ import (
|
|||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Reexport basic mongo structs
|
||||||
|
type (
|
||||||
|
Cursor = mongo.Cursor
|
||||||
|
Client = mongo.Client
|
||||||
|
Collection = mongo.Collection
|
||||||
|
)
|
||||||
|
|
||||||
// Database is the mongox database interface
|
// Database is the mongox database interface
|
||||||
type Database interface {
|
type Database interface {
|
||||||
Client() *mongo.Client
|
Client() *Client
|
||||||
Context() context.Context
|
Context() context.Context
|
||||||
Name() string
|
Name() string
|
||||||
New(ctx context.Context) Database
|
New(ctx context.Context) Database
|
||||||
GetCollectionOf(document interface{}) *mongo.Collection
|
GetCollectionOf(document interface{}) *Collection
|
||||||
Count(target interface{}, filters ...interface{}) (int64, error)
|
Count(target interface{}, filters ...interface{}) (int64, error)
|
||||||
DeleteArray(target interface{}) error
|
DeleteArray(target interface{}) error
|
||||||
DeleteOne(target interface{}, filters ...interface{}) error
|
DeleteOne(target interface{}, filters ...interface{}) error
|
||||||
@@ -21,10 +28,12 @@ type Database interface {
|
|||||||
LoadOne(target interface{}, filters ...interface{}) error
|
LoadOne(target interface{}, filters ...interface{}) error
|
||||||
LoadStream(target interface{}, filters ...interface{}) (StreamLoader, error)
|
LoadStream(target interface{}, filters ...interface{}) (StreamLoader, error)
|
||||||
SaveOne(source interface{}) error
|
SaveOne(source interface{}) error
|
||||||
|
IndexEnsure(cfg interface{}, document interface{}) error
|
||||||
}
|
}
|
||||||
|
|
||||||
// StreamLoader is a interface to control database cursor
|
// StreamLoader is a interface to control database cursor
|
||||||
type StreamLoader interface {
|
type StreamLoader interface {
|
||||||
|
Cursor() *Cursor
|
||||||
DecodeNext() error
|
DecodeNext() error
|
||||||
Decode() error
|
Decode() error
|
||||||
Next() error
|
Next() error
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
|
|
||||||
"github.com/mainnika/mongox-go-driver/v2/mongox/base/protection"
|
"github.com/mainnika/mongox-go-driver/v2/mongox/base/protection"
|
||||||
|
"github.com/mainnika/mongox-go-driver/v2/mongox/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Compose is a function to compose filters into a single query
|
// Compose is a function to compose filters into a single query
|
||||||
@@ -26,6 +27,10 @@ func Compose(filters ...interface{}) *Query {
|
|||||||
// Push applies single filter to a query
|
// Push applies single filter to a query
|
||||||
func Push(q *Query, f interface{}) bool {
|
func Push(q *Query, f interface{}) bool {
|
||||||
|
|
||||||
|
if utils.IsNil(f) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
ok := false
|
ok := false
|
||||||
ok = ok || applyBson(q, f)
|
ok = ok || applyBson(q, f)
|
||||||
ok = ok || applyLimit(q, f)
|
ok = ok || applyLimit(q, f)
|
||||||
@@ -91,9 +96,6 @@ func applyProtection(q *Query, f interface{}) bool {
|
|||||||
x = &f.X
|
x = &f.X
|
||||||
v = &f.V
|
v = &f.V
|
||||||
case *protection.Key:
|
case *protection.Key:
|
||||||
if f == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
x = &f.X
|
x = &f.X
|
||||||
v = &f.V
|
v = &f.V
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
// IsNil function evaluates the interface value to nil
|
||||||
|
func IsNil(i interface{}) bool {
|
||||||
|
|
||||||
|
type iface struct {
|
||||||
|
_ *interface{}
|
||||||
|
ptr unsafe.Pointer
|
||||||
|
}
|
||||||
|
|
||||||
|
unpacked := (*iface)(unsafe.Pointer(&i))
|
||||||
|
if unpacked.ptr == nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return *(*unsafe.Pointer)(unpacked.ptr) == nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestIsNil(t *testing.T) {
|
||||||
|
|
||||||
|
testvalues := []struct {
|
||||||
|
i interface{}
|
||||||
|
isnil bool
|
||||||
|
}{
|
||||||
|
{nil, true},
|
||||||
|
{(*string)(nil), true},
|
||||||
|
{([]string)(nil), true},
|
||||||
|
{(map[string]string)(nil), true},
|
||||||
|
{(func() bool)(nil), true},
|
||||||
|
{(chan func() bool)(nil), true},
|
||||||
|
{"", true},
|
||||||
|
{0, true},
|
||||||
|
{append(([]string)(nil), ""), false},
|
||||||
|
{[]string{}, false},
|
||||||
|
{1, false},
|
||||||
|
{"1", false},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range testvalues {
|
||||||
|
assert.Equal(t, tt.isnil, IsNil(tt.i))
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user