文档
入门
快速入门

快速入门

最快的入门方式是 克隆快速入门仓库。或者,你可以通过 下面的手动步骤从头开始创建一个新项目.

使用快速入门仓库

克隆快速入门仓库

git clone [email protected]:steebchen/prisma-go-demo.git && cd prisma-go-demo

创建预定义的 SQLite 数据库并生成 Go 客户端

go run github.com/steebchen/prisma-client-go db push

最后,运行位于 main.go 的简单主程序

go run .
# created post: {
#   "id": "ckfnrp7ec0000oh9kygil9s94",
#   "createdAt": "2020-09-29T09:37:44.628Z",
#   "updatedAt": "2020-09-29T09:37:44.628Z",
#   "title": "Hi from Prisma!",
#   "published": true,
#   "desc": "Prisma is a database toolkit and makes databases easy."
# }
# post: {
#   "id": "ckfnrp7ec0000oh9kygil9s94",
#   "createdAt": "2020-09-29T09:37:44.628Z",
#   "updatedAt": "2020-09-29T09:37:44.628Z",
#   "title": "Hi from Prisma!",
#   "published": true,
#   "desc": "Prisma is a database toolkit and makes databases easy."
# }
# The posts's title is: Prisma is a database toolkit and makes databases easy.

下一步

我们只是触及了你能做的事情的表面。阅读 高级教程,了解更复杂的查询以及如何查询关系。

你也可以在 GoPrisma (在新标签页中打开) 中阅读完整文档。

手动设置

初始化一个新的 Go 项目

如果您还没有 Go 项目,请使用 Go 模块初始化一个项目

mkdir demo && cd demo
go mod init demo

获取 Prisma Client Go

在您的项目中安装 Go 模块

go get github.com/steebchen/prisma-client-go

准备您的 Prisma 数据库模式

schema.prisma 文件中准备您的数据库模式。例如,一个简单的模式,使用 sqlite 数据库和 Prisma Client Go 作为生成器,包含两个模型,如下所示

datasource db {
  // could be postgresql or mysql
  provider = "sqlite"
  url      = "file:dev.db"
}
 
generator db {
  provider = "go run github.com/steebchen/prisma-client-go"
}
 
model Post {
  id        String   @default(cuid()) @id
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  title     String
  published Boolean
  desc      String?
}

接下来,运行 db push 将您的模式与数据库同步。如果数据库不存在,它也会创建数据库。

# sync the database with your schema
go run github.com/steebchen/prisma-client-go db push
# The Prisma Client Go client is automatically generated in your project.
# You can re-run this command any time to sync your schema with the database.

如果您只想重新生成客户端,请运行 go run github.com/steebchen/prisma-client-go generate

要为您的生产数据库创建迁移,请使用 Prisma 迁移工具 migrate (在新标签页中打开) 来创建和迁移您的数据库。

用法

创建一个文件 main.go(如果需要,将导入调整到 db 文件夹)

package main
 
import (
  "context"
  "encoding/json"
  "fmt"
 
  // adapt "demo" to your module name if it differs
  "demo/db"
)
 
func main() {
  if err := run(); err != nil {
    panic(err)
  }
}
 
func run() error {
  client := db.NewClient()
  if err := client.Prisma.Connect(); err != nil {
    return err
  }
 
  defer func() {
    if err := client.Prisma.Disconnect(); err != nil {
      panic(err)
    }
  }()
 
  ctx := context.Background()
 
  // create a post
  createdPost, err := client.Post.CreateOne(
    db.Post.Title.Set("Hi from Prisma!"),
    db.Post.Published.Set(true),
    db.Post.Desc.Set("Prisma is a database toolkit and makes databases easy."),
  ).Exec(ctx)
  if err != nil {
    return err
  }
 
  result, _ := json.MarshalIndent(createdPost, "", "  ")
  fmt.Printf("created post: %s\n", result)
 
  // find a single post
  post, err := client.Post.FindUnique(
    db.Post.ID.Equals(createdPost.ID),
  ).Exec(ctx)
  if err != nil {
    return err
  }
 
  result, _ = json.MarshalIndent(post, "", "  ")
  fmt.Printf("post: %s\n", result)
 
  // for optional/nullable values, you need to check the function and create two return values
  // `desc` is a string, and `ok` is a bool whether the record is null or not. If it's null,
  // `ok` is false, and `desc` will default to Go's default values; in this case an empty string (""). Otherwise,
  // `ok` is true and `desc` will be "my description".
  desc, ok := post.Desc()
  if !ok {
    return fmt.Errorf("post's description is null")
  }
 
  fmt.Printf("The posts's description is: %s\n", desc)
 
  return nil
}

确保您的 go.mod 是最新的

go mod tidy

然后运行它

go run .
❯ go run .
created post: {
  "id": "ckfnrp7ec0000oh9kygil9s94",
  "createdAt": "2020-09-29T09:37:44.628Z",
  "updatedAt": "2020-09-29T09:37:44.628Z",
  "title": "Hi from Prisma!",
  "published": true,
  "desc": "Prisma is a database toolkit and makes databases easy."
}
post: {
  "id": "ckfnrp7ec0000oh9kygil9s94",
  "createdAt": "2020-09-29T09:37:44.628Z",
  "updatedAt": "2020-09-29T09:37:44.628Z",
  "title": "Hi from Prisma!",
  "published": true,
  "desc": "Prisma is a database toolkit and makes databases easy."
}
The posts's title is: Prisma is a database toolkit and makes databases easy.

下一步

了解更多关于 使用 Go CLI 的 Prisma CLI 命令,例如 generatemigratedbintrospect

我们只是触及了你能做的事情的表面。阅读 高级教程,了解更复杂的查询以及如何查询关系。