Skip to content

Commit

Permalink
tests: add edgeschema example
Browse files Browse the repository at this point in the history
  • Loading branch information
troypoulter committed Jul 18, 2023
1 parent 733eb5b commit 01049fd
Show file tree
Hide file tree
Showing 19 changed files with 987 additions and 6 deletions.
5 changes: 0 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,3 @@
go.work

bin/

# Ignore any ent generated code in examples
examples/*
!examples/*/*.md
!examples/*/schema/*
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ example.start:
example.m2m2types:
go run main.go -s ./examples/m2m2types/schema -t ./examples/m2m2types/readme.md -o markdown

example.all: example.readme example.start example.m2m2types
example.edgeschema:
go run main.go -s ./examples/edgeschema/schema -t ./examples/edgeschema/readme.md -o markdown

example.all: example.readme example.start example.m2m2types example.edgeschema

build:
go build -o ./bin/entmaid
Expand Down
34 changes: 34 additions & 0 deletions examples/edgeschema/readme-expected.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Edge Schema

Schema taken from: <https://github.com/ent/ent/tree/master/entc/integration/edgeschema>

## Schema

> **Note**
>
> The following schema was generated by `entmaid`.
<!-- #start:entmaid -->
```mermaid
erDiagram
Group {
int id PK
string name
}
group_users {
int group_id PK,FK
int user_id PK,FK
}
User {
int id PK
int age
string name
}
Group |o--o{ group_users : users-groups
User |o--o{ group_users : groups-users
```
<!-- #end:entmaid -->
197 changes: 197 additions & 0 deletions examples/edgeschema/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
# Edge Schema

Schema taken from: <https://github.com/ent/ent/tree/master/entc/integration/edgeschema>

## Schema

> **Note**
>
> The following schema was generated by `entmaid`.
<!-- #start:entmaid -->
```mermaid
erDiagram
AttachedFile {
int id PK
time-Time attach_time
int f_id
int proc_id
}
File {
int id PK
string name
}
Friendship {
int id PK
int weight
time-Time created_at
int user_id
int friend_id
}
Group {
int id PK
string name
}
GroupTag {
int id PK
int tag_id
int group_id
}
Process {
int id PK
}
attached_files {
int proc_id PK,FK
int f_id PK,FK
}
Relationship {
int weight
int user_id
int relative_id
int info_id
}
RelationshipInfo {
int id PK
string text
}
Role {
int id PK
string name
time-Time created_at
}
RoleUser {
time-Time created_at
int role_id
int user_id
}
Tag {
int id PK
string value
}
tweet_tags {
int tag_id PK,FK
int tweet_id PK,FK
}
group_tags {
int tag_id PK,FK
int group_id PK,FK
}
Tweet {
int id PK
string text
}
TweetLike {
time-Time liked_at
int user_id
int tweet_id
}
TweetTag {
uuid-UUID id PK
time-Time added_at
int tag_id
int tweet_id
}
User {
int id PK
string name
}
user_groups {
int user_id PK,FK
int group_id PK,FK
}
friendships {
int user_id PK,FK
int friend_id PK,FK
}
relationships {
int user_id PK,FK
int relative_id PK,FK
}
tweet_likes {
int user_id PK,FK
int tweet_id PK,FK
}
user_tweets {
int user_id PK,FK
int tweet_id PK,FK
}
role_users {
int user_id PK,FK
int role_id PK,FK
}
UserGroup {
int id PK
time-Time joined_at
int user_id
int group_id
}
UserTweet {
int id PK
time-Time created_at
int user_id
int tweet_id
}
AttachedFile }o--o| File : fi
AttachedFile }o--o| Process : proc
File |o--o{ attached_files : processes-files
Friendship }o--o| User : user
Friendship }o--o| User : friend
Group |o--o{ user_groups : users-groups
Group |o--o{ group_tags : tags-groups
GroupTag }o--o| Tag : tag
GroupTag }o--o| Group : group
Process |o--o{ attached_files : files-processes
Relationship }o--o| User : user
Relationship }o--o| User : relative
Relationship }o--o| RelationshipInfo : info
Role |o--o{ role_users : user-roles
RoleUser }o--o| Role : role
RoleUser }o--o| User : user
Tag |o--o{ tweet_tags : tweets-tags
Tag |o--o{ group_tags : groups-tags
Tweet |o--o{ tweet_likes : liked_users-liked_tweets
Tweet |o--o{ user_tweets : user-tweets
Tweet |o--o{ tweet_tags : tags-tweets
TweetLike }o--o| Tweet : tweet
TweetLike }o--o| User : user
TweetTag }o--o| Tag : tag
TweetTag }o--o| Tweet : tweet
User |o--o{ user_groups : groups-users
User |o--o{ friendships : friends
User |o--o{ relationships : relatives
User |o--o{ tweet_likes : liked_tweets-liked_users
User |o--o{ user_tweets : tweets-user
User |o--o{ role_users : roles-user
UserGroup }o--o| User : user
UserGroup }o--o| Group : group
UserTweet }o--o| User : user
UserTweet }o--o| Tweet : tweet
```
<!-- #end:entmaid -->
66 changes: 66 additions & 0 deletions examples/edgeschema/schema/friendship.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// 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.
//
// Code has been copied from the ent examples folder to demonstrate common schema patterns.
// You can find the original code here: https://github.com/ent/ent/tree/master/entc/integration/edgeschema

package schema

import (
"time"

"entgo.io/ent"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
"entgo.io/ent/schema/index"
)

// Friendship holds the edge schema definition of the Friendship relationship.
type Friendship struct {
ent.Schema
}

// 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"),
// By default, Ent generates a unique index named <T>_<FK1>_<FK2>
// for edge-schemas with an ID field to enforce the uniqueness of
// the edges reside in the join table. However, in this case it is
// skipped because we define it explicitly in the definition below.
index.Fields("user_id", "friend_id").
Unique().
StorageKey("friendships_edge"),
}
}
39 changes: 39 additions & 0 deletions examples/edgeschema/schema/group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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.
//
// Code has been copied from the ent examples folder to demonstrate common schema patterns.
// You can find the original code here: https://github.com/ent/ent/tree/master/entc/integration/edgeschema

package schema

import (
"entgo.io/ent"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
)

// Group holds the schema definition for the Group entity.
type Group struct {
ent.Schema
}

// 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{
edge.From("users", User.Type).
Ref("groups").
Through("joined_users", UserGroup.Type),
edge.From("tags", Tag.Type).
Ref("groups").
Through("group_tags", GroupTag.Type),
}
}
Loading

0 comments on commit 01049fd

Please sign in to comment.