-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mongodb): add support for mongodb
- Loading branch information
1 parent
460fff6
commit f2a2850
Showing
6 changed files
with
138 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,6 +98,7 @@ ports: | |
visibility: private | ||
|
||
- port: 27017 | ||
name: MongoDB | ||
visibility: private | ||
|
||
vscode: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package mongodb | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
"go.mongodb.org/mongo-driver/mongo/options" | ||
) | ||
|
||
type mongoConnection struct { | ||
client *mongo.Client | ||
db *mongo.Database | ||
} | ||
|
||
type MongoDB struct { | ||
activeConnection mongoConnection | ||
connectionUri string | ||
database string | ||
} | ||
|
||
func (db *MongoDB) Auth() error { | ||
opts := options.Client(). | ||
ApplyURI(db.connectionUri). | ||
SetConnectTimeout(1 * time.Second) // Connection should have a short timeout to fail fast | ||
|
||
client, err := mongo.Connect(context.TODO(), opts) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Store the Mongo connection | ||
db.activeConnection = mongoConnection{ | ||
client: client, | ||
db: client.Database(db.database), | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (db *MongoDB) Close() error { | ||
return db.activeConnection.client.Disconnect(context.TODO()) | ||
} | ||
|
||
func (db *MongoDB) InsertBulk(collection string, raw []map[string]interface{}) (int, error) { | ||
var err error | ||
data := make([]interface{}, 0) | ||
|
||
// Conver the raw data into MongoDB format | ||
for _, row := range raw { | ||
item := bson.D{} | ||
|
||
for k, v := range row { | ||
item = append(item, bson.E{ | ||
Key: k, | ||
Value: v, | ||
}) | ||
} | ||
|
||
data = append(data, item) | ||
} | ||
|
||
insertCount := len(data) | ||
|
||
if insertCount > 0 { | ||
_, err = db.activeConnection.db. | ||
Collection(collection). | ||
InsertMany(context.TODO(), data) | ||
} | ||
|
||
return insertCount, err | ||
} | ||
|
||
func (db *MongoDB) Truncate(collection string) error { | ||
return db.activeConnection.db. | ||
Collection(collection). | ||
Drop(context.TODO()) | ||
} | ||
|
||
func New(connectionUri string, database string) *MongoDB { | ||
return &MongoDB{ | ||
connectionUri: connectionUri, | ||
database: database, | ||
} | ||
} |