feat(schema): restore complete Ent schema files

This commit is contained in:
2025-11-07 09:03:43 +01:00
parent 1f8c2626dc
commit 0912f0f81b
14 changed files with 553 additions and 9 deletions

59
ent/schema/audit_log.go Normal file
View File

@@ -0,0 +1,59 @@
// Package schema defines the Ent schema for domain entities.
package schema
import (
"time"
"entgo.io/ent"
"entgo.io/ent/schema/field"
"entgo.io/ent/schema/index"
)
// AuditLog holds the schema definition for the AuditLog entity.
type AuditLog struct {
ent.Schema
}
// Fields of the AuditLog.
func (AuditLog) Fields() []ent.Field {
return []ent.Field{
field.String("id").
Unique().
Immutable(),
field.String("user_id").
NotEmpty().
Comment("ID of the user/actor performing the action"),
field.String("action").
NotEmpty().
Comment("Action performed (e.g., user.create, user.update)"),
field.String("resource").
Optional().
Comment("Resource type (e.g., user, role)"),
field.String("resource_id").
Optional().
Comment("ID of the target resource"),
field.String("ip_address").
Optional().
Comment("IP address of the client"),
field.String("user_agent").
Optional().
Comment("User agent of the client"),
field.JSON("metadata", map[string]interface{}{}).
Optional().
Comment("Additional metadata as JSON"),
field.Time("timestamp").
Default(time.Now).
Immutable(),
}
}
// Indexes of the AuditLog.
func (AuditLog) Indexes() []ent.Index {
return []ent.Index{
index.Fields("user_id"),
index.Fields("resource_id"),
index.Fields("timestamp"),
index.Fields("action"),
index.Fields("resource"),
}
}

View File

