snapshot of https://github.com/ent/ent.git (master, 2026-09-15; blobs > 131072B omitted)
This commit is contained in:
65
entc/integration/multischema/versioned/schema/friendship.go
Normal file
65
entc/integration/multischema/versioned/schema/friendship.go
Normal file
@@ -0,0 +1,65 @@
|
||||
// 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 (
|
||||
"time"
|
||||
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/schema/edge"
|
||||
"entgo.io/ent/schema/field"
|
||||
"entgo.io/ent/schema/index"
|
||||
)
|
||||
|
||||
// An example for an ent/schema that mixed in an ent/mixin to define default table schema (in the database).
|
||||
|
||||
// Friendship holds the edge schema definition of the Friendship relationship.
|
||||
type Friendship struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
// Mixin defines the schemas that mixed into this schema.
|
||||
func (Friendship) Mixin() []ent.Mixin {
|
||||
return []ent.Mixin{
|
||||
Mixin{},
|
||||
}
|
||||
}
|
||||
|
||||
// Fields of the Friendship.
|
||||
func (Friendship) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.Int("weight").
|
||||
Default(1),
|
||||
field.Time("created_at").
|
||||
Default(time.Now),
|
||||
field.Int("user_id").
|
||||
Immutable(),
|
||||
field.Int("friend_id").
|
||||
Immutable(),
|
||||
}
|
||||
}
|
||||
|
||||
// Edges of the Friendship.
|
||||
func (Friendship) Edges() []ent.Edge {
|
||||
return []ent.Edge{
|
||||
edge.To("user", User.Type).
|
||||
Unique().
|
||||
Required().
|
||||
Immutable().
|
||||
Field("user_id"),
|
||||
edge.To("friend", User.Type).
|
||||
Unique().
|
||||
Required().
|
||||
Immutable().
|
||||
Field("friend_id"),
|
||||
}
|
||||
}
|
||||
|
||||
// Indexes of the Friendship.
|
||||
func (Friendship) Indexes() []ent.Index {
|
||||
return []ent.Index{
|
||||
index.Fields("created_at"),
|
||||
}
|
||||
}
|
||||
34
entc/integration/multischema/versioned/schema/group.go
Normal file
34
entc/integration/multischema/versioned/schema/group.go
Normal file
@@ -0,0 +1,34 @@
|
||||
// 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 (
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/schema/edge"
|
||||
"entgo.io/ent/schema/field"
|
||||
)
|
||||
|
||||
// An example for an ent/schema that uses struct embedding to define default table schema (in the database).
|
||||
|
||||
// Group holds the schema definition for the Group entity.
|
||||
type Group struct {
|
||||
base
|
||||
}
|
||||
|
||||
// Fields of the Group.
|
||||
func (Group) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.String("name").
|
||||
Default("unknown"),
|
||||
}
|
||||
}
|
||||
|
||||
// Edges of the Group.
|
||||
func (Group) Edges() []ent.Edge {
|
||||
return []ent.Edge{
|
||||
// The edge schema (join table) is defaults to the edge owner schema (Group).
|
||||
edge.To("users", User.Type),
|
||||
}
|
||||
}
|
||||
44
entc/integration/multischema/versioned/schema/mixin.go
Normal file
44
entc/integration/multischema/versioned/schema/mixin.go
Normal file
@@ -0,0 +1,44 @@
|
||||
// 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 (
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/dialect/entsql"
|
||||
"entgo.io/ent/schema"
|
||||
"entgo.io/ent/schema/mixin"
|
||||
)
|
||||
|
||||
// This file contains two example of how to define the base schema.
|
||||
// 1. Use Mixin and use it in all schemas that reside in "db1".
|
||||
// 2. Create a "base" schema and use struct embedding to in all schemas that reside in "db1".
|
||||
|
||||
// Example 1:
|
||||
|
||||
// Mixin holds the default configuration for most schemas in this package.
|
||||
type Mixin struct {
|
||||
mixin.Schema
|
||||
}
|
||||
|
||||
// Annotations of the Mixin.
|
||||
func (Mixin) Annotations() []schema.Annotation {
|
||||
return []schema.Annotation{
|
||||
entsql.Schema("db1"),
|
||||
}
|
||||
}
|
||||
|
||||
// Example 2:
|
||||
|
||||
// Base schema.
|
||||
type base struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
// Annotations of the base schema.
|
||||
func (base) Annotations() []schema.Annotation {
|
||||
return []schema.Annotation{
|
||||
entsql.Schema("db1"),
|
||||
}
|
||||
}
|
||||
45
entc/integration/multischema/versioned/schema/pet.go
Normal file
45
entc/integration/multischema/versioned/schema/pet.go
Normal file
@@ -0,0 +1,45 @@
|
||||
// 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 (
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/dialect/entsql"
|
||||
"entgo.io/ent/schema"
|
||||
"entgo.io/ent/schema/edge"
|
||||
"entgo.io/ent/schema/field"
|
||||
)
|
||||
|
||||
// Pet holds the schema definition for the Pet entity.
|
||||
type Pet struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
// Fields of the Pet.
|
||||
func (Pet) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.String("name").
|
||||
Default("unknown"),
|
||||
field.Int("owner_id").
|
||||
Optional(),
|
||||
}
|
||||
}
|
||||
|
||||
// Edges of the Pet.
|
||||
func (Pet) Edges() []ent.Edge {
|
||||
return []ent.Edge{
|
||||
edge.From("owner", User.Type).
|
||||
Ref("pets").
|
||||
Field("owner_id").
|
||||
Unique(),
|
||||
}
|
||||
}
|
||||
|
||||
// Annotations of the Pet.
|
||||
func (Pet) Annotations() []schema.Annotation {
|
||||
return []schema.Annotation{
|
||||
entsql.Schema("db2"),
|
||||
}
|
||||
}
|
||||
50
entc/integration/multischema/versioned/schema/user.go
Normal file
50
entc/integration/multischema/versioned/schema/user.go
Normal file
@@ -0,0 +1,50 @@
|
||||
// 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 (
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/dialect/entsql"
|
||||
"entgo.io/ent/schema"
|
||||
"entgo.io/ent/schema/edge"
|
||||
"entgo.io/ent/schema/field"
|
||||
)
|
||||
|
||||
// User holds the schema definition for the User entity.
|
||||
type User struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
// Fields of the User.
|
||||
func (User) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.String("name").
|
||||
Default("unknown"),
|
||||
}
|
||||
}
|
||||
|
||||
// Edges of the User.
|
||||
func (User) Edges() []ent.Edge {
|
||||
return []ent.Edge{
|
||||
edge.To("pets", Pet.Type),
|
||||
edge.From("groups", Group.Type).
|
||||
Ref("users"),
|
||||
edge.To("friends", User.Type).
|
||||
Through("friendships", Friendship.Type),
|
||||
edge.To("following", User.Type).
|
||||
// An example for an edge that defines the schema of its join table.
|
||||
Annotations(
|
||||
entsql.Schema("db3"),
|
||||
).
|
||||
From("followers"),
|
||||
}
|
||||
}
|
||||
|
||||
// Annotations of the User.
|
||||
func (User) Annotations() []schema.Annotation {
|
||||
return []schema.Annotation{
|
||||
entsql.Schema("db3"),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user