| 1 |
|
package model |
| 2 |
|
|
| 3 |
|
import ( |
| 4 |
|
"time" |
| 5 |
|
|
| 6 |
|
"sourcecraft.dev/bigbes/sr-ht-dolt/core" |
| 7 |
|
) |
| 8 |
|
|
| 9 |
|
// Database is hand-written rather than generated for one reason: the fields it |
| 10 |
|
// does NOT carry. branches, log, tables and defaultBranch each open the bare |
| 11 |
|
// store on disk, and acl is a second query against Postgres — as generated |
| 12 |
|
// struct fields they would be computed for every database in a listing, whether |
| 13 |
|
// or not the query asked for them. Absent here, gqlgen makes them field |
| 14 |
|
// resolvers, so `{ databases { results { name visibility } } }` opens nothing. |
| 15 |
|
// |
| 16 |
|
// Repo is what those resolvers need and the schema never exposes: the row's id |
| 17 |
|
// for an ACL lookup and its on-disk path for a browse session. |
| 18 |
|
type Database struct { |
| 19 |
|
ID int |
| 20 |
|
Name string |
| 21 |
|
Description string |
| 22 |
|
Visibility Visibility |
| 23 |
|
Created time.Time |
| 24 |
|
Updated time.Time |
| 25 |
|
Owner *User |
| 26 |
|
|
| 27 |
|
Repo *core.Repo |
| 28 |
|
} |
| 29 |
|
|
| 30 |
|
// NewDatabase adapts a metadata row to the schema's type. The visibility enum |
| 31 |
|
// is the same string the column holds, so it is converted and not mapped: a |
| 32 |
|
// value the schema does not declare would be a value the database does not |
| 33 |
|
// hold either. |
| 34 |
0 |
func NewDatabase(repo *core.Repo) *Database { |
| 35 |
0 |
return &Database{ |
| 36 |
0 |
ID: repo.ID, |
| 37 |
0 |
Name: repo.Name, |
| 38 |
0 |
Description: repo.Description, |
| 39 |
0 |
Visibility: Visibility(repo.Visibility), |
| 40 |
0 |
Created: repo.Created, |
| 41 |
0 |
Updated: repo.Updated, |
| 42 |
0 |
Owner: NewUser(repo.OwnerName), |
| 43 |
0 |
Repo: repo, |
| 44 |
0 |
} |
| 45 |
0 |
} |
| 46 |
|
|
| 47 |
|
// NewUser builds the schema's account type from a username with no leading "~". |
| 48 |
|
// Both spellings are carried because both are asked for: the bare name |
| 49 |
|
// addresses the account in a query and in a clone URL, and the canonical one is |
| 50 |
|
// how SourceHut writes it everywhere else. |
| 51 |
0 |
func NewUser(username string) *User { |
| 52 |
0 |
if username == "" { |
| 53 |
0 |
return nil |
| 54 |
0 |
} |
| 55 |
0 |
return &User{Username: username, CanonicalName: "~" + username} |
| 56 |
|
} |