feat(schema): 添加基础混入定义公共时间字段

This commit is contained in:
unclesam committed 2026-07-07 19:43:16 +08:00
1 parent 5281459fcb
commit fe86c081f4
16 files changed
+1715 -14

No files matched your search

+2
View File
@@ -29,3 +29,5 @@ vendor/
# 环境变量 # 环境变量
.env .env
.env.local .env.local
tmp/
+7 -7
View File
@@ -2,16 +2,16 @@
# Web 服务配置 # Web 服务配置
server: server:
port: 8080 # 服务监听的端口 port: 8080 # 服务监听的端口
mode: "release" # 运行模式:debug (开发模式,带控制台彩色日志) / release (生产模式,只输出 JSON 日志) mode: "release" # 运行模式:debug (开发模式,带控制台彩色日志) / release (生产模式,只输出 JSON 日志)
database: database:
driver: "postgres" # 驱动,支持 mysql 或 postgres driver: "postgres" # 驱动,支持 mysql 或 postgres
host: "127.0.0.1" # 数据库主机地址 host: "127.0.0.1" # 数据库主机地址
port: 5432 # 端口号:MySQL 默认 3306,PostgreSQL 默认 5432 port: 5432 # 端口号:MySQL 默认 3306,PostgreSQL 默认 5432
user: "postgres" # 用户名 user: "unclesam" # 用户名
password: "123456" # 密码 password: "wangchan" # 密码
dbname: "test_db" # 数据库名 dbname: "test_db" # 数据库名
# Zap 日志配置 # Zap 日志配置
logger: logger:
+24
View File
@@ -18,12 +18,20 @@ github.com/bytedance/sonic v1.15.0 h1:/PXeWFaR5ElNcVE84U0dOHjiMHQOwNIx3K4ymzh/uS
github.com/bytedance/sonic v1.15.0/go.mod h1:tFkWrPz0/CUCLEF4ri4UkHekCIcdnkqXw9VduqpJh0k= github.com/bytedance/sonic v1.15.0/go.mod h1:tFkWrPz0/CUCLEF4ri4UkHekCIcdnkqXw9VduqpJh0k=
github.com/bytedance/sonic/loader v0.5.0 h1:gXH3KVnatgY7loH5/TkeVyXPfESoqSBSBEiDd5VjlgE= github.com/bytedance/sonic/loader v0.5.0 h1:gXH3KVnatgY7loH5/TkeVyXPfESoqSBSBEiDd5VjlgE=
github.com/bytedance/sonic/loader v0.5.0/go.mod h1:AR4NYCk5DdzZizZ5djGqQ92eEhCCcdf5x77udYiSJRo= github.com/bytedance/sonic/loader v0.5.0/go.mod h1:AR4NYCk5DdzZizZ5djGqQ92eEhCCcdf5x77udYiSJRo=
github.com/clipperhouse/displaywidth v0.6.2 h1:ZDpTkFfpHOKte4RG5O/BOyf3ysnvFswpyYrV7z2uAKo=
github.com/clipperhouse/displaywidth v0.6.2/go.mod h1:R+kHuzaYWFkTm7xoMmK1lFydbci4X2CicfbGstSGg0o=
github.com/clipperhouse/stringish v0.1.1 h1:+NSqMOr3GR6k1FdRhhnXrLfztGzuG+VuFDfatpWHKCs=
github.com/clipperhouse/stringish v0.1.1/go.mod h1:v/WhFtE1q0ovMta2+m+UbpZ+2/HEXNWYXQgCt4hdOzA=
github.com/clipperhouse/uax29/v2 v2.3.0 h1:SNdx9DVUqMoBuBoW3iLOj4FQv3dN5mDtuqwuhIGpJy4=
github.com/clipperhouse/uax29/v2 v2.3.0/go.mod h1:Wn1g7MK6OoeDT0vL+Q0SQLDz/KpfsVRgg6W7ihQeh4g=
github.com/cloudwego/base64x v0.1.6 h1:t11wG9AECkCDk5fMSoxmufanudBtJ+/HemLstXDLI2M= github.com/cloudwego/base64x v0.1.6 h1:t11wG9AECkCDk5fMSoxmufanudBtJ+/HemLstXDLI2M=
github.com/cloudwego/base64x v0.1.6/go.mod h1:OFcloc187FXDaYHvrNIjxSe8ncn0OOM8gEHfghB2IPU= github.com/cloudwego/base64x v0.1.6/go.mod h1:OFcloc187FXDaYHvrNIjxSe8ncn0OOM8gEHfghB2IPU=
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g= github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.10.1 h1:b0/UzAf9yR5rhf3RPm9gf3ehBPpf0oZKIjtpKrx59Ho= github.com/fsnotify/fsnotify v1.10.1 h1:b0/UzAf9yR5rhf3RPm9gf3ehBPpf0oZKIjtpKrx59Ho=
@@ -77,8 +85,12 @@ github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
github.com/lib/pq v1.12.3 h1:tTWxr2YLKwIvK90ZXEw8GP7UFHtcbTtty8zsI+YjrfQ= github.com/lib/pq v1.12.3 h1:tTWxr2YLKwIvK90ZXEw8GP7UFHtcbTtty8zsI+YjrfQ=
github.com/lib/pq v1.12.3/go.mod h1:/p+8NSbOcwzAEI7wiMXFlgydTwcgTr3OSKMsD2BitpA= github.com/lib/pq v1.12.3/go.mod h1:/p+8NSbOcwzAEI7wiMXFlgydTwcgTr3OSKMsD2BitpA=
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.19 h1:v++JhqYnZuu5jSKrk9RbgF5v4CGUjqRfBm05byFGLdw=
github.com/mattn/go-runewidth v0.0.19/go.mod h1:XBkDxAl56ILZc9knddidhrOlY5R/pDhgLpndooCuJAs=
github.com/mattn/go-sqlite3 v1.14.28 h1:ThEiQrnbtumT+QMknw63Befp/ce/nUPgBPMlRFEum7A= github.com/mattn/go-sqlite3 v1.14.28 h1:ThEiQrnbtumT+QMknw63Befp/ce/nUPgBPMlRFEum7A=
github.com/mattn/go-sqlite3 v1.14.28/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= github.com/mattn/go-sqlite3 v1.14.28/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0= github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0=
@@ -88,6 +100,14 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/olekukonko/cat v0.0.0-20250911104152-50322a0618f6 h1:zrbMGy9YXpIeTnGj4EljqMiZsIcE09mmF8XsD5AYOJc=
github.com/olekukonko/cat v0.0.0-20250911104152-50322a0618f6/go.mod h1:rEKTHC9roVVicUIfZK7DYrdIoM0EOr8mK1Hj5s3JjH0=
github.com/olekukonko/errors v1.1.0 h1:RNuGIh15QdDenh+hNvKrJkmxxjV4hcS50Db478Ou5sM=
github.com/olekukonko/errors v1.1.0/go.mod h1:ppzxA5jBKcO1vIpCXQ9ZqgDh8iwODz6OXIGKU8r5m4Y=
github.com/olekukonko/ll v0.1.4-0.20260115111900-9e59c2286df0 h1:jrYnow5+hy3WRDCBypUFvVKNSPPCdqgSXIE9eJDD8LM=
github.com/olekukonko/ll v0.1.4-0.20260115111900-9e59c2286df0/go.mod h1:b52bVQRRPObe+yyBl0TxNfhesL0nedD4Cht0/zx55Ew=
github.com/olekukonko/tablewriter v1.1.3 h1:VSHhghXxrP0JHl+0NnKid7WoEmd9/urKRJLysb70nnA=
github.com/olekukonko/tablewriter v1.1.3/go.mod h1:9VU0knjhmMkXjnMKrZ3+L2JhhtsQ/L38BbL3CRNE8tM=
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4= github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY= github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -157,11 +177,15 @@ golang.org/x/mod v0.32.0 h1:9F4d3PHLljb6x//jOyokMv3eX+YDeepZSEo3mFJy93c=
golang.org/x/mod v0.32.0/go.mod h1:SgipZ/3h2Ci89DlEtEXWUk/HteuRin+HHhN+WbNhguU= golang.org/x/mod v0.32.0/go.mod h1:SgipZ/3h2Ci89DlEtEXWUk/HteuRin+HHhN+WbNhguU=
golang.org/x/net v0.51.0 h1:94R/GTO7mt3/4wIKpcR5gkGmRLOuE/2hNGeWq/GBIFo= golang.org/x/net v0.51.0 h1:94R/GTO7mt3/4wIKpcR5gkGmRLOuE/2hNGeWq/GBIFo=
golang.org/x/net v0.51.0/go.mod h1:aamm+2QF5ogm02fjy5Bb7CQ0WMt1/WVM7FtyaTLlA9Y= golang.org/x/net v0.51.0/go.mod h1:aamm+2QF5ogm02fjy5Bb7CQ0WMt1/WVM7FtyaTLlA9Y=
golang.org/x/sync v0.19.0 h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4=
golang.org/x/sync v0.19.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.41.0 h1:Ivj+2Cp/ylzLiEU89QhWblYnOE9zerudt9Ftecq2C6k= golang.org/x/sys v0.41.0 h1:Ivj+2Cp/ylzLiEU89QhWblYnOE9zerudt9Ftecq2C6k=
golang.org/x/sys v0.41.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= golang.org/x/sys v0.41.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
golang.org/x/text v0.34.0 h1:oL/Qq0Kdaqxa1KbNeMKwQq0reLCCaFtqu2eNuSeNHbk= golang.org/x/text v0.34.0 h1:oL/Qq0Kdaqxa1KbNeMKwQq0reLCCaFtqu2eNuSeNHbk=
golang.org/x/text v0.34.0/go.mod h1:homfLqTYRFyVYemLBFl5GgL/DWEiH5wcsQ5gSh1yziA= golang.org/x/text v0.34.0/go.mod h1:homfLqTYRFyVYemLBFl5GgL/DWEiH5wcsQ5gSh1yziA=
golang.org/x/tools v0.41.0 h1:a9b8iMweWG+S0OBnlU36rzLp20z1Rp10w+IY2czHTQc=
golang.org/x/tools v0.41.0/go.mod h1:XSY6eDqxVNiYgezAVqqCeihT4j1U2CCsqvH3WhQpnlg=
google.golang.org/protobuf v1.36.10 h1:AYd7cD/uASjIL6Q9LiTjz8JLcrh/88q5UObnmY3aOOE= google.golang.org/protobuf v1.36.10 h1:AYd7cD/uASjIL6Q9LiTjz8JLcrh/88q5UObnmY3aOOE=
google.golang.org/protobuf v1.36.10/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco= google.golang.org/protobuf v1.36.10/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+5
View File
@@ -24,6 +24,11 @@ func Init(driver string, dsn string) error {
return fmt.Errorf("打开数据库连接失败 [%s]: %w", driver, err) return fmt.Errorf("打开数据库连接失败 [%s]: %w", driver, err)
} }
// 立即 Ping 数据库,验证账号密码及数据库是否存在
if err := db.Ping(); err != nil {
return fmt.Errorf("无法连接至数据库 [%s],请检查账号密码或数据库是否存在: %w", driver, err)
}
// 连接池设置 // 连接池设置
db.SetMaxIdleConns(10) db.SetMaxIdleConns(10)
db.SetMaxOpenConns(100) db.SetMaxOpenConns(100)
+7
View File
@@ -11,6 +11,13 @@ var (
// UsersColumns holds the columns for the "users" table. // UsersColumns holds the columns for the "users" table.
UsersColumns = []*schema.Column{ UsersColumns = []*schema.Column{
{Name: "id", Type: field.TypeInt, Increment: true}, {Name: "id", Type: field.TypeInt, Increment: true},
{Name: "created_at", Type: field.TypeTime},
{Name: "updated_at", Type: field.TypeTime},
{Name: "username", Type: field.TypeString, Unique: true},
{Name: "password", Type: field.TypeString},
{Name: "email", Type: field.TypeString, Nullable: true},
{Name: "ip", Type: field.TypeString, Nullable: true},
{Name: "addr", Type: field.TypeString, Nullable: true, Size: 500},
} }
// UsersTable holds the schema information for the "users" table. // UsersTable holds the schema information for the "users" table.
UsersTable = &schema.Table{ UsersTable = &schema.Table{
+450 -2
View File
@@ -4,10 +4,12 @@ package ent
import ( import (
"EnGin/internal/ent/predicate" "EnGin/internal/ent/predicate"
"EnGin/internal/ent/user"
"context" "context"
"errors" "errors"
"fmt" "fmt"
"sync" "sync"
"time"
"entgo.io/ent" "entgo.io/ent"
"entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql"
@@ -31,6 +33,13 @@ type UserMutation struct {
op Op op Op
typ string typ string
id *int id *int
created_at *time.Time
updated_at *time.Time
username *string
password *string
email *string
ip *string
addr *string
clearedFields map[string]struct{} clearedFields map[string]struct{}
done bool done bool
oldValue func(context.Context) (*User, error) oldValue func(context.Context) (*User, error)
@@ -135,6 +144,297 @@ func (m *UserMutation) IDs(ctx context.Context) ([]int, error) {
} }
} }
// SetCreatedAt sets the "created_at" field.
func (m *UserMutation) SetCreatedAt(t time.Time) {
m.created_at = &t
}
// CreatedAt returns the value of the "created_at" field in the mutation.
func (m *UserMutation) CreatedAt() (r time.Time, exists bool) {
v := m.created_at
if v == nil {
return
}
return *v, true
}
// OldCreatedAt returns the old "created_at" field's value of the User entity.
// If the User object wasn't provided to the builder, the object is fetched from the database.
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
func (m *UserMutation) OldCreatedAt(ctx context.Context) (v time.Time, err error) {
if !m.op.Is(OpUpdateOne) {
return v, errors.New("OldCreatedAt is only allowed on UpdateOne operations")
}
if m.id == nil || m.oldValue == nil {
return v, errors.New("OldCreatedAt requires an ID field in the mutation")
}
oldValue, err := m.oldValue(ctx)
if err != nil {
return v, fmt.Errorf("querying old value for OldCreatedAt: %w", err)
}
return oldValue.CreatedAt, nil
}
// ResetCreatedAt resets all changes to the "created_at" field.
func (m *UserMutation) ResetCreatedAt() {
m.created_at = nil
}
// SetUpdatedAt sets the "updated_at" field.
func (m *UserMutation) SetUpdatedAt(t time.Time) {
m.updated_at = &t
}
// UpdatedAt returns the value of the "updated_at" field in the mutation.
func (m *UserMutation) UpdatedAt() (r time.Time, exists bool) {
v := m.updated_at
if v == nil {
return
}
return *v, true
}
// OldUpdatedAt returns the old "updated_at" field's value of the User entity.
// If the User object wasn't provided to the builder, the object is fetched from the database.
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
func (m *UserMutation) OldUpdatedAt(ctx context.Context) (v time.Time, err error) {
if !m.op.Is(OpUpdateOne) {
return v, errors.New("OldUpdatedAt is only allowed on UpdateOne operations")
}
if m.id == nil || m.oldValue == nil {
return v, errors.New("OldUpdatedAt requires an ID field in the mutation")
}
oldValue, err := m.oldValue(ctx)
if err != nil {
return v, fmt.Errorf("querying old value for OldUpdatedAt: %w", err)
}
return oldValue.UpdatedAt, nil
}
// ResetUpdatedAt resets all changes to the "updated_at" field.
func (m *UserMutation) ResetUpdatedAt() {
m.updated_at = nil
}
// SetUsername sets the "username" field.
func (m *UserMutation) SetUsername(s string) {
m.username = &s
}
// Username returns the value of the "username" field in the mutation.
func (m *UserMutation) Username() (r string, exists bool) {
v := m.username
if v == nil {
return
}
return *v, true
}
// OldUsername returns the old "username" field's value of the User entity.
// If the User object wasn't provided to the builder, the object is fetched from the database.
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
func (m *UserMutation) OldUsername(ctx context.Context) (v string, err error) {
if !m.op.Is(OpUpdateOne) {
return v, errors.New("OldUsername is only allowed on UpdateOne operations")
}
if m.id == nil || m.oldValue == nil {
return v, errors.New("OldUsername requires an ID field in the mutation")
}
oldValue, err := m.oldValue(ctx)
if err != nil {
return v, fmt.Errorf("querying old value for OldUsername: %w", err)
}
return oldValue.Username, nil
}
// ResetUsername resets all changes to the "username" field.
func (m *UserMutation) ResetUsername() {
m.username = nil
}
// SetPassword sets the "password" field.
func (m *UserMutation) SetPassword(s string) {
m.password = &s
}
// Password returns the value of the "password" field in the mutation.
func (m *UserMutation) Password() (r string, exists bool) {
v := m.password
if v == nil {
return
}
return *v, true
}
// OldPassword returns the old "password" field's value of the User entity.
// If the User object wasn't provided to the builder, the object is fetched from the database.
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
func (m *UserMutation) OldPassword(ctx context.Context) (v string, err error) {
if !m.op.Is(OpUpdateOne) {
return v, errors.New("OldPassword is only allowed on UpdateOne operations")
}
if m.id == nil || m.oldValue == nil {
return v, errors.New("OldPassword requires an ID field in the mutation")
}
oldValue, err := m.oldValue(ctx)
if err != nil {
return v, fmt.Errorf("querying old value for OldPassword: %w", err)
}
return oldValue.Password, nil
}
// ResetPassword resets all changes to the "password" field.
func (m *UserMutation) ResetPassword() {
m.password = nil
}
// SetEmail sets the "email" field.
func (m *UserMutation) SetEmail(s string) {
m.email = &s
}
// Email returns the value of the "email" field in the mutation.
func (m *UserMutation) Email() (r string, exists bool) {
v := m.email
if v == nil {
return
}
return *v, true
}
// OldEmail returns the old "email" field's value of the User entity.
// If the User object wasn't provided to the builder, the object is fetched from the database.
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
func (m *UserMutation) OldEmail(ctx context.Context) (v string, err error) {
if !m.op.Is(OpUpdateOne) {
return v, errors.New("OldEmail is only allowed on UpdateOne operations")
}
if m.id == nil || m.oldValue == nil {
return v, errors.New("OldEmail requires an ID field in the mutation")
}
oldValue, err := m.oldValue(ctx)
if err != nil {
return v, fmt.Errorf("querying old value for OldEmail: %w", err)
}
return oldValue.Email, nil
}
// ClearEmail clears the value of the "email" field.
func (m *UserMutation) ClearEmail() {
m.email = nil
m.clearedFields[user.FieldEmail] = struct{}{}
}
// EmailCleared returns if the "email" field was cleared in this mutation.
func (m *UserMutation) EmailCleared() bool {
_, ok := m.clearedFields[user.FieldEmail]
return ok
}
// ResetEmail resets all changes to the "email" field.
func (m *UserMutation) ResetEmail() {
m.email = nil
delete(m.clearedFields, user.FieldEmail)
}
// SetIP sets the "ip" field.
func (m *UserMutation) SetIP(s string) {
m.ip = &s
}
// IP returns the value of the "ip" field in the mutation.
func (m *UserMutation) IP() (r string, exists bool) {
v := m.ip
if v == nil {
return
}
return *v, true
}
// OldIP returns the old "ip" field's value of the User entity.
// If the User object wasn't provided to the builder, the object is fetched from the database.
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
func (m *UserMutation) OldIP(ctx context.Context) (v string, err error) {
if !m.op.Is(OpUpdateOne) {
return v, errors.New("OldIP is only allowed on UpdateOne operations")
}
if m.id == nil || m.oldValue == nil {
return v, errors.New("OldIP requires an ID field in the mutation")
}
oldValue, err := m.oldValue(ctx)
if err != nil {
return v, fmt.Errorf("querying old value for OldIP: %w", err)
}
return oldValue.IP, nil
}
// ClearIP clears the value of the "ip" field.
func (m *UserMutation) ClearIP() {
m.ip = nil
m.clearedFields[user.FieldIP] = struct{}{}
}
// IPCleared returns if the "ip" field was cleared in this mutation.
func (m *UserMutation) IPCleared() bool {
_, ok := m.clearedFields[user.FieldIP]
return ok
}
// ResetIP resets all changes to the "ip" field.
func (m *UserMutation) ResetIP() {
m.ip = nil
delete(m.clearedFields, user.FieldIP)
}
// SetAddr sets the "addr" field.
func (m *UserMutation) SetAddr(s string) {
m.addr = &s
}
// Addr returns the value of the "addr" field in the mutation.
func (m *UserMutation) Addr() (r string, exists bool) {
v := m.addr
if v == nil {
return
}
return *v, true
}
// OldAddr returns the old "addr" field's value of the User entity.
// If the User object wasn't provided to the builder, the object is fetched from the database.
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
func (m *UserMutation) OldAddr(ctx context.Context) (v string, err error) {
if !m.op.Is(OpUpdateOne) {
return v, errors.New("OldAddr is only allowed on UpdateOne operations")
}
if m.id == nil || m.oldValue == nil {
return v, errors.New("OldAddr requires an ID field in the mutation")
}
oldValue, err := m.oldValue(ctx)
if err != nil {
return v, fmt.Errorf("querying old value for OldAddr: %w", err)
}
return oldValue.Addr, nil
}
// ClearAddr clears the value of the "addr" field.
func (m *UserMutation) ClearAddr() {
m.addr = nil
m.clearedFields[user.FieldAddr] = struct{}{}
}
// AddrCleared returns if the "addr" field was cleared in this mutation.
func (m *UserMutation) AddrCleared() bool {
_, ok := m.clearedFields[user.FieldAddr]
return ok
}
// ResetAddr resets all changes to the "addr" field.
func (m *UserMutation) ResetAddr() {
m.addr = nil
delete(m.clearedFields, user.FieldAddr)
}
// Where appends a list predicates to the UserMutation builder. // Where appends a list predicates to the UserMutation builder.
func (m *UserMutation) Where(ps ...predicate.User) { func (m *UserMutation) Where(ps ...predicate.User) {
m.predicates = append(m.predicates, ps...) m.predicates = append(m.predicates, ps...)
@@ -169,7 +469,28 @@ func (m *UserMutation) Type() string {
// order to get all numeric fields that were incremented/decremented, call // order to get all numeric fields that were incremented/decremented, call
// AddedFields(). // AddedFields().
func (m *UserMutation) Fields() []string { func (m *UserMutation) Fields() []string {
fields := make([]string, 0, 0) fields := make([]string, 0, 7)
if m.created_at != nil {
fields = append(fields, user.FieldCreatedAt)
}
if m.updated_at != nil {
fields = append(fields, user.FieldUpdatedAt)
}
if m.username != nil {
fields = append(fields, user.FieldUsername)
}
if m.password != nil {
fields = append(fields, user.FieldPassword)
}
if m.email != nil {
fields = append(fields, user.FieldEmail)
}
if m.ip != nil {
fields = append(fields, user.FieldIP)
}
if m.addr != nil {
fields = append(fields, user.FieldAddr)
}
return fields return fields
} }
@@ -177,6 +498,22 @@ func (m *UserMutation) Fields() []string {
// return value indicates that this field was not set, or was not defined in the // return value indicates that this field was not set, or was not defined in the
// schema. // schema.
func (m *UserMutation) Field(name string) (ent.Value, bool) { func (m *UserMutation) Field(name string) (ent.Value, bool) {
switch name {
case user.FieldCreatedAt:
return m.CreatedAt()
case user.FieldUpdatedAt:
return m.UpdatedAt()
case user.FieldUsername:
return m.Username()
case user.FieldPassword:
return m.Password()
case user.FieldEmail:
return m.Email()
case user.FieldIP:
return m.IP()
case user.FieldAddr:
return m.Addr()
}
return nil, false return nil, false
} }
@@ -184,6 +521,22 @@ func (m *UserMutation) Field(name string) (ent.Value, bool) {
// returned if the mutation operation is not UpdateOne, or the query to the // returned if the mutation operation is not UpdateOne, or the query to the
// database failed. // database failed.
func (m *UserMutation) OldField(ctx context.Context, name string) (ent.Value, error) { func (m *UserMutation) OldField(ctx context.Context, name string) (ent.Value, error) {
switch name {
case user.FieldCreatedAt:
return m.OldCreatedAt(ctx)
case user.FieldUpdatedAt:
return m.OldUpdatedAt(ctx)
case user.FieldUsername:
return m.OldUsername(ctx)
case user.FieldPassword:
return m.OldPassword(ctx)
case user.FieldEmail:
return m.OldEmail(ctx)
case user.FieldIP:
return m.OldIP(ctx)
case user.FieldAddr:
return m.OldAddr(ctx)
}
return nil, fmt.Errorf("unknown User field %s", name) return nil, fmt.Errorf("unknown User field %s", name)
} }
@@ -192,6 +545,55 @@ func (m *UserMutation) OldField(ctx context.Context, name string) (ent.Value, er
// type. // type.
func (m *UserMutation) SetField(name string, value ent.Value) error { func (m *UserMutation) SetField(name string, value ent.Value) error {
switch name { switch name {
case user.FieldCreatedAt:
v, ok := value.(time.Time)
if !ok {
return fmt.Errorf("unexpected type %T for field %s", value, name)
}
m.SetCreatedAt(v)
return nil
case user.FieldUpdatedAt:
v, ok := value.(time.Time)
if !ok {
return fmt.Errorf("unexpected type %T for field %s", value, name)
}
m.SetUpdatedAt(v)
return nil
case user.FieldUsername:
v, ok := value.(string)
if !ok {
return fmt.Errorf("unexpected type %T for field %s", value, name)
}
m.SetUsername(v)
return nil
case user.FieldPassword:
v, ok := value.(string)
if !ok {
return fmt.Errorf("unexpected type %T for field %s", value, name)
}
m.SetPassword(v)
return nil
case user.FieldEmail:
v, ok := value.(string)
if !ok {
return fmt.Errorf("unexpected type %T for field %s", value, name)
}
m.SetEmail(v)
return nil
case user.FieldIP:
v, ok := value.(string)
if !ok {
return fmt.Errorf("unexpected type %T for field %s", value, name)
}
m.SetIP(v)
return nil
case user.FieldAddr:
v, ok := value.(string)
if !ok {
return fmt.Errorf("unexpected type %T for field %s", value, name)
}
m.SetAddr(v)
return nil
} }
return fmt.Errorf("unknown User field %s", name) return fmt.Errorf("unknown User field %s", name)
} }
@@ -213,13 +615,25 @@ func (m *UserMutation) AddedField(name string) (ent.Value, bool) {
// the field is not defined in the schema, or if the type mismatched the field // the field is not defined in the schema, or if the type mismatched the field
// type. // type.
func (m *UserMutation) AddField(name string, value ent.Value) error { func (m *UserMutation) AddField(name string, value ent.Value) error {
switch name {
}
return fmt.Errorf("unknown User numeric field %s", name) return fmt.Errorf("unknown User numeric field %s", name)
} }
// ClearedFields returns all nullable fields that were cleared during this // ClearedFields returns all nullable fields that were cleared during this
// mutation. // mutation.
func (m *UserMutation) ClearedFields() []string { func (m *UserMutation) ClearedFields() []string {
return nil var fields []string
if m.FieldCleared(user.FieldEmail) {
fields = append(fields, user.FieldEmail)
}
if m.FieldCleared(user.FieldIP) {
fields = append(fields, user.FieldIP)
}
if m.FieldCleared(user.FieldAddr) {
fields = append(fields, user.FieldAddr)
}
return fields
} }
// FieldCleared returns a boolean indicating if a field with the given name was // FieldCleared returns a boolean indicating if a field with the given name was
@@ -232,12 +646,46 @@ func (m *UserMutation) FieldCleared(name string) bool {
// ClearField clears the value of the field with the given name. It returns an // ClearField clears the value of the field with the given name. It returns an
// error if the field is not defined in the schema. // error if the field is not defined in the schema.
func (m *UserMutation) ClearField(name string) error { func (m *UserMutation) ClearField(name string) error {
switch name {
case user.FieldEmail:
m.ClearEmail()
return nil
case user.FieldIP:
m.ClearIP()
return nil
case user.FieldAddr:
m.ClearAddr()
return nil
}
return fmt.Errorf("unknown User nullable field %s", name) return fmt.Errorf("unknown User nullable field %s", name)
} }
// ResetField resets all changes in the mutation for the field with the given name. // ResetField resets all changes in the mutation for the field with the given name.
// It returns an error if the field is not defined in the schema. // It returns an error if the field is not defined in the schema.
func (m *UserMutation) ResetField(name string) error { func (m *UserMutation) ResetField(name string) error {
switch name {
case user.FieldCreatedAt:
m.ResetCreatedAt()
return nil
case user.FieldUpdatedAt:
m.ResetUpdatedAt()
return nil
case user.FieldUsername:
m.ResetUsername()
return nil
case user.FieldPassword:
m.ResetPassword()
return nil
case user.FieldEmail:
m.ResetEmail()
return nil
case user.FieldIP:
m.ResetIP()
return nil
case user.FieldAddr:
m.ResetAddr()
return nil
}
return fmt.Errorf("unknown User field %s", name) return fmt.Errorf("unknown User field %s", name)
} }
+37
View File
@@ -2,8 +2,45 @@
package ent package ent
import (
"EnGin/internal/ent/schema"
"EnGin/internal/ent/user"
"time"
)
// The init function reads all schema descriptors with runtime code // The init function reads all schema descriptors with runtime code
// (default values, validators, hooks and policies) and stitches it // (default values, validators, hooks and policies) and stitches it
// to their package variables. // to their package variables.
func init() { func init() {
userMixin := schema.User{}.Mixin()
userMixinFields0 := userMixin[0].Fields()
_ = userMixinFields0
userFields := schema.User{}.Fields()
_ = userFields
// userDescCreatedAt is the schema descriptor for created_at field.
userDescCreatedAt := userMixinFields0[0].Descriptor()
// user.DefaultCreatedAt holds the default value on creation for the created_at field.
user.DefaultCreatedAt = userDescCreatedAt.Default.(func() time.Time)
// userDescUpdatedAt is the schema descriptor for updated_at field.
userDescUpdatedAt := userMixinFields0[1].Descriptor()
// user.DefaultUpdatedAt holds the default value on creation for the updated_at field.
user.DefaultUpdatedAt = userDescUpdatedAt.Default.(func() time.Time)
// user.UpdateDefaultUpdatedAt holds the default value on update for the updated_at field.
user.UpdateDefaultUpdatedAt = userDescUpdatedAt.UpdateDefault.(func() time.Time)
// userDescUsername is the schema descriptor for username field.
userDescUsername := userFields[0].Descriptor()
// user.UsernameValidator is a validator for the "username" field. It is called by the builders before save.
user.UsernameValidator = userDescUsername.Validators[0].(func(string) error)
// userDescEmail is the schema descriptor for email field.
userDescEmail := userFields[2].Descriptor()
// user.EmailValidator is a validator for the "email" field. It is called by the builders before save.
user.EmailValidator = userDescEmail.Validators[0].(func(string) error)
// userDescIP is the schema descriptor for ip field.
userDescIP := userFields[3].Descriptor()
// user.IPValidator is a validator for the "ip" field. It is called by the builders before save.
user.IPValidator = userDescIP.Validators[0].(func(string) error)
// userDescAddr is the schema descriptor for addr field.
userDescAddr := userFields[4].Descriptor()
// user.AddrValidator is a validator for the "addr" field. It is called by the builders before save.
user.AddrValidator = userDescAddr.Validators[0].(func(string) error)
} }
+29
View File
@@ -0,0 +1,29 @@
package schema
import (
"time"
"entgo.io/ent"
"entgo.io/ent/schema/field"
"entgo.io/ent/schema/mixin"
)
// BaseMixin 定义公共的基础混入
type BaseMixin struct {
mixin.Schema
}
// Fields 定义所有表都拥有的公共字段
func (BaseMixin) Fields() []ent.Field {
return []ent.Field{
// 创建时间:默认当前时间,不可修改(Immutable)
field.Time("created_at").
Default(time.Now).
Immutable(),
// 更新时间:默认当前时间,并且在每次数据 Update 时自动更新为当前时间
field.Time("updated_at").
Default(time.Now).
UpdateDefault(time.Now),
}
}
+28 -2
View File
@@ -1,15 +1,41 @@
package schema package schema
import "entgo.io/ent" import (
"regexp"
"entgo.io/ent"
"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 {
ent.Schema ent.Schema
} }
func (User) Mixin() []ent.Mixin {
return []ent.Mixin{
BaseMixin{},
}
}
// 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("username").
Unique().
NotEmpty(),
field.String("password").
Sensitive(),
field.String("email").
Optional().
Match(regexp.MustCompile(`^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`)), // 邮箱格式正则校验
field.String("ip").
Optional().
Match(regexp.MustCompile(`^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$|^([\da-fA-F]{1,4}:){7}[\da-fA-F]{1,4}$`)),
field.String("addr").
Optional().
MaxLen(500),
}
} }
// Edges of the User. // Edges of the User.
+83 -3
View File
@@ -6,6 +6,7 @@ import (
"EnGin/internal/ent/user" "EnGin/internal/ent/user"
"fmt" "fmt"
"strings" "strings"
"time"
"entgo.io/ent" "entgo.io/ent"
"entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql"
@@ -13,9 +14,23 @@ import (
// User is the model entity for the User schema. // User is the model entity for the User schema.
type User struct { type User struct {
config config `json:"-"`
// ID of the ent. // ID of the ent.
ID int `json:"id,omitempty"` ID int `json:"id,omitempty"`
// CreatedAt holds the value of the "created_at" field.
CreatedAt time.Time `json:"created_at,omitempty"`
// UpdatedAt holds the value of the "updated_at" field.
UpdatedAt time.Time `json:"updated_at,omitempty"`
// Username holds the value of the "username" field.
Username string `json:"username,omitempty"`
// Password holds the value of the "password" field.
Password string `json:"-"`
// Email holds the value of the "email" field.
Email string `json:"email,omitempty"`
// IP holds the value of the "ip" field.
IP string `json:"ip,omitempty"`
// Addr holds the value of the "addr" field.
Addr string `json:"addr,omitempty"`
selectValues sql.SelectValues selectValues sql.SelectValues
} }
@@ -26,6 +41,10 @@ func (*User) scanValues(columns []string) ([]any, error) {
switch columns[i] { switch columns[i] {
case user.FieldID: case user.FieldID:
values[i] = new(sql.NullInt64) values[i] = new(sql.NullInt64)
case user.FieldUsername, user.FieldPassword, user.FieldEmail, user.FieldIP, user.FieldAddr:
values[i] = new(sql.NullString)
case user.FieldCreatedAt, user.FieldUpdatedAt:
values[i] = new(sql.NullTime)
default: default:
values[i] = new(sql.UnknownType) values[i] = new(sql.UnknownType)
} }
@@ -47,6 +66,48 @@ func (_m *User) assignValues(columns []string, values []any) error {
return fmt.Errorf("unexpected type %T for field id", value) return fmt.Errorf("unexpected type %T for field id", value)
} }
_m.ID = int(value.Int64) _m.ID = int(value.Int64)
case user.FieldCreatedAt:
if value, ok := values[i].(*sql.NullTime); !ok {
return fmt.Errorf("unexpected type %T for field created_at", values[i])
} else if value.Valid {
_m.CreatedAt = value.Time
}
case user.FieldUpdatedAt:
if value, ok := values[i].(*sql.NullTime); !ok {
return fmt.Errorf("unexpected type %T for field updated_at", values[i])
} else if value.Valid {
_m.UpdatedAt = value.Time
}
case user.FieldUsername:
if value, ok := values[i].(*sql.NullString); !ok {
return fmt.Errorf("unexpected type %T for field username", values[i])
} else if value.Valid {
_m.Username = value.String
}
case user.FieldPassword:
if value, ok := values[i].(*sql.NullString); !ok {
return fmt.Errorf("unexpected type %T for field password", values[i])
} else if value.Valid {
_m.Password = value.String
}
case user.FieldEmail:
if value, ok := values[i].(*sql.NullString); !ok {
return fmt.Errorf("unexpected type %T for field email", values[i])
} else if value.Valid {
_m.Email = value.String
}
case user.FieldIP:
if value, ok := values[i].(*sql.NullString); !ok {
return fmt.Errorf("unexpected type %T for field ip", values[i])
} else if value.Valid {
_m.IP = value.String
}
case user.FieldAddr:
if value, ok := values[i].(*sql.NullString); !ok {
return fmt.Errorf("unexpected type %T for field addr", values[i])
} else if value.Valid {
_m.Addr = value.String
}
default: default:
_m.selectValues.Set(columns[i], values[i]) _m.selectValues.Set(columns[i], values[i])
} }
@@ -82,7 +143,26 @@ func (_m *User) Unwrap() *User {
func (_m *User) String() string { func (_m *User) String() string {
var builder strings.Builder var builder strings.Builder
builder.WriteString("User(") builder.WriteString("User(")
builder.WriteString(fmt.Sprintf("id=%v", _m.ID)) builder.WriteString(fmt.Sprintf("id=%v, ", _m.ID))
builder.WriteString("created_at=")
builder.WriteString(_m.CreatedAt.Format(time.ANSIC))
builder.WriteString(", ")
builder.WriteString("updated_at=")
builder.WriteString(_m.UpdatedAt.Format(time.ANSIC))
builder.WriteString(", ")
builder.WriteString("username=")
builder.WriteString(_m.Username)
builder.WriteString(", ")
builder.WriteString("password=<sensitive>")
builder.WriteString(", ")
builder.WriteString("email=")
builder.WriteString(_m.Email)
builder.WriteString(", ")
builder.WriteString("ip=")
builder.WriteString(_m.IP)
builder.WriteString(", ")
builder.WriteString("addr=")
builder.WriteString(_m.Addr)
builder.WriteByte(')') builder.WriteByte(')')
return builder.String() return builder.String()
} }
+75
View File
@@ -3,6 +3,8 @@
package user package user
import ( import (
"time"
"entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql"
) )
@@ -11,6 +13,20 @@ const (
Label = "user" Label = "user"
// FieldID holds the string denoting the id field in the database. // FieldID holds the string denoting the id field in the database.
FieldID = "id" FieldID = "id"
// FieldCreatedAt holds the string denoting the created_at field in the database.
FieldCreatedAt = "created_at"
// FieldUpdatedAt holds the string denoting the updated_at field in the database.
FieldUpdatedAt = "updated_at"
// FieldUsername holds the string denoting the username field in the database.
FieldUsername = "username"
// FieldPassword holds the string denoting the password field in the database.
FieldPassword = "password"
// FieldEmail holds the string denoting the email field in the database.
FieldEmail = "email"
// FieldIP holds the string denoting the ip field in the database.
FieldIP = "ip"
// FieldAddr holds the string denoting the addr field in the database.
FieldAddr = "addr"
// Table holds the table name of the user in the database. // Table holds the table name of the user in the database.
Table = "users" Table = "users"
) )
@@ -18,6 +34,13 @@ const (
// Columns holds all SQL columns for user fields. // Columns holds all SQL columns for user fields.
var Columns = []string{ var Columns = []string{
FieldID, FieldID,
FieldCreatedAt,
FieldUpdatedAt,
FieldUsername,
FieldPassword,
FieldEmail,
FieldIP,
FieldAddr,
} }
// ValidColumn reports if the column name is valid (part of the table columns). // ValidColumn reports if the column name is valid (part of the table columns).
@@ -30,6 +53,23 @@ func ValidColumn(column string) bool {
return false return false
} }
var (
// DefaultCreatedAt holds the default value on creation for the "created_at" field.
DefaultCreatedAt func() time.Time
// DefaultUpdatedAt holds the default value on creation for the "updated_at" field.
DefaultUpdatedAt func() time.Time
// UpdateDefaultUpdatedAt holds the default value on update for the "updated_at" field.
UpdateDefaultUpdatedAt func() time.Time
// UsernameValidator is a validator for the "username" field. It is called by the builders before save.
UsernameValidator func(string) error
// EmailValidator is a validator for the "email" field. It is called by the builders before save.
EmailValidator func(string) error
// IPValidator is a validator for the "ip" field. It is called by the builders before save.
IPValidator func(string) error
// AddrValidator is a validator for the "addr" field. It is called by the builders before save.
AddrValidator func(string) error
)
// OrderOption defines the ordering options for the User queries. // OrderOption defines the ordering options for the User queries.
type OrderOption func(*sql.Selector) type OrderOption func(*sql.Selector)
@@ -37,3 +77,38 @@ type OrderOption func(*sql.Selector)
func ByID(opts ...sql.OrderTermOption) OrderOption { func ByID(opts ...sql.OrderTermOption) OrderOption {
return sql.OrderByField(FieldID, opts...).ToFunc() return sql.OrderByField(FieldID, opts...).ToFunc()
} }
// ByCreatedAt orders the results by the created_at field.
func ByCreatedAt(opts ...sql.OrderTermOption) OrderOption {
return sql.OrderByField(FieldCreatedAt, opts...).ToFunc()
}
// ByUpdatedAt orders the results by the updated_at field.
func ByUpdatedAt(opts ...sql.OrderTermOption) OrderOption {
return sql.OrderByField(FieldUpdatedAt, opts...).ToFunc()
}
// ByUsername orders the results by the username field.
func ByUsername(opts ...sql.OrderTermOption) OrderOption {
return sql.OrderByField(FieldUsername, opts...).ToFunc()
}
// ByPassword orders the results by the password field.
func ByPassword(opts ...sql.OrderTermOption) OrderOption {
return sql.OrderByField(FieldPassword, opts...).ToFunc()
}
// ByEmail orders the results by the email field.
func ByEmail(opts ...sql.OrderTermOption) OrderOption {
return sql.OrderByField(FieldEmail, opts...).ToFunc()
}
// ByIP orders the results by the ip field.
func ByIP(opts ...sql.OrderTermOption) OrderOption {
return sql.OrderByField(FieldIP, opts...).ToFunc()
}
// ByAddr orders the results by the addr field.
func ByAddr(opts ...sql.OrderTermOption) OrderOption {
return sql.OrderByField(FieldAddr, opts...).ToFunc()
}
+471
View File
@@ -4,6 +4,7 @@ package user
import ( import (
"EnGin/internal/ent/predicate" "EnGin/internal/ent/predicate"
"time"
"entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql"
) )
@@ -53,6 +54,476 @@ func IDLTE(id int) predicate.User {
return predicate.User(sql.FieldLTE(FieldID, id)) return predicate.User(sql.FieldLTE(FieldID, id))
} }
// CreatedAt applies equality check predicate on the "created_at" field. It's identical to CreatedAtEQ.
func CreatedAt(v time.Time) predicate.User {
return predicate.User(sql.FieldEQ(FieldCreatedAt, v))
}
// UpdatedAt applies equality check predicate on the "updated_at" field. It's identical to UpdatedAtEQ.
func UpdatedAt(v time.Time) predicate.User {
return predicate.User(sql.FieldEQ(FieldUpdatedAt, v))
}
// Username applies equality check predicate on the "username" field. It's identical to UsernameEQ.
func Username(v string) predicate.User {
return predicate.User(sql.FieldEQ(FieldUsername, v))
}
// Password applies equality check predicate on the "password" field. It's identical to PasswordEQ.
func Password(v string) predicate.User {
return predicate.User(sql.FieldEQ(FieldPassword, v))
}
// Email applies equality check predicate on the "email" field. It's identical to EmailEQ.
func Email(v string) predicate.User {
return predicate.User(sql.FieldEQ(FieldEmail, v))
}
// IP applies equality check predicate on the "ip" field. It's identical to IPEQ.
func IP(v string) predicate.User {
return predicate.User(sql.FieldEQ(FieldIP, v))
}
// Addr applies equality check predicate on the "addr" field. It's identical to AddrEQ.
func Addr(v string) predicate.User {
return predicate.User(sql.FieldEQ(FieldAddr, v))
}
// CreatedAtEQ applies the EQ predicate on the "created_at" field.
func CreatedAtEQ(v time.Time) predicate.User {
return predicate.User(sql.FieldEQ(FieldCreatedAt, v))
}
// CreatedAtNEQ applies the NEQ predicate on the "created_at" field.
func CreatedAtNEQ(v time.Time) predicate.User {
return predicate.User(sql.FieldNEQ(FieldCreatedAt, v))
}
// CreatedAtIn applies the In predicate on the "created_at" field.
func CreatedAtIn(vs ...time.Time) predicate.User {
return predicate.User(sql.FieldIn(FieldCreatedAt, vs...))
}
// CreatedAtNotIn applies the NotIn predicate on the "created_at" field.
func CreatedAtNotIn(vs ...time.Time) predicate.User {
return predicate.User(sql.FieldNotIn(FieldCreatedAt, vs...))
}
// CreatedAtGT applies the GT predicate on the "created_at" field.
func CreatedAtGT(v time.Time) predicate.User {
return predicate.User(sql.FieldGT(FieldCreatedAt, v))
}
// CreatedAtGTE applies the GTE predicate on the "created_at" field.
func CreatedAtGTE(v time.Time) predicate.User {
return predicate.User(sql.FieldGTE(FieldCreatedAt, v))
}
// CreatedAtLT applies the LT predicate on the "created_at" field.
func CreatedAtLT(v time.Time) predicate.User {
return predicate.User(sql.FieldLT(FieldCreatedAt, v))
}
// CreatedAtLTE applies the LTE predicate on the "created_at" field.
func CreatedAtLTE(v time.Time) predicate.User {
return predicate.User(sql.FieldLTE(FieldCreatedAt, v))
}
// UpdatedAtEQ applies the EQ predicate on the "updated_at" field.
func UpdatedAtEQ(v time.Time) predicate.User {
return predicate.User(sql.FieldEQ(FieldUpdatedAt, v))
}
// UpdatedAtNEQ applies the NEQ predicate on the "updated_at" field.
func UpdatedAtNEQ(v time.Time) predicate.User {
return predicate.User(sql.FieldNEQ(FieldUpdatedAt, v))
}
// UpdatedAtIn applies the In predicate on the "updated_at" field.
func UpdatedAtIn(vs ...time.Time) predicate.User {
return predicate.User(sql.FieldIn(FieldUpdatedAt, vs...))
}
// UpdatedAtNotIn applies the NotIn predicate on the "updated_at" field.
func UpdatedAtNotIn(vs ...time.Time) predicate.User {
return predicate.User(sql.FieldNotIn(FieldUpdatedAt, vs...))
}
// UpdatedAtGT applies the GT predicate on the "updated_at" field.
func UpdatedAtGT(v time.Time) predicate.User {
return predicate.User(sql.FieldGT(FieldUpdatedAt, v))
}
// UpdatedAtGTE applies the GTE predicate on the "updated_at" field.
func UpdatedAtGTE(v time.Time) predicate.User {
return predicate.User(sql.FieldGTE(FieldUpdatedAt, v))
}
// UpdatedAtLT applies the LT predicate on the "updated_at" field.
func UpdatedAtLT(v time.Time) predicate.User {
return predicate.User(sql.FieldLT(FieldUpdatedAt, v))
}
// UpdatedAtLTE applies the LTE predicate on the "updated_at" field.
func UpdatedAtLTE(v time.Time) predicate.User {
return predicate.User(sql.FieldLTE(FieldUpdatedAt, v))
}
// UsernameEQ applies the EQ predicate on the "username" field.
func UsernameEQ(v string) predicate.User {
return predicate.User(sql.FieldEQ(FieldUsername, v))
}
// UsernameNEQ applies the NEQ predicate on the "username" field.
func UsernameNEQ(v string) predicate.User {
return predicate.User(sql.FieldNEQ(FieldUsername, v))
}
// UsernameIn applies the In predicate on the "username" field.
func UsernameIn(vs ...string) predicate.User {
return predicate.User(sql.FieldIn(FieldUsername, vs...))
}
// UsernameNotIn applies the NotIn predicate on the "username" field.
func UsernameNotIn(vs ...string) predicate.User {
return predicate.User(sql.FieldNotIn(FieldUsername, vs...))
}
// UsernameGT applies the GT predicate on the "username" field.
func UsernameGT(v string) predicate.User {
return predicate.User(sql.FieldGT(FieldUsername, v))
}
// UsernameGTE applies the GTE predicate on the "username" field.
func UsernameGTE(v string) predicate.User {
return predicate.User(sql.FieldGTE(FieldUsername, v))
}
// UsernameLT applies the LT predicate on the "username" field.
func UsernameLT(v string) predicate.User {
return predicate.User(sql.FieldLT(FieldUsername, v))
}
// UsernameLTE applies the LTE predicate on the "username" field.
func UsernameLTE(v string) predicate.User {
return predicate.User(sql.FieldLTE(FieldUsername, v))
}
// UsernameContains applies the Contains predicate on the "username" field.
func UsernameContains(v string) predicate.User {
return predicate.User(sql.FieldContains(FieldUsername, v))
}
// UsernameHasPrefix applies the HasPrefix predicate on the "username" field.
func UsernameHasPrefix(v string) predicate.User {
return predicate.User(sql.FieldHasPrefix(FieldUsername, v))
}
// UsernameHasSuffix applies the HasSuffix predicate on the "username" field.
func UsernameHasSuffix(v string) predicate.User {
return predicate.User(sql.FieldHasSuffix(FieldUsername, v))
}
// UsernameEqualFold applies the EqualFold predicate on the "username" field.
func UsernameEqualFold(v string) predicate.User {
return predicate.User(sql.FieldEqualFold(FieldUsername, v))
}
// UsernameContainsFold applies the ContainsFold predicate on the "username" field.
func UsernameContainsFold(v string) predicate.User {
return predicate.User(sql.FieldContainsFold(FieldUsername, v))
}
// PasswordEQ applies the EQ predicate on the "password" field.
func PasswordEQ(v string) predicate.User {
return predicate.User(sql.FieldEQ(FieldPassword, v))
}
// PasswordNEQ applies the NEQ predicate on the "password" field.
func PasswordNEQ(v string) predicate.User {
return predicate.User(sql.FieldNEQ(FieldPassword, v))
}
// PasswordIn applies the In predicate on the "password" field.
func PasswordIn(vs ...string) predicate.User {
return predicate.User(sql.FieldIn(FieldPassword, vs...))
}
// PasswordNotIn applies the NotIn predicate on the "password" field.
func PasswordNotIn(vs ...string) predicate.User {
return predicate.User(sql.FieldNotIn(FieldPassword, vs...))
}
// PasswordGT applies the GT predicate on the "password" field.
func PasswordGT(v string) predicate.User {
return predicate.User(sql.FieldGT(FieldPassword, v))
}
// PasswordGTE applies the GTE predicate on the "password" field.
func PasswordGTE(v string) predicate.User {
return predicate.User(sql.FieldGTE(FieldPassword, v))
}
// PasswordLT applies the LT predicate on the "password" field.
func PasswordLT(v string) predicate.User {
return predicate.User(sql.FieldLT(FieldPassword, v))
}
// PasswordLTE applies the LTE predicate on the "password" field.
func PasswordLTE(v string) predicate.User {
return predicate.User(sql.FieldLTE(FieldPassword, v))
}
// PasswordContains applies the Contains predicate on the "password" field.
func PasswordContains(v string) predicate.User {
return predicate.User(sql.FieldContains(FieldPassword, v))
}
// PasswordHasPrefix applies the HasPrefix predicate on the "password" field.
func PasswordHasPrefix(v string) predicate.User {
return predicate.User(sql.FieldHasPrefix(FieldPassword, v))
}
// PasswordHasSuffix applies the HasSuffix predicate on the "password" field.
func PasswordHasSuffix(v string) predicate.User {
return predicate.User(sql.FieldHasSuffix(FieldPassword, v))
}
// PasswordEqualFold applies the EqualFold predicate on the "password" field.
func PasswordEqualFold(v string) predicate.User {
return predicate.User(sql.FieldEqualFold(FieldPassword, v))
}
// PasswordContainsFold applies the ContainsFold predicate on the "password" field.
func PasswordContainsFold(v string) predicate.User {
return predicate.User(sql.FieldContainsFold(FieldPassword, v))
}
// EmailEQ applies the EQ predicate on the "email" field.
func EmailEQ(v string) predicate.User {
return predicate.User(sql.FieldEQ(FieldEmail, v))
}
// EmailNEQ applies the NEQ predicate on the "email" field.
func EmailNEQ(v string) predicate.User {
return predicate.User(sql.FieldNEQ(FieldEmail, v))
}
// EmailIn applies the In predicate on the "email" field.
func EmailIn(vs ...string) predicate.User {
return predicate.User(sql.FieldIn(FieldEmail, vs...))
}
// EmailNotIn applies the NotIn predicate on the "email" field.
func EmailNotIn(vs ...string) predicate.User {
return predicate.User(sql.FieldNotIn(FieldEmail, vs...))
}
// EmailGT applies the GT predicate on the "email" field.
func EmailGT(v string) predicate.User {
return predicate.User(sql.FieldGT(FieldEmail, v))
}
// EmailGTE applies the GTE predicate on the "email" field.
func EmailGTE(v string) predicate.User {
return predicate.User(sql.FieldGTE(FieldEmail, v))
}
// EmailLT applies the LT predicate on the "email" field.
func EmailLT(v string) predicate.User {
return predicate.User(sql.FieldLT(FieldEmail, v))
}
// EmailLTE applies the LTE predicate on the "email" field.
func EmailLTE(v string) predicate.User {
return predicate.User(sql.FieldLTE(FieldEmail, v))
}
// EmailContains applies the Contains predicate on the "email" field.
func EmailContains(v string) predicate.User {
return predicate.User(sql.FieldContains(FieldEmail, v))
}
// EmailHasPrefix applies the HasPrefix predicate on the "email" field.
func EmailHasPrefix(v string) predicate.User {
return predicate.User(sql.FieldHasPrefix(FieldEmail, v))
}
// EmailHasSuffix applies the HasSuffix predicate on the "email" field.
func EmailHasSuffix(v string) predicate.User {
return predicate.User(sql.FieldHasSuffix(FieldEmail, v))
}
// EmailIsNil applies the IsNil predicate on the "email" field.
func EmailIsNil() predicate.User {
return predicate.User(sql.FieldIsNull(FieldEmail))
}
// EmailNotNil applies the NotNil predicate on the "email" field.
func EmailNotNil() predicate.User {
return predicate.User(sql.FieldNotNull(FieldEmail))
}
// EmailEqualFold applies the EqualFold predicate on the "email" field.
func EmailEqualFold(v string) predicate.User {
return predicate.User(sql.FieldEqualFold(FieldEmail, v))
}
// EmailContainsFold applies the ContainsFold predicate on the "email" field.
func EmailContainsFold(v string) predicate.User {
return predicate.User(sql.FieldContainsFold(FieldEmail, v))
}
// IPEQ applies the EQ predicate on the "ip" field.
func IPEQ(v string) predicate.User {
return predicate.User(sql.FieldEQ(FieldIP, v))
}
// IPNEQ applies the NEQ predicate on the "ip" field.
func IPNEQ(v string) predicate.User {
return predicate.User(sql.FieldNEQ(FieldIP, v))
}
// IPIn applies the In predicate on the "ip" field.
func IPIn(vs ...string) predicate.User {
return predicate.User(sql.FieldIn(FieldIP, vs...))
}
// IPNotIn applies the NotIn predicate on the "ip" field.
func IPNotIn(vs ...string) predicate.User {
return predicate.User(sql.FieldNotIn(FieldIP, vs...))
}
// IPGT applies the GT predicate on the "ip" field.
func IPGT(v string) predicate.User {
return predicate.User(sql.FieldGT(FieldIP, v))
}
// IPGTE applies the GTE predicate on the "ip" field.
func IPGTE(v string) predicate.User {
return predicate.User(sql.FieldGTE(FieldIP, v))
}
// IPLT applies the LT predicate on the "ip" field.
func IPLT(v string) predicate.User {
return predicate.User(sql.FieldLT(FieldIP, v))
}
// IPLTE applies the LTE predicate on the "ip" field.
func IPLTE(v string) predicate.User {
return predicate.User(sql.FieldLTE(FieldIP, v))
}
// IPContains applies the Contains predicate on the "ip" field.
func IPContains(v string) predicate.User {
return predicate.User(sql.FieldContains(FieldIP, v))
}
// IPHasPrefix applies the HasPrefix predicate on the "ip" field.
func IPHasPrefix(v string) predicate.User {
return predicate.User(sql.FieldHasPrefix(FieldIP, v))
}
// IPHasSuffix applies the HasSuffix predicate on the "ip" field.
func IPHasSuffix(v string) predicate.User {
return predicate.User(sql.FieldHasSuffix(FieldIP, v))
}
// IPIsNil applies the IsNil predicate on the "ip" field.
func IPIsNil() predicate.User {
return predicate.User(sql.FieldIsNull(FieldIP))
}
// IPNotNil applies the NotNil predicate on the "ip" field.
func IPNotNil() predicate.User {
return predicate.User(sql.FieldNotNull(FieldIP))
}
// IPEqualFold applies the EqualFold predicate on the "ip" field.
func IPEqualFold(v string) predicate.User {
return predicate.User(sql.FieldEqualFold(FieldIP, v))
}
// IPContainsFold applies the ContainsFold predicate on the "ip" field.
func IPContainsFold(v string) predicate.User {
return predicate.User(sql.FieldContainsFold(FieldIP, v))
}
// AddrEQ applies the EQ predicate on the "addr" field.
func AddrEQ(v string) predicate.User {
return predicate.User(sql.FieldEQ(FieldAddr, v))
}
// AddrNEQ applies the NEQ predicate on the "addr" field.
func AddrNEQ(v string) predicate.User {
return predicate.User(sql.FieldNEQ(FieldAddr, v))
}
// AddrIn applies the In predicate on the "addr" field.
func AddrIn(vs ...string) predicate.User {
return predicate.User(sql.FieldIn(FieldAddr, vs...))
}
// AddrNotIn applies the NotIn predicate on the "addr" field.
func AddrNotIn(vs ...string) predicate.User {
return predicate.User(sql.FieldNotIn(FieldAddr, vs...))
}
// AddrGT applies the GT predicate on the "addr" field.
func AddrGT(v string) predicate.User {
return predicate.User(sql.FieldGT(FieldAddr, v))
}
// AddrGTE applies the GTE predicate on the "addr" field.
func AddrGTE(v string) predicate.User {
return predicate.User(sql.FieldGTE(FieldAddr, v))
}
// AddrLT applies the LT predicate on the "addr" field.
func AddrLT(v string) predicate.User {
return predicate.User(sql.FieldLT(FieldAddr, v))
}
// AddrLTE applies the LTE predicate on the "addr" field.
func AddrLTE(v string) predicate.User {
return predicate.User(sql.FieldLTE(FieldAddr, v))
}
// AddrContains applies the Contains predicate on the "addr" field.
func AddrContains(v string) predicate.User {
return predicate.User(sql.FieldContains(FieldAddr, v))
}
// AddrHasPrefix applies the HasPrefix predicate on the "addr" field.
func AddrHasPrefix(v string) predicate.User {
return predicate.User(sql.FieldHasPrefix(FieldAddr, v))
}
// AddrHasSuffix applies the HasSuffix predicate on the "addr" field.
func AddrHasSuffix(v string) predicate.User {
return predicate.User(sql.FieldHasSuffix(FieldAddr, v))
}
// AddrIsNil applies the IsNil predicate on the "addr" field.
func AddrIsNil() predicate.User {
return predicate.User(sql.FieldIsNull(FieldAddr))
}
// AddrNotNil applies the NotNil predicate on the "addr" field.
func AddrNotNil() predicate.User {
return predicate.User(sql.FieldNotNull(FieldAddr))
}
// AddrEqualFold applies the EqualFold predicate on the "addr" field.
func AddrEqualFold(v string) predicate.User {
return predicate.User(sql.FieldEqualFold(FieldAddr, v))
}
// AddrContainsFold applies the ContainsFold predicate on the "addr" field.
func AddrContainsFold(v string) predicate.User {
return predicate.User(sql.FieldContainsFold(FieldAddr, v))
}
// And groups predicates with the AND operator between them. // And groups predicates with the AND operator between them.
func And(predicates ...predicate.User) predicate.User { func And(predicates ...predicate.User) predicate.User {
return predicate.User(sql.AndPredicates(predicates...)) return predicate.User(sql.AndPredicates(predicates...))
+158
View File
@@ -5,7 +5,9 @@ package ent
import ( import (
"EnGin/internal/ent/user" "EnGin/internal/ent/user"
"context" "context"
"errors"
"fmt" "fmt"
"time"
"entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field" "entgo.io/ent/schema/field"
@@ -18,6 +20,88 @@ type UserCreate struct {
hooks []Hook hooks []Hook
} }
// SetCreatedAt sets the "created_at" field.
func (_c *UserCreate) SetCreatedAt(v time.Time) *UserCreate {
_c.mutation.SetCreatedAt(v)
return _c
}
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (_c *UserCreate) SetNillableCreatedAt(v *time.Time) *UserCreate {
if v != nil {
_c.SetCreatedAt(*v)
}
return _c
}
// SetUpdatedAt sets the "updated_at" field.
func (_c *UserCreate) SetUpdatedAt(v time.Time) *UserCreate {
_c.mutation.SetUpdatedAt(v)
return _c
}
// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
func (_c *UserCreate) SetNillableUpdatedAt(v *time.Time) *UserCreate {
if v != nil {
_c.SetUpdatedAt(*v)
}
return _c
}
// SetUsername sets the "username" field.
func (_c *UserCreate) SetUsername(v string) *UserCreate {
_c.mutation.SetUsername(v)
return _c
}
// SetPassword sets the "password" field.
func (_c *UserCreate) SetPassword(v string) *UserCreate {
_c.mutation.SetPassword(v)
return _c
}
// SetEmail sets the "email" field.
func (_c *UserCreate) SetEmail(v string) *UserCreate {
_c.mutation.SetEmail(v)
return _c
}
// SetNillableEmail sets the "email" field if the given value is not nil.
func (_c *UserCreate) SetNillableEmail(v *string) *UserCreate {
if v != nil {
_c.SetEmail(*v)
}
return _c
}
// SetIP sets the "ip" field.
func (_c *UserCreate) SetIP(v string) *UserCreate {
_c.mutation.SetIP(v)
return _c
}
// SetNillableIP sets the "ip" field if the given value is not nil.
func (_c *UserCreate) SetNillableIP(v *string) *UserCreate {
if v != nil {
_c.SetIP(*v)
}
return _c
}
// SetAddr sets the "addr" field.
func (_c *UserCreate) SetAddr(v string) *UserCreate {
_c.mutation.SetAddr(v)
return _c
}
// SetNillableAddr sets the "addr" field if the given value is not nil.
func (_c *UserCreate) SetNillableAddr(v *string) *UserCreate {
if v != nil {
_c.SetAddr(*v)
}
return _c
}
// Mutation returns the UserMutation object of the builder. // Mutation returns the UserMutation object of the builder.
func (_c *UserCreate) Mutation() *UserMutation { func (_c *UserCreate) Mutation() *UserMutation {
return _c.mutation return _c.mutation
@@ -25,6 +109,7 @@ func (_c *UserCreate) Mutation() *UserMutation {
// Save creates the User in the database. // Save creates the User in the database.
func (_c *UserCreate) Save(ctx context.Context) (*User, error) { func (_c *UserCreate) Save(ctx context.Context) (*User, error) {
_c.defaults()
return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks) return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
} }
@@ -50,8 +135,52 @@ func (_c *UserCreate) ExecX(ctx context.Context) {
} }
} }
// defaults sets the default values of the builder before save.
func (_c *UserCreate) defaults() {
if _, ok := _c.mutation.CreatedAt(); !ok {
v := user.DefaultCreatedAt()
_c.mutation.SetCreatedAt(v)
}
if _, ok := _c.mutation.UpdatedAt(); !ok {
v := user.DefaultUpdatedAt()
_c.mutation.SetUpdatedAt(v)
}
}
// check runs all checks and user-defined validators on the builder. // check runs all checks and user-defined validators on the builder.
func (_c *UserCreate) check() error { func (_c *UserCreate) check() error {
if _, ok := _c.mutation.CreatedAt(); !ok {
return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "User.created_at"`)}
}
if _, ok := _c.mutation.UpdatedAt(); !ok {
return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "User.updated_at"`)}
}
if _, ok := _c.mutation.Username(); !ok {
return &ValidationError{Name: "username", err: errors.New(`ent: missing required field "User.username"`)}
}
if v, ok := _c.mutation.Username(); ok {
if err := user.UsernameValidator(v); err != nil {
return &ValidationError{Name: "username", err: fmt.Errorf(`ent: validator failed for field "User.username": %w`, err)}
}
}
if _, ok := _c.mutation.Password(); !ok {
return &ValidationError{Name: "password", err: errors.New(`ent: missing required field "User.password"`)}
}
if v, ok := _c.mutation.Email(); ok {
if err := user.EmailValidator(v); err != nil {
return &ValidationError{Name: "email", err: fmt.Errorf(`ent: validator failed for field "User.email": %w`, err)}
}
}
if v, ok := _c.mutation.IP(); ok {
if err := user.IPValidator(v); err != nil {
return &ValidationError{Name: "ip", err: fmt.Errorf(`ent: validator failed for field "User.ip": %w`, err)}
}
}
if v, ok := _c.mutation.Addr(); ok {
if err := user.AddrValidator(v); err != nil {
return &ValidationError{Name: "addr", err: fmt.Errorf(`ent: validator failed for field "User.addr": %w`, err)}
}
}
return nil return nil
} }
@@ -78,6 +207,34 @@ func (_c *UserCreate) createSpec() (*User, *sqlgraph.CreateSpec) {
_node = &User{config: _c.config} _node = &User{config: _c.config}
_spec = sqlgraph.NewCreateSpec(user.Table, sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt)) _spec = sqlgraph.NewCreateSpec(user.Table, sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt))
) )
if value, ok := _c.mutation.CreatedAt(); ok {
_spec.SetField(user.FieldCreatedAt, field.TypeTime, value)
_node.CreatedAt = value
}
if value, ok := _c.mutation.UpdatedAt(); ok {
_spec.SetField(user.FieldUpdatedAt, field.TypeTime, value)
_node.UpdatedAt = value
}
if value, ok := _c.mutation.Username(); ok {
_spec.SetField(user.FieldUsername, field.TypeString, value)
_node.Username = value
}
if value, ok := _c.mutation.Password(); ok {
_spec.SetField(user.FieldPassword, field.TypeString, value)
_node.Password = value
}
if value, ok := _c.mutation.Email(); ok {
_spec.SetField(user.FieldEmail, field.TypeString, value)
_node.Email = value
}
if value, ok := _c.mutation.IP(); ok {
_spec.SetField(user.FieldIP, field.TypeString, value)
_node.IP = value
}
if value, ok := _c.mutation.Addr(); ok {
_spec.SetField(user.FieldAddr, field.TypeString, value)
_node.Addr = value
}
return _node, _spec return _node, _spec
} }
@@ -99,6 +256,7 @@ func (_c *UserCreateBulk) Save(ctx context.Context) ([]*User, error) {
for i := range _c.builders { for i := range _c.builders {
func(i int, root context.Context) { func(i int, root context.Context) {
builder := _c.builders[i] builder := _c.builders[i]
builder.defaults()
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*UserMutation) mutation, ok := m.(*UserMutation)
if !ok { if !ok {
+22
View File
@@ -258,6 +258,18 @@ func (_q *UserQuery) Clone() *UserQuery {
// GroupBy is used to group vertices by one or more fields/columns. // GroupBy is used to group vertices by one or more fields/columns.
// It is often used with aggregate functions, like: count, max, mean, min, sum. // It is often used with aggregate functions, like: count, max, mean, min, sum.
//
// Example:
//
// var v []struct {
// CreatedAt time.Time `json:"created_at,omitempty"`
// Count int `json:"count,omitempty"`
// }
//
// client.User.Query().
// GroupBy(user.FieldCreatedAt).
// Aggregate(ent.Count()).
// Scan(ctx, &v)
func (_q *UserQuery) GroupBy(field string, fields ...string) *UserGroupBy { func (_q *UserQuery) GroupBy(field string, fields ...string) *UserGroupBy {
_q.ctx.Fields = append([]string{field}, fields...) _q.ctx.Fields = append([]string{field}, fields...)
grbuild := &UserGroupBy{build: _q} grbuild := &UserGroupBy{build: _q}
@@ -269,6 +281,16 @@ func (_q *UserQuery) GroupBy(field string, fields ...string) *UserGroupBy {
// Select allows the selection one or more fields/columns for the given query, // Select allows the selection one or more fields/columns for the given query,
// instead of selecting all fields in the entity. // instead of selecting all fields in the entity.
//
// Example:
//
// var v []struct {
// CreatedAt time.Time `json:"created_at,omitempty"`
// }
//
// client.User.Query().
// Select(user.FieldCreatedAt).
// Scan(ctx, &v)
func (_q *UserQuery) Select(fields ...string) *UserSelect { func (_q *UserQuery) Select(fields ...string) *UserSelect {
_q.ctx.Fields = append(_q.ctx.Fields, fields...) _q.ctx.Fields = append(_q.ctx.Fields, fields...)
sbuild := &UserSelect{UserQuery: _q} sbuild := &UserSelect{UserQuery: _q}
+317
View File
@@ -8,6 +8,7 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"time"
"entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/dialect/sql/sqlgraph"
@@ -27,6 +28,100 @@ func (_u *UserUpdate) Where(ps ...predicate.User) *UserUpdate {
return _u return _u
} }
// SetUpdatedAt sets the "updated_at" field.
func (_u *UserUpdate) SetUpdatedAt(v time.Time) *UserUpdate {
_u.mutation.SetUpdatedAt(v)
return _u
}
// SetUsername sets the "username" field.
func (_u *UserUpdate) SetUsername(v string) *UserUpdate {
_u.mutation.SetUsername(v)
return _u
}
// SetNillableUsername sets the "username" field if the given value is not nil.
func (_u *UserUpdate) SetNillableUsername(v *string) *UserUpdate {
if v != nil {
_u.SetUsername(*v)
}
return _u
}
// SetPassword sets the "password" field.
func (_u *UserUpdate) SetPassword(v string) *UserUpdate {
_u.mutation.SetPassword(v)
return _u
}
// SetNillablePassword sets the "password" field if the given value is not nil.
func (_u *UserUpdate) SetNillablePassword(v *string) *UserUpdate {
if v != nil {
_u.SetPassword(*v)
}
return _u
}
// SetEmail sets the "email" field.
func (_u *UserUpdate) SetEmail(v string) *UserUpdate {
_u.mutation.SetEmail(v)
return _u
}
// SetNillableEmail sets the "email" field if the given value is not nil.
func (_u *UserUpdate) SetNillableEmail(v *string) *UserUpdate {
if v != nil {
_u.SetEmail(*v)
}
return _u
}
// ClearEmail clears the value of the "email" field.
func (_u *UserUpdate) ClearEmail() *UserUpdate {
_u.mutation.ClearEmail()
return _u
}
// SetIP sets the "ip" field.
func (_u *UserUpdate) SetIP(v string) *UserUpdate {
_u.mutation.SetIP(v)
return _u
}
// SetNillableIP sets the "ip" field if the given value is not nil.
func (_u *UserUpdate) SetNillableIP(v *string) *UserUpdate {
if v != nil {
_u.SetIP(*v)
}
return _u
}
// ClearIP clears the value of the "ip" field.
func (_u *UserUpdate) ClearIP() *UserUpdate {
_u.mutation.ClearIP()
return _u
}
// SetAddr sets the "addr" field.
func (_u *UserUpdate) SetAddr(v string) *UserUpdate {
_u.mutation.SetAddr(v)
return _u
}
// SetNillableAddr sets the "addr" field if the given value is not nil.
func (_u *UserUpdate) SetNillableAddr(v *string) *UserUpdate {
if v != nil {
_u.SetAddr(*v)
}
return _u
}
// ClearAddr clears the value of the "addr" field.
func (_u *UserUpdate) ClearAddr() *UserUpdate {
_u.mutation.ClearAddr()
return _u
}
// Mutation returns the UserMutation object of the builder. // Mutation returns the UserMutation object of the builder.
func (_u *UserUpdate) Mutation() *UserMutation { func (_u *UserUpdate) Mutation() *UserMutation {
return _u.mutation return _u.mutation
@@ -34,6 +129,7 @@ func (_u *UserUpdate) Mutation() *UserMutation {
// Save executes the query and returns the number of nodes affected by the update operation. // Save executes the query and returns the number of nodes affected by the update operation.
func (_u *UserUpdate) Save(ctx context.Context) (int, error) { func (_u *UserUpdate) Save(ctx context.Context) (int, error) {
_u.defaults()
return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks) return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
} }
@@ -59,7 +155,43 @@ func (_u *UserUpdate) ExecX(ctx context.Context) {
} }
} }
// defaults sets the default values of the builder before save.
func (_u *UserUpdate) defaults() {
if _, ok := _u.mutation.UpdatedAt(); !ok {
v := user.UpdateDefaultUpdatedAt()
_u.mutation.SetUpdatedAt(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (_u *UserUpdate) check() error {
if v, ok := _u.mutation.Username(); ok {
if err := user.UsernameValidator(v); err != nil {
return &ValidationError{Name: "username", err: fmt.Errorf(`ent: validator failed for field "User.username": %w`, err)}
}
}
if v, ok := _u.mutation.Email(); ok {
if err := user.EmailValidator(v); err != nil {
return &ValidationError{Name: "email", err: fmt.Errorf(`ent: validator failed for field "User.email": %w`, err)}
}
}
if v, ok := _u.mutation.IP(); ok {
if err := user.IPValidator(v); err != nil {
return &ValidationError{Name: "ip", err: fmt.Errorf(`ent: validator failed for field "User.ip": %w`, err)}
}
}
if v, ok := _u.mutation.Addr(); ok {
if err := user.AddrValidator(v); err != nil {
return &ValidationError{Name: "addr", err: fmt.Errorf(`ent: validator failed for field "User.addr": %w`, err)}
}
}
return nil
}
func (_u *UserUpdate) sqlSave(ctx context.Context) (_node int, err error) { func (_u *UserUpdate) sqlSave(ctx context.Context) (_node int, err error) {
if err := _u.check(); err != nil {
return _node, err
}
_spec := sqlgraph.NewUpdateSpec(user.Table, user.Columns, sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt)) _spec := sqlgraph.NewUpdateSpec(user.Table, user.Columns, sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt))
if ps := _u.mutation.predicates; len(ps) > 0 { if ps := _u.mutation.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) { _spec.Predicate = func(selector *sql.Selector) {
@@ -68,6 +200,33 @@ func (_u *UserUpdate) sqlSave(ctx context.Context) (_node int, err error) {
} }
} }
} }
if value, ok := _u.mutation.UpdatedAt(); ok {
_spec.SetField(user.FieldUpdatedAt, field.TypeTime, value)
}
if value, ok := _u.mutation.Username(); ok {
_spec.SetField(user.FieldUsername, field.TypeString, value)
}
if value, ok := _u.mutation.Password(); ok {
_spec.SetField(user.FieldPassword, field.TypeString, value)
}
if value, ok := _u.mutation.Email(); ok {
_spec.SetField(user.FieldEmail, field.TypeString, value)
}
if _u.mutation.EmailCleared() {
_spec.ClearField(user.FieldEmail, field.TypeString)
}
if value, ok := _u.mutation.IP(); ok {
_spec.SetField(user.FieldIP, field.TypeString, value)
}
if _u.mutation.IPCleared() {
_spec.ClearField(user.FieldIP, field.TypeString)
}
if value, ok := _u.mutation.Addr(); ok {
_spec.SetField(user.FieldAddr, field.TypeString, value)
}
if _u.mutation.AddrCleared() {
_spec.ClearField(user.FieldAddr, field.TypeString)
}
if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil { if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok { if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{user.Label} err = &NotFoundError{user.Label}
@@ -88,6 +247,100 @@ type UserUpdateOne struct {
mutation *UserMutation mutation *UserMutation
} }
// SetUpdatedAt sets the "updated_at" field.
func (_u *UserUpdateOne) SetUpdatedAt(v time.Time) *UserUpdateOne {
_u.mutation.SetUpdatedAt(v)
return _u
}
// SetUsername sets the "username" field.
func (_u *UserUpdateOne) SetUsername(v string) *UserUpdateOne {
_u.mutation.SetUsername(v)
return _u
}
// SetNillableUsername sets the "username" field if the given value is not nil.
func (_u *UserUpdateOne) SetNillableUsername(v *string) *UserUpdateOne {
if v != nil {
_u.SetUsername(*v)
}
return _u
}
// SetPassword sets the "password" field.
func (_u *UserUpdateOne) SetPassword(v string) *UserUpdateOne {
_u.mutation.SetPassword(v)
return _u
}
// SetNillablePassword sets the "password" field if the given value is not nil.
func (_u *UserUpdateOne) SetNillablePassword(v *string) *UserUpdateOne {
if v != nil {
_u.SetPassword(*v)
}
return _u
}
// SetEmail sets the "email" field.
func (_u *UserUpdateOne) SetEmail(v string) *UserUpdateOne {
_u.mutation.SetEmail(v)
return _u
}
// SetNillableEmail sets the "email" field if the given value is not nil.
func (_u *UserUpdateOne) SetNillableEmail(v *string) *UserUpdateOne {
if v != nil {
_u.SetEmail(*v)
}
return _u
}
// ClearEmail clears the value of the "email" field.
func (_u *UserUpdateOne) ClearEmail() *UserUpdateOne {
_u.mutation.ClearEmail()
return _u
}
// SetIP sets the "ip" field.
func (_u *UserUpdateOne) SetIP(v string) *UserUpdateOne {
_u.mutation.SetIP(v)
return _u
}
// SetNillableIP sets the "ip" field if the given value is not nil.
func (_u *UserUpdateOne) SetNillableIP(v *string) *UserUpdateOne {
if v != nil {
_u.SetIP(*v)
}
return _u
}
// ClearIP clears the value of the "ip" field.
func (_u *UserUpdateOne) ClearIP() *UserUpdateOne {
_u.mutation.ClearIP()
return _u
}
// SetAddr sets the "addr" field.
func (_u *UserUpdateOne) SetAddr(v string) *UserUpdateOne {
_u.mutation.SetAddr(v)
return _u
}
// SetNillableAddr sets the "addr" field if the given value is not nil.
func (_u *UserUpdateOne) SetNillableAddr(v *string) *UserUpdateOne {
if v != nil {
_u.SetAddr(*v)
}
return _u
}
// ClearAddr clears the value of the "addr" field.
func (_u *UserUpdateOne) ClearAddr() *UserUpdateOne {
_u.mutation.ClearAddr()
return _u
}
// Mutation returns the UserMutation object of the builder. // Mutation returns the UserMutation object of the builder.
func (_u *UserUpdateOne) Mutation() *UserMutation { func (_u *UserUpdateOne) Mutation() *UserMutation {
return _u.mutation return _u.mutation
@@ -108,6 +361,7 @@ func (_u *UserUpdateOne) Select(field string, fields ...string) *UserUpdateOne {
// Save executes the query and returns the updated User entity. // Save executes the query and returns the updated User entity.
func (_u *UserUpdateOne) Save(ctx context.Context) (*User, error) { func (_u *UserUpdateOne) Save(ctx context.Context) (*User, error) {
_u.defaults()
return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks) return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
} }
@@ -133,7 +387,43 @@ func (_u *UserUpdateOne) ExecX(ctx context.Context) {
} }
} }
// defaults sets the default values of the builder before save.
func (_u *UserUpdateOne) defaults() {
if _, ok := _u.mutation.UpdatedAt(); !ok {
v := user.UpdateDefaultUpdatedAt()
_u.mutation.SetUpdatedAt(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (_u *UserUpdateOne) check() error {
if v, ok := _u.mutation.Username(); ok {
if err := user.UsernameValidator(v); err != nil {
return &ValidationError{Name: "username", err: fmt.Errorf(`ent: validator failed for field "User.username": %w`, err)}
}
}
if v, ok := _u.mutation.Email(); ok {
if err := user.EmailValidator(v); err != nil {
return &ValidationError{Name: "email", err: fmt.Errorf(`ent: validator failed for field "User.email": %w`, err)}
}
}
if v, ok := _u.mutation.IP(); ok {
if err := user.IPValidator(v); err != nil {
return &ValidationError{Name: "ip", err: fmt.Errorf(`ent: validator failed for field "User.ip": %w`, err)}
}
}
if v, ok := _u.mutation.Addr(); ok {
if err := user.AddrValidator(v); err != nil {
return &ValidationError{Name: "addr", err: fmt.Errorf(`ent: validator failed for field "User.addr": %w`, err)}
}
}
return nil
}
func (_u *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) { func (_u *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) {
if err := _u.check(); err != nil {
return _node, err
}
_spec := sqlgraph.NewUpdateSpec(user.Table, user.Columns, sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt)) _spec := sqlgraph.NewUpdateSpec(user.Table, user.Columns, sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt))
id, ok := _u.mutation.ID() id, ok := _u.mutation.ID()
if !ok { if !ok {
@@ -159,6 +449,33 @@ func (_u *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) {
} }
} }
} }
if value, ok := _u.mutation.UpdatedAt(); ok {
_spec.SetField(user.FieldUpdatedAt, field.TypeTime, value)
}
if value, ok := _u.mutation.Username(); ok {
_spec.SetField(user.FieldUsername, field.TypeString, value)
}
if value, ok := _u.mutation.Password(); ok {
_spec.SetField(user.FieldPassword, field.TypeString, value)
}
if value, ok := _u.mutation.Email(); ok {
_spec.SetField(user.FieldEmail, field.TypeString, value)
}
if _u.mutation.EmailCleared() {
_spec.ClearField(user.FieldEmail, field.TypeString)
}
if value, ok := _u.mutation.IP(); ok {
_spec.SetField(user.FieldIP, field.TypeString, value)
}
if _u.mutation.IPCleared() {
_spec.ClearField(user.FieldIP, field.TypeString)
}
if value, ok := _u.mutation.Addr(); ok {
_spec.SetField(user.FieldAddr, field.TypeString, value)
}
if _u.mutation.AddrCleared() {
_spec.ClearField(user.FieldAddr, field.TypeString)
}
_node = &User{config: _u.config} _node = &User{config: _u.config}
_spec.Assign = _node.assignValues _spec.Assign = _node.assignValues
_spec.ScanValues = _node.scanValues _spec.ScanValues = _node.scanValues
BIN
View File
Binary file not shown.