go-nunu / nunu

A CLI tool for building Go applications.
https://go-nunu.github.io/nunu/
MIT License
1.92k stars 136 forks source link

postgresql数据库支持 #45

Closed exqmjmz closed 8 months ago

exqmjmz commented 9 months ago

目前只看到了对mysql的支持,如果需要支持postgresql,需要修改什么地方呢?

codingcn commented 9 months ago

参考gorm文档,安装"gorm.io/driver/postgres",修改nunu相应配置参数即可,与MySQL区别不大。

https://gorm.io/zh_CN/docs/connecting_to_the_database.html#PostgreSQL

infezek commented 9 months ago

可以有一个标志自动添加到其他数据库,如Postgres。

cleveng commented 8 months ago

目前只看到了对mysql的支持,如果需要支持postgresql,需要修改什么地方呢?

func (r *Repository) setDB(c *conf.Data_Postgres) {
    dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=disable TimeZone=Asia/Shanghai", c.Host, c.Username, c.Password, c.Database, c.Port)
    db, err := gorm.Open(postgres.New(postgres.Config{
        DSN:                  dsn,
        PreferSimpleProtocol: true, // disables implicit prepared statement usage
    }), &gorm.Config{
        DisableForeignKeyConstraintWhenMigrating: true, // 关闭外键约束,即允许零值
    })
    if err != nil {
        r.logger.Error("failed to open gorm connection", zap.Error(err))
        panic(err)
    }

    r.db = db
}