@@ -1,6 +1,10 @@
package schema package schema
import "entgo.io/ent" import (
"entgo.io/ent"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
)
// Permission holds the schema definition for the Permission entity. // Permission holds the schema definition for the Permission entity.
type Permission struct { type Permission struct {
@@ -9,10 +13,20 @@ type Permission struct {
// Fields of the Permission. // Fields of the Permission.
func (Permission) Fields() []ent.Field { func (Permission) Fields() []ent.Field {
return nil return []ent.Field{
field.String("id").
Unique().
Immutable(),
field.String("name").
Unique().
NotEmpty().
Comment("Format: module.resource.action"),
}
} }
// Edges of the Permission. // Edges of the Permission.
func (Permission) Edges() []ent.Edge { func (Permission) Edges() []ent.Edge {
return nil return []ent.Edge{
edge.To("role_permissions", RolePermission.Type),
}
} }

View File

@@ -0,0 +1,44 @@
package schema
import (
"time"
"entgo.io/ent"
"entgo.io/ent/schema/field"
"entgo.io/ent/schema/index"
)
// RefreshToken holds the schema definition for the RefreshToken entity.
type RefreshToken struct {
ent.Schema
}
// Fields of the RefreshToken.
func (RefreshToken) Fields() []ent.Field {
return []ent.Field{
field.String("id").
Unique().
Immutable(),
field.String("user_id").
NotEmpty().
Comment("ID of the user who owns this refresh token"),
field.String("token_hash").
NotEmpty().
Sensitive().
Comment("SHA256 hash of the refresh token"),
field.Time("expires_at").
Comment("When the refresh token expires"),
field.Time("created_at").
Default(time.Now).
Immutable(),
}
}
// Indexes of the RefreshToken.
func (RefreshToken) Indexes() []ent.Index {
return []ent.Index{
index.Fields("user_id"),
index.Fields("token_hash"),
index.Fields("expires_at"),
}
}

View File

@@ -1,6 +1,12 @@
package schema package schema
import "entgo.io/ent" import (
"time"
"entgo.io/ent"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
)
// Role holds the schema definition for the Role entity. // Role holds the schema definition for the Role entity.
type Role struct { type Role struct {
@@ -9,10 +15,25 @@ type Role struct {
// Fields of the Role. // Fields of the Role.
func (Role) Fields() []ent.Field { func (Role) Fields() []ent.Field {
return nil return []ent.Field{
field.String("id").
Unique().
Immutable(),
field.String("name").
Unique().
NotEmpty(),
field.String("description").
Optional(),
field.Time("created_at").
Default(time.Now).
Immutable(),
}
} }
// Edges of the Role. // Edges of the Role.
func (Role) Edges() []ent.Edge { func (Role) Edges() []ent.Edge {
return nil return []ent.Edge{
edge.To("role_permissions", RolePermission.Type),
edge.To("user_roles", UserRole.Type),
}
} }

View File

@@ -0,0 +1,34 @@
package schema
import (
"entgo.io/ent"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
)
// RolePermission holds the schema definition for the RolePermission entity (many-to-many relationship).
type RolePermission struct {
ent.Schema
}
// Fields of the RolePermission.
func (RolePermission) Fields() []ent.Field {
return []ent.Field{
field.String("role_id"),
field.String("permission_id"),
}
}
// Edges of the RolePermission.
func (RolePermission) Edges() []ent.Edge {
return []ent.Edge{
edge.To("role", Role.Type).
Unique().
Required().
Field("role_id"),
edge.To("permission", Permission.Type).
Unique().
Required().
Field("permission_id"),
}
}

View File

@@ -1,6 +1,12 @@
package schema package schema
import "entgo.io/ent" import (
"time"
"entgo.io/ent"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
)
// User holds the schema definition for the User entity. // User holds the schema definition for the User entity.
type User struct { type User struct {
@@ -9,10 +15,43 @@ type User struct {
// Fields of the User. // Fields of the User.
func (User) Fields() []ent.Field { func (User) Fields() []ent.Field {
return nil return []ent.Field{
field.String("id").
Unique().
Immutable(),
field.String("email").
Unique().
NotEmpty(),
field.String("username").
Optional(),
field.String("first_name").
Optional(),
field.String("last_name").
Optional(),
field.String("password_hash").
NotEmpty(),
field.Bool("verified").
Default(false),
field.String("email_verification_token").
Optional().
Sensitive(),
field.String("password_reset_token").
Optional().
Sensitive(),
field.Time("password_reset_expires_at").
Optional(),
field.Time("created_at").
Default(time.Now).
Immutable(),
field.Time("updated_at").
Default(time.Now).
UpdateDefault(time.Now),
}
} }
// Edges of the User. // Edges of the User.
func (User) Edges() []ent.Edge { func (User) Edges() []ent.Edge {
return nil return []ent.Edge{
edge.To("user_roles", UserRole.Type),
}
} }

34
ent/schema/user_role.go Normal file
View File

@@ -0,0 +1,34 @@
package schema
import (
"entgo.io/ent"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
)
// UserRole holds the schema definition for the UserRole entity (many-to-many relationship).
type UserRole struct {
ent.Schema
}
// Fields of the UserRole.
func (UserRole) Fields() []ent.Field {
return []ent.Field{
field.String("user_id"),
field.String("role_id"),
}
}
// Edges of the UserRole.
func (UserRole) Edges() []ent.Edge {
return []ent.Edge{
edge.To("user", User.Type).
Unique().
Required().
Field("user_id"),
edge.To("role", Role.Type).
Unique().
Required().
Field("role_id"),
}
}

View File

@@ -0,0 +1,59 @@
// Package schema defines the Ent schema for domain entities.
package schema
import (
"time"
"entgo.io/ent"
"entgo.io/ent/schema/field"
"entgo.io/ent/schema/index"
)
// AuditLog holds the schema definition for the AuditLog entity.
type AuditLog struct {
ent.Schema
}
// Fields of the AuditLog.
func (AuditLog) Fields() []ent.Field {
return []ent.Field{
field.String("id").
Unique().
Immutable(),
field.String("user_id").
NotEmpty().
Comment("ID of the user/actor performing the action"),
field.String("action").
NotEmpty().
Comment("Action performed (e.g., user.create, user.update)"),
field.String("resource").
Optional().
Comment("Resource type (e.g., user, role)"),
field.String("resource_id").
Optional().
Comment("ID of the target resource"),
field.String("ip_address").
Optional().
Comment("IP address of the client"),
field.String("user_agent").
Optional().
Comment("User agent of the client"),
field.JSON("metadata", map[string]interface{}{}).
Optional().
Comment("Additional metadata as JSON"),
field.Time("timestamp").
Default(time.Now).
Immutable(),
}
}
// Indexes of the AuditLog.
func (AuditLog) Indexes() []ent.Index {
return []ent.Index{
index.Fields("user_id"),
index.Fields("resource_id"),
index.Fields("timestamp"),
index.Fields("action"),
index.Fields("resource"),
}
}

View File

@@ -0,0 +1,32 @@
package schema
import (
"entgo.io/ent"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
)
// Permission holds the schema definition for the Permission entity.
type Permission struct {
ent.Schema
}
// Fields of the Permission.
func (Permission) Fields() []ent.Field {
return []ent.Field{
field.String("id").
Unique().
Immutable(),
field.String("name").
Unique().
NotEmpty().
Comment("Format: module.resource.action"),
}
}
// Edges of the Permission.
func (Permission) Edges() []ent.Edge {
return []ent.Edge{
edge.To("role_permissions", RolePermission.Type),
}
}

View File

@@ -0,0 +1,44 @@
package schema
import (
"time"
"entgo.io/ent"
"entgo.io/ent/schema/field"
"entgo.io/ent/schema/index"
)
// RefreshToken holds the schema definition for the RefreshToken entity.
type RefreshToken struct {
ent.Schema
}
// Fields of the RefreshToken.
func (RefreshToken) Fields() []ent.Field {
return []ent.Field{
field.String("id").
Unique().
Immutable(),
field.String("user_id").
NotEmpty().
Comment("ID of the user who owns this refresh token"),
field.String("token_hash").
NotEmpty().
Sensitive().
Comment("SHA256 hash of the refresh token"),
field.Time("expires_at").
Comment("When the refresh token expires"),
field.Time("created_at").
Default(time.Now).
Immutable(),
}
}
// Indexes of the RefreshToken.
func (RefreshToken) Indexes() []ent.Index {
return []ent.Index{
index.Fields("user_id"),
index.Fields("token_hash"),
index.Fields("expires_at"),
}
}

View File

@@ -0,0 +1,39 @@
package schema
import (
"time"
"entgo.io/ent"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
)
// Role holds the schema definition for the Role entity.
type Role struct {
ent.Schema
}
// Fields of the Role.
func (Role) Fields() []ent.Field {
return []ent.Field{
field.String("id").
Unique().
Immutable(),
field.String("name").
Unique().
NotEmpty(),
field.String("description").
Optional(),
field.Time("created_at").
Default(time.Now).
Immutable(),
}
}
// Edges of the Role.
func (Role) Edges() []ent.Edge {
return []ent.Edge{
edge.To("role_permissions", RolePermission.Type),
edge.To("user_roles", UserRole.Type),
}
}

View File

@@ -0,0 +1,34 @@
package schema
import (
"entgo.io/ent"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
)
// RolePermission holds the schema definition for the RolePermission entity (many-to-many relationship).
type RolePermission struct {
ent.Schema
}
// Fields of the RolePermission.
func (RolePermission) Fields() []ent.Field {
return []ent.Field{
field.String("role_id"),
field.String("permission_id"),
}
}
// Edges of the RolePermission.
func (RolePermission) Edges() []ent.Edge {
return []ent.Edge{
edge.To("role", Role.Type).
Unique().
Required().
Field("role_id"),
edge.To("permission", Permission.Type).
Unique().
Required().
Field("permission_id"),
}
}

View File

@@ -0,0 +1,57 @@
package schema
import (
"time"
"entgo.io/ent"
"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("id").
Unique().
Immutable(),
field.String("email").
Unique().
NotEmpty(),
field.String("username").
Optional(),
field.String("first_name").
Optional(),
field.String("last_name").
Optional(),
field.String("password_hash").
NotEmpty(),
field.Bool("verified").
Default(false),
field.String("email_verification_token").
Optional().
Sensitive(),
field.String("password_reset_token").
Optional().
Sensitive(),
field.Time("password_reset_expires_at").
Optional(),
field.Time("created_at").
Default(time.Now).
Immutable(),
field.Time("updated_at").
Default(time.Now).
UpdateDefault(time.Now),
}
}
// Edges of the User.
func (User) Edges() []ent.Edge {
return []ent.Edge{
edge.To("user_roles", UserRole.Type),
}
}

View File

@@ -0,0 +1,34 @@
package schema
import (
"entgo.io/ent"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
)
// UserRole holds the schema definition for the UserRole entity (many-to-many relationship).
type UserRole struct {
ent.Schema
}
// Fields of the UserRole.
func (UserRole) Fields() []ent.Field {
return []ent.Field{
field.String("user_id"),
field.String("role_id"),
}
}
// Edges of the UserRole.
func (UserRole) Edges() []ent.Edge {
return []ent.Edge{
edge.To("user", User.Type).
Unique().
Required().
Field("user_id"),
edge.To("role", Role.Type).
Unique().
Required().
Field("role_id"),
}
}