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.

107 lines
2.6 KiB

  1. package postgres
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "git.aiterp.net/rpdata/api/database/postgres/psqlcore"
  7. "git.aiterp.net/rpdata/api/internal/config"
  8. "git.aiterp.net/rpdata/api/repositories"
  9. "time"
  10. )
  11. func Connect(cfg config.Database) (*DB, error) {
  12. sslMode := "disable"
  13. if cfg.SSL {
  14. sslMode = "enable"
  15. }
  16. psqlInfo := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=%s",
  17. cfg.Host, cfg.Port, cfg.Username, cfg.Password, cfg.Db, sslMode,
  18. )
  19. timeout, cancel := context.WithTimeout(context.Background(), time.Second*15)
  20. defer cancel()
  21. db, err := sql.Open("postgres", psqlInfo)
  22. if err != nil {
  23. return nil, err
  24. }
  25. if err := db.PingContext(timeout); err != nil {
  26. return nil, err
  27. }
  28. db.SetMaxIdleConns(32)
  29. q := psqlcore.New(db)
  30. if err := q.EnsureCounter(timeout, "data_character_id"); err != nil {
  31. return nil, err
  32. }
  33. if err := q.EnsureCounter(timeout, "data_change_id"); err != nil {
  34. return nil, err
  35. }
  36. if err := q.EnsureCounter(timeout, "log_short_id"); err != nil {
  37. return nil, err
  38. }
  39. return &DB{
  40. db: db,
  41. insertWithIDs: cfg.RestoreIDs,
  42. }, nil
  43. }
  44. type DB struct {
  45. insertWithIDs bool
  46. db *sql.DB
  47. }
  48. func (d *DB) Changes() repositories.ChangeRepository {
  49. return &changeRepository{insertWithIDs: d.insertWithIDs, db: d.db}
  50. }
  51. func (d *DB) Channels() repositories.ChannelRepository {
  52. return &channelRepository{insertWithIDs: d.insertWithIDs, db: d.db}
  53. }
  54. func (d *DB) Characters() repositories.CharacterRepository {
  55. return &characterRepository{insertWithIDs: d.insertWithIDs, db: d.db}
  56. }
  57. func (d *DB) Tags() repositories.TagRepository {
  58. return &tagRepository{db: d.db}
  59. }
  60. func (d *DB) Logs() repositories.LogRepository {
  61. return &logRepository{insertWithIDs: d.insertWithIDs, db: d.db}
  62. }
  63. func (d *DB) Posts() repositories.PostRepository {
  64. return &postRepository{insertWithIDs: d.insertWithIDs, db: d.db}
  65. }
  66. func (d *DB) Stories() repositories.StoryRepository {
  67. return &storyRepository{insertWithIDs: d.insertWithIDs, db: d.db}
  68. }
  69. func (d *DB) Chapters() repositories.ChapterRepository {
  70. return &chapterRepository{insertWithIDs: d.insertWithIDs, db: d.db}
  71. }
  72. func (d *DB) Comments() repositories.CommentRepository {
  73. return &commentRepository{insertWithIDs: d.insertWithIDs, db: d.db}
  74. }
  75. func (d *DB) Keys() repositories.KeyRepository {
  76. return &keyRepository{insertWithIDs: d.insertWithIDs, db: d.db}
  77. }
  78. func (d *DB) Users() repositories.UserRepository {
  79. return &userRepository{db: d.db}
  80. }
  81. func (d *DB) Files() repositories.FileRepository {
  82. panic("implement me")
  83. }
  84. func (d *DB) Close(context.Context) error {
  85. return d.db.Close()
  86. }