package db import ( "context" "pcgamedb/config" "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...) } 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...) } 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...) } 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...) } 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...) }