Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Config API same as dbresolver #2

Closed
huacnlee opened this issue Jan 19, 2022 · 1 comment
Closed

Refactor Config API same as dbresolver #2

huacnlee opened this issue Jan 19, 2022 · 1 comment
Assignees

Comments

@huacnlee
Copy link
Collaborator

db.Use(sharding.Register(sharding.Config{
		ShardingKey: "user_id",
		ShardingAlgorithm: func(value interface{}) (suffix string, err error) {
				if uid, ok := value.(int64); ok {
						return fmt.Sprintf("_%02d", uid%4), nil
				}
				return "", errors.New("invalid user_id")
		},
		PrimaryKeyGenerate: func(tableIdx int64) interface{} {
				// use Built in keygen for generate a sequence primary key with table index
				return keygen.Next(tableIdx)
		}
}, "orders").Register(sharding.Config{
		ShardingKey: "user_id",
		ShardingAlgorithm: func(value interface{}) (suffix string, err error) {
				if uid, ok := value.(int64); ok {
						return fmt.Sprintf("_%02d", uid%4), nil
				}
				return "", errors.New("invalid user_id")
		},
		PrimaryKeyGenerate: func(tableIdx int64) interface{} {
				return uuid.UUID()
		}
		// This case for show up give notifications, activities table use same sharding rule.
}, Notification{}, Acitivity{}))
@hyperphoton
Copy link
Collaborator

Fixed by #4

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

No branches or pull requests

3 participants