Messaging/pkg/internal/database/source.go

32 lines
776 B
Go
Raw Normal View History

2024-03-26 15:05:13 +00:00
package database
import (
2024-11-02 05:23:27 +00:00
"fmt"
2024-11-02 05:24:37 +00:00
"git.solsynth.dev/hypernet/messaging/pkg/internal/gap"
2024-11-02 05:23:27 +00:00
"git.solsynth.dev/hypernet/nexus/pkg/nex/cruda"
2024-03-26 15:05:13 +00:00
"github.com/rs/zerolog/log"
"github.com/samber/lo"
"github.com/spf13/viper"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
var C *gorm.DB
2024-11-02 05:23:27 +00:00
func NewGorm() error {
dsn, err := cruda.NewCrudaConn(gap.Nx).AllocDatabase("messaging")
if err != nil {
return fmt.Errorf("failed to alloc database from nexus: %v", err)
}
2024-03-26 15:05:13 +00:00
2024-11-02 05:23:27 +00:00
C, err = gorm.Open(postgres.Open(dsn), &gorm.Config{Logger: logger.New(&log.Logger, logger.Config{
2024-03-26 15:05:13 +00:00
Colorful: true,
IgnoreRecordNotFoundError: true,
LogLevel: lo.Ternary(viper.GetBool("debug.database"), logger.Info, logger.Silent),
})})
return err
2024-11-02 05:23:27 +00:00
2024-03-26 15:05:13 +00:00
}