<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Queryx類型安全 Go ORM

          聯(lián)合創(chuàng)作 · 2023-10-01 00:18

          Queryx 是 Schema 優(yōu)先且類型安全的 Go ORM。

          Queryx 使用schema.hcl來描述數(shù)據(jù)庫,在以下例子中定義了數(shù)據(jù)庫環(huán)境以及數(shù)據(jù)庫模型。

          schema.hcl

          database "db" {
            adapter = "postgresql"
          
            config "development" {
              url = "postgres://postgres:postgres@localhost:5432/blog_development?sslmode=disable"
            }
          
            config "production" {
              url = env("DATABASE_URL")
            }
          
            generator "client-golang" {}
          
            model "Post" {
              column "title" {
                type = string
              }
              column "content" {
                type = text
              }
            }
          }
          
          

          運行queryx db:create命令創(chuàng)建 postgres 數(shù)據(jù)庫,然后運行queryx db:migrate,就可以自動創(chuàng)建對應的 migration 文件和數(shù)據(jù)庫結構。

          CRUD

          運行queryx gdb目錄下會生成對應的 ORM 代碼,生成的代碼根據(jù)數(shù)據(jù)庫生成對應的 Go 類型。生成的代碼除了 driver 之外沒有其他第三方依賴。

          下面是一些 CRUD 操作的示例代碼:

          // 創(chuàng)建
          newPost := c.ChangePost().SetTitle("post title")
          post, err := c.QueryPost().Create(newPost)
          
          // 查詢
          post, err := c.QueryPost().Find(1)
          posts, err := c.QueryPost().Where(c.PostTitle.EQ("post title")).All()
          
          // 更新
          updatePost := c.ChangePost().SetTitle("new post title")
          err := post.Update(updatePost)
          updated, err := c.QueryPost().Where(c.PostTitle.EQ("post title")).UpdateAll(updatePost)
          
          // 刪除
          err := post.Delete()
          deleted, err := c.QueryPost().Where(c.PostTitle.EQ("post title")).DeleteAll()
          
          

          關系

          schema.hcl也可以聲明各個 model 之間的關系,包括belongs_to,has_one,has_many,例如:

          model "User" {
            belongs_to "group" {}
          
            column "name" {
              type = string
            }
          }
          
          model "Group" {
            has_many "users" {}
          
            column "name" {
              type = string
            }
          }
          
          

          聲明關系之后,可以使用生成的preload方法來避免 n+1 查詢,比如:

          users, err := c.QueryUser().PreloadGroup().All()
          // users[0].Groups
          
          groups, err := c.QueryGroup().PreloadUsers().All()
          // groups[0].User
          
          
          瀏覽 20
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          編輯 分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          編輯 分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  91综合一| 九色在线入口 | 日韩毛片一区二区 | 色av导航 | 99热手机在线精品 |