Skip to content

Commit

Permalink
Merge pull request #8 from bhavyagupta3006/feat/mongo-crud-operations
Browse files Browse the repository at this point in the history
feat: mongo crud operations added
  • Loading branch information
GhMartingit authored Jun 6, 2023
2 parents 74d7f9d + 64adfb4 commit b24afe1
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 13 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@ k6
# Output of the go coverage tool, specifically when used with LiteIDE
*.out

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
out/
32 changes: 32 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
MAKEFLAGS += --silent

all: clean format test build

## help: Prints a list of available build targets.
help:
echo "Usage: make <OPTIONS> ... <TARGETS>"
echo ""
echo "Available targets are:"
echo ''
sed -n 's/^##//p' ${PWD}/Makefile | column -t -s ':' | sed -e 's/^/ /'
echo
echo "Targets run by default are: `sed -n 's/^all: //p' ./Makefile | sed -e 's/ /, /g' | sed -e 's/\(.*\), /\1, and /'`"

## clean: Removes any previously created build artifacts.
clean:
rm -f ./k6

## build: Builds a custom 'k6' with the local extension.
build:
go install go.k6.io/xk6/cmd/xk6@latest
xk6 build --with $(shell go list -m)=.

## format: Applies Go formatting to code.
format:
go fmt ./...

## test: Executes any unit tests.
test:
go test -cover -race ./...

.PHONY: build clean format help test
52 changes: 49 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,55 @@ K6 extension to perform tests on mongo.
## Currently Supported Commands

- Supports inserting a document.
- Support findOne (Fetch by primary key)
- Support checking query performance
- Supports inserting document batch.
- Supports find a document based on filter.
- Supports find all documents of a collection.
- Supports delete first document based on filter.
- Supports deleting all documents for a specific filter.
- Supports dropping a collection.

# xk6-mongo
A k6 extension for interacting with mongoDb while testing.

## Build

To build a custom `k6` binary with this extension, first ensure you have the prerequisites:

- [Go toolchain](https://go101.org/article/go-toolchain.html)
- Git

1. Download [xk6](https://github.com/grafana/xk6):

```bash
go install go.k6.io/xk6/cmd/xk6@latest
```

2. [Build the k6 binary](https://github.com/grafana/xk6#command-usage):

```bash
xk6 build --with github.com/GhMartingit/xk6-mongo
```

This will create a k6 binary that includes the xk6-mongo extension in your local folder. This k6 binary can now run a k6 test.

### Development
To make development a little smoother, use the `Makefile` in the root folder. The default target will format your code, run tests, and create a `k6` binary with your local code rather than from GitHub.

```shell
git clone [email protected]/GhMartingit/xk6-mongo.git
cd xk6-mongo
make build
```

Using the `k6` binary with `xk6-mongo`, run the k6 test as usual:

```bash
./k6 run k8s-test-script.js
```

## Examples:

### Document Insertion Test
```js
import xk6_mongo from 'k6/x/mongo';
Expand All @@ -28,4 +73,5 @@ export default ()=> {
client.insert("testdb", "testcollection", doc);
}
```
```

71 changes: 61 additions & 10 deletions mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package xk6_mongo

import (
"context"
"go.mongodb.org/mongo-driver/bson"
"log"

"strings"
"errors"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"

Expand Down Expand Up @@ -35,11 +36,11 @@ func (*Mongo) NewClient(connURI string) interface{} {
if err != nil {
return err
}
return &Client{client: client}

return &Client{client: client}
}

func (c *Client) Insert(database string, collection string, doc interface{}) error {
func (c *Client) Insert(database string, collection string, doc map[string]string) error {
db := c.client.Database(database)
col := db.Collection(collection)
_, err := col.InsertOne(context.TODO(), doc)
Expand All @@ -49,29 +50,27 @@ func (c *Client) Insert(database string, collection string, doc interface{}) err
return nil
}

func (c *Client) InsertBatch(database string, collection string, docs []any) error {

func (c *Client) InsertMany(database string, collection string, docs []any) error {
log.Printf("Insert multiple documents")
db := c.client.Database(database)
col := db.Collection(collection)
_, err := col.InsertMany(context.TODO(), docs)
if err != nil {
return err
}
return nil

}

func (c *Client) Find(database string, collection string, filter interface{}) []bson.M {

func (c *Client) Find(database string, collection string, filter interface{}) []bson.M{
db := c.client.Database(database)
col := db.Collection(collection)

log.Print("filter is ", filter)
cur, err := col.Find(context.TODO(), filter)
if err != nil {
log.Fatal(err)
// return nil
}

var results []bson.M
if err = cur.All(context.TODO(), &results); err != nil {
panic(err)
Expand All @@ -92,3 +91,55 @@ func (c *Client) FindOne(database string, collection string, filter map[string]s
log.Printf("found document %v", result)
return nil
}

func (c *Client) FindAll(database string, collection string) []bson.M{
log.Printf("Find all documents")
db := c.client.Database(database)
col := db.Collection(collection)
cur, err := col.Find(context.TODO(), bson.D{{}})
if err != nil {
log.Fatal(err)
}
var results []bson.M
if err = cur.All(context.TODO(), &results); err != nil {
panic(err)
}
return results
}

func (c *Client) DeleteOne(database string, collection string, filter map[string]string) error {
db := c.client.Database(database)
col := db.Collection(collection)
opts := options.Delete().SetHint(bson.D{{"_id", 1}})
log.Print("filter is ", filter)
result, err := col.DeleteMany(context.TODO(), filter, opts)
if err != nil {
log.Fatal(err)
}
log.Printf("Deleted documents %v", result)
return nil
}

func (c *Client) DeleteMany(database string, collection string, filter map[string]string) error {
db := c.client.Database(database)
col := db.Collection(collection)
opts := options.Delete().SetHint(bson.D{{"_id", 1}})
log.Print("filter is ", filter)
result, err := col.DeleteMany(context.TODO(), filter, opts)
if err != nil {
log.Fatal(err)
}
log.Printf("Deleted documents %v", result)
return nil
}

func (c *Client) DropCollection(database string, collection string) error {
log.Printf("Delete collection if present")
db := c.client.Database(database)
col := db.Collection(collection)
err := col.Drop(context.TODO())
if err != nil {
log.Fatal(err)
}
return nil
}

0 comments on commit b24afe1

Please sign in to comment.