You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.6 KiB
Go

package db
import (
"context"
"database/sql"
"time"
"git.cheetah.cat/archivinator-ng/workerclient/common"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/pgdialect"
"github.com/uptrace/bun/driver/pgdriver"
)
var (
BunDB *bun.DB
ctx context.Context
)
type Request struct {
bun.BaseModel `bun:"table:requests"`
ID int64 `bun:"id,pk"`
Name string `bun:"column:name,notnull"`
Type string `bun:"column:type,notnull"`
URL string `bun:"column:url"`
Flags []string `bun:"column:flags"`
DefaultQuality string `bun:"column:defaultquality"`
Tags []Tag `bun:"m2m:tag_to_request,join:Tag=Request"`
Active bool `bun:"column:chipselect"`
Interval int `bun:"column:interval"`
LastCheck time.Time `bun:"column:lastcheck"`
CreatedAt time.Time `bun:"createdat"`
}
type Video struct {
bun.BaseModel `bun:"table:video"`
ID int64 `bun:"id,pk"`
Type string `bun:"column:type,notnull"`
Title string `bun:"column:title,notnull"`
Description string `bun:"column:description"`
IsLivestream bool `bun:"column:livestream"`
Duration int `bun:"column:duration"`
ChannelName string `bun:"column:channel_name"`
Tags []Tag `bun:"m2m:tag_to_videos,join:Tag=Video"`
ArchiveDate time.Time `bun:"archive_date"`
UploadDate time.Time `bun:"upload_date"`
}
type Tag struct {
bun.BaseModel `bun:"table:tags"`
ID int64 `bun:"id,pk"`
Name string `bun:"column:name,notnull"`
}
type TagToVideo struct {
bun.BaseModel `bun:"table:tag_to_videos"`
TagID int64 `bun:",pk"`
Tag *Tag `bun:"rel:belongs-to,join:tag_id=id"`
VideoID int64 `bun:",pk"`
Video *Video `bun:"rel:belongs-to,join:video_id=id"`
}
type TagToRequest struct {
bun.BaseModel `bun:"table:tag_to_request"`
TagID int64 `bun:",pk"`
Tag *Tag `bun:"rel:belongs-to,join:tag_id=id"`
RequestID int64 `bun:",pk"`
Request *Request `bun:"rel:belongs-to,join:request_id=id"`
}
func Initialize() {
ctx = context.Background()
// Open a PostgreSQL database.
dsn := common.WorkerSettings.PostgresURL
pgdb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(dsn)))
// Create a Bun db on top of it.
BunDB = bun.NewDB(pgdb, pgdialect.New())
// Print all queries to stdout.
//BunDB.AddQueryHook(bundebug.NewQueryHook(bundebug.WithVerbose(true)))
// Register M2M Tables
BunDB.RegisterModel((*TagToVideo)(nil))
BunDB.RegisterModel((*TagToRequest)(nil))
//BunDB.ResetModel(context.Background(), (*TagToVideo)(nil), (*TagToRequest)(nil))
}