How to use gorm with Beego

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

How to use gorm with Beego

问题

Beego ORM目前还不完整(例如,它不支持外键约束)。所以我决定在Beego中使用gorm。这样做的正确方式是什么?我看到了gorm的示例代码:

import (
    "github.com/jinzhu/gorm"
    _ "github.com/jinzhu/gorm/dialects/postgres"
)

func main() {
  db, err := gorm.Open("postgres", "host=myhost user=gorm dbname=gorm sslmode=disable password=mypassword")
  defer db.Close()
}

但是我每次在每个控制器函数中都需要连接到数据库吗?有没有一种像长轮询连接这样的方式可以使用?

英文:

Beego ORM is somehow incomplete for now (for example it doesn't support foreign key constraints). So I've decided to use gorm with Beego. What is proper way of doing that? I've seen the sample code from gorm:

import (
    "github.com/jinzhu/gorm"
    _ "github.com/jinzhu/gorm/dialects/postgres"
)

func main() {
  db, err := gorm.Open("postgres", "host=myhost user=gorm dbname=gorm sslmode=disable password=mypassword")
  defer db.Close()
}

But do I have to connect to database each time in every controller function? Is there a way to use something like long polling connections?

答案1

得分: 3

gorm在底层使用嵌入在gorm.DB中的sql.DB类型。

DB是表示零个或多个底层连接池的数据库句柄。它可以被多个goroutine并发使用。sql包会自动创建和释放连接,并维护一个空闲连接池。

因此,您可以在代码中全局使用获取到的DB,如果您希望在请求处理中使用事务来实现隔离级别,可以使用以下代码:

tr := db.Begin()
英文:

gorm uses sql.DB type embedded in gorm.DB under the hood which

> DB is a database handle representing a pool of zero or more
> underlying connections. It's safe for concurrent use by multiple
> goroutines. The sql package creates and frees connections
> automatically; it also maintains a free pool of idle connections.

So you can use obtained DB globally in your code,
if you want level of isolation in request handling use transaction

tr:=db.Begin()

答案2

得分: 1

所以,正如@Uvelichitel指出的那样,你的选择是在全局级别定义你的db连接,并从所需的位置使用它(可能是主函数来打开连接和模型层来查询结果)。

因此,你可以创建一个包含数据库连接逻辑的文件:

// appname/conn.go

package db

import (
  "github.com/jinzhu/gorm"
  ...
)

var (
  // 这个变量将包含一个已打开的连接
  // 确保在使用之前调用Connect()
  Conn *gorm.DB
)

func Connect(dbConnString string) (*gorm.DB, error) {
  db, err := gorm.Open("postgres", dbConnString)
  Conn = db
  return db, err
}

在你的main.go中调用db.Connect之后,你可以在应用程序的任何地方自由地使用已打开的连接db.Conn(只需确保在使用的地方导入该包)。

import "appname/db"

func main() {
  conn, _ := db.Connect("host=localhost user=postgres ...")
  // db.Conn已初始化并可以在其他任何地方使用
}

在单个main.go文件中也可以实现相同的结果,直接在那里声明全局变量并进行连接逻辑的处理。

英文:

So, as @Uvelichitel pointed out, your option is to define your db connection at the global level and to use it from a desired place (probably main function to open a connection and model layer to query for results).

So you could basically have a file containing your db connection logics:

// appname/conn.go

package db

import (
  "github.com/jinzhu/gorm"
  ...
)

var (
  // this one gonna contain an open connection
  // make sure to call Connect() before using it
  Conn *gorm.DB
)

func Connect(dbConnString string) (*gorm.DB, error) {
  db, err := gorm.Open("postgres", dbConnString)
  Conn = db
  return db, err
}

After you call db.Connect from your main.go you are free to use an opened connection db.Conn from anywhere of your application (just make sure you're importing this package to the places of use).

import "appname/db"

func main() {
  conn, _ := db.Connect("host=localhost user=postgres ...")
  // db.Conn is initialized and ready for usage anywhere else

The same result could be achieved within a single main.go file, moving global variable declaration and a connection logics straight there.

huangapple
  • 本文由 发表于 2016年9月8日 09:56:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/39381459.html
匿名

发表评论

匿名网友

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

确定