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.
28 lines
400 B
28 lines
400 B
package database |
|
|
|
import ( |
|
"sync" |
|
|
|
"gorm.io/gorm" |
|
) |
|
|
|
var ( |
|
db *gorm.DB |
|
mutex sync.RWMutex |
|
) |
|
|
|
func SetDatabase(d *gorm.DB) { |
|
db = d |
|
} |
|
|
|
func ReadDB[T any](callback func(*gorm.DB) (T, error)) (T, error) { |
|
mutex.RLock() |
|
defer mutex.RUnlock() |
|
return callback(db) |
|
} |
|
|
|
func WriteDB[T any](callback func(*gorm.DB) (T, error)) (T, error) { |
|
mutex.Lock() |
|
defer mutex.Unlock() |
|
return callback(db) |
|
}
|
|
|