From 7ca4d7b11b7b557ac3676ce1d984c25da14370e9 Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Thu, 26 Sep 2019 09:30:40 +0700 Subject: [PATCH 01/51] fix update and insert --- query.go | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/query.go b/query.go index 6900b4c..974c1c4 100644 --- a/query.go +++ b/query.go @@ -34,7 +34,7 @@ func (q *Query) BuildFilter(f *df.Filter) (interface{}, error) { } else if f.Op == df.OpNe { fm.Set(f.Field, M{}.Set("$ne", f.Value)) } else if f.Op == df.OpContains { - fs := f.Value.([]string) + fs := f.Value.([]interface{}) if len(fs) > 1 { bfs := []interface{}{} for _, ff := range fs { @@ -251,13 +251,17 @@ func (q *Query) Execute(m M) (interface{}, error) { ct := q.Config(df.ConfigKeyCommandType, "N/A") switch ct { case df.QueryInsert: - return coll.InsertOne(conn.ctx, data) + res, err := coll.InsertOne(conn.ctx, data) + if err != nil { + return nil, err + } + return res.InsertedID, nil case df.QueryUpdate: var err error if hasWhere { - //singleupdate := m.Get("singleupdate", true).(bool) - singleupdate := false + singleupdate := m.Get("singleupdate", false).(bool) + //singleupdate := false if !singleupdate { //-- get the field for update updateqi, _ := parts[df.QueryUpdate] @@ -285,11 +289,13 @@ func (q *Query) Execute(m M) (interface{}, error) { } //updatedData := toolkit.M{}.Set("$set", dataS) - _, err = coll.UpdateMany(conn.ctx, where, dataS, - new(options.UpdateOptions).SetUpsert(true)) + _, err = coll.UpdateMany(conn.ctx, where, + toolkit.M{}.Set("$set", dataS), + new(options.UpdateOptions).SetUpsert(false)) } else { - _, err = coll.UpdateOne(conn.ctx, where, data, - new(options.UpdateOptions).SetUpsert(true)) + _, err = coll.UpdateOne(conn.ctx, where, + toolkit.M{}.Set("$set", data), + new(options.UpdateOptions).SetUpsert(false)) } return nil, err } else { From 57e601f15e2c0ef12f3ccee3c372e2b9249e872b Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Sun, 26 Apr 2020 00:06:01 +0700 Subject: [PATCH 02/51] k-upd various --- connection.go | 74 +++++++++--------- cursor.go | 14 +++- flexmgo.go | 2 +- flexmgo_test.go | 119 +++++++++++++++++++++++++++-- query.go | 197 ++++++++++++++++++++++++++++++++++-------------- 5 files changed, 306 insertions(+), 100 deletions(-) diff --git a/connection.go b/connection.go index 579ffaa..6fd608f 100644 --- a/connection.go +++ b/connection.go @@ -2,10 +2,11 @@ package flexmgo import ( "context" + "fmt" "strings" "time" - "git.eaciitapp.com/sebar/dbflex" + "git.kanosolution.net/kano/dbflex" "github.com/eaciit/toolkit" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" @@ -16,6 +17,7 @@ type Connection struct { ctx context.Context client *mongo.Client db *mongo.Database + sess mongo.Session } func (c *Connection) Connect() error { @@ -40,6 +42,10 @@ func (c *Connection) Connect() error { case "serverselectiontimeout": opts.SetServerSelectionTimeout( time.Duration(toolkit.ToInt(v, toolkit.RoundingAuto)) * time.Millisecond) + + case "replicaset": + opts.SetReplicaSet(v.(string)) + //opts.SetWriteConcern() } } @@ -103,48 +109,48 @@ func (c *Connection) DropTable(name string) error { return c.db.Collection(name).Drop(c.ctx) } -/* -func (c *Connection) Prepare(dbflex.ICommand) (dbflex.IQuery, error) { - panic("not implemented") -} - -func (c *Connection) Execute(dbflex.ICommand, toolkit.M) (interface{}, error) { - panic("not implemented") -} - -func (c *Connection) Cursor(dbflex.ICommand, toolkit.M) dbflex.ICursor { - panic("not implemented") -} +func (c *Connection) BeginTx() error { + if c.sess != nil { + return fmt.Errorf("session already exist. Pls commit or rollback last") + } -func (c *Connection) NewQuery() dbflex.IQuery { - panic("not implemented") + sess, err := c.client.StartSession() + if err != nil { + return fmt.Errorf("unable to start new transaction. %s", err.Error()) + } + sess.StartTransaction() + c.sess = sess + return nil } -func (c *Connection) ObjectNames(dbflex.ObjTypeEnum) []string { - panic("not implemented") -} +func (c *Connection) Commit() error { + if c.sess == nil { + return fmt.Errorf("transaction session is not exists yet") + } -func (c *Connection) ValidateTable(interface{}, bool) error { - panic("not implemented") -} + err := c.sess.CommitTransaction(c.ctx) + if err != nil { + return fmt.Errorf("unable to commit. %s", err.Error()) + } -func (c *Connection) DropTable(string) error { - panic("not implemented") + c.sess = nil + return nil } -func (c *Connection) SetThis(dbflex.IConnection) dbflex.IConnection { - panic("not implemented") -} +func (c *Connection) Rollback() error { + if c.sess == nil { + return fmt.Errorf("transaction session is not exists yet") + } -func (c *Connection) This() dbflex.IConnection { - panic("not implemented") -} + err := c.sess.AbortTransaction(c.ctx) + if err != nil { + return fmt.Errorf("unable to commit. %s", err.Error()) + } -func (c *Connection) SetFieldNameTag(string) { - panic("not implemented") + c.sess = nil + return nil } -func (c *Connection) FieldNameTag() string { - panic("not implemented") +func (c *Connection) IsTx() bool { + return c.sess != nil } -*/ diff --git a/cursor.go b/cursor.go index 5248998..48dd76c 100644 --- a/cursor.go +++ b/cursor.go @@ -5,7 +5,7 @@ import ( "io" "reflect" - "git.eaciitapp.com/sebar/dbflex" + "git.kanosolution.net/kano/dbflex" "github.com/eaciit/toolkit" "go.mongodb.org/mongo-driver/mongo" ) @@ -27,9 +27,19 @@ func (cr *Cursor) Close() { } func (cr *Cursor) Count() int { + if cr.countParm == nil || len(cr.countParm) == 0 { + return 0 + } + + if cr.countParm.Get("count") == "" { + return 0 + } + sr := cr.conn.db.RunCommand(cr.conn.ctx, cr.countParm) if sr.Err() != nil { - dbflex.Logger().Errorf("unablet to get count. %s", sr.Err().Error()) + dbflex.Logger().Errorf("unable to get count. %s, countparm: %s", + sr.Err().Error(), + toolkit.JsonString(cr.countParm)) return -1 } diff --git a/flexmgo.go b/flexmgo.go index 3012413..e94b6b8 100644 --- a/flexmgo.go +++ b/flexmgo.go @@ -1,6 +1,6 @@ package flexmgo -import "git.eaciitapp.com/sebar/dbflex" +import "git.kanosolution.net/kano/dbflex" func init() { dbflex.RegisterDriver("mongodb", func(si *dbflex.ServerInfo) dbflex.IConnection { diff --git a/flexmgo_test.go b/flexmgo_test.go index 938e8de..35fd15e 100644 --- a/flexmgo_test.go +++ b/flexmgo_test.go @@ -3,17 +3,16 @@ package flexmgo_test import ( "bufio" "bytes" - "errors" "fmt" "math" "strings" "testing" "time" - "git.eaciitapp.com/sebar/dbflex/orm" + "git.kanosolution.net/kano/dbflex/orm" _ "github.com/eaciit/flexmgo" - "git.eaciitapp.com/sebar/dbflex" + "git.kanosolution.net/kano/dbflex" "github.com/eaciit/toolkit" cv "github.com/smartystreets/goconvey/convey" ) @@ -162,7 +161,8 @@ func TestUpdateData(t *testing.T) { cv.So(r.Title, cv.ShouldStartWith, "Title is ") cv.Convey("update data", func() { - _, err = conn.Execute(dbflex.From(tablename).Save(), toolkit.M{}.Set("data", r)) + _, err = conn.Execute(dbflex.From(tablename).Update("title"), + toolkit.M{}.Set("data", r)) cv.So(err, cv.ShouldBeNil) cv.Convey("vaidate", func() { @@ -175,6 +175,100 @@ func TestUpdateData(t *testing.T) { }) } +type country struct { + ID string `bson:"_id" json:"_id" ecname:"_id"` + Title string +} + +type state struct { + ID string `bson:"_id" json:"_id" ecname:"_id"` + Title string + CountryID string +} + +var ( + countriesTableName = "countries" + stateTableName = "states" +) + +func TestMdbTrx(t *testing.T) { + t.Skip() + cv.Convey("connect", t, func() { + conn, err := connect() + cv.So(err, cv.ShouldBeNil) + defer conn.Close() + + cv.Convey("insert countries without trx", func() { + countries := []*country{ + &country{"SG", "Singapore"}, + &country{"ID", "Indonesia"}, + &country{"MY", "Malaysia"}, + &country{"IN", "India"}, + } + + err = nil + cmd := dbflex.From(countriesTableName).Save() + for _, country := range countries { + _, err = conn.Execute(cmd, toolkit.M{}.Set("data", country)) + if err != nil { + break + } + } + cv.So(err, cv.ShouldBeNil) + + cmd = dbflex.From(countriesTableName).Select() + cur := conn.Cursor(cmd, nil) + cv.So(cur.Error(), cv.ShouldBeNil) + defer cur.Close() + + ms := []toolkit.M{} + cv.So(cur.Fetchs(&ms, 0), cv.ShouldBeNil) + cv.So(len(ms), cv.ShouldEqual, len(countries)) + + cv.Convey("insert state with trx", func() { + err = conn.BeginTx() + cv.So(err, cv.ShouldBeNil) + + states := []*state{ + &state{"SG", "Singapore", "SG"}, + &state{"JK", "Jakarta", "ID"}, + &state{"MB", "Mumbai", "IN"}, + } + cmd := dbflex.From(stateTableName).Save() + for _, state := range states { + _, err = conn.Execute(cmd, toolkit.M{}.Set("data", state)) + if err != nil { + break + } + } + cv.So(err, cv.ShouldBeNil) + + cmd = dbflex.From(stateTableName).Select() + cur := conn.Cursor(cmd, nil) + cv.So(cur.Error(), cv.ShouldBeNil) + defer cur.Close() + ms := []toolkit.M{} + cv.So(cur.Fetchs(&ms, 0), cv.ShouldBeNil) + cv.So(len(ms), cv.ShouldEqual, len(states)) + + cv.Convey("rollback", func() { + err = conn.RollBack() + //cv.Println("Tx status", conn.IsTx()) + cv.So(err, cv.ShouldBeNil) + + cmd = dbflex.From(stateTableName).Select() + cur := conn.Cursor(cmd, nil) + cv.So(cur.Error(), cv.ShouldBeNil) + defer cur.Close() + ms1 := []toolkit.M{} + cv.So(cur.Fetchs(&ms1, 0), cv.ShouldBeNil) + cv.So(len(ms1), cv.ShouldEqual, 0) + }) + }) + }) + }) +} + /* func TestWatch(t *testing.T) { cv.Convey("change stream", t, func() { @@ -326,6 +420,7 @@ TO DO func connect() (dbflex.IConnection, error) { if conn, err := dbflex.NewConnectionFromURI(connTxt, nil); err == nil { if err = conn.Connect(); err == nil { + conn.SetFieldNameTag("json") return conn, nil } else { return nil, err @@ -333,7 +428,6 @@ func connect() (dbflex.IConnection, error) { } else { return nil, err } - return nil, errors.New("not implemented yet") } type Record struct { @@ -356,3 +450,18 @@ func (r *Record) GetID() ([]string, []interface{}) { func (r *Record) SetID(obj []interface{}) { r.ID = obj[0].(string) } + +/* +var demoConfig = { + _id: "rs", + members: [ + { _id: 0, + host: 'localhost:27017', + priority: 10 + }, + { _id: 1, + host: 'localhost:27018' + } + ] + }; +*/ diff --git a/query.go b/query.go index 974c1c4..bf0c117 100644 --- a/query.go +++ b/query.go @@ -1,13 +1,13 @@ package flexmgo import ( + "context" "fmt" "io" "strings" - "time" - "git.eaciitapp.com/sebar/dbflex" - df "git.eaciitapp.com/sebar/dbflex" + "git.kanosolution.net/kano/dbflex" + df "git.kanosolution.net/kano/dbflex" "github.com/eaciit/toolkit" . "github.com/eaciit/toolkit" @@ -30,7 +30,7 @@ func (q *Query) BuildCommand() (interface{}, error) { func (q *Query) BuildFilter(f *df.Filter) (interface{}, error) { fm := M{} if f.Op == df.OpEq { - fm.Set(f.Field, M{}.Set("$eq", f.Value)) + fm.Set(f.Field, f.Value) } else if f.Op == df.OpNe { fm.Set(f.Field, M{}.Set("$ne", f.Value)) } else if f.Op == df.OpContains { @@ -146,26 +146,42 @@ func (q *Query) Cursor(m M) df.ICursor { } if hasWhere { + //fmt.Println("filters:", toolkit.JsonString(where)) pipes = append(pipes, M{}.Set("$match", where)) } pipes = append(pipes, M{}.Set("$group", aggrExpression)) - cur, err := coll.Aggregate(conn.ctx, pipes, new(options.AggregateOptions).SetAllowDiskUse(true)) + var cur *mongo.Cursor + err := wrapTx(conn, func(ctx context.Context) error { + var err error + cur, err = coll.Aggregate(ctx, pipes, new(options.AggregateOptions).SetAllowDiskUse(true)) + return err + }) if err != nil { cursor.SetError(err) } else { cursor.cursor = cur cursor.conn = conn - cursor.countParm = toolkit.M{}. - Set("count", tablename). - Set("query", where) + if len(where) == 0 { + cursor.countParm = toolkit.M{}. + Set("count", tablename). + Set("query", where) + } else { + cursor.countParm = toolkit.M{}. + Set("count", tablename). + Set("query", where) + } } } else if hasCommand { mCmd := commandParts[0].Value.(toolkit.M) cmdObj, _ := mCmd["command"] switch cmdObj.(type) { case toolkit.M: - //cmdParm := cmdObj.(toolkit.M).Get("commandParm") - curCommand, err := conn.db.RunCommandCursor(conn.ctx, cmdObj) + var curCommand *mongo.Cursor + err := wrapTx(conn, func(ctx context.Context) error { + var err error + curCommand, err = conn.db.RunCommandCursor(ctx, cmdObj) + return err + }) if err != nil { cursor.SetError(err) } else { @@ -173,6 +189,43 @@ func (q *Query) Cursor(m M) df.ICursor { } return cursor + case string: + switch cmdObj.(string) { + case "aggregate": + pipes := []toolkit.M{} + if hasWhere && len(where) > 0 { + pipes = append(pipes, M{}.Set("$match", where)) + } + if pipeM, hasPipe := m["pipe"]; hasPipe { + var ( + pipeMs []toolkit.M + //ok bool + cur *mongo.Cursor + err error + ) + + toolkit.Serde(pipeM, &pipeMs, "") + if len(pipeMs) > 0 { + if _, has := pipeMs[0]["$text"]; has { + pipes = pipeMs + } else { + pipes = append(pipes, pipeMs...) + } + } + + //fmt.Println("pipe:", toolkit.JsonString(pipes), "\n") + if cur, err = coll.Aggregate(conn.ctx, pipes, new(options.AggregateOptions).SetAllowDiskUse(true)); err != nil { + cursor.SetError(err) + return cursor + } + + cursor.cursor = cur + cursor.conn = conn + cursor.countParm = nil + return cursor + } + } + default: cursor.SetError(toolkit.Errorf("invalid command %v", cmdObj)) return cursor @@ -197,25 +250,42 @@ func (q *Query) Cursor(m M) df.ICursor { } else { opt := options.Find() if items, ok := parts[df.QuerySelect]; ok { - fields := items[0].Value.([]string) - if len(fields) > 0 { - opt.SetProjection(fields) + if fields, ok := items[0].Value.([]string); ok { + if len(fields) > 0 { + projection := toolkit.M{} + for _, field := range fields { + projection.Set(field, 1) + } + opt.SetProjection(projection) + } } } if items, ok := parts[df.QueryOrder]; ok { sortKeys := items[0].Value.([]string) - opt.SetSort(sortKeys) + sortM := toolkit.M{} + for _, key := range sortKeys { + if key[0] == '-' { + sortM.Set(key[1:], -1) + } else { + sortM.Set(key, 1) + } + } + + if len(sortM) > 0 { + opt.SetSort(sortM) + } } if items, ok := parts[df.QuerySkip]; ok { - skip := items[0].Value.(int64) + skip := int64(items[0].Value.(int)) opt.SetSkip(skip) } if items, ok := parts[df.QueryTake]; ok { - take := items[0].Value.(int64) - opt.SetLimit(take) + if take, ok := items[0].Value.(int); ok { + opt.SetLimit(int64(take)) + } } var ( @@ -223,8 +293,12 @@ func (q *Query) Cursor(m M) df.ICursor { err error ) - qry, err = coll.Find(conn.ctx, where, opt) - //toolkit.Logger().Debugf("querying data. where:%v error:%v", where, err) + err = wrapTx(conn, func(ctx context.Context) error { + var err error + //fmt.Println(toolkit.JsonString(opt.Sort)) + qry, err = coll.Find(ctx, where, opt) + return err + }) if err != nil { cursor.SetError(err) @@ -232,7 +306,11 @@ func (q *Query) Cursor(m M) df.ICursor { } cursor.cursor = qry - cursor.countParm = toolkit.M{}.Set("count", tablename).Set("query", where) + if len(where) == 0 { + cursor.countParm = toolkit.M{}.Set("count", tablename) + } else { + cursor.countParm = toolkit.M{}.Set("count", tablename).Set("query", where) + } cursor.conn = conn } return cursor @@ -268,7 +346,7 @@ func (q *Query) Execute(m M) (interface{}, error) { updatevals := updateqi[0].Value.([]string) var dataM toolkit.M - dataM, err = ToM(data) + dataM, err = toolkit.ToMTag(data, conn.FieldNameTag()) dataS := M{} if err != nil { return nil, err @@ -289,13 +367,19 @@ func (q *Query) Execute(m M) (interface{}, error) { } //updatedData := toolkit.M{}.Set("$set", dataS) - _, err = coll.UpdateMany(conn.ctx, where, - toolkit.M{}.Set("$set", dataS), - new(options.UpdateOptions).SetUpsert(false)) + err = wrapTx(conn, func(ctx context.Context) error { + _, err := coll.UpdateMany(ctx, where, + toolkit.M{}.Set("$set", dataS), + new(options.UpdateOptions).SetUpsert(false)) + return err + }) } else { - _, err = coll.UpdateOne(conn.ctx, where, - toolkit.M{}.Set("$set", data), - new(options.UpdateOptions).SetUpsert(false)) + err = wrapTx(conn, func(ctx context.Context) error { + _, err := coll.UpdateOne(ctx, where, + toolkit.M{}.Set("$set", data), + new(options.UpdateOptions).SetUpsert(false)) + return err + }) } return nil, err } else { @@ -304,7 +388,10 @@ func (q *Query) Execute(m M) (interface{}, error) { case df.QueryDelete: if hasWhere { - _, err := coll.DeleteMany(conn.ctx, where) + err := wrapTx(conn, func(ctx context.Context) error { + _, err := coll.DeleteMany(ctx, where) + return err + }) return nil, err } else { return nil, toolkit.Errorf("delete need to have where clause. For delete all data in a collection, please use DropTable instead of Delete") @@ -312,7 +399,7 @@ func (q *Query) Execute(m M) (interface{}, error) { case df.QuerySave: whereSave := M{} - datam, err := toolkit.ToM(data) + datam, err := toolkit.ToMTag(data, conn.FieldNameTag()) if err != nil { return nil, toolkit.Errorf("unable to deserialize data: %s", err.Error()) } @@ -322,9 +409,12 @@ func (q *Query) Execute(m M) (interface{}, error) { return nil, toolkit.Error("_id field is required") } - _, err = coll.UpdateMany(conn.ctx, whereSave, - toolkit.M{}.Set("$set", datam), - new(options.UpdateOptions).SetUpsert(true)) + err = wrapTx(conn, func(ctx context.Context) error { + _, err := coll.UpdateMany(ctx, whereSave, + toolkit.M{}.Set("$set", datam), + new(options.UpdateOptions).SetUpsert(true)) + return err + }) return nil, err case df.QueryCommand: @@ -425,7 +515,6 @@ func (q *Query) Execute(m M) (interface{}, error) { case "gfsremove", "gfsdelete": gfsId, hasId := m["id"] - var err error if hasId && gfsId != "" { err = bucket.Delete(gfsId) @@ -436,35 +525,14 @@ func (q *Query) Execute(m M) (interface{}, error) { err := bucket.Drop() return nil, err - case "watch": - watchFn := m.Get("fn", nil) - if watchFn == nil { - return nil, fmt.Errorf("watch need a func(toolkit.M)") - } - - opt := options.ChangeStream() - opt.SetBatchSize(1024) - opt.SetMaxAwaitTime(24 * time.Hour) - - toolkit.Logger().Debugf("prepare to wacth %s", tablename) - cs, err := coll.Watch(conn.ctx, []toolkit.M{}, opt) + case "distinct": + fieldName := m.GetString("field") + vs, err := coll.Distinct(conn.ctx, fieldName, where) if err != nil { - toolkit.Logger().Debugf("watch %s has error", tablename, err.Error()) return nil, err } - toolkit.Logger().Debugf("watch %s is currently running", tablename) - - go func() { - defer cs.Close(conn.ctx) - for cs.Next(conn.ctx) { - data := toolkit.M{} - if err = cs.Decode(&data); err != nil { - continue - } + return vs, nil - watchFn.(func(toolkit.M))(data) - } - }() default: return nil, toolkit.Errorf("Invalid command: %v", commandTxt) } @@ -486,6 +554,19 @@ func (q *Query) Execute(m M) (interface{}, error) { return nil, nil } +func wrapTx(conn *Connection, fn func(ctx context.Context) error) error { + var err error + //fmt.Println("Connection in tx", conn.IsTx(), " sess", conn.sess) + if conn.sess != nil { + err = mongo.WithSession(conn.ctx, conn.sess, func(sc mongo.SessionContext) error { + return fn(sc) + }) + } else { + err = fn(conn.ctx) + } + return err +} + /* func (q *Query) SetThis(q dbflex.IQuery) { panic("not implemented") From d6d51c710f26c5aac6043305ee8549ce10dfb9c8 Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Sun, 31 May 2020 19:24:05 +0700 Subject: [PATCH 03/51] fixing count issue --- cursor.go | 4 ++-- query.go | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/cursor.go b/cursor.go index 48dd76c..4448193 100644 --- a/cursor.go +++ b/cursor.go @@ -40,13 +40,13 @@ func (cr *Cursor) Count() int { dbflex.Logger().Errorf("unable to get count. %s, countparm: %s", sr.Err().Error(), toolkit.JsonString(cr.countParm)) - return -1 + return 0 } countModel := new(struct{ N int }) if err := sr.Decode(countModel); err != nil { dbflex.Logger().Errorf("unablet to decode count. %s", sr.Err().Error()) - return -1 + return 0 } return countModel.N } diff --git a/query.go b/query.go index bf0c117..156d90a 100644 --- a/query.go +++ b/query.go @@ -163,8 +163,7 @@ func (q *Query) Cursor(m M) df.ICursor { cursor.conn = conn if len(where) == 0 { cursor.countParm = toolkit.M{}. - Set("count", tablename). - Set("query", where) + Set("count", tablename) } else { cursor.countParm = toolkit.M{}. Set("count", tablename). From 4968950ab914d4634ef50142a25b3ee782686186 Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Tue, 9 Jun 2020 08:39:13 +0700 Subject: [PATCH 04/51] remove dep to eaciit --- flexmgo_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flexmgo_test.go b/flexmgo_test.go index 35fd15e..a2c215b 100644 --- a/flexmgo_test.go +++ b/flexmgo_test.go @@ -10,7 +10,7 @@ import ( "time" "git.kanosolution.net/kano/dbflex/orm" - _ "github.com/eaciit/flexmgo" + _ "github.com/ariefdarmawan/flexmgo" "git.kanosolution.net/kano/dbflex" "github.com/eaciit/toolkit" From 24e10ec5d83e7a447a222ddd91a64357e3f280ee Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Fri, 12 Jun 2020 20:39:43 +0700 Subject: [PATCH 05/51] update with latest df --- cursor.go | 27 +++++++++++++++++---------- flexmgo_test.go | 16 ++++++++-------- query.go | 30 ++++++++++++++---------------- 3 files changed, 39 insertions(+), 34 deletions(-) diff --git a/cursor.go b/cursor.go index 4448193..610f244 100644 --- a/cursor.go +++ b/cursor.go @@ -20,10 +20,12 @@ type Cursor struct { cursor *mongo.Cursor } -func (cr *Cursor) Close() { +func (cr *Cursor) Close() error { + e := cr.Error() if cr.mc != nil { cr.mc.Close(cr.conn.ctx) } + return e } func (cr *Cursor) Count() int { @@ -51,25 +53,29 @@ func (cr *Cursor) Count() int { return countModel.N } -func (cr *Cursor) Fetch(out interface{}) error { +func (cr *Cursor) Fetch(out interface{}) dbflex.ICursor { if cr.Error() != nil { - return toolkit.Errorf("unable to fetch data. %s", cr.Error()) + cr.SetError(toolkit.Errorf("unable to fetch data. %s", cr.Error())) + return cr } if neof := cr.cursor.Next(cr.conn.ctx); !neof { - return io.EOF + cr.SetError(io.EOF) + return cr } if err := cr.cursor.Decode(out); err != nil { - return toolkit.Errorf("unable to decode output. %s", err.Error()) + cr.SetError(toolkit.Errorf("unable to decode output. %s", err.Error())) + return cr } - return nil + return cr } -func (cr *Cursor) Fetchs(result interface{}, n int) error { +func (cr *Cursor) Fetchs(result interface{}, n int) dbflex.ICursor { if cr.Error() != nil { - return toolkit.Errorf("unable to fetch data. %s", cr.Error()) + cr.SetError(toolkit.Errorf("unable to fetch data. %s", cr.Error())) + return cr } v := reflect.TypeOf(result).Elem().Elem() @@ -84,7 +90,8 @@ func (cr *Cursor) Fetchs(result interface{}, n int) error { iv := reflect.New(v).Interface() err := cr.cursor.Decode(iv) if err != nil { - return fmt.Errorf("unable to decode cursor data. %s", err.Error()) + cr.SetError(fmt.Errorf("unable to decode cursor data. %s", err.Error())) + return cr } ivs = reflect.Append(ivs, reflect.ValueOf(iv).Elem()) @@ -94,7 +101,7 @@ func (cr *Cursor) Fetchs(result interface{}, n int) error { } } reflect.ValueOf(result).Elem().Set(ivs) - return nil + return cr } /* diff --git a/flexmgo_test.go b/flexmgo_test.go index a2c215b..5f7df53 100644 --- a/flexmgo_test.go +++ b/flexmgo_test.go @@ -137,7 +137,7 @@ func TestListData(t *testing.T) { cur := conn.Cursor(dbflex.From(tablename).Select().Where(sc.filter), nil) defer cur.Close() rs := []*Record{} - err := cur.Fetchs(&rs, 0) + err := cur.Fetchs(&rs, 0).Error() cv.So(err, cv.ShouldBeNil) cv.So(len(rs), cv.ShouldBeGreaterThan, 0) cv.So(sc.validator(rs[0]), cv.ShouldBeTrue) @@ -156,13 +156,13 @@ func TestUpdateData(t *testing.T) { r := new(Record) cmdget := dbflex.From(tablename).Select().Where(dbflex.Eq("_id", "record-id-3")) cur := conn.Cursor(cmdget, nil) - err := cur.Fetch(r) + err := cur.Fetch(r).Error() cv.So(err, cv.ShouldBeNil) cv.So(r.Title, cv.ShouldStartWith, "Title is ") cv.Convey("update data", func() { - _, err = conn.Execute(dbflex.From(tablename).Update("title"), - toolkit.M{}.Set("data", r)) + cmd := dbflex.From(tablename).Update("title") + _, err = conn.Execute(cmd, toolkit.M{}.Set("data", r)) cv.So(err, cv.ShouldBeNil) cv.Convey("vaidate", func() { @@ -222,7 +222,7 @@ func TestMdbTrx(t *testing.T) { defer cur.Close() ms := []toolkit.M{} - cv.So(cur.Fetchs(&ms, 0), cv.ShouldBeNil) + cv.So(cur.Fetchs(&ms, 0).Error(), cv.ShouldBeNil) cv.So(len(ms), cv.ShouldEqual, len(countries)) cv.Convey("insert state with trx", func() { @@ -248,7 +248,7 @@ func TestMdbTrx(t *testing.T) { cv.So(cur.Error(), cv.ShouldBeNil) defer cur.Close() ms := []toolkit.M{} - cv.So(cur.Fetchs(&ms, 0), cv.ShouldBeNil) + cv.So(cur.Fetchs(&ms, 0).Error(), cv.ShouldBeNil) cv.So(len(ms), cv.ShouldEqual, len(states)) cv.Convey("rollback", func() { @@ -261,7 +261,7 @@ func TestMdbTrx(t *testing.T) { cv.So(cur.Error(), cv.ShouldBeNil) defer cur.Close() ms1 := []toolkit.M{} - cv.So(cur.Fetchs(&ms1, 0), cv.ShouldBeNil) + cv.So(cur.Fetchs(&ms1, 0).Error(), cv.ShouldBeNil) cv.So(len(ms1), cv.ShouldEqual, 0) }) }) @@ -341,7 +341,7 @@ func TestAggregateData(t *testing.T) { total := float64(0) for { r := new(Record) - if err := cur2.Fetch(r); err == nil { + if err := cur2.Fetch(r).Error(); err == nil { total += r.Salary } else { break diff --git a/query.go b/query.go index 156d90a..a5ae530 100644 --- a/query.go +++ b/query.go @@ -107,7 +107,7 @@ func (q *Query) Cursor(m M) df.ICursor { tablename := q.Config(df.ConfigKeyTableName, "").(string) coll := conn.db.Collection(tablename) - parts := q.Config(df.ConfigKeyGroupedQueryItems, df.GroupedQueryItems{}).(df.GroupedQueryItems) + parts := q.Config(df.ConfigKeyGroupedQueryItems, df.QueryItems{}).(df.QueryItems) where := q.Config(df.ConfigKeyWhere, M{}).(M) hasWhere := where != nil @@ -118,7 +118,7 @@ func (q *Query) Cursor(m M) df.ICursor { if hasAggr { pipes := []M{} - items := aggrs[0].Value.([]*df.AggrItem) + items := aggrs.Value.([]*df.AggrItem) aggrExpression := M{} for _, item := range items { if item.Op == df.AggrCount { @@ -132,12 +132,10 @@ func (q *Query) Cursor(m M) df.ICursor { } else { groups := func() M { s := M{} - for _, v := range groupby { - gs := v.Value.([]string) - for _, g := range gs { - if strings.TrimSpace(g) != "" { - s.Set(strings.Replace(g, ".", "_", -1), "$"+g) - } + gs := groupby.Value.([]string) + for _, g := range gs { + if strings.TrimSpace(g) != "" { + s.Set(strings.Replace(g, ".", "_", -1), "$"+g) } } return s @@ -171,7 +169,7 @@ func (q *Query) Cursor(m M) df.ICursor { } } } else if hasCommand { - mCmd := commandParts[0].Value.(toolkit.M) + mCmd := commandParts.Value.(toolkit.M) cmdObj, _ := mCmd["command"] switch cmdObj.(type) { case toolkit.M: @@ -249,7 +247,7 @@ func (q *Query) Cursor(m M) df.ICursor { } else { opt := options.Find() if items, ok := parts[df.QuerySelect]; ok { - if fields, ok := items[0].Value.([]string); ok { + if fields, ok := items.Value.([]string); ok { if len(fields) > 0 { projection := toolkit.M{} for _, field := range fields { @@ -261,7 +259,7 @@ func (q *Query) Cursor(m M) df.ICursor { } if items, ok := parts[df.QueryOrder]; ok { - sortKeys := items[0].Value.([]string) + sortKeys := items.Value.([]string) sortM := toolkit.M{} for _, key := range sortKeys { if key[0] == '-' { @@ -277,12 +275,12 @@ func (q *Query) Cursor(m M) df.ICursor { } if items, ok := parts[df.QuerySkip]; ok { - skip := int64(items[0].Value.(int)) + skip := int64(items.Value.(int)) opt.SetSkip(skip) } if items, ok := parts[df.QueryTake]; ok { - if take, ok := items[0].Value.(int); ok { + if take, ok := items.Value.(int); ok { opt.SetLimit(int64(take)) } } @@ -321,7 +319,7 @@ func (q *Query) Execute(m M) (interface{}, error) { coll := conn.db.Collection(tablename) data := m.Get("data") - parts := q.Config(df.ConfigKeyGroupedQueryItems, df.GroupedQueryItems{}).(df.GroupedQueryItems) + parts := q.Config(df.ConfigKeyGroupedQueryItems, df.QueryItems{}).(df.QueryItems) where := q.Config(df.ConfigKeyWhere, M{}).(M) hasWhere := where != nil @@ -342,7 +340,7 @@ func (q *Query) Execute(m M) (interface{}, error) { if !singleupdate { //-- get the field for update updateqi, _ := parts[df.QueryUpdate] - updatevals := updateqi[0].Value.([]string) + updatevals := updateqi.Value.([]string) var dataM toolkit.M dataM, err = toolkit.ToMTag(data, conn.FieldNameTag()) @@ -422,7 +420,7 @@ func (q *Query) Execute(m M) (interface{}, error) { return nil, toolkit.Errorf("No command") } - mCommand := commands[0].Value.(toolkit.M) + mCommand := commands.Value.(toolkit.M) cmd, _ := mCommand["command"] switch cmd.(type) { From 3fa3ed74288987fe946b13a8ecbc171fdb54550e Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Thu, 9 Jul 2020 17:40:19 +0700 Subject: [PATCH 06/51] fix comand --- query.go | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/query.go b/query.go index a5ae530..f27f42f 100644 --- a/query.go +++ b/query.go @@ -169,14 +169,13 @@ func (q *Query) Cursor(m M) df.ICursor { } } } else if hasCommand { - mCmd := commandParts.Value.(toolkit.M) - cmdObj, _ := mCmd["command"] - switch cmdObj.(type) { + cmdValue := commandParts.Value + switch cmdValue.(type) { case toolkit.M: var curCommand *mongo.Cursor err := wrapTx(conn, func(ctx context.Context) error { var err error - curCommand, err = conn.db.RunCommandCursor(ctx, cmdObj) + curCommand, err = conn.db.RunCommandCursor(ctx, cmdValue) return err }) if err != nil { @@ -187,13 +186,13 @@ func (q *Query) Cursor(m M) df.ICursor { return cursor case string: - switch cmdObj.(string) { + switch cmdValue.(string) { case "aggregate": pipes := []toolkit.M{} if hasWhere && len(where) > 0 { pipes = append(pipes, M{}.Set("$match", where)) } - if pipeM, hasPipe := m["pipe"]; hasPipe { + if hasPipe, pipeM := q.Command().HasAttr("CommandParm"); hasPipe { var ( pipeMs []toolkit.M //ok bool @@ -224,7 +223,7 @@ func (q *Query) Cursor(m M) df.ICursor { } default: - cursor.SetError(toolkit.Errorf("invalid command %v", cmdObj)) + cursor.SetError(toolkit.Errorf("invalid command %v", cmdValue)) return cursor } /* @@ -420,12 +419,12 @@ func (q *Query) Execute(m M) (interface{}, error) { return nil, toolkit.Errorf("No command") } - mCommand := commands.Value.(toolkit.M) - cmd, _ := mCommand["command"] - - switch cmd.(type) { + //mCommand := commands.Value.(toolkit.M) + //cmd, _ := + cmdValue := commands.Value + switch cmdValue.(type) { case string: - commandTxt := cmd.(string) + commandTxt := cmdValue.(string) if commandTxt == "" { return nil, toolkit.Errorf("No command") } @@ -446,6 +445,9 @@ func (q *Query) Execute(m M) (interface{}, error) { } } + if hasParm, parm := q.Command().HasAttr("CommandParm"); hasParm { + m = parm.(toolkit.M) + } switch strings.ToLower(commandTxt) { case "gfswrite": var reader io.Reader @@ -535,7 +537,7 @@ func (q *Query) Execute(m M) (interface{}, error) { } case toolkit.M: - cmdM := cmd.(toolkit.M) + cmdM := cmdValue.(toolkit.M) sr := conn.db.RunCommand(conn.ctx, cmdM) if sr.Err() != nil { return nil, toolkit.Errorf("unablet to run command. %s. Command: %s", @@ -544,7 +546,7 @@ func (q *Query) Execute(m M) (interface{}, error) { return sr, nil default: - return nil, toolkit.Errorf("Unknown command %v", cmd) + return nil, toolkit.Errorf("Unknown command %v", cmdValue) } } From 1cd95db1f4c0e055a8ec828c197b6d576c56c2ae Mon Sep 17 00:00:00 2001 From: Bagus Cahyono Date: Wed, 9 Sep 2020 11:28:41 +0700 Subject: [PATCH 07/51] Include config params in connection string --- connection.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/connection.go b/connection.go index 6fd608f..bcd6c35 100644 --- a/connection.go +++ b/connection.go @@ -21,8 +21,16 @@ type Connection struct { } func (c *Connection) Connect() error { + configString := "?" + for k, v := range c.Config { + configString += k + "=" + v.(string) + "&" + } + connURI := "mongodb://" connURI += c.Host + "/" + connURI += configString + + fmt.Println(connURI) opts := options.Client().ApplyURI(connURI) //opts.SetConnectTimeout(5 * time.Second) From 5f5d535f2cb1b4dddd5669c6f3327126e0c09f89 Mon Sep 17 00:00:00 2001 From: Bagus Cahyono Date: Wed, 9 Sep 2020 11:29:52 +0700 Subject: [PATCH 08/51] remove print debug --- connection.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/connection.go b/connection.go index bcd6c35..bd7c48e 100644 --- a/connection.go +++ b/connection.go @@ -30,8 +30,6 @@ func (c *Connection) Connect() error { connURI += c.Host + "/" connURI += configString - fmt.Println(connURI) - opts := options.Client().ApplyURI(connURI) //opts.SetConnectTimeout(5 * time.Second) //opts.SetSocketTimeout(3 * time.Second) From 63598aea899d50ca9d4c7289ac2a25239c6cf957 Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Sat, 19 Sep 2020 20:44:34 +0700 Subject: [PATCH 09/51] fix name sourcing --- cursor.go | 33 ++++++++++++++++++++------------- flexmgo_test.go | 2 +- query.go | 3 ++- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/cursor.go b/cursor.go index 610f244..c6bc658 100644 --- a/cursor.go +++ b/cursor.go @@ -37,20 +37,27 @@ func (cr *Cursor) Count() int { return 0 } - sr := cr.conn.db.RunCommand(cr.conn.ctx, cr.countParm) - if sr.Err() != nil { - dbflex.Logger().Errorf("unable to get count. %s, countparm: %s", - sr.Err().Error(), - toolkit.JsonString(cr.countParm)) - return 0 - } + tableName := cr.countParm.GetString("count") + where := cr.countParm.Get("query", nil) + n, _ := cr.conn.db.Collection(tableName).CountDocuments(cr.conn.ctx, where) + return int(n) + + /* + sr := cr.conn.db.RunCommand(cr.conn.ctx, cr.countParm) + if sr.Err() != nil { + dbflex.Logger().Errorf("unable to get count. %s, countparm: %s", + sr.Err().Error(), + toolkit.JsonString(cr.countParm)) + return 0 + } - countModel := new(struct{ N int }) - if err := sr.Decode(countModel); err != nil { - dbflex.Logger().Errorf("unablet to decode count. %s", sr.Err().Error()) - return 0 - } - return countModel.N + countModel := new(struct{ N int }) + if err := sr.Decode(countModel); err != nil { + dbflex.Logger().Errorf("unablet to decode count. %s", sr.Err().Error()) + return 0 + } + return countModel.N + */ } func (cr *Cursor) Fetch(out interface{}) dbflex.ICursor { diff --git a/flexmgo_test.go b/flexmgo_test.go index 5f7df53..c2d6509 100644 --- a/flexmgo_test.go +++ b/flexmgo_test.go @@ -133,7 +133,7 @@ func TestListData(t *testing.T) { cv.So(err, cv.ShouldBeNil) defer conn.Close() - cv.Convey("fetchs", func() { + cv.Convey("fetch ", func() { cur := conn.Cursor(dbflex.From(tablename).Select().Where(sc.filter), nil) defer cur.Close() rs := []*Record{} diff --git a/query.go b/query.go index f27f42f..3159a20 100644 --- a/query.go +++ b/query.go @@ -325,7 +325,8 @@ func (q *Query) Execute(m M) (interface{}, error) { ct := q.Config(df.ConfigKeyCommandType, "N/A") switch ct { case df.QueryInsert: - res, err := coll.InsertOne(conn.ctx, data) + dataM, _ := toolkit.ToMTag(data, conn.FieldNameTag()) + res, err := coll.InsertOne(conn.ctx, dataM) if err != nil { return nil, err } From 35735156de993e1e16faca3933aaba22bc668612 Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Sun, 20 Sep 2020 00:54:35 +0700 Subject: [PATCH 10/51] add bson.m support for pipe --- query.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/query.go b/query.go index 3159a20..b11a1be 100644 --- a/query.go +++ b/query.go @@ -13,6 +13,7 @@ import ( "bufio" + "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/gridfs" @@ -171,7 +172,7 @@ func (q *Query) Cursor(m M) df.ICursor { } else if hasCommand { cmdValue := commandParts.Value switch cmdValue.(type) { - case toolkit.M: + case toolkit.M, bson.M: var curCommand *mongo.Cursor err := wrapTx(conn, func(ctx context.Context) error { var err error @@ -187,12 +188,12 @@ func (q *Query) Cursor(m M) df.ICursor { case string: switch cmdValue.(string) { - case "aggregate": + case "aggregate", "pipe": pipes := []toolkit.M{} if hasWhere && len(where) > 0 { pipes = append(pipes, M{}.Set("$match", where)) } - if hasPipe, pipeM := q.Command().HasAttr("CommandParm"); hasPipe { + if hasPipe, pipeM := q.Command().HasAttr("pipe"); hasPipe { var ( pipeMs []toolkit.M //ok bool From d861b68e55c88a25c0cfc2f948210de5efee7ae6 Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Sun, 20 Sep 2020 05:17:13 +0700 Subject: [PATCH 11/51] update fetch thus can anticipate rrequiment for bson taggiing --- cursor.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/cursor.go b/cursor.go index c6bc658..ab7e55e 100644 --- a/cursor.go +++ b/cursor.go @@ -71,7 +71,13 @@ func (cr *Cursor) Fetch(out interface{}) dbflex.ICursor { return cr } - if err := cr.cursor.Decode(out); err != nil { + m := toolkit.M{} + if err := cr.cursor.Decode(&m); err != nil { + cr.SetError(toolkit.Errorf("unable to decode output. %s", err.Error())) + return cr + } + + if err := toolkit.Serde(m, out, ""); err != nil { cr.SetError(toolkit.Errorf("unable to decode output. %s", err.Error())) return cr } From dcd99ef073dd8eb342c778325e97ac946a3dc59e Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Sun, 20 Sep 2020 05:27:33 +0700 Subject: [PATCH 12/51] update ftechs to anticipate bson tagging --- cursor.go | 42 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/cursor.go b/cursor.go index ab7e55e..6f3c2ff 100644 --- a/cursor.go +++ b/cursor.go @@ -3,7 +3,6 @@ package flexmgo import ( "fmt" "io" - "reflect" "git.kanosolution.net/kano/dbflex" "github.com/eaciit/toolkit" @@ -91,29 +90,56 @@ func (cr *Cursor) Fetchs(result interface{}, n int) dbflex.ICursor { return cr } - v := reflect.TypeOf(result).Elem().Elem() - ivs := reflect.MakeSlice(reflect.SliceOf(v), 0, 0) - + /* + v := reflect.TypeOf(result).Elem().Elem() + ivs := reflect.MakeSlice(reflect.SliceOf(v), 0, 0) + + read := 0 + for { + if !cr.cursor.Next(cr.conn.ctx) { + break + } + + iv := reflect.New(v).Interface() + err := cr.cursor.Decode(iv) + if err != nil { + cr.SetError(fmt.Errorf("unable to decode cursor data. %s", err.Error())) + return cr + } + ivs = reflect.Append(ivs, reflect.ValueOf(iv).Elem()) + + read++ + if n != 0 && read == n { + break + } + } + reflect.ValueOf(result).Elem().Set(ivs) + */ read := 0 + ms := []toolkit.M{} for { if !cr.cursor.Next(cr.conn.ctx) { break } - iv := reflect.New(v).Interface() - err := cr.cursor.Decode(iv) + m := toolkit.M{} + err := cr.cursor.Decode(&m) if err != nil { cr.SetError(fmt.Errorf("unable to decode cursor data. %s", err.Error())) return cr } - ivs = reflect.Append(ivs, reflect.ValueOf(iv).Elem()) + ms = append(ms, m) read++ if n != 0 && read == n { break } } - reflect.ValueOf(result).Elem().Set(ivs) + if err := toolkit.Serde(ms, result, ""); err != nil { + cr.SetError(fmt.Errorf("unable to decode cursor data. %s", err.Error())) + return cr + } + return cr } From 85b1b3ae35d93d3ad62bf272a519792baf46a4ad Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Mon, 21 Sep 2020 16:32:06 +0700 Subject: [PATCH 13/51] handle date --- cursor.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/cursor.go b/cursor.go index 6f3c2ff..f75e541 100644 --- a/cursor.go +++ b/cursor.go @@ -3,6 +3,7 @@ package flexmgo import ( "fmt" "io" + "time" "git.kanosolution.net/kano/dbflex" "github.com/eaciit/toolkit" @@ -75,7 +76,16 @@ func (cr *Cursor) Fetch(out interface{}) dbflex.ICursor { cr.SetError(toolkit.Errorf("unable to decode output. %s", err.Error())) return cr } - + for mk, mv := range m { + // update date value to date + if mvs, ok := mv.(string); ok { + if mvs[4] == '-' && mvs[7] == '-' && mvs[10] == 'T' { + if dt, err := time.Parse(time.RFC3339, mvs); err == nil { + m.Set(mk, dt) + } + } + } + } if err := toolkit.Serde(m, out, ""); err != nil { cr.SetError(toolkit.Errorf("unable to decode output. %s", err.Error())) return cr @@ -128,6 +138,16 @@ func (cr *Cursor) Fetchs(result interface{}, n int) dbflex.ICursor { cr.SetError(fmt.Errorf("unable to decode cursor data. %s", err.Error())) return cr } + for mk, mv := range m { + // update date value to date + if mvs, ok := mv.(string); ok { + if mvs[4] == '-' && mvs[7] == '-' && mvs[10] == 'T' { + if dt, err := time.Parse(time.RFC3339, mvs); err == nil { + m.Set(mk, dt) + } + } + } + } ms = append(ms, m) read++ From 0585eaeb641fb58182ee1bb874d9c01a6f15a31e Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Mon, 21 Sep 2020 16:54:08 +0700 Subject: [PATCH 14/51] fix date --- cursor.go | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/cursor.go b/cursor.go index f75e541..b652195 100644 --- a/cursor.go +++ b/cursor.go @@ -3,6 +3,7 @@ package flexmgo import ( "fmt" "io" + "reflect" "time" "git.kanosolution.net/kano/dbflex" @@ -78,7 +79,7 @@ func (cr *Cursor) Fetch(out interface{}) dbflex.ICursor { } for mk, mv := range m { // update date value to date - if mvs, ok := mv.(string); ok { + if mvs, ok := mv.(string); ok && len(mvs) >= 11 { if mvs[4] == '-' && mvs[7] == '-' && mvs[10] == 'T' { if dt, err := time.Parse(time.RFC3339, mvs); err == nil { m.Set(mk, dt) @@ -86,9 +87,13 @@ func (cr *Cursor) Fetch(out interface{}) dbflex.ICursor { } } } - if err := toolkit.Serde(m, out, ""); err != nil { - cr.SetError(toolkit.Errorf("unable to decode output. %s", err.Error())) - return cr + if reflect.ValueOf(m).Type().String() == reflect.Indirect(reflect.ValueOf(out)).Type().String() { + reflect.ValueOf(out).Elem().Set(reflect.ValueOf(m)) + } else { + if err := toolkit.Serde(m, out, ""); err != nil { + cr.SetError(toolkit.Errorf("unable to decode output. %s", err.Error())) + return cr + } } return cr @@ -140,10 +145,11 @@ func (cr *Cursor) Fetchs(result interface{}, n int) dbflex.ICursor { } for mk, mv := range m { // update date value to date - if mvs, ok := mv.(string); ok { + if mvs, ok := mv.(string); ok && len(mvs) >= 11 { if mvs[4] == '-' && mvs[7] == '-' && mvs[10] == 'T' { if dt, err := time.Parse(time.RFC3339, mvs); err == nil { m.Set(mk, dt) + //fmt.Println(mk, mvs, dt, m, fmt.Sprintf("%t", m.Get("Created"))) } } } @@ -155,9 +161,13 @@ func (cr *Cursor) Fetchs(result interface{}, n int) dbflex.ICursor { break } } - if err := toolkit.Serde(ms, result, ""); err != nil { - cr.SetError(fmt.Errorf("unable to decode cursor data. %s", err.Error())) - return cr + if reflect.ValueOf(ms).Type().String() == reflect.Indirect(reflect.ValueOf(result)).Type().String() { + reflect.ValueOf(result).Elem().Set(reflect.ValueOf(ms)) + } else { + if err := toolkit.Serde(ms, result, ""); err != nil { + cr.SetError(fmt.Errorf("unable to decode cursor data. %s", err.Error())) + return cr + } } return cr From 209ba93f5c18310f92844a8c81e4b038cda34b1d Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Tue, 24 Nov 2020 17:01:09 +0700 Subject: [PATCH 15/51] fix count --- cursor.go | 9 +++++++-- flexmgo_test.go | 4 ++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/cursor.go b/cursor.go index b652195..0074b54 100644 --- a/cursor.go +++ b/cursor.go @@ -40,8 +40,13 @@ func (cr *Cursor) Count() int { tableName := cr.countParm.GetString("count") where := cr.countParm.Get("query", nil) - n, _ := cr.conn.db.Collection(tableName).CountDocuments(cr.conn.ctx, where) - return int(n) + if where == nil { + n, _ := cr.conn.db.Collection(tableName).CountDocuments(cr.conn.ctx, toolkit.M{}) + return int(n) + } else { + n, _ := cr.conn.db.Collection(tableName).CountDocuments(cr.conn.ctx, where) + return int(n) + } /* sr := cr.conn.db.RunCommand(cr.conn.ctx, cr.countParm) diff --git a/flexmgo_test.go b/flexmgo_test.go index c2d6509..527cbbf 100644 --- a/flexmgo_test.go +++ b/flexmgo_test.go @@ -431,8 +431,8 @@ func connect() (dbflex.IConnection, error) { } type Record struct { - orm.DataModelBase `bson:"-" json:"-" ecname:"-"` - ID string `bson:"_id" json:"_id" ecname:"_id"` + orm.DataModelBase `bson:"-" json:"-"` + ID string `bson:"_id" json:"_id"` Title string Age int Salary float64 From 3803ca5888f213d3299e5f5457e8f958c1a82e77 Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Thu, 31 Dec 2020 10:47:48 +0700 Subject: [PATCH 16/51] updateMany fix to be more efficient --- query.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/query.go b/query.go index b11a1be..d2a4df4 100644 --- a/query.go +++ b/query.go @@ -408,9 +408,8 @@ func (q *Query) Execute(m M) (interface{}, error) { } err = wrapTx(conn, func(ctx context.Context) error { - _, err := coll.UpdateMany(ctx, whereSave, - toolkit.M{}.Set("$set", datam), - new(options.UpdateOptions).SetUpsert(true)) + _, err := coll.ReplaceOne(ctx, whereSave, datam, + new(options.ReplaceOptions).SetUpsert(true)) return err }) return nil, err From 936c7cb674f9bab33e5da6396ec344f2c9f641c5 Mon Sep 17 00:00:00 2001 From: arief Date: Thu, 15 Jul 2021 01:24:55 +0700 Subject: [PATCH 17/51] inserted id is returned aafter insert --- connection.go | 12 +++++ go.mod | 10 ++++ go.sum | 144 ++++++++++++++++++++++++++++++++++++++++++++++++++ query.go | 1 + 4 files changed, 167 insertions(+) create mode 100644 go.mod create mode 100644 go.sum diff --git a/connection.go b/connection.go index bd7c48e..fa18d67 100644 --- a/connection.go +++ b/connection.go @@ -52,6 +52,18 @@ func (c *Connection) Connect() error { case "replicaset": opts.SetReplicaSet(v.(string)) //opts.SetWriteConcern() + + case "poolsize": + poolSize := toolkit.ToInt(v.(string), toolkit.RoundingAuto) + if poolSize > 0 { + opts.SetMaxPoolSize(uint64(poolSize)) + } + + case "idle": + idle := toolkit.ToInt(v.(string), toolkit.RoundingAuto) + if idle > 0 { + opts.SetMaxConnIdleTime(time.Duration(idle) * time.Second) + } } } diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..0ab1b54 --- /dev/null +++ b/go.mod @@ -0,0 +1,10 @@ +module github.com/ariefdarmawan/flexmgo + +go 1.15 + +require ( + git.kanosolution.net/kano/dbflex v1.0.12 + github.com/eaciit/toolkit v0.0.0-20210610161449-593d5fadf78e + github.com/smartystreets/goconvey v1.6.4 + go.mongodb.org/mongo-driver v1.6.0 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..3a61917 --- /dev/null +++ b/go.sum @@ -0,0 +1,144 @@ +git.kanosolution.net/kano/dbflex v1.0.12 h1:FsMeCXjwE0cYRvX5bvp7opYd95pnlmmPMgmVUcFA26Q= +git.kanosolution.net/kano/dbflex v1.0.12/go.mod h1:JwhKlws6r/uBbPE126aJpHbq3x4iugF0ViJlE/fFg/k= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/ariefdarmawan/flexmgo v0.1.15/go.mod h1:ZNVfnly+iMMBJ9sP142Kkt31CeQk+M8LLWYEfF59Qr8= +github.com/aws/aws-sdk-go v1.34.28/go.mod h1:H7NKnBqNVzoTJpGfLrQkkD+ytBA93eiDYi/+8rV9s48= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/eaciit/toolkit v0.0.0-20210610161449-593d5fadf78e h1:2/wdIW25ZaPHfKQ5HvQUSAqeH9KAZQH1PlCMnrSJ9xE= +github.com/eaciit/toolkit v0.0.0-20210610161449-593d5fadf78e/go.mod h1:r4OKDNGrY6n6gCVqEFdld+JTfgdNgp78RPvZHr2B9jU= +github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= +github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk= +github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/gobuffalo/attrs v0.0.0-20190224210810-a9411de4debd/go.mod h1:4duuawTqi2wkkpB4ePgWMaai6/Kc6WEz83bhFwpHzj0= +github.com/gobuffalo/depgen v0.0.0-20190329151759-d478694a28d3/go.mod h1:3STtPUQYuzV0gBVOY3vy6CfMm/ljR4pABfrTeHNLHUY= +github.com/gobuffalo/depgen v0.1.0/go.mod h1:+ifsuy7fhi15RWncXQQKjWS9JPkdah5sZvtHc2RXGlg= +github.com/gobuffalo/envy v1.6.15/go.mod h1:n7DRkBerg/aorDM8kbduw5dN3oXGswK5liaSCx4T5NI= +github.com/gobuffalo/envy v1.7.0/go.mod h1:n7DRkBerg/aorDM8kbduw5dN3oXGswK5liaSCx4T5NI= +github.com/gobuffalo/flect v0.1.0/go.mod h1:d2ehjJqGOH/Kjqcoz+F7jHTBbmDb38yXA598Hb50EGs= +github.com/gobuffalo/flect v0.1.1/go.mod h1:8JCgGVbRjJhVgD6399mQr4fx5rRfGKVzFjbj6RE/9UI= +github.com/gobuffalo/flect v0.1.3/go.mod h1:8JCgGVbRjJhVgD6399mQr4fx5rRfGKVzFjbj6RE/9UI= +github.com/gobuffalo/genny v0.0.0-20190329151137-27723ad26ef9/go.mod h1:rWs4Z12d1Zbf19rlsn0nurr75KqhYp52EAGGxTbBhNk= +github.com/gobuffalo/genny v0.0.0-20190403191548-3ca520ef0d9e/go.mod h1:80lIj3kVJWwOrXWWMRzzdhW3DsrdjILVil/SFKBzF28= +github.com/gobuffalo/genny v0.1.0/go.mod h1:XidbUqzak3lHdS//TPu2OgiFB+51Ur5f7CSnXZ/JDvo= +github.com/gobuffalo/genny v0.1.1/go.mod h1:5TExbEyY48pfunL4QSXxlDOmdsD44RRq4mVZ0Ex28Xk= +github.com/gobuffalo/gitgen v0.0.0-20190315122116-cc086187d211/go.mod h1:vEHJk/E9DmhejeLeNt7UVvlSGv3ziL+djtTr3yyzcOw= +github.com/gobuffalo/gogen v0.0.0-20190315121717-8f38393713f5/go.mod h1:V9QVDIxsgKNZs6L2IYiGR8datgMhB577vzTDqypH360= +github.com/gobuffalo/gogen v0.1.0/go.mod h1:8NTelM5qd8RZ15VjQTFkAW6qOMx5wBbW4dSCS3BY8gg= +github.com/gobuffalo/gogen v0.1.1/go.mod h1:y8iBtmHmGc4qa3urIyo1shvOD8JftTtfcKi+71xfDNE= +github.com/gobuffalo/logger v0.0.0-20190315122211-86e12af44bc2/go.mod h1:QdxcLw541hSGtBnhUc4gaNIXRjiDppFGaDqzbrBd3v8= +github.com/gobuffalo/mapi v1.0.1/go.mod h1:4VAGh89y6rVOvm5A8fKFxYG+wIW6LO1FMTG9hnKStFc= +github.com/gobuffalo/mapi v1.0.2/go.mod h1:4VAGh89y6rVOvm5A8fKFxYG+wIW6LO1FMTG9hnKStFc= +github.com/gobuffalo/packd v0.0.0-20190315124812-a385830c7fc0/go.mod h1:M2Juc+hhDXf/PnmBANFCqx4DM3wRbgDvnVWeG2RIxq4= +github.com/gobuffalo/packd v0.1.0/go.mod h1:M2Juc+hhDXf/PnmBANFCqx4DM3wRbgDvnVWeG2RIxq4= +github.com/gobuffalo/packr/v2 v2.0.9/go.mod h1:emmyGweYTm6Kdper+iywB6YK5YzuKchGtJQZ0Odn4pQ= +github.com/gobuffalo/packr/v2 v2.2.0/go.mod h1:CaAwI0GPIAv+5wKLtv8Afwl+Cm78K/I/VCm/3ptBN+0= +github.com/gobuffalo/syncx v0.0.0-20190224160051-33c29581e754/go.mod h1:HhnNqWY95UYwwW3uSASeV7vtgYkT2t16hJgV3AEPUpw= +github.com/gofrs/flock v0.8.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU= +github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4= +github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM= +github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= +github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= +github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= +github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= +github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= +github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= +github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= +github.com/karrick/godirwalk v1.8.0/go.mod h1:H5KPZjojv4lE+QYImBI8xVtrBRgYrIVsaRPx4tDPEn4= +github.com/karrick/godirwalk v1.10.3/go.mod h1:RoGL9dQei4vP9ilrpETWE8CLOZ1kiN0LhBygSwrAsHA= +github.com/klauspost/compress v1.9.5 h1:U+CaK85mrNNb4k8BNOfgJtJ/gr6kswUCFj6miSzVC6M= +github.com/klauspost/compress v1.9.5/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/markbates/oncer v0.0.0-20181203154359-bf2de49a0be2/go.mod h1:Ld9puTsIW75CHf65OeIOkyKbteujpZVXDpWK6YGZbxE= +github.com/markbates/safe v1.0.1/go.mod h1:nAqgmRi7cY2nqMc92/bSEeQA+R4OheNU2T1kNSCBdG0= +github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc= +github.com/pelletier/go-toml v1.7.0/go.mod h1:vwGMzjaWMwyfHwgIBhI2YUM4fB6nL6lVAvS1LBMMhTE= +github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/go-internal v1.1.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= +github.com/rogpeppe/go-internal v1.2.2/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= +github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= +github.com/sirupsen/logrus v1.4.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= +github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q= +github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= +github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM= +github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= +github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s= +github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= +github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= +github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/theckman/go-flock v0.8.1 h1:kTixuOsFBOtGYSTLRLWK6GOs1hk/8OD11sR1pDd0dl4= +github.com/theckman/go-flock v0.8.1/go.mod h1:kjuth3y9VJ2aNlkNEO99G/8lp9fMIKaGyBmh84IBheM= +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-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c= +github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI= +github.com/xdg-go/scram v1.0.2 h1:akYIkZ28e6A96dkWNJQu3nmCzH3YfwMPQExUYDaRv7w= +github.com/xdg-go/scram v1.0.2/go.mod h1:1WAq6h33pAW+iRreB34OORO2Nf7qel3VV3fjBj+hCSs= +github.com/xdg-go/stringprep v1.0.2 h1:6iq84/ryjjeRmMJwxutI51F2GIPlP5BfTvXHeYjyhBc= +github.com/xdg-go/stringprep v1.0.2/go.mod h1:8F9zXuvzgwmyT5DUm4GUfZGDdT3W+LCvS6+da4O5kxM= +github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d h1:splanxYIlg+5LfHAM6xpdFEAYOk8iySO56hMFq6uLyA= +github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA= +go.mongodb.org/mongo-driver v1.5.4/go.mod h1:gRXCHX4Jo7J0IJ1oDQyUxF7jfy19UfxniMS4xxMmUqw= +go.mongodb.org/mongo-driver v1.6.0 h1:ccc26ylcoRWJQRbjU7GvqfxNzwKcoIcEL3BPuFR/pJ0= +go.mongodb.org/mongo-driver v1.6.0/go.mod h1:Q4oFMbo1+MSNqICAdYMlC/zSTrwCogR4R8NzkI+yfU8= +golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190422162423-af44ce270edf/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE= +golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073 h1:xMPOj6Pz6UipU1wXLkrtqpHbR0AVFnyPEQq/wRWz9lM= +golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200202094626-16171245cfb2 h1:CCH4IOTTfewWjGOlSp+zGcjutRKlBEZQ6wTn8ozI/nI= +golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190412183630-56d357773e84/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e h1:vcxGaoTs7kV8m5Np9uUNQin4BrLOthgV7252N8V+FwY= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190419153524-e8e3143a4f4a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190531175056-4c3a928424d2 h1:T5DasATyLQfmbTpfEXx/IOL9vfjzW6up+ZDkmHvIf2s= +golang.org/x/sys v0.0.0-20190531175056-4c3a928424d2/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.5 h1:i6eZZ+zk0SOf0xgBpEpPD18qWcJda6q1sxt3S0kzyUQ= +golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190329151228-23e29df326fe/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190416151739-9c9e1878f421/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190420181800-aa740d480789/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190531172133-b3315ee88b7d/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/query.go b/query.go index d2a4df4..0dc47c1 100644 --- a/query.go +++ b/query.go @@ -331,6 +331,7 @@ func (q *Query) Execute(m M) (interface{}, error) { if err != nil { return nil, err } + dataM.Set("_id", res.InsertedID) return res.InsertedID, nil case df.QueryUpdate: From 8583060536e805e6a153edcb4351f2c368741e9c Mon Sep 17 00:00:00 2001 From: arief Date: Mon, 19 Jul 2021 18:18:08 +0700 Subject: [PATCH 18/51] enable transaction --- connection.go | 14 ++++++++++---- flexmgo_test.go | 47 ++++++++++++++++++++++++++++++++++------------- go.mod | 2 ++ go.sum | 8 ++++++-- query.go | 26 +++++++++++++++----------- 5 files changed, 67 insertions(+), 30 deletions(-) diff --git a/connection.go b/connection.go index fa18d67..599abf2 100644 --- a/connection.go +++ b/connection.go @@ -10,6 +10,8 @@ import ( "github.com/eaciit/toolkit" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" + "go.mongodb.org/mongo-driver/mongo/readconcern" + "go.mongodb.org/mongo-driver/mongo/writeconcern" ) type Connection struct { @@ -75,7 +77,7 @@ func (c *Connection) Connect() error { //toolkit.Logger().Debug("client generated: OK") if c.ctx == nil { - c.ctx = context.Background() + c.ctx = context.TODO() } //toolkit.Logger().Debug("context generated: OK") @@ -128,6 +130,10 @@ func (c *Connection) DropTable(name string) error { } func (c *Connection) BeginTx() error { + wc := writeconcern.New(writeconcern.WMajority()) + rc := readconcern.Snapshot() + txnOpts := options.Transaction().SetWriteConcern(wc).SetReadConcern(rc) + if c.sess != nil { return fmt.Errorf("session already exist. Pls commit or rollback last") } @@ -136,7 +142,7 @@ func (c *Connection) BeginTx() error { if err != nil { return fmt.Errorf("unable to start new transaction. %s", err.Error()) } - sess.StartTransaction() + sess.StartTransaction(txnOpts) c.sess = sess return nil } @@ -155,14 +161,14 @@ func (c *Connection) Commit() error { return nil } -func (c *Connection) Rollback() error { +func (c *Connection) RollBack() error { if c.sess == nil { return fmt.Errorf("transaction session is not exists yet") } err := c.sess.AbortTransaction(c.ctx) if err != nil { - return fmt.Errorf("unable to commit. %s", err.Error()) + return fmt.Errorf("unable to rollback. %s", err.Error()) } c.sess = nil diff --git a/flexmgo_test.go b/flexmgo_test.go index 527cbbf..577d33d 100644 --- a/flexmgo_test.go +++ b/flexmgo_test.go @@ -192,18 +192,17 @@ var ( ) func TestMdbTrx(t *testing.T) { - t.Skip() cv.Convey("connect", t, func() { - conn, err := connect() + conn, err := connectTrx() cv.So(err, cv.ShouldBeNil) defer conn.Close() cv.Convey("insert countries without trx", func() { countries := []*country{ - &country{"SG", "Singapore"}, - &country{"ID", "Indonesia"}, - &country{"MY", "Malaysia"}, - &country{"IN", "India"}, + {"SG", "Singapore"}, + {"ID", "Indonesia"}, + {"MY", "Malaysia"}, + {"IN", "India"}, } err = nil @@ -226,13 +225,15 @@ func TestMdbTrx(t *testing.T) { cv.So(len(ms), cv.ShouldEqual, len(countries)) cv.Convey("insert state with trx", func() { + conn.Execute(dbflex.From(stateTableName).Delete(), nil) + err = conn.BeginTx() cv.So(err, cv.ShouldBeNil) states := []*state{ - &state{"SG", "Singapore", "SG"}, - &state{"JK", "Jakarta", "ID"}, - &state{"MB", "Mumbai", "IN"}, + {"SG", "Singapore", "SG"}, + {"JK", "Jakarta", "ID"}, + {"MB", "Mumbai", "IN"}, } cmd := dbflex.From(stateTableName).Save() for _, state := range states { @@ -243,26 +244,33 @@ func TestMdbTrx(t *testing.T) { } cv.So(err, cv.ShouldBeNil) + commitErr := conn.Commit() + cv.So(commitErr, cv.ShouldBeNil) + cmd = dbflex.From(stateTableName).Select() cur := conn.Cursor(cmd, nil) cv.So(cur.Error(), cv.ShouldBeNil) - defer cur.Close() + cur.Close() ms := []toolkit.M{} cv.So(cur.Fetchs(&ms, 0).Error(), cv.ShouldBeNil) cv.So(len(ms), cv.ShouldEqual, len(states)) cv.Convey("rollback", func() { + err = conn.BeginTx() + cv.So(err, cv.ShouldBeNil) + + cmd := dbflex.From(stateTableName).Insert() + conn.Execute(cmd, toolkit.M{}.Set("data", &state{"JT", "Jawa Timur", "ID"})) err = conn.RollBack() - //cv.Println("Tx status", conn.IsTx()) cv.So(err, cv.ShouldBeNil) cmd = dbflex.From(stateTableName).Select() cur := conn.Cursor(cmd, nil) cv.So(cur.Error(), cv.ShouldBeNil) - defer cur.Close() + cur.Close() ms1 := []toolkit.M{} cv.So(cur.Fetchs(&ms1, 0).Error(), cv.ShouldBeNil) - cv.So(len(ms1), cv.ShouldEqual, 0) + cv.So(len(ms1), cv.ShouldEqual, len(states)) }) }) }) @@ -430,6 +438,19 @@ func connect() (dbflex.IConnection, error) { } } +func connectTrx() (dbflex.IConnection, error) { + if conn, err := dbflex.NewConnectionFromURI("mongodb://localhost:27201,localhost:27202/rsdb?replicaSet=rs01", nil); err == nil { + if err = conn.Connect(); err == nil { + conn.SetFieldNameTag("json") + return conn, nil + } else { + return nil, err + } + } else { + return nil, err + } +} + type Record struct { orm.DataModelBase `bson:"-" json:"-"` ID string `bson:"_id" json:"_id"` diff --git a/go.mod b/go.mod index 0ab1b54..63464ee 100644 --- a/go.mod +++ b/go.mod @@ -2,6 +2,8 @@ module github.com/ariefdarmawan/flexmgo go 1.15 +replace git.kanosolution.net/kano/dbflex => D:\Users\AriefDarmawan\Coding\go\src\git.kanosolution.net\kano\dbflex + require ( git.kanosolution.net/kano/dbflex v1.0.12 github.com/eaciit/toolkit v0.0.0-20210610161449-593d5fadf78e diff --git a/go.sum b/go.sum index 3a61917..4bdb8be 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ -git.kanosolution.net/kano/dbflex v1.0.12 h1:FsMeCXjwE0cYRvX5bvp7opYd95pnlmmPMgmVUcFA26Q= -git.kanosolution.net/kano/dbflex v1.0.12/go.mod h1:JwhKlws6r/uBbPE126aJpHbq3x4iugF0ViJlE/fFg/k= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/ariefdarmawan/flexmgo v0.1.15/go.mod h1:ZNVfnly+iMMBJ9sP142Kkt31CeQk+M8LLWYEfF59Qr8= +github.com/ariefdarmawan/reflector v0.0.0-20210429160254-3690a39ca6e7 h1:Bo3imCZPoGt6SNGPgJ5oICKscpLO+60d2SG9Gv6DnGg= +github.com/ariefdarmawan/reflector v0.0.0-20210429160254-3690a39ca6e7/go.mod h1:C3eiJ2xw5JT/13i4snLE3F+ToanCZSewAxkCxCepq7o= github.com/aws/aws-sdk-go v1.34.28/go.mod h1:H7NKnBqNVzoTJpGfLrQkkD+ytBA93eiDYi/+8rV9s48= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= @@ -35,6 +35,7 @@ github.com/gobuffalo/packd v0.1.0/go.mod h1:M2Juc+hhDXf/PnmBANFCqx4DM3wRbgDvnVWe github.com/gobuffalo/packr/v2 v2.0.9/go.mod h1:emmyGweYTm6Kdper+iywB6YK5YzuKchGtJQZ0Odn4pQ= github.com/gobuffalo/packr/v2 v2.2.0/go.mod h1:CaAwI0GPIAv+5wKLtv8Afwl+Cm78K/I/VCm/3ptBN+0= github.com/gobuffalo/syncx v0.0.0-20190224160051-33c29581e754/go.mod h1:HhnNqWY95UYwwW3uSASeV7vtgYkT2t16hJgV3AEPUpw= +github.com/gofrs/flock v0.8.1 h1:+gYjHKf32LDeiEEFhQaotPbLuUXjY5ZqxKgXy7n59aw= github.com/gofrs/flock v0.8.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU= github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4= github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= @@ -54,8 +55,10 @@ github.com/klauspost/compress v1.9.5 h1:U+CaK85mrNNb4k8BNOfgJtJ/gr6kswUCFj6miSzV github.com/klauspost/compress v1.9.5/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/markbates/oncer v0.0.0-20181203154359-bf2de49a0be2/go.mod h1:Ld9puTsIW75CHf65OeIOkyKbteujpZVXDpWK6YGZbxE= github.com/markbates/safe v1.0.1/go.mod h1:nAqgmRi7cY2nqMc92/bSEeQA+R4OheNU2T1kNSCBdG0= @@ -136,6 +139,7 @@ golang.org/x/tools v0.0.0-20190531172133-b3315ee88b7d/go.mod h1:/rFqwRUd4F7ZHNgw golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= 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/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/query.go b/query.go index 0dc47c1..f4b3480 100644 --- a/query.go +++ b/query.go @@ -1,7 +1,6 @@ package flexmgo import ( - "context" "fmt" "io" "strings" @@ -150,7 +149,7 @@ func (q *Query) Cursor(m M) df.ICursor { } pipes = append(pipes, M{}.Set("$group", aggrExpression)) var cur *mongo.Cursor - err := wrapTx(conn, func(ctx context.Context) error { + err := wrapTx(conn, func(ctx mongo.SessionContext) error { var err error cur, err = coll.Aggregate(ctx, pipes, new(options.AggregateOptions).SetAllowDiskUse(true)) return err @@ -174,7 +173,7 @@ func (q *Query) Cursor(m M) df.ICursor { switch cmdValue.(type) { case toolkit.M, bson.M: var curCommand *mongo.Cursor - err := wrapTx(conn, func(ctx context.Context) error { + err := wrapTx(conn, func(ctx mongo.SessionContext) error { var err error curCommand, err = conn.db.RunCommandCursor(ctx, cmdValue) return err @@ -290,7 +289,7 @@ func (q *Query) Cursor(m M) df.ICursor { err error ) - err = wrapTx(conn, func(ctx context.Context) error { + err = wrapTx(conn, func(ctx mongo.SessionContext) error { var err error //fmt.Println(toolkit.JsonString(opt.Sort)) qry, err = coll.Find(ctx, where, opt) @@ -326,8 +325,13 @@ func (q *Query) Execute(m M) (interface{}, error) { ct := q.Config(df.ConfigKeyCommandType, "N/A") switch ct { case df.QueryInsert: + var res *mongo.InsertOneResult dataM, _ := toolkit.ToMTag(data, conn.FieldNameTag()) - res, err := coll.InsertOne(conn.ctx, dataM) + err := wrapTx(conn, func(ctx mongo.SessionContext) error { + var err error + res, err = coll.InsertOne(ctx, dataM) + return err + }) if err != nil { return nil, err } @@ -366,14 +370,14 @@ func (q *Query) Execute(m M) (interface{}, error) { } //updatedData := toolkit.M{}.Set("$set", dataS) - err = wrapTx(conn, func(ctx context.Context) error { + err = wrapTx(conn, func(ctx mongo.SessionContext) error { _, err := coll.UpdateMany(ctx, where, toolkit.M{}.Set("$set", dataS), new(options.UpdateOptions).SetUpsert(false)) return err }) } else { - err = wrapTx(conn, func(ctx context.Context) error { + err = wrapTx(conn, func(ctx mongo.SessionContext) error { _, err := coll.UpdateOne(ctx, where, toolkit.M{}.Set("$set", data), new(options.UpdateOptions).SetUpsert(false)) @@ -387,7 +391,7 @@ func (q *Query) Execute(m M) (interface{}, error) { case df.QueryDelete: if hasWhere { - err := wrapTx(conn, func(ctx context.Context) error { + err := wrapTx(conn, func(ctx mongo.SessionContext) error { _, err := coll.DeleteMany(ctx, where) return err }) @@ -408,7 +412,7 @@ func (q *Query) Execute(m M) (interface{}, error) { return nil, toolkit.Error("_id field is required") } - err = wrapTx(conn, func(ctx context.Context) error { + err = wrapTx(conn, func(ctx mongo.SessionContext) error { _, err := coll.ReplaceOne(ctx, whereSave, datam, new(options.ReplaceOptions).SetUpsert(true)) return err @@ -555,7 +559,7 @@ func (q *Query) Execute(m M) (interface{}, error) { return nil, nil } -func wrapTx(conn *Connection, fn func(ctx context.Context) error) error { +func wrapTx(conn *Connection, fn func(ctx mongo.SessionContext) error) error { var err error //fmt.Println("Connection in tx", conn.IsTx(), " sess", conn.sess) if conn.sess != nil { @@ -563,7 +567,7 @@ func wrapTx(conn *Connection, fn func(ctx context.Context) error) error { return fn(sc) }) } else { - err = fn(conn.ctx) + err = fn(nil) } return err } From d6461541f23d15f654158178af637a7cb886a74f Mon Sep 17 00:00:00 2001 From: arief Date: Mon, 19 Jul 2021 19:02:42 +0700 Subject: [PATCH 19/51] add supportTx so it can be used by datahub --- connection.go | 4 ++++ flexmgo_test.go | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/connection.go b/connection.go index 599abf2..57ad511 100644 --- a/connection.go +++ b/connection.go @@ -178,3 +178,7 @@ func (c *Connection) RollBack() error { func (c *Connection) IsTx() bool { return c.sess != nil } + +func (c *Connection) SupportTx() bool { + return true +} diff --git a/flexmgo_test.go b/flexmgo_test.go index 577d33d..76e0886 100644 --- a/flexmgo_test.go +++ b/flexmgo_test.go @@ -439,7 +439,7 @@ func connect() (dbflex.IConnection, error) { } func connectTrx() (dbflex.IConnection, error) { - if conn, err := dbflex.NewConnectionFromURI("mongodb://localhost:27201,localhost:27202/rsdb?replicaSet=rs01", nil); err == nil { + if conn, err := dbflex.NewConnectionFromURI("mongodb://localhost:27017/rsdb", nil); err == nil { if err = conn.Connect(); err == nil { conn.SetFieldNameTag("json") return conn, nil From e2d449c7244692b8876df9274cfe9e536296aa42 Mon Sep 17 00:00:00 2001 From: arief Date: Tue, 20 Jul 2021 15:27:02 +0700 Subject: [PATCH 20/51] add comment --- connection.go | 1 + 1 file changed, 1 insertion(+) diff --git a/connection.go b/connection.go index 57ad511..3cec9f4 100644 --- a/connection.go +++ b/connection.go @@ -179,6 +179,7 @@ func (c *Connection) IsTx() bool { return c.sess != nil } +// SupportTx to identify if underlying connection support Tx or not func (c *Connection) SupportTx() bool { return true } From aa1de9338413dd4d161c0f5b85a5c58b7cec86e8 Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Tue, 14 Jun 2022 23:24:22 +0700 Subject: [PATCH 21/51] add ensuretable and update deps --- connection.go | 4 ++++ go.mod | 10 +++++----- go.sum | 32 ++++++++++++++++++++------------ 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/connection.go b/connection.go index 3cec9f4..f9fdced 100644 --- a/connection.go +++ b/connection.go @@ -125,6 +125,10 @@ func (c *Connection) NewQuery() dbflex.IQuery { return q } +func (c *Connection) EnsureTable(name string, keys []string, obj interface{}) error { + return nil +} + func (c *Connection) DropTable(name string) error { return c.db.Collection(name).Drop(c.ctx) } diff --git a/go.mod b/go.mod index 63464ee..c4f8b9f 100644 --- a/go.mod +++ b/go.mod @@ -1,12 +1,12 @@ module github.com/ariefdarmawan/flexmgo -go 1.15 +go 1.16 -replace git.kanosolution.net/kano/dbflex => D:\Users\AriefDarmawan\Coding\go\src\git.kanosolution.net\kano\dbflex +//replace git.kanosolution.net/kano/dbflex => D:\coding\lib\dbflex require ( - git.kanosolution.net/kano/dbflex v1.0.12 + git.kanosolution.net/kano/dbflex v1.1.0 github.com/eaciit/toolkit v0.0.0-20210610161449-593d5fadf78e - github.com/smartystreets/goconvey v1.6.4 - go.mongodb.org/mongo-driver v1.6.0 + github.com/smartystreets/goconvey v1.7.2 + go.mongodb.org/mongo-driver v1.9.1 ) diff --git a/go.sum b/go.sum index 4bdb8be..b57c6f0 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,11 @@ +git.kanosolution.net/kano/dbflex v1.0.12/go.mod h1:JwhKlws6r/uBbPE126aJpHbq3x4iugF0ViJlE/fFg/k= +git.kanosolution.net/kano/dbflex v1.1.0 h1:kMP1plQyjOXjXChqivzXb9k5JevvPU8OSZY5aW0apow= +git.kanosolution.net/kano/dbflex v1.1.0/go.mod h1:3YZtv6TD1Io6khYnzdoOmyzGqt9lf03zp0LQBLSMQaQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/ariefdarmawan/flexmgo v0.1.15/go.mod h1:ZNVfnly+iMMBJ9sP142Kkt31CeQk+M8LLWYEfF59Qr8= -github.com/ariefdarmawan/reflector v0.0.0-20210429160254-3690a39ca6e7 h1:Bo3imCZPoGt6SNGPgJ5oICKscpLO+60d2SG9Gv6DnGg= -github.com/ariefdarmawan/reflector v0.0.0-20210429160254-3690a39ca6e7/go.mod h1:C3eiJ2xw5JT/13i4snLE3F+ToanCZSewAxkCxCepq7o= +github.com/ariefdarmawan/flexmgo v0.2.2/go.mod h1:uW09cBzpeI/4Az0j2iLO4Va2wxfJ5T+m9012fUzM7JM= +github.com/ariefdarmawan/reflector v0.0.3 h1:qbfwP5iKMvdYBWRG2jZyoXRLPsGedJ1sNcC8bw+MhpY= +github.com/ariefdarmawan/reflector v0.0.3/go.mod h1:xxeY7n6iT0q5pK4j3s8l2xo1CvmQgS2oYtj0lsV4eo4= github.com/aws/aws-sdk-go v1.34.28/go.mod h1:H7NKnBqNVzoTJpGfLrQkkD+ytBA93eiDYi/+8rV9s48= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= @@ -35,7 +39,6 @@ github.com/gobuffalo/packd v0.1.0/go.mod h1:M2Juc+hhDXf/PnmBANFCqx4DM3wRbgDvnVWe github.com/gobuffalo/packr/v2 v2.0.9/go.mod h1:emmyGweYTm6Kdper+iywB6YK5YzuKchGtJQZ0Odn4pQ= github.com/gobuffalo/packr/v2 v2.2.0/go.mod h1:CaAwI0GPIAv+5wKLtv8Afwl+Cm78K/I/VCm/3ptBN+0= github.com/gobuffalo/syncx v0.0.0-20190224160051-33c29581e754/go.mod h1:HhnNqWY95UYwwW3uSASeV7vtgYkT2t16hJgV3AEPUpw= -github.com/gofrs/flock v0.8.1 h1:+gYjHKf32LDeiEEFhQaotPbLuUXjY5ZqxKgXy7n59aw= github.com/gofrs/flock v0.8.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU= github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4= github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= @@ -51,14 +54,13 @@ github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7 github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/karrick/godirwalk v1.8.0/go.mod h1:H5KPZjojv4lE+QYImBI8xVtrBRgYrIVsaRPx4tDPEn4= github.com/karrick/godirwalk v1.10.3/go.mod h1:RoGL9dQei4vP9ilrpETWE8CLOZ1kiN0LhBygSwrAsHA= -github.com/klauspost/compress v1.9.5 h1:U+CaK85mrNNb4k8BNOfgJtJ/gr6kswUCFj6miSzVC6M= github.com/klauspost/compress v1.9.5/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A= +github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc= +github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= -github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/markbates/oncer v0.0.0-20181203154359-bf2de49a0be2/go.mod h1:Ld9puTsIW75CHf65OeIOkyKbteujpZVXDpWK6YGZbxE= github.com/markbates/safe v1.0.1/go.mod h1:nAqgmRi7cY2nqMc92/bSEeQA+R4OheNU2T1kNSCBdG0= @@ -76,10 +78,12 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR github.com/sirupsen/logrus v1.4.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= -github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= -github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s= +github.com/smartystreets/assertions v1.2.0 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N3yZFZkDFs= +github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo= github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= +github.com/smartystreets/goconvey v1.7.2 h1:9RBaZCeXEQ3UselpuwUQHltGVXvdwm6cv1hgR6gDIPg= +github.com/smartystreets/goconvey v1.7.2/go.mod h1:Vw0tHAZW6lzCRk3xgdin6fKYcG+G3Pg9vgXWeJpQFMM= github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -102,13 +106,16 @@ github.com/xdg-go/stringprep v1.0.2/go.mod h1:8F9zXuvzgwmyT5DUm4GUfZGDdT3W+LCvS6 github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d h1:splanxYIlg+5LfHAM6xpdFEAYOk8iySO56hMFq6uLyA= github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA= go.mongodb.org/mongo-driver v1.5.4/go.mod h1:gRXCHX4Jo7J0IJ1oDQyUxF7jfy19UfxniMS4xxMmUqw= -go.mongodb.org/mongo-driver v1.6.0 h1:ccc26ylcoRWJQRbjU7GvqfxNzwKcoIcEL3BPuFR/pJ0= go.mongodb.org/mongo-driver v1.6.0/go.mod h1:Q4oFMbo1+MSNqICAdYMlC/zSTrwCogR4R8NzkI+yfU8= +go.mongodb.org/mongo-driver v1.7.1/go.mod h1:Q4oFMbo1+MSNqICAdYMlC/zSTrwCogR4R8NzkI+yfU8= +go.mongodb.org/mongo-driver v1.9.1 h1:m078y9v7sBItkt1aaoe2YlvWEXcD263e1a4E1fBrJ1c= +go.mongodb.org/mongo-driver v1.9.1/go.mod h1:0sQWfOeY63QTntERDJJ/0SuKK0T1uVSgKCuAROlKEPY= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190422162423-af44ce270edf/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE= -golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073 h1:xMPOj6Pz6UipU1wXLkrtqpHbR0AVFnyPEQq/wRWz9lM= golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20201216223049-8b5274cf687f h1:aZp0e2vLN4MToVqnjNEYEtrEA8RH8U8FN1CU7JgqsPU= +golang.org/x/crypto v0.0.0-20201216223049-8b5274cf687f/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= @@ -125,8 +132,10 @@ golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190419153524-e8e3143a4f4a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190531175056-4c3a928424d2 h1:T5DasATyLQfmbTpfEXx/IOL9vfjzW6up+ZDkmHvIf2s= golang.org/x/sys v0.0.0-20190531175056-4c3a928424d2/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.5 h1:i6eZZ+zk0SOf0xgBpEpPD18qWcJda6q1sxt3S0kzyUQ= golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= @@ -139,7 +148,6 @@ golang.org/x/tools v0.0.0-20190531172133-b3315ee88b7d/go.mod h1:/rFqwRUd4F7ZHNgw golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= 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/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= From 62970db609149ee8765f4d5d6d73b379e07eda60 Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Fri, 17 Jun 2022 01:01:07 +0700 Subject: [PATCH 22/51] no toolkit --- connection.go | 16 ++++----- cursor.go | 27 +++++++------- flexmgo_test.go | 72 +++++++++++++++++++------------------ go.mod | 12 ++++++- go.sum | 77 --------------------------------------- query.go | 96 +++++++++++++++++++++++++------------------------ 6 files changed, 120 insertions(+), 180 deletions(-) diff --git a/connection.go b/connection.go index f9fdced..4f26964 100644 --- a/connection.go +++ b/connection.go @@ -7,7 +7,7 @@ import ( "time" "git.kanosolution.net/kano/dbflex" - "github.com/eaciit/toolkit" + "github.com/sebarcode/codekit" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/readconcern" @@ -49,43 +49,43 @@ func (c *Connection) Connect() error { switch klow { case "serverselectiontimeout": opts.SetServerSelectionTimeout( - time.Duration(toolkit.ToInt(v, toolkit.RoundingAuto)) * time.Millisecond) + time.Duration(codekit.ToInt(v, codekit.RoundingAuto)) * time.Millisecond) case "replicaset": opts.SetReplicaSet(v.(string)) //opts.SetWriteConcern() case "poolsize": - poolSize := toolkit.ToInt(v.(string), toolkit.RoundingAuto) + poolSize := codekit.ToInt(v.(string), codekit.RoundingAuto) if poolSize > 0 { opts.SetMaxPoolSize(uint64(poolSize)) } case "idle": - idle := toolkit.ToInt(v.(string), toolkit.RoundingAuto) + idle := codekit.ToInt(v.(string), codekit.RoundingAuto) if idle > 0 { opts.SetMaxConnIdleTime(time.Duration(idle) * time.Second) } } } - //toolkit.Logger().Debugf("opts: %s", toolkit.JsonString(opts)) + //logger.Logger().Debugf("opts: %s", codekit.JsonString(opts)) client, err := mongo.NewClient(opts) if err != nil { return err } - //toolkit.Logger().Debug("client generated: OK") + //logger.Logger().Debug("client generated: OK") if c.ctx == nil { c.ctx = context.TODO() } - //toolkit.Logger().Debug("context generated: OK") + //logger.Logger().Debug("context generated: OK") if err = client.Connect(c.ctx); err != nil { return err } - //toolkit.Logger().Debug("client connected: OK") + //logger.Logger().Debug("client connected: OK") if err = client.Ping(c.ctx, nil); err != nil { return err } diff --git a/cursor.go b/cursor.go index 0074b54..18af330 100644 --- a/cursor.go +++ b/cursor.go @@ -7,7 +7,8 @@ import ( "time" "git.kanosolution.net/kano/dbflex" - "github.com/eaciit/toolkit" + "github.com/ariefdarmawan/serde" + "github.com/sebarcode/codekit" "go.mongodb.org/mongo-driver/mongo" ) @@ -16,7 +17,7 @@ type Cursor struct { mc *mongo.Cursor tablename string - countParm toolkit.M + countParm codekit.M conn *Connection cursor *mongo.Cursor } @@ -41,7 +42,7 @@ func (cr *Cursor) Count() int { tableName := cr.countParm.GetString("count") where := cr.countParm.Get("query", nil) if where == nil { - n, _ := cr.conn.db.Collection(tableName).CountDocuments(cr.conn.ctx, toolkit.M{}) + n, _ := cr.conn.db.Collection(tableName).CountDocuments(cr.conn.ctx, codekit.M{}) return int(n) } else { n, _ := cr.conn.db.Collection(tableName).CountDocuments(cr.conn.ctx, where) @@ -53,7 +54,7 @@ func (cr *Cursor) Count() int { if sr.Err() != nil { dbflex.Logger().Errorf("unable to get count. %s, countparm: %s", sr.Err().Error(), - toolkit.JsonString(cr.countParm)) + codekit.JsonString(cr.countParm)) return 0 } @@ -68,7 +69,7 @@ func (cr *Cursor) Count() int { func (cr *Cursor) Fetch(out interface{}) dbflex.ICursor { if cr.Error() != nil { - cr.SetError(toolkit.Errorf("unable to fetch data. %s", cr.Error())) + cr.SetError(fmt.Errorf("unable to fetch data. %s", cr.Error())) return cr } @@ -77,9 +78,9 @@ func (cr *Cursor) Fetch(out interface{}) dbflex.ICursor { return cr } - m := toolkit.M{} + m := codekit.M{} if err := cr.cursor.Decode(&m); err != nil { - cr.SetError(toolkit.Errorf("unable to decode output. %s", err.Error())) + cr.SetError(fmt.Errorf("unable to decode output. %s", err.Error())) return cr } for mk, mv := range m { @@ -95,8 +96,8 @@ func (cr *Cursor) Fetch(out interface{}) dbflex.ICursor { if reflect.ValueOf(m).Type().String() == reflect.Indirect(reflect.ValueOf(out)).Type().String() { reflect.ValueOf(out).Elem().Set(reflect.ValueOf(m)) } else { - if err := toolkit.Serde(m, out, ""); err != nil { - cr.SetError(toolkit.Errorf("unable to decode output. %s", err.Error())) + if err := serde.Serde(m, out); err != nil { + cr.SetError(fmt.Errorf("unable to decode output. %s", err.Error())) return cr } } @@ -106,7 +107,7 @@ func (cr *Cursor) Fetch(out interface{}) dbflex.ICursor { func (cr *Cursor) Fetchs(result interface{}, n int) dbflex.ICursor { if cr.Error() != nil { - cr.SetError(toolkit.Errorf("unable to fetch data. %s", cr.Error())) + cr.SetError(fmt.Errorf("unable to fetch data. %s", cr.Error())) return cr } @@ -136,13 +137,13 @@ func (cr *Cursor) Fetchs(result interface{}, n int) dbflex.ICursor { reflect.ValueOf(result).Elem().Set(ivs) */ read := 0 - ms := []toolkit.M{} + ms := []codekit.M{} for { if !cr.cursor.Next(cr.conn.ctx) { break } - m := toolkit.M{} + m := codekit.M{} err := cr.cursor.Decode(&m) if err != nil { cr.SetError(fmt.Errorf("unable to decode cursor data. %s", err.Error())) @@ -169,7 +170,7 @@ func (cr *Cursor) Fetchs(result interface{}, n int) dbflex.ICursor { if reflect.ValueOf(ms).Type().String() == reflect.Indirect(reflect.ValueOf(result)).Type().String() { reflect.ValueOf(result).Elem().Set(reflect.ValueOf(ms)) } else { - if err := toolkit.Serde(ms, result, ""); err != nil { + if err := serde.Serde(ms, result); err != nil { cr.SetError(fmt.Errorf("unable to decode cursor data. %s", err.Error())) return cr } diff --git a/flexmgo_test.go b/flexmgo_test.go index 76e0886..885af39 100644 --- a/flexmgo_test.go +++ b/flexmgo_test.go @@ -11,9 +11,10 @@ import ( "git.kanosolution.net/kano/dbflex/orm" _ "github.com/ariefdarmawan/flexmgo" + logger "github.com/sebarcode/logger" "git.kanosolution.net/kano/dbflex" - "github.com/eaciit/toolkit" + "github.com/sebarcode/codekit" cv "github.com/smartystreets/goconvey/convey" ) @@ -23,7 +24,7 @@ const ( func init() { fmt.Println("Debug level is activated") - toolkit.Logger().SetLevelStdOut(toolkit.DebugLevel, true) + logger.Logger().SetLevelStdOut(logger.DebugLevel, true) } func TestConnect(t *testing.T) { @@ -55,14 +56,14 @@ func TestSaveData(t *testing.T) { es := []error{} for i := 1; i <= 10; i++ { r := new(Record) - r.ID = toolkit.Sprintf("record-id-%d", i) - r.Title = "Title is " + toolkit.RandomString(32) - r.Age = toolkit.RandInt(10) + 18 - r.Salary = toolkit.RandFloat(8000, 4) + float64(5000) + r.ID = fmt.Sprintf("record-id-%d", i) + r.Title = "Title is " + codekit.RandomString(32) + r.Age = codekit.RandInt(10) + 18 + r.Salary = codekit.RandFloat(8000, 4) + float64(5000) r.DateJoin = time.Date(2000, 1, 1, 0, 0, 0, 0, - time.Now().Location()).Add(24 * time.Hour * time.Duration(toolkit.RandInt(1000))) + time.Now().Location()).Add(24 * time.Hour * time.Duration(codekit.RandInt(1000))) - if _, err := conn.Execute(cmd, toolkit.M{}.Set("data", r)); err != nil { + if _, err := conn.Execute(cmd, codekit.M{}.Set("data", r)); err != nil { es = append(es, err) break } @@ -98,36 +99,39 @@ func TestListData(t *testing.T) { }, "gt": { - dbflex.Gt("salary", 1000), + dbflex.Gt("Salary", 1000), func(r *Record) bool { return r.Salary > 1000 }, }, "gte": { - dbflex.Gte("salary", 1000), func(r *Record) bool { + dbflex.Gte("Salary", 1000), func(r *Record) bool { return r.Salary >= 1000 }, }, - "lt": {dbflex.Lt("age", 80), func(r *Record) bool { + "lt": {dbflex.Lt("Age", 80), func(r *Record) bool { return r.Age < 80 }, }, - "lte": {dbflex.Lte("age", 90), func(r *Record) bool { + "lte": {dbflex.Lte("Age", 90), func(r *Record) bool { return r.Age <= 90 }, }, - "range": {dbflex.Range("datejoin", - toolkit.String2Date("2000-01-01", "YYYY-mm-dd"), + "range": {dbflex.Range("DateJoin", + codekit.String2Date("2000-01-01", "YYYY-MM-DD"), time.Now()), func(r *Record) bool { - return r.DateJoin.After(toolkit.String2Date("2000-01-01", "YYYY-mm-dd")) && + return r.DateJoin.After(codekit.String2Date("2000-01-01", "YYYY-MM-DD")) && r.DateJoin.Before(time.Now()) }, }, } for key, sc := range scenarios { + if key != "range" { + continue + } cv.Convey("scenario "+key, t, func() { conn, err := connect() cv.So(err, cv.ShouldBeNil) @@ -162,7 +166,7 @@ func TestUpdateData(t *testing.T) { cv.Convey("update data", func() { cmd := dbflex.From(tablename).Update("title") - _, err = conn.Execute(cmd, toolkit.M{}.Set("data", r)) + _, err = conn.Execute(cmd, codekit.M{}.Set("data", r)) cv.So(err, cv.ShouldBeNil) cv.Convey("vaidate", func() { @@ -208,7 +212,7 @@ func TestMdbTrx(t *testing.T) { err = nil cmd := dbflex.From(countriesTableName).Save() for _, country := range countries { - _, err = conn.Execute(cmd, toolkit.M{}.Set("data", country)) + _, err = conn.Execute(cmd, codekit.M{}.Set("data", country)) if err != nil { break } @@ -220,7 +224,7 @@ func TestMdbTrx(t *testing.T) { cv.So(cur.Error(), cv.ShouldBeNil) defer cur.Close() - ms := []toolkit.M{} + ms := []codekit.M{} cv.So(cur.Fetchs(&ms, 0).Error(), cv.ShouldBeNil) cv.So(len(ms), cv.ShouldEqual, len(countries)) @@ -237,7 +241,7 @@ func TestMdbTrx(t *testing.T) { } cmd := dbflex.From(stateTableName).Save() for _, state := range states { - _, err = conn.Execute(cmd, toolkit.M{}.Set("data", state)) + _, err = conn.Execute(cmd, codekit.M{}.Set("data", state)) if err != nil { break } @@ -251,7 +255,7 @@ func TestMdbTrx(t *testing.T) { cur := conn.Cursor(cmd, nil) cv.So(cur.Error(), cv.ShouldBeNil) cur.Close() - ms := []toolkit.M{} + ms := []codekit.M{} cv.So(cur.Fetchs(&ms, 0).Error(), cv.ShouldBeNil) cv.So(len(ms), cv.ShouldEqual, len(states)) @@ -260,7 +264,7 @@ func TestMdbTrx(t *testing.T) { cv.So(err, cv.ShouldBeNil) cmd := dbflex.From(stateTableName).Insert() - conn.Execute(cmd, toolkit.M{}.Set("data", &state{"JT", "Jawa Timur", "ID"})) + conn.Execute(cmd, codekit.M{}.Set("data", &state{"JT", "Jawa Timur", "ID"})) err = conn.RollBack() cv.So(err, cv.ShouldBeNil) @@ -268,7 +272,7 @@ func TestMdbTrx(t *testing.T) { cur := conn.Cursor(cmd, nil) cv.So(cur.Error(), cv.ShouldBeNil) cur.Close() - ms1 := []toolkit.M{} + ms1 := []codekit.M{} cv.So(cur.Fetchs(&ms1, 0).Error(), cv.ShouldBeNil) cv.So(len(ms1), cv.ShouldEqual, len(states)) }) @@ -284,23 +288,23 @@ func TestWatch(t *testing.T) { cv.So(err, cv.ShouldBeNil) defer conn.Close() - changed := make(chan toolkit.M) + changed := make(chan codekit.M) cmd := dbflex.From(tablename).Command("watch") - _, err = conn.Execute(cmd, toolkit.M{}. - Set("fn", func(data toolkit.M) { + _, err = conn.Execute(cmd, codekit.M{}. + Set("fn", func(data codekit.M) { changed <- data close(changed) })) cv.So(err, cv.ShouldBeNil) cv.Convey("validate", func() { - m := toolkit.M{} + m := codekit.M{} cmdGet := dbflex.From(tablename).Select().Where(dbflex.Eq("_id", "record-id-5")) err := conn.Cursor(cmdGet, nil).SetCloseAfterFetch().Fetch(&m) cv.So(err, cv.ShouldBeNil) m.Set("title", "Test Change Stream") - _, err = conn.Execute(dbflex.From(tablename).Save(), toolkit.M{}.Set("data", m)) + _, err = conn.Execute(dbflex.From(tablename).Save(), codekit.M{}.Set("data", m)) changedData := <-changed cv.So(changedData.GetString("title"), cv.ShouldEqual, "Test Change Stream") @@ -337,7 +341,7 @@ func TestAggregateData(t *testing.T) { defer conn.Close() cv.Convey("aggregate", func() { - cmd := dbflex.From(tablename).Aggr(dbflex.NewAggrItem("salary", dbflex.AggrSum, "salary")) + cmd := dbflex.From(tablename).Aggr(dbflex.NewAggrItem("Salary", dbflex.AggrSum, "Salary")) cur := conn.Cursor(cmd, nil) cv.So(cur.Error(), cv.ShouldBeNil) defer cur.Close() @@ -360,7 +364,7 @@ func TestAggregateData(t *testing.T) { cur.Fetch(aggrModel) cv.So(math.Abs(aggrModel.Salary-total), cv.ShouldBeLessThan, 1) - toolkit.Logger().Debugf("total is: %v", total) + logger.Logger().Debugf("total is: %v", total) }) }) }) @@ -381,7 +385,7 @@ func TestDropTable(t *testing.T) { func TestGridFsUpdate(t *testing.T) { cv.Convey("preparing file", t, func() { - data := []byte(toolkit.RandomString(512)) + data := []byte(codekit.RandomString(512)) conn, _ := connect() defer conn.Close() @@ -390,8 +394,8 @@ func TestGridFsUpdate(t *testing.T) { reader := bufio.NewReader(buff) cmd := dbflex.From("fs").Command("GfsWrite") - metadata := toolkit.M{}.Set("data", "ini adalah meta") - _, err := conn.Execute(cmd, toolkit.M{}. + metadata := codekit.M{}.Set("data", "ini adalah meta") + _, err := conn.Execute(cmd, codekit.M{}. Set("id", "doc1"). Set("metadata", metadata). Set("source", reader)) @@ -402,7 +406,7 @@ func TestGridFsUpdate(t *testing.T) { writer := bufio.NewWriter(&buff) cmd := dbflex.From("fs").Command("gfsread") - _, err := conn.Execute(cmd, toolkit.M{}. + _, err := conn.Execute(cmd, codekit.M{}. Set("id", "doc1"). Set("output", writer)) cv.So(err, cv.ShouldBeNil) @@ -410,7 +414,7 @@ func TestGridFsUpdate(t *testing.T) { cv.Convey("delete grid", func() { cmd := dbflex.From("fs").Command("gfsdelete") - _, err := conn.Execute(cmd, toolkit.M{}.Set("id", "doc1")) + _, err := conn.Execute(cmd, codekit.M{}.Set("id", "doc1")) cv.So(err, cv.ShouldBeNil) }) }) diff --git a/go.mod b/go.mod index c4f8b9f..c558ed5 100644 --- a/go.mod +++ b/go.mod @@ -2,11 +2,21 @@ module github.com/ariefdarmawan/flexmgo go 1.16 +replace git.kanosolution.net/kano/dbflex => D:\Coding\lib\dbflex + +replace github.com/sebarcode/codekit => D:\Coding\lib\codekit + +replace github.com/sebarcode/logger => D:\Coding\lib\logger + +replace github.com/ariefdarmawan/serde => D:\Coding\lib\serde + //replace git.kanosolution.net/kano/dbflex => D:\coding\lib\dbflex require ( git.kanosolution.net/kano/dbflex v1.1.0 - github.com/eaciit/toolkit v0.0.0-20210610161449-593d5fadf78e + github.com/ariefdarmawan/serde v0.0.0-20220616152415-ac746173e86f + github.com/sebarcode/codekit v0.0.0-20220616144406-d7c5cafaca19 + github.com/sebarcode/logger v0.0.0-00010101000000-000000000000 github.com/smartystreets/goconvey v1.7.2 go.mongodb.org/mongo-driver v1.9.1 ) diff --git a/go.sum b/go.sum index b57c6f0..9323b3f 100644 --- a/go.sum +++ b/go.sum @@ -1,44 +1,12 @@ -git.kanosolution.net/kano/dbflex v1.0.12/go.mod h1:JwhKlws6r/uBbPE126aJpHbq3x4iugF0ViJlE/fFg/k= -git.kanosolution.net/kano/dbflex v1.1.0 h1:kMP1plQyjOXjXChqivzXb9k5JevvPU8OSZY5aW0apow= -git.kanosolution.net/kano/dbflex v1.1.0/go.mod h1:3YZtv6TD1Io6khYnzdoOmyzGqt9lf03zp0LQBLSMQaQ= -github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= -github.com/ariefdarmawan/flexmgo v0.1.15/go.mod h1:ZNVfnly+iMMBJ9sP142Kkt31CeQk+M8LLWYEfF59Qr8= -github.com/ariefdarmawan/flexmgo v0.2.2/go.mod h1:uW09cBzpeI/4Az0j2iLO4Va2wxfJ5T+m9012fUzM7JM= github.com/ariefdarmawan/reflector v0.0.3 h1:qbfwP5iKMvdYBWRG2jZyoXRLPsGedJ1sNcC8bw+MhpY= github.com/ariefdarmawan/reflector v0.0.3/go.mod h1:xxeY7n6iT0q5pK4j3s8l2xo1CvmQgS2oYtj0lsV4eo4= -github.com/aws/aws-sdk-go v1.34.28/go.mod h1:H7NKnBqNVzoTJpGfLrQkkD+ytBA93eiDYi/+8rV9s48= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/eaciit/toolkit v0.0.0-20210610161449-593d5fadf78e h1:2/wdIW25ZaPHfKQ5HvQUSAqeH9KAZQH1PlCMnrSJ9xE= github.com/eaciit/toolkit v0.0.0-20210610161449-593d5fadf78e/go.mod h1:r4OKDNGrY6n6gCVqEFdld+JTfgdNgp78RPvZHr2B9jU= -github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= -github.com/gobuffalo/attrs v0.0.0-20190224210810-a9411de4debd/go.mod h1:4duuawTqi2wkkpB4ePgWMaai6/Kc6WEz83bhFwpHzj0= -github.com/gobuffalo/depgen v0.0.0-20190329151759-d478694a28d3/go.mod h1:3STtPUQYuzV0gBVOY3vy6CfMm/ljR4pABfrTeHNLHUY= -github.com/gobuffalo/depgen v0.1.0/go.mod h1:+ifsuy7fhi15RWncXQQKjWS9JPkdah5sZvtHc2RXGlg= -github.com/gobuffalo/envy v1.6.15/go.mod h1:n7DRkBerg/aorDM8kbduw5dN3oXGswK5liaSCx4T5NI= -github.com/gobuffalo/envy v1.7.0/go.mod h1:n7DRkBerg/aorDM8kbduw5dN3oXGswK5liaSCx4T5NI= -github.com/gobuffalo/flect v0.1.0/go.mod h1:d2ehjJqGOH/Kjqcoz+F7jHTBbmDb38yXA598Hb50EGs= -github.com/gobuffalo/flect v0.1.1/go.mod h1:8JCgGVbRjJhVgD6399mQr4fx5rRfGKVzFjbj6RE/9UI= -github.com/gobuffalo/flect v0.1.3/go.mod h1:8JCgGVbRjJhVgD6399mQr4fx5rRfGKVzFjbj6RE/9UI= -github.com/gobuffalo/genny v0.0.0-20190329151137-27723ad26ef9/go.mod h1:rWs4Z12d1Zbf19rlsn0nurr75KqhYp52EAGGxTbBhNk= -github.com/gobuffalo/genny v0.0.0-20190403191548-3ca520ef0d9e/go.mod h1:80lIj3kVJWwOrXWWMRzzdhW3DsrdjILVil/SFKBzF28= -github.com/gobuffalo/genny v0.1.0/go.mod h1:XidbUqzak3lHdS//TPu2OgiFB+51Ur5f7CSnXZ/JDvo= -github.com/gobuffalo/genny v0.1.1/go.mod h1:5TExbEyY48pfunL4QSXxlDOmdsD44RRq4mVZ0Ex28Xk= -github.com/gobuffalo/gitgen v0.0.0-20190315122116-cc086187d211/go.mod h1:vEHJk/E9DmhejeLeNt7UVvlSGv3ziL+djtTr3yyzcOw= -github.com/gobuffalo/gogen v0.0.0-20190315121717-8f38393713f5/go.mod h1:V9QVDIxsgKNZs6L2IYiGR8datgMhB577vzTDqypH360= -github.com/gobuffalo/gogen v0.1.0/go.mod h1:8NTelM5qd8RZ15VjQTFkAW6qOMx5wBbW4dSCS3BY8gg= -github.com/gobuffalo/gogen v0.1.1/go.mod h1:y8iBtmHmGc4qa3urIyo1shvOD8JftTtfcKi+71xfDNE= -github.com/gobuffalo/logger v0.0.0-20190315122211-86e12af44bc2/go.mod h1:QdxcLw541hSGtBnhUc4gaNIXRjiDppFGaDqzbrBd3v8= -github.com/gobuffalo/mapi v1.0.1/go.mod h1:4VAGh89y6rVOvm5A8fKFxYG+wIW6LO1FMTG9hnKStFc= -github.com/gobuffalo/mapi v1.0.2/go.mod h1:4VAGh89y6rVOvm5A8fKFxYG+wIW6LO1FMTG9hnKStFc= -github.com/gobuffalo/packd v0.0.0-20190315124812-a385830c7fc0/go.mod h1:M2Juc+hhDXf/PnmBANFCqx4DM3wRbgDvnVWeG2RIxq4= -github.com/gobuffalo/packd v0.1.0/go.mod h1:M2Juc+hhDXf/PnmBANFCqx4DM3wRbgDvnVWeG2RIxq4= -github.com/gobuffalo/packr/v2 v2.0.9/go.mod h1:emmyGweYTm6Kdper+iywB6YK5YzuKchGtJQZ0Odn4pQ= -github.com/gobuffalo/packr/v2 v2.2.0/go.mod h1:CaAwI0GPIAv+5wKLtv8Afwl+Cm78K/I/VCm/3ptBN+0= -github.com/gobuffalo/syncx v0.0.0-20190224160051-33c29581e754/go.mod h1:HhnNqWY95UYwwW3uSASeV7vtgYkT2t16hJgV3AEPUpw= github.com/gofrs/flock v0.8.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU= github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4= github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= @@ -46,54 +14,28 @@ github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= -github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= -github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= -github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= -github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= -github.com/karrick/godirwalk v1.8.0/go.mod h1:H5KPZjojv4lE+QYImBI8xVtrBRgYrIVsaRPx4tDPEn4= -github.com/karrick/godirwalk v1.10.3/go.mod h1:RoGL9dQei4vP9ilrpETWE8CLOZ1kiN0LhBygSwrAsHA= -github.com/klauspost/compress v1.9.5/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A= github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc= github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= -github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= -github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= -github.com/markbates/oncer v0.0.0-20181203154359-bf2de49a0be2/go.mod h1:Ld9puTsIW75CHf65OeIOkyKbteujpZVXDpWK6YGZbxE= -github.com/markbates/safe v1.0.1/go.mod h1:nAqgmRi7cY2nqMc92/bSEeQA+R4OheNU2T1kNSCBdG0= github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc= -github.com/pelletier/go-toml v1.7.0/go.mod h1:vwGMzjaWMwyfHwgIBhI2YUM4fB6nL6lVAvS1LBMMhTE= -github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/rogpeppe/go-internal v1.1.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= -github.com/rogpeppe/go-internal v1.2.2/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= -github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= -github.com/sirupsen/logrus v1.4.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= -github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q= -github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= github.com/smartystreets/assertions v1.2.0 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N3yZFZkDFs= github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo= github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= github.com/smartystreets/goconvey v1.7.2 h1:9RBaZCeXEQ3UselpuwUQHltGVXvdwm6cv1hgR6gDIPg= github.com/smartystreets/goconvey v1.7.2/go.mod h1:Vw0tHAZW6lzCRk3xgdin6fKYcG+G3Pg9vgXWeJpQFMM= -github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= -github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= -github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/theckman/go-flock v0.8.1 h1:kTixuOsFBOtGYSTLRLWK6GOs1hk/8OD11sR1pDd0dl4= github.com/theckman/go-flock v0.8.1/go.mod h1:kjuth3y9VJ2aNlkNEO99G/8lp9fMIKaGyBmh84IBheM= github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4= github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= @@ -105,15 +47,9 @@ github.com/xdg-go/stringprep v1.0.2 h1:6iq84/ryjjeRmMJwxutI51F2GIPlP5BfTvXHeYjyh github.com/xdg-go/stringprep v1.0.2/go.mod h1:8F9zXuvzgwmyT5DUm4GUfZGDdT3W+LCvS6+da4O5kxM= github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d h1:splanxYIlg+5LfHAM6xpdFEAYOk8iySO56hMFq6uLyA= github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA= -go.mongodb.org/mongo-driver v1.5.4/go.mod h1:gRXCHX4Jo7J0IJ1oDQyUxF7jfy19UfxniMS4xxMmUqw= -go.mongodb.org/mongo-driver v1.6.0/go.mod h1:Q4oFMbo1+MSNqICAdYMlC/zSTrwCogR4R8NzkI+yfU8= -go.mongodb.org/mongo-driver v1.7.1/go.mod h1:Q4oFMbo1+MSNqICAdYMlC/zSTrwCogR4R8NzkI+yfU8= go.mongodb.org/mongo-driver v1.9.1 h1:m078y9v7sBItkt1aaoe2YlvWEXcD263e1a4E1fBrJ1c= go.mongodb.org/mongo-driver v1.9.1/go.mod h1:0sQWfOeY63QTntERDJJ/0SuKK0T1uVSgKCuAROlKEPY= -golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20190422162423-af44ce270edf/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE= -golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20201216223049-8b5274cf687f h1:aZp0e2vLN4MToVqnjNEYEtrEA8RH8U8FN1CU7JgqsPU= golang.org/x/crypto v0.0.0-20201216223049-8b5274cf687f/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= @@ -121,19 +57,11 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200202094626-16171245cfb2 h1:CCH4IOTTfewWjGOlSp+zGcjutRKlBEZQ6wTn8ozI/nI= golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20190412183630-56d357773e84/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e h1:vcxGaoTs7kV8m5Np9uUNQin4BrLOthgV7252N8V+FwY= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190419153524-e8e3143a4f4a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190531175056-4c3a928424d2/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -141,16 +69,11 @@ golang.org/x/text v0.3.5 h1:i6eZZ+zk0SOf0xgBpEpPD18qWcJda6q1sxt3S0kzyUQ= golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= -golang.org/x/tools v0.0.0-20190329151228-23e29df326fe/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= -golang.org/x/tools v0.0.0-20190416151739-9c9e1878f421/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= -golang.org/x/tools v0.0.0-20190420181800-aa740d480789/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190531172133-b3315ee88b7d/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/query.go b/query.go index f4b3480..5f08899 100644 --- a/query.go +++ b/query.go @@ -1,14 +1,16 @@ package flexmgo import ( + "errors" "fmt" "io" "strings" "git.kanosolution.net/kano/dbflex" df "git.kanosolution.net/kano/dbflex" - "github.com/eaciit/toolkit" - . "github.com/eaciit/toolkit" + "github.com/ariefdarmawan/serde" + "github.com/sebarcode/codekit" + . "github.com/sebarcode/codekit" "bufio" @@ -144,7 +146,7 @@ func (q *Query) Cursor(m M) df.ICursor { } if hasWhere { - //fmt.Println("filters:", toolkit.JsonString(where)) + //fmt.Println("filters:", codekit.JsonString(where)) pipes = append(pipes, M{}.Set("$match", where)) } pipes = append(pipes, M{}.Set("$group", aggrExpression)) @@ -160,10 +162,10 @@ func (q *Query) Cursor(m M) df.ICursor { cursor.cursor = cur cursor.conn = conn if len(where) == 0 { - cursor.countParm = toolkit.M{}. + cursor.countParm = codekit.M{}. Set("count", tablename) } else { - cursor.countParm = toolkit.M{}. + cursor.countParm = codekit.M{}. Set("count", tablename). Set("query", where) } @@ -171,7 +173,7 @@ func (q *Query) Cursor(m M) df.ICursor { } else if hasCommand { cmdValue := commandParts.Value switch cmdValue.(type) { - case toolkit.M, bson.M: + case codekit.M, bson.M: var curCommand *mongo.Cursor err := wrapTx(conn, func(ctx mongo.SessionContext) error { var err error @@ -188,19 +190,19 @@ func (q *Query) Cursor(m M) df.ICursor { case string: switch cmdValue.(string) { case "aggregate", "pipe": - pipes := []toolkit.M{} + pipes := []codekit.M{} if hasWhere && len(where) > 0 { pipes = append(pipes, M{}.Set("$match", where)) } if hasPipe, pipeM := q.Command().HasAttr("pipe"); hasPipe { var ( - pipeMs []toolkit.M + pipeMs []codekit.M //ok bool cur *mongo.Cursor err error ) - toolkit.Serde(pipeM, &pipeMs, "") + serde.Serde(pipeM, &pipeMs) if len(pipeMs) > 0 { if _, has := pipeMs[0]["$text"]; has { pipes = pipeMs @@ -209,7 +211,7 @@ func (q *Query) Cursor(m M) df.ICursor { } } - //fmt.Println("pipe:", toolkit.JsonString(pipes), "\n") + //fmt.Println("pipe:", codekit.JsonString(pipes), "\n") if cur, err = coll.Aggregate(conn.ctx, pipes, new(options.AggregateOptions).SetAllowDiskUse(true)); err != nil { cursor.SetError(err) return cursor @@ -223,7 +225,7 @@ func (q *Query) Cursor(m M) df.ICursor { } default: - cursor.SetError(toolkit.Errorf("invalid command %v", cmdValue)) + cursor.SetError(fmt.Errorf("invalid command %v", cmdValue)) return cursor } /* @@ -235,7 +237,7 @@ func (q *Query) Cursor(m M) df.ICursor { case "pipe": pipe, ok := m["pipe"] if !ok { - cursor.SetError(toolkit.Errorf("invalid command, calling pipe without pipe data")) + cursor.SetError(fmt.Errorf("invalid command, calling pipe without pipe data")) return cursor } @@ -248,7 +250,7 @@ func (q *Query) Cursor(m M) df.ICursor { if items, ok := parts[df.QuerySelect]; ok { if fields, ok := items.Value.([]string); ok { if len(fields) > 0 { - projection := toolkit.M{} + projection := codekit.M{} for _, field := range fields { projection.Set(field, 1) } @@ -259,7 +261,7 @@ func (q *Query) Cursor(m M) df.ICursor { if items, ok := parts[df.QueryOrder]; ok { sortKeys := items.Value.([]string) - sortM := toolkit.M{} + sortM := codekit.M{} for _, key := range sortKeys { if key[0] == '-' { sortM.Set(key[1:], -1) @@ -291,7 +293,7 @@ func (q *Query) Cursor(m M) df.ICursor { err = wrapTx(conn, func(ctx mongo.SessionContext) error { var err error - //fmt.Println(toolkit.JsonString(opt.Sort)) + //fmt.Println(codekit.JsonString(opt.Sort)) qry, err = coll.Find(ctx, where, opt) return err }) @@ -303,9 +305,9 @@ func (q *Query) Cursor(m M) df.ICursor { cursor.cursor = qry if len(where) == 0 { - cursor.countParm = toolkit.M{}.Set("count", tablename) + cursor.countParm = codekit.M{}.Set("count", tablename) } else { - cursor.countParm = toolkit.M{}.Set("count", tablename).Set("query", where) + cursor.countParm = codekit.M{}.Set("count", tablename).Set("query", where) } cursor.conn = conn } @@ -326,7 +328,7 @@ func (q *Query) Execute(m M) (interface{}, error) { switch ct { case df.QueryInsert: var res *mongo.InsertOneResult - dataM, _ := toolkit.ToMTag(data, conn.FieldNameTag()) + dataM, _ := codekit.ToMTag(data, conn.FieldNameTag()) err := wrapTx(conn, func(ctx mongo.SessionContext) error { var err error res, err = coll.InsertOne(ctx, dataM) @@ -348,8 +350,8 @@ func (q *Query) Execute(m M) (interface{}, error) { updateqi, _ := parts[df.QueryUpdate] updatevals := updateqi.Value.([]string) - var dataM toolkit.M - dataM, err = toolkit.ToMTag(data, conn.FieldNameTag()) + var dataM codekit.M + dataM, err = codekit.ToMTag(data, conn.FieldNameTag()) dataS := M{} if err != nil { return nil, err @@ -368,25 +370,25 @@ func (q *Query) Execute(m M) (interface{}, error) { dataS[k] = v } } - //updatedData := toolkit.M{}.Set("$set", dataS) + //updatedData := codekit.M{}.Set("$set", dataS) err = wrapTx(conn, func(ctx mongo.SessionContext) error { _, err := coll.UpdateMany(ctx, where, - toolkit.M{}.Set("$set", dataS), + codekit.M{}.Set("$set", dataS), new(options.UpdateOptions).SetUpsert(false)) return err }) } else { err = wrapTx(conn, func(ctx mongo.SessionContext) error { _, err := coll.UpdateOne(ctx, where, - toolkit.M{}.Set("$set", data), + codekit.M{}.Set("$set", data), new(options.UpdateOptions).SetUpsert(false)) return err }) } return nil, err } else { - return nil, toolkit.Errorf("update need to have where clause") + return nil, fmt.Errorf("update need to have where clause") } case df.QueryDelete: @@ -397,19 +399,19 @@ func (q *Query) Execute(m M) (interface{}, error) { }) return nil, err } else { - return nil, toolkit.Errorf("delete need to have where clause. For delete all data in a collection, please use DropTable instead of Delete") + return nil, fmt.Errorf("delete need to have where clause. For delete all data in a collection, please use DropTable instead of Delete") } case df.QuerySave: whereSave := M{} - datam, err := toolkit.ToMTag(data, conn.FieldNameTag()) + datam, err := codekit.ToMTag(data, conn.FieldNameTag()) if err != nil { - return nil, toolkit.Errorf("unable to deserialize data: %s", err.Error()) + return nil, fmt.Errorf("unable to deserialize data: %s", err.Error()) } if datam.Has("_id") { whereSave = M{}.Set("_id", datam.Get("_id")) } else { - return nil, toolkit.Error("_id field is required") + return nil, errors.New("_id field is required") } err = wrapTx(conn, func(ctx mongo.SessionContext) error { @@ -422,17 +424,17 @@ func (q *Query) Execute(m M) (interface{}, error) { case df.QueryCommand: commands, ok := parts[df.QueryCommand] if !ok { - return nil, toolkit.Errorf("No command") + return nil, fmt.Errorf("No command") } - //mCommand := commands.Value.(toolkit.M) + //mCommand := commands.Value.(codekit.M) //cmd, _ := cmdValue := commands.Value switch cmdValue.(type) { case string: commandTxt := cmdValue.(string) if commandTxt == "" { - return nil, toolkit.Errorf("No command") + return nil, fmt.Errorf("No command") } var ( @@ -447,12 +449,12 @@ func (q *Query) Execute(m M) (interface{}, error) { bucketOpt.SetName(tablename) bucket, err = gridfs.NewBucket(conn.db, bucketOpt) if err != nil { - return nil, toolkit.Errorf("error prepare GridFS bucket. %s", err.Error()) + return nil, fmt.Errorf("error prepare GridFS bucket. %s", err.Error()) } } if hasParm, parm := q.Command().HasAttr("CommandParm"); hasParm { - m = parm.(toolkit.M) + m = parm.(codekit.M) } switch strings.ToLower(commandTxt) { case "gfswrite": @@ -462,7 +464,7 @@ func (q *Query) Execute(m M) (interface{}, error) { gfsFileName := m.GetString("name") reader = m.Get("source", nil).(io.Reader) if reader == nil { - return nil, toolkit.Errorf("invalid reader") + return nil, fmt.Errorf("invalid reader") } //-- check if file exist, delete if already exist @@ -471,7 +473,7 @@ func (q *Query) Execute(m M) (interface{}, error) { } if !hasMetadata { - gfsMetadata = toolkit.M{} + gfsMetadata = codekit.M{} } uploadOpt := new(options.UploadOptions) uploadOpt.SetMetadata(gfsMetadata) @@ -479,7 +481,7 @@ func (q *Query) Execute(m M) (interface{}, error) { gfsFileName = gfsId.(string) } if gfsFileName == "" { - gfsFileName = toolkit.RandomString(32) + gfsFileName = codekit.RandomString(32) } var objId primitive.ObjectID @@ -489,7 +491,7 @@ func (q *Query) Execute(m M) (interface{}, error) { objId, err = bucket.UploadFromStream(gfsFileName, reader, uploadOpt) } if err != nil { - return nil, toolkit.Errorf("error upload file to GridFS. %s", err.Error()) + return nil, fmt.Errorf("error upload file to GridFS. %s", err.Error()) } return objId, nil @@ -511,7 +513,7 @@ func (q *Query) Execute(m M) (interface{}, error) { defer ds.Close() if err != nil { - return nil, toolkit.Errorf("unable to open GFS %s-%s. %s", tablename, gfsFileName, err.Error()) + return nil, fmt.Errorf("unable to open GFS %s-%s. %s", tablename, gfsFileName, err.Error()) } defer ds.Close() @@ -539,20 +541,20 @@ func (q *Query) Execute(m M) (interface{}, error) { return vs, nil default: - return nil, toolkit.Errorf("Invalid command: %v", commandTxt) + return nil, fmt.Errorf("Invalid command: %v", commandTxt) } - case toolkit.M: - cmdM := cmdValue.(toolkit.M) + case codekit.M: + cmdM := cmdValue.(codekit.M) sr := conn.db.RunCommand(conn.ctx, cmdM) if sr.Err() != nil { - return nil, toolkit.Errorf("unablet to run command. %s. Command: %s", - sr.Err().Error(), toolkit.JsonString(cmdM)) + return nil, fmt.Errorf("unablet to run command. %s. Command: %s", + sr.Err().Error(), codekit.JsonString(cmdM)) } return sr, nil default: - return nil, toolkit.Errorf("Unknown command %v", cmdValue) + return nil, fmt.Errorf("Unknown command %v", cmdValue) } } @@ -589,11 +591,11 @@ func (q *Query) BuildCommand() (interface{}, error) { panic("not implemented") } -func (q *Query) Cursor(toolkit.M) dbflex.ICursor { +func (q *Query) Cursor(codekit.M) dbflex.ICursor { panic("not implemented") } -func (q *Query) Execute(toolkit.M) (interface{}, error) { +func (q *Query) Execute(codekit.M) (interface{}, error) { panic("not implemented") } @@ -601,7 +603,7 @@ func (q *Query) SetConfig(string, interface{}) { panic("not implemented") } -func (q *Query) SetConfigM(toolkit.M) { +func (q *Query) SetConfigM(codekit.M) { panic("not implemented") } From 8ab032c11c3af8d7e03acc673e38a489d86ffb7f Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Fri, 17 Jun 2022 07:08:50 +0700 Subject: [PATCH 23/51] fix deps and serde --- go.mod | 18 ++++++++---------- go.sum | 12 +++++++++--- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/go.mod b/go.mod index c558ed5..94aeeea 100644 --- a/go.mod +++ b/go.mod @@ -2,21 +2,19 @@ module github.com/ariefdarmawan/flexmgo go 1.16 -replace git.kanosolution.net/kano/dbflex => D:\Coding\lib\dbflex +//replace git.kanosolution.net/kano/dbflex => D:\Coding\lib\dbflex -replace github.com/sebarcode/codekit => D:\Coding\lib\codekit +//replace github.com/sebarcode/codekit => D:\Coding\lib\codekit -replace github.com/sebarcode/logger => D:\Coding\lib\logger +//replace github.com/sebarcode/logger => D:\Coding\lib\logger -replace github.com/ariefdarmawan/serde => D:\Coding\lib\serde - -//replace git.kanosolution.net/kano/dbflex => D:\coding\lib\dbflex +//replace github.com/ariefdarmawan/serde => D:\Coding\lib\serde require ( - git.kanosolution.net/kano/dbflex v1.1.0 - github.com/ariefdarmawan/serde v0.0.0-20220616152415-ac746173e86f - github.com/sebarcode/codekit v0.0.0-20220616144406-d7c5cafaca19 - github.com/sebarcode/logger v0.0.0-00010101000000-000000000000 + git.kanosolution.net/kano/dbflex v1.2.1 + github.com/ariefdarmawan/serde v0.1.0 + github.com/sebarcode/codekit v0.1.0 + github.com/sebarcode/logger v0.1.1 github.com/smartystreets/goconvey v1.7.2 go.mongodb.org/mongo-driver v1.9.1 ) diff --git a/go.sum b/go.sum index 9323b3f..e4a31ac 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,9 @@ +git.kanosolution.net/kano/dbflex v1.2.1 h1:SllhA0qMcF1BHOYMdXIsIj1cbqohj2XgFp+7c4ZrZls= +git.kanosolution.net/kano/dbflex v1.2.1/go.mod h1:kMbDllDjd+lwhCO4XRrCJP1kP0X46iPPrNG7hblrbcA= github.com/ariefdarmawan/reflector v0.0.3 h1:qbfwP5iKMvdYBWRG2jZyoXRLPsGedJ1sNcC8bw+MhpY= github.com/ariefdarmawan/reflector v0.0.3/go.mod h1:xxeY7n6iT0q5pK4j3s8l2xo1CvmQgS2oYtj0lsV4eo4= +github.com/ariefdarmawan/serde v0.1.0 h1:gVml1PZbomom4+gLcvM8k86PW05VWTy3EM7NaMBzPR8= +github.com/ariefdarmawan/serde v0.1.0/go.mod h1:geSqYU2wYlGlmOePjLbU9S1A4FkkaQB59vuAnTeZclM= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -26,6 +30,10 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sebarcode/codekit v0.1.0 h1:yPYtVJyfEIc7qQDwRZHt29P0rMv5m2Wsck88Gdox4zQ= +github.com/sebarcode/codekit v0.1.0/go.mod h1:o54sVKGC7+M1leMkKhVXssOXwuHCylW5GJto0UiQJEg= +github.com/sebarcode/logger v0.1.1 h1:qy8Ip5UacaK4LFE0hN/HXUCIu3ZBe4uta8WHQ067jfQ= +github.com/sebarcode/logger v0.1.1/go.mod h1:dBcREBmgQLUN1kAAs5t9y7dN5RUp+OYEsk4rqUWuM5Q= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= github.com/smartystreets/assertions v1.2.0 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N3yZFZkDFs= github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo= @@ -54,14 +62,12 @@ golang.org/x/crypto v0.0.0-20201216223049-8b5274cf687f h1:aZp0e2vLN4MToVqnjNEYEt golang.org/x/crypto v0.0.0-20201216223049-8b5274cf687f/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa h1:F+8P+gmewFQYRk6JoLQLwjBCTu3mcIURZfNkVweuRKA= golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20200202094626-16171245cfb2 h1:CCH4IOTTfewWjGOlSp+zGcjutRKlBEZQ6wTn8ozI/nI= -golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e h1:vcxGaoTs7kV8m5Np9uUNQin4BrLOthgV7252N8V+FwY= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190531175056-4c3a928424d2/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= From 63e2ac138a1f88ee0ddcdd9da19832cd82d59819 Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Fri, 17 Jun 2022 07:10:51 +0700 Subject: [PATCH 24/51] update deps and change compat to minimum v1.6 --- go.mod | 8 -------- 1 file changed, 8 deletions(-) diff --git a/go.mod b/go.mod index 94aeeea..9c039ff 100644 --- a/go.mod +++ b/go.mod @@ -2,14 +2,6 @@ module github.com/ariefdarmawan/flexmgo go 1.16 -//replace git.kanosolution.net/kano/dbflex => D:\Coding\lib\dbflex - -//replace github.com/sebarcode/codekit => D:\Coding\lib\codekit - -//replace github.com/sebarcode/logger => D:\Coding\lib\logger - -//replace github.com/ariefdarmawan/serde => D:\Coding\lib\serde - require ( git.kanosolution.net/kano/dbflex v1.2.1 github.com/ariefdarmawan/serde v0.1.0 From 4cf07d72efc443a29ede0ae492e8923e09847c1b Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Fri, 17 Jun 2022 07:48:23 +0700 Subject: [PATCH 25/51] update fetch and fetchs decode mechanism --- cursor.go | 166 +++++++++++++----------------------------------------- helper.go | 21 +++++++ 2 files changed, 61 insertions(+), 126 deletions(-) create mode 100644 helper.go diff --git a/cursor.go b/cursor.go index 18af330..2e56946 100644 --- a/cursor.go +++ b/cursor.go @@ -4,10 +4,8 @@ import ( "fmt" "io" "reflect" - "time" "git.kanosolution.net/kano/dbflex" - "github.com/ariefdarmawan/serde" "github.com/sebarcode/codekit" "go.mongodb.org/mongo-driver/mongo" ) @@ -78,29 +76,10 @@ func (cr *Cursor) Fetch(out interface{}) dbflex.ICursor { return cr } - m := codekit.M{} - if err := cr.cursor.Decode(&m); err != nil { + if err := cr.cursor.Decode(out); err != nil { cr.SetError(fmt.Errorf("unable to decode output. %s", err.Error())) return cr } - for mk, mv := range m { - // update date value to date - if mvs, ok := mv.(string); ok && len(mvs) >= 11 { - if mvs[4] == '-' && mvs[7] == '-' && mvs[10] == 'T' { - if dt, err := time.Parse(time.RFC3339, mvs); err == nil { - m.Set(mk, dt) - } - } - } - } - if reflect.ValueOf(m).Type().String() == reflect.Indirect(reflect.ValueOf(out)).Type().String() { - reflect.ValueOf(out).Elem().Set(reflect.ValueOf(m)) - } else { - if err := serde.Serde(m, out); err != nil { - cr.SetError(fmt.Errorf("unable to decode output. %s", err.Error())) - return cr - } - } return cr } @@ -111,129 +90,64 @@ func (cr *Cursor) Fetchs(result interface{}, n int) dbflex.ICursor { return cr } - /* - v := reflect.TypeOf(result).Elem().Elem() - ivs := reflect.MakeSlice(reflect.SliceOf(v), 0, 0) - - read := 0 - for { - if !cr.cursor.Next(cr.conn.ctx) { - break - } - - iv := reflect.New(v).Interface() - err := cr.cursor.Decode(iv) - if err != nil { - cr.SetError(fmt.Errorf("unable to decode cursor data. %s", err.Error())) - return cr - } - ivs = reflect.Append(ivs, reflect.ValueOf(iv).Elem()) - - read++ - if n != 0 && read == n { - break - } - } - reflect.ValueOf(result).Elem().Set(ivs) - */ + v := reflect.ValueOf(result) + if v.Kind() != reflect.Ptr { + return cr.SetError(fmt.Errorf("result should be a pointer of slice")) + } + v = v.Elem() + if v.Kind() != reflect.Slice { + return cr.SetError(fmt.Errorf("result should be a pointer of slice")) + } + + sliceType := v.Type() + elemType := sliceType.Elem() + elemIsPtr := elemType.Kind() == reflect.Ptr + + destBuffer := reflect.MakeSlice(sliceType, 1000, 1000) read := 0 - ms := []codekit.M{} + used := 0 for { if !cr.cursor.Next(cr.conn.ctx) { break } - m := codekit.M{} - err := cr.cursor.Decode(&m) + destItemValue := createPtrFromType(elemType) + destItem := destItemValue.Interface() + err := cr.cursor.Decode(destItem) if err != nil { cr.SetError(fmt.Errorf("unable to decode cursor data. %s", err.Error())) return cr } - for mk, mv := range m { - // update date value to date - if mvs, ok := mv.(string); ok && len(mvs) >= 11 { - if mvs[4] == '-' && mvs[7] == '-' && mvs[10] == 'T' { - if dt, err := time.Parse(time.RFC3339, mvs); err == nil { - m.Set(mk, dt) - //fmt.Println(mk, mvs, dt, m, fmt.Sprintf("%t", m.Get("Created"))) - } - } - } + + if elemIsPtr { + destBuffer.Index(read).Set(reflect.ValueOf(destItem)) + } else { + destBuffer.Index(read).Set(reflect.ValueOf(destItem).Elem()) } - ms = append(ms, m) read++ + used++ + + if used == 1000 { + used = 0 + newLen := read + 1000 - 1 + biggerBuffer := reflect.MakeSlice(sliceType, newLen, newLen) + reflect.Copy(biggerBuffer, destBuffer) + destBuffer = biggerBuffer + } + if n != 0 && read == n { break } } - if reflect.ValueOf(ms).Type().String() == reflect.Indirect(reflect.ValueOf(result)).Type().String() { - reflect.ValueOf(result).Elem().Set(reflect.ValueOf(ms)) + + if destBuffer.Len() != read { + lesseBuffer := reflect.MakeSlice(sliceType, read, read) + reflect.Copy(lesseBuffer, destBuffer) + v.Set(lesseBuffer) } else { - if err := serde.Serde(ms, result); err != nil { - cr.SetError(fmt.Errorf("unable to decode cursor data. %s", err.Error())) - return cr - } + v.Set(destBuffer) } return cr } - -/* -func (cr *Cursor) Reset() error { - panic("not implemented") -} - -func (cr *Cursor) Fetch(interface{}) error { - panic("not implemented") -} - -func (cr *Cursor) Fetchs(interface{}, int) error { - panic("not implemented") -} - - -func (cr *Cursor) CountAsync() <-chan int { - panic("not implemented") -} - -func (cr *Cursor) Error() error { - panic("not implemented") -} - -func (cr *Cursor) CloseAfterFetch() bool { - panic("not implemented") -} - -func (cr *Cursor) SetCountCommand(dbflex.ICommand) { - panic("not implemented") -} - -func (cr *Cursor) CountCommand() dbflex.ICommand { - panic("not implemented") -} - -func (cr *Cursor) Connection() dbflex.IConnection { - panic("not implemented") -} - -func (cr *Cursor) SetConnection(dbflex.IConnection) { - panic("not implemented") -} - -func (cr *Cursor) ConfigRef(key string, def interface{}, out interface{}) { - panic("not implemented") -} - -func (cr *Cursor) Set(key string, value interface{}) { - panic("not implemented") -} - -func (cr *Cursor) SetCloseAfterFetch() dbflex.ICursor { - panic("not implemented") -} - -func (cr *Cursor) AutoClose(time.Duration) dbflex.ICursor { - panic("not implemented") -} -*/ diff --git a/helper.go b/helper.go new file mode 100644 index 0000000..e3d7ac2 --- /dev/null +++ b/helper.go @@ -0,0 +1,21 @@ +package flexmgo + +import "reflect" + +func createPtrFromType(t reflect.Type) reflect.Value { + isPtr := t.Kind() == reflect.Ptr + elemType := t + + if isPtr { + elemType = elemType.Elem() + } + + if elemType.Kind() == reflect.Map { + ptr := reflect.New(elemType) + m := reflect.MakeMap(elemType) + ptr.Elem().Set(m) + return ptr + } + + return reflect.New(elemType) +} From 118c4193d972bf911d6b0f4b1702c0585de90230 Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Wed, 6 Jul 2022 21:23:50 +0700 Subject: [PATCH 26/51] apply ensureIndex --- connection.go | 78 +++++++++++++++++++++++++++++++++++++++++++++++++ cursor.go | 6 +--- flexmgo_test.go | 52 +++++++++++++++++++++++++++++++++ go.mod | 2 +- go.sum | 2 ++ 5 files changed, 134 insertions(+), 6 deletions(-) diff --git a/connection.go b/connection.go index 4f26964..e1872b3 100644 --- a/connection.go +++ b/connection.go @@ -8,6 +8,8 @@ import ( "git.kanosolution.net/kano/dbflex" "github.com/sebarcode/codekit" + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/readconcern" @@ -129,6 +131,82 @@ func (c *Connection) EnsureTable(name string, keys []string, obj interface{}) er return nil } +func (c *Connection) EnsureIndex(tableName, indexName string, isUnique bool, fields ...string) error { + indexFound := false + currentIndex := bson.M{} + ctx := context.Background() + coll := c.db.Collection(tableName) + cursorIndex, e := coll.Indexes().List(ctx) + if e != nil { + return e + } + + for cursorIndex.Next(ctx) { + if e := cursorIndex.Decode(¤tIndex); e != nil { + continue + } + + if currentIndex["name"].(string) == indexName { + indexFound = true + break + } + } + + createIndex := false + if indexFound { + keys := currentIndex["key"].(primitive.M) + unique, uniqueOK := currentIndex["unique"] + if uniqueOK && unique != isUnique { + createIndex = true + } else { + keyChecking: + for _, f := range fields { + fieldName := f + indexValue := 1 + if f[0] == '-' { + fieldName = f[1:] + indexValue = -1 + } + + if existingIndexValue, ok := keys[fieldName]; !ok { + createIndex = true + break keyChecking + } else if existingIndexValue != indexValue { + createIndex = true + break keyChecking + } + } + + } + } else { + createIndex = true + } + + if createIndex { + if indexFound { + coll.Indexes().DropOne(ctx, indexName) + } + + indexKeys := bson.D{} + for _, f := range fields { + if f[0] == '-' { + indexKeys = append(indexKeys, bson.E{f[1:], -1}) + } else { + indexKeys = append(indexKeys, bson.E{f, 1}) + } + } + + if _, err := coll.Indexes().CreateOne(ctx, mongo.IndexModel{ + Keys: indexKeys, + Options: options.Index().SetName(indexName).SetUnique(isUnique), + }); err != nil { + return err + } + + } + return nil +} + func (c *Connection) DropTable(name string) error { return c.db.Collection(name).Drop(c.ctx) } diff --git a/cursor.go b/cursor.go index 2e56946..0ef4990 100644 --- a/cursor.go +++ b/cursor.go @@ -106,11 +106,7 @@ func (cr *Cursor) Fetchs(result interface{}, n int) dbflex.ICursor { destBuffer := reflect.MakeSlice(sliceType, 1000, 1000) read := 0 used := 0 - for { - if !cr.cursor.Next(cr.conn.ctx) { - break - } - + for cr.cursor.Next(cr.conn.ctx) { destItemValue := createPtrFromType(elemType) destItem := destItemValue.Interface() err := cr.cursor.Decode(destItem) diff --git a/flexmgo_test.go b/flexmgo_test.go index 885af39..5f5290b 100644 --- a/flexmgo_test.go +++ b/flexmgo_test.go @@ -3,6 +3,7 @@ package flexmgo_test import ( "bufio" "bytes" + "context" "fmt" "math" "strings" @@ -12,14 +13,21 @@ import ( "git.kanosolution.net/kano/dbflex/orm" _ "github.com/ariefdarmawan/flexmgo" logger "github.com/sebarcode/logger" + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/bson/primitive" + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" "git.kanosolution.net/kano/dbflex" "github.com/sebarcode/codekit" + "github.com/smartystreets/goconvey/convey" cv "github.com/smartystreets/goconvey/convey" ) const ( connTxt = "mongodb://localhost:27017/dbapp" + host = "mongodb://localhost:27017" + dbname = "dbapp" ) func init() { @@ -422,6 +430,50 @@ func TestGridFsUpdate(t *testing.T) { }) } +func TestCheckIndex(t *testing.T) { + cv.Convey("connect and create index", t, func() { + conn, _ := connect() + defer conn.Close() + + e := conn.EnsureIndex(tablename, "record_salary_index", false, "Age", "-Salary") + cv.So(e, cv.ShouldBeNil) + + cv.Convey("validate", func() { + ctx := context.TODO() + mconn, _ := mongo.Connect(ctx, options.Client().ApplyURI(host)) + defer mconn.Disconnect(ctx) + + indexCursor, e := mconn.Database(dbname).Collection(tablename).Indexes().List(ctx) + cv.So(e, cv.ShouldBeNil) + + indexModels := []bson.M{} + indexCursor.All(ctx, &indexModels) + + found := false + indexOk := false + for _, index := range indexModels { + //cv.Println(codekit.JsonString(index)) + name, nameOk := index["name"].(string) + if nameOk && name == "record_salary_index" { + found = true + key := index["key"].(primitive.M) + indexOk = key["Age"].(int32) == 1 && key["Salary"].(int32) == -1 + } + } + + cv.So(found, cv.ShouldBeTrue) + cv.So(indexOk, cv.ShouldBeTrue) + + convey.Convey("reindex", func() { + e := conn.EnsureIndex(tablename, "record_salary_index", false, "Age", "-Salary") + cv.So(e, cv.ShouldBeNil) + + mconn.Database(dbname).Collection(tablename).Indexes().DropOne(ctx, "record_salary_index") + }) + }) + }) +} + /* TO DO - Command Cursor diff --git a/go.mod b/go.mod index 9c039ff..72cc0b7 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/ariefdarmawan/flexmgo go 1.16 require ( - git.kanosolution.net/kano/dbflex v1.2.1 + git.kanosolution.net/kano/dbflex v1.2.2-0.20220706113442-c678736ba69e github.com/ariefdarmawan/serde v0.1.0 github.com/sebarcode/codekit v0.1.0 github.com/sebarcode/logger v0.1.1 diff --git a/go.sum b/go.sum index e4a31ac..8cba5d3 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ git.kanosolution.net/kano/dbflex v1.2.1 h1:SllhA0qMcF1BHOYMdXIsIj1cbqohj2XgFp+7c4ZrZls= git.kanosolution.net/kano/dbflex v1.2.1/go.mod h1:kMbDllDjd+lwhCO4XRrCJP1kP0X46iPPrNG7hblrbcA= +git.kanosolution.net/kano/dbflex v1.2.2-0.20220706113442-c678736ba69e h1:eB4hM5lhGQUKidwIcqpm0B9aQAMcoqeSK/Mqzajv0Ho= +git.kanosolution.net/kano/dbflex v1.2.2-0.20220706113442-c678736ba69e/go.mod h1:wa4mqv+CP6fBRC6dVD4Z2473iQtiat8fWf96gBK9//E= github.com/ariefdarmawan/reflector v0.0.3 h1:qbfwP5iKMvdYBWRG2jZyoXRLPsGedJ1sNcC8bw+MhpY= github.com/ariefdarmawan/reflector v0.0.3/go.mod h1:xxeY7n6iT0q5pK4j3s8l2xo1CvmQgS2oYtj0lsV4eo4= github.com/ariefdarmawan/serde v0.1.0 h1:gVml1PZbomom4+gLcvM8k86PW05VWTy3EM7NaMBzPR8= From 7cc47b40c1c5f6b22d19dcd671bf88d88c45c154 Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Wed, 6 Jul 2022 21:28:58 +0700 Subject: [PATCH 27/51] update go mod --- go.mod | 2 +- go.sum | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index 72cc0b7..719dbb7 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/ariefdarmawan/flexmgo go 1.16 require ( - git.kanosolution.net/kano/dbflex v1.2.2-0.20220706113442-c678736ba69e + git.kanosolution.net/kano/dbflex v1.2.2 github.com/ariefdarmawan/serde v0.1.0 github.com/sebarcode/codekit v0.1.0 github.com/sebarcode/logger v0.1.1 diff --git a/go.sum b/go.sum index 8cba5d3..308e341 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,5 @@ -git.kanosolution.net/kano/dbflex v1.2.1 h1:SllhA0qMcF1BHOYMdXIsIj1cbqohj2XgFp+7c4ZrZls= -git.kanosolution.net/kano/dbflex v1.2.1/go.mod h1:kMbDllDjd+lwhCO4XRrCJP1kP0X46iPPrNG7hblrbcA= -git.kanosolution.net/kano/dbflex v1.2.2-0.20220706113442-c678736ba69e h1:eB4hM5lhGQUKidwIcqpm0B9aQAMcoqeSK/Mqzajv0Ho= -git.kanosolution.net/kano/dbflex v1.2.2-0.20220706113442-c678736ba69e/go.mod h1:wa4mqv+CP6fBRC6dVD4Z2473iQtiat8fWf96gBK9//E= +git.kanosolution.net/kano/dbflex v1.2.2 h1:OjYL02l1zriJNbWj3oTVSFOcHxPcaKqafkQM0OsAfxg= +git.kanosolution.net/kano/dbflex v1.2.2/go.mod h1:wa4mqv+CP6fBRC6dVD4Z2473iQtiat8fWf96gBK9//E= github.com/ariefdarmawan/reflector v0.0.3 h1:qbfwP5iKMvdYBWRG2jZyoXRLPsGedJ1sNcC8bw+MhpY= github.com/ariefdarmawan/reflector v0.0.3/go.mod h1:xxeY7n6iT0q5pK4j3s8l2xo1CvmQgS2oYtj0lsV4eo4= github.com/ariefdarmawan/serde v0.1.0 h1:gVml1PZbomom4+gLcvM8k86PW05VWTy3EM7NaMBzPR8= From ce9df0376b1b1ff4ac0113ba7ad9313e399b7fab Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Sun, 14 Aug 2022 19:32:11 +0700 Subject: [PATCH 28/51] all dan elemMatch --- filter_test.go | 23 +++++++++++++++++++++++ go.mod | 2 +- go.sum | 14 ++++++++++---- query.go | 22 ++++++++++++++++++++-- 4 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 filter_test.go diff --git a/filter_test.go b/filter_test.go new file mode 100644 index 0000000..128c4fa --- /dev/null +++ b/filter_test.go @@ -0,0 +1,23 @@ +package flexmgo_test + +import ( + "fmt" + "testing" + + "git.kanosolution.net/kano/dbflex" + "github.com/ariefdarmawan/flexmgo" + "github.com/sebarcode/codekit" +) + +func TestElemMatch(t *testing.T) { + queryFilter := dbflex.And( + dbflex.Eq("SafeCardType", "Safe"), + dbflex.Or( + dbflex.ElemMatch("Dimension", dbflex.Eq("Kind", "Company"), dbflex.Eq("Value", "Kano")), + dbflex.ElemMatch("Dimension", dbflex.Eq("Kind", "Project"), dbflex.Eq("Value", "Petrosea")), + )) + + q := new(flexmgo.Query) + qfm, _ := q.BuildFilter(queryFilter) + fmt.Println("filter", codekit.JsonString(qfm)) +} diff --git a/go.mod b/go.mod index 719dbb7..ccfd85b 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/ariefdarmawan/flexmgo go 1.16 require ( - git.kanosolution.net/kano/dbflex v1.2.2 + git.kanosolution.net/kano/dbflex v1.2.6-0.20220814122711-97eb283c056e github.com/ariefdarmawan/serde v0.1.0 github.com/sebarcode/codekit v0.1.0 github.com/sebarcode/logger v0.1.1 diff --git a/go.sum b/go.sum index 308e341..31365df 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -git.kanosolution.net/kano/dbflex v1.2.2 h1:OjYL02l1zriJNbWj3oTVSFOcHxPcaKqafkQM0OsAfxg= -git.kanosolution.net/kano/dbflex v1.2.2/go.mod h1:wa4mqv+CP6fBRC6dVD4Z2473iQtiat8fWf96gBK9//E= +git.kanosolution.net/kano/dbflex v1.2.6-0.20220814122711-97eb283c056e h1:FkR5b7mqG2O1JTF5oxfKCsXKh3+d21mAmZ/g/Jm9uGM= +git.kanosolution.net/kano/dbflex v1.2.6-0.20220814122711-97eb283c056e/go.mod h1:FQZKWzBpYXNalY8syaXBjVBXyg58hIuUXQuIB08l7Zw= github.com/ariefdarmawan/reflector v0.0.3 h1:qbfwP5iKMvdYBWRG2jZyoXRLPsGedJ1sNcC8bw+MhpY= github.com/ariefdarmawan/reflector v0.0.3/go.mod h1:xxeY7n6iT0q5pK4j3s8l2xo1CvmQgS2oYtj0lsV4eo4= github.com/ariefdarmawan/serde v0.1.0 h1:gVml1PZbomom4+gLcvM8k86PW05VWTy3EM7NaMBzPR8= @@ -62,17 +62,23 @@ golang.org/x/crypto v0.0.0-20201216223049-8b5274cf687f h1:aZp0e2vLN4MToVqnjNEYEt golang.org/x/crypto v0.0.0-20201216223049-8b5274cf687f/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa h1:F+8P+gmewFQYRk6JoLQLwjBCTu3mcIURZfNkVweuRKA= golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f h1:OfiFi4JbukWwe3lzw+xunroH1mnC1e2Gy5cxNJApiSY= +golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e h1:vcxGaoTs7kV8m5Np9uUNQin4BrLOthgV7252N8V+FwY= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.5 h1:i6eZZ+zk0SOf0xgBpEpPD18qWcJda6q1sxt3S0kzyUQ= golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190531172133-b3315ee88b7d/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= diff --git a/query.go b/query.go index 5f08899..9c6f1f4 100644 --- a/query.go +++ b/query.go @@ -87,7 +87,6 @@ func (q *Query) BuildFilter(f *df.Filter) (interface{}, error) { bfs = append(bfs, bf) } } - fm.Set(string(f.Op), bfs) } else if f.Op == df.OpNot { bf, eb := q.BuildFilter(f.Items[0]) @@ -95,8 +94,27 @@ func (q *Query) BuildFilter(f *df.Filter) (interface{}, error) { field := f.Items[0].Field fm.Set(field, M{}.Set("$not", bf.(M).Get(field))) } + } else if f.Op == df.OpAll { + values, ok := f.Value.([]interface{}) + if !ok { + return nil, fmt.Errorf("fail to translate %s. %s", f.Op, JsonString(f)) + } + fm.Set(f.Field, M{}.Set("$all", values)) + } else if f.Op == df.OpElemMatch { + matchM := M{} + for _, item := range f.Items { + bf, eb := q.BuildFilter(item) + if eb != nil { + return nil, fmt.Errorf("error translate filter %s", JsonString(item)) + } + bfm := bf.(codekit.M) + for k, v := range bfm { + matchM.Set(k, v) + } + } + fm.Set(f.Field, M{}.Set("$elemMatch", matchM)) } else { - return nil, fmt.Errorf("Filter Op %s is not defined", f.Op) + return nil, fmt.Errorf("filter op %s is not defined", f.Op) } return fm, nil } From e18f3a5414c8afb46b06f92d955a7ba365759e81 Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Sun, 14 Aug 2022 19:44:14 +0700 Subject: [PATCH 29/51] update mod to latest --- go.mod | 6 +++--- go.sum | 46 ++++++++++++++++++++++------------------------ 2 files changed, 25 insertions(+), 27 deletions(-) diff --git a/go.mod b/go.mod index ccfd85b..9f4c293 100644 --- a/go.mod +++ b/go.mod @@ -4,9 +4,9 @@ go 1.16 require ( git.kanosolution.net/kano/dbflex v1.2.6-0.20220814122711-97eb283c056e - github.com/ariefdarmawan/serde v0.1.0 - github.com/sebarcode/codekit v0.1.0 + github.com/ariefdarmawan/serde v0.1.1 + github.com/sebarcode/codekit v0.1.2 github.com/sebarcode/logger v0.1.1 github.com/smartystreets/goconvey v1.7.2 - go.mongodb.org/mongo-driver v1.9.1 + go.mongodb.org/mongo-driver v1.10.1 ) diff --git a/go.sum b/go.sum index 31365df..71e3446 100644 --- a/go.sum +++ b/go.sum @@ -2,15 +2,13 @@ git.kanosolution.net/kano/dbflex v1.2.6-0.20220814122711-97eb283c056e h1:FkR5b7m git.kanosolution.net/kano/dbflex v1.2.6-0.20220814122711-97eb283c056e/go.mod h1:FQZKWzBpYXNalY8syaXBjVBXyg58hIuUXQuIB08l7Zw= github.com/ariefdarmawan/reflector v0.0.3 h1:qbfwP5iKMvdYBWRG2jZyoXRLPsGedJ1sNcC8bw+MhpY= github.com/ariefdarmawan/reflector v0.0.3/go.mod h1:xxeY7n6iT0q5pK4j3s8l2xo1CvmQgS2oYtj0lsV4eo4= -github.com/ariefdarmawan/serde v0.1.0 h1:gVml1PZbomom4+gLcvM8k86PW05VWTy3EM7NaMBzPR8= -github.com/ariefdarmawan/serde v0.1.0/go.mod h1:geSqYU2wYlGlmOePjLbU9S1A4FkkaQB59vuAnTeZclM= +github.com/ariefdarmawan/serde v0.1.1 h1:TIhTKKFfK4WfYcS/hW1nSD81Uqd1jkY9Xli8e/3h/MA= +github.com/ariefdarmawan/serde v0.1.1/go.mod h1:geSqYU2wYlGlmOePjLbU9S1A4FkkaQB59vuAnTeZclM= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/eaciit/toolkit v0.0.0-20210610161449-593d5fadf78e h1:2/wdIW25ZaPHfKQ5HvQUSAqeH9KAZQH1PlCMnrSJ9xE= github.com/eaciit/toolkit v0.0.0-20210610161449-593d5fadf78e/go.mod h1:r4OKDNGrY6n6gCVqEFdld+JTfgdNgp78RPvZHr2B9jU= -github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk= -github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/gofrs/flock v0.8.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU= github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4= github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= @@ -25,13 +23,15 @@ github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47e github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe h1:iruDEfMl2E6fbMZ9s0scYfZQ84/6SPL6zC8ACM2oIL0= github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/sebarcode/codekit v0.1.0 h1:yPYtVJyfEIc7qQDwRZHt29P0rMv5m2Wsck88Gdox4zQ= github.com/sebarcode/codekit v0.1.0/go.mod h1:o54sVKGC7+M1leMkKhVXssOXwuHCylW5GJto0UiQJEg= +github.com/sebarcode/codekit v0.1.2 h1:ycQnlEo+YTbqXfRlp+K7J3ASQkDiu/3sWUm790Eexl4= +github.com/sebarcode/codekit v0.1.2/go.mod h1:CNHNFOALfsqgDmu+GIFSo4RAoCsECBCS84RmHbqj88I= github.com/sebarcode/logger v0.1.1 h1:qy8Ip5UacaK4LFE0hN/HXUCIu3ZBe4uta8WHQ067jfQ= github.com/sebarcode/logger v0.1.1/go.mod h1:dBcREBmgQLUN1kAAs5t9y7dN5RUp+OYEsk4rqUWuM5Q= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= @@ -49,43 +49,41 @@ 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-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c= github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI= -github.com/xdg-go/scram v1.0.2 h1:akYIkZ28e6A96dkWNJQu3nmCzH3YfwMPQExUYDaRv7w= -github.com/xdg-go/scram v1.0.2/go.mod h1:1WAq6h33pAW+iRreB34OORO2Nf7qel3VV3fjBj+hCSs= -github.com/xdg-go/stringprep v1.0.2 h1:6iq84/ryjjeRmMJwxutI51F2GIPlP5BfTvXHeYjyhBc= -github.com/xdg-go/stringprep v1.0.2/go.mod h1:8F9zXuvzgwmyT5DUm4GUfZGDdT3W+LCvS6+da4O5kxM= +github.com/xdg-go/scram v1.1.1 h1:VOMT+81stJgXW3CpHyqHN3AXDYIMsx56mEFrB37Mb/E= +github.com/xdg-go/scram v1.1.1/go.mod h1:RaEWvsqvNKKvBPvcKeFjrG2cJqOkHTiyTpzz23ni57g= +github.com/xdg-go/stringprep v1.0.3 h1:kdwGpVNwPFtjs98xCGkHjQtGKh86rDcRZN17QEMCOIs= +github.com/xdg-go/stringprep v1.0.3/go.mod h1:W3f5j4i+9rC0kuIEJL0ky1VpHXQU3ocBgklLGvcBnW8= github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d h1:splanxYIlg+5LfHAM6xpdFEAYOk8iySO56hMFq6uLyA= github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA= -go.mongodb.org/mongo-driver v1.9.1 h1:m078y9v7sBItkt1aaoe2YlvWEXcD263e1a4E1fBrJ1c= -go.mongodb.org/mongo-driver v1.9.1/go.mod h1:0sQWfOeY63QTntERDJJ/0SuKK0T1uVSgKCuAROlKEPY= +go.mongodb.org/mongo-driver v1.10.1 h1:NujsPveKwHaWuKUer/ceo9DzEe7HIj1SlJ6uvXZG0S4= +go.mongodb.org/mongo-driver v1.10.1/go.mod h1:z4XpeoU6w+9Vht+jAFyLgVrD+jGSQQe0+CBWFHNiHt8= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20201216223049-8b5274cf687f h1:aZp0e2vLN4MToVqnjNEYEtrEA8RH8U8FN1CU7JgqsPU= -golang.org/x/crypto v0.0.0-20201216223049-8b5274cf687f/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= +golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d h1:sK3txAijHtOK88l68nt020reeT1ZdKLIYetKl95FzVY= +golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f h1:OfiFi4JbukWwe3lzw+xunroH1mnC1e2Gy5cxNJApiSY= golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e h1:vcxGaoTs7kV8m5Np9uUNQin4BrLOthgV7252N8V+FwY= -golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 h1:CIJ76btIcR3eFI5EgSo6k1qKw9KJexJuRLI9G7Hp5wE= +golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= -golang.org/x/tools v0.0.0-20190531172133-b3315ee88b7d/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= From d2d897fbf5bc55bea04373e5e7548dd66d7cf56e Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Thu, 25 Aug 2022 09:01:22 +0700 Subject: [PATCH 30/51] applying disableTx --- connection.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/connection.go b/connection.go index e1872b3..55f789a 100644 --- a/connection.go +++ b/connection.go @@ -22,6 +22,8 @@ type Connection struct { client *mongo.Client db *mongo.Database sess mongo.Session + + _disableTx bool } func (c *Connection) Connect() error { @@ -212,6 +214,10 @@ func (c *Connection) DropTable(name string) error { } func (c *Connection) BeginTx() error { + if c._disableTx { + return errors.New("tx is disabled") + } + wc := writeconcern.New(writeconcern.WMajority()) rc := readconcern.Snapshot() txnOpts := options.Transaction().SetWriteConcern(wc).SetReadConcern(rc) @@ -257,11 +263,16 @@ func (c *Connection) RollBack() error { return nil } +func (c *Connection) DisableTx() { + c._disableTx = true +} + func (c *Connection) IsTx() bool { return c.sess != nil } // SupportTx to identify if underlying connection support Tx or not func (c *Connection) SupportTx() bool { + if (c._disableTx) return false return true } From a80e3badb371dc5d58ee590571f6be640c0014b6 Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Thu, 25 Aug 2022 09:05:06 +0700 Subject: [PATCH 31/51] fix supportTx --- connection.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/connection.go b/connection.go index 55f789a..11e6738 100644 --- a/connection.go +++ b/connection.go @@ -217,7 +217,7 @@ func (c *Connection) BeginTx() error { if c._disableTx { return errors.New("tx is disabled") } - + wc := writeconcern.New(writeconcern.WMajority()) rc := readconcern.Snapshot() txnOpts := options.Transaction().SetWriteConcern(wc).SetReadConcern(rc) @@ -273,6 +273,8 @@ func (c *Connection) IsTx() bool { // SupportTx to identify if underlying connection support Tx or not func (c *Connection) SupportTx() bool { - if (c._disableTx) return false + if (c._disableTx) { + return false + } return true } From 4bf8a919eff81f139a412af49f86fdfd833665d6 Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Thu, 25 Aug 2022 09:21:10 +0700 Subject: [PATCH 32/51] fix supportTx --- connection.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/connection.go b/connection.go index 11e6738..ec54f8a 100644 --- a/connection.go +++ b/connection.go @@ -263,8 +263,8 @@ func (c *Connection) RollBack() error { return nil } -func (c *Connection) DisableTx() { - c._disableTx = true +func (c *Connection) DisableTx(disable bool) { + c._disableTx = disable } func (c *Connection) IsTx() bool { From f6b2d2fd80e7f847708b0046ae1f5322943dbe6f Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Thu, 25 Aug 2022 09:34:39 +0700 Subject: [PATCH 33/51] add dep --- connection.go | 1 + 1 file changed, 1 insertion(+) diff --git a/connection.go b/connection.go index ec54f8a..6b38c2a 100644 --- a/connection.go +++ b/connection.go @@ -5,6 +5,7 @@ import ( "fmt" "strings" "time" + "errors" "git.kanosolution.net/kano/dbflex" "github.com/sebarcode/codekit" From 8a3be627f6d71999cf381ca127fcf85c9bbe7fb2 Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Thu, 15 Sep 2022 14:50:44 +0700 Subject: [PATCH 34/51] apply ObjectNames --- connection.go | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/connection.go b/connection.go index 6b38c2a..296b66f 100644 --- a/connection.go +++ b/connection.go @@ -2,10 +2,11 @@ package flexmgo import ( "context" + "crypto/tls" + "errors" "fmt" "strings" "time" - "errors" "git.kanosolution.net/kano/dbflex" "github.com/sebarcode/codekit" @@ -66,6 +67,16 @@ func (c *Connection) Connect() error { opts.SetMaxPoolSize(uint64(poolSize)) } + case "tlsinsecure": + opts.SetTLSConfig(&tls.Config{InsecureSkipVerify: true}) + + case "tlscertificate": + opts.SetTLSConfig(loadCerts(&tls.Config{}, strings.Split(v.(string), ",")...)) + + case "writeconcern": + wConcern := writeconcern.New(writeconcern.WMajority()) + opts.SetWriteConcern(wConcern) + case "idle": idle := codekit.ToInt(v.(string), codekit.RoundingAuto) if idle > 0 { @@ -103,6 +114,10 @@ func (c *Connection) Connect() error { return nil } +func loadCerts(cfg *tls.Config, certFiles ...string) *tls.Config { + return cfg +} + func (c *Connection) Mdb() *mongo.Database { return c.db } @@ -274,8 +289,14 @@ func (c *Connection) IsTx() bool { // SupportTx to identify if underlying connection support Tx or not func (c *Connection) SupportTx() bool { - if (c._disableTx) { - return false + return !c._disableTx +} + +func (c *Connection) ObjectNames(kind dbflex.ObjTypeEnum) []string { + if !(kind == dbflex.ObjTypeAll || kind == dbflex.ObjTypeTable) { + return []string{} } - return true + + names, _ := c.db.ListCollectionNames(context.TODO(), bson.D{}) + return names } From 07c97f2063f021da2788c6138f688516d7cf6138 Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Sat, 17 Sep 2022 22:38:38 +0700 Subject: [PATCH 35/51] fix on cursor more than 2000 rows --- cursor.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cursor.go b/cursor.go index 0ef4990..01fcfcf 100644 --- a/cursor.go +++ b/cursor.go @@ -126,7 +126,7 @@ func (cr *Cursor) Fetchs(result interface{}, n int) dbflex.ICursor { if used == 1000 { used = 0 - newLen := read + 1000 - 1 + newLen := read + 1000 biggerBuffer := reflect.MakeSlice(sliceType, newLen, newLen) reflect.Copy(biggerBuffer, destBuffer) destBuffer = biggerBuffer From 77174474d2a269e732f2db6904b92362c6fa3c8a Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Fri, 30 Sep 2022 05:06:08 +0700 Subject: [PATCH 36/51] add limit on count and text search --- cursor.go | 5 +++-- query.go | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/cursor.go b/cursor.go index 01fcfcf..1bee6c5 100644 --- a/cursor.go +++ b/cursor.go @@ -8,6 +8,7 @@ import ( "git.kanosolution.net/kano/dbflex" "github.com/sebarcode/codekit" "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" ) type Cursor struct { @@ -40,10 +41,10 @@ func (cr *Cursor) Count() int { tableName := cr.countParm.GetString("count") where := cr.countParm.Get("query", nil) if where == nil { - n, _ := cr.conn.db.Collection(tableName).CountDocuments(cr.conn.ctx, codekit.M{}) + n, _ := cr.conn.db.Collection(tableName).EstimatedDocumentCount(cr.conn.ctx) //.CountDocuments(cr.conn.ctx, codekit.M{}) return int(n) } else { - n, _ := cr.conn.db.Collection(tableName).CountDocuments(cr.conn.ctx, where) + n, _ := cr.conn.db.Collection(tableName).CountDocuments(cr.conn.ctx, where, options.Count().SetLimit(10000000)) return int(n) } diff --git a/query.go b/query.go index 9c6f1f4..a897074 100644 --- a/query.go +++ b/query.go @@ -35,6 +35,8 @@ func (q *Query) BuildFilter(f *df.Filter) (interface{}, error) { fm.Set(f.Field, f.Value) } else if f.Op == df.OpNe { fm.Set(f.Field, M{}.Set("$ne", f.Value)) + } else if f.Op == "$text" { + fm.Set("$text", M{}.Set("$search", f.Value)) } else if f.Op == df.OpContains { fs := f.Value.([]interface{}) if len(fs) > 1 { @@ -417,7 +419,7 @@ func (q *Query) Execute(m M) (interface{}, error) { }) return nil, err } else { - return nil, fmt.Errorf("delete need to have where clause. For delete all data in a collection, please use DropTable instead of Delete") + return nil, fmt.Errorf("delete need to have where clause. To delete all data in a collection, please use DropTable instead of Delete") } case df.QuerySave: From e5a9df72724c106f8b98caa9396337ff99b62e48 Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Sat, 8 Oct 2022 05:52:35 +0700 Subject: [PATCH 37/51] fix command --- cursor.go | 2 +- flexmgo_test.go | 6 ++-- go.mod | 2 +- go.sum | 4 +-- query.go | 79 +++++++++++++++++++++++-------------------------- 5 files changed, 44 insertions(+), 49 deletions(-) diff --git a/cursor.go b/cursor.go index 1bee6c5..c6c5006 100644 --- a/cursor.go +++ b/cursor.go @@ -15,7 +15,7 @@ type Cursor struct { dbflex.CursorBase mc *mongo.Cursor - tablename string + //tablename string countParm codekit.M conn *Connection cursor *mongo.Cursor diff --git a/flexmgo_test.go b/flexmgo_test.go index 5f5290b..50c8d92 100644 --- a/flexmgo_test.go +++ b/flexmgo_test.go @@ -401,7 +401,7 @@ func TestGridFsUpdate(t *testing.T) { buff := bytes.NewReader([]byte(data)) reader := bufio.NewReader(buff) - cmd := dbflex.From("fs").Command("GfsWrite") + cmd := dbflex.From("fs").Command("GfsWrite", nil) metadata := codekit.M{}.Set("data", "ini adalah meta") _, err := conn.Execute(cmd, codekit.M{}. Set("id", "doc1"). @@ -413,7 +413,7 @@ func TestGridFsUpdate(t *testing.T) { var buff bytes.Buffer writer := bufio.NewWriter(&buff) - cmd := dbflex.From("fs").Command("gfsread") + cmd := dbflex.From("fs").Command("gfsread", nil) _, err := conn.Execute(cmd, codekit.M{}. Set("id", "doc1"). Set("output", writer)) @@ -421,7 +421,7 @@ func TestGridFsUpdate(t *testing.T) { cv.So(string(data), cv.ShouldEqual, string(buff.Bytes())) cv.Convey("delete grid", func() { - cmd := dbflex.From("fs").Command("gfsdelete") + cmd := dbflex.From("fs").Command("gfsdelete", nil) _, err := conn.Execute(cmd, codekit.M{}.Set("id", "doc1")) cv.So(err, cv.ShouldBeNil) }) diff --git a/go.mod b/go.mod index 9f4c293..1af0d8b 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/ariefdarmawan/flexmgo go 1.16 require ( - git.kanosolution.net/kano/dbflex v1.2.6-0.20220814122711-97eb283c056e + git.kanosolution.net/kano/dbflex v1.2.9 github.com/ariefdarmawan/serde v0.1.1 github.com/sebarcode/codekit v0.1.2 github.com/sebarcode/logger v0.1.1 diff --git a/go.sum b/go.sum index 71e3446..2fca374 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -git.kanosolution.net/kano/dbflex v1.2.6-0.20220814122711-97eb283c056e h1:FkR5b7mqG2O1JTF5oxfKCsXKh3+d21mAmZ/g/Jm9uGM= -git.kanosolution.net/kano/dbflex v1.2.6-0.20220814122711-97eb283c056e/go.mod h1:FQZKWzBpYXNalY8syaXBjVBXyg58hIuUXQuIB08l7Zw= +git.kanosolution.net/kano/dbflex v1.2.9 h1:6lto8XVLWoHMLIATE3ALGcCfkks2kAS+uUp7DyjX9xU= +git.kanosolution.net/kano/dbflex v1.2.9/go.mod h1:FQZKWzBpYXNalY8syaXBjVBXyg58hIuUXQuIB08l7Zw= github.com/ariefdarmawan/reflector v0.0.3 h1:qbfwP5iKMvdYBWRG2jZyoXRLPsGedJ1sNcC8bw+MhpY= github.com/ariefdarmawan/reflector v0.0.3/go.mod h1:xxeY7n6iT0q5pK4j3s8l2xo1CvmQgS2oYtj0lsV4eo4= github.com/ariefdarmawan/serde v0.1.1 h1:TIhTKKFfK4WfYcS/hW1nSD81Uqd1jkY9Xli8e/3h/MA= diff --git a/query.go b/query.go index a897074..0424acb 100644 --- a/query.go +++ b/query.go @@ -14,7 +14,6 @@ import ( "bufio" - "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/gridfs" @@ -191,9 +190,44 @@ func (q *Query) Cursor(m M) df.ICursor { } } } else if hasCommand { + cmdName := commandParts.Op cmdValue := commandParts.Value - switch cmdValue.(type) { - case codekit.M, bson.M: + switch cmdName { + case "aggregate", "aggr", "pipe": + pipes := []codekit.M{} + if hasWhere && len(where) > 0 { + pipes = append(pipes, M{}.Set("$match", where)) + } + + pipeM := cmdValue + var ( + pipeMs []codekit.M + //ok bool + cur *mongo.Cursor + err error + ) + + serde.Serde(pipeM, &pipeMs) + if len(pipeMs) > 0 { + if _, has := pipeMs[0]["$text"]; has { + pipes = pipeMs + } else { + pipes = append(pipes, pipeMs...) + } + } + + //fmt.Println("pipe:", codekit.JsonString(pipes), "\n") + if cur, err = coll.Aggregate(conn.ctx, pipes, new(options.AggregateOptions).SetAllowDiskUse(true)); err != nil { + cursor.SetError(err) + return cursor + } + + cursor.cursor = cur + cursor.conn = conn + cursor.countParm = nil + return cursor + + case "command": var curCommand *mongo.Cursor err := wrapTx(conn, func(ctx mongo.SessionContext) error { var err error @@ -207,43 +241,6 @@ func (q *Query) Cursor(m M) df.ICursor { } return cursor - case string: - switch cmdValue.(string) { - case "aggregate", "pipe": - pipes := []codekit.M{} - if hasWhere && len(where) > 0 { - pipes = append(pipes, M{}.Set("$match", where)) - } - if hasPipe, pipeM := q.Command().HasAttr("pipe"); hasPipe { - var ( - pipeMs []codekit.M - //ok bool - cur *mongo.Cursor - err error - ) - - serde.Serde(pipeM, &pipeMs) - if len(pipeMs) > 0 { - if _, has := pipeMs[0]["$text"]; has { - pipes = pipeMs - } else { - pipes = append(pipes, pipeMs...) - } - } - - //fmt.Println("pipe:", codekit.JsonString(pipes), "\n") - if cur, err = coll.Aggregate(conn.ctx, pipes, new(options.AggregateOptions).SetAllowDiskUse(true)); err != nil { - cursor.SetError(err) - return cursor - } - - cursor.cursor = cur - cursor.conn = conn - cursor.countParm = nil - return cursor - } - } - default: cursor.SetError(fmt.Errorf("invalid command %v", cmdValue)) return cursor @@ -263,8 +260,6 @@ func (q *Query) Cursor(m M) df.ICursor { cursor.mgoiter = coll.Pipe(pipe).AllowDiskUse().Iter() */ - cursor.SetError(fmt.Errorf("pipe and command is not yet applied")) - return cursor } else { opt := options.Find() if items, ok := parts[df.QuerySelect]; ok { From 2d2ce415d1b46be2ffb69106451ee97d0f55db00 Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Sat, 8 Oct 2022 06:01:47 +0700 Subject: [PATCH 38/51] Allowing alias for cmd.Select(fields ...string) Now we can pass alias as one of the field using format _alias:field_ ie: cmd.Select("Count:$LongFieldNameThatHenceNeedAlias") Receiever cursor needs to have this field ready or we can use map as its receiver --- query.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/query.go b/query.go index 0424acb..770320f 100644 --- a/query.go +++ b/query.go @@ -267,7 +267,16 @@ func (q *Query) Cursor(m M) df.ICursor { if len(fields) > 0 { projection := codekit.M{} for _, field := range fields { - projection.Set(field, 1) + if strings.Contains(field, ":") { + alias := strings.Split(field, ":") + if len(alias) <= 1 { + cursor.SetError(errors.New("error on translating projection field " + field)) + return cursor + } + projection.Set(alias[0], alias[1]) + } else { + projection.Set(field, 1) + } } opt.SetProjection(projection) } From 62d7b5dd3bbf700c5f81f80598ea00a7d01564a1 Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Fri, 30 Dec 2022 09:28:18 +0700 Subject: [PATCH 39/51] update connection setting working with different authSource --- connection.go | 26 +++++++++++++++----------- flexmgo_test.go | 2 +- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/connection.go b/connection.go index 296b66f..130e53b 100644 --- a/connection.go +++ b/connection.go @@ -35,21 +35,25 @@ func (c *Connection) Connect() error { } connURI := "mongodb://" - connURI += c.Host + "/" - connURI += configString - opts := options.Client().ApplyURI(connURI) - //opts.SetConnectTimeout(5 * time.Second) - //opts.SetSocketTimeout(3 * time.Second) - //opts.SetServerSelectionTimeout(3 * time.Second) if c.User != "" { - opts.SetAuth(options.Credential{ - Username: c.User, - Password: c.Password, - AuthSource: "admin", - }) + authPrefix := c.User + ":" + c.Password + connURI += authPrefix + "@" } + connURI += c.Host + "/" + connURI += configString + + /* + if c.User != "" { + opts.SetAuth(options.Credential{ + Username: c.User, + Password: c.Password, + AuthSource: authSource, + }) + } + */ + opts := options.Client().ApplyURI(connURI) for k, v := range c.Config { klow := strings.ToLower(k) switch klow { diff --git a/flexmgo_test.go b/flexmgo_test.go index 50c8d92..e6ec194 100644 --- a/flexmgo_test.go +++ b/flexmgo_test.go @@ -43,7 +43,7 @@ func TestConnect(t *testing.T) { }) } -var tablename = "testrecord" +var tablename = "test_arief" func TestConnectFail(t *testing.T) { cv.Convey("connect", t, func() { From a5a15b5decea128b5527ecda71b62d3da56b8861 Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Fri, 30 Dec 2022 09:55:24 +0700 Subject: [PATCH 40/51] authmechanism update --- connection.go | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/connection.go b/connection.go index 130e53b..b2f7a9d 100644 --- a/connection.go +++ b/connection.go @@ -31,6 +31,10 @@ type Connection struct { func (c *Connection) Connect() error { configString := "?" for k, v := range c.Config { + if strings.ToLower(k) == "authmechanism" && strings.ToLower(v.(string)) == "default" { + configString += k + "=SCRAM-SHA-256&" + continue + } configString += k + "=" + v.(string) + "&" } @@ -43,16 +47,6 @@ func (c *Connection) Connect() error { connURI += c.Host + "/" connURI += configString - /* - if c.User != "" { - opts.SetAuth(options.Credential{ - Username: c.User, - Password: c.Password, - AuthSource: authSource, - }) - } - */ - opts := options.Client().ApplyURI(connURI) for k, v := range c.Config { klow := strings.ToLower(k) From 71617fb9962e96745598c5df7098c23637df015e Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Fri, 7 Apr 2023 04:50:14 +0700 Subject: [PATCH 41/51] fix command cursor by adding connection --- flexmgo_test.go | 23 +++++++++++++++++++++++ query.go | 2 ++ 2 files changed, 25 insertions(+) diff --git a/flexmgo_test.go b/flexmgo_test.go index e6ec194..4e1ce30 100644 --- a/flexmgo_test.go +++ b/flexmgo_test.go @@ -474,6 +474,29 @@ func TestCheckIndex(t *testing.T) { }) } +func TestSelectCommand(t *testing.T) { + convey.Convey("select command", t, func() { + convey.Convey("connect", func() { + c, e := connect() + convey.So(e, convey.ShouldBeNil) + defer c.Close() + + convey.Convey("select", func() { + cmd := dbflex.From("mytable").Command("command", codekit.M{"find": "mytable"}) + cur := c.Cursor(cmd, nil) + convey.So(cur.Error(), convey.ShouldBeNil) + defer cur.Close() + + res := []codekit.M{} + cur.Fetchs(&res, 0) + convey.So(cur.Error(), convey.ShouldBeNil) + convey.Printf("records returned: %d\n", len(res)) + convey.Printf("serialized: %s\n", res) + }) + }) + }) +} + /* TO DO - Command Cursor diff --git a/query.go b/query.go index 770320f..2c589ba 100644 --- a/query.go +++ b/query.go @@ -238,6 +238,8 @@ func (q *Query) Cursor(m M) df.ICursor { cursor.SetError(err) } else { cursor.cursor = curCommand + cursor.conn = conn + cursor.countParm = nil } return cursor From 2293e9f86fcc5524070d179d68789a15cdd42e97 Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Tue, 16 May 2023 17:05:00 +0700 Subject: [PATCH 42/51] fix startswith --- query.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/query.go b/query.go index 2c589ba..f90099f 100644 --- a/query.go +++ b/query.go @@ -55,11 +55,11 @@ func (q *Query) BuildFilter(f *df.Filter) (interface{}, error) { } } else if f.Op == df.OpStartWith { fm.Set(f.Field, M{}. - Set("$regex", fmt.Sprintf("^%s.*$", f.Value)). + Set("$regex", fmt.Sprintf("^%s", f.Value)). Set("$options", "i")) } else if f.Op == df.OpEndWith { fm.Set(f.Field, M{}. - Set("$regex", fmt.Sprintf("^.*%s$", f.Value)). + Set("$regex", fmt.Sprintf("%s$", f.Value)). Set("$options", "i")) } else if f.Op == df.OpIn { fm.Set(f.Field, M{}.Set("$in", f.Value)) From 818a651b6240a1606c6cf864e37de9a2e58bed88 Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Sat, 20 May 2023 06:17:10 +0700 Subject: [PATCH 43/51] fix sort --- query.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/query.go b/query.go index 2c589ba..c44fec6 100644 --- a/query.go +++ b/query.go @@ -287,12 +287,12 @@ func (q *Query) Cursor(m M) df.ICursor { if items, ok := parts[df.QueryOrder]; ok { sortKeys := items.Value.([]string) - sortM := codekit.M{} + sortM := []codekit.M{} for _, key := range sortKeys { if key[0] == '-' { - sortM.Set(key[1:], -1) + sortM = append(sortM, codekit.M{key: -1}) } else { - sortM.Set(key, 1) + sortM = append(sortM, codekit.M{key: 1}) } } From 2d277742b5cfbe98ccd66c587e15e598db5b6ae2 Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Sat, 20 May 2023 06:24:23 +0700 Subject: [PATCH 44/51] sort change from M to bson.D --- query.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/query.go b/query.go index bfd1267..e71dd01 100644 --- a/query.go +++ b/query.go @@ -14,6 +14,7 @@ import ( "bufio" + "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/gridfs" @@ -287,17 +288,17 @@ func (q *Query) Cursor(m M) df.ICursor { if items, ok := parts[df.QueryOrder]; ok { sortKeys := items.Value.([]string) - sortM := []codekit.M{} + sortDoc := bson.D{} for _, key := range sortKeys { if key[0] == '-' { - sortM = append(sortM, codekit.M{key: -1}) + sortDoc = append(sortDoc, bson.E{key, -1}) } else { - sortM = append(sortM, codekit.M{key: 1}) + sortDoc = append(sortDoc, bson.E{key, 1}) } } - if len(sortM) > 0 { - opt.SetSort(sortM) + if len(sortDoc) > 0 { + opt.SetSort(sortDoc) } } From 49587f318524c757bf49b9e3c793efa9e9ab3a17 Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Sat, 20 May 2023 06:44:20 +0700 Subject: [PATCH 45/51] fix sort multi key --- query.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/query.go b/query.go index e71dd01..9edd37e 100644 --- a/query.go +++ b/query.go @@ -291,7 +291,7 @@ func (q *Query) Cursor(m M) df.ICursor { sortDoc := bson.D{} for _, key := range sortKeys { if key[0] == '-' { - sortDoc = append(sortDoc, bson.E{key, -1}) + sortDoc = append(sortDoc, bson.E{key[1:], -1}) } else { sortDoc = append(sortDoc, bson.E{key, 1}) } From 90c3585461996ef5748419a2d29eb8a1a7bf27dc Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Sun, 11 Jun 2023 19:50:58 +0700 Subject: [PATCH 46/51] split command handling and add gfs --- command_handler.go | 154 ++++++++++++++++++++++++++++++++ flexmgo_test.go | 71 ++++----------- query.go | 218 ++------------------------------------------- 3 files changed, 176 insertions(+), 267 deletions(-) create mode 100644 command_handler.go diff --git a/command_handler.go b/command_handler.go new file mode 100644 index 0000000..ad4f121 --- /dev/null +++ b/command_handler.go @@ -0,0 +1,154 @@ +package flexmgo + +import ( + "bufio" + "errors" + "fmt" + "io" + "strings" + + df "git.kanosolution.net/kano/dbflex" + "github.com/sebarcode/codekit" + "go.mongodb.org/mongo-driver/bson/primitive" + "go.mongodb.org/mongo-driver/mongo/gridfs" + "go.mongodb.org/mongo-driver/mongo/options" +) + +func (q *Query) handleExecuteCommand(conn *Connection) (interface{}, error) { + tablename := q.Config(df.ConfigKeyTableName, "").(string) + coll := conn.db.Collection(tablename) + parts := q.Config(df.ConfigKeyGroupedQueryItems, df.QueryItems{}).(df.QueryItems) + + commands, ok := parts[df.QueryCommand] + if !ok { + return nil, fmt.Errorf("no command") + } + + where := q.Config(df.ConfigKeyWhere, codekit.M{}).(codekit.M) + cmdName := commands.Op + cmdValue := commands.Value + + var ( + bucket *gridfs.Bucket + gfsBuffSize int32 + err error + cmdParm codekit.M + mOK bool + ) + if strings.ToLower(cmdName)[:3] == "gfs" { + gfsBuffSize = int32(cmdParm.Get("size", 1024).(int)) + bucketOpt := new(options.BucketOptions) + bucketOpt.SetChunkSizeBytes(gfsBuffSize) + bucketOpt.SetName(tablename) + bucket, err = gridfs.NewBucket(conn.db, bucketOpt) + if err != nil { + return nil, fmt.Errorf("error prepare GridFS bucket. %s", err.Error()) + } + + cmdParm, mOK = cmdValue.(codekit.M) + if !mOK { + cmdParm = codekit.M{} + } + } + + switch cmdName { + case "gfswrite": + var reader io.Reader + gfsId, hasId := cmdParm["id"] + gfsMetadata, hasMetadata := cmdParm["metadata"] + gfsFileName := cmdParm.GetString("name") + reader, readerOK := cmdParm.Get("source", nil).(io.Reader) + if !readerOK { + return nil, fmt.Errorf("invalid reader") + } + + //-- check if file exist, delete if already exist + if hasId { + bucket.Delete(gfsId) + } + + if !hasMetadata { + gfsMetadata = codekit.M{} + } + uploadOpt := new(options.UploadOptions) + uploadOpt.SetMetadata(gfsMetadata) + if gfsFileName == "" && hasId { + gfsFileName = gfsId.(string) + } + if gfsFileName == "" { + gfsFileName = codekit.RandomString(32) + } + + var objId primitive.ObjectID + if hasId { + err = bucket.UploadFromStreamWithID(gfsId, gfsFileName, reader, uploadOpt) + } else { + objId, err = bucket.UploadFromStream(gfsFileName, reader, uploadOpt) + } + if err != nil { + return nil, fmt.Errorf("error upload file to GridFS. %s", err.Error()) + } + return objId, nil + + case "gfsread": + gfsId, hasId := cmdParm["id"] + gfsFileName := cmdParm.GetString("name") + if gfsFileName == "" && hasId { + gfsFileName = gfsId.(string) + } + dest := cmdParm.Get("output", &bufio.Writer{}).(io.Writer) + var err error + + var ds *gridfs.DownloadStream + if hasId { + ds, err = bucket.OpenDownloadStream(gfsId) + } else { + ds, err = bucket.OpenDownloadStreamByName(gfsFileName) + } + defer ds.Close() + + if err != nil { + return nil, fmt.Errorf("unable to open GFS %s-%s. %s", tablename, gfsFileName, err.Error()) + } + defer ds.Close() + + io.Copy(dest, ds) + return nil, nil + + case "gfsremove", "gfsdelete": + gfsId, hasId := cmdParm["id"] + var err error + if hasId && gfsId != "" { + err = bucket.Delete(gfsId) + } + return nil, err + + case "gfstruncate": + err := bucket.Drop() + return nil, err + + case "distinct": + fieldName := "" + switch cmdValue := cmdValue.(type) { + case string: + fieldName = cmdValue + + case codekit.M: + fieldName = cmdValue.GetString("field") + if fieldName == "" { + return nil, errors.New("field attribute is mandatory") + } + + default: + return nil, errors.New("distinct only accepts string or codekit.M") + } + vs, err := coll.Distinct(conn.ctx, fieldName, where) + if err != nil { + return nil, err + } + return vs, nil + + default: + return nil, fmt.Errorf("invalid command: %v", cmdName) + } +} diff --git a/flexmgo_test.go b/flexmgo_test.go index 4e1ce30..19aead3 100644 --- a/flexmgo_test.go +++ b/flexmgo_test.go @@ -289,38 +289,6 @@ func TestMdbTrx(t *testing.T) { }) } -/* -func TestWatch(t *testing.T) { - cv.Convey("change stream", t, func() { - conn, err := connect() - cv.So(err, cv.ShouldBeNil) - defer conn.Close() - - changed := make(chan codekit.M) - cmd := dbflex.From(tablename).Command("watch") - _, err = conn.Execute(cmd, codekit.M{}. - Set("fn", func(data codekit.M) { - changed <- data - close(changed) - })) - cv.So(err, cv.ShouldBeNil) - - cv.Convey("validate", func() { - m := codekit.M{} - cmdGet := dbflex.From(tablename).Select().Where(dbflex.Eq("_id", "record-id-5")) - err := conn.Cursor(cmdGet, nil).SetCloseAfterFetch().Fetch(&m) - cv.So(err, cv.ShouldBeNil) - - m.Set("title", "Test Change Stream") - _, err = conn.Execute(dbflex.From(tablename).Save(), codekit.M{}.Set("data", m)) - - changedData := <-changed - cv.So(changedData.GetString("title"), cv.ShouldEqual, "Test Change Stream") - }) - }) -} -*/ - func TestDeleteData(t *testing.T) { cv.Convey("connect", t, func() { conn, err := connect() @@ -401,28 +369,36 @@ func TestGridFsUpdate(t *testing.T) { buff := bytes.NewReader([]byte(data)) reader := bufio.NewReader(buff) - cmd := dbflex.From("fs").Command("GfsWrite", nil) - metadata := codekit.M{}.Set("data", "ini adalah meta") - _, err := conn.Execute(cmd, codekit.M{}. + metadata1 := codekit.M{}.Set("data", "ini adalah meta1") + cmd := dbflex.From("fs").Command("gfswrite", codekit.M{}. Set("id", "doc1"). - Set("metadata", metadata). + Set("metadata", metadata1). Set("source", reader)) + _, err := conn.Execute(cmd, nil) + cv.So(err, cv.ShouldBeNil) + + metadata2 := codekit.M{}.Set("data", "ini adalah meta2") + cmd = dbflex.From("fs").Command("gfswrite", codekit.M{}. + Set("id", "doc2"). + Set("metadata", metadata2). + Set("source", reader)) + _, err = conn.Execute(cmd, nil) cv.So(err, cv.ShouldBeNil) cv.Convey("reading from grid", func() { var buff bytes.Buffer writer := bufio.NewWriter(&buff) - cmd := dbflex.From("fs").Command("gfsread", nil) - _, err := conn.Execute(cmd, codekit.M{}. + cmd := dbflex.From("fs").Command("gfsread", codekit.M{}. Set("id", "doc1"). Set("output", writer)) + _, err := conn.Execute(cmd, nil) cv.So(err, cv.ShouldBeNil) cv.So(string(data), cv.ShouldEqual, string(buff.Bytes())) cv.Convey("delete grid", func() { - cmd := dbflex.From("fs").Command("gfsdelete", nil) - _, err := conn.Execute(cmd, codekit.M{}.Set("id", "doc1")) + cmd := dbflex.From("fs").Command("gfsdelete", codekit.M{}.Set("id", "doc1")) + _, err := conn.Execute(cmd, nil) cv.So(err, cv.ShouldBeNil) }) }) @@ -550,18 +526,3 @@ func (r *Record) GetID() ([]string, []interface{}) { func (r *Record) SetID(obj []interface{}) { r.ID = obj[0].(string) } - -/* -var demoConfig = { - _id: "rs", - members: [ - { _id: 0, - host: 'localhost:27017', - priority: 10 - }, - { _id: 1, - host: 'localhost:27018' - } - ] - }; -*/ diff --git a/query.go b/query.go index 9edd37e..513bdad 100644 --- a/query.go +++ b/query.go @@ -3,7 +3,6 @@ package flexmgo import ( "errors" "fmt" - "io" "strings" "git.kanosolution.net/kano/dbflex" @@ -12,12 +11,8 @@ import ( "github.com/sebarcode/codekit" . "github.com/sebarcode/codekit" - "bufio" - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" - "go.mongodb.org/mongo-driver/mongo/gridfs" "go.mongodb.org/mongo-driver/mongo/options" ) @@ -135,9 +130,9 @@ func (q *Query) Cursor(m M) df.ICursor { aggrs, hasAggr := parts[df.QueryAggr] groupby, hasGroup := parts[df.QueryGroup] - //commandParts, hasCommand := parts[df.QueryCommand] commandParts, hasCommand := parts[df.QueryCommand] + // run simple aggregation if hasAggr { pipes := []M{} items := aggrs.Value.([]*df.AggrItem) @@ -190,6 +185,8 @@ func (q *Query) Cursor(m M) df.ICursor { Set("query", where) } } + + // run a specific command } else if hasCommand { cmdName := commandParts.Op cmdValue := commandParts.Value @@ -217,7 +214,6 @@ func (q *Query) Cursor(m M) df.ICursor { } } - //fmt.Println("pipe:", codekit.JsonString(pipes), "\n") if cur, err = coll.Aggregate(conn.ctx, pipes, new(options.AggregateOptions).SetAllowDiskUse(true)); err != nil { cursor.SetError(err) return cursor @@ -245,24 +241,11 @@ func (q *Query) Cursor(m M) df.ICursor { return cursor default: - cursor.SetError(fmt.Errorf("invalid command %v", cmdValue)) + cursor.SetError(fmt.Errorf("invalid command %v", cmdName)) return cursor } - /* - case "gfsfind": - b, err := gridfs.NewBucket(conn.db) - qry := q.db.GridFS(tablename).Find(where) - cursor.mgocursor = qry - cursor.mgoiter = qry.Iter() - case "pipe": - pipe, ok := m["pipe"] - if !ok { - cursor.SetError(fmt.Errorf("invalid command, calling pipe without pipe data")) - return cursor - } - cursor.mgoiter = coll.Pipe(pipe).AllowDiskUse().Iter() - */ + // basic find } else { opt := options.Find() if items, ok := parts[df.QuerySelect]; ok { @@ -449,148 +432,13 @@ func (q *Query) Execute(m M) (interface{}, error) { return nil, err case df.QueryCommand: - commands, ok := parts[df.QueryCommand] - if !ok { - return nil, fmt.Errorf("No command") - } - - //mCommand := commands.Value.(codekit.M) - //cmd, _ := - cmdValue := commands.Value - switch cmdValue.(type) { - case string: - commandTxt := cmdValue.(string) - if commandTxt == "" { - return nil, fmt.Errorf("No command") - } - - var ( - bucket *gridfs.Bucket - gfsBuffSize int32 - err error - ) - if strings.ToLower(commandTxt)[:3] == "gfs" { - gfsBuffSize = int32(m.Get("size", 1024).(int)) - bucketOpt := new(options.BucketOptions) - bucketOpt.SetChunkSizeBytes(gfsBuffSize) - bucketOpt.SetName(tablename) - bucket, err = gridfs.NewBucket(conn.db, bucketOpt) - if err != nil { - return nil, fmt.Errorf("error prepare GridFS bucket. %s", err.Error()) - } - } - - if hasParm, parm := q.Command().HasAttr("CommandParm"); hasParm { - m = parm.(codekit.M) - } - switch strings.ToLower(commandTxt) { - case "gfswrite": - var reader io.Reader - gfsId, hasId := m["id"] - gfsMetadata, hasMetadata := m["metadata"] - gfsFileName := m.GetString("name") - reader = m.Get("source", nil).(io.Reader) - if reader == nil { - return nil, fmt.Errorf("invalid reader") - } - - //-- check if file exist, delete if already exist - if hasId { - bucket.Delete(gfsId) - } - - if !hasMetadata { - gfsMetadata = codekit.M{} - } - uploadOpt := new(options.UploadOptions) - uploadOpt.SetMetadata(gfsMetadata) - if gfsFileName == "" && hasId { - gfsFileName = gfsId.(string) - } - if gfsFileName == "" { - gfsFileName = codekit.RandomString(32) - } - - var objId primitive.ObjectID - if hasId { - err = bucket.UploadFromStreamWithID(gfsId, gfsFileName, reader, uploadOpt) - } else { - objId, err = bucket.UploadFromStream(gfsFileName, reader, uploadOpt) - } - if err != nil { - return nil, fmt.Errorf("error upload file to GridFS. %s", err.Error()) - } - return objId, nil - - case "gfsread": - gfsId, hasId := m["id"] - gfsFileName := m.GetString("name") - if gfsFileName == "" && hasId { - gfsFileName = gfsId.(string) - } - dest := m.Get("output", &bufio.Writer{}).(io.Writer) - var err error - - var ds *gridfs.DownloadStream - if hasId { - ds, err = bucket.OpenDownloadStream(gfsId) - } else { - ds, err = bucket.OpenDownloadStreamByName(gfsFileName) - } - defer ds.Close() - - if err != nil { - return nil, fmt.Errorf("unable to open GFS %s-%s. %s", tablename, gfsFileName, err.Error()) - } - defer ds.Close() - - io.Copy(dest, ds) - return nil, nil - - case "gfsremove", "gfsdelete": - gfsId, hasId := m["id"] - var err error - if hasId && gfsId != "" { - err = bucket.Delete(gfsId) - } - return nil, err - - case "gfstruncate": - err := bucket.Drop() - return nil, err - - case "distinct": - fieldName := m.GetString("field") - vs, err := coll.Distinct(conn.ctx, fieldName, where) - if err != nil { - return nil, err - } - return vs, nil - - default: - return nil, fmt.Errorf("Invalid command: %v", commandTxt) - } - - case codekit.M: - cmdM := cmdValue.(codekit.M) - sr := conn.db.RunCommand(conn.ctx, cmdM) - if sr.Err() != nil { - return nil, fmt.Errorf("unablet to run command. %s. Command: %s", - sr.Err().Error(), codekit.JsonString(cmdM)) - } - return sr, nil - - default: - return nil, fmt.Errorf("Unknown command %v", cmdValue) - } - + return q.handleExecuteCommand(conn) } return nil, nil } func wrapTx(conn *Connection, fn func(ctx mongo.SessionContext) error) error { var err error - //fmt.Println("Connection in tx", conn.IsTx(), " sess", conn.sess) if conn.sess != nil { err = mongo.WithSession(conn.ctx, conn.sess, func(sc mongo.SessionContext) error { return fn(sc) @@ -600,57 +448,3 @@ func wrapTx(conn *Connection, fn func(ctx mongo.SessionContext) error) error { } return err } - -/* -func (q *Query) SetThis(q dbflex.IQuery) { - panic("not implemented") -} - -func (q *Query) This() dbflex.IQuery { - panic("not implemented") -} - -func (q *Query) BuildFilter(*dbflex.Filter) (interface{}, error) { - panic("not implemented") -} - -func (q *Query) BuildCommand() (interface{}, error) { - panic("not implemented") -} - -func (q *Query) Cursor(codekit.M) dbflex.ICursor { - panic("not implemented") -} - -func (q *Query) Execute(codekit.M) (interface{}, error) { - panic("not implemented") -} - -func (q *Query) SetConfig(string, interface{}) { - panic("not implemented") -} - -func (q *Query) SetConfigM(codekit.M) { - panic("not implemented") -} - -func (q *Query) Config(string, interface{}) interface{} { - panic("not implemented") -} - -func (q *Query) ConfigRef(string, interface{}, interface{}) { - panic("not implemented") -} - -func (q *Query) DeleteConfig(...string) { - panic("not implemented") -} - -func (q *Query) Connection() dbflex.IConnection { - panic("not implemented") -} - -func (q *Query) SetConnection(dbflex.IConnection) { - panic("not implemented") -} -*/ From 20cdd614d2d4840f8bcbbe96de9cdaf3507f84fd Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Sun, 5 Nov 2023 07:40:40 +0700 Subject: [PATCH 47/51] add atlas support --- atlas_test.go | 33 ++++++++++++++ command_handler.go | 3 ++ connection.go | 105 +++++++++++++++++++++++---------------------- flexmgo.go | 7 +++ go.mod | 6 ++- go.sum | 48 +++++++++++++-------- 6 files changed, 130 insertions(+), 72 deletions(-) create mode 100644 atlas_test.go diff --git a/atlas_test.go b/atlas_test.go new file mode 100644 index 0000000..5560b7f --- /dev/null +++ b/atlas_test.go @@ -0,0 +1,33 @@ +package flexmgo_test + +import ( + "testing" + + "git.kanosolution.net/kano/dbflex" + "github.com/sebarcode/codekit" + "github.com/smartystreets/goconvey/convey" +) + +func TestAtlas(t *testing.T) { + convey.Convey("prepare db", t, func() { + connTxt := "mongodb+srv://readonly:Password.1@cluster0.lobvo.mongodb.net/appdb?retryWrites=true&w=majority" + conn, err := dbflex.NewConnectionFromURI(connTxt, nil) + convey.So(err, convey.ShouldBeNil) + + convey.Convey("connect", func() { + err = conn.Connect() + convey.So(err, convey.ShouldBeNil) + defer conn.Close() + conn.SetFieldNameTag("json") + + convey.Convey("read data", func() { + dest := codekit.M{} + err = conn.Cursor(dbflex.From("info").Select().Take(1), nil).Fetch(&dest).Error() + convey.So(err, convey.ShouldBeNil) + convey.So(dest.GetInt("Version"), convey.ShouldNotEqual, 0) + convey.Println() + convey.Println(codekit.JsonString(dest)) + }) + }) + }) +} diff --git a/command_handler.go b/command_handler.go index ad4f121..1e3e842 100644 --- a/command_handler.go +++ b/command_handler.go @@ -57,6 +57,9 @@ func (q *Query) handleExecuteCommand(conn *Connection) (interface{}, error) { gfsId, hasId := cmdParm["id"] gfsMetadata, hasMetadata := cmdParm["metadata"] gfsFileName := cmdParm.GetString("name") + if gfsFileName == "" { + gfsFileName = codekit.RandomString(32) + } reader, readerOK := cmdParm.Get("source", nil).(io.Reader) if !readerOK { return nil, fmt.Errorf("invalid reader") diff --git a/connection.go b/connection.go index b2f7a9d..2e5d03b 100644 --- a/connection.go +++ b/connection.go @@ -29,73 +29,74 @@ type Connection struct { } func (c *Connection) Connect() error { - configString := "?" - for k, v := range c.Config { - if strings.ToLower(k) == "authmechanism" && strings.ToLower(v.(string)) == "default" { - configString += k + "=SCRAM-SHA-256&" - continue + var opts *options.ClientOptions + + if c.ServerInfo.ConnectionString == "" { + configString := "?" + for k, v := range c.Config { + if strings.ToLower(k) == "authmechanism" && strings.ToLower(v.(string)) == "default" { + configString += k + "=SCRAM-SHA-256&" + continue + } + configString += k + "=" + v.(string) + "&" } - configString += k + "=" + v.(string) + "&" - } - connURI := "mongodb://" + connURI := "mongodb://" - if c.User != "" { - authPrefix := c.User + ":" + c.Password - connURI += authPrefix + "@" - } - connURI += c.Host + "/" - connURI += configString - - opts := options.Client().ApplyURI(connURI) - for k, v := range c.Config { - klow := strings.ToLower(k) - switch klow { - case "serverselectiontimeout": - opts.SetServerSelectionTimeout( - time.Duration(codekit.ToInt(v, codekit.RoundingAuto)) * time.Millisecond) - - case "replicaset": - opts.SetReplicaSet(v.(string)) - //opts.SetWriteConcern() - - case "poolsize": - poolSize := codekit.ToInt(v.(string), codekit.RoundingAuto) - if poolSize > 0 { - opts.SetMaxPoolSize(uint64(poolSize)) - } + if c.User != "" { + authPrefix := c.User + ":" + c.Password + connURI += authPrefix + "@" + } + connURI += c.Host + "/" + connURI += configString + + opts = options.Client().ApplyURI(connURI) + for k, v := range c.Config { + klow := strings.ToLower(k) + switch klow { + case "serverselectiontimeout": + opts.SetServerSelectionTimeout( + time.Duration(codekit.ToInt(v, codekit.RoundingAuto)) * time.Millisecond) + + case "replicaset": + opts.SetReplicaSet(v.(string)) + //opts.SetWriteConcern() + + case "poolsize": + poolSize := codekit.ToInt(v.(string), codekit.RoundingAuto) + if poolSize > 0 { + opts.SetMaxPoolSize(uint64(poolSize)) + } - case "tlsinsecure": - opts.SetTLSConfig(&tls.Config{InsecureSkipVerify: true}) + case "tlsinsecure": + opts.SetTLSConfig(&tls.Config{InsecureSkipVerify: true}) - case "tlscertificate": - opts.SetTLSConfig(loadCerts(&tls.Config{}, strings.Split(v.(string), ",")...)) + case "tlscertificate": + opts.SetTLSConfig(loadCerts(&tls.Config{}, strings.Split(v.(string), ",")...)) - case "writeconcern": - wConcern := writeconcern.New(writeconcern.WMajority()) - opts.SetWriteConcern(wConcern) + case "writeconcern": + wConcern := writeconcern.New(writeconcern.WMajority()) + opts.SetWriteConcern(wConcern) - case "idle": - idle := codekit.ToInt(v.(string), codekit.RoundingAuto) - if idle > 0 { - opts.SetMaxConnIdleTime(time.Duration(idle) * time.Second) + case "idle": + idle := codekit.ToInt(v.(string), codekit.RoundingAuto) + if idle > 0 { + opts.SetMaxConnIdleTime(time.Duration(idle) * time.Second) + } } } + } else { + opts = options.Client().ApplyURI(c.ServerInfo.ConnectionString) } - //logger.Logger().Debugf("opts: %s", codekit.JsonString(opts)) - client, err := mongo.NewClient(opts) - if err != nil { - return err - } - - //logger.Logger().Debug("client generated: OK") + // logger.Logger().Debug("client generated: OK") if c.ctx == nil { c.ctx = context.TODO() } - //logger.Logger().Debug("context generated: OK") - if err = client.Connect(c.ctx); err != nil { + //logger.Logger().Debugf("opts: %s", codekit.JsonString(opts)) + client, err := mongo.Connect(c.ctx, opts) + if err != nil { return err } diff --git a/flexmgo.go b/flexmgo.go index e94b6b8..8b88c51 100644 --- a/flexmgo.go +++ b/flexmgo.go @@ -9,4 +9,11 @@ func init() { c.SetThis(c) return c }) + + dbflex.RegisterDriver("mongodb+srv", func(si *dbflex.ServerInfo) dbflex.IConnection { + c := new(Connection) + c.ServerInfo = *si + c.SetThis(c) + return c + }) } diff --git a/go.mod b/go.mod index 1af0d8b..bdd30fd 100644 --- a/go.mod +++ b/go.mod @@ -3,10 +3,12 @@ module github.com/ariefdarmawan/flexmgo go 1.16 require ( - git.kanosolution.net/kano/dbflex v1.2.9 + git.kanosolution.net/kano/dbflex v1.3.4-0.20231105001524-58de8c516292 github.com/ariefdarmawan/serde v0.1.1 github.com/sebarcode/codekit v0.1.2 github.com/sebarcode/logger v0.1.1 github.com/smartystreets/goconvey v1.7.2 - go.mongodb.org/mongo-driver v1.10.1 + github.com/stretchr/testify v1.6.1 // indirect + go.mongodb.org/mongo-driver v1.12.1 + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 2fca374..01aa59b 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ git.kanosolution.net/kano/dbflex v1.2.9 h1:6lto8XVLWoHMLIATE3ALGcCfkks2kAS+uUp7DyjX9xU= git.kanosolution.net/kano/dbflex v1.2.9/go.mod h1:FQZKWzBpYXNalY8syaXBjVBXyg58hIuUXQuIB08l7Zw= +git.kanosolution.net/kano/dbflex v1.3.4-0.20231105001524-58de8c516292 h1:4ai+Jd7TO02dMvMJEDtFyPoFwJ4i1DRbpTAXfAWRVwc= +git.kanosolution.net/kano/dbflex v1.3.4-0.20231105001524-58de8c516292/go.mod h1:mqgrdJe4IIfuLOMf1qj5kJUkL2ckiQ3XcA8LFQfE9k4= github.com/ariefdarmawan/reflector v0.0.3 h1:qbfwP5iKMvdYBWRG2jZyoXRLPsGedJ1sNcC8bw+MhpY= github.com/ariefdarmawan/reflector v0.0.3/go.mod h1:xxeY7n6iT0q5pK4j3s8l2xo1CvmQgS2oYtj0lsV4eo4= github.com/ariefdarmawan/serde v0.1.1 h1:TIhTKKFfK4WfYcS/hW1nSD81Uqd1jkY9Xli8e/3h/MA= @@ -14,19 +16,16 @@ github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4= github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc= github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= -github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= -github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe h1:iruDEfMl2E6fbMZ9s0scYfZQ84/6SPL6zC8ACM2oIL0= github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc= -github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= -github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/sebarcode/codekit v0.1.0/go.mod h1:o54sVKGC7+M1leMkKhVXssOXwuHCylW5GJto0UiQJEg= @@ -45,44 +44,57 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/theckman/go-flock v0.8.1/go.mod h1:kjuth3y9VJ2aNlkNEO99G/8lp9fMIKaGyBmh84IBheM= -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-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c= github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI= -github.com/xdg-go/scram v1.1.1 h1:VOMT+81stJgXW3CpHyqHN3AXDYIMsx56mEFrB37Mb/E= -github.com/xdg-go/scram v1.1.1/go.mod h1:RaEWvsqvNKKvBPvcKeFjrG2cJqOkHTiyTpzz23ni57g= -github.com/xdg-go/stringprep v1.0.3 h1:kdwGpVNwPFtjs98xCGkHjQtGKh86rDcRZN17QEMCOIs= -github.com/xdg-go/stringprep v1.0.3/go.mod h1:W3f5j4i+9rC0kuIEJL0ky1VpHXQU3ocBgklLGvcBnW8= +github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY= +github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4= +github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6c8= +github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM= github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d h1:splanxYIlg+5LfHAM6xpdFEAYOk8iySO56hMFq6uLyA= github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA= -go.mongodb.org/mongo-driver v1.10.1 h1:NujsPveKwHaWuKUer/ceo9DzEe7HIj1SlJ6uvXZG0S4= -go.mongodb.org/mongo-driver v1.10.1/go.mod h1:z4XpeoU6w+9Vht+jAFyLgVrD+jGSQQe0+CBWFHNiHt8= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +go.mongodb.org/mongo-driver v1.12.1 h1:nLkghSU8fQNaK7oUmDhQFsnrtcoNy7Z6LVFKsEecqgE= +go.mongodb.org/mongo-driver v1.12.1/go.mod h1:/rGBTebI3XYboVmgz+Wv3Bcbl3aD0QF9zl6kDDw18rQ= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d h1:sK3txAijHtOK88l68nt020reeT1ZdKLIYetKl95FzVY= golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 h1:CIJ76btIcR3eFI5EgSo6k1qKw9KJexJuRLI9G7Hp5wE= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= -golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b h1:PxfKdU9lEEDYjdIzOtC4qFWgkU2rGHdKlKowJSMN9h0= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= +golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= From 212457767271f44f0358fd4f3f5b12db908f14e2 Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Fri, 10 Nov 2023 16:03:28 +0700 Subject: [PATCH 48/51] update test and dep for atlas --- atlas_test.go | 2 +- go.mod | 2 +- go.sum | 6 ++---- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/atlas_test.go b/atlas_test.go index 5560b7f..b861e89 100644 --- a/atlas_test.go +++ b/atlas_test.go @@ -10,7 +10,7 @@ import ( func TestAtlas(t *testing.T) { convey.Convey("prepare db", t, func() { - connTxt := "mongodb+srv://readonly:Password.1@cluster0.lobvo.mongodb.net/appdb?retryWrites=true&w=majority" + connTxt := "mongodb+srv://coba-user:Password.1@cluster0.lobvo.mongodb.net/appdb?retryWrites=true&w=majority" conn, err := dbflex.NewConnectionFromURI(connTxt, nil) convey.So(err, convey.ShouldBeNil) diff --git a/go.mod b/go.mod index bdd30fd..027afcb 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/ariefdarmawan/flexmgo go 1.16 require ( - git.kanosolution.net/kano/dbflex v1.3.4-0.20231105001524-58de8c516292 + git.kanosolution.net/kano/dbflex v1.3.4 github.com/ariefdarmawan/serde v0.1.1 github.com/sebarcode/codekit v0.1.2 github.com/sebarcode/logger v0.1.1 diff --git a/go.sum b/go.sum index 01aa59b..fa65ab3 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,5 @@ -git.kanosolution.net/kano/dbflex v1.2.9 h1:6lto8XVLWoHMLIATE3ALGcCfkks2kAS+uUp7DyjX9xU= -git.kanosolution.net/kano/dbflex v1.2.9/go.mod h1:FQZKWzBpYXNalY8syaXBjVBXyg58hIuUXQuIB08l7Zw= -git.kanosolution.net/kano/dbflex v1.3.4-0.20231105001524-58de8c516292 h1:4ai+Jd7TO02dMvMJEDtFyPoFwJ4i1DRbpTAXfAWRVwc= -git.kanosolution.net/kano/dbflex v1.3.4-0.20231105001524-58de8c516292/go.mod h1:mqgrdJe4IIfuLOMf1qj5kJUkL2ckiQ3XcA8LFQfE9k4= +git.kanosolution.net/kano/dbflex v1.3.4 h1:DRjvrVQmNxttEieJcvih9g84JWl6wdZjGR9tCr4KmF8= +git.kanosolution.net/kano/dbflex v1.3.4/go.mod h1:mqgrdJe4IIfuLOMf1qj5kJUkL2ckiQ3XcA8LFQfE9k4= github.com/ariefdarmawan/reflector v0.0.3 h1:qbfwP5iKMvdYBWRG2jZyoXRLPsGedJ1sNcC8bw+MhpY= github.com/ariefdarmawan/reflector v0.0.3/go.mod h1:xxeY7n6iT0q5pK4j3s8l2xo1CvmQgS2oYtj0lsV4eo4= github.com/ariefdarmawan/serde v0.1.1 h1:TIhTKKFfK4WfYcS/hW1nSD81Uqd1jkY9Xli8e/3h/MA= From af4b276137d1369d32e3b61ffd42afe12d0804d2 Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Mon, 4 Dec 2023 08:46:20 +0700 Subject: [PATCH 49/51] deps --- go.mod | 26 ++++++++++++++++++++++++-- go.sum | 6 ------ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index 027afcb..7ae56a3 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,8 @@ module github.com/ariefdarmawan/flexmgo -go 1.16 +go 1.19 + +replace git.kanosolution.net/kano/dbflex => ../dbflex require ( git.kanosolution.net/kano/dbflex v1.3.4 @@ -8,7 +10,27 @@ require ( github.com/sebarcode/codekit v0.1.2 github.com/sebarcode/logger v0.1.1 github.com/smartystreets/goconvey v1.7.2 - github.com/stretchr/testify v1.6.1 // indirect go.mongodb.org/mongo-driver v1.12.1 +) + +require ( + github.com/ariefdarmawan/reflector v0.0.3 // indirect + github.com/eaciit/toolkit v0.0.0-20210610161449-593d5fadf78e // indirect + github.com/golang/snappy v0.0.1 // indirect + github.com/google/uuid v1.3.0 // indirect + github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 // indirect + github.com/jtolds/gls v4.20.0+incompatible // indirect + github.com/klauspost/compress v1.13.6 // indirect + github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect + github.com/smartystreets/assertions v1.2.0 // indirect + github.com/stretchr/testify v1.6.1 // indirect + github.com/xdg-go/pbkdf2 v1.0.0 // indirect + github.com/xdg-go/scram v1.1.2 // indirect + github.com/xdg-go/stringprep v1.0.4 // indirect + github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect + golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect + golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect + golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect + golang.org/x/text v0.7.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index fa65ab3..221b8d0 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,3 @@ -git.kanosolution.net/kano/dbflex v1.3.4 h1:DRjvrVQmNxttEieJcvih9g84JWl6wdZjGR9tCr4KmF8= -git.kanosolution.net/kano/dbflex v1.3.4/go.mod h1:mqgrdJe4IIfuLOMf1qj5kJUkL2ckiQ3XcA8LFQfE9k4= github.com/ariefdarmawan/reflector v0.0.3 h1:qbfwP5iKMvdYBWRG2jZyoXRLPsGedJ1sNcC8bw+MhpY= github.com/ariefdarmawan/reflector v0.0.3/go.mod h1:xxeY7n6iT0q5pK4j3s8l2xo1CvmQgS2oYtj0lsV4eo4= github.com/ariefdarmawan/serde v0.1.1 h1:TIhTKKFfK4WfYcS/hW1nSD81Uqd1jkY9Xli8e/3h/MA= @@ -9,7 +7,6 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/eaciit/toolkit v0.0.0-20210610161449-593d5fadf78e h1:2/wdIW25ZaPHfKQ5HvQUSAqeH9KAZQH1PlCMnrSJ9xE= github.com/eaciit/toolkit v0.0.0-20210610161449-593d5fadf78e/go.mod h1:r4OKDNGrY6n6gCVqEFdld+JTfgdNgp78RPvZHr2B9jU= -github.com/gofrs/flock v0.8.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU= github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4= github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM= @@ -41,7 +38,6 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/theckman/go-flock v0.8.1/go.mod h1:kjuth3y9VJ2aNlkNEO99G/8lp9fMIKaGyBmh84IBheM= github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c= github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI= github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY= @@ -62,7 +58,6 @@ golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20220722155237-a158d28d115b h1:PxfKdU9lEEDYjdIzOtC4qFWgkU2rGHdKlKowJSMN9h0= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= @@ -73,7 +68,6 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= From 9e946571d8dc5a66062620cf991ccba6beed7fa4 Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Wed, 31 Jan 2024 05:11:52 +0700 Subject: [PATCH 50/51] updated deprecated function --- atlas_test.go | 30 ++++++++++++++++++++++++++++++ connection.go | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/atlas_test.go b/atlas_test.go index b861e89..fff4baa 100644 --- a/atlas_test.go +++ b/atlas_test.go @@ -2,6 +2,7 @@ package flexmgo_test import ( "testing" + "time" "git.kanosolution.net/kano/dbflex" "github.com/sebarcode/codekit" @@ -31,3 +32,32 @@ func TestAtlas(t *testing.T) { }) }) } + +func TestCluster(t *testing.T) { + convey.Convey("prepare db", t, func() { + connTxt := "mongodb://devops:w0bTOURMkypnd4mIPH2ZPgbB@node01.mongo.bagong.kanosolution.app:27017,node02.mongo.bagong.kanosolution.app:27017,node03.mongo.bagong.kanosolution.app:27017/bis-stg?authSource=admin&retryWrites=true&w=majority" + conn, err := dbflex.NewConnectionFromURI(connTxt, nil) + convey.So(err, convey.ShouldBeNil) + + convey.Convey("connect", func() { + err = conn.Connect() + convey.So(err, convey.ShouldBeNil) + defer conn.Close() + conn.SetFieldNameTag("json") + + convey.Convey("insert data", func() { + _, err = conn.Execute(dbflex.From("info").Insert(), codekit.M{}.Set("data", codekit.M{}.Set("Version", 1).Set("Ts", time.Now()))) + convey.So(err, convey.ShouldBeNil) + + convey.Convey("read data", func() { + dest := codekit.M{} + err = conn.Cursor(dbflex.From("info").Select().Take(1), nil).Fetch(&dest).Error() + convey.So(err, convey.ShouldBeNil) + convey.So(dest.GetInt("Version"), convey.ShouldNotEqual, 0) + convey.Println() + convey.Println(codekit.JsonString(dest)) + }) + }) + }) + }) +} diff --git a/connection.go b/connection.go index 2e5d03b..a5acd2f 100644 --- a/connection.go +++ b/connection.go @@ -233,7 +233,7 @@ func (c *Connection) BeginTx() error { return errors.New("tx is disabled") } - wc := writeconcern.New(writeconcern.WMajority()) + wc := writeconcern.Majority() rc := readconcern.Snapshot() txnOpts := options.Transaction().SetWriteConcern(wc).SetReadConcern(rc) From 37e02377f734d2e4fa765c58eefba2ebd94cf380 Mon Sep 17 00:00:00 2001 From: Arief Darmawan Date: Wed, 17 Apr 2024 14:24:41 +0700 Subject: [PATCH 51/51] update wraptx to create new context bcause nil will raise error on official mongo driver v1.5 --- query.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/query.go b/query.go index 513bdad..c246831 100644 --- a/query.go +++ b/query.go @@ -1,6 +1,7 @@ package flexmgo import ( + "context" "errors" "fmt" "strings" @@ -303,7 +304,6 @@ func (q *Query) Cursor(m M) df.ICursor { err = wrapTx(conn, func(ctx mongo.SessionContext) error { var err error - //fmt.Println(codekit.JsonString(opt.Sort)) qry, err = coll.Find(ctx, where, opt) return err }) @@ -444,7 +444,7 @@ func wrapTx(conn *Connection, fn func(ctx mongo.SessionContext) error) error { return fn(sc) }) } else { - err = fn(nil) + err = fn(mongo.NewSessionContext(context.Background(), nil)) } return err }