GraphQL API and utilities for the rpdata project
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.

43 lines
1.2 KiB

  1. package database
  2. import (
  3. "context"
  4. "errors"
  5. "git.aiterp.net/rpdata/api/database/postgres"
  6. "git.aiterp.net/rpdata/api/database/mongodb"
  7. "git.aiterp.net/rpdata/api/internal/config"
  8. "git.aiterp.net/rpdata/api/repositories"
  9. )
  10. // ErrDriverUnrecognized is returned if the driver is not recognized
  11. var ErrDriverUnrecognized = errors.New("driver not recognized, check installed version or your configuration")
  12. type Database interface {
  13. Changes() repositories.ChangeRepository
  14. Channels() repositories.ChannelRepository
  15. Characters() repositories.CharacterRepository
  16. Tags() repositories.TagRepository
  17. Logs() repositories.LogRepository
  18. Posts() repositories.PostRepository
  19. Stories() repositories.StoryRepository
  20. Chapters() repositories.ChapterRepository
  21. Comments() repositories.CommentRepository
  22. Keys() repositories.KeyRepository
  23. Users() repositories.UserRepository
  24. Files() repositories.FileRepository
  25. Close(ctx context.Context) error
  26. }
  27. // Init sets up the database.
  28. func Init(config config.Database) (Database, error) {
  29. switch config.Driver {
  30. case "mongo", "mgo", "mongodb":
  31. return mongodb.Init(config)
  32. case "postgres", "psql", "pq", "postgressql":
  33. return postgres.Connect(config)
  34. default:
  35. return nil, ErrDriverUnrecognized
  36. }
  37. }