Commit 7331220e authored by Edric Li's avatar Edric Li
Browse files

Merge remote-tracking branch 'upstream/main'

# Conflicts:
#	frontend/src/components/account/CreateAccountModal.vue
parents fb86002e 4f13c8de
...@@ -41,12 +41,16 @@ const ( ...@@ -41,12 +41,16 @@ const (
FieldWeeklyLimitUsd = "weekly_limit_usd" FieldWeeklyLimitUsd = "weekly_limit_usd"
// FieldMonthlyLimitUsd holds the string denoting the monthly_limit_usd field in the database. // FieldMonthlyLimitUsd holds the string denoting the monthly_limit_usd field in the database.
FieldMonthlyLimitUsd = "monthly_limit_usd" FieldMonthlyLimitUsd = "monthly_limit_usd"
// FieldDefaultValidityDays holds the string denoting the default_validity_days field in the database.
FieldDefaultValidityDays = "default_validity_days"
// EdgeAPIKeys holds the string denoting the api_keys edge name in mutations. // EdgeAPIKeys holds the string denoting the api_keys edge name in mutations.
EdgeAPIKeys = "api_keys" EdgeAPIKeys = "api_keys"
// EdgeRedeemCodes holds the string denoting the redeem_codes edge name in mutations. // EdgeRedeemCodes holds the string denoting the redeem_codes edge name in mutations.
EdgeRedeemCodes = "redeem_codes" EdgeRedeemCodes = "redeem_codes"
// EdgeSubscriptions holds the string denoting the subscriptions edge name in mutations. // EdgeSubscriptions holds the string denoting the subscriptions edge name in mutations.
EdgeSubscriptions = "subscriptions" EdgeSubscriptions = "subscriptions"
// EdgeUsageLogs holds the string denoting the usage_logs edge name in mutations.
EdgeUsageLogs = "usage_logs"
// EdgeAccounts holds the string denoting the accounts edge name in mutations. // EdgeAccounts holds the string denoting the accounts edge name in mutations.
EdgeAccounts = "accounts" EdgeAccounts = "accounts"
// EdgeAllowedUsers holds the string denoting the allowed_users edge name in mutations. // EdgeAllowedUsers holds the string denoting the allowed_users edge name in mutations.
...@@ -78,6 +82,13 @@ const ( ...@@ -78,6 +82,13 @@ const (
SubscriptionsInverseTable = "user_subscriptions" SubscriptionsInverseTable = "user_subscriptions"
// SubscriptionsColumn is the table column denoting the subscriptions relation/edge. // SubscriptionsColumn is the table column denoting the subscriptions relation/edge.
SubscriptionsColumn = "group_id" SubscriptionsColumn = "group_id"
// UsageLogsTable is the table that holds the usage_logs relation/edge.
UsageLogsTable = "usage_logs"
// UsageLogsInverseTable is the table name for the UsageLog entity.
// It exists in this package in order to avoid circular dependency with the "usagelog" package.
UsageLogsInverseTable = "usage_logs"
// UsageLogsColumn is the table column denoting the usage_logs relation/edge.
UsageLogsColumn = "group_id"
// AccountsTable is the table that holds the accounts relation/edge. The primary key declared below. // AccountsTable is the table that holds the accounts relation/edge. The primary key declared below.
AccountsTable = "account_groups" AccountsTable = "account_groups"
// AccountsInverseTable is the table name for the Account entity. // AccountsInverseTable is the table name for the Account entity.
...@@ -120,6 +131,7 @@ var Columns = []string{ ...@@ -120,6 +131,7 @@ var Columns = []string{
FieldDailyLimitUsd, FieldDailyLimitUsd,
FieldWeeklyLimitUsd, FieldWeeklyLimitUsd,
FieldMonthlyLimitUsd, FieldMonthlyLimitUsd,
FieldDefaultValidityDays,
} }
var ( var (
...@@ -173,6 +185,8 @@ var ( ...@@ -173,6 +185,8 @@ var (
DefaultSubscriptionType string DefaultSubscriptionType string
// SubscriptionTypeValidator is a validator for the "subscription_type" field. It is called by the builders before save. // SubscriptionTypeValidator is a validator for the "subscription_type" field. It is called by the builders before save.
SubscriptionTypeValidator func(string) error SubscriptionTypeValidator func(string) error
// DefaultDefaultValidityDays holds the default value on creation for the "default_validity_days" field.
DefaultDefaultValidityDays int
) )
// OrderOption defines the ordering options for the Group queries. // OrderOption defines the ordering options for the Group queries.
...@@ -248,6 +262,11 @@ func ByMonthlyLimitUsd(opts ...sql.OrderTermOption) OrderOption { ...@@ -248,6 +262,11 @@ func ByMonthlyLimitUsd(opts ...sql.OrderTermOption) OrderOption {
return sql.OrderByField(FieldMonthlyLimitUsd, opts...).ToFunc() return sql.OrderByField(FieldMonthlyLimitUsd, opts...).ToFunc()
} }
// ByDefaultValidityDays orders the results by the default_validity_days field.
func ByDefaultValidityDays(opts ...sql.OrderTermOption) OrderOption {
return sql.OrderByField(FieldDefaultValidityDays, opts...).ToFunc()
}
// ByAPIKeysCount orders the results by api_keys count. // ByAPIKeysCount orders the results by api_keys count.
func ByAPIKeysCount(opts ...sql.OrderTermOption) OrderOption { func ByAPIKeysCount(opts ...sql.OrderTermOption) OrderOption {
return func(s *sql.Selector) { return func(s *sql.Selector) {
...@@ -290,6 +309,20 @@ func BySubscriptions(term sql.OrderTerm, terms ...sql.OrderTerm) OrderOption { ...@@ -290,6 +309,20 @@ func BySubscriptions(term sql.OrderTerm, terms ...sql.OrderTerm) OrderOption {
} }
} }
// ByUsageLogsCount orders the results by usage_logs count.
func ByUsageLogsCount(opts ...sql.OrderTermOption) OrderOption {
return func(s *sql.Selector) {
sqlgraph.OrderByNeighborsCount(s, newUsageLogsStep(), opts...)
}
}
// ByUsageLogs orders the results by usage_logs terms.
func ByUsageLogs(term sql.OrderTerm, terms ...sql.OrderTerm) OrderOption {
return func(s *sql.Selector) {
sqlgraph.OrderByNeighborTerms(s, newUsageLogsStep(), append([]sql.OrderTerm{term}, terms...)...)
}
}
// ByAccountsCount orders the results by accounts count. // ByAccountsCount orders the results by accounts count.
func ByAccountsCount(opts ...sql.OrderTermOption) OrderOption { func ByAccountsCount(opts ...sql.OrderTermOption) OrderOption {
return func(s *sql.Selector) { return func(s *sql.Selector) {
...@@ -366,6 +399,13 @@ func newSubscriptionsStep() *sqlgraph.Step { ...@@ -366,6 +399,13 @@ func newSubscriptionsStep() *sqlgraph.Step {
sqlgraph.Edge(sqlgraph.O2M, false, SubscriptionsTable, SubscriptionsColumn), sqlgraph.Edge(sqlgraph.O2M, false, SubscriptionsTable, SubscriptionsColumn),
) )
} }
func newUsageLogsStep() *sqlgraph.Step {
return sqlgraph.NewStep(
sqlgraph.From(Table, FieldID),
sqlgraph.To(UsageLogsInverseTable, FieldID),
sqlgraph.Edge(sqlgraph.O2M, false, UsageLogsTable, UsageLogsColumn),
)
}
func newAccountsStep() *sqlgraph.Step { func newAccountsStep() *sqlgraph.Step {
return sqlgraph.NewStep( return sqlgraph.NewStep(
sqlgraph.From(Table, FieldID), sqlgraph.From(Table, FieldID),
......
...@@ -120,6 +120,11 @@ func MonthlyLimitUsd(v float64) predicate.Group { ...@@ -120,6 +120,11 @@ func MonthlyLimitUsd(v float64) predicate.Group {
return predicate.Group(sql.FieldEQ(FieldMonthlyLimitUsd, v)) return predicate.Group(sql.FieldEQ(FieldMonthlyLimitUsd, v))
} }
// DefaultValidityDays applies equality check predicate on the "default_validity_days" field. It's identical to DefaultValidityDaysEQ.
func DefaultValidityDays(v int) predicate.Group {
return predicate.Group(sql.FieldEQ(FieldDefaultValidityDays, v))
}
// CreatedAtEQ applies the EQ predicate on the "created_at" field. // CreatedAtEQ applies the EQ predicate on the "created_at" field.
func CreatedAtEQ(v time.Time) predicate.Group { func CreatedAtEQ(v time.Time) predicate.Group {
return predicate.Group(sql.FieldEQ(FieldCreatedAt, v)) return predicate.Group(sql.FieldEQ(FieldCreatedAt, v))
...@@ -785,6 +790,46 @@ func MonthlyLimitUsdNotNil() predicate.Group { ...@@ -785,6 +790,46 @@ func MonthlyLimitUsdNotNil() predicate.Group {
return predicate.Group(sql.FieldNotNull(FieldMonthlyLimitUsd)) return predicate.Group(sql.FieldNotNull(FieldMonthlyLimitUsd))
} }
// DefaultValidityDaysEQ applies the EQ predicate on the "default_validity_days" field.
func DefaultValidityDaysEQ(v int) predicate.Group {
return predicate.Group(sql.FieldEQ(FieldDefaultValidityDays, v))
}
// DefaultValidityDaysNEQ applies the NEQ predicate on the "default_validity_days" field.
func DefaultValidityDaysNEQ(v int) predicate.Group {
return predicate.Group(sql.FieldNEQ(FieldDefaultValidityDays, v))
}
// DefaultValidityDaysIn applies the In predicate on the "default_validity_days" field.
func DefaultValidityDaysIn(vs ...int) predicate.Group {
return predicate.Group(sql.FieldIn(FieldDefaultValidityDays, vs...))
}
// DefaultValidityDaysNotIn applies the NotIn predicate on the "default_validity_days" field.
func DefaultValidityDaysNotIn(vs ...int) predicate.Group {
return predicate.Group(sql.FieldNotIn(FieldDefaultValidityDays, vs...))
}
// DefaultValidityDaysGT applies the GT predicate on the "default_validity_days" field.
func DefaultValidityDaysGT(v int) predicate.Group {
return predicate.Group(sql.FieldGT(FieldDefaultValidityDays, v))
}
// DefaultValidityDaysGTE applies the GTE predicate on the "default_validity_days" field.
func DefaultValidityDaysGTE(v int) predicate.Group {
return predicate.Group(sql.FieldGTE(FieldDefaultValidityDays, v))
}
// DefaultValidityDaysLT applies the LT predicate on the "default_validity_days" field.
func DefaultValidityDaysLT(v int) predicate.Group {
return predicate.Group(sql.FieldLT(FieldDefaultValidityDays, v))
}
// DefaultValidityDaysLTE applies the LTE predicate on the "default_validity_days" field.
func DefaultValidityDaysLTE(v int) predicate.Group {
return predicate.Group(sql.FieldLTE(FieldDefaultValidityDays, v))
}
// HasAPIKeys applies the HasEdge predicate on the "api_keys" edge. // HasAPIKeys applies the HasEdge predicate on the "api_keys" edge.
func HasAPIKeys() predicate.Group { func HasAPIKeys() predicate.Group {
return predicate.Group(func(s *sql.Selector) { return predicate.Group(func(s *sql.Selector) {
...@@ -854,6 +899,29 @@ func HasSubscriptionsWith(preds ...predicate.UserSubscription) predicate.Group { ...@@ -854,6 +899,29 @@ func HasSubscriptionsWith(preds ...predicate.UserSubscription) predicate.Group {
}) })
} }
// HasUsageLogs applies the HasEdge predicate on the "usage_logs" edge.
func HasUsageLogs() predicate.Group {
return predicate.Group(func(s *sql.Selector) {
step := sqlgraph.NewStep(
sqlgraph.From(Table, FieldID),
sqlgraph.Edge(sqlgraph.O2M, false, UsageLogsTable, UsageLogsColumn),
)
sqlgraph.HasNeighbors(s, step)
})
}
// HasUsageLogsWith applies the HasEdge predicate on the "usage_logs" edge with a given conditions (other predicates).
func HasUsageLogsWith(preds ...predicate.UsageLog) predicate.Group {
return predicate.Group(func(s *sql.Selector) {
step := newUsageLogsStep()
sqlgraph.HasNeighborsWith(s, step, func(s *sql.Selector) {
for _, p := range preds {
p(s)
}
})
})
}
// HasAccounts applies the HasEdge predicate on the "accounts" edge. // HasAccounts applies the HasEdge predicate on the "accounts" edge.
func HasAccounts() predicate.Group { func HasAccounts() predicate.Group {
return predicate.Group(func(s *sql.Selector) { return predicate.Group(func(s *sql.Selector) {
......
...@@ -15,6 +15,7 @@ import ( ...@@ -15,6 +15,7 @@ import (
"github.com/Wei-Shaw/sub2api/ent/apikey" "github.com/Wei-Shaw/sub2api/ent/apikey"
"github.com/Wei-Shaw/sub2api/ent/group" "github.com/Wei-Shaw/sub2api/ent/group"
"github.com/Wei-Shaw/sub2api/ent/redeemcode" "github.com/Wei-Shaw/sub2api/ent/redeemcode"
"github.com/Wei-Shaw/sub2api/ent/usagelog"
"github.com/Wei-Shaw/sub2api/ent/user" "github.com/Wei-Shaw/sub2api/ent/user"
"github.com/Wei-Shaw/sub2api/ent/usersubscription" "github.com/Wei-Shaw/sub2api/ent/usersubscription"
) )
...@@ -201,6 +202,20 @@ func (_c *GroupCreate) SetNillableMonthlyLimitUsd(v *float64) *GroupCreate { ...@@ -201,6 +202,20 @@ func (_c *GroupCreate) SetNillableMonthlyLimitUsd(v *float64) *GroupCreate {
return _c return _c
} }
// SetDefaultValidityDays sets the "default_validity_days" field.
func (_c *GroupCreate) SetDefaultValidityDays(v int) *GroupCreate {
_c.mutation.SetDefaultValidityDays(v)
return _c
}
// SetNillableDefaultValidityDays sets the "default_validity_days" field if the given value is not nil.
func (_c *GroupCreate) SetNillableDefaultValidityDays(v *int) *GroupCreate {
if v != nil {
_c.SetDefaultValidityDays(*v)
}
return _c
}
// AddAPIKeyIDs adds the "api_keys" edge to the ApiKey entity by IDs. // AddAPIKeyIDs adds the "api_keys" edge to the ApiKey entity by IDs.
func (_c *GroupCreate) AddAPIKeyIDs(ids ...int64) *GroupCreate { func (_c *GroupCreate) AddAPIKeyIDs(ids ...int64) *GroupCreate {
_c.mutation.AddAPIKeyIDs(ids...) _c.mutation.AddAPIKeyIDs(ids...)
...@@ -246,6 +261,21 @@ func (_c *GroupCreate) AddSubscriptions(v ...*UserSubscription) *GroupCreate { ...@@ -246,6 +261,21 @@ func (_c *GroupCreate) AddSubscriptions(v ...*UserSubscription) *GroupCreate {
return _c.AddSubscriptionIDs(ids...) return _c.AddSubscriptionIDs(ids...)
} }
// AddUsageLogIDs adds the "usage_logs" edge to the UsageLog entity by IDs.
func (_c *GroupCreate) AddUsageLogIDs(ids ...int64) *GroupCreate {
_c.mutation.AddUsageLogIDs(ids...)
return _c
}
// AddUsageLogs adds the "usage_logs" edges to the UsageLog entity.
func (_c *GroupCreate) AddUsageLogs(v ...*UsageLog) *GroupCreate {
ids := make([]int64, len(v))
for i := range v {
ids[i] = v[i].ID
}
return _c.AddUsageLogIDs(ids...)
}
// AddAccountIDs adds the "accounts" edge to the Account entity by IDs. // AddAccountIDs adds the "accounts" edge to the Account entity by IDs.
func (_c *GroupCreate) AddAccountIDs(ids ...int64) *GroupCreate { func (_c *GroupCreate) AddAccountIDs(ids ...int64) *GroupCreate {
_c.mutation.AddAccountIDs(ids...) _c.mutation.AddAccountIDs(ids...)
...@@ -347,6 +377,10 @@ func (_c *GroupCreate) defaults() error { ...@@ -347,6 +377,10 @@ func (_c *GroupCreate) defaults() error {
v := group.DefaultSubscriptionType v := group.DefaultSubscriptionType
_c.mutation.SetSubscriptionType(v) _c.mutation.SetSubscriptionType(v)
} }
if _, ok := _c.mutation.DefaultValidityDays(); !ok {
v := group.DefaultDefaultValidityDays
_c.mutation.SetDefaultValidityDays(v)
}
return nil return nil
} }
...@@ -396,6 +430,9 @@ func (_c *GroupCreate) check() error { ...@@ -396,6 +430,9 @@ func (_c *GroupCreate) check() error {
return &ValidationError{Name: "subscription_type", err: fmt.Errorf(`ent: validator failed for field "Group.subscription_type": %w`, err)} return &ValidationError{Name: "subscription_type", err: fmt.Errorf(`ent: validator failed for field "Group.subscription_type": %w`, err)}
} }
} }
if _, ok := _c.mutation.DefaultValidityDays(); !ok {
return &ValidationError{Name: "default_validity_days", err: errors.New(`ent: missing required field "Group.default_validity_days"`)}
}
return nil return nil
} }
...@@ -475,6 +512,10 @@ func (_c *GroupCreate) createSpec() (*Group, *sqlgraph.CreateSpec) { ...@@ -475,6 +512,10 @@ func (_c *GroupCreate) createSpec() (*Group, *sqlgraph.CreateSpec) {
_spec.SetField(group.FieldMonthlyLimitUsd, field.TypeFloat64, value) _spec.SetField(group.FieldMonthlyLimitUsd, field.TypeFloat64, value)
_node.MonthlyLimitUsd = &value _node.MonthlyLimitUsd = &value
} }
if value, ok := _c.mutation.DefaultValidityDays(); ok {
_spec.SetField(group.FieldDefaultValidityDays, field.TypeInt, value)
_node.DefaultValidityDays = value
}
if nodes := _c.mutation.APIKeysIDs(); len(nodes) > 0 { if nodes := _c.mutation.APIKeysIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{ edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M, Rel: sqlgraph.O2M,
...@@ -523,6 +564,22 @@ func (_c *GroupCreate) createSpec() (*Group, *sqlgraph.CreateSpec) { ...@@ -523,6 +564,22 @@ func (_c *GroupCreate) createSpec() (*Group, *sqlgraph.CreateSpec) {
} }
_spec.Edges = append(_spec.Edges, edge) _spec.Edges = append(_spec.Edges, edge)
} }
if nodes := _c.mutation.UsageLogsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: group.UsageLogsTable,
Columns: []string{group.UsageLogsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(usagelog.FieldID, field.TypeInt64),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := _c.mutation.AccountsIDs(); len(nodes) > 0 { if nodes := _c.mutation.AccountsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{ edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2M, Rel: sqlgraph.M2M,
...@@ -813,6 +870,24 @@ func (u *GroupUpsert) ClearMonthlyLimitUsd() *GroupUpsert { ...@@ -813,6 +870,24 @@ func (u *GroupUpsert) ClearMonthlyLimitUsd() *GroupUpsert {
return u return u
} }
// SetDefaultValidityDays sets the "default_validity_days" field.
func (u *GroupUpsert) SetDefaultValidityDays(v int) *GroupUpsert {
u.Set(group.FieldDefaultValidityDays, v)
return u
}
// UpdateDefaultValidityDays sets the "default_validity_days" field to the value that was provided on create.
func (u *GroupUpsert) UpdateDefaultValidityDays() *GroupUpsert {
u.SetExcluded(group.FieldDefaultValidityDays)
return u
}
// AddDefaultValidityDays adds v to the "default_validity_days" field.
func (u *GroupUpsert) AddDefaultValidityDays(v int) *GroupUpsert {
u.Add(group.FieldDefaultValidityDays, v)
return u
}
// UpdateNewValues updates the mutable fields using the new values that were set on create. // UpdateNewValues updates the mutable fields using the new values that were set on create.
// Using this option is equivalent to using: // Using this option is equivalent to using:
// //
...@@ -1089,6 +1164,27 @@ func (u *GroupUpsertOne) ClearMonthlyLimitUsd() *GroupUpsertOne { ...@@ -1089,6 +1164,27 @@ func (u *GroupUpsertOne) ClearMonthlyLimitUsd() *GroupUpsertOne {
}) })
} }
// SetDefaultValidityDays sets the "default_validity_days" field.
func (u *GroupUpsertOne) SetDefaultValidityDays(v int) *GroupUpsertOne {
return u.Update(func(s *GroupUpsert) {
s.SetDefaultValidityDays(v)
})
}
// AddDefaultValidityDays adds v to the "default_validity_days" field.
func (u *GroupUpsertOne) AddDefaultValidityDays(v int) *GroupUpsertOne {
return u.Update(func(s *GroupUpsert) {
s.AddDefaultValidityDays(v)
})
}
// UpdateDefaultValidityDays sets the "default_validity_days" field to the value that was provided on create.
func (u *GroupUpsertOne) UpdateDefaultValidityDays() *GroupUpsertOne {
return u.Update(func(s *GroupUpsert) {
s.UpdateDefaultValidityDays()
})
}
// Exec executes the query. // Exec executes the query.
func (u *GroupUpsertOne) Exec(ctx context.Context) error { func (u *GroupUpsertOne) Exec(ctx context.Context) error {
if len(u.create.conflict) == 0 { if len(u.create.conflict) == 0 {
...@@ -1531,6 +1627,27 @@ func (u *GroupUpsertBulk) ClearMonthlyLimitUsd() *GroupUpsertBulk { ...@@ -1531,6 +1627,27 @@ func (u *GroupUpsertBulk) ClearMonthlyLimitUsd() *GroupUpsertBulk {
}) })
} }
// SetDefaultValidityDays sets the "default_validity_days" field.
func (u *GroupUpsertBulk) SetDefaultValidityDays(v int) *GroupUpsertBulk {
return u.Update(func(s *GroupUpsert) {
s.SetDefaultValidityDays(v)
})
}
// AddDefaultValidityDays adds v to the "default_validity_days" field.
func (u *GroupUpsertBulk) AddDefaultValidityDays(v int) *GroupUpsertBulk {
return u.Update(func(s *GroupUpsert) {
s.AddDefaultValidityDays(v)
})
}
// UpdateDefaultValidityDays sets the "default_validity_days" field to the value that was provided on create.
func (u *GroupUpsertBulk) UpdateDefaultValidityDays() *GroupUpsertBulk {
return u.Update(func(s *GroupUpsert) {
s.UpdateDefaultValidityDays()
})
}
// Exec executes the query. // Exec executes the query.
func (u *GroupUpsertBulk) Exec(ctx context.Context) error { func (u *GroupUpsertBulk) Exec(ctx context.Context) error {
if u.create.err != nil { if u.create.err != nil {
......
...@@ -18,6 +18,7 @@ import ( ...@@ -18,6 +18,7 @@ import (
"github.com/Wei-Shaw/sub2api/ent/group" "github.com/Wei-Shaw/sub2api/ent/group"
"github.com/Wei-Shaw/sub2api/ent/predicate" "github.com/Wei-Shaw/sub2api/ent/predicate"
"github.com/Wei-Shaw/sub2api/ent/redeemcode" "github.com/Wei-Shaw/sub2api/ent/redeemcode"
"github.com/Wei-Shaw/sub2api/ent/usagelog"
"github.com/Wei-Shaw/sub2api/ent/user" "github.com/Wei-Shaw/sub2api/ent/user"
"github.com/Wei-Shaw/sub2api/ent/userallowedgroup" "github.com/Wei-Shaw/sub2api/ent/userallowedgroup"
"github.com/Wei-Shaw/sub2api/ent/usersubscription" "github.com/Wei-Shaw/sub2api/ent/usersubscription"
...@@ -33,6 +34,7 @@ type GroupQuery struct { ...@@ -33,6 +34,7 @@ type GroupQuery struct {
withAPIKeys *ApiKeyQuery withAPIKeys *ApiKeyQuery
withRedeemCodes *RedeemCodeQuery withRedeemCodes *RedeemCodeQuery
withSubscriptions *UserSubscriptionQuery withSubscriptions *UserSubscriptionQuery
withUsageLogs *UsageLogQuery
withAccounts *AccountQuery withAccounts *AccountQuery
withAllowedUsers *UserQuery withAllowedUsers *UserQuery
withAccountGroups *AccountGroupQuery withAccountGroups *AccountGroupQuery
...@@ -139,6 +141,28 @@ func (_q *GroupQuery) QuerySubscriptions() *UserSubscriptionQuery { ...@@ -139,6 +141,28 @@ func (_q *GroupQuery) QuerySubscriptions() *UserSubscriptionQuery {
return query return query
} }
// QueryUsageLogs chains the current query on the "usage_logs" edge.
func (_q *GroupQuery) QueryUsageLogs() *UsageLogQuery {
query := (&UsageLogClient{config: _q.config}).Query()
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
if err := _q.prepareQuery(ctx); err != nil {
return nil, err
}
selector := _q.sqlQuery(ctx)
if err := selector.Err(); err != nil {
return nil, err
}
step := sqlgraph.NewStep(
sqlgraph.From(group.Table, group.FieldID, selector),
sqlgraph.To(usagelog.Table, usagelog.FieldID),
sqlgraph.Edge(sqlgraph.O2M, false, group.UsageLogsTable, group.UsageLogsColumn),
)
fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
return fromU, nil
}
return query
}
// QueryAccounts chains the current query on the "accounts" edge. // QueryAccounts chains the current query on the "accounts" edge.
func (_q *GroupQuery) QueryAccounts() *AccountQuery { func (_q *GroupQuery) QueryAccounts() *AccountQuery {
query := (&AccountClient{config: _q.config}).Query() query := (&AccountClient{config: _q.config}).Query()
...@@ -422,6 +446,7 @@ func (_q *GroupQuery) Clone() *GroupQuery { ...@@ -422,6 +446,7 @@ func (_q *GroupQuery) Clone() *GroupQuery {
withAPIKeys: _q.withAPIKeys.Clone(), withAPIKeys: _q.withAPIKeys.Clone(),
withRedeemCodes: _q.withRedeemCodes.Clone(), withRedeemCodes: _q.withRedeemCodes.Clone(),
withSubscriptions: _q.withSubscriptions.Clone(), withSubscriptions: _q.withSubscriptions.Clone(),
withUsageLogs: _q.withUsageLogs.Clone(),
withAccounts: _q.withAccounts.Clone(), withAccounts: _q.withAccounts.Clone(),
withAllowedUsers: _q.withAllowedUsers.Clone(), withAllowedUsers: _q.withAllowedUsers.Clone(),
withAccountGroups: _q.withAccountGroups.Clone(), withAccountGroups: _q.withAccountGroups.Clone(),
...@@ -465,6 +490,17 @@ func (_q *GroupQuery) WithSubscriptions(opts ...func(*UserSubscriptionQuery)) *G ...@@ -465,6 +490,17 @@ func (_q *GroupQuery) WithSubscriptions(opts ...func(*UserSubscriptionQuery)) *G
return _q return _q
} }
// WithUsageLogs tells the query-builder to eager-load the nodes that are connected to
// the "usage_logs" edge. The optional arguments are used to configure the query builder of the edge.
func (_q *GroupQuery) WithUsageLogs(opts ...func(*UsageLogQuery)) *GroupQuery {
query := (&UsageLogClient{config: _q.config}).Query()
for _, opt := range opts {
opt(query)
}
_q.withUsageLogs = query
return _q
}
// WithAccounts tells the query-builder to eager-load the nodes that are connected to // WithAccounts tells the query-builder to eager-load the nodes that are connected to
// the "accounts" edge. The optional arguments are used to configure the query builder of the edge. // the "accounts" edge. The optional arguments are used to configure the query builder of the edge.
func (_q *GroupQuery) WithAccounts(opts ...func(*AccountQuery)) *GroupQuery { func (_q *GroupQuery) WithAccounts(opts ...func(*AccountQuery)) *GroupQuery {
...@@ -587,10 +623,11 @@ func (_q *GroupQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*Group, ...@@ -587,10 +623,11 @@ func (_q *GroupQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*Group,
var ( var (
nodes = []*Group{} nodes = []*Group{}
_spec = _q.querySpec() _spec = _q.querySpec()
loadedTypes = [7]bool{ loadedTypes = [8]bool{
_q.withAPIKeys != nil, _q.withAPIKeys != nil,
_q.withRedeemCodes != nil, _q.withRedeemCodes != nil,
_q.withSubscriptions != nil, _q.withSubscriptions != nil,
_q.withUsageLogs != nil,
_q.withAccounts != nil, _q.withAccounts != nil,
_q.withAllowedUsers != nil, _q.withAllowedUsers != nil,
_q.withAccountGroups != nil, _q.withAccountGroups != nil,
...@@ -636,6 +673,13 @@ func (_q *GroupQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*Group, ...@@ -636,6 +673,13 @@ func (_q *GroupQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*Group,
return nil, err return nil, err
} }
} }
if query := _q.withUsageLogs; query != nil {
if err := _q.loadUsageLogs(ctx, query, nodes,
func(n *Group) { n.Edges.UsageLogs = []*UsageLog{} },
func(n *Group, e *UsageLog) { n.Edges.UsageLogs = append(n.Edges.UsageLogs, e) }); err != nil {
return nil, err
}
}
if query := _q.withAccounts; query != nil { if query := _q.withAccounts; query != nil {
if err := _q.loadAccounts(ctx, query, nodes, if err := _q.loadAccounts(ctx, query, nodes,
func(n *Group) { n.Edges.Accounts = []*Account{} }, func(n *Group) { n.Edges.Accounts = []*Account{} },
...@@ -763,6 +807,39 @@ func (_q *GroupQuery) loadSubscriptions(ctx context.Context, query *UserSubscrip ...@@ -763,6 +807,39 @@ func (_q *GroupQuery) loadSubscriptions(ctx context.Context, query *UserSubscrip
} }
return nil return nil
} }
func (_q *GroupQuery) loadUsageLogs(ctx context.Context, query *UsageLogQuery, nodes []*Group, init func(*Group), assign func(*Group, *UsageLog)) error {
fks := make([]driver.Value, 0, len(nodes))
nodeids := make(map[int64]*Group)
for i := range nodes {
fks = append(fks, nodes[i].ID)
nodeids[nodes[i].ID] = nodes[i]
if init != nil {
init(nodes[i])
}
}
if len(query.ctx.Fields) > 0 {
query.ctx.AppendFieldOnce(usagelog.FieldGroupID)
}
query.Where(predicate.UsageLog(func(s *sql.Selector) {
s.Where(sql.InValues(s.C(group.UsageLogsColumn), fks...))
}))
neighbors, err := query.All(ctx)
if err != nil {
return err
}
for _, n := range neighbors {
fk := n.GroupID
if fk == nil {
return fmt.Errorf(`foreign-key "group_id" is nil for node %v`, n.ID)
}
node, ok := nodeids[*fk]
if !ok {
return fmt.Errorf(`unexpected referenced foreign-key "group_id" returned %v for node %v`, *fk, n.ID)
}
assign(node, n)
}
return nil
}
func (_q *GroupQuery) loadAccounts(ctx context.Context, query *AccountQuery, nodes []*Group, init func(*Group), assign func(*Group, *Account)) error { func (_q *GroupQuery) loadAccounts(ctx context.Context, query *AccountQuery, nodes []*Group, init func(*Group), assign func(*Group, *Account)) error {
edgeIDs := make([]driver.Value, len(nodes)) edgeIDs := make([]driver.Value, len(nodes))
byID := make(map[int64]*Group) byID := make(map[int64]*Group)
......
...@@ -16,6 +16,7 @@ import ( ...@@ -16,6 +16,7 @@ import (
"github.com/Wei-Shaw/sub2api/ent/group" "github.com/Wei-Shaw/sub2api/ent/group"
"github.com/Wei-Shaw/sub2api/ent/predicate" "github.com/Wei-Shaw/sub2api/ent/predicate"
"github.com/Wei-Shaw/sub2api/ent/redeemcode" "github.com/Wei-Shaw/sub2api/ent/redeemcode"
"github.com/Wei-Shaw/sub2api/ent/usagelog"
"github.com/Wei-Shaw/sub2api/ent/user" "github.com/Wei-Shaw/sub2api/ent/user"
"github.com/Wei-Shaw/sub2api/ent/usersubscription" "github.com/Wei-Shaw/sub2api/ent/usersubscription"
) )
...@@ -251,6 +252,27 @@ func (_u *GroupUpdate) ClearMonthlyLimitUsd() *GroupUpdate { ...@@ -251,6 +252,27 @@ func (_u *GroupUpdate) ClearMonthlyLimitUsd() *GroupUpdate {
return _u return _u
} }
// SetDefaultValidityDays sets the "default_validity_days" field.
func (_u *GroupUpdate) SetDefaultValidityDays(v int) *GroupUpdate {
_u.mutation.ResetDefaultValidityDays()
_u.mutation.SetDefaultValidityDays(v)
return _u
}
// SetNillableDefaultValidityDays sets the "default_validity_days" field if the given value is not nil.
func (_u *GroupUpdate) SetNillableDefaultValidityDays(v *int) *GroupUpdate {
if v != nil {
_u.SetDefaultValidityDays(*v)
}
return _u
}
// AddDefaultValidityDays adds value to the "default_validity_days" field.
func (_u *GroupUpdate) AddDefaultValidityDays(v int) *GroupUpdate {
_u.mutation.AddDefaultValidityDays(v)
return _u
}
// AddAPIKeyIDs adds the "api_keys" edge to the ApiKey entity by IDs. // AddAPIKeyIDs adds the "api_keys" edge to the ApiKey entity by IDs.
func (_u *GroupUpdate) AddAPIKeyIDs(ids ...int64) *GroupUpdate { func (_u *GroupUpdate) AddAPIKeyIDs(ids ...int64) *GroupUpdate {
_u.mutation.AddAPIKeyIDs(ids...) _u.mutation.AddAPIKeyIDs(ids...)
...@@ -296,6 +318,21 @@ func (_u *GroupUpdate) AddSubscriptions(v ...*UserSubscription) *GroupUpdate { ...@@ -296,6 +318,21 @@ func (_u *GroupUpdate) AddSubscriptions(v ...*UserSubscription) *GroupUpdate {
return _u.AddSubscriptionIDs(ids...) return _u.AddSubscriptionIDs(ids...)
} }
// AddUsageLogIDs adds the "usage_logs" edge to the UsageLog entity by IDs.
func (_u *GroupUpdate) AddUsageLogIDs(ids ...int64) *GroupUpdate {
_u.mutation.AddUsageLogIDs(ids...)
return _u
}
// AddUsageLogs adds the "usage_logs" edges to the UsageLog entity.
func (_u *GroupUpdate) AddUsageLogs(v ...*UsageLog) *GroupUpdate {
ids := make([]int64, len(v))
for i := range v {
ids[i] = v[i].ID
}
return _u.AddUsageLogIDs(ids...)
}
// AddAccountIDs adds the "accounts" edge to the Account entity by IDs. // AddAccountIDs adds the "accounts" edge to the Account entity by IDs.
func (_u *GroupUpdate) AddAccountIDs(ids ...int64) *GroupUpdate { func (_u *GroupUpdate) AddAccountIDs(ids ...int64) *GroupUpdate {
_u.mutation.AddAccountIDs(ids...) _u.mutation.AddAccountIDs(ids...)
...@@ -394,6 +431,27 @@ func (_u *GroupUpdate) RemoveSubscriptions(v ...*UserSubscription) *GroupUpdate ...@@ -394,6 +431,27 @@ func (_u *GroupUpdate) RemoveSubscriptions(v ...*UserSubscription) *GroupUpdate
return _u.RemoveSubscriptionIDs(ids...) return _u.RemoveSubscriptionIDs(ids...)
} }
// ClearUsageLogs clears all "usage_logs" edges to the UsageLog entity.
func (_u *GroupUpdate) ClearUsageLogs() *GroupUpdate {
_u.mutation.ClearUsageLogs()
return _u
}
// RemoveUsageLogIDs removes the "usage_logs" edge to UsageLog entities by IDs.
func (_u *GroupUpdate) RemoveUsageLogIDs(ids ...int64) *GroupUpdate {
_u.mutation.RemoveUsageLogIDs(ids...)
return _u
}
// RemoveUsageLogs removes "usage_logs" edges to UsageLog entities.
func (_u *GroupUpdate) RemoveUsageLogs(v ...*UsageLog) *GroupUpdate {
ids := make([]int64, len(v))
for i := range v {
ids[i] = v[i].ID
}
return _u.RemoveUsageLogIDs(ids...)
}
// ClearAccounts clears all "accounts" edges to the Account entity. // ClearAccounts clears all "accounts" edges to the Account entity.
func (_u *GroupUpdate) ClearAccounts() *GroupUpdate { func (_u *GroupUpdate) ClearAccounts() *GroupUpdate {
_u.mutation.ClearAccounts() _u.mutation.ClearAccounts()
...@@ -578,6 +636,12 @@ func (_u *GroupUpdate) sqlSave(ctx context.Context) (_node int, err error) { ...@@ -578,6 +636,12 @@ func (_u *GroupUpdate) sqlSave(ctx context.Context) (_node int, err error) {
if _u.mutation.MonthlyLimitUsdCleared() { if _u.mutation.MonthlyLimitUsdCleared() {
_spec.ClearField(group.FieldMonthlyLimitUsd, field.TypeFloat64) _spec.ClearField(group.FieldMonthlyLimitUsd, field.TypeFloat64)
} }
if value, ok := _u.mutation.DefaultValidityDays(); ok {
_spec.SetField(group.FieldDefaultValidityDays, field.TypeInt, value)
}
if value, ok := _u.mutation.AddedDefaultValidityDays(); ok {
_spec.AddField(group.FieldDefaultValidityDays, field.TypeInt, value)
}
if _u.mutation.APIKeysCleared() { if _u.mutation.APIKeysCleared() {
edge := &sqlgraph.EdgeSpec{ edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M, Rel: sqlgraph.O2M,
...@@ -713,6 +777,51 @@ func (_u *GroupUpdate) sqlSave(ctx context.Context) (_node int, err error) { ...@@ -713,6 +777,51 @@ func (_u *GroupUpdate) sqlSave(ctx context.Context) (_node int, err error) {
} }
_spec.Edges.Add = append(_spec.Edges.Add, edge) _spec.Edges.Add = append(_spec.Edges.Add, edge)
} }
if _u.mutation.UsageLogsCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: group.UsageLogsTable,
Columns: []string{group.UsageLogsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(usagelog.FieldID, field.TypeInt64),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.RemovedUsageLogsIDs(); len(nodes) > 0 && !_u.mutation.UsageLogsCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: group.UsageLogsTable,
Columns: []string{group.UsageLogsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(usagelog.FieldID, field.TypeInt64),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.UsageLogsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: group.UsageLogsTable,
Columns: []string{group.UsageLogsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(usagelog.FieldID, field.TypeInt64),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
if _u.mutation.AccountsCleared() { if _u.mutation.AccountsCleared() {
edge := &sqlgraph.EdgeSpec{ edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2M, Rel: sqlgraph.M2M,
...@@ -1065,6 +1174,27 @@ func (_u *GroupUpdateOne) ClearMonthlyLimitUsd() *GroupUpdateOne { ...@@ -1065,6 +1174,27 @@ func (_u *GroupUpdateOne) ClearMonthlyLimitUsd() *GroupUpdateOne {
return _u return _u
} }
// SetDefaultValidityDays sets the "default_validity_days" field.
func (_u *GroupUpdateOne) SetDefaultValidityDays(v int) *GroupUpdateOne {
_u.mutation.ResetDefaultValidityDays()
_u.mutation.SetDefaultValidityDays(v)
return _u
}
// SetNillableDefaultValidityDays sets the "default_validity_days" field if the given value is not nil.
func (_u *GroupUpdateOne) SetNillableDefaultValidityDays(v *int) *GroupUpdateOne {
if v != nil {
_u.SetDefaultValidityDays(*v)
}
return _u
}
// AddDefaultValidityDays adds value to the "default_validity_days" field.
func (_u *GroupUpdateOne) AddDefaultValidityDays(v int) *GroupUpdateOne {
_u.mutation.AddDefaultValidityDays(v)
return _u
}
// AddAPIKeyIDs adds the "api_keys" edge to the ApiKey entity by IDs. // AddAPIKeyIDs adds the "api_keys" edge to the ApiKey entity by IDs.
func (_u *GroupUpdateOne) AddAPIKeyIDs(ids ...int64) *GroupUpdateOne { func (_u *GroupUpdateOne) AddAPIKeyIDs(ids ...int64) *GroupUpdateOne {
_u.mutation.AddAPIKeyIDs(ids...) _u.mutation.AddAPIKeyIDs(ids...)
...@@ -1110,6 +1240,21 @@ func (_u *GroupUpdateOne) AddSubscriptions(v ...*UserSubscription) *GroupUpdateO ...@@ -1110,6 +1240,21 @@ func (_u *GroupUpdateOne) AddSubscriptions(v ...*UserSubscription) *GroupUpdateO
return _u.AddSubscriptionIDs(ids...) return _u.AddSubscriptionIDs(ids...)
} }
// AddUsageLogIDs adds the "usage_logs" edge to the UsageLog entity by IDs.
func (_u *GroupUpdateOne) AddUsageLogIDs(ids ...int64) *GroupUpdateOne {
_u.mutation.AddUsageLogIDs(ids...)
return _u
}
// AddUsageLogs adds the "usage_logs" edges to the UsageLog entity.
func (_u *GroupUpdateOne) AddUsageLogs(v ...*UsageLog) *GroupUpdateOne {
ids := make([]int64, len(v))
for i := range v {
ids[i] = v[i].ID
}
return _u.AddUsageLogIDs(ids...)
}
// AddAccountIDs adds the "accounts" edge to the Account entity by IDs. // AddAccountIDs adds the "accounts" edge to the Account entity by IDs.
func (_u *GroupUpdateOne) AddAccountIDs(ids ...int64) *GroupUpdateOne { func (_u *GroupUpdateOne) AddAccountIDs(ids ...int64) *GroupUpdateOne {
_u.mutation.AddAccountIDs(ids...) _u.mutation.AddAccountIDs(ids...)
...@@ -1208,6 +1353,27 @@ func (_u *GroupUpdateOne) RemoveSubscriptions(v ...*UserSubscription) *GroupUpda ...@@ -1208,6 +1353,27 @@ func (_u *GroupUpdateOne) RemoveSubscriptions(v ...*UserSubscription) *GroupUpda
return _u.RemoveSubscriptionIDs(ids...) return _u.RemoveSubscriptionIDs(ids...)
} }
// ClearUsageLogs clears all "usage_logs" edges to the UsageLog entity.
func (_u *GroupUpdateOne) ClearUsageLogs() *GroupUpdateOne {
_u.mutation.ClearUsageLogs()
return _u
}
// RemoveUsageLogIDs removes the "usage_logs" edge to UsageLog entities by IDs.
func (_u *GroupUpdateOne) RemoveUsageLogIDs(ids ...int64) *GroupUpdateOne {
_u.mutation.RemoveUsageLogIDs(ids...)
return _u
}
// RemoveUsageLogs removes "usage_logs" edges to UsageLog entities.
func (_u *GroupUpdateOne) RemoveUsageLogs(v ...*UsageLog) *GroupUpdateOne {
ids := make([]int64, len(v))
for i := range v {
ids[i] = v[i].ID
}
return _u.RemoveUsageLogIDs(ids...)
}
// ClearAccounts clears all "accounts" edges to the Account entity. // ClearAccounts clears all "accounts" edges to the Account entity.
func (_u *GroupUpdateOne) ClearAccounts() *GroupUpdateOne { func (_u *GroupUpdateOne) ClearAccounts() *GroupUpdateOne {
_u.mutation.ClearAccounts() _u.mutation.ClearAccounts()
...@@ -1422,6 +1588,12 @@ func (_u *GroupUpdateOne) sqlSave(ctx context.Context) (_node *Group, err error) ...@@ -1422,6 +1588,12 @@ func (_u *GroupUpdateOne) sqlSave(ctx context.Context) (_node *Group, err error)
if _u.mutation.MonthlyLimitUsdCleared() { if _u.mutation.MonthlyLimitUsdCleared() {
_spec.ClearField(group.FieldMonthlyLimitUsd, field.TypeFloat64) _spec.ClearField(group.FieldMonthlyLimitUsd, field.TypeFloat64)
} }
if value, ok := _u.mutation.DefaultValidityDays(); ok {
_spec.SetField(group.FieldDefaultValidityDays, field.TypeInt, value)
}
if value, ok := _u.mutation.AddedDefaultValidityDays(); ok {
_spec.AddField(group.FieldDefaultValidityDays, field.TypeInt, value)
}
if _u.mutation.APIKeysCleared() { if _u.mutation.APIKeysCleared() {
edge := &sqlgraph.EdgeSpec{ edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M, Rel: sqlgraph.O2M,
...@@ -1557,6 +1729,51 @@ func (_u *GroupUpdateOne) sqlSave(ctx context.Context) (_node *Group, err error) ...@@ -1557,6 +1729,51 @@ func (_u *GroupUpdateOne) sqlSave(ctx context.Context) (_node *Group, err error)
} }
_spec.Edges.Add = append(_spec.Edges.Add, edge) _spec.Edges.Add = append(_spec.Edges.Add, edge)
} }
if _u.mutation.UsageLogsCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: group.UsageLogsTable,
Columns: []string{group.UsageLogsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(usagelog.FieldID, field.TypeInt64),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.RemovedUsageLogsIDs(); len(nodes) > 0 && !_u.mutation.UsageLogsCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: group.UsageLogsTable,
Columns: []string{group.UsageLogsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(usagelog.FieldID, field.TypeInt64),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.UsageLogsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: group.UsageLogsTable,
Columns: []string{group.UsageLogsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(usagelog.FieldID, field.TypeInt64),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
if _u.mutation.AccountsCleared() { if _u.mutation.AccountsCleared() {
edge := &sqlgraph.EdgeSpec{ edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2M, Rel: sqlgraph.M2M,
......
...@@ -93,6 +93,18 @@ func (f SettingFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, err ...@@ -93,6 +93,18 @@ func (f SettingFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, err
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.SettingMutation", m) return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.SettingMutation", m)
} }
// The UsageLogFunc type is an adapter to allow the use of ordinary
// function as UsageLog mutator.
type UsageLogFunc func(context.Context, *ent.UsageLogMutation) (ent.Value, error)
// Mutate calls f(ctx, m).
func (f UsageLogFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
if mv, ok := m.(*ent.UsageLogMutation); ok {
return f(ctx, mv)
}
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.UsageLogMutation", m)
}
// The UserFunc type is an adapter to allow the use of ordinary // The UserFunc type is an adapter to allow the use of ordinary
// function as User mutator. // function as User mutator.
type UserFunc func(context.Context, *ent.UserMutation) (ent.Value, error) type UserFunc func(context.Context, *ent.UserMutation) (ent.Value, error)
......
...@@ -16,6 +16,7 @@ import ( ...@@ -16,6 +16,7 @@ import (
"github.com/Wei-Shaw/sub2api/ent/proxy" "github.com/Wei-Shaw/sub2api/ent/proxy"
"github.com/Wei-Shaw/sub2api/ent/redeemcode" "github.com/Wei-Shaw/sub2api/ent/redeemcode"
"github.com/Wei-Shaw/sub2api/ent/setting" "github.com/Wei-Shaw/sub2api/ent/setting"
"github.com/Wei-Shaw/sub2api/ent/usagelog"
"github.com/Wei-Shaw/sub2api/ent/user" "github.com/Wei-Shaw/sub2api/ent/user"
"github.com/Wei-Shaw/sub2api/ent/userallowedgroup" "github.com/Wei-Shaw/sub2api/ent/userallowedgroup"
"github.com/Wei-Shaw/sub2api/ent/usersubscription" "github.com/Wei-Shaw/sub2api/ent/usersubscription"
...@@ -266,6 +267,33 @@ func (f TraverseSetting) Traverse(ctx context.Context, q ent.Query) error { ...@@ -266,6 +267,33 @@ func (f TraverseSetting) Traverse(ctx context.Context, q ent.Query) error {
return fmt.Errorf("unexpected query type %T. expect *ent.SettingQuery", q) return fmt.Errorf("unexpected query type %T. expect *ent.SettingQuery", q)
} }
// The UsageLogFunc type is an adapter to allow the use of ordinary function as a Querier.
type UsageLogFunc func(context.Context, *ent.UsageLogQuery) (ent.Value, error)
// Query calls f(ctx, q).
func (f UsageLogFunc) Query(ctx context.Context, q ent.Query) (ent.Value, error) {
if q, ok := q.(*ent.UsageLogQuery); ok {
return f(ctx, q)
}
return nil, fmt.Errorf("unexpected query type %T. expect *ent.UsageLogQuery", q)
}
// The TraverseUsageLog type is an adapter to allow the use of ordinary function as Traverser.
type TraverseUsageLog func(context.Context, *ent.UsageLogQuery) error
// Intercept is a dummy implementation of Intercept that returns the next Querier in the pipeline.
func (f TraverseUsageLog) Intercept(next ent.Querier) ent.Querier {
return next
}
// Traverse calls f(ctx, q).
func (f TraverseUsageLog) Traverse(ctx context.Context, q ent.Query) error {
if q, ok := q.(*ent.UsageLogQuery); ok {
return f(ctx, q)
}
return fmt.Errorf("unexpected query type %T. expect *ent.UsageLogQuery", q)
}
// The UserFunc type is an adapter to allow the use of ordinary function as a Querier. // The UserFunc type is an adapter to allow the use of ordinary function as a Querier.
type UserFunc func(context.Context, *ent.UserQuery) (ent.Value, error) type UserFunc func(context.Context, *ent.UserQuery) (ent.Value, error)
...@@ -364,6 +392,8 @@ func NewQuery(q ent.Query) (Query, error) { ...@@ -364,6 +392,8 @@ func NewQuery(q ent.Query) (Query, error) {
return &query[*ent.RedeemCodeQuery, predicate.RedeemCode, redeemcode.OrderOption]{typ: ent.TypeRedeemCode, tq: q}, nil return &query[*ent.RedeemCodeQuery, predicate.RedeemCode, redeemcode.OrderOption]{typ: ent.TypeRedeemCode, tq: q}, nil
case *ent.SettingQuery: case *ent.SettingQuery:
return &query[*ent.SettingQuery, predicate.Setting, setting.OrderOption]{typ: ent.TypeSetting, tq: q}, nil return &query[*ent.SettingQuery, predicate.Setting, setting.OrderOption]{typ: ent.TypeSetting, tq: q}, nil
case *ent.UsageLogQuery:
return &query[*ent.UsageLogQuery, predicate.UsageLog, usagelog.OrderOption]{typ: ent.TypeUsageLog, tq: q}, nil
case *ent.UserQuery: case *ent.UserQuery:
return &query[*ent.UserQuery, predicate.User, user.OrderOption]{typ: ent.TypeUser, tq: q}, nil return &query[*ent.UserQuery, predicate.User, user.OrderOption]{typ: ent.TypeUser, tq: q}, nil
case *ent.UserAllowedGroupQuery: case *ent.UserAllowedGroupQuery:
......
...@@ -20,7 +20,6 @@ var ( ...@@ -20,7 +20,6 @@ var (
{Name: "type", Type: field.TypeString, Size: 20}, {Name: "type", Type: field.TypeString, Size: 20},
{Name: "credentials", Type: field.TypeJSON, SchemaType: map[string]string{"postgres": "jsonb"}}, {Name: "credentials", Type: field.TypeJSON, SchemaType: map[string]string{"postgres": "jsonb"}},
{Name: "extra", Type: field.TypeJSON, SchemaType: map[string]string{"postgres": "jsonb"}}, {Name: "extra", Type: field.TypeJSON, SchemaType: map[string]string{"postgres": "jsonb"}},
{Name: "proxy_id", Type: field.TypeInt64, Nullable: true},
{Name: "concurrency", Type: field.TypeInt, Default: 3}, {Name: "concurrency", Type: field.TypeInt, Default: 3},
{Name: "priority", Type: field.TypeInt, Default: 50}, {Name: "priority", Type: field.TypeInt, Default: 50},
{Name: "status", Type: field.TypeString, Size: 20, Default: "active"}, {Name: "status", Type: field.TypeString, Size: 20, Default: "active"},
...@@ -33,12 +32,21 @@ var ( ...@@ -33,12 +32,21 @@ var (
{Name: "session_window_start", Type: field.TypeTime, Nullable: true, SchemaType: map[string]string{"postgres": "timestamptz"}}, {Name: "session_window_start", Type: field.TypeTime, Nullable: true, SchemaType: map[string]string{"postgres": "timestamptz"}},
{Name: "session_window_end", Type: field.TypeTime, Nullable: true, SchemaType: map[string]string{"postgres": "timestamptz"}}, {Name: "session_window_end", Type: field.TypeTime, Nullable: true, SchemaType: map[string]string{"postgres": "timestamptz"}},
{Name: "session_window_status", Type: field.TypeString, Nullable: true, Size: 20}, {Name: "session_window_status", Type: field.TypeString, Nullable: true, Size: 20},
{Name: "proxy_id", Type: field.TypeInt64, Nullable: true},
} }
// AccountsTable holds the schema information for the "accounts" table. // AccountsTable holds the schema information for the "accounts" table.
AccountsTable = &schema.Table{ AccountsTable = &schema.Table{
Name: "accounts", Name: "accounts",
Columns: AccountsColumns, Columns: AccountsColumns,
PrimaryKey: []*schema.Column{AccountsColumns[0]}, PrimaryKey: []*schema.Column{AccountsColumns[0]},
ForeignKeys: []*schema.ForeignKey{
{
Symbol: "accounts_proxies_proxy",
Columns: []*schema.Column{AccountsColumns[21]},
RefColumns: []*schema.Column{ProxiesColumns[0]},
OnDelete: schema.SetNull,
},
},
Indexes: []*schema.Index{ Indexes: []*schema.Index{
{ {
Name: "account_platform", Name: "account_platform",
...@@ -53,42 +61,42 @@ var ( ...@@ -53,42 +61,42 @@ var (
{ {
Name: "account_status", Name: "account_status",
Unique: false, Unique: false,
Columns: []*schema.Column{AccountsColumns[12]}, Columns: []*schema.Column{AccountsColumns[11]},
}, },
{ {
Name: "account_proxy_id", Name: "account_proxy_id",
Unique: false, Unique: false,
Columns: []*schema.Column{AccountsColumns[9]}, Columns: []*schema.Column{AccountsColumns[21]},
}, },
{ {
Name: "account_priority", Name: "account_priority",
Unique: false, Unique: false,
Columns: []*schema.Column{AccountsColumns[11]}, Columns: []*schema.Column{AccountsColumns[10]},
}, },
{ {
Name: "account_last_used_at", Name: "account_last_used_at",
Unique: false, Unique: false,
Columns: []*schema.Column{AccountsColumns[14]}, Columns: []*schema.Column{AccountsColumns[13]},
}, },
{ {
Name: "account_schedulable", Name: "account_schedulable",
Unique: false, Unique: false,
Columns: []*schema.Column{AccountsColumns[15]}, Columns: []*schema.Column{AccountsColumns[14]},
}, },
{ {
Name: "account_rate_limited_at", Name: "account_rate_limited_at",
Unique: false, Unique: false,
Columns: []*schema.Column{AccountsColumns[16]}, Columns: []*schema.Column{AccountsColumns[15]},
}, },
{ {
Name: "account_rate_limit_reset_at", Name: "account_rate_limit_reset_at",
Unique: false, Unique: false,
Columns: []*schema.Column{AccountsColumns[17]}, Columns: []*schema.Column{AccountsColumns[16]},
}, },
{ {
Name: "account_overload_until", Name: "account_overload_until",
Unique: false, Unique: false,
Columns: []*schema.Column{AccountsColumns[18]}, Columns: []*schema.Column{AccountsColumns[17]},
}, },
{ {
Name: "account_deleted_at", Name: "account_deleted_at",
...@@ -100,7 +108,7 @@ var ( ...@@ -100,7 +108,7 @@ var (
// AccountGroupsColumns holds the columns for the "account_groups" table. // AccountGroupsColumns holds the columns for the "account_groups" table.
AccountGroupsColumns = []*schema.Column{ AccountGroupsColumns = []*schema.Column{
{Name: "priority", Type: field.TypeInt, Default: 50}, {Name: "priority", Type: field.TypeInt, Default: 50},
{Name: "created_at", Type: field.TypeTime}, {Name: "created_at", Type: field.TypeTime, SchemaType: map[string]string{"postgres": "timestamptz"}},
{Name: "account_id", Type: field.TypeInt64}, {Name: "account_id", Type: field.TypeInt64},
{Name: "group_id", Type: field.TypeInt64}, {Name: "group_id", Type: field.TypeInt64},
} }
...@@ -168,11 +176,6 @@ var ( ...@@ -168,11 +176,6 @@ var (
}, },
}, },
Indexes: []*schema.Index{ Indexes: []*schema.Index{
{
Name: "apikey_key",
Unique: true,
Columns: []*schema.Column{APIKeysColumns[4]},
},
{ {
Name: "apikey_user_id", Name: "apikey_user_id",
Unique: false, Unique: false,
...@@ -201,7 +204,7 @@ var ( ...@@ -201,7 +204,7 @@ var (
{Name: "created_at", Type: field.TypeTime, SchemaType: map[string]string{"postgres": "timestamptz"}}, {Name: "created_at", Type: field.TypeTime, SchemaType: map[string]string{"postgres": "timestamptz"}},
{Name: "updated_at", Type: field.TypeTime, SchemaType: map[string]string{"postgres": "timestamptz"}}, {Name: "updated_at", Type: field.TypeTime, SchemaType: map[string]string{"postgres": "timestamptz"}},
{Name: "deleted_at", Type: field.TypeTime, Nullable: true, SchemaType: map[string]string{"postgres": "timestamptz"}}, {Name: "deleted_at", Type: field.TypeTime, Nullable: true, SchemaType: map[string]string{"postgres": "timestamptz"}},
{Name: "name", Type: field.TypeString, Unique: true, Size: 100}, {Name: "name", Type: field.TypeString, Size: 100},
{Name: "description", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"postgres": "text"}}, {Name: "description", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"postgres": "text"}},
{Name: "rate_multiplier", Type: field.TypeFloat64, Default: 1, SchemaType: map[string]string{"postgres": "decimal(10,4)"}}, {Name: "rate_multiplier", Type: field.TypeFloat64, Default: 1, SchemaType: map[string]string{"postgres": "decimal(10,4)"}},
{Name: "is_exclusive", Type: field.TypeBool, Default: false}, {Name: "is_exclusive", Type: field.TypeBool, Default: false},
...@@ -211,6 +214,7 @@ var ( ...@@ -211,6 +214,7 @@ var (
{Name: "daily_limit_usd", Type: field.TypeFloat64, Nullable: true, SchemaType: map[string]string{"postgres": "decimal(20,8)"}}, {Name: "daily_limit_usd", Type: field.TypeFloat64, Nullable: true, SchemaType: map[string]string{"postgres": "decimal(20,8)"}},
{Name: "weekly_limit_usd", Type: field.TypeFloat64, Nullable: true, SchemaType: map[string]string{"postgres": "decimal(20,8)"}}, {Name: "weekly_limit_usd", Type: field.TypeFloat64, Nullable: true, SchemaType: map[string]string{"postgres": "decimal(20,8)"}},
{Name: "monthly_limit_usd", Type: field.TypeFloat64, Nullable: true, SchemaType: map[string]string{"postgres": "decimal(20,8)"}}, {Name: "monthly_limit_usd", Type: field.TypeFloat64, Nullable: true, SchemaType: map[string]string{"postgres": "decimal(20,8)"}},
{Name: "default_validity_days", Type: field.TypeInt, Default: 30},
} }
// GroupsTable holds the schema information for the "groups" table. // GroupsTable holds the schema information for the "groups" table.
GroupsTable = &schema.Table{ GroupsTable = &schema.Table{
...@@ -218,11 +222,6 @@ var ( ...@@ -218,11 +222,6 @@ var (
Columns: GroupsColumns, Columns: GroupsColumns,
PrimaryKey: []*schema.Column{GroupsColumns[0]}, PrimaryKey: []*schema.Column{GroupsColumns[0]},
Indexes: []*schema.Index{ Indexes: []*schema.Index{
{
Name: "group_name",
Unique: true,
Columns: []*schema.Column{GroupsColumns[4]},
},
{ {
Name: "group_status", Name: "group_status",
Unique: false, Unique: false,
...@@ -316,11 +315,6 @@ var ( ...@@ -316,11 +315,6 @@ var (
}, },
}, },
Indexes: []*schema.Index{ Indexes: []*schema.Index{
{
Name: "redeemcode_code",
Unique: true,
Columns: []*schema.Column{RedeemCodesColumns[1]},
},
{ {
Name: "redeemcode_status", Name: "redeemcode_status",
Unique: false, Unique: false,
...@@ -350,11 +344,123 @@ var ( ...@@ -350,11 +344,123 @@ var (
Name: "settings", Name: "settings",
Columns: SettingsColumns, Columns: SettingsColumns,
PrimaryKey: []*schema.Column{SettingsColumns[0]}, PrimaryKey: []*schema.Column{SettingsColumns[0]},
}
// UsageLogsColumns holds the columns for the "usage_logs" table.
UsageLogsColumns = []*schema.Column{
{Name: "id", Type: field.TypeInt64, Increment: true},
{Name: "request_id", Type: field.TypeString, Size: 64},
{Name: "model", Type: field.TypeString, Size: 100},
{Name: "input_tokens", Type: field.TypeInt, Default: 0},
{Name: "output_tokens", Type: field.TypeInt, Default: 0},
{Name: "cache_creation_tokens", Type: field.TypeInt, Default: 0},
{Name: "cache_read_tokens", Type: field.TypeInt, Default: 0},
{Name: "cache_creation_5m_tokens", Type: field.TypeInt, Default: 0},
{Name: "cache_creation_1h_tokens", Type: field.TypeInt, Default: 0},
{Name: "input_cost", Type: field.TypeFloat64, Default: 0, SchemaType: map[string]string{"postgres": "decimal(20,10)"}},
{Name: "output_cost", Type: field.TypeFloat64, Default: 0, SchemaType: map[string]string{"postgres": "decimal(20,10)"}},
{Name: "cache_creation_cost", Type: field.TypeFloat64, Default: 0, SchemaType: map[string]string{"postgres": "decimal(20,10)"}},
{Name: "cache_read_cost", Type: field.TypeFloat64, Default: 0, SchemaType: map[string]string{"postgres": "decimal(20,10)"}},
{Name: "total_cost", Type: field.TypeFloat64, Default: 0, SchemaType: map[string]string{"postgres": "decimal(20,10)"}},
{Name: "actual_cost", Type: field.TypeFloat64, Default: 0, SchemaType: map[string]string{"postgres": "decimal(20,10)"}},
{Name: "rate_multiplier", Type: field.TypeFloat64, Default: 1, SchemaType: map[string]string{"postgres": "decimal(10,4)"}},
{Name: "billing_type", Type: field.TypeInt8, Default: 0},
{Name: "stream", Type: field.TypeBool, Default: false},
{Name: "duration_ms", Type: field.TypeInt, Nullable: true},
{Name: "first_token_ms", Type: field.TypeInt, Nullable: true},
{Name: "created_at", Type: field.TypeTime, SchemaType: map[string]string{"postgres": "timestamptz"}},
{Name: "account_id", Type: field.TypeInt64},
{Name: "api_key_id", Type: field.TypeInt64},
{Name: "group_id", Type: field.TypeInt64, Nullable: true},
{Name: "user_id", Type: field.TypeInt64},
{Name: "subscription_id", Type: field.TypeInt64, Nullable: true},
}
// UsageLogsTable holds the schema information for the "usage_logs" table.
UsageLogsTable = &schema.Table{
Name: "usage_logs",
Columns: UsageLogsColumns,
PrimaryKey: []*schema.Column{UsageLogsColumns[0]},
ForeignKeys: []*schema.ForeignKey{
{
Symbol: "usage_logs_accounts_usage_logs",
Columns: []*schema.Column{UsageLogsColumns[21]},
RefColumns: []*schema.Column{AccountsColumns[0]},
OnDelete: schema.NoAction,
},
{
Symbol: "usage_logs_api_keys_usage_logs",
Columns: []*schema.Column{UsageLogsColumns[22]},
RefColumns: []*schema.Column{APIKeysColumns[0]},
OnDelete: schema.NoAction,
},
{
Symbol: "usage_logs_groups_usage_logs",
Columns: []*schema.Column{UsageLogsColumns[23]},
RefColumns: []*schema.Column{GroupsColumns[0]},
OnDelete: schema.SetNull,
},
{
Symbol: "usage_logs_users_usage_logs",
Columns: []*schema.Column{UsageLogsColumns[24]},
RefColumns: []*schema.Column{UsersColumns[0]},
OnDelete: schema.NoAction,
},
{
Symbol: "usage_logs_user_subscriptions_usage_logs",
Columns: []*schema.Column{UsageLogsColumns[25]},
RefColumns: []*schema.Column{UserSubscriptionsColumns[0]},
OnDelete: schema.SetNull,
},
},
Indexes: []*schema.Index{ Indexes: []*schema.Index{
{ {
Name: "setting_key", Name: "usagelog_user_id",
Unique: true, Unique: false,
Columns: []*schema.Column{SettingsColumns[1]}, Columns: []*schema.Column{UsageLogsColumns[24]},
},
{
Name: "usagelog_api_key_id",
Unique: false,
Columns: []*schema.Column{UsageLogsColumns[22]},
},
{
Name: "usagelog_account_id",
Unique: false,
Columns: []*schema.Column{UsageLogsColumns[21]},
},
{
Name: "usagelog_group_id",
Unique: false,
Columns: []*schema.Column{UsageLogsColumns[23]},
},
{
Name: "usagelog_subscription_id",
Unique: false,
Columns: []*schema.Column{UsageLogsColumns[25]},
},
{
Name: "usagelog_created_at",
Unique: false,
Columns: []*schema.Column{UsageLogsColumns[20]},
},
{
Name: "usagelog_model",
Unique: false,
Columns: []*schema.Column{UsageLogsColumns[2]},
},
{
Name: "usagelog_request_id",
Unique: false,
Columns: []*schema.Column{UsageLogsColumns[1]},
},
{
Name: "usagelog_user_id_created_at",
Unique: false,
Columns: []*schema.Column{UsageLogsColumns[24], UsageLogsColumns[20]},
},
{
Name: "usagelog_api_key_id_created_at",
Unique: false,
Columns: []*schema.Column{UsageLogsColumns[22], UsageLogsColumns[20]},
}, },
}, },
} }
...@@ -364,7 +470,7 @@ var ( ...@@ -364,7 +470,7 @@ var (
{Name: "created_at", Type: field.TypeTime, SchemaType: map[string]string{"postgres": "timestamptz"}}, {Name: "created_at", Type: field.TypeTime, SchemaType: map[string]string{"postgres": "timestamptz"}},
{Name: "updated_at", Type: field.TypeTime, SchemaType: map[string]string{"postgres": "timestamptz"}}, {Name: "updated_at", Type: field.TypeTime, SchemaType: map[string]string{"postgres": "timestamptz"}},
{Name: "deleted_at", Type: field.TypeTime, Nullable: true, SchemaType: map[string]string{"postgres": "timestamptz"}}, {Name: "deleted_at", Type: field.TypeTime, Nullable: true, SchemaType: map[string]string{"postgres": "timestamptz"}},
{Name: "email", Type: field.TypeString, Unique: true, Size: 255}, {Name: "email", Type: field.TypeString, Size: 255},
{Name: "password_hash", Type: field.TypeString, Size: 255}, {Name: "password_hash", Type: field.TypeString, Size: 255},
{Name: "role", Type: field.TypeString, Size: 20, Default: "user"}, {Name: "role", Type: field.TypeString, Size: 20, Default: "user"},
{Name: "balance", Type: field.TypeFloat64, Default: 0, SchemaType: map[string]string{"postgres": "decimal(20,8)"}}, {Name: "balance", Type: field.TypeFloat64, Default: 0, SchemaType: map[string]string{"postgres": "decimal(20,8)"}},
...@@ -380,11 +486,6 @@ var ( ...@@ -380,11 +486,6 @@ var (
Columns: UsersColumns, Columns: UsersColumns,
PrimaryKey: []*schema.Column{UsersColumns[0]}, PrimaryKey: []*schema.Column{UsersColumns[0]},
Indexes: []*schema.Index{ Indexes: []*schema.Index{
{
Name: "user_email",
Unique: true,
Columns: []*schema.Column{UsersColumns[4]},
},
{ {
Name: "user_status", Name: "user_status",
Unique: false, Unique: false,
...@@ -399,7 +500,7 @@ var ( ...@@ -399,7 +500,7 @@ var (
} }
// UserAllowedGroupsColumns holds the columns for the "user_allowed_groups" table. // UserAllowedGroupsColumns holds the columns for the "user_allowed_groups" table.
UserAllowedGroupsColumns = []*schema.Column{ UserAllowedGroupsColumns = []*schema.Column{
{Name: "created_at", Type: field.TypeTime}, {Name: "created_at", Type: field.TypeTime, SchemaType: map[string]string{"postgres": "timestamptz"}},
{Name: "user_id", Type: field.TypeInt64}, {Name: "user_id", Type: field.TypeInt64},
{Name: "group_id", Type: field.TypeInt64}, {Name: "group_id", Type: field.TypeInt64},
} }
...@@ -435,6 +536,7 @@ var ( ...@@ -435,6 +536,7 @@ var (
{Name: "id", Type: field.TypeInt64, Increment: true}, {Name: "id", Type: field.TypeInt64, Increment: true},
{Name: "created_at", Type: field.TypeTime, SchemaType: map[string]string{"postgres": "timestamptz"}}, {Name: "created_at", Type: field.TypeTime, SchemaType: map[string]string{"postgres": "timestamptz"}},
{Name: "updated_at", Type: field.TypeTime, SchemaType: map[string]string{"postgres": "timestamptz"}}, {Name: "updated_at", Type: field.TypeTime, SchemaType: map[string]string{"postgres": "timestamptz"}},
{Name: "deleted_at", Type: field.TypeTime, Nullable: true, SchemaType: map[string]string{"postgres": "timestamptz"}},
{Name: "starts_at", Type: field.TypeTime, SchemaType: map[string]string{"postgres": "timestamptz"}}, {Name: "starts_at", Type: field.TypeTime, SchemaType: map[string]string{"postgres": "timestamptz"}},
{Name: "expires_at", Type: field.TypeTime, SchemaType: map[string]string{"postgres": "timestamptz"}}, {Name: "expires_at", Type: field.TypeTime, SchemaType: map[string]string{"postgres": "timestamptz"}},
{Name: "status", Type: field.TypeString, Size: 20, Default: "active"}, {Name: "status", Type: field.TypeString, Size: 20, Default: "active"},
...@@ -458,19 +560,19 @@ var ( ...@@ -458,19 +560,19 @@ var (
ForeignKeys: []*schema.ForeignKey{ ForeignKeys: []*schema.ForeignKey{
{ {
Symbol: "user_subscriptions_groups_subscriptions", Symbol: "user_subscriptions_groups_subscriptions",
Columns: []*schema.Column{UserSubscriptionsColumns[14]}, Columns: []*schema.Column{UserSubscriptionsColumns[15]},
RefColumns: []*schema.Column{GroupsColumns[0]}, RefColumns: []*schema.Column{GroupsColumns[0]},
OnDelete: schema.NoAction, OnDelete: schema.NoAction,
}, },
{ {
Symbol: "user_subscriptions_users_subscriptions", Symbol: "user_subscriptions_users_subscriptions",
Columns: []*schema.Column{UserSubscriptionsColumns[15]}, Columns: []*schema.Column{UserSubscriptionsColumns[16]},
RefColumns: []*schema.Column{UsersColumns[0]}, RefColumns: []*schema.Column{UsersColumns[0]},
OnDelete: schema.NoAction, OnDelete: schema.NoAction,
}, },
{ {
Symbol: "user_subscriptions_users_assigned_subscriptions", Symbol: "user_subscriptions_users_assigned_subscriptions",
Columns: []*schema.Column{UserSubscriptionsColumns[16]}, Columns: []*schema.Column{UserSubscriptionsColumns[17]},
RefColumns: []*schema.Column{UsersColumns[0]}, RefColumns: []*schema.Column{UsersColumns[0]},
OnDelete: schema.SetNull, OnDelete: schema.SetNull,
}, },
...@@ -479,32 +581,37 @@ var ( ...@@ -479,32 +581,37 @@ var (
{ {
Name: "usersubscription_user_id", Name: "usersubscription_user_id",
Unique: false, Unique: false,
Columns: []*schema.Column{UserSubscriptionsColumns[15]}, Columns: []*schema.Column{UserSubscriptionsColumns[16]},
}, },
{ {
Name: "usersubscription_group_id", Name: "usersubscription_group_id",
Unique: false, Unique: false,
Columns: []*schema.Column{UserSubscriptionsColumns[14]}, Columns: []*schema.Column{UserSubscriptionsColumns[15]},
}, },
{ {
Name: "usersubscription_status", Name: "usersubscription_status",
Unique: false, Unique: false,
Columns: []*schema.Column{UserSubscriptionsColumns[5]}, Columns: []*schema.Column{UserSubscriptionsColumns[6]},
}, },
{ {
Name: "usersubscription_expires_at", Name: "usersubscription_expires_at",
Unique: false, Unique: false,
Columns: []*schema.Column{UserSubscriptionsColumns[4]}, Columns: []*schema.Column{UserSubscriptionsColumns[5]},
}, },
{ {
Name: "usersubscription_assigned_by", Name: "usersubscription_assigned_by",
Unique: false, Unique: false,
Columns: []*schema.Column{UserSubscriptionsColumns[16]}, Columns: []*schema.Column{UserSubscriptionsColumns[17]},
}, },
{ {
Name: "usersubscription_user_id_group_id", Name: "usersubscription_user_id_group_id",
Unique: true, Unique: false,
Columns: []*schema.Column{UserSubscriptionsColumns[15], UserSubscriptionsColumns[14]}, Columns: []*schema.Column{UserSubscriptionsColumns[16], UserSubscriptionsColumns[15]},
},
{
Name: "usersubscription_deleted_at",
Unique: false,
Columns: []*schema.Column{UserSubscriptionsColumns[3]},
}, },
}, },
} }
...@@ -517,6 +624,7 @@ var ( ...@@ -517,6 +624,7 @@ var (
ProxiesTable, ProxiesTable,
RedeemCodesTable, RedeemCodesTable,
SettingsTable, SettingsTable,
UsageLogsTable,
UsersTable, UsersTable,
UserAllowedGroupsTable, UserAllowedGroupsTable,
UserSubscriptionsTable, UserSubscriptionsTable,
...@@ -524,6 +632,7 @@ var ( ...@@ -524,6 +632,7 @@ var (
) )
func init() { func init() {
AccountsTable.ForeignKeys[0].RefTable = ProxiesTable
AccountsTable.Annotation = &entsql.Annotation{ AccountsTable.Annotation = &entsql.Annotation{
Table: "accounts", Table: "accounts",
} }
...@@ -551,6 +660,14 @@ func init() { ...@@ -551,6 +660,14 @@ func init() {
SettingsTable.Annotation = &entsql.Annotation{ SettingsTable.Annotation = &entsql.Annotation{
Table: "settings", Table: "settings",
} }
UsageLogsTable.ForeignKeys[0].RefTable = AccountsTable
UsageLogsTable.ForeignKeys[1].RefTable = APIKeysTable
UsageLogsTable.ForeignKeys[2].RefTable = GroupsTable
UsageLogsTable.ForeignKeys[3].RefTable = UsersTable
UsageLogsTable.ForeignKeys[4].RefTable = UserSubscriptionsTable
UsageLogsTable.Annotation = &entsql.Annotation{
Table: "usage_logs",
}
UsersTable.Annotation = &entsql.Annotation{ UsersTable.Annotation = &entsql.Annotation{
Table: "users", Table: "users",
} }
......
This diff is collapsed.
...@@ -27,6 +27,9 @@ type RedeemCode func(*sql.Selector) ...@@ -27,6 +27,9 @@ type RedeemCode func(*sql.Selector)
// Setting is the predicate function for setting builders. // Setting is the predicate function for setting builders.
type Setting func(*sql.Selector) type Setting func(*sql.Selector)
// UsageLog is the predicate function for usagelog builders.
type UsageLog func(*sql.Selector)
// User is the predicate function for user builders. // User is the predicate function for user builders.
type User func(*sql.Selector) type User func(*sql.Selector)
......
...@@ -36,10 +36,31 @@ type Proxy struct { ...@@ -36,10 +36,31 @@ type Proxy struct {
// Password holds the value of the "password" field. // Password holds the value of the "password" field.
Password *string `json:"password,omitempty"` Password *string `json:"password,omitempty"`
// Status holds the value of the "status" field. // Status holds the value of the "status" field.
Status string `json:"status,omitempty"` Status string `json:"status,omitempty"`
// Edges holds the relations/edges for other nodes in the graph.
// The values are being populated by the ProxyQuery when eager-loading is set.
Edges ProxyEdges `json:"edges"`
selectValues sql.SelectValues selectValues sql.SelectValues
} }
// ProxyEdges holds the relations/edges for other nodes in the graph.
type ProxyEdges struct {
// Accounts holds the value of the accounts edge.
Accounts []*Account `json:"accounts,omitempty"`
// loadedTypes holds the information for reporting if a
// type was loaded (or requested) in eager-loading or not.
loadedTypes [1]bool
}
// AccountsOrErr returns the Accounts value or an error if the edge
// was not loaded in eager-loading.
func (e ProxyEdges) AccountsOrErr() ([]*Account, error) {
if e.loadedTypes[0] {
return e.Accounts, nil
}
return nil, &NotLoadedError{edge: "accounts"}
}
// scanValues returns the types for scanning values from sql.Rows. // scanValues returns the types for scanning values from sql.Rows.
func (*Proxy) scanValues(columns []string) ([]any, error) { func (*Proxy) scanValues(columns []string) ([]any, error) {
values := make([]any, len(columns)) values := make([]any, len(columns))
...@@ -148,6 +169,11 @@ func (_m *Proxy) Value(name string) (ent.Value, error) { ...@@ -148,6 +169,11 @@ func (_m *Proxy) Value(name string) (ent.Value, error) {
return _m.selectValues.Get(name) return _m.selectValues.Get(name)
} }
// QueryAccounts queries the "accounts" edge of the Proxy entity.
func (_m *Proxy) QueryAccounts() *AccountQuery {
return NewProxyClient(_m.config).QueryAccounts(_m)
}
// Update returns a builder for updating this Proxy. // Update returns a builder for updating this Proxy.
// Note that you need to call Proxy.Unwrap() before calling this method if this Proxy // Note that you need to call Proxy.Unwrap() before calling this method if this Proxy
// was returned from a transaction, and the transaction was committed or rolled back. // was returned from a transaction, and the transaction was committed or rolled back.
......
...@@ -7,6 +7,7 @@ import ( ...@@ -7,6 +7,7 @@ import (
"entgo.io/ent" "entgo.io/ent"
"entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph"
) )
const ( const (
...@@ -34,8 +35,17 @@ const ( ...@@ -34,8 +35,17 @@ const (
FieldPassword = "password" FieldPassword = "password"
// FieldStatus holds the string denoting the status field in the database. // FieldStatus holds the string denoting the status field in the database.
FieldStatus = "status" FieldStatus = "status"
// EdgeAccounts holds the string denoting the accounts edge name in mutations.
EdgeAccounts = "accounts"
// Table holds the table name of the proxy in the database. // Table holds the table name of the proxy in the database.
Table = "proxies" Table = "proxies"
// AccountsTable is the table that holds the accounts relation/edge.
AccountsTable = "accounts"
// AccountsInverseTable is the table name for the Account entity.
// It exists in this package in order to avoid circular dependency with the "account" package.
AccountsInverseTable = "accounts"
// AccountsColumn is the table column denoting the accounts relation/edge.
AccountsColumn = "proxy_id"
) )
// Columns holds all SQL columns for proxy fields. // Columns holds all SQL columns for proxy fields.
...@@ -150,3 +160,24 @@ func ByPassword(opts ...sql.OrderTermOption) OrderOption { ...@@ -150,3 +160,24 @@ func ByPassword(opts ...sql.OrderTermOption) OrderOption {
func ByStatus(opts ...sql.OrderTermOption) OrderOption { func ByStatus(opts ...sql.OrderTermOption) OrderOption {
return sql.OrderByField(FieldStatus, opts...).ToFunc() return sql.OrderByField(FieldStatus, opts...).ToFunc()
} }
// ByAccountsCount orders the results by accounts count.
func ByAccountsCount(opts ...sql.OrderTermOption) OrderOption {
return func(s *sql.Selector) {
sqlgraph.OrderByNeighborsCount(s, newAccountsStep(), opts...)
}
}
// ByAccounts orders the results by accounts terms.
func ByAccounts(term sql.OrderTerm, terms ...sql.OrderTerm) OrderOption {
return func(s *sql.Selector) {
sqlgraph.OrderByNeighborTerms(s, newAccountsStep(), append([]sql.OrderTerm{term}, terms...)...)
}
}
func newAccountsStep() *sqlgraph.Step {
return sqlgraph.NewStep(
sqlgraph.From(Table, FieldID),
sqlgraph.To(AccountsInverseTable, FieldID),
sqlgraph.Edge(sqlgraph.O2M, true, AccountsTable, AccountsColumn),
)
}
...@@ -6,6 +6,7 @@ import ( ...@@ -6,6 +6,7 @@ import (
"time" "time"
"entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph"
"github.com/Wei-Shaw/sub2api/ent/predicate" "github.com/Wei-Shaw/sub2api/ent/predicate"
) )
...@@ -684,6 +685,29 @@ func StatusContainsFold(v string) predicate.Proxy { ...@@ -684,6 +685,29 @@ func StatusContainsFold(v string) predicate.Proxy {
return predicate.Proxy(sql.FieldContainsFold(FieldStatus, v)) return predicate.Proxy(sql.FieldContainsFold(FieldStatus, v))
} }
// HasAccounts applies the HasEdge predicate on the "accounts" edge.
func HasAccounts() predicate.Proxy {
return predicate.Proxy(func(s *sql.Selector) {
step := sqlgraph.NewStep(
sqlgraph.From(Table, FieldID),
sqlgraph.Edge(sqlgraph.O2M, true, AccountsTable, AccountsColumn),
)
sqlgraph.HasNeighbors(s, step)
})
}
// HasAccountsWith applies the HasEdge predicate on the "accounts" edge with a given conditions (other predicates).
func HasAccountsWith(preds ...predicate.Account) predicate.Proxy {
return predicate.Proxy(func(s *sql.Selector) {
step := newAccountsStep()
sqlgraph.HasNeighborsWith(s, step, func(s *sql.Selector) {
for _, p := range preds {
p(s)
}
})
})
}
// And groups predicates with the AND operator between them. // And groups predicates with the AND operator between them.
func And(predicates ...predicate.Proxy) predicate.Proxy { func And(predicates ...predicate.Proxy) predicate.Proxy {
return predicate.Proxy(sql.AndPredicates(predicates...)) return predicate.Proxy(sql.AndPredicates(predicates...))
......
...@@ -11,6 +11,7 @@ import ( ...@@ -11,6 +11,7 @@ import (
"entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field" "entgo.io/ent/schema/field"
"github.com/Wei-Shaw/sub2api/ent/account"
"github.com/Wei-Shaw/sub2api/ent/proxy" "github.com/Wei-Shaw/sub2api/ent/proxy"
) )
...@@ -130,6 +131,21 @@ func (_c *ProxyCreate) SetNillableStatus(v *string) *ProxyCreate { ...@@ -130,6 +131,21 @@ func (_c *ProxyCreate) SetNillableStatus(v *string) *ProxyCreate {
return _c return _c
} }
// AddAccountIDs adds the "accounts" edge to the Account entity by IDs.
func (_c *ProxyCreate) AddAccountIDs(ids ...int64) *ProxyCreate {
_c.mutation.AddAccountIDs(ids...)
return _c
}
// AddAccounts adds the "accounts" edges to the Account entity.
func (_c *ProxyCreate) AddAccounts(v ...*Account) *ProxyCreate {
ids := make([]int64, len(v))
for i := range v {
ids[i] = v[i].ID
}
return _c.AddAccountIDs(ids...)
}
// Mutation returns the ProxyMutation object of the builder. // Mutation returns the ProxyMutation object of the builder.
func (_c *ProxyCreate) Mutation() *ProxyMutation { func (_c *ProxyCreate) Mutation() *ProxyMutation {
return _c.mutation return _c.mutation
...@@ -308,6 +324,22 @@ func (_c *ProxyCreate) createSpec() (*Proxy, *sqlgraph.CreateSpec) { ...@@ -308,6 +324,22 @@ func (_c *ProxyCreate) createSpec() (*Proxy, *sqlgraph.CreateSpec) {
_spec.SetField(proxy.FieldStatus, field.TypeString, value) _spec.SetField(proxy.FieldStatus, field.TypeString, value)
_node.Status = value _node.Status = value
} }
if nodes := _c.mutation.AccountsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: true,
Table: proxy.AccountsTable,
Columns: []string{proxy.AccountsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeInt64),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
return _node, _spec return _node, _spec
} }
......
...@@ -4,6 +4,7 @@ package ent ...@@ -4,6 +4,7 @@ package ent
import ( import (
"context" "context"
"database/sql/driver"
"fmt" "fmt"
"math" "math"
...@@ -11,6 +12,7 @@ import ( ...@@ -11,6 +12,7 @@ import (
"entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field" "entgo.io/ent/schema/field"
"github.com/Wei-Shaw/sub2api/ent/account"
"github.com/Wei-Shaw/sub2api/ent/predicate" "github.com/Wei-Shaw/sub2api/ent/predicate"
"github.com/Wei-Shaw/sub2api/ent/proxy" "github.com/Wei-Shaw/sub2api/ent/proxy"
) )
...@@ -18,10 +20,11 @@ import ( ...@@ -18,10 +20,11 @@ import (
// ProxyQuery is the builder for querying Proxy entities. // ProxyQuery is the builder for querying Proxy entities.
type ProxyQuery struct { type ProxyQuery struct {
config config
ctx *QueryContext ctx *QueryContext
order []proxy.OrderOption order []proxy.OrderOption
inters []Interceptor inters []Interceptor
predicates []predicate.Proxy predicates []predicate.Proxy
withAccounts *AccountQuery
// intermediate query (i.e. traversal path). // intermediate query (i.e. traversal path).
sql *sql.Selector sql *sql.Selector
path func(context.Context) (*sql.Selector, error) path func(context.Context) (*sql.Selector, error)
...@@ -58,6 +61,28 @@ func (_q *ProxyQuery) Order(o ...proxy.OrderOption) *ProxyQuery { ...@@ -58,6 +61,28 @@ func (_q *ProxyQuery) Order(o ...proxy.OrderOption) *ProxyQuery {
return _q return _q
} }
// QueryAccounts chains the current query on the "accounts" edge.
func (_q *ProxyQuery) QueryAccounts() *AccountQuery {
query := (&AccountClient{config: _q.config}).Query()
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
if err := _q.prepareQuery(ctx); err != nil {
return nil, err
}
selector := _q.sqlQuery(ctx)
if err := selector.Err(); err != nil {
return nil, err
}
step := sqlgraph.NewStep(
sqlgraph.From(proxy.Table, proxy.FieldID, selector),
sqlgraph.To(account.Table, account.FieldID),
sqlgraph.Edge(sqlgraph.O2M, true, proxy.AccountsTable, proxy.AccountsColumn),
)
fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
return fromU, nil
}
return query
}
// First returns the first Proxy entity from the query. // First returns the first Proxy entity from the query.
// Returns a *NotFoundError when no Proxy was found. // Returns a *NotFoundError when no Proxy was found.
func (_q *ProxyQuery) First(ctx context.Context) (*Proxy, error) { func (_q *ProxyQuery) First(ctx context.Context) (*Proxy, error) {
...@@ -245,17 +270,29 @@ func (_q *ProxyQuery) Clone() *ProxyQuery { ...@@ -245,17 +270,29 @@ func (_q *ProxyQuery) Clone() *ProxyQuery {
return nil return nil
} }
return &ProxyQuery{ return &ProxyQuery{
config: _q.config, config: _q.config,
ctx: _q.ctx.Clone(), ctx: _q.ctx.Clone(),
order: append([]proxy.OrderOption{}, _q.order...), order: append([]proxy.OrderOption{}, _q.order...),
inters: append([]Interceptor{}, _q.inters...), inters: append([]Interceptor{}, _q.inters...),
predicates: append([]predicate.Proxy{}, _q.predicates...), predicates: append([]predicate.Proxy{}, _q.predicates...),
withAccounts: _q.withAccounts.Clone(),
// clone intermediate query. // clone intermediate query.
sql: _q.sql.Clone(), sql: _q.sql.Clone(),
path: _q.path, path: _q.path,
} }
} }
// WithAccounts tells the query-builder to eager-load the nodes that are connected to
// the "accounts" edge. The optional arguments are used to configure the query builder of the edge.
func (_q *ProxyQuery) WithAccounts(opts ...func(*AccountQuery)) *ProxyQuery {
query := (&AccountClient{config: _q.config}).Query()
for _, opt := range opts {
opt(query)
}
_q.withAccounts = query
return _q
}
// 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.
// //
...@@ -332,8 +369,11 @@ func (_q *ProxyQuery) prepareQuery(ctx context.Context) error { ...@@ -332,8 +369,11 @@ func (_q *ProxyQuery) prepareQuery(ctx context.Context) error {
func (_q *ProxyQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*Proxy, error) { func (_q *ProxyQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*Proxy, error) {
var ( var (
nodes = []*Proxy{} nodes = []*Proxy{}
_spec = _q.querySpec() _spec = _q.querySpec()
loadedTypes = [1]bool{
_q.withAccounts != nil,
}
) )
_spec.ScanValues = func(columns []string) ([]any, error) { _spec.ScanValues = func(columns []string) ([]any, error) {
return (*Proxy).scanValues(nil, columns) return (*Proxy).scanValues(nil, columns)
...@@ -341,6 +381,7 @@ func (_q *ProxyQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*Proxy, ...@@ -341,6 +381,7 @@ func (_q *ProxyQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*Proxy,
_spec.Assign = func(columns []string, values []any) error { _spec.Assign = func(columns []string, values []any) error {
node := &Proxy{config: _q.config} node := &Proxy{config: _q.config}
nodes = append(nodes, node) nodes = append(nodes, node)
node.Edges.loadedTypes = loadedTypes
return node.assignValues(columns, values) return node.assignValues(columns, values)
} }
for i := range hooks { for i := range hooks {
...@@ -352,9 +393,50 @@ func (_q *ProxyQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*Proxy, ...@@ -352,9 +393,50 @@ func (_q *ProxyQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*Proxy,
if len(nodes) == 0 { if len(nodes) == 0 {
return nodes, nil return nodes, nil
} }
if query := _q.withAccounts; query != nil {
if err := _q.loadAccounts(ctx, query, nodes,
func(n *Proxy) { n.Edges.Accounts = []*Account{} },
func(n *Proxy, e *Account) { n.Edges.Accounts = append(n.Edges.Accounts, e) }); err != nil {
return nil, err
}
}
return nodes, nil return nodes, nil
} }
func (_q *ProxyQuery) loadAccounts(ctx context.Context, query *AccountQuery, nodes []*Proxy, init func(*Proxy), assign func(*Proxy, *Account)) error {
fks := make([]driver.Value, 0, len(nodes))
nodeids := make(map[int64]*Proxy)
for i := range nodes {
fks = append(fks, nodes[i].ID)
nodeids[nodes[i].ID] = nodes[i]
if init != nil {
init(nodes[i])
}
}
if len(query.ctx.Fields) > 0 {
query.ctx.AppendFieldOnce(account.FieldProxyID)
}
query.Where(predicate.Account(func(s *sql.Selector) {
s.Where(sql.InValues(s.C(proxy.AccountsColumn), fks...))
}))
neighbors, err := query.All(ctx)
if err != nil {
return err
}
for _, n := range neighbors {
fk := n.ProxyID
if fk == nil {
return fmt.Errorf(`foreign-key "proxy_id" is nil for node %v`, n.ID)
}
node, ok := nodeids[*fk]
if !ok {
return fmt.Errorf(`unexpected referenced foreign-key "proxy_id" returned %v for node %v`, *fk, n.ID)
}
assign(node, n)
}
return nil
}
func (_q *ProxyQuery) sqlCount(ctx context.Context) (int, error) { func (_q *ProxyQuery) sqlCount(ctx context.Context) (int, error) {
_spec := _q.querySpec() _spec := _q.querySpec()
_spec.Node.Columns = _q.ctx.Fields _spec.Node.Columns = _q.ctx.Fields
......
...@@ -11,6 +11,7 @@ import ( ...@@ -11,6 +11,7 @@ import (
"entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field" "entgo.io/ent/schema/field"
"github.com/Wei-Shaw/sub2api/ent/account"
"github.com/Wei-Shaw/sub2api/ent/predicate" "github.com/Wei-Shaw/sub2api/ent/predicate"
"github.com/Wei-Shaw/sub2api/ent/proxy" "github.com/Wei-Shaw/sub2api/ent/proxy"
) )
...@@ -171,11 +172,47 @@ func (_u *ProxyUpdate) SetNillableStatus(v *string) *ProxyUpdate { ...@@ -171,11 +172,47 @@ func (_u *ProxyUpdate) SetNillableStatus(v *string) *ProxyUpdate {
return _u return _u
} }
// AddAccountIDs adds the "accounts" edge to the Account entity by IDs.
func (_u *ProxyUpdate) AddAccountIDs(ids ...int64) *ProxyUpdate {
_u.mutation.AddAccountIDs(ids...)
return _u
}
// AddAccounts adds the "accounts" edges to the Account entity.
func (_u *ProxyUpdate) AddAccounts(v ...*Account) *ProxyUpdate {
ids := make([]int64, len(v))
for i := range v {
ids[i] = v[i].ID
}
return _u.AddAccountIDs(ids...)
}
// Mutation returns the ProxyMutation object of the builder. // Mutation returns the ProxyMutation object of the builder.
func (_u *ProxyUpdate) Mutation() *ProxyMutation { func (_u *ProxyUpdate) Mutation() *ProxyMutation {
return _u.mutation return _u.mutation
} }
// ClearAccounts clears all "accounts" edges to the Account entity.
func (_u *ProxyUpdate) ClearAccounts() *ProxyUpdate {
_u.mutation.ClearAccounts()
return _u
}
// RemoveAccountIDs removes the "accounts" edge to Account entities by IDs.
func (_u *ProxyUpdate) RemoveAccountIDs(ids ...int64) *ProxyUpdate {
_u.mutation.RemoveAccountIDs(ids...)
return _u
}
// RemoveAccounts removes "accounts" edges to Account entities.
func (_u *ProxyUpdate) RemoveAccounts(v ...*Account) *ProxyUpdate {
ids := make([]int64, len(v))
for i := range v {
ids[i] = v[i].ID
}
return _u.RemoveAccountIDs(ids...)
}
// 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 *ProxyUpdate) Save(ctx context.Context) (int, error) { func (_u *ProxyUpdate) Save(ctx context.Context) (int, error) {
if err := _u.defaults(); err != nil { if err := _u.defaults(); err != nil {
...@@ -304,6 +341,51 @@ func (_u *ProxyUpdate) sqlSave(ctx context.Context) (_node int, err error) { ...@@ -304,6 +341,51 @@ func (_u *ProxyUpdate) sqlSave(ctx context.Context) (_node int, err error) {
if value, ok := _u.mutation.Status(); ok { if value, ok := _u.mutation.Status(); ok {
_spec.SetField(proxy.FieldStatus, field.TypeString, value) _spec.SetField(proxy.FieldStatus, field.TypeString, value)
} }
if _u.mutation.AccountsCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: true,
Table: proxy.AccountsTable,
Columns: []string{proxy.AccountsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeInt64),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.RemovedAccountsIDs(); len(nodes) > 0 && !_u.mutation.AccountsCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: true,
Table: proxy.AccountsTable,
Columns: []string{proxy.AccountsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeInt64),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.AccountsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: true,
Table: proxy.AccountsTable,
Columns: []string{proxy.AccountsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeInt64),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
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{proxy.Label} err = &NotFoundError{proxy.Label}
...@@ -467,11 +549,47 @@ func (_u *ProxyUpdateOne) SetNillableStatus(v *string) *ProxyUpdateOne { ...@@ -467,11 +549,47 @@ func (_u *ProxyUpdateOne) SetNillableStatus(v *string) *ProxyUpdateOne {
return _u return _u
} }
// AddAccountIDs adds the "accounts" edge to the Account entity by IDs.
func (_u *ProxyUpdateOne) AddAccountIDs(ids ...int64) *ProxyUpdateOne {
_u.mutation.AddAccountIDs(ids...)
return _u
}
// AddAccounts adds the "accounts" edges to the Account entity.
func (_u *ProxyUpdateOne) AddAccounts(v ...*Account) *ProxyUpdateOne {
ids := make([]int64, len(v))
for i := range v {
ids[i] = v[i].ID
}
return _u.AddAccountIDs(ids...)
}
// Mutation returns the ProxyMutation object of the builder. // Mutation returns the ProxyMutation object of the builder.
func (_u *ProxyUpdateOne) Mutation() *ProxyMutation { func (_u *ProxyUpdateOne) Mutation() *ProxyMutation {
return _u.mutation return _u.mutation
} }
// ClearAccounts clears all "accounts" edges to the Account entity.
func (_u *ProxyUpdateOne) ClearAccounts() *ProxyUpdateOne {
_u.mutation.ClearAccounts()
return _u
}
// RemoveAccountIDs removes the "accounts" edge to Account entities by IDs.
func (_u *ProxyUpdateOne) RemoveAccountIDs(ids ...int64) *ProxyUpdateOne {
_u.mutation.RemoveAccountIDs(ids...)
return _u
}
// RemoveAccounts removes "accounts" edges to Account entities.
func (_u *ProxyUpdateOne) RemoveAccounts(v ...*Account) *ProxyUpdateOne {
ids := make([]int64, len(v))
for i := range v {
ids[i] = v[i].ID
}
return _u.RemoveAccountIDs(ids...)
}
// Where appends a list predicates to the ProxyUpdate builder. // Where appends a list predicates to the ProxyUpdate builder.
func (_u *ProxyUpdateOne) Where(ps ...predicate.Proxy) *ProxyUpdateOne { func (_u *ProxyUpdateOne) Where(ps ...predicate.Proxy) *ProxyUpdateOne {
_u.mutation.Where(ps...) _u.mutation.Where(ps...)
...@@ -630,6 +748,51 @@ func (_u *ProxyUpdateOne) sqlSave(ctx context.Context) (_node *Proxy, err error) ...@@ -630,6 +748,51 @@ func (_u *ProxyUpdateOne) sqlSave(ctx context.Context) (_node *Proxy, err error)
if value, ok := _u.mutation.Status(); ok { if value, ok := _u.mutation.Status(); ok {
_spec.SetField(proxy.FieldStatus, field.TypeString, value) _spec.SetField(proxy.FieldStatus, field.TypeString, value)
} }
if _u.mutation.AccountsCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: true,
Table: proxy.AccountsTable,
Columns: []string{proxy.AccountsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeInt64),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.RemovedAccountsIDs(); len(nodes) > 0 && !_u.mutation.AccountsCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: true,
Table: proxy.AccountsTable,
Columns: []string{proxy.AccountsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeInt64),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.AccountsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: true,
Table: proxy.AccountsTable,
Columns: []string{proxy.AccountsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeInt64),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
_node = &Proxy{config: _u.config} _node = &Proxy{config: _u.config}
_spec.Assign = _node.assignValues _spec.Assign = _node.assignValues
_spec.ScanValues = _node.scanValues _spec.ScanValues = _node.scanValues
......
...@@ -13,6 +13,7 @@ import ( ...@@ -13,6 +13,7 @@ import (
"github.com/Wei-Shaw/sub2api/ent/redeemcode" "github.com/Wei-Shaw/sub2api/ent/redeemcode"
"github.com/Wei-Shaw/sub2api/ent/schema" "github.com/Wei-Shaw/sub2api/ent/schema"
"github.com/Wei-Shaw/sub2api/ent/setting" "github.com/Wei-Shaw/sub2api/ent/setting"
"github.com/Wei-Shaw/sub2api/ent/usagelog"
"github.com/Wei-Shaw/sub2api/ent/user" "github.com/Wei-Shaw/sub2api/ent/user"
"github.com/Wei-Shaw/sub2api/ent/userallowedgroup" "github.com/Wei-Shaw/sub2api/ent/userallowedgroup"
"github.com/Wei-Shaw/sub2api/ent/usersubscription" "github.com/Wei-Shaw/sub2api/ent/usersubscription"
...@@ -259,6 +260,10 @@ func init() { ...@@ -259,6 +260,10 @@ func init() {
group.DefaultSubscriptionType = groupDescSubscriptionType.Default.(string) group.DefaultSubscriptionType = groupDescSubscriptionType.Default.(string)
// group.SubscriptionTypeValidator is a validator for the "subscription_type" field. It is called by the builders before save. // group.SubscriptionTypeValidator is a validator for the "subscription_type" field. It is called by the builders before save.
group.SubscriptionTypeValidator = groupDescSubscriptionType.Validators[0].(func(string) error) group.SubscriptionTypeValidator = groupDescSubscriptionType.Validators[0].(func(string) error)
// groupDescDefaultValidityDays is the schema descriptor for default_validity_days field.
groupDescDefaultValidityDays := groupFields[10].Descriptor()
// group.DefaultDefaultValidityDays holds the default value on creation for the default_validity_days field.
group.DefaultDefaultValidityDays = groupDescDefaultValidityDays.Default.(int)
proxyMixin := schema.Proxy{}.Mixin() proxyMixin := schema.Proxy{}.Mixin()
proxyMixinHooks1 := proxyMixin[1].Hooks() proxyMixinHooks1 := proxyMixin[1].Hooks()
proxy.Hooks[0] = proxyMixinHooks1[0] proxy.Hooks[0] = proxyMixinHooks1[0]
...@@ -410,16 +415,114 @@ func init() { ...@@ -410,16 +415,114 @@ func init() {
return nil return nil
} }
}() }()
// settingDescValue is the schema descriptor for value field.
settingDescValue := settingFields[1].Descriptor()
// setting.ValueValidator is a validator for the "value" field. It is called by the builders before save.
setting.ValueValidator = settingDescValue.Validators[0].(func(string) error)
// settingDescUpdatedAt is the schema descriptor for updated_at field. // settingDescUpdatedAt is the schema descriptor for updated_at field.
settingDescUpdatedAt := settingFields[2].Descriptor() settingDescUpdatedAt := settingFields[2].Descriptor()
// setting.DefaultUpdatedAt holds the default value on creation for the updated_at field. // setting.DefaultUpdatedAt holds the default value on creation for the updated_at field.
setting.DefaultUpdatedAt = settingDescUpdatedAt.Default.(func() time.Time) setting.DefaultUpdatedAt = settingDescUpdatedAt.Default.(func() time.Time)
// setting.UpdateDefaultUpdatedAt holds the default value on update for the updated_at field. // setting.UpdateDefaultUpdatedAt holds the default value on update for the updated_at field.
setting.UpdateDefaultUpdatedAt = settingDescUpdatedAt.UpdateDefault.(func() time.Time) setting.UpdateDefaultUpdatedAt = settingDescUpdatedAt.UpdateDefault.(func() time.Time)
usagelogFields := schema.UsageLog{}.Fields()
_ = usagelogFields
// usagelogDescRequestID is the schema descriptor for request_id field.
usagelogDescRequestID := usagelogFields[3].Descriptor()
// usagelog.RequestIDValidator is a validator for the "request_id" field. It is called by the builders before save.
usagelog.RequestIDValidator = func() func(string) error {
validators := usagelogDescRequestID.Validators
fns := [...]func(string) error{
validators[0].(func(string) error),
validators[1].(func(string) error),
}
return func(request_id string) error {
for _, fn := range fns {
if err := fn(request_id); err != nil {
return err
}
}
return nil
}
}()
// usagelogDescModel is the schema descriptor for model field.
usagelogDescModel := usagelogFields[4].Descriptor()
// usagelog.ModelValidator is a validator for the "model" field. It is called by the builders before save.
usagelog.ModelValidator = func() func(string) error {
validators := usagelogDescModel.Validators
fns := [...]func(string) error{
validators[0].(func(string) error),
validators[1].(func(string) error),
}
return func(model string) error {
for _, fn := range fns {
if err := fn(model); err != nil {
return err
}
}
return nil
}
}()
// usagelogDescInputTokens is the schema descriptor for input_tokens field.
usagelogDescInputTokens := usagelogFields[7].Descriptor()
// usagelog.DefaultInputTokens holds the default value on creation for the input_tokens field.
usagelog.DefaultInputTokens = usagelogDescInputTokens.Default.(int)
// usagelogDescOutputTokens is the schema descriptor for output_tokens field.
usagelogDescOutputTokens := usagelogFields[8].Descriptor()
// usagelog.DefaultOutputTokens holds the default value on creation for the output_tokens field.
usagelog.DefaultOutputTokens = usagelogDescOutputTokens.Default.(int)
// usagelogDescCacheCreationTokens is the schema descriptor for cache_creation_tokens field.
usagelogDescCacheCreationTokens := usagelogFields[9].Descriptor()
// usagelog.DefaultCacheCreationTokens holds the default value on creation for the cache_creation_tokens field.
usagelog.DefaultCacheCreationTokens = usagelogDescCacheCreationTokens.Default.(int)
// usagelogDescCacheReadTokens is the schema descriptor for cache_read_tokens field.
usagelogDescCacheReadTokens := usagelogFields[10].Descriptor()
// usagelog.DefaultCacheReadTokens holds the default value on creation for the cache_read_tokens field.
usagelog.DefaultCacheReadTokens = usagelogDescCacheReadTokens.Default.(int)
// usagelogDescCacheCreation5mTokens is the schema descriptor for cache_creation_5m_tokens field.
usagelogDescCacheCreation5mTokens := usagelogFields[11].Descriptor()
// usagelog.DefaultCacheCreation5mTokens holds the default value on creation for the cache_creation_5m_tokens field.
usagelog.DefaultCacheCreation5mTokens = usagelogDescCacheCreation5mTokens.Default.(int)
// usagelogDescCacheCreation1hTokens is the schema descriptor for cache_creation_1h_tokens field.
usagelogDescCacheCreation1hTokens := usagelogFields[12].Descriptor()
// usagelog.DefaultCacheCreation1hTokens holds the default value on creation for the cache_creation_1h_tokens field.
usagelog.DefaultCacheCreation1hTokens = usagelogDescCacheCreation1hTokens.Default.(int)
// usagelogDescInputCost is the schema descriptor for input_cost field.
usagelogDescInputCost := usagelogFields[13].Descriptor()
// usagelog.DefaultInputCost holds the default value on creation for the input_cost field.
usagelog.DefaultInputCost = usagelogDescInputCost.Default.(float64)
// usagelogDescOutputCost is the schema descriptor for output_cost field.
usagelogDescOutputCost := usagelogFields[14].Descriptor()
// usagelog.DefaultOutputCost holds the default value on creation for the output_cost field.
usagelog.DefaultOutputCost = usagelogDescOutputCost.Default.(float64)
// usagelogDescCacheCreationCost is the schema descriptor for cache_creation_cost field.
usagelogDescCacheCreationCost := usagelogFields[15].Descriptor()
// usagelog.DefaultCacheCreationCost holds the default value on creation for the cache_creation_cost field.
usagelog.DefaultCacheCreationCost = usagelogDescCacheCreationCost.Default.(float64)
// usagelogDescCacheReadCost is the schema descriptor for cache_read_cost field.
usagelogDescCacheReadCost := usagelogFields[16].Descriptor()
// usagelog.DefaultCacheReadCost holds the default value on creation for the cache_read_cost field.
usagelog.DefaultCacheReadCost = usagelogDescCacheReadCost.Default.(float64)
// usagelogDescTotalCost is the schema descriptor for total_cost field.
usagelogDescTotalCost := usagelogFields[17].Descriptor()
// usagelog.DefaultTotalCost holds the default value on creation for the total_cost field.
usagelog.DefaultTotalCost = usagelogDescTotalCost.Default.(float64)
// usagelogDescActualCost is the schema descriptor for actual_cost field.
usagelogDescActualCost := usagelogFields[18].Descriptor()
// usagelog.DefaultActualCost holds the default value on creation for the actual_cost field.
usagelog.DefaultActualCost = usagelogDescActualCost.Default.(float64)
// usagelogDescRateMultiplier is the schema descriptor for rate_multiplier field.
usagelogDescRateMultiplier := usagelogFields[19].Descriptor()
// usagelog.DefaultRateMultiplier holds the default value on creation for the rate_multiplier field.
usagelog.DefaultRateMultiplier = usagelogDescRateMultiplier.Default.(float64)
// usagelogDescBillingType is the schema descriptor for billing_type field.
usagelogDescBillingType := usagelogFields[20].Descriptor()
// usagelog.DefaultBillingType holds the default value on creation for the billing_type field.
usagelog.DefaultBillingType = usagelogDescBillingType.Default.(int8)
// usagelogDescStream is the schema descriptor for stream field.
usagelogDescStream := usagelogFields[21].Descriptor()
// usagelog.DefaultStream holds the default value on creation for the stream field.
usagelog.DefaultStream = usagelogDescStream.Default.(bool)
// usagelogDescCreatedAt is the schema descriptor for created_at field.
usagelogDescCreatedAt := usagelogFields[24].Descriptor()
// usagelog.DefaultCreatedAt holds the default value on creation for the created_at field.
usagelog.DefaultCreatedAt = usagelogDescCreatedAt.Default.(func() time.Time)
userMixin := schema.User{}.Mixin() userMixin := schema.User{}.Mixin()
userMixinHooks1 := userMixin[1].Hooks() userMixinHooks1 := userMixin[1].Hooks()
user.Hooks[0] = userMixinHooks1[0] user.Hooks[0] = userMixinHooks1[0]
...@@ -518,6 +621,10 @@ func init() { ...@@ -518,6 +621,10 @@ func init() {
// userallowedgroup.DefaultCreatedAt holds the default value on creation for the created_at field. // userallowedgroup.DefaultCreatedAt holds the default value on creation for the created_at field.
userallowedgroup.DefaultCreatedAt = userallowedgroupDescCreatedAt.Default.(func() time.Time) userallowedgroup.DefaultCreatedAt = userallowedgroupDescCreatedAt.Default.(func() time.Time)
usersubscriptionMixin := schema.UserSubscription{}.Mixin() usersubscriptionMixin := schema.UserSubscription{}.Mixin()
usersubscriptionMixinHooks1 := usersubscriptionMixin[1].Hooks()
usersubscription.Hooks[0] = usersubscriptionMixinHooks1[0]
usersubscriptionMixinInters1 := usersubscriptionMixin[1].Interceptors()
usersubscription.Interceptors[0] = usersubscriptionMixinInters1[0]
usersubscriptionMixinFields0 := usersubscriptionMixin[0].Fields() usersubscriptionMixinFields0 := usersubscriptionMixin[0].Fields()
_ = usersubscriptionMixinFields0 _ = usersubscriptionMixinFields0
usersubscriptionFields := schema.UserSubscription{}.Fields() usersubscriptionFields := schema.UserSubscription{}.Fields()
......
...@@ -168,6 +168,13 @@ func (Account) Edges() []ent.Edge { ...@@ -168,6 +168,13 @@ func (Account) Edges() []ent.Edge {
// 一个账户可以属于多个分组,一个分组可以包含多个账户 // 一个账户可以属于多个分组,一个分组可以包含多个账户
edge.To("groups", Group.Type). edge.To("groups", Group.Type).
Through("account_groups", AccountGroup.Type), Through("account_groups", AccountGroup.Type),
// proxy: 账户使用的代理配置(可选的一对一关系)
// 使用已有的 proxy_id 外键字段
edge.To("proxy", Proxy.Type).
Field("proxy_id").
Unique(),
// usage_logs: 该账户的使用日志
edge.To("usage_logs", UsageLog.Type),
} }
} }
......
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"time" "time"
"entgo.io/ent" "entgo.io/ent"
"entgo.io/ent/dialect"
"entgo.io/ent/dialect/entsql" "entgo.io/ent/dialect/entsql"
"entgo.io/ent/schema" "entgo.io/ent/schema"
"entgo.io/ent/schema/edge" "entgo.io/ent/schema/edge"
...@@ -33,7 +34,8 @@ func (AccountGroup) Fields() []ent.Field { ...@@ -33,7 +34,8 @@ func (AccountGroup) Fields() []ent.Field {
Default(50), Default(50),
field.Time("created_at"). field.Time("created_at").
Immutable(). Immutable().
Default(time.Now), Default(time.Now).
SchemaType(map[string]string{dialect.Postgres: "timestamptz"}),
} }
} }
......
...@@ -60,12 +60,13 @@ func (ApiKey) Edges() []ent.Edge { ...@@ -60,12 +60,13 @@ func (ApiKey) Edges() []ent.Edge {
Ref("api_keys"). Ref("api_keys").
Field("group_id"). Field("group_id").
Unique(), Unique(),
edge.To("usage_logs", UsageLog.Type),
} }
} }
func (ApiKey) Indexes() []ent.Index { func (ApiKey) Indexes() []ent.Index {
return []ent.Index{ return []ent.Index{
index.Fields("key").Unique(), // key 字段已在 Fields() 中声明 Unique(),无需重复索引
index.Fields("user_id"), index.Fields("user_id"),
index.Fields("group_id"), index.Fields("group_id"),
index.Fields("status"), index.Fields("status"),
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment