core/database/agent.go
2024-07-31 20:14:46 +07:00

31 lines
746 B
Go

package database
import (
"context"
"time"
"github.com/uptrace/bun"
)
type Agent struct {
ID int64 `bun:",pk,autoincrement"`
Hostname string `bun:",notnull" json:"hostname"`
IP string `bun:",notnull"`
IsOnline bool `bun:",notnull,default:true" json:"isOnline"`
LastOnline time.Time `json:"lastOnline"`
CreatedAt time.Time `bun:",notnull,default:current_timestamp" json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
var _ bun.BeforeAppendModelHook = (*Agent)(nil)
func (s *Agent) BeforeAppendModel(ctx context.Context, query bun.Query) error {
switch query.(type) {
case *bun.InsertQuery:
s.LastOnline = time.Now()
case *bun.UpdateQuery:
s.UpdatedAt = time.Now()
}
return nil
}