Fixed schemas

This commit is contained in:
cdricms
2025-01-15 09:29:18 +01:00
parent 354f8cc02e
commit d18245736f
2 changed files with 40 additions and 22 deletions

View File

@@ -1,9 +1,14 @@
package main
import (
"context"
"database/sql"
"time"
"github.com/google/uuid"
"github.com/uptrace/bun"
"time"
"github.com/uptrace/bun/dialect/pgdialect"
"github.com/uptrace/bun/driver/pgdriver"
)
type Role string
@@ -16,7 +21,7 @@ const (
type User struct {
bun.BaseModel `bun:"table:users"`
ID uuid.UUID `bun:"type:uuid,pk,default:gen_random_uuid()"`
UserID uuid.UUID `bun:"type:uuid,pk,default:gen_random_uuid()"`
FirstName string `bun:"firstname,notnull"`
LastName string `bun:"lastname,notnull"`
Email string `bun:"email,unique,notnull"`
@@ -24,6 +29,8 @@ type User struct {
Role Role `bun:"role,notnull,default:'user'"`
CreatedAt time.Time `bun:"created_at,default:current_timestamp"`
UpdatedAt time.Time `bun:"updated_at,default:current_timestamp"`
Events []Event `bun:"m2m:events_to_users,join:User=Event"`
Articles []*Blog `bun:"rel:has-many,join:user_id=blog_id"`
}
type Event struct {
@@ -36,35 +43,30 @@ type Event struct {
Status string `bun:"status,notnull"`
}
type EventsToUsers struct {
type EventToUser struct {
bun.BaseModel `bun:"table:events_to_users"`
EventID uuid.UUID `bun:"type:uuid,notnull"`
UserID uuid.UUID `bun:"type:uuid,notnull"`
EventID uuid.UUID `bun:"type:uuid,pk"`
UserID uuid.UUID `bun:"type:uuid,pk"`
Event *Event `bun:"rel:belongs_to,join:event_id=event_id"`
User *User `bun:"rel:belongs_to,join:user_id=user_id"`
PrimaryKey struct {
EventID uuid.UUID `bun:"pk"`
UserID uuid.UUID `bun:"pk"`
}
Event *Event `bun:"rel:belongs-to,join:event_id=event_id"`
User *User `bun:"rel:belongs-to,join:user_id=user_id"`
}
type Blog struct {
bun.BaseModel `bun:"table:blogs"`
UUID uuid.UUID `bun:"type:uuid,pk"`
BlogID uuid.UUID `bun:"type:uuid,pk"`
Slug string `bun:"slug,unique,notnull"`
Content string `bun:"content,notnull"`
Label string `bun:"label"`
AuthorID uuid.UUID `bun:"author,notnull"`
AuthorID uuid.UUID `bun:"author_id,notnull"`
Published time.Time `bun:"published,default:current_timestamp"`
Summary string `bun:"summary"`
Image string `bun:"image"`
Href string `bun:"href"`
Author *User `bun:"rel:belongs_to,join:author=author_id"`
Author *User `bun:"rel:belongs-to,join:author_id=user_id"`
}
type WebsiteSettings struct {
@@ -73,3 +75,20 @@ type WebsiteSettings struct {
ID uuid.UUID `bun:"type:uuid,pk,default:gen_random_uuid()"`
AutoAcceptDemand bool `bun:"auto_accept_demand,default:false"`
}
func InitDatabase(dsn DSN) (*bun.DB, error) {
sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(dsn.ToString())))
db := bun.NewDB(sqldb, pgdialect.New())
db.RegisterModel((*EventToUser)(nil))
_, err := db.NewCreateTable().Model((*User)(nil)).IfNotExists().Exec(context.Background())
_, err = db.NewCreateTable().Model((*Event)(nil)).IfNotExists().Exec(context.Background())
_, err = db.NewCreateTable().Model((*EventToUser)(nil)).IfNotExists().Exec(context.Background())
_, err = db.NewCreateTable().Model((*Blog)(nil)).IfNotExists().Exec(context.Background())
_, err = db.NewCreateTable().Model((*WebsiteSettings)(nil)).IfNotExists().Exec(context.Background())
if err != nil {
return nil, err
}
return db, nil
}