feat: add user_tenant associations
This commit is contained in:
@@ -35,6 +35,11 @@ func newTenant(db *gorm.DB, opts ...gen.DOOption) tenantQuery {
|
||||
_tenantQuery.ExpiredAt = field.NewTime(tableName, "expired_at")
|
||||
_tenantQuery.CreatedAt = field.NewTime(tableName, "created_at")
|
||||
_tenantQuery.UpdatedAt = field.NewTime(tableName, "updated_at")
|
||||
_tenantQuery.Users = tenantQueryManyToManyUsers{
|
||||
db: db.Session(&gorm.Session{}),
|
||||
|
||||
RelationField: field.NewRelation("Users", "User"),
|
||||
}
|
||||
|
||||
_tenantQuery.fillFieldMap()
|
||||
|
||||
@@ -55,6 +60,7 @@ type tenantQuery struct {
|
||||
ExpiredAt field.Time
|
||||
CreatedAt field.Time
|
||||
UpdatedAt field.Time
|
||||
Users tenantQueryManyToManyUsers
|
||||
|
||||
fieldMap map[string]field.Expr
|
||||
}
|
||||
@@ -111,7 +117,7 @@ func (t *tenantQuery) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
|
||||
}
|
||||
|
||||
func (t *tenantQuery) fillFieldMap() {
|
||||
t.fieldMap = make(map[string]field.Expr, 10)
|
||||
t.fieldMap = make(map[string]field.Expr, 11)
|
||||
t.fieldMap["id"] = t.ID
|
||||
t.fieldMap["user_id"] = t.UserID
|
||||
t.fieldMap["code"] = t.Code
|
||||
@@ -122,18 +128,103 @@ func (t *tenantQuery) fillFieldMap() {
|
||||
t.fieldMap["expired_at"] = t.ExpiredAt
|
||||
t.fieldMap["created_at"] = t.CreatedAt
|
||||
t.fieldMap["updated_at"] = t.UpdatedAt
|
||||
|
||||
}
|
||||
|
||||
func (t tenantQuery) clone(db *gorm.DB) tenantQuery {
|
||||
t.tenantQueryDo.ReplaceConnPool(db.Statement.ConnPool)
|
||||
t.Users.db = db.Session(&gorm.Session{Initialized: true})
|
||||
t.Users.db.Statement.ConnPool = db.Statement.ConnPool
|
||||
return t
|
||||
}
|
||||
|
||||
func (t tenantQuery) replaceDB(db *gorm.DB) tenantQuery {
|
||||
t.tenantQueryDo.ReplaceDB(db)
|
||||
t.Users.db = db.Session(&gorm.Session{})
|
||||
return t
|
||||
}
|
||||
|
||||
type tenantQueryManyToManyUsers struct {
|
||||
db *gorm.DB
|
||||
|
||||
field.RelationField
|
||||
}
|
||||
|
||||
func (a tenantQueryManyToManyUsers) Where(conds ...field.Expr) *tenantQueryManyToManyUsers {
|
||||
if len(conds) == 0 {
|
||||
return &a
|
||||
}
|
||||
|
||||
exprs := make([]clause.Expression, 0, len(conds))
|
||||
for _, cond := range conds {
|
||||
exprs = append(exprs, cond.BeCond().(clause.Expression))
|
||||
}
|
||||
a.db = a.db.Clauses(clause.Where{Exprs: exprs})
|
||||
return &a
|
||||
}
|
||||
|
||||
func (a tenantQueryManyToManyUsers) WithContext(ctx context.Context) *tenantQueryManyToManyUsers {
|
||||
a.db = a.db.WithContext(ctx)
|
||||
return &a
|
||||
}
|
||||
|
||||
func (a tenantQueryManyToManyUsers) Session(session *gorm.Session) *tenantQueryManyToManyUsers {
|
||||
a.db = a.db.Session(session)
|
||||
return &a
|
||||
}
|
||||
|
||||
func (a tenantQueryManyToManyUsers) Model(m *Tenant) *tenantQueryManyToManyUsersTx {
|
||||
return &tenantQueryManyToManyUsersTx{a.db.Model(m).Association(a.Name())}
|
||||
}
|
||||
|
||||
func (a tenantQueryManyToManyUsers) Unscoped() *tenantQueryManyToManyUsers {
|
||||
a.db = a.db.Unscoped()
|
||||
return &a
|
||||
}
|
||||
|
||||
type tenantQueryManyToManyUsersTx struct{ tx *gorm.Association }
|
||||
|
||||
func (a tenantQueryManyToManyUsersTx) Find() (result []*User, err error) {
|
||||
return result, a.tx.Find(&result)
|
||||
}
|
||||
|
||||
func (a tenantQueryManyToManyUsersTx) Append(values ...*User) (err error) {
|
||||
targetValues := make([]interface{}, len(values))
|
||||
for i, v := range values {
|
||||
targetValues[i] = v
|
||||
}
|
||||
return a.tx.Append(targetValues...)
|
||||
}
|
||||
|
||||
func (a tenantQueryManyToManyUsersTx) Replace(values ...*User) (err error) {
|
||||
targetValues := make([]interface{}, len(values))
|
||||
for i, v := range values {
|
||||
targetValues[i] = v
|
||||
}
|
||||
return a.tx.Replace(targetValues...)
|
||||
}
|
||||
|
||||
func (a tenantQueryManyToManyUsersTx) Delete(values ...*User) (err error) {
|
||||
targetValues := make([]interface{}, len(values))
|
||||
for i, v := range values {
|
||||
targetValues[i] = v
|
||||
}
|
||||
return a.tx.Delete(targetValues...)
|
||||
}
|
||||
|
||||
func (a tenantQueryManyToManyUsersTx) Clear() error {
|
||||
return a.tx.Clear()
|
||||
}
|
||||
|
||||
func (a tenantQueryManyToManyUsersTx) Count() int64 {
|
||||
return a.tx.Count()
|
||||
}
|
||||
|
||||
func (a tenantQueryManyToManyUsersTx) Unscoped() *tenantQueryManyToManyUsersTx {
|
||||
a.tx = a.tx.Unscoped()
|
||||
return &a
|
||||
}
|
||||
|
||||
type tenantQueryDo struct{ gen.DO }
|
||||
|
||||
func (t tenantQueryDo) Debug() *tenantQueryDo {
|
||||
|
||||
Reference in New Issue
Block a user