2024-09-24 06:17:11 -04:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-11-15 02:02:45 -05:00
|
|
|
|
2024-11-20 06:09:04 -05:00
|
|
|
"pcgamedb/config"
|
2024-09-24 06:17:11 -04:00
|
|
|
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CustomCollection struct {
|
|
|
|
collName string
|
|
|
|
coll *mongo.Collection
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CustomCollection) Find(ctx context.Context, filter interface{},
|
|
|
|
opts ...*options.FindOptions) (cur *mongo.Cursor, err error) {
|
|
|
|
CheckConnect()
|
|
|
|
if c.coll == nil {
|
|
|
|
c.coll = mongoDB.Database(config.Config.Database.Database).Collection(c.collName)
|
|
|
|
}
|
|
|
|
return c.coll.Find(ctx, filter, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CustomCollection) FindOne(ctx context.Context, filter interface{},
|
|
|
|
opts ...*options.FindOneOptions) *mongo.SingleResult {
|
|
|
|
CheckConnect()
|
|
|
|
if c.coll == nil {
|
|
|
|
c.coll = mongoDB.Database(config.Config.Database.Database).Collection(c.collName)
|
|
|
|
}
|
|
|
|
return c.coll.FindOne(ctx, filter, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CustomCollection) UpdateOne(ctx context.Context, filter interface{}, update interface{},
|
|
|
|
opts ...*options.UpdateOptions) (*mongo.UpdateResult, error) {
|
|
|
|
CheckConnect()
|
|
|
|
if c.coll == nil {
|
|
|
|
c.coll = mongoDB.Database(config.Config.Database.Database).Collection(c.collName)
|
|
|
|
}
|
|
|
|
return c.coll.UpdateOne(ctx, filter, update, opts...)
|
|
|
|
}
|
|
|
|
|
2024-11-21 12:30:26 -05:00
|
|
|
func (c *CustomCollection) UpdateMany(ctx context.Context, filter interface{}, update interface{},
|
|
|
|
opts ...*options.UpdateOptions) (*mongo.UpdateResult, error) {
|
|
|
|
CheckConnect()
|
|
|
|
if c.coll == nil {
|
|
|
|
c.coll = mongoDB.Database(config.Config.Database.Database).Collection(c.collName)
|
|
|
|
}
|
|
|
|
return c.coll.UpdateMany(ctx, filter, update, opts...)
|
|
|
|
}
|
|
|
|
|
2024-09-24 06:17:11 -04:00
|
|
|
func (c *CustomCollection) Aggregate(ctx context.Context, pipeline interface{},
|
|
|
|
opts ...*options.AggregateOptions) (*mongo.Cursor, error) {
|
|
|
|
CheckConnect()
|
|
|
|
if c.coll == nil {
|
|
|
|
c.coll = mongoDB.Database(config.Config.Database.Database).Collection(c.collName)
|
|
|
|
}
|
|
|
|
return c.coll.Aggregate(ctx, pipeline, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CustomCollection) DeleteOne(ctx context.Context, filter interface{},
|
|
|
|
opts ...*options.DeleteOptions) (*mongo.DeleteResult, error) {
|
|
|
|
CheckConnect()
|
|
|
|
if c.coll == nil {
|
|
|
|
c.coll = mongoDB.Database(config.Config.Database.Database).Collection(c.collName)
|
|
|
|
}
|
|
|
|
return c.coll.DeleteOne(ctx, filter, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CustomCollection) DeleteMany(ctx context.Context, filter interface{},
|
|
|
|
opts ...*options.DeleteOptions) (*mongo.DeleteResult, error) {
|
|
|
|
CheckConnect()
|
|
|
|
if c.coll == nil {
|
|
|
|
c.coll = mongoDB.Database(config.Config.Database.Database).Collection(c.collName)
|
|
|
|
}
|
|
|
|
return c.coll.DeleteMany(ctx, filter, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CustomCollection) CountDocuments(ctx context.Context, filter interface{},
|
|
|
|
opts ...*options.CountOptions) (int64, error) {
|
|
|
|
CheckConnect()
|
|
|
|
if c.coll == nil {
|
|
|
|
c.coll = mongoDB.Database(config.Config.Database.Database).Collection(c.collName)
|
|
|
|
}
|
|
|
|
return c.coll.CountDocuments(ctx, filter, opts...)
|
|
|
|
}
|
2024-11-17 00:29:04 -05:00
|
|
|
|
|
|
|
func (c *CustomCollection) InsertOne(ctx context.Context, document interface{},
|
|
|
|
opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error) {
|
|
|
|
CheckConnect()
|
|
|
|
if c.coll == nil {
|
|
|
|
c.coll = mongoDB.Database(config.Config.Database.Database).Collection(c.collName)
|
|
|
|
}
|
|
|
|
return c.coll.InsertOne(ctx, document, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CustomCollection) InsertMany(ctx context.Context, documents []interface{},
|
|
|
|
opts ...*options.InsertManyOptions) (*mongo.InsertManyResult, error) {
|
|
|
|
CheckConnect()
|
|
|
|
if c.coll == nil {
|
|
|
|
c.coll = mongoDB.Database(config.Config.Database.Database).Collection(c.collName)
|
|
|
|
}
|
|
|
|
return c.coll.InsertMany(ctx, documents, opts...)
|
|
|
|
}
|
2024-11-21 12:30:26 -05:00
|
|
|
|
|
|
|
func (c *CustomCollection) BulkWrite(ctx context.Context, models []mongo.WriteModel,
|
|
|
|
opts ...*options.BulkWriteOptions) (*mongo.BulkWriteResult, error) {
|
|
|
|
CheckConnect()
|
|
|
|
if c.coll == nil {
|
|
|
|
c.coll = mongoDB.Database(config.Config.Database.Database).Collection(c.collName)
|
|
|
|
}
|
|
|
|
return c.coll.BulkWrite(ctx, models, opts...)
|
|
|
|
}
|