mirror of
https://github.com/mainnika/mongox-go-driver.git
synced 2026-05-23 00:03:36 +00:00
Compare commits
24 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9cf3551c20 | |||
| 3035d8d571 | |||
| eac50d1770 | |||
| 05ebb25e70 | |||
| fd53c66690 | |||
| 6111341a3c | |||
| 9f647ca094 | |||
| eeb83daf4b | |||
| 6e6a042a16 | |||
| e0c26f770f | |||
| e7a05d94e1 | |||
| 8267abe5be | |||
| ab4227eaef | |||
| 6e5dccbc96 | |||
| 0b313e82fb | |||
| f1fe41d336 | |||
| 06b1ef5d9a | |||
| 2c49854aee | |||
| 7f46008227 | |||
| cf23c3b579 | |||
| f3adfb039a | |||
| 45a41c6c6c | |||
| db6c11f455 | |||
| deac951551 |
+21
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright © 2020 Nikita Tokarchuk
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -4,6 +4,7 @@ require (
|
||||
github.com/google/go-cmp v0.3.0 // indirect
|
||||
github.com/klauspost/compress v1.10.1 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/stretchr/testify v1.5.1
|
||||
github.com/xdg/stringprep v1.0.0 // indirect
|
||||
go.mongodb.org/mongo-driver v1.3.0
|
||||
golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d // indirect
|
||||
|
||||
@@ -71,6 +71,8 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
|
||||
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
||||
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
|
||||
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
|
||||
github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c h1:u40Z8hqBAAQyv+vATcGgV0YCnDjqSL7/q/JyPhhJSPk=
|
||||
@@ -113,4 +115,5 @@ golang.org/x/tools v0.0.0-20190531172133-b3315ee88b7d/go.mod h1:/rFqwRUd4F7ZHNgw
|
||||
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/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
|
||||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package tempdb
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -12,24 +12,24 @@ import (
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox/database"
|
||||
)
|
||||
|
||||
// TempDB is a temporary database connection that will be destroyed after close
|
||||
type TempDB struct {
|
||||
// EphemeralDatabase is a temporary database connection that will be destroyed after close
|
||||
type EphemeralDatabase struct {
|
||||
mongox.Database
|
||||
}
|
||||
|
||||
// NewTempDB creates new mongo connection
|
||||
func NewTempDB(URI string) (tempdb *TempDB, err error) {
|
||||
// NewEphemeral creates new mongo connection
|
||||
func NewEphemeral(URI string) (db *EphemeralDatabase, err error) {
|
||||
|
||||
name := strconv.Itoa(rand.Int())
|
||||
opts := options.Client().ApplyURI(URI)
|
||||
client, err := mongo.Connect(context.Background(), opts)
|
||||
|
||||
tempdb = &TempDB{Database: database.NewDatabase(client, name)}
|
||||
db = &EphemeralDatabase{Database: database.NewDatabase(client, name)}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Close the connection and drop database
|
||||
func (tdb *TempDB) Close() {
|
||||
_ = tdb.Client().Database(tdb.Name()).Drop(tdb.Context())
|
||||
func (e *EphemeralDatabase) Close() error {
|
||||
return e.Client().Database(e.Name()).Drop(e.Context())
|
||||
}
|
||||
+25
-10
@@ -1,28 +1,33 @@
|
||||
package base
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox"
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox/errors"
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox/utils"
|
||||
)
|
||||
|
||||
// GetID returns source document id
|
||||
func GetID(source interface{}) (id interface{}) {
|
||||
|
||||
switch doc := source.(type) {
|
||||
case mongox.BaseObjectID:
|
||||
case mongox.OIDBased:
|
||||
return getObjectIDOrGenerate(doc)
|
||||
case mongox.BaseString:
|
||||
case mongox.StringBased:
|
||||
return getStringIDOrPanic(doc)
|
||||
case mongox.BaseObject:
|
||||
case mongox.JSONBased:
|
||||
return getObjectOrPanic(doc)
|
||||
case mongox.InterfaceBased:
|
||||
return getInterfaceOrPanic(doc)
|
||||
|
||||
default:
|
||||
panic(errors.Malformedf("source contains malformed document, %v", source))
|
||||
panic(fmt.Errorf("source contains malformed document, %v", source))
|
||||
}
|
||||
}
|
||||
|
||||
func getObjectIDOrGenerate(source mongox.BaseObjectID) (id primitive.ObjectID) {
|
||||
func getObjectIDOrGenerate(source mongox.OIDBased) (id primitive.ObjectID) {
|
||||
|
||||
id = source.GetID()
|
||||
if id != primitive.NilObjectID {
|
||||
@@ -35,22 +40,32 @@ func getObjectIDOrGenerate(source mongox.BaseObjectID) (id primitive.ObjectID) {
|
||||
return
|
||||
}
|
||||
|
||||
func getStringIDOrPanic(source mongox.BaseString) (id string) {
|
||||
func getStringIDOrPanic(source mongox.StringBased) (id string) {
|
||||
|
||||
id = source.GetID()
|
||||
if id != "" {
|
||||
return id
|
||||
}
|
||||
|
||||
panic(errors.Malformedf("victim contains malformed document, %v", source))
|
||||
panic(fmt.Errorf("source contains malformed document, %v", source))
|
||||
}
|
||||
|
||||
func getObjectOrPanic(source mongox.BaseObject) (id primitive.D) {
|
||||
func getObjectOrPanic(source mongox.JSONBased) (id primitive.D) {
|
||||
|
||||
id = source.GetID()
|
||||
if id != nil {
|
||||
return id
|
||||
}
|
||||
|
||||
panic(errors.Malformedf("victim contains malformed document, %v", source))
|
||||
panic(fmt.Errorf("source contains malformed document, %v", source))
|
||||
}
|
||||
|
||||
func getInterfaceOrPanic(source mongox.InterfaceBased) (id interface{}) {
|
||||
|
||||
id = source.GetID()
|
||||
if !utils.IsNil(id) {
|
||||
return id
|
||||
}
|
||||
|
||||
panic(fmt.Errorf("source contains malformed document, %v", source))
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package base
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox/base/jsonbased"
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox/base/oidbased"
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox/base/stringbased"
|
||||
)
|
||||
|
||||
type DocWithCustomInterface struct {
|
||||
ID int `bson:"_id" json:"_id" collection:"4"`
|
||||
}
|
||||
|
||||
func (d *DocWithCustomInterface) GetID() interface{} {
|
||||
return d.ID
|
||||
}
|
||||
|
||||
func (d *DocWithCustomInterface) SetID(interface{}) {
|
||||
panic("not implemented")
|
||||
}
|
||||
|
||||
func TestGetID(t *testing.T) {
|
||||
|
||||
type DocWithObjectID struct {
|
||||
oidbased.Primary `bson:",inline" json:",inline" collection:"1"`
|
||||
}
|
||||
type DocWithObject struct {
|
||||
jsonbased.Primary `bson:",inline" json:",inline" collection:"2"`
|
||||
}
|
||||
type DocWithString struct {
|
||||
stringbased.Primary `bson:",inline" json:",inline" collection:"3"`
|
||||
}
|
||||
|
||||
assert.Equal(t, primitive.ObjectID([12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2}), GetID(&DocWithObjectID{oidbased.Primary{[12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2}}}))
|
||||
assert.Equal(t, primitive.D{{"1", "2"}}, GetID(&DocWithObject{jsonbased.Primary{primitive.D{{"1", "2"}}}}))
|
||||
assert.Equal(t, "foobar", GetID(&DocWithString{stringbased.Primary{"foobar"}}))
|
||||
assert.Equal(t, 420, GetID(&DocWithCustomInterface{ID: 420}))
|
||||
}
|
||||
@@ -2,10 +2,12 @@ package base
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox/base/protection"
|
||||
)
|
||||
|
||||
// GetProtection function finds protection field in the source document otherwise returns nil
|
||||
func GetProtection(source interface{}) *Protection {
|
||||
func GetProtection(source interface{}) *protection.Key {
|
||||
|
||||
v := reflect.ValueOf(source)
|
||||
if v.Kind() != reflect.Ptr || v.IsNil() {
|
||||
@@ -17,13 +19,16 @@ func GetProtection(source interface{}) *Protection {
|
||||
|
||||
for i := 0; i < numField; i++ {
|
||||
field := el.Field(i)
|
||||
if !field.CanInterface() {
|
||||
continue
|
||||
}
|
||||
|
||||
switch field.Interface().(type) {
|
||||
case *Protection:
|
||||
return field.Interface().(*Protection)
|
||||
case Protection:
|
||||
case *protection.Key:
|
||||
return field.Interface().(*protection.Key)
|
||||
case protection.Key:
|
||||
ptr := field.Addr()
|
||||
return ptr.Interface().(*Protection)
|
||||
return ptr.Interface().(*protection.Key)
|
||||
default:
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package jsonbased
|
||||
|
||||
import (
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox"
|
||||
)
|
||||
|
||||
var _ mongox.JSONBased = (*Primary)(nil)
|
||||
|
||||
// Primary is a structure with object as an _id field
|
||||
type Primary struct {
|
||||
ID primitive.D `bson:"_id" json:"_id"`
|
||||
}
|
||||
|
||||
// GetID returns an _id
|
||||
func (p *Primary) GetID() primitive.D {
|
||||
return p.ID
|
||||
}
|
||||
|
||||
// SetID sets an _id
|
||||
func (p *Primary) SetID(id primitive.D) {
|
||||
p.ID = id
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package jsonbased
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox-testing/database"
|
||||
)
|
||||
|
||||
func Test_GetID(t *testing.T) {
|
||||
|
||||
type DocWithObject struct {
|
||||
Primary `bson:",inline" json:",inline" collection:"1"`
|
||||
}
|
||||
|
||||
doc := &DocWithObject{Primary{primitive.D{{"1", "one"}, {"2", "two"}}}}
|
||||
|
||||
assert.Equal(t, primitive.D{{"1", "one"}, {"2", "two"}}, doc.GetID())
|
||||
}
|
||||
|
||||
func Test_SetID(t *testing.T) {
|
||||
|
||||
type DocWithObject struct {
|
||||
Primary `bson:",inline" json:",inline" collection:"1"`
|
||||
}
|
||||
|
||||
doc := &DocWithObject{Primary{primitive.D{{"1", "one"}, {"2", "two"}}}}
|
||||
|
||||
doc.SetID(primitive.D{{"3", "three"}, {"4", "you"}})
|
||||
|
||||
assert.Equal(t, primitive.D{{"3", "three"}, {"4", "you"}}, doc.Primary.ID)
|
||||
assert.Equal(t, primitive.D{{"3", "three"}, {"4", "you"}}, doc.GetID())
|
||||
}
|
||||
|
||||
func Test_SaveLoad(t *testing.T) {
|
||||
|
||||
type DocWithObjectID struct {
|
||||
Primary `bson:",inline" json:",inline" collection:"1"`
|
||||
}
|
||||
|
||||
db, err := database.NewEphemeral("mongodb://localhost")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
defer db.Close()
|
||||
|
||||
doc1 := &DocWithObjectID{Primary{primitive.D{{"1", "one"}, {"2", "two"}}}}
|
||||
doc2 := &DocWithObjectID{}
|
||||
|
||||
err = db.SaveOne(doc1)
|
||||
assert.NoError(t, err)
|
||||
|
||||
err = db.LoadOne(doc2)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, doc1, doc2)
|
||||
|
||||
bytes1, _ := json.Marshal(doc1)
|
||||
bytes2, _ := json.Marshal(doc2)
|
||||
|
||||
assert.Equal(t, bytes1, bytes2)
|
||||
}
|
||||
|
||||
func Test_Marshal(t *testing.T) {
|
||||
|
||||
type DocWithObjectID struct {
|
||||
Primary `bson:",inline" json:",inline" collection:"1"`
|
||||
}
|
||||
|
||||
id := primitive.D{{"1", "one"}, {"2", "two"}}
|
||||
doc := &DocWithObjectID{Primary{id}}
|
||||
|
||||
bytes, err := json.Marshal(doc)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, `{"_id":[{"Key":"1","Value":"one"},{"Key":"2","Value":"two"}]}`, string(bytes))
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
package base
|
||||
|
||||
import (
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox"
|
||||
)
|
||||
|
||||
var _ mongox.BaseObject = &Object{}
|
||||
|
||||
// Object is a structure with object as an _id field
|
||||
type Object struct {
|
||||
ID primitive.D `bson:"_id,omitempty" json:"_id,omitempty"`
|
||||
}
|
||||
|
||||
// GetID returns an _id
|
||||
func (db *Object) GetID() primitive.D {
|
||||
return db.ID
|
||||
}
|
||||
|
||||
// SetID sets an _id
|
||||
func (db *Object) SetID(id primitive.D) {
|
||||
db.ID = id
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
package base
|
||||
|
||||
import (
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox"
|
||||
)
|
||||
|
||||
var _ mongox.BaseObjectID = &ObjectID{}
|
||||
|
||||
// ObjectID is a structure with objectId as an _id field
|
||||
type ObjectID struct {
|
||||
ID primitive.ObjectID `bson:"_id,omitempty" json:"_id,omitempty"`
|
||||
}
|
||||
|
||||
// GetID returns an _id
|
||||
func (db *ObjectID) GetID() primitive.ObjectID {
|
||||
return db.ID
|
||||
}
|
||||
|
||||
// SetID sets an _id
|
||||
func (db *ObjectID) SetID(id primitive.ObjectID) {
|
||||
db.ID = id
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package oidbased
|
||||
|
||||
import (
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox"
|
||||
)
|
||||
|
||||
var _ mongox.OIDBased = (*Primary)(nil)
|
||||
|
||||
// Primary is a structure with objectId as the primary key
|
||||
type Primary struct {
|
||||
ID primitive.ObjectID `bson:"_id" json:"_id"`
|
||||
}
|
||||
|
||||
// GetID returns an _id
|
||||
func (p *Primary) GetID() primitive.ObjectID {
|
||||
return p.ID
|
||||
}
|
||||
|
||||
// SetID sets an _id
|
||||
func (p *Primary) SetID(id primitive.ObjectID) {
|
||||
p.ID = id
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package oidbased
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox-testing/database"
|
||||
)
|
||||
|
||||
func Test_GetID(t *testing.T) {
|
||||
|
||||
type DocWithObjectID struct {
|
||||
Primary `bson:",inline" json:",inline" collection:"1"`
|
||||
}
|
||||
|
||||
doc := &DocWithObjectID{Primary{[12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2}}}
|
||||
|
||||
assert.Equal(t, primitive.ObjectID([12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2}), doc.GetID())
|
||||
}
|
||||
|
||||
func Test_SetID(t *testing.T) {
|
||||
|
||||
type DocWithObjectID struct {
|
||||
Primary `bson:",inline" json:",inline" collection:"1"`
|
||||
}
|
||||
|
||||
doc := &DocWithObjectID{}
|
||||
|
||||
doc.SetID([12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2})
|
||||
|
||||
assert.Equal(t, primitive.ObjectID([12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2}), doc.Primary.ID)
|
||||
assert.Equal(t, primitive.ObjectID([12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2}), doc.GetID())
|
||||
}
|
||||
|
||||
func Test_SaveLoad(t *testing.T) {
|
||||
|
||||
type DocWithObjectID struct {
|
||||
Primary `bson:",inline" json:",inline" collection:"1"`
|
||||
}
|
||||
|
||||
db, err := database.NewEphemeral("mongodb://localhost")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
defer db.Close()
|
||||
|
||||
doc1 := &DocWithObjectID{}
|
||||
doc2 := &DocWithObjectID{}
|
||||
|
||||
err = db.SaveOne(doc1)
|
||||
assert.NoError(t, err)
|
||||
|
||||
err = db.LoadOne(doc2)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, doc1, doc2)
|
||||
|
||||
bytes1, _ := json.Marshal(doc1)
|
||||
bytes2, _ := json.Marshal(doc2)
|
||||
|
||||
assert.Equal(t, bytes1, bytes2)
|
||||
}
|
||||
|
||||
func Test_Marshal(t *testing.T) {
|
||||
|
||||
type DocWithObjectID struct {
|
||||
Primary `bson:",inline" json:",inline" collection:"1"`
|
||||
}
|
||||
|
||||
id, _ := primitive.ObjectIDFromHex("feadbeeffeadbeeffeadbeef")
|
||||
doc := &DocWithObjectID{Primary{id}}
|
||||
|
||||
bytes, err := json.Marshal(doc)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, `{"_id":"feadbeeffeadbeeffeadbeef"}`, string(bytes))
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
package base
|
||||
package protection
|
||||
|
||||
import (
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
// Protection field stores unique document id and version
|
||||
type Protection struct {
|
||||
// Key field stores unique document id and version
|
||||
type Key struct {
|
||||
X primitive.ObjectID `bson:"_x" json:"_x" index:",hashed"`
|
||||
V int64 `bson:"_v" json:"_v"`
|
||||
}
|
||||
@@ -2,14 +2,16 @@ package base
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox"
|
||||
)
|
||||
|
||||
// Reset function creates new zero object for the target pointer
|
||||
func Reset(target interface{}) {
|
||||
|
||||
resettable, canReset := target.(mongox.Resetter)
|
||||
type resetter interface {
|
||||
Reset()
|
||||
}
|
||||
|
||||
resettable, canReset := target.(resetter)
|
||||
if canReset {
|
||||
resettable.Reset()
|
||||
return
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
package base
|
||||
|
||||
import (
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox"
|
||||
)
|
||||
|
||||
var _ mongox.BaseString = &String{}
|
||||
|
||||
// String is a structure with string as an _id field
|
||||
type String struct {
|
||||
ID string `bson:"_id,omitempty" json:"_id,omitempty"`
|
||||
}
|
||||
|
||||
// GetID returns an _id
|
||||
func (db *String) GetID() string {
|
||||
return db.ID
|
||||
}
|
||||
|
||||
// SetID sets an _id
|
||||
func (db *String) SetID(id string) {
|
||||
db.ID = id
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package stringbased
|
||||
|
||||
import (
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox"
|
||||
)
|
||||
|
||||
var _ mongox.StringBased = (*Primary)(nil)
|
||||
|
||||
// Primary is a structure with string as an _id field
|
||||
type Primary struct {
|
||||
ID string `bson:"_id" json:"_id"`
|
||||
}
|
||||
|
||||
// GetID returns an _id
|
||||
func (p *Primary) GetID() string {
|
||||
return p.ID
|
||||
}
|
||||
|
||||
// SetID sets an _id
|
||||
func (p *Primary) SetID(id string) {
|
||||
p.ID = id
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package stringbased
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox-testing/database"
|
||||
)
|
||||
|
||||
func Test_GetID(t *testing.T) {
|
||||
|
||||
type DocWithString struct {
|
||||
Primary `bson:",inline" json:",inline" collection:"1"`
|
||||
}
|
||||
|
||||
doc := &DocWithString{Primary{"foobar"}}
|
||||
|
||||
assert.Equal(t, "foobar", doc.GetID())
|
||||
}
|
||||
|
||||
func Test_SetID(t *testing.T) {
|
||||
|
||||
type DocWithString struct {
|
||||
Primary `bson:",inline" json:",inline" collection:"1"`
|
||||
}
|
||||
|
||||
doc := &DocWithString{Primary{"foobar"}}
|
||||
|
||||
doc.SetID("rockrockrock")
|
||||
|
||||
assert.Equal(t, "rockrockrock", doc.Primary.ID)
|
||||
assert.Equal(t, "rockrockrock", doc.GetID())
|
||||
}
|
||||
|
||||
func Test_SaveLoad(t *testing.T) {
|
||||
|
||||
type DocWithObjectID struct {
|
||||
Primary `bson:",inline" json:",inline" collection:"1"`
|
||||
}
|
||||
|
||||
db, err := database.NewEphemeral("mongodb://localhost")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
defer db.Close()
|
||||
|
||||
doc1 := &DocWithObjectID{Primary{"foobar"}}
|
||||
doc2 := &DocWithObjectID{}
|
||||
|
||||
err = db.SaveOne(doc1)
|
||||
assert.NoError(t, err)
|
||||
|
||||
err = db.LoadOne(doc2)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, doc1, doc2)
|
||||
|
||||
bytes1, _ := json.Marshal(doc1)
|
||||
bytes2, _ := json.Marshal(doc2)
|
||||
|
||||
assert.Equal(t, bytes1, bytes2)
|
||||
}
|
||||
|
||||
func Test_Marshal(t *testing.T) {
|
||||
|
||||
type DocWithObjectID struct {
|
||||
Primary `bson:",inline" json:",inline" collection:"1"`
|
||||
}
|
||||
|
||||
doc := &DocWithObjectID{Primary{"foobar"}}
|
||||
|
||||
bytes, err := json.Marshal(doc)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, `{"_id":"foobar"}`, string(bytes))
|
||||
}
|
||||
@@ -1,162 +0,0 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"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/errors"
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox/query"
|
||||
)
|
||||
|
||||
func createSimpleLoad(db mongox.Database, target interface{}, composed *query.Query) (cursor *mongo.Cursor, err error) {
|
||||
|
||||
collection := db.GetCollectionOf(target)
|
||||
opts := options.Find()
|
||||
|
||||
opts.Sort = composed.Sorter()
|
||||
opts.Limit = composed.Limiter()
|
||||
opts.Skip = composed.Skipper()
|
||||
|
||||
return collection.Find(db.Context(), composed.M(), opts)
|
||||
}
|
||||
|
||||
func createAggregateLoad(db mongox.Database, target interface{}, composed *query.Query) (cursor *mongo.Cursor, err error) {
|
||||
|
||||
collection := db.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, errors.Malformedf("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 := db.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(db.Context(), pipeline, opts)
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"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/errors"
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox/query"
|
||||
)
|
||||
|
||||
// LoadOne function loads a first single target document by a query
|
||||
func LoadOne(db mongox.Database, 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 = createAggregateLoad(db, target, composed)
|
||||
} else {
|
||||
result, err = createSimpleLoad(db, target, composed)
|
||||
}
|
||||
if err != nil {
|
||||
return errors.InternalErrorf("can't create find result: %s", err)
|
||||
}
|
||||
|
||||
hasNext := result.Next(db.Context())
|
||||
if !hasNext {
|
||||
return errors.NotFoundErrorf("can't find result: %s", result.Err())
|
||||
}
|
||||
|
||||
base.Reset(target)
|
||||
|
||||
return result.Decode(target)
|
||||
}
|
||||
@@ -1,78 +0,0 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"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/errors"
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox/query"
|
||||
)
|
||||
|
||||
// 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 !hasNext {
|
||||
return errors.NotFoundErrorf("%s", mongo.ErrNoDocuments)
|
||||
}
|
||||
|
||||
base.Reset(l.target)
|
||||
|
||||
err := l.Decode(l.target)
|
||||
if err != nil {
|
||||
return errors.InternalErrorf("can't decode desult: %s", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Next loads next documents but doesn't perform decoding
|
||||
func (l *StreamLoader) Next() error {
|
||||
|
||||
hasNext := l.Cursor.Next(l.ctx)
|
||||
if !hasNext {
|
||||
return errors.NotFoundErrorf("%s", mongo.ErrNoDocuments)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Close cursor
|
||||
func (l *StreamLoader) Close() error {
|
||||
|
||||
return l.Cursor.Close(l.ctx)
|
||||
}
|
||||
|
||||
// LoadStream function loads documents one by one into a target channel
|
||||
func LoadStream(db mongox.Database, target interface{}, filters ...interface{}) (*StreamLoader, error) {
|
||||
|
||||
var cursor *mongo.Cursor
|
||||
var err error
|
||||
|
||||
composed := query.Compose(filters...)
|
||||
hasPreloader, _ := composed.Preloader()
|
||||
|
||||
if hasPreloader {
|
||||
cursor, err = createAggregateLoad(db, target, composed)
|
||||
} else {
|
||||
cursor, err = createSimpleLoad(db, target, composed)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, errors.InternalErrorf("can't create find result: %s", err)
|
||||
}
|
||||
|
||||
l := &StreamLoader{Cursor: cursor, ctx: db.Context(), target: target}
|
||||
|
||||
return l, nil
|
||||
}
|
||||
@@ -1,31 +1,31 @@
|
||||
package common
|
||||
package database
|
||||
|
||||
import (
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"fmt"
|
||||
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox"
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox/errors"
|
||||
"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 Count(db mongox.Database, target interface{}, filters ...interface{}) (int64, error) {
|
||||
func (d *Database) Count(target interface{}, filters ...interface{}) (int64, error) {
|
||||
|
||||
collection := db.GetCollectionOf(target)
|
||||
collection := d.GetCollectionOf(target)
|
||||
opts := options.Count()
|
||||
composed := query.Compose(filters...)
|
||||
|
||||
opts.Limit = composed.Limiter()
|
||||
opts.Skip = composed.Skipper()
|
||||
|
||||
result, err := collection.CountDocuments(db.Context(), composed.M(), opts)
|
||||
if err == mongo.ErrNoDocuments {
|
||||
return 0, errors.NotFoundErrorf("%s", err)
|
||||
result, err := collection.CountDocuments(d.Context(), composed.M(), opts)
|
||||
if err == mongox.ErrNoDocuments {
|
||||
return 0, err
|
||||
}
|
||||
if err != nil {
|
||||
return 0, errors.InternalErrorf("can't decode desult: %s", err)
|
||||
return 0, fmt.Errorf("can't decode desult: %w", err)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
+167
-10
@@ -2,23 +2,27 @@ package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox"
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox/errors"
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox/query"
|
||||
)
|
||||
|
||||
// Database handler
|
||||
type Database struct {
|
||||
client *mongo.Client
|
||||
client *mongox.Client
|
||||
dbname string
|
||||
ctx context.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.client = client
|
||||
@@ -28,13 +32,19 @@ func NewDatabase(client *mongo.Client, dbname string) mongox.Database {
|
||||
}
|
||||
|
||||
// Client function returns a mongo client
|
||||
func (d *Database) Client() mongox.MongoClient {
|
||||
func (d *Database) Client() *mongox.Client {
|
||||
return d.client
|
||||
}
|
||||
|
||||
// Context function returns a context
|
||||
func (d *Database) Context() context.Context {
|
||||
return d.ctx
|
||||
func (d *Database) Context() (ctx context.Context) {
|
||||
|
||||
ctx = d.ctx
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Name function returns a database name
|
||||
@@ -45,7 +55,7 @@ func (d *Database) Name() string {
|
||||
// New function creates new database context with same client
|
||||
func (d *Database) New(ctx context.Context) mongox.Database {
|
||||
|
||||
if ctx != nil {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
|
||||
@@ -62,7 +72,7 @@ func (d *Database) New(ctx context.Context) mongox.Database {
|
||||
// base.ObjectID `bson:",inline" json:",inline" collection:"foobars"`
|
||||
// ...
|
||||
// Will panic if there is no «collection» tag
|
||||
func (d *Database) GetCollectionOf(document interface{}) mongox.MongoCollection {
|
||||
func (d *Database) GetCollectionOf(document interface{}) *mongox.Collection {
|
||||
|
||||
el := reflect.TypeOf(document).Elem()
|
||||
numField := el.NumField()
|
||||
@@ -78,5 +88,152 @@ func (d *Database) GetCollectionOf(document interface{}) mongox.MongoCollection
|
||||
return d.client.Database(d.dbname).Collection(found)
|
||||
}
|
||||
|
||||
panic(errors.InternalErrorf("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 *mongox.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 *mongox.Cursor, err error) {
|
||||
|
||||
collection := d.GetCollectionOf(target)
|
||||
opts := options.Aggregate()
|
||||
|
||||
pipeline := primitive.A{}
|
||||
|
||||
if !composed.Empty() {
|
||||
pipeline = append(pipeline, primitive.M{"$match": 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)
|
||||
}
|
||||
|
||||
@@ -1,41 +1,40 @@
|
||||
package common
|
||||
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"
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox/base"
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox/errors"
|
||||
)
|
||||
|
||||
// DeleteArray removes documents list from a database by their ids
|
||||
func DeleteArray(db mongox.Database, target interface{}) error {
|
||||
func (d *Database) DeleteArray(target interface{}) error {
|
||||
|
||||
targetV := reflect.ValueOf(target)
|
||||
targetT := targetV.Type()
|
||||
|
||||
targetK := targetV.Kind()
|
||||
if targetK != reflect.Ptr {
|
||||
panic(errors.Malformedf("target is not a ptr"))
|
||||
panic(fmt.Errorf("target is not a ptr"))
|
||||
}
|
||||
|
||||
targetSliceV := targetV.Elem()
|
||||
targetSliceT := targetT.Elem()
|
||||
if targetSliceT.Kind() != reflect.Slice {
|
||||
panic(errors.Malformedf("target should be a ptr to a slice"))
|
||||
panic(fmt.Errorf("target should be a ptr to a slice"))
|
||||
}
|
||||
|
||||
targetSliceElemT := targetSliceT.Elem()
|
||||
if targetSliceElemT.Kind() != reflect.Ptr {
|
||||
panic(errors.Malformedf("target slice should contain ptrs"))
|
||||
panic(fmt.Errorf("target slice should contain ptrs"))
|
||||
}
|
||||
|
||||
zeroElem := reflect.Zero(targetSliceElemT)
|
||||
targetLen := targetSliceV.Len()
|
||||
collection := db.GetCollectionOf(zeroElem.Interface())
|
||||
collection := d.GetCollectionOf(zeroElem.Interface())
|
||||
opts := options.Delete()
|
||||
ids := primitive.A{}
|
||||
|
||||
@@ -45,15 +44,15 @@ func DeleteArray(db mongox.Database, target interface{}) error {
|
||||
}
|
||||
|
||||
if len(ids) == 0 {
|
||||
return errors.Malformedf("can't delete zero elements")
|
||||
return fmt.Errorf("can't delete zero elements")
|
||||
}
|
||||
|
||||
result, err := collection.DeleteMany(db.Context(), primitive.M{"_id": primitive.M{"$in": ids}}, opts)
|
||||
result, err := collection.DeleteMany(d.Context(), primitive.M{"_id": primitive.M{"$in": ids}}, opts)
|
||||
if err != nil {
|
||||
return errors.NotFoundErrorf("can't create find and delete result: %s", err)
|
||||
return fmt.Errorf("can't create find and delete result: %w", err)
|
||||
}
|
||||
if result.DeletedCount != int64(targetLen) {
|
||||
return errors.InternalErrorf("can't verify delete result: removed count mismatch %d != %d", result.DeletedCount, targetLen)
|
||||
return fmt.Errorf("can't verify delete result: removed count mismatch %d != %d", result.DeletedCount, targetLen)
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -1,29 +1,29 @@
|
||||
package common
|
||||
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"
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox/base"
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox/errors"
|
||||
"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
|
||||
func DeleteOne(db mongox.Database, target interface{}, filters ...interface{}) error {
|
||||
func (d *Database) DeleteOne(target interface{}, filters ...interface{}) error {
|
||||
|
||||
collection := db.GetCollectionOf(target)
|
||||
collection := d.GetCollectionOf(target)
|
||||
opts := &options.FindOneAndDeleteOptions{}
|
||||
composed := query.Compose(filters...)
|
||||
protected := base.GetProtection(target)
|
||||
|
||||
opts.Sort = composed.Sorter()
|
||||
|
||||
if target != nil {
|
||||
if !utils.IsNil(target) {
|
||||
composed.And(primitive.M{"_id": base.GetID(target)})
|
||||
}
|
||||
|
||||
@@ -33,17 +33,17 @@ func DeleteOne(db mongox.Database, target interface{}, filters ...interface{}) e
|
||||
protected.V = time.Now().Unix()
|
||||
}
|
||||
|
||||
result := collection.FindOneAndDelete(db.Context(), composed.M(), opts)
|
||||
result := collection.FindOneAndDelete(d.Context(), composed.M(), opts)
|
||||
if result.Err() != nil {
|
||||
return errors.InternalErrorf("can't create find one and delete result: %s", result.Err())
|
||||
return fmt.Errorf("can't create find one and delete result: %w", result.Err())
|
||||
}
|
||||
|
||||
err := result.Decode(target)
|
||||
if err == mongo.ErrNoDocuments {
|
||||
return errors.NotFoundErrorf("%s", err)
|
||||
if err == mongox.ErrNoDocuments {
|
||||
return err
|
||||
}
|
||||
if err != nil {
|
||||
return errors.InternalErrorf("can't decode result: %s", err)
|
||||
return fmt.Errorf("can't decode result: %w", err)
|
||||
}
|
||||
|
||||
return 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"))
|
||||
}
|
||||
|
||||
opts := &options.IndexOptions{
|
||||
Background: &f,
|
||||
Unique: &f,
|
||||
Name: &name,
|
||||
}
|
||||
|
||||
index := primitive.D{{Key: key, Value: 1}}
|
||||
if indexValues[0] == "-" {
|
||||
index = primitive.D{{Key: key, Value: -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 = append(index, primitive.E{compoundValue[1:], -1})
|
||||
} else {
|
||||
index = append(index, primitive.E{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])
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,58 +1,55 @@
|
||||
package common
|
||||
package database
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"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/errors"
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox/query"
|
||||
)
|
||||
|
||||
// LoadArray loads an array of documents from the database by query
|
||||
func LoadArray(db mongox.Database, target interface{}, filters ...interface{}) error {
|
||||
func (d *Database) LoadArray(target interface{}, filters ...interface{}) error {
|
||||
|
||||
targetV := reflect.ValueOf(target)
|
||||
targetT := targetV.Type()
|
||||
|
||||
targetK := targetV.Kind()
|
||||
if targetK != reflect.Ptr {
|
||||
panic(errors.InternalErrorf("target is not a ptr"))
|
||||
panic(fmt.Errorf("target is not a ptr"))
|
||||
}
|
||||
|
||||
targetSliceV := targetV.Elem()
|
||||
targetSliceT := targetT.Elem()
|
||||
if targetSliceT.Kind() != reflect.Slice {
|
||||
panic(errors.InternalErrorf("target should be a ptr to a slice"))
|
||||
panic(fmt.Errorf("target should be a ptr to a slice"))
|
||||
}
|
||||
|
||||
targetSliceElemT := targetSliceT.Elem()
|
||||
if targetSliceElemT.Kind() != reflect.Ptr {
|
||||
panic(errors.InternalErrorf("target slice should contain ptrs"))
|
||||
panic(fmt.Errorf("target slice should contain ptrs"))
|
||||
}
|
||||
|
||||
composed := query.Compose(filters...)
|
||||
zeroElem := reflect.Zero(targetSliceElemT)
|
||||
hasPreloader, _ := composed.Preloader()
|
||||
|
||||
var result *mongo.Cursor
|
||||
var result *mongox.Cursor
|
||||
var err error
|
||||
|
||||
if hasPreloader {
|
||||
result, err = createAggregateLoad(db, zeroElem.Interface(), composed)
|
||||
result, err = d.createAggregateLoad(zeroElem.Interface(), composed)
|
||||
} else {
|
||||
result, err = createSimpleLoad(db, zeroElem.Interface(), composed)
|
||||
result, err = d.createSimpleLoad(zeroElem.Interface(), composed)
|
||||
}
|
||||
if err != nil {
|
||||
return errors.InternalErrorf("can't create find result: %s", err)
|
||||
return fmt.Errorf("can't create find result: %w", err)
|
||||
}
|
||||
|
||||
defer result.Close(db.Context())
|
||||
var i int
|
||||
|
||||
for i = 0; result.Next(db.Context()); {
|
||||
for i = 0; result.Next(d.Context()); {
|
||||
if targetSliceV.Len() == i {
|
||||
elem := reflect.New(targetSliceElemT.Elem())
|
||||
if err = result.Decode(elem.Interface()); err == nil {
|
||||
@@ -74,5 +71,5 @@ func LoadArray(db mongox.Database, target interface{}, filters ...interface{}) e
|
||||
targetSliceV = targetSliceV.Slice(0, i)
|
||||
targetV.Elem().Set(targetSliceV)
|
||||
|
||||
return nil
|
||||
return result.Close(d.Context())
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"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/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 *mongox.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 mongox.ErrNoDocuments
|
||||
}
|
||||
|
||||
base.Reset(target)
|
||||
|
||||
return result.Decode(target)
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"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 *mongox.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{cur: cursor, ctx: d.Context(), target: target}
|
||||
|
||||
return l, nil
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package common
|
||||
package database
|
||||
|
||||
import (
|
||||
"time"
|
||||
@@ -7,16 +7,14 @@ import (
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"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/errors"
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox/query"
|
||||
)
|
||||
|
||||
// SaveOne saves a single source document to the database
|
||||
func SaveOne(db mongox.Database, source interface{}) error {
|
||||
func (d *Database) SaveOne(source interface{}) error {
|
||||
|
||||
collection := db.GetCollectionOf(source)
|
||||
collection := d.GetCollectionOf(source)
|
||||
opts := options.FindOneAndReplace()
|
||||
id := base.GetID(source)
|
||||
protected := base.GetProtection(source)
|
||||
@@ -31,9 +29,9 @@ func SaveOne(db mongox.Database, source interface{}) error {
|
||||
protected.V = time.Now().Unix()
|
||||
}
|
||||
|
||||
result := collection.FindOneAndReplace(db.Context(), composed.M(), source, opts)
|
||||
result := collection.FindOneAndReplace(d.Context(), composed.M(), source, opts)
|
||||
if result.Err() != nil {
|
||||
return errors.NotFoundErrorf("%s", result.Err())
|
||||
return result.Err()
|
||||
}
|
||||
|
||||
return result.Decode(source)
|
||||
@@ -0,0 +1,79 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox"
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox/base"
|
||||
)
|
||||
|
||||
// StreamLoader is a controller for a database cursor
|
||||
type StreamLoader struct {
|
||||
cur *mongox.Cursor
|
||||
ctx context.Context
|
||||
target interface{}
|
||||
}
|
||||
|
||||
// DecodeNext loads next documents to a target or returns an error
|
||||
func (l *StreamLoader) DecodeNext() error {
|
||||
|
||||
hasNext := l.cur.Next(l.ctx)
|
||||
|
||||
if l.cur.Err() != nil {
|
||||
return l.cur.Err()
|
||||
}
|
||||
if !hasNext {
|
||||
return mongox.ErrNoDocuments
|
||||
}
|
||||
|
||||
base.Reset(l.target)
|
||||
|
||||
err := l.cur.Decode(l.target)
|
||||
if err != nil {
|
||||
return fmt.Errorf("can't decode desult: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Decode function decodes the current cursor document into the target
|
||||
func (l *StreamLoader) Decode() error {
|
||||
|
||||
base.Reset(l.target)
|
||||
|
||||
err := l.cur.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.cur.Next(l.ctx)
|
||||
|
||||
if l.cur.Err() != nil {
|
||||
return l.cur.Err()
|
||||
}
|
||||
if !hasNext {
|
||||
return mongox.ErrNoDocuments
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *StreamLoader) Cursor() *mongox.Cursor {
|
||||
return l.cur
|
||||
}
|
||||
|
||||
// Close cursor
|
||||
func (l *StreamLoader) Close() error {
|
||||
return l.cur.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
|
||||
)
|
||||
@@ -1,16 +0,0 @@
|
||||
package errors
|
||||
|
||||
import "fmt"
|
||||
|
||||
// InternalError error
|
||||
type InternalError string
|
||||
|
||||
// Error message
|
||||
func (ie InternalError) Error() string {
|
||||
return fmt.Sprintf("internal error, %s", string(ie))
|
||||
}
|
||||
|
||||
// InternalErrorf function creates an instance of InternalError
|
||||
func InternalErrorf(format string, params ...interface{}) error {
|
||||
return InternalError(fmt.Sprintf(format, params...))
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package errors
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Malformed error
|
||||
type Malformed string
|
||||
|
||||
// Error message
|
||||
func (m Malformed) Error() string {
|
||||
return fmt.Sprintf("Malformed, %s", string(m))
|
||||
}
|
||||
|
||||
// Malformedf creates an instance of Malformed
|
||||
func Malformedf(format string, params ...interface{}) error {
|
||||
return Malformed(fmt.Sprintf(format, params...))
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package errors
|
||||
|
||||
import "fmt"
|
||||
|
||||
// NotFound error
|
||||
type NotFound string
|
||||
|
||||
// Error message
|
||||
func (nf NotFound) Error() string {
|
||||
return fmt.Sprintf("can not find, %s", string(nf))
|
||||
}
|
||||
|
||||
// NotFoundErrorf function creates an instance of BadRequestError
|
||||
func NotFoundErrorf(format string, params ...interface{}) error {
|
||||
return NotFound(fmt.Sprintf(format, params...))
|
||||
}
|
||||
+37
-70
@@ -5,95 +5,62 @@ import (
|
||||
|
||||
"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/readpref"
|
||||
)
|
||||
|
||||
// Reexport basic mongo structs
|
||||
type (
|
||||
Cursor = mongo.Cursor
|
||||
Client = mongo.Client
|
||||
Collection = mongo.Collection
|
||||
)
|
||||
|
||||
// Database is the mongox database interface
|
||||
type Database interface {
|
||||
Client() MongoClient
|
||||
Client() *Client
|
||||
Context() context.Context
|
||||
Name() string
|
||||
New(ctx context.Context) Database
|
||||
GetCollectionOf(document interface{}) MongoCollection
|
||||
GetCollectionOf(document interface{}) *Collection
|
||||
Count(target interface{}, filters ...interface{}) (int64, error)
|
||||
DeleteArray(target interface{}) error
|
||||
DeleteOne(target interface{}, filters ...interface{}) error
|
||||
LoadArray(target interface{}, filters ...interface{}) error
|
||||
LoadOne(target interface{}, filters ...interface{}) error
|
||||
LoadStream(target interface{}, filters ...interface{}) (StreamLoader, error)
|
||||
SaveOne(source interface{}) error
|
||||
IndexEnsure(cfg interface{}, document interface{}) error
|
||||
}
|
||||
|
||||
// MongoClient is the mongo client interface
|
||||
type MongoClient interface {
|
||||
Connect(ctx context.Context) error
|
||||
Disconnect(ctx context.Context) error
|
||||
Ping(ctx context.Context, rp *readpref.ReadPref) error
|
||||
StartSession(opts ...*options.SessionOptions) (mongo.Session, error)
|
||||
Database(name string, opts ...*options.DatabaseOptions) *mongo.Database
|
||||
ListDatabases(ctx context.Context, filter interface{}, opts ...*options.ListDatabasesOptions) (mongo.ListDatabasesResult, error)
|
||||
ListDatabaseNames(ctx context.Context, filter interface{}, opts ...*options.ListDatabasesOptions) ([]string, error)
|
||||
UseSession(ctx context.Context, fn func(mongo.SessionContext) error) error
|
||||
UseSessionWithOptions(ctx context.Context, opts *options.SessionOptions, fn func(mongo.SessionContext) error) error
|
||||
Watch(ctx context.Context, pipeline interface{}, opts ...*options.ChangeStreamOptions) (*mongo.ChangeStream, error)
|
||||
NumberSessionsInProgress() int
|
||||
// StreamLoader is a interface to control database cursor
|
||||
type StreamLoader interface {
|
||||
Cursor() *Cursor
|
||||
DecodeNext() error
|
||||
Decode() error
|
||||
Next() error
|
||||
Close() error
|
||||
Err() error
|
||||
}
|
||||
|
||||
// MongoCollection is the mongo collection interface
|
||||
type MongoCollection interface {
|
||||
Clone(opts ...*options.CollectionOptions) (*mongo.Collection, error)
|
||||
Name() string
|
||||
Database() *mongo.Database
|
||||
BulkWrite(ctx context.Context, models []mongo.WriteModel, opts ...*options.BulkWriteOptions) (*mongo.BulkWriteResult, error)
|
||||
InsertOne(ctx context.Context, document interface{}, opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error)
|
||||
InsertMany(ctx context.Context, documents []interface{}, opts ...*options.InsertManyOptions) (*mongo.InsertManyResult, error)
|
||||
DeleteOne(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
|
||||
DeleteMany(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
|
||||
UpdateOne(ctx context.Context, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
|
||||
UpdateMany(ctx context.Context, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
|
||||
ReplaceOne(ctx context.Context, filter interface{}, replacement interface{}, opts ...*options.ReplaceOptions) (*mongo.UpdateResult, error)
|
||||
Aggregate(ctx context.Context, pipeline interface{}, opts ...*options.AggregateOptions) (*mongo.Cursor, error)
|
||||
CountDocuments(ctx context.Context, filter interface{}, opts ...*options.CountOptions) (int64, error)
|
||||
EstimatedDocumentCount(ctx context.Context, opts ...*options.EstimatedDocumentCountOptions) (int64, error)
|
||||
Distinct(ctx context.Context, fieldName string, filter interface{}, opts ...*options.DistinctOptions) ([]interface{}, error)
|
||||
Find(ctx context.Context, filter interface{}, opts ...*options.FindOptions) (*mongo.Cursor, error)
|
||||
FindOne(ctx context.Context, filter interface{}, opts ...*options.FindOneOptions) *mongo.SingleResult
|
||||
FindOneAndDelete(ctx context.Context, filter interface{}, opts ...*options.FindOneAndDeleteOptions) *mongo.SingleResult
|
||||
FindOneAndReplace(ctx context.Context, filter interface{}, replacement interface{}, opts ...*options.FindOneAndReplaceOptions) *mongo.SingleResult
|
||||
FindOneAndUpdate(ctx context.Context, filter interface{}, update interface{}, opts ...*options.FindOneAndUpdateOptions) *mongo.SingleResult
|
||||
Watch(ctx context.Context, pipeline interface{}, opts ...*options.ChangeStreamOptions) (*mongo.ChangeStream, error)
|
||||
Indexes() mongo.IndexView
|
||||
Drop(ctx context.Context) error
|
||||
}
|
||||
|
||||
// Saver is an interface for documents that can be saved
|
||||
type Saver interface {
|
||||
Save(db Database) error
|
||||
}
|
||||
|
||||
// Deleter is an interface for documents that can be deleted
|
||||
type Deleter interface {
|
||||
Delete(db Database) error
|
||||
}
|
||||
|
||||
// Loader is an interface for documents that can be loaded
|
||||
type Loader interface {
|
||||
Load(db Database, filters ...interface{}) error
|
||||
}
|
||||
|
||||
// Resetter is an interface for documenta that can be resetted
|
||||
type Resetter interface {
|
||||
Reset()
|
||||
}
|
||||
|
||||
// BaseObjectID is an interface for documents that have objectId type for the _id field
|
||||
type BaseObjectID interface {
|
||||
// OIDBased is an interface for documents that have objectId type for the _id field
|
||||
type OIDBased interface {
|
||||
GetID() primitive.ObjectID
|
||||
SetID(id primitive.ObjectID)
|
||||
}
|
||||
|
||||
// BaseString is an interface for documents that have string type for the _id field
|
||||
type BaseString interface {
|
||||
// StringBased is an interface for documents that have string type for the _id field
|
||||
type StringBased interface {
|
||||
GetID() string
|
||||
SetID(id string)
|
||||
}
|
||||
|
||||
// BaseObject is an interface for documents that have object type for the _id field
|
||||
type BaseObject interface {
|
||||
// JSONBased is an interface for documents that have object type for the _id field
|
||||
type JSONBased interface {
|
||||
GetID() primitive.D
|
||||
SetID(id primitive.D)
|
||||
}
|
||||
|
||||
// InterfaceBased is an interface for documents that have custom declated type for the _id field
|
||||
type InterfaceBased interface {
|
||||
GetID() interface{}
|
||||
SetID(id interface{})
|
||||
}
|
||||
|
||||
+11
-8
@@ -1,11 +1,13 @@
|
||||
package query
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox/base"
|
||||
"github.com/mainnika/mongox-go-driver/v2/mongox/errors"
|
||||
"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
|
||||
@@ -15,7 +17,7 @@ func Compose(filters ...interface{}) *Query {
|
||||
|
||||
for _, f := range filters {
|
||||
if !Push(q, f) {
|
||||
panic(errors.InternalErrorf("unknown filter %v", f))
|
||||
panic(fmt.Errorf("unknown filter %v", f))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,6 +27,10 @@ func Compose(filters ...interface{}) *Query {
|
||||
// Push applies single filter to a query
|
||||
func Push(q *Query, f interface{}) bool {
|
||||
|
||||
if utils.IsNil(f) {
|
||||
return true
|
||||
}
|
||||
|
||||
ok := false
|
||||
ok = ok || applyBson(q, f)
|
||||
ok = ok || applyLimit(q, f)
|
||||
@@ -86,13 +92,10 @@ func applyProtection(q *Query, f interface{}) bool {
|
||||
var v *int64
|
||||
|
||||
switch f := f.(type) {
|
||||
case base.Protection:
|
||||
case protection.Key:
|
||||
x = &f.X
|
||||
v = &f.V
|
||||
case *base.Protection:
|
||||
if f == nil {
|
||||
return false
|
||||
}
|
||||
case *protection.Key:
|
||||
x = &f.X
|
||||
v = &f.V
|
||||
|
||||
|
||||
@@ -12,6 +12,5 @@ var _ Preloader = Preload{}
|
||||
|
||||
// Preload returns a preload list
|
||||
func (l Preload) Preload() []string {
|
||||
|
||||
return Preload(l)
|
||||
return l
|
||||
}
|
||||
|
||||
@@ -2,8 +2,6 @@ package query
|
||||
|
||||
import (
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// Query is an enchanched bson.M map
|
||||
@@ -82,11 +80,7 @@ func (q *Query) Preloader() (empty bool, preloader []string) {
|
||||
|
||||
// Empty checks the query for any content
|
||||
func (q *Query) Empty() bool {
|
||||
|
||||
qv := reflect.ValueOf(q.m)
|
||||
keys := qv.MapKeys()
|
||||
|
||||
return len(keys) == 0
|
||||
return len(q.m) == 0
|
||||
}
|
||||
|
||||
// M returns underlying query map
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// IsNil function evaluates the interface value to nil
|
||||
func IsNil(i interface{}) bool {
|
||||
|
||||
type iface struct {
|
||||
_ unsafe.Pointer
|
||||
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