|
@@ -14,6 +14,7 @@ import (
|
|
|
|
|
|
"git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/api/errs"
|
|
"git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/api/errs"
|
|
"github.com/davecgh/go-spew/spew"
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
+ "github.com/kataras/iris"
|
|
iriscontext "github.com/kataras/iris/v12/context"
|
|
iriscontext "github.com/kataras/iris/v12/context"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
@@ -79,11 +80,11 @@ type Mongo struct {
|
|
// Password string `json:"password"`
|
|
// Password string `json:"password"`
|
|
// DataBase string `json:"database"`
|
|
// DataBase string `json:"database"`
|
|
// Addrs string `json:"addrs"`
|
|
// Addrs string `json:"addrs"`
|
|
- subject *BehaviorSubjectStruct
|
|
|
|
- client *mongo.Client
|
|
|
|
|
|
+ subject *BehaviorSubjectStruct
|
|
|
|
+ client *mongo.Client
|
|
Credential *options.Credential
|
|
Credential *options.Credential
|
|
- Config string `json:"config"`
|
|
|
|
- Clients map[string]*mongo.Client
|
|
|
|
|
|
+ Config string `json:"config"`
|
|
|
|
+ Clients map[string]*mongo.Client
|
|
}
|
|
}
|
|
type execfn func(context.Context, *mongo.Collection)
|
|
type execfn func(context.Context, *mongo.Collection)
|
|
|
|
|
|
@@ -97,10 +98,11 @@ type Filter struct {
|
|
UserId primitive.ObjectID
|
|
UserId primitive.ObjectID
|
|
// NextPageToken primitive.ObjectID
|
|
// NextPageToken primitive.ObjectID
|
|
// PageToken PageToken
|
|
// PageToken PageToken
|
|
- Fields *bson.M
|
|
|
|
- Query *bson.M
|
|
|
|
- Patchs *bson.A
|
|
|
|
- Sort *bson.M
|
|
|
|
|
|
+ Fields *bson.M
|
|
|
|
+ Query *bson.M
|
|
|
|
+ Patchs *bson.A
|
|
|
|
+ Sort *bson.M
|
|
|
|
+ Context *iriscontext.Context
|
|
// Sort []string
|
|
// Sort []string
|
|
Collection string
|
|
Collection string
|
|
QueryType string
|
|
QueryType string
|
|
@@ -171,7 +173,7 @@ func (t *Mongo) Ready() *BehaviorSubjectStruct {
|
|
}
|
|
}
|
|
|
|
|
|
func (t *Mongo) Init() (err error) {
|
|
func (t *Mongo) Init() (err error) {
|
|
- defer func(){
|
|
|
|
|
|
+ defer func() {
|
|
spew.Dump(err)
|
|
spew.Dump(err)
|
|
}()
|
|
}()
|
|
|
|
|
|
@@ -245,7 +247,10 @@ func (t *Mongo) Dispatch(f *Filter) {
|
|
}
|
|
}
|
|
|
|
|
|
func (t *Mongo) InsertOne(f *Filter) (res *mongo.InsertOneResult, err *errs.Error) {
|
|
func (t *Mongo) InsertOne(f *Filter) (res *mongo.InsertOneResult, err *errs.Error) {
|
|
-
|
|
|
|
|
|
+ defer func() {
|
|
|
|
+ event := createDebugEvent(f, "models.insert.one")
|
|
|
|
+ event.Error = err
|
|
|
|
+ }()
|
|
f.Insertion = InsertOne
|
|
f.Insertion = InsertOne
|
|
|
|
|
|
t.exec(f, func(ctx context.Context, collection *mongo.Collection) {
|
|
t.exec(f, func(ctx context.Context, collection *mongo.Collection) {
|
|
@@ -262,6 +267,10 @@ func (t *Mongo) InsertOne(f *Filter) (res *mongo.InsertOneResult, err *errs.Erro
|
|
}
|
|
}
|
|
|
|
|
|
func (t *Mongo) InsertMany(f *Filter) (res *mongo.InsertManyResult, err *errs.Error) {
|
|
func (t *Mongo) InsertMany(f *Filter) (res *mongo.InsertManyResult, err *errs.Error) {
|
|
|
|
+ defer func() {
|
|
|
|
+ event := createDebugEvent(f, "models.insert.many")
|
|
|
|
+ event.Error = err
|
|
|
|
+ }()
|
|
|
|
|
|
f.Insertion = InsertMany
|
|
f.Insertion = InsertMany
|
|
|
|
|
|
@@ -285,6 +294,10 @@ func (t *Mongo) InsertMany(f *Filter) (res *mongo.InsertManyResult, err *errs.Er
|
|
|
|
|
|
//Remove os elementos da colecao, selecionados pela query
|
|
//Remove os elementos da colecao, selecionados pela query
|
|
func (t *Mongo) RemoveOne(f *Filter) (res *mongo.DeleteResult, err *errs.Error) {
|
|
func (t *Mongo) RemoveOne(f *Filter) (res *mongo.DeleteResult, err *errs.Error) {
|
|
|
|
+ defer func() {
|
|
|
|
+ event := createDebugEvent(f, "models.remove.one")
|
|
|
|
+ event.Error = err
|
|
|
|
+ }()
|
|
|
|
|
|
f.CheckQuery = true
|
|
f.CheckQuery = true
|
|
|
|
|
|
@@ -309,6 +322,11 @@ func (t *Mongo) RemoveOne(f *Filter) (res *mongo.DeleteResult, err *errs.Error)
|
|
func (t *Mongo) RemoveMany(f *Filter) (res *mongo.DeleteResult, err *errs.Error) {
|
|
func (t *Mongo) RemoveMany(f *Filter) (res *mongo.DeleteResult, err *errs.Error) {
|
|
var lerr error
|
|
var lerr error
|
|
|
|
|
|
|
|
+ defer func() {
|
|
|
|
+ event := createDebugEvent(f, "models.remove.many")
|
|
|
|
+ event.Error = err
|
|
|
|
+ }()
|
|
|
|
+
|
|
f.CheckQuery = true
|
|
f.CheckQuery = true
|
|
|
|
|
|
t.exec(f, func(ctx context.Context, collection *mongo.Collection) {
|
|
t.exec(f, func(ctx context.Context, collection *mongo.Collection) {
|
|
@@ -330,6 +348,11 @@ func (t *Mongo) RemoveMany(f *Filter) (res *mongo.DeleteResult, err *errs.Error)
|
|
func (t *Mongo) UpdateOne(f *Filter) (res *mongo.UpdateResult, err *errs.Error) {
|
|
func (t *Mongo) UpdateOne(f *Filter) (res *mongo.UpdateResult, err *errs.Error) {
|
|
var lerr error
|
|
var lerr error
|
|
|
|
|
|
|
|
+ defer func() {
|
|
|
|
+ event := createDebugEvent(f, "models.update.one")
|
|
|
|
+ event.Error = err
|
|
|
|
+ }()
|
|
|
|
+
|
|
f.Insertion = InsertOne
|
|
f.Insertion = InsertOne
|
|
f.CheckQuery = true
|
|
f.CheckQuery = true
|
|
|
|
|
|
@@ -356,6 +379,11 @@ func (t *Mongo) UpdateMany(f *Filter) (res *mongo.UpdateResult, err *errs.Error)
|
|
value = reflect.ValueOf(f.Entities)
|
|
value = reflect.ValueOf(f.Entities)
|
|
)
|
|
)
|
|
|
|
|
|
|
|
+ defer func() {
|
|
|
|
+ event := createDebugEvent(f, "models.update.many")
|
|
|
|
+ event.Error = err
|
|
|
|
+ }()
|
|
|
|
+
|
|
f.Insertion = InsertMany
|
|
f.Insertion = InsertMany
|
|
f.CheckQuery = true
|
|
f.CheckQuery = true
|
|
|
|
|
|
@@ -431,9 +459,9 @@ func ParseQuery(ctx iriscontext.Context, filter *Filter, basequery string, data
|
|
})
|
|
})
|
|
return
|
|
return
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
x := buf.Bytes()
|
|
x := buf.Bytes()
|
|
-
|
|
|
|
|
|
+
|
|
fmt.Println("buf.Bytes()", string(x))
|
|
fmt.Println("buf.Bytes()", string(x))
|
|
|
|
|
|
if templateErr = bson.UnmarshalExtJSON(x, false, &filter.Query); templateErr != nil {
|
|
if templateErr = bson.UnmarshalExtJSON(x, false, &filter.Query); templateErr != nil {
|
|
@@ -446,7 +474,10 @@ func ParseQuery(ctx iriscontext.Context, filter *Filter, basequery string, data
|
|
}
|
|
}
|
|
|
|
|
|
func (t *Mongo) PatchOne(f *Filter) (res *mongo.UpdateResult, err *errs.Error) {
|
|
func (t *Mongo) PatchOne(f *Filter) (res *mongo.UpdateResult, err *errs.Error) {
|
|
-
|
|
|
|
|
|
+ defer func() {
|
|
|
|
+ event := createDebugEvent(f, "models.patch.one")
|
|
|
|
+ event.Error = err
|
|
|
|
+ }()
|
|
f.Insertion = Patch
|
|
f.Insertion = Patch
|
|
f.CheckQuery = true
|
|
f.CheckQuery = true
|
|
|
|
|
|
@@ -516,6 +547,10 @@ func (t *Mongo) PatchOne(f *Filter) (res *mongo.UpdateResult, err *errs.Error) {
|
|
}
|
|
}
|
|
|
|
|
|
func (t *Mongo) PatchMany(f *Filter) (res *mongo.UpdateResult, err *errs.Error) {
|
|
func (t *Mongo) PatchMany(f *Filter) (res *mongo.UpdateResult, err *errs.Error) {
|
|
|
|
+ defer func() {
|
|
|
|
+ event := createDebugEvent(f, "models.patch.many")
|
|
|
|
+ event.Error = err
|
|
|
|
+ }()
|
|
|
|
|
|
f.Insertion = Patch
|
|
f.Insertion = Patch
|
|
f.CheckQuery = true
|
|
f.CheckQuery = true
|
|
@@ -603,6 +638,10 @@ func (mongo *Mongo) FindOneRx(options *Filter) *ObservableStruct {
|
|
}
|
|
}
|
|
|
|
|
|
func (t *Mongo) FindOne(f *Filter) (res *mongo.SingleResult, err *errs.Error) {
|
|
func (t *Mongo) FindOne(f *Filter) (res *mongo.SingleResult, err *errs.Error) {
|
|
|
|
+ defer func() {
|
|
|
|
+ event := createDebugEvent(f, "models.find.one")
|
|
|
|
+ event.Error = err
|
|
|
|
+ }()
|
|
|
|
|
|
f.CheckQuery = (f.QueryType != "aggregate")
|
|
f.CheckQuery = (f.QueryType != "aggregate")
|
|
|
|
|
|
@@ -652,6 +691,10 @@ func findIds(filter *Filter) (ids []primitive.ObjectID) {
|
|
func (t *Mongo) FindMany(f *Filter) (cursor *mongo.Cursor, err *errs.Error) {
|
|
func (t *Mongo) FindMany(f *Filter) (cursor *mongo.Cursor, err *errs.Error) {
|
|
|
|
|
|
// f.CheckQuery = true
|
|
// f.CheckQuery = true
|
|
|
|
+ defer func() {
|
|
|
|
+ event := createDebugEvent(f, "models.find.many")
|
|
|
|
+ event.Error = err
|
|
|
|
+ }()
|
|
|
|
|
|
t.exec(f, func(ctx context.Context, collection *mongo.Collection) {
|
|
t.exec(f, func(ctx context.Context, collection *mongo.Collection) {
|
|
var (
|
|
var (
|
|
@@ -747,8 +790,21 @@ func (t *Mongo) FindMany(f *Filter) (cursor *mongo.Cursor, err *errs.Error) {
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+func createDebugEvent(options *Filter, eventType string) *DebugEvent {
|
|
|
|
+ debug := options.Context.Values().Get("#debug")
|
|
|
|
+ event := debug.Event(eventType)
|
|
|
|
+ event.Data = iris.Map{}
|
|
|
|
+ return event
|
|
|
|
+}
|
|
|
|
+
|
|
func (models *Mongo) Exists(options *Filter) (exists bool, err *errs.Error) {
|
|
func (models *Mongo) Exists(options *Filter) (exists bool, err *errs.Error) {
|
|
|
|
|
|
|
|
+ defer func() {
|
|
|
|
+ event := createDebugEvent(options, "models.exists")
|
|
|
|
+ event.Data = iris.Map{"response": exists}
|
|
|
|
+ event.Error = err
|
|
|
|
+ }()
|
|
|
|
+
|
|
options.Fields = &bson.M{"_id": 1}
|
|
options.Fields = &bson.M{"_id": 1}
|
|
|
|
|
|
if _, err = models.FindOne(options); err != nil {
|
|
if _, err = models.FindOne(options); err != nil {
|