~bigbes/sr-ht-dolt

ref: 12c7ff77f8281cd0ca61177bcf07b9c031a04c97 sr-ht-dolt/graph/model/database.go -rw-r--r-- 1.8 KiB
12c7ff77 — Eugene Blikh ci: publish this build's own coverage and benchmarks 2 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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}
}