使用 Postgres 和 MongoDB 实现增删改查操作?
时间:2024-02-07
浏览:158
作者:佚名
对于一个Golang开发者来说,牢固扎实的基础是十分重要的,golang学习网就来带大家一点点的掌握基础知识点。今天本篇文章带大家了解《使用 Postgres 和 MongoDB 实现增删改查操作?》,主要介绍了,希望对大家的知识积累有所帮助,快点收藏起来吧,否则需要时就找不到了!
问题内容
我对 Go 和后端还很陌生,并且正在参加 Go 实习计划。我们构建了一个连接到 psql 数据库的 CRUD,现在我被告知连接 mongoDB,我们将其用于开发,而 PSQL 将用于生产。 我是否必须从头开始创建用于 mongo 的新处理程序,或者我可以使用相同的处理程序并以某种方式确定正在使用的数据库类型并相应地使用相应的逻辑吗? 例如,我有一个用于用户注册端点的处理程序:
func (ctrl *UserController) Register(c *gin.Context) {
var user models.User
if err := c.BindJSON(&user); err != nil {
c.AbortWithStatusJSON(http.StatusUnprocessableEntity, gin.H{
"error": true,
"message": err.Error(),
})
return
}
err := ctrl.userService.Register(&user)
if err != nil {
c.AbortWithStatusJSON(http.StatusUnprocessableEntity, gin.H{
"error": true,
"message": err.Error(),
})
return
}
c.JSON(http.StatusCreated, gin.H{
"message": "successfully created an user",
})
}
func (svc *UserService) Register(user *models.User) error {
if svc.userRepo.CheckIfEmailExists(user.Email) {
return errors.New("user already registered")
}
hash, err := svc.generatePasswordHash(user.Password)
if err != nil {
return errors.New("err can't register user")
}
user.Password = hash
return svc.userRepo.Insert(user)
}
func (repo *UserRepository) CheckIfEmailExists(mail string) bool {
var user models.User
err := repo.dbClient.Debug().Model(models.User{}).Where("email = ?", mail).Find(&user).Error
return errors.Is(err, gorm.ErrRecordNotFound)
}
func (repo *UserRepository) Insert(user *models.User) error {
err := repo.dbClient.Debug().Model(models.User{}).Create(user).Error
if err != nil {
log.Printf("failed to insert user: %v\n", err)
return err
}
return nil
}
我为 mongo 构建了插入、注册和 CheckIfEmailExists 函数:
func (repo *UserRepository) InsertInMongo(user *models.UserB) error {
coll := repo.mongoClient.DB.Collection("users")
_, err := coll.InsertOne(context.TODO(), user)
if err != nil {
log.Printf("failed to insert user: %v\n", err)
return err
}
return nil
}
func (svc *UserService) RegisterToMongo(user *models.UserB) error {
check := svc.userRepo.CheckIfEmailExistsInMongo(user.Email)
if check {
return errors.New("user already registered")
}
hash, err := svc.generatePasswordHash(user.Password)
if err != nil {
return errors.New("err can't register user")
}
user.Password = hash
return svc.userRepo.InsertInMongo(user)
}
func (repo *UserRepository) CheckIfEmailExistsInMongo(email string) bool {
coll := repo.mongoClient.Collection
filter := bson.D{{Key: "email", Value: email}}
count, err := coll.CountDocuments(context.TODO(), filter)
if err != nil {
panic(err)
}
if count != 0 {
return true
}
return false
}正确答案
您没有提供如何创建 UserService 的代码。
理想情况下,您应该有一个如下所示的界面:
type UserRepository interface {
CheckIfEmailExists(mail string) bool
Insert(user *models.User) error
…
}
然后,UserService 应该像这样创建:
// We use the interface as param
func NewUserService(userRepo UserRepository) UserService {
return UserService{
UserRepo: userRepo
}
}
并且您将有两个单独的存储库,都实现 UserRepository 接口 - 这意味着它们应该具有与接口相同的名称和相同签名(参数、返回类型)的方法:
mongo_user_repository.go
Type MongoUserRepo struct {
…
}
func (repo MongoUserRepo) CheckIfEmailExists(mail string) bool {
… some mongo logic here
}
func (repo MongoUserRepo) Insert(user *models.User) error {
… some mongo logic here
}
postgres_user_repository.go
type PostgresUserRepo struct {
…
}
func (repo PostgresUserRepo) CheckIfEmailExists(mail string) bool {
… some postgres logic here
}
func (repo PostgresUserRepo) Insert(user *models.User) error {
… some postgres logic here
}
您可以根据用例传递其中任何一个,如下面的(非常糟糕的)示例:
main.go
…
var userService UserService
if os.Getenv(“environment”) == “prod” {
userService = NewUserService(postgreUserRepo)
} else if os.Getenv(“environment”) == “dev” {
userService = NewUserService(mongoUserRepo)
}今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
11.20
11.20
11.20
11.20
热门专题
