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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
// 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"
"github.com/google/uuid"
"entgo.io/ent"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
)
// Session holds the schema definition for the Session entity.
type Session struct {
ent.Schema
}
// Fields of the Session.
func (Session) Fields() []ent.Field {
return []ent.Field{
field.Bytes("id").
MaxLen(64).
GoType(ID{}).
DefaultFunc(NewID),
}
}
// Edges of the Session.
func (Session) Edges() []ent.Edge {
return []ent.Edge{
edge.From("device", Device.Type).
Ref("sessions").
Unique(),
}
}
// Device holds the schema definition for the Device entity.
type Device struct {
ent.Schema
}
// Fields of the Device.
func (Device) Fields() []ent.Field {
return []ent.Field{
field.Bytes("id").
MaxLen(64).
GoType(ID{}).
DefaultFunc(NewID),
}
}
// Edges of the Device.
func (Device) Edges() []ent.Edge {
return []ent.Edge{
edge.To("active_session", Session.Type).
Unique(),
edge.To("sessions", Session.Type),
}
}
type ID [64]byte
func NewID() ID {
var id [64]byte
copy(id[:], uuid.NewString()+uuid.NewString()+uuid.NewString()+uuid.NewString())
return id
}
func (i ID) String() string {
return string(i[:])
}
func (i *ID) Scan(v any) error {
switch v := v.(type) {
case []byte:
copy(i[:], v)
case string:
copy(i[:], v)
default:
return fmt.Errorf("unexpcted type: %T", v)
}
return nil
}
func (i ID) Value() (driver.Value, error) {
return string(i[:]), nil
}
|