Files
ent/entc/integration/ent/schema/file.go
Ariel Mashraki 6ed99b93e5 entc/integration: add edges-only index
Summary: Pull Request resolved: https://github.com/facebookincubator/ent/pull/153

Reviewed By: idoshveki

Differential Revision: D18419861

fbshipit-source-id: af7a07eacc601d0db54a5512f6edf26d725f5f7e
2019-11-10 02:56:34 -08:00

66 lines
1.5 KiB
Go

// 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 (
"math"
"github.com/facebookincubator/ent"
"github.com/facebookincubator/ent/schema/edge"
"github.com/facebookincubator/ent/schema/field"
"github.com/facebookincubator/ent/schema/index"
)
// File holds the schema definition for the File entity.
type File struct {
ent.Schema
}
// Fields of the File.
func (File) Fields() []ent.Field {
return []ent.Field{
field.Int("size").
StorageKey("fsize").
Default(math.MaxInt32).
Positive(),
field.String("name"),
field.String("user").
Optional().
Nillable(),
field.String("group").
Optional(),
}
}
// Edges of the File.
func (File) Edges() []ent.Edge {
return []ent.Edge{
edge.From("owner", User.Type).
Ref("files").
Unique(),
edge.From("type", FileType.Type).
Ref("files").
Unique(),
}
}
// Indexes of a file.
func (File) Indexes() []ent.Index {
return []ent.Index{
// non-unique index should not prevent duplicates.
index.Fields("name", "size"),
// unique index prevents duplicates records.
index.Fields("name", "user").
Unique(),
// index on edges only.
index.Edges("owner", "type"),
// unique index under the "owner" sub-tree.
// user/owner can't have files with duplicate names.
index.Fields("name").
Edges("owner", "type").
Unique(),
}
}