File: schema-def.md

package info (click to toggle)
golang-github-facebook-ent 0.5.4-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 14,284 kB
  • sloc: javascript: 349; makefile: 8
file content (68 lines) | stat: -rwxr-xr-x 1,565 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
---
id: schema-def
title: Introduction
---

## Quick Summary

Schema describes the definition of one entity type in the graph, like `User` or `Group`,
and can contain the following configurations:
- Entity fields (or properties), like: name or age of a `User`.
- Entity edges (or relations), like: `User`'s groups, or `User`'s friends.
- Database specific options, like: indexes or unique indexes.

<br/>
Here's an example of a schema:

```go
package schema

import (
	"github.com/facebook/ent"
	"github.com/facebook/ent/schema/field"
	"github.com/facebook/ent/schema/edge"
	"github.com/facebook/ent/schema/index"
)

type User struct {
	ent.Schema
}

func (User) Fields() []ent.Field {
	return []ent.Field{
		field.Int("age"),
		field.String("name"),
		field.String("nickname").
			Unique(),
	}
}

func (User) Edges() []ent.Edge {
	return []ent.Edge{
		edge.To("groups", Group.Type),
		edge.To("friends", User.Type),
	}
}

func (User) Index() []ent.Index {
	return []ent.Index{
		index.Fields("age", "name").
			Unique(),
	}
}
```

Entity schemas are usually stored inside `ent/schema` directory under
the root directory of your project, and can be generated by `entc` as follows:

```console
go run github.com/facebook/ent/cmd/ent init User Group
```

## It's Just Another ORM

If you are used to the definition of relations over edges, that's fine.
The modeling is the same. You can model with `ent` whatever you can model
with other traditional ORMs.
There are many examples in this website that can help you get started
in the [Edges](schema-edges.md) section.