| 1 |
// Copyright 2022 The Archivista Contributors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package schema |
| 16 |
|
| 17 |
import ( |
| 18 |
"time" |
| 19 |
|
| 20 |
"entgo.io/contrib/entgql" |
| 21 |
"entgo.io/ent" |
| 22 |
"entgo.io/ent/schema" |
| 23 |
"entgo.io/ent/schema/edge" |
| 24 |
"entgo.io/ent/schema/field" |
| 25 |
"entgo.io/ent/schema/index" |
| 26 |
"github.com/google/uuid" |
| 27 |
) |
| 28 |
|
| 29 |
// Subject represents subjects from an in-toto statement. |
| 30 |
type Subject struct { |
| 31 |
ent.Schema |
| 32 |
} |
| 33 |
|
| 34 |
// Fields of the Subject. |
| 35 |
func (Subject) Fields() []ent.Field { |
| 36 |
return []ent.Field{ |
| 37 |
field.UUID("id", uuid.UUID{}).Default(uuid.New).Immutable().Unique(), |
| 38 |
field.Time("created_at").Default(time.Now).Immutable().Optional().Nillable().Annotations(entgql.OrderField("CREATED_AT")), |
| 39 |
field.String("name").NotEmpty(), |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
// Edges of the Subject. |
| 44 |
func (Subject) Edges() []ent.Edge { |
| 45 |
return []ent.Edge{ |
| 46 |
edge.To("subject_digests", SubjectDigest.Type), |
| 47 |
|
| 48 |
edge.From("statement", Statement.Type). |
| 49 |
Ref("subjects").Unique(), |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
func (Subject) Indexes() []ent.Index { |
| 54 |
return []ent.Index{ |
| 55 |
// Index on the "name" field. |
| 56 |
index.Fields("name"), |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
func (Subject) Annotations() []schema.Annotation { |
| 61 |
return []schema.Annotation{ |
| 62 |
entgql.RelayConnection(), |
| 63 |
entgql.QueryField(), |
| 64 |
} |
| 65 |
} |