File: index.go

package info (click to toggle)
golang-entgo-ent 0.11.3-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 21,976 kB
  • sloc: javascript: 641; makefile: 8; sql: 2
file content (121 lines) | stat: -rw-r--r-- 3,205 bytes parent folder | download | duplicates (2)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// 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 index

import "entgo.io/ent/schema"

// A Descriptor for index configuration.
type Descriptor struct {
	Unique      bool                // unique index.
	Edges       []string            // edge columns.
	Fields      []string            // field columns.
	StorageKey  string              // custom index name.
	Annotations []schema.Annotation // index annotations.
}

// Builder for indexes on vertex columns and edges in the graph.
type Builder struct {
	desc *Descriptor
}

// Fields creates an index on the given vertex fields.
// Note that indexes are implemented only for SQL dialects, and does not support gremlin.
//
//	func (T) Indexes() []ent.Index {
//
//		// Unique index on 2 fields.
//		index.Fields("first", "last").
//			Unique(),
//
//		// Unique index of field under specific edge.
//		index.Fields("name").
//			Edges("parent").
//			Unique(),
//
//	}
//
func Fields(fields ...string) *Builder {
	return &Builder{desc: &Descriptor{Fields: fields}}
}

// Edges creates an index on the given vertex edge fields.
// Note that indexes are implemented only for SQL dialects, and does not support gremlin.
//
//	func (T) Indexes() []ent.Index {
//
//		// Unique index of field under 2 edges.
//		index.Fields("name").
//			Edges("parent", "type").
//			Unique(),
//
//	}
//
func Edges(edges ...string) *Builder {
	return &Builder{desc: &Descriptor{Edges: edges}}
}

// Fields sets the fields of the index.
//
//	func (T) Indexes() []ent.Index {
//
//		// Unique "name" and "age" fields under the "parent" edge.
//		index.Edges("parent").
//			Fields("name", "age").
//			Unique(),
//
//	}
func (b *Builder) Fields(fields ...string) *Builder {
	b.desc.Fields = fields
	return b
}

// Edges sets the fields index to be unique under the set of edges (sub-graph). For example:
//
//	func (T) Indexes() []ent.Index {
//
//		// Unique "name" field under the "parent" edge.
//		index.Fields("name").
//			Edges("parent").
//			Unique(),
//	}
//
func (b *Builder) Edges(edges ...string) *Builder {
	b.desc.Edges = edges
	return b
}

// Unique sets the index to be a unique index.
// Note that defining a uniqueness on optional fields won't prevent
// duplicates if one of the column contains NULL values.
func (b *Builder) Unique() *Builder {
	b.desc.Unique = true
	return b
}

// StorageKey sets the storage key of the index. In SQL dialects, it's the index name.
func (b *Builder) StorageKey(key string) *Builder {
	b.desc.StorageKey = key
	return b
}

// Annotations adds a list of annotations to the index object to be used by codegen extensions.
//
//	func (T) Indexes() []ent.Index {
//
//		// Partial index on name where the entity is not deleted.
//		index.Fields("name").
//			Annotations(entsql.Prefix(100))
//
//	}
//
func (b *Builder) Annotations(annotations ...schema.Annotation) *Builder {
	b.desc.Annotations = append(b.desc.Annotations, annotations...)
	return b
}

// Descriptor implements the ent.Descriptor interface.
func (b *Builder) Descriptor() *Descriptor {
	return b.desc
}