Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix update and insert #2

Open
wants to merge 54 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
7ca4d7b
fix update and insert
ariefdarmawan Sep 26, 2019
57e601f
k-upd various
ariefdarmawan Apr 25, 2020
d6d51c7
fixing count issue
ariefdarmawan May 31, 2020
4968950
remove dep to eaciit
ariefdarmawan Jun 9, 2020
24e10ec
update with latest df
ariefdarmawan Jun 12, 2020
3fa3ed7
fix comand
ariefdarmawan Jul 9, 2020
1cd95db
Include config params in connection string
sugab Sep 9, 2020
5f5d535
remove print debug
sugab Sep 9, 2020
0362de9
Merge pull request #1 from sugab/master
ariefdarmawan Sep 9, 2020
63598ae
fix name sourcing
ariefdarmawan Sep 19, 2020
3573515
add bson.m support for pipe
ariefdarmawan Sep 19, 2020
d861b68
update fetch thus can anticipate rrequiment for bson taggiing
ariefdarmawan Sep 19, 2020
dcd99ef
update ftechs to anticipate bson tagging
ariefdarmawan Sep 19, 2020
85b1b3a
handle date
ariefdarmawan Sep 21, 2020
0585eae
fix date
ariefdarmawan Sep 21, 2020
209ba93
fix count
ariefdarmawan Nov 24, 2020
3803ca5
updateMany fix to be more efficient
ariefdarmawan Dec 31, 2020
936c7cb
inserted id is returned aafter insert
Jul 14, 2021
8583060
enable transaction
Jul 19, 2021
e4648bf
Merge pull request #2 from ariefdarmawan/dev.arief.transaction
ariefdarmawan Jul 19, 2021
d646154
add supportTx so it can be used by datahub
Jul 19, 2021
e2d449c
add comment
Jul 20, 2021
aa1de93
add ensuretable and update deps
ariefdarmawan Jun 14, 2022
62970db
no toolkit
ariefdarmawan Jun 16, 2022
8ab032c
fix deps and serde
ariefdarmawan Jun 17, 2022
63e2ac1
update deps and change compat to minimum v1.6
ariefdarmawan Jun 17, 2022
4cf07d7
update fetch and fetchs decode mechanism
ariefdarmawan Jun 17, 2022
118c419
apply ensureIndex
ariefdarmawan Jul 6, 2022
7cc47b4
update go mod
ariefdarmawan Jul 6, 2022
ce9df03
all dan elemMatch
ariefdarmawan Aug 14, 2022
e18f3a5
update mod to latest
ariefdarmawan Aug 14, 2022
d2d897f
applying disableTx
ariefdarmawan Aug 25, 2022
a80e3ba
fix supportTx
ariefdarmawan Aug 25, 2022
4bf8a91
fix supportTx
ariefdarmawan Aug 25, 2022
f6b2d2f
add dep
ariefdarmawan Aug 25, 2022
8a3be62
apply ObjectNames
ariefdarmawan Sep 15, 2022
07c97f2
fix on cursor more than 2000 rows
ariefdarmawan Sep 17, 2022
7717447
add limit on count and text search
ariefdarmawan Sep 29, 2022
e5a9df7
fix command
ariefdarmawan Oct 7, 2022
2d2ce41
Allowing alias for cmd.Select(fields ...string)
ariefdarmawan Oct 7, 2022
62d7b5d
update connection setting working with different authSource
ariefdarmawan Dec 30, 2022
a5a15b5
authmechanism update
ariefdarmawan Dec 30, 2022
71617fb
fix command cursor by adding connection
ariefdarmawan Apr 6, 2023
2293e9f
fix startswith
ariefdarmawan May 16, 2023
818a651
fix sort
ariefdarmawan May 19, 2023
8ff28a1
Merge branch 'master' of https://github.com/ariefdarmawan/flexmgo
ariefdarmawan May 19, 2023
2d27774
sort change from M to bson.D
ariefdarmawan May 19, 2023
49587f3
fix sort multi key
ariefdarmawan May 19, 2023
90c3585
split command handling and add gfs
ariefdarmawan Jun 11, 2023
20cdd61
add atlas support
ariefdarmawan Nov 5, 2023
2124577
update test and dep for atlas
ariefdarmawan Nov 10, 2023
af4b276
deps
ariefdarmawan Dec 4, 2023
9e94657
updated deprecated function
ariefdarmawan Jan 30, 2024
37e0237
update wraptx to create new context bcause nil will raise error on
ariefdarmawan Apr 17, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions atlas_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package flexmgo_test

import (
"testing"
"time"

"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://coba-user:[email protected]/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))
})
})
})
}

func TestCluster(t *testing.T) {
convey.Convey("prepare db", t, func() {
connTxt := "mongodb://devops:[email protected]: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))
})
})
})
})
}
157 changes: 157 additions & 0 deletions command_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
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")
if gfsFileName == "" {
gfsFileName = codekit.RandomString(32)
}
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)
}
}
Loading