Skip to content

Conversation

@AlbertChenshiqi
Copy link

@AlbertChenshiqi AlbertChenshiqi commented Jul 22, 2025

  • Do only one thing
  • Non breaking API changes
  • Tested

What did this pull request do?

通常情况下,多张业务逻辑表有多个分表的维度。例如,订单表可以按订单ID分表;同样,按用户ID分表,在根据用户ID查询订单时也能提速,就像我们常查看自己的订单那样。这个组件原来也是支持这种功能,只不过需要初始化不同的数据库实例才能实现。为了更好的支持这个功能:赋能单表,灵活支持多分表策略,我基于model实现多model的register。

User Case Description

support multiple config with multiple model register
model must implement ShardingInterface
example: base_test.go

type Order struct {
    gorm.Model
    UserID    uint64
    ProductID int64
    Amount    int64
    sharding.BaseSharding
}

func (Order) TableName() string {
    return "order"
}

type OtherTable struct {
    gorm.Model
    UserID    uint64
    
    sharding.BaseSharding
}

func (OtherTable) TableName() string {
    return "other_table"
}
otherTable := &OtherTable{}
otherTable.DefaultShards = 2
db.Use(sharding.RegisterWithModel(&Order{}, otherTable))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant