/vendor/github.com/KyleBanks/go-kit/orm/orm.go

https://github.com/KyleBanks/goggles · Go · 135 lines · 82 code · 24 blank · 29 comment · 15 complexity · b18646ea47b8330ffe66c907ecdc11f0 MD5 · raw file

  1. // Package orm manages access to a database, including ORM-like functionality.
  2. //
  3. // The package wraps the GORM library, which can then be potentially swapped out with
  4. // minimal changes.
  5. package orm
  6. import (
  7. "github.com/KyleBanks/go-kit/log"
  8. // Required to initialize the mysql driver.
  9. _ "github.com/go-sql-driver/mysql"
  10. "github.com/jinzhu/gorm"
  11. // Required to initialize the sqlite driver.
  12. _ "github.com/jinzhu/gorm/dialects/sqlite"
  13. )
  14. var (
  15. // ErrRecordNotFound is the error returned when trying to load a record
  16. // that cannot be found.
  17. ErrRecordNotFound = gorm.ErrRecordNotFound
  18. )
  19. // ORM is a container for the underlying database connection.
  20. type ORM struct {
  21. conn *gorm.DB
  22. }
  23. // Model is a type added to domain models that will provide ORM functionality.
  24. type Model struct {
  25. gorm.Model
  26. }
  27. // Open creates a database connection, or returns an existing one if present.
  28. func (orm *ORM) Open(dialect, connectionString string) (*gorm.DB, error) {
  29. if orm.conn != nil {
  30. return orm.conn, nil
  31. }
  32. db, err := gorm.Open(dialect, connectionString)
  33. if err != nil {
  34. return nil, err
  35. }
  36. // Configure
  37. // TODO: Accept options as a param to Open
  38. db.SetLogger(log.Logger)
  39. db.LogMode(true)
  40. db.DB().SetMaxIdleConns(10)
  41. db.DB().SetMaxOpenConns(0) // Unlimited
  42. orm.conn = db
  43. return orm.conn, nil
  44. }
  45. // AutoMigrate performs database migration for all Model types provided.
  46. func (orm ORM) AutoMigrate(models []interface{}) error {
  47. for _, model := range models {
  48. if err := orm.conn.AutoMigrate(model).Error; err != nil {
  49. return err
  50. }
  51. }
  52. return nil
  53. }
  54. // Exec performs a raw SQL query against the underlying database.
  55. func (orm ORM) Exec(query string, output interface{}) *gorm.DB {
  56. return orm.conn.Raw(query).Scan(output)
  57. }
  58. // Begin starts a new database transaction.
  59. func (orm ORM) Begin() *gorm.DB {
  60. return orm.conn.Begin()
  61. }
  62. // Where performs a query with "Where" parameters.
  63. func (orm ORM) Where(query interface{}, args ...interface{}) *gorm.DB {
  64. return orm.conn.Where(query, args...)
  65. }
  66. // Create inserts a new model instance into the database.
  67. func (orm ORM) Create(model interface{}) *gorm.DB {
  68. return orm.conn.Create(model)
  69. }
  70. // Save updates a model with the given attributes.
  71. func (orm ORM) Save(value interface{}) *gorm.DB {
  72. return orm.conn.Save(value)
  73. }
  74. // Model specifies the domain model that subsequent queries will be run against.
  75. func (orm ORM) Model(model interface{}) *gorm.DB {
  76. return orm.conn.Model(model)
  77. }
  78. // First returns the first model (ordered by ID) that matches the specified query.
  79. func (orm ORM) First(model interface{}, where ...interface{}) *gorm.DB {
  80. return orm.conn.First(model, where...)
  81. }
  82. // Last returns the last model (ordered by ID) that matches the specified query.
  83. func (orm ORM) Last(model interface{}, where ...interface{}) *gorm.DB {
  84. return orm.conn.Last(model, where...)
  85. }
  86. // ModelWithID returns an instance of the specified model with the given ID.
  87. func (orm ORM) ModelWithID(model interface{}, id uint) error {
  88. // First check if the Model exists.
  89. // We do this so that we can avoid an error returned by the ORM
  90. // when a query returns no results.
  91. if exists, err := orm.ModelExistsWithID(model, id); err != nil {
  92. return err
  93. } else if !exists {
  94. return nil
  95. }
  96. // It exists, so let's load it
  97. if err := orm.First(model, id).Error; err != nil {
  98. return err
  99. }
  100. return nil
  101. }
  102. // ModelExistsWithID returns a boolean indicating if an instance of the
  103. // specified model exists with a given ID.
  104. func (orm ORM) ModelExistsWithID(model interface{}, id uint) (bool, error) {
  105. var count int64
  106. err := orm.Model(model).Where(id).Count(&count).Error
  107. if err != nil {
  108. return false, err
  109. }
  110. return count > 0, nil
  111. }