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.

55 lines
1.1 KiB

4 years ago
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 (d *Database) ProjectGroups() models.ProjectGroupRepository {
  30. return &projectGroupRepository{db: d.db}
  31. }
  32. func Setup(ctx context.Context, connect string) (*Database, error) {
  33. db, err := sqlx.ConnectContext(ctx, "postgres", connect)
  34. if err != nil {
  35. return nil, err
  36. }
  37. err = db.PingContext(ctx)
  38. if err != nil {
  39. return nil, err
  40. }
  41. return &Database{db: db}, nil
  42. }