package model
import (
"time"
"sourcecraft.dev/bigbes/sr-ht-dolt/core"
)
// Database is hand-written rather than generated for one reason: the fields it
// does NOT carry. branches, log, tables and defaultBranch each open the bare
// store on disk, and acl is a second query against Postgres — as generated
// struct fields they would be computed for every database in a listing, whether
// or not the query asked for them. Absent here, gqlgen makes them field
// resolvers, so `{ databases { results { name visibility } } }` opens nothing.
//
// Repo is what those resolvers need and the schema never exposes: the row's id
// for an ACL lookup and its on-disk path for a browse session.
type Database struct {
ID int
Name string
Description string
Visibility Visibility
Created time.Time
Updated time.Time
Owner *User
Repo *core.Repo
}
// NewDatabase adapts a metadata row to the schema's type. The visibility enum
// is the same string the column holds, so it is converted and not mapped: a
// value the schema does not declare would be a value the database does not
// hold either.
func NewDatabase(repo *core.Repo) *Database {
return &Database{
ID: repo.ID,
Name: repo.Name,
Description: repo.Description,
Visibility: Visibility(repo.Visibility),
Created: repo.Created,
Updated: repo.Updated,
Owner: NewUser(repo.OwnerName),
Repo: repo,
}
}
// NewUser builds the schema's account type from a username with no leading "~".
// Both spellings are carried because both are asked for: the bare name
// addresses the account in a query and in a clone URL, and the canonical one is
// how SourceHut writes it everywhere else.
func NewUser(username string) *User {
if username == "" {
return nil
}
return &User{Username: username, CanonicalName: "~" + username}
}