Database connection

This commit is contained in:
nefrace 2023-01-20 16:31:45 +03:00
parent 1d2716ae1d
commit cacb3db0fa
1 changed files with 14 additions and 5 deletions

View File

@ -14,11 +14,20 @@ import (
) )
type Database struct { type Database struct {
db *mongo.Client database string
client *mongo.Client
}
func NewConnection(uri string, database string) (*Database, error) {
client, err := mongo.Connect(context.Background())
if err != nil {
return nil, err
}
return &Database{client: client, database: database}, nil
} }
func (d *Database) Collection(col string) *mongo.Collection { func (d *Database) Collection(col string) *mongo.Collection {
return d.db.Database("eeee").Collection(col) return d.client.Database(d.database).Collection(col)
} }
func getFilter(f *bson.D) bson.D { func getFilter(f *bson.D) bson.D {
@ -45,12 +54,12 @@ func QueryFilter(q *url.Values) bson.D {
return filter return filter
} }
func NewStore[T Collectable](db *mongo.Client) Store[T] { func NewStore[T Collectable](db *Database) Store[T] {
var item T var item T
coll := item.Coll() coll := item.Coll()
return Store[T]{ return Store[T]{
Db: db, Db: db.client,
Coll: db.Database("instagetter").Collection(coll), Coll: db.Collection(coll),
} }
} }