x
This commit is contained in:
+156
@@ -0,0 +1,156 @@
|
||||
package mongodb
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/dop251/goja"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
type MongoClient struct {
|
||||
client *mongo.Client
|
||||
Uri string
|
||||
Vm *goja.Runtime
|
||||
}
|
||||
|
||||
func (mc *MongoClient) Connect(uri string) {
|
||||
var err error
|
||||
if mc.Uri == "" {
|
||||
mc.Uri = uri
|
||||
}
|
||||
mc.client, err = GetClient(mc.Uri)
|
||||
if err != nil {
|
||||
panic(mc.Vm.NewGoError(err))
|
||||
}
|
||||
}
|
||||
|
||||
func (mc *MongoClient) Db(dbname string) interface{} {
|
||||
if mc.client == nil {
|
||||
mc.Connect("")
|
||||
}
|
||||
return &MongoDB{
|
||||
Vm: mc.Vm,
|
||||
database: mc.client.Database(dbname),
|
||||
}
|
||||
}
|
||||
|
||||
type MongoDB struct {
|
||||
database *mongo.Database
|
||||
Vm *goja.Runtime
|
||||
}
|
||||
|
||||
func (db *MongoDB) Collection(clname string) interface{} {
|
||||
return &Collection{
|
||||
Vm: db.Vm,
|
||||
collection: db.database.Collection(clname),
|
||||
}
|
||||
}
|
||||
|
||||
type Collection struct {
|
||||
collection *mongo.Collection
|
||||
Vm *goja.Runtime
|
||||
}
|
||||
|
||||
func (cl *Collection) InsertOne(insert map[string]interface{}) interface{} {
|
||||
result, err := cl.collection.InsertOne(context.Background(), insert)
|
||||
if err != nil {
|
||||
|
||||
panic(cl.Vm.NewGoError(err))
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (cl *Collection) FindOne(filter interface{}) interface{} {
|
||||
var result = map[string]interface{}{}
|
||||
err := cl.collection.FindOne(context.Background(), filter).Decode(result)
|
||||
if err != nil {
|
||||
panic(cl.Vm.NewGoError(err))
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (cl *Collection) FindOneAndDelete(filter interface{}) interface{} {
|
||||
var result = map[string]interface{}{}
|
||||
err := cl.collection.FindOneAndDelete(context.Background(), filter).Decode(result)
|
||||
if err != nil {
|
||||
panic(cl.Vm.NewGoError(err))
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (cl *Collection) FindOneAndReplace(filter interface{}, replace interface{}) interface{} {
|
||||
var result = map[string]interface{}{}
|
||||
err := cl.collection.FindOneAndReplace(context.Background(), filter, replace).Decode(result)
|
||||
if err != nil {
|
||||
panic(cl.Vm.NewGoError(err))
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (cl *Collection) FindOneAndUpdate(filter interface{}, update interface{}) interface{} {
|
||||
var result = map[string]interface{}{}
|
||||
err := cl.collection.FindOneAndUpdate(context.Background(), filter, update).Decode(result)
|
||||
if err != nil {
|
||||
panic(cl.Vm.NewGoError(err))
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (cl *Collection) Find(filter interface{}) interface{} {
|
||||
cur, err := cl.collection.Find(context.Background(), filter)
|
||||
if err != nil {
|
||||
panic(cl.Vm.NewGoError(err))
|
||||
}
|
||||
defer cur.Close(context.Background())
|
||||
var results = []map[string]interface{}{}
|
||||
for cur.Next(context.Background()) {
|
||||
var person = map[string]interface{}{}
|
||||
err := cur.Decode(&person)
|
||||
if err != nil {
|
||||
panic(cl.Vm.NewGoError(err))
|
||||
}
|
||||
results = append(results, person)
|
||||
}
|
||||
if err := cur.Err(); err != nil {
|
||||
panic(cl.Vm.NewGoError(err))
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
func (cl *Collection) UpdateOne(filter, update interface{}, params map[string]interface{}) interface{} {
|
||||
opts := []*options.UpdateOptions{}
|
||||
if v, ok := params["upsert"]; ok {
|
||||
opts = append(opts, options.Update().SetUpsert(v.(bool)))
|
||||
}
|
||||
result, err := cl.collection.UpdateOne(context.Background(), filter, update, opts...)
|
||||
if err != nil {
|
||||
panic(cl.Vm.NewGoError(err))
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (cl *Collection) UpdateMany(filter, update interface{}) interface{} {
|
||||
|
||||
result, err := cl.collection.UpdateMany(context.Background(), filter, update)
|
||||
if err != nil {
|
||||
panic(cl.Vm.NewGoError(err))
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (cl *Collection) DeleteOne(filter interface{}) interface{} {
|
||||
result, err := cl.collection.DeleteOne(context.Background(), filter)
|
||||
if err != nil {
|
||||
panic(cl.Vm.NewGoError(err))
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (cl *Collection) DeleteMany(filter interface{}) interface{} {
|
||||
result, err := cl.collection.DeleteMany(context.Background(), filter)
|
||||
if err != nil {
|
||||
panic(cl.Vm.NewGoError(err))
|
||||
}
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package mongodb
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
// MongoDB连接池
|
||||
type MongoDBPool struct {
|
||||
clients map[string]*mongo.Client // 客户端连接map
|
||||
mutex sync.Mutex // 互斥锁
|
||||
}
|
||||
|
||||
var p *MongoDBPool
|
||||
|
||||
// 创建一个新的MongoDB连接池
|
||||
func NewMongoDBPool() *MongoDBPool {
|
||||
return &MongoDBPool{
|
||||
clients: make(map[string]*mongo.Client),
|
||||
}
|
||||
}
|
||||
|
||||
// 获取一个MongoDB客户端连接
|
||||
func GetClient(uri string) (*mongo.Client, error) {
|
||||
if p == nil {
|
||||
p = NewMongoDBPool()
|
||||
}
|
||||
// 加锁
|
||||
p.mutex.Lock()
|
||||
defer p.mutex.Unlock()
|
||||
|
||||
// 检查连接是否存在
|
||||
if client, ok := p.clients[uri]; ok {
|
||||
return client, nil
|
||||
}
|
||||
|
||||
// 创建新的连接
|
||||
client, err := mongo.NewClient(options.Client().ApplyURI(uri))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 连接数据库
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
err = client.Connect(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 保存连接
|
||||
p.clients[uri] = client
|
||||
|
||||
return client, nil
|
||||
}
|
||||
|
||||
// 关闭一个MongoDB客户端连接
|
||||
func CloseClient(uri string) error {
|
||||
// 加锁
|
||||
p.mutex.Lock()
|
||||
defer p.mutex.Unlock()
|
||||
|
||||
// 检查连接是否存在
|
||||
if client, ok := p.clients[uri]; ok {
|
||||
// 关闭连接
|
||||
err := client.Disconnect(context.Background())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 删除连接
|
||||
delete(p.clients, uri)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
return errors.New("client not found")
|
||||
}
|
||||
Reference in New Issue
Block a user