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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
// Copyright 2019-present Facebook Inc. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
package schema
import (
"database/sql/driver"
"fmt"
"entgo.io/ent"
"entgo.io/ent/dialect"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
"github.com/google/uuid"
"ariga.io/atlas/sql/postgres"
)
// Doc holds the schema definition for the Doc entity.
type Doc struct {
ent.Schema
}
// Fields of the Doc.
func (Doc) Fields() []ent.Field {
return []ent.Field{
field.String("id").
GoType(DocID("")).
MaxLen(36).
NotEmpty().
Unique().
Immutable().
DefaultFunc(func() DocID {
return DocID(uuid.NewString())
}).
SchemaType(map[string]string{
dialect.Postgres: postgres.TypeUUID,
}),
field.String("text").
Optional(),
}
}
// Edges of the Doc.
func (Doc) Edges() []ent.Edge {
return []ent.Edge{
edge.To("children", Doc.Type).
From("parent").
Unique(),
edge.To("related", Doc.Type),
}
}
type DocID string
// Scan implements the Scanner interface.
func (s *DocID) Scan(value any) (err error) {
switch v := value.(type) {
case nil:
case []byte:
*s = DocID(v)
case string:
*s = DocID(v)
default:
err = fmt.Errorf("unexpected type %T", v)
}
return
}
// Value implements the driver Valuer interface.
func (s DocID) Value() (driver.Value, error) {
return string(s), nil
}
|