Created
July 8, 2022 04:10
-
-
Save tabvn/ba890eff77febfd60eda50cd5e8574e5 to your computer and use it in GitHub Desktop.
database.go
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
package tracking | |
import ( | |
"context" | |
) | |
type InsertOneResult struct { | |
InsertedID interface{} `bson:"insertedId"` | |
} | |
type Collection interface { | |
InsertOne(ctx context.Context, document interface{}) (*InsertOneResult, error) | |
UpdateOne(ctx context.Context, filter interface{}, update interface{}) (interface{}, error) | |
DeleteOne(ctx context.Context, filter interface{}) (interface{}, error) | |
FindOne(ctx context.Context, filter interface{}) (interface{}, error) | |
Find(ctx context.Context, filter interface{}) ([]interface{}, error) | |
} | |
type Database interface { | |
Collection(name string) Collection | |
} | |
// mongodb driver mongo.go | |
package tracking | |
import ( | |
"context" | |
"go.mongodb.org/mongo-driver/mongo" | |
) | |
type mongoDB struct { | |
db *mongo.Database | |
} | |
type mongoCollection struct { | |
name string | |
col *mongo.Collection | |
} | |
func (m *mongoCollection) DeleteOne(ctx context.Context, filter interface{}) (interface{}, error) { | |
//TODO implement me | |
panic("implement me") | |
} | |
func (m *mongoCollection) InsertOne(ctx context.Context, document interface{}) (*InsertOneResult, error) { | |
res, err := m.col.InsertOne(ctx, document) | |
if err != nil { | |
return nil, err | |
} | |
return &InsertOneResult{InsertedID: res.InsertedID}, nil | |
} | |
func (m *mongoCollection) UpdateOne(ctx context.Context, filter interface{}, update interface{}) (interface{}, error) { | |
return m.col.UpdateOne(ctx, filter, update) | |
} | |
func newMongoDB(db *mongo.Database) *mongoDB { | |
return &mongoDB{ | |
db: db, | |
} | |
} | |
func (m *mongoCollection) FindOne(ctx context.Context, filter interface{}) (interface{}, error) { | |
//TODO implement me | |
panic("implement me") | |
} | |
func (m *mongoCollection) Find(ctx context.Context, filter interface{}) ([]interface{}, error) { | |
//TODO implement me | |
panic("implement me") | |
} | |
func (m *mongoDB) Collection(name string) Collection { | |
return &mongoCollection{ | |
name: name, | |
col: m.db.Collection(name), | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment