自动将哈希编码数据插入gorm中。

huangapple go评论125阅读模式
英文:

insert hash coded data in gorm automatically

问题

你可以使用GORM的钩子函数来实现在插入或更新数据时自动填充HashID列。钩子函数可以在模型的创建、更新、删除等事件发生时执行自定义的逻辑。

在你的模型中,你可以定义一个BeforeSave钩子函数,它会在保存数据之前被调用。在这个钩子函数中,你可以计算PhoneNumber列的sha256哈希值,并将结果赋值给HashID列。

以下是一个示例代码:

  1. import (
  2. "crypto/sha256"
  3. "encoding/hex"
  4. )
  5. type Users struct {
  6. gorm.Model
  7. ID uint `gorm:"autoIncrement;unique" json:"id"`
  8. PhoneNumber string `json:"phone_number"`
  9. HashID string `gorm:"primaryKey" json:"hash_id"`
  10. Name string `gorm:"default:dear user" json:"name"`
  11. Rank uint `json:"rank"`
  12. Score uint `json:"score"`
  13. Image string `json:"image"`
  14. Email string `json:"email"`
  15. Address string `json:"address"`
  16. Birthday string `json:"birthday"`
  17. Biography string `json:"biography"`
  18. }
  19. func (u *Users) BeforeSave(tx *gorm.DB) (err error) {
  20. hash := sha256.Sum256([]byte(u.PhoneNumber))
  21. u.HashID = hex.EncodeToString(hash[:])
  22. return nil
  23. }

在这个示例中,BeforeSave函数接收一个*gorm.DB参数,你可以在函数中访问数据库事务对象。在函数内部,我们使用sha256包计算PhoneNumber列的哈希值,并使用hex包将其转换为字符串形式。然后,我们将计算得到的哈希值赋值给HashID列。

当你插入或更新数据时,GORM会自动调用BeforeSave函数,并在保存数据之前执行其中的逻辑。这样,HashID列就会被自动填充为PhoneNumber列的sha256哈希值。

希望对你有所帮助!如果你还有其他问题,请随时提问。

英文:

I have a table that represents my user data. There is a field that represents a phone number, and I want to store the hash of that automatically in the database at either update or insert.
so my model is :

  1. type Users struct {
  2. gorm.Model
  3. ID uint `gorm:"autoIncrement;unique" json:"id"`
  4. PhoneNumber string `json:"phone_number"`
  5. HashID string `gorm:"primaryKey" json:"hash_id"`
  6. Name string `gorm:"default:dear user" json:"name"`
  7. Rank uint `json:"rank"`
  8. Score uint `json:"score"`
  9. Image string `json:"image"`
  10. Email string `json:"email"`
  11. Address string `json:"address"`
  12. Birthday string `json:"birthday"`
  13. Biography string `json:"biography"
  14. }

How can I tell the GORM to fill the HashID column with the sha256 hash code of the PhoneNumber column while inserting or updating data?

答案1

得分: 1

你需要这样的代码:

  1. package main
  2. import (
  3. "crypto/sha256"
  4. "fmt"
  5. "gorm.io/driver/sqlite"
  6. "gorm.io/gorm"
  7. )
  8. type Users struct {
  9. gorm.Model
  10. Key string `json:"phone_number"`
  11. Hash string `gorm:"primaryKey" json:"hash_id"`
  12. }
  13. func (u *Users) BeforeCreate(tx *gorm.DB) (err error) {
  14. h := sha256.Sum256([]byte(u.Key))
  15. u.Hash = fmt.Sprintf("%x", h[:])
  16. return nil
  17. }
  18. func (u *Users) BeforeSave(tx *gorm.DB) (err error) {
  19. h := sha256.Sum256([]byte(u.Key))
  20. u.Hash = fmt.Sprintf("%x", h[:])
  21. return nil
  22. }
  23. func main() {
  24. db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
  25. if err != nil {
  26. panic("failed to connect database")
  27. }
  28. db.AutoMigrate(&Users{})
  29. u := Users{Key: "123"}
  30. db.Create(&u)
  31. }

请查看 https://gorm.io/docs/index.html

英文:

You need something like this:

  1. package main
  2. import (
  3. "crypto/sha256"
  4. "fmt"
  5. "gorm.io/driver/sqlite"
  6. "gorm.io/gorm"
  7. )
  8. type Users struct {
  9. gorm.Model
  10. Key string `json:"phone_number"`
  11. Hash string `gorm:"primaryKey" json:"hash_id"`
  12. }
  13. func (u *Users) BeforeCreate(tx *gorm.DB) (err error) {
  14. h := sha256.Sum256([]byte(u.Key))
  15. u.Hash = fmt.Sprintf("%x", h[:])
  16. return nil
  17. }
  18. func (u *Users) BeforeSave(tx *gorm.DB) (err error) {
  19. h := sha256.Sum256([]byte(u.Key))
  20. u.Hash = fmt.Sprintf("%x", h[:])
  21. return nil
  22. }
  23. func main() {
  24. db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
  25. if err != nil {
  26. panic("failed to connect database")
  27. }
  28. db.AutoMigrate(&Users{})
  29. u := Users{Key: "123"}
  30. db.Create(&u)
  31. }

Check https://gorm.io/docs/index.html

huangapple
  • 本文由 发表于 2021年8月18日 15:59:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/68828561.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定