48 lines
1.0 KiB
Go
48 lines
1.0 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"go.mongodb.org/mongo-driver/v2/mongo"
|
|
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
|
)
|
|
|
|
// Client wraps the mongo client and exposes typed collection accessors.
|
|
type Client struct {
|
|
client *mongo.Client
|
|
db *mongo.Database
|
|
}
|
|
|
|
func Connect(ctx context.Context, uri string) (*Client, error) {
|
|
opts := options.Client().ApplyURI(uri)
|
|
c, err := mongo.Connect(opts)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
pingCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
|
defer cancel()
|
|
if err := c.Ping(pingCtx, nil); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &Client{client: c, db: c.Database("logjensticks")}, nil
|
|
}
|
|
|
|
func (c *Client) Disconnect(ctx context.Context) error {
|
|
return c.client.Disconnect(ctx)
|
|
}
|
|
|
|
func (c *Client) Users() *mongo.Collection {
|
|
return c.db.Collection("users")
|
|
}
|
|
|
|
func (c *Client) Sessions() *mongo.Collection {
|
|
return c.db.Collection("sessions")
|
|
}
|
|
|
|
func (c *Client) Lanes() *mongo.Collection {
|
|
return c.db.Collection("lanes")
|
|
}
|