Database connection
This commit is contained in:
parent
1d2716ae1d
commit
cacb3db0fa
19
store.go
19
store.go
|
@ -14,11 +14,20 @@ import (
|
|||
)
|
||||
|
||||
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 {
|
||||
return d.db.Database("eeee").Collection(col)
|
||||
return d.client.Database(d.database).Collection(col)
|
||||
}
|
||||
|
||||
func getFilter(f *bson.D) bson.D {
|
||||
|
@ -45,12 +54,12 @@ func QueryFilter(q *url.Values) bson.D {
|
|||
return filter
|
||||
}
|
||||
|
||||
func NewStore[T Collectable](db *mongo.Client) Store[T] {
|
||||
func NewStore[T Collectable](db *Database) Store[T] {
|
||||
var item T
|
||||
coll := item.Coll()
|
||||
return Store[T]{
|
||||
Db: db,
|
||||
Coll: db.Database("instagetter").Collection(coll),
|
||||
Db: db.client,
|
||||
Coll: db.Collection(coll),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue