You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
971 B

4 years ago
  1. package postgres
  2. import (
  3. "context"
  4. "github.com/gissleh/stufflog/models"
  5. "github.com/jmoiron/sqlx"
  6. _ "github.com/lib/pq"
  7. )
  8. type Database struct {
  9. db *sqlx.DB
  10. }
  11. func (d *Database) Goals() models.GoalRepository {
  12. return &goalRepository{db: d.db}
  13. }
  14. func (d *Database) Groups() models.GroupRepository {
  15. return &groupRepository{db: d.db}
  16. }
  17. func (d *Database) Items() models.ItemRepository {
  18. return &itemRepository{db: d.db}
  19. }
  20. func (d *Database) Logs() models.LogRepository {
  21. return &logRepository{db: d.db}
  22. }
  23. func (d *Database) Projects() models.ProjectRepository {
  24. return &projectRepository{db: d.db}
  25. }
  26. func (d *Database) Tasks() models.TaskRepository {
  27. return &taskRepository{db: d.db}
  28. }
  29. func Setup(ctx context.Context, connect string) (*Database, error) {
  30. db, err := sqlx.ConnectContext(ctx, "postgres", connect)
  31. if err != nil {
  32. return nil, err
  33. }
  34. err = db.PingContext(ctx)
  35. if err != nil {
  36. return nil, err
  37. }
  38. return &Database{db: db}, nil
  39. }