2024-09-13 09:58:29 +00:00
|
|
|
// TODO: Unify function names
|
|
|
|
|
|
|
|
// Storage is the handler for cache and db access
|
|
|
|
// It handles storing various data in the database as well as caching that data
|
|
|
|
// Said data includes notes, accounts, metadata about media files, servers and similar
|
2024-05-31 09:54:39 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2024-09-12 06:56:57 +00:00
|
|
|
"errors"
|
|
|
|
|
2024-09-15 10:24:36 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2024-09-12 06:56:57 +00:00
|
|
|
"gitlab.com/mstarongitlab/linstrom/storage/cache"
|
2024-05-31 09:54:39 +00:00
|
|
|
"gorm.io/driver/postgres"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
2024-05-31 15:21:29 +00:00
|
|
|
// Storage is responsible for all database, cache and media related actions
|
|
|
|
// and serves as the lowest layer of the cake
|
2024-05-31 09:54:39 +00:00
|
|
|
type Storage struct {
|
2024-09-12 06:56:57 +00:00
|
|
|
db *gorm.DB
|
|
|
|
cache *cache.Cache
|
2024-05-31 09:54:39 +00:00
|
|
|
}
|
|
|
|
|
2024-09-12 06:56:57 +00:00
|
|
|
var ErrInvalidData = errors.New("invalid data")
|
|
|
|
|
2024-09-15 10:24:36 +00:00
|
|
|
func NewStorage(dbUrl string, cache *cache.Cache) (*Storage, error) {
|
|
|
|
db, err := gorm.Open(postgres.Open(dbUrl), &gorm.Config{
|
|
|
|
Logger: newGormLogger(log.Logger),
|
|
|
|
})
|
2024-05-31 09:54:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-09-15 10:24:36 +00:00
|
|
|
err = db.AutoMigrate(
|
2024-09-13 07:18:05 +00:00
|
|
|
MediaMetadata{},
|
2024-08-28 15:20:38 +00:00
|
|
|
Account{},
|
|
|
|
RemoteServer{},
|
|
|
|
Note{},
|
|
|
|
Role{},
|
|
|
|
PasskeySession{},
|
2024-09-15 10:24:36 +00:00
|
|
|
InboundJob{},
|
|
|
|
OutboundJob{},
|
2024-05-31 15:21:29 +00:00
|
|
|
)
|
2024-09-12 06:56:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-05-31 15:21:29 +00:00
|
|
|
|
2024-09-15 10:24:36 +00:00
|
|
|
return &Storage{db, cache}, nil
|
2024-05-31 09:54:39 +00:00
|
|
|
}
|