mongoclient pointer 리시버로 수정

This commit is contained in:
2024-06-25 10:26:26 +09:00
parent 5352e6ba59
commit eaae5a5ccd

View File

@ -127,21 +127,21 @@ func newMongoClient(ctx context.Context, ci *ConnectionInfo) (MongoClient, error
return MongoClient{c: client, db: mdb}, nil
}
func (mc MongoClient) Connected() bool {
func (mc *MongoClient) Connected() bool {
return mc.db != nil && mc.c != nil
}
func (mc MongoClient) Close() {
func (mc *MongoClient) Close() {
if mc.c != nil {
mc.c.Disconnect(mc.ctx)
}
}
func (mc MongoClient) Drop() error {
func (mc *MongoClient) Drop() error {
return mc.db.Drop(mc.ctx)
}
func (mc MongoClient) DropIndex(coll CollectionName, name string) error {
func (mc *MongoClient) DropIndex(coll CollectionName, name string) error {
matchcoll := mc.Collection(coll)
_, err := matchcoll.Indexes().DropOne(mc.ctx, name)
if commanderr, ok := err.(mongo.CommandError); ok {
@ -153,18 +153,18 @@ func (mc MongoClient) DropIndex(coll CollectionName, name string) error {
return err
}
func (mc MongoClient) Watch(coll CollectionName, pipeline mongo.Pipeline, opts ...*options.ChangeStreamOptions) (*mongo.ChangeStream, error) {
func (mc *MongoClient) Watch(coll CollectionName, pipeline mongo.Pipeline, opts ...*options.ChangeStreamOptions) (*mongo.ChangeStream, error) {
if len(opts) == 0 {
opts = []*options.ChangeStreamOptions{options.ChangeStream().SetFullDocument(options.UpdateLookup).SetMaxAwaitTime(0)}
}
return mc.Collection(coll).Watch(mc.ctx, pipeline, opts...)
}
func (mc MongoClient) Collection(collname CollectionName) *mongo.Collection {
func (mc *MongoClient) Collection(collname CollectionName) *mongo.Collection {
return mc.db.Collection(string(collname))
}
func (mc MongoClient) AllAs(coll CollectionName, output any, opts ...*options.FindOptions) error {
func (mc *MongoClient) AllAs(coll CollectionName, output any, opts ...*options.FindOptions) error {
cursor, err := mc.Collection(coll).Find(mc.ctx, bson.D{}, opts...)
if err != nil {
return err
@ -179,13 +179,13 @@ func (mc MongoClient) AllAs(coll CollectionName, output any, opts ...*options.Fi
return nil
}
func (mc MongoClient) All(coll CollectionName, opts ...*options.FindOptions) ([]bson.M, error) {
func (mc *MongoClient) All(coll CollectionName, opts ...*options.FindOptions) ([]bson.M, error) {
var all []bson.M
err := mc.AllAs(coll, &all, opts...)
return all, err
}
func (mc MongoClient) FindOneAndDelete(coll CollectionName, filter bson.M, opts ...*options.FindOneAndDeleteOptions) (bson.M, error) {
func (mc *MongoClient) FindOneAndDelete(coll CollectionName, filter bson.M, opts ...*options.FindOneAndDeleteOptions) (bson.M, error) {
result := mc.Collection(coll).FindOneAndDelete(mc.ctx, filter, opts...)
err := result.Err()
if err != nil {
@ -204,7 +204,7 @@ func (mc MongoClient) FindOneAndDelete(coll CollectionName, filter bson.M, opts
return bson.M(tmp), nil
}
func (mc MongoClient) Delete(coll CollectionName, filter bson.M, opts ...*options.DeleteOptions) (bool, error) {
func (mc *MongoClient) Delete(coll CollectionName, filter bson.M, opts ...*options.DeleteOptions) (bool, error) {
r, err := mc.Collection(coll).DeleteOne(mc.ctx, filter, opts...)
if err != nil {
return false, err
@ -213,14 +213,14 @@ func (mc MongoClient) Delete(coll CollectionName, filter bson.M, opts ...*option
return r.DeletedCount > 0, nil
}
func (mc MongoClient) UnsetField(coll CollectionName, filter bson.M, doc bson.M) error {
func (mc *MongoClient) UnsetField(coll CollectionName, filter bson.M, doc bson.M) error {
_, err := mc.Collection(coll).UpdateOne(mc.ctx, filter, bson.M{
"$unset": doc,
})
return err
}
func (mc MongoClient) DeleteMany(coll CollectionName, filters bson.D, opts ...*options.DeleteOptions) (int, error) {
func (mc *MongoClient) DeleteMany(coll CollectionName, filters bson.D, opts ...*options.DeleteOptions) (int, error) {
if len(filters) == 0 {
// 큰일난다
return 0, nil
@ -248,7 +248,7 @@ func (c *CommandInsertMany[T]) Exec(opts ...*options.InsertManyOptions) (int, er
return c.InsertMany(c.Collection, conv, opts...)
}
func (mc MongoClient) InsertMany(coll CollectionName, documents []interface{}, opts ...*options.InsertManyOptions) (int, error) {
func (mc *MongoClient) InsertMany(coll CollectionName, documents []interface{}, opts ...*options.InsertManyOptions) (int, error) {
result, err := mc.Collection(coll).InsertMany(mc.ctx, documents, opts...)
if err != nil {
return 0, err
@ -257,7 +257,7 @@ func (mc MongoClient) InsertMany(coll CollectionName, documents []interface{}, o
return len(result.InsertedIDs), nil
}
func (mc MongoClient) UpdateMany(coll CollectionName, filter bson.M, doc bson.M, opts ...*options.UpdateOptions) (count int, err error) {
func (mc *MongoClient) UpdateMany(coll CollectionName, filter bson.M, doc bson.M, opts ...*options.UpdateOptions) (count int, err error) {
result, e := mc.Collection(coll).UpdateMany(mc.ctx, filter, doc, opts...)
if e != nil {
@ -281,7 +281,7 @@ func (m *JsonDefaultMashaller) MarshalBSON() ([]byte, error) {
return json.Marshal(m.doc)
}
func (mc MongoClient) Update(coll CollectionName, filter bson.M, doc interface{}, opts ...*options.UpdateOptions) (worked bool, newid interface{}, err error) {
func (mc *MongoClient) Update(coll CollectionName, filter bson.M, doc interface{}, opts ...*options.UpdateOptions) (worked bool, newid interface{}, err error) {
result, e := mc.Collection(coll).UpdateOne(mc.ctx, filter, doc, opts...)
if e != nil {
@ -294,7 +294,7 @@ func (mc MongoClient) Update(coll CollectionName, filter bson.M, doc interface{}
return
}
func (mc MongoClient) UpsertOne(coll CollectionName, filter bson.M, doc interface{}) (worked bool, newid interface{}, err error) {
func (mc *MongoClient) UpsertOne(coll CollectionName, filter bson.M, doc interface{}) (worked bool, newid interface{}, err error) {
return mc.Update(coll, filter, bson.M{
"$set": doc,
}, options.Update().SetUpsert(true))
@ -304,7 +304,7 @@ func (mc MongoClient) UpsertOne(coll CollectionName, filter bson.M, doc interfac
// }}, options.Update().SetUpsert(true))
}
func (mc MongoClient) FindOneAs(coll CollectionName, filter bson.M, out interface{}, opts ...*options.FindOneOptions) error {
func (mc *MongoClient) FindOneAs(coll CollectionName, filter bson.M, out interface{}, opts ...*options.FindOneOptions) error {
err := mc.Collection(coll).FindOne(mc.ctx, filter, opts...).Decode(out)
if err == mongo.ErrNoDocuments {
err = nil
@ -312,7 +312,7 @@ func (mc MongoClient) FindOneAs(coll CollectionName, filter bson.M, out interfac
return err
}
func (mc MongoClient) FindOne(coll CollectionName, filter bson.M, opts ...*options.FindOneOptions) (doc bson.M, err error) {
func (mc *MongoClient) FindOne(coll CollectionName, filter bson.M, opts ...*options.FindOneOptions) (doc bson.M, err error) {
result := mc.Collection(coll).FindOne(mc.ctx, filter, opts...)
tmp := make(map[string]interface{})
err = result.Decode(&tmp)
@ -325,7 +325,7 @@ func (mc MongoClient) FindOne(coll CollectionName, filter bson.M, opts ...*optio
return
}
func (mc MongoClient) FindOneAndUpdateAs(coll CollectionName, filter bson.M, doc bson.M, out interface{}, opts ...*options.FindOneAndUpdateOptions) error {
func (mc *MongoClient) FindOneAndUpdateAs(coll CollectionName, filter bson.M, doc bson.M, out interface{}, opts ...*options.FindOneAndUpdateOptions) error {
result := mc.Collection(coll).FindOneAndUpdate(mc.ctx, filter, doc, opts...)
err := result.Decode(out)
if err == nil {
@ -339,7 +339,7 @@ func (mc MongoClient) FindOneAndUpdateAs(coll CollectionName, filter bson.M, doc
return err
}
func (mc MongoClient) FindOneAndUpdate(coll CollectionName, filter bson.M, doc bson.M, opts ...*options.FindOneAndUpdateOptions) (olddoc bson.M, err error) {
func (mc *MongoClient) FindOneAndUpdate(coll CollectionName, filter bson.M, doc bson.M, opts ...*options.FindOneAndUpdateOptions) (olddoc bson.M, err error) {
result := mc.Collection(coll).FindOneAndUpdate(mc.ctx, filter, doc, opts...)
tmp := make(map[string]interface{})
err = result.Decode(&tmp)
@ -352,7 +352,7 @@ func (mc MongoClient) FindOneAndUpdate(coll CollectionName, filter bson.M, doc b
return
}
func (mc MongoClient) Exists(coll CollectionName, filter bson.M) (bool, error) {
func (mc *MongoClient) Exists(coll CollectionName, filter bson.M) (bool, error) {
cnt, err := mc.Collection(coll).CountDocuments(mc.ctx, filter, options.Count().SetLimit(1))
if err != nil {
return false, err
@ -360,7 +360,7 @@ func (mc MongoClient) Exists(coll CollectionName, filter bson.M) (bool, error) {
return cnt > 0, nil
}
func (mc MongoClient) SearchText(coll CollectionName, text string, opts ...*options.FindOptions) ([]bson.M, error) {
func (mc *MongoClient) SearchText(coll CollectionName, text string, opts ...*options.FindOptions) ([]bson.M, error) {
cursor, err := mc.Collection(coll).Find(mc.ctx, bson.M{"$text": bson.M{"$search": text}}, opts...)
if err != nil {
return nil, err
@ -376,7 +376,7 @@ func (mc MongoClient) SearchText(coll CollectionName, text string, opts ...*opti
return output, nil
}
func (mc MongoClient) FindAll(coll CollectionName, filter bson.M, opts ...*options.FindOptions) ([]bson.M, error) {
func (mc *MongoClient) FindAll(coll CollectionName, filter bson.M, opts ...*options.FindOptions) ([]bson.M, error) {
cursor, err := mc.Collection(coll).Find(mc.ctx, filter, opts...)
if err != nil {
return nil, err
@ -392,7 +392,7 @@ func (mc MongoClient) FindAll(coll CollectionName, filter bson.M, opts ...*optio
return output, nil
}
func (mc MongoClient) FindAllAs(coll CollectionName, filter bson.M, output interface{}, opts ...*options.FindOptions) error {
func (mc *MongoClient) FindAllAs(coll CollectionName, filter bson.M, output interface{}, opts ...*options.FindOptions) error {
cursor, err := mc.Collection(coll).Find(mc.ctx, filter, opts...)
if err != nil {
return err
@ -406,7 +406,7 @@ func (mc MongoClient) FindAllAs(coll CollectionName, filter bson.M, output inter
return nil
}
func (mc MongoClient) MakeExpireIndex(coll CollectionName, expireSeconds int32) error {
func (mc *MongoClient) MakeExpireIndex(coll CollectionName, expireSeconds int32) error {
matchcoll := mc.Collection(coll)
indices, err := matchcoll.Indexes().List(mc.ctx, options.ListIndexes().SetMaxTime(time.Second))
if err != nil {
@ -461,7 +461,7 @@ IndexSearchLabel:
return err
}
func (mc MongoClient) makeIndicesWithOption(coll CollectionName, indices map[string]bson.D, opts ...*options.IndexOptions) error {
func (mc *MongoClient) makeIndicesWithOption(coll CollectionName, indices map[string]bson.D, opts ...*options.IndexOptions) error {
collection := mc.Collection(coll)
cursor, err := collection.Indexes().List(mc.ctx, options.ListIndexes().SetMaxTime(time.Second))
if err != nil {
@ -507,10 +507,10 @@ func (mc MongoClient) makeIndicesWithOption(coll CollectionName, indices map[str
return nil
}
func (mc MongoClient) MakeUniqueIndices(coll CollectionName, indices map[string]bson.D, opts ...*options.IndexOptions) error {
func (mc *MongoClient) MakeUniqueIndices(coll CollectionName, indices map[string]bson.D, opts ...*options.IndexOptions) error {
return mc.makeIndicesWithOption(coll, indices, append(opts, options.Index().SetUnique(true))...)
}
func (mc MongoClient) MakeIndices(coll CollectionName, indices map[string]bson.D, opts ...*options.IndexOptions) error {
func (mc *MongoClient) MakeIndices(coll CollectionName, indices map[string]bson.D, opts ...*options.IndexOptions) error {
return mc.makeIndicesWithOption(coll, indices, opts...)
}