Go DriverArangoDB 官方 Go 驱动程序

联合创作 · 2023-09-30 20:04

Go Driver 是 ArangoDB 数据库的官方 Go 驱动程序。

支持的版本

  • ArangoDB 3.1 及更高版本
    • 单服务器和集群设置
    • 有或没有认证都支持
  • Go 1.7 及更高版本

Go 依赖

配置

要使用驱动程序,首先将源提取到您的GOPATH.

go get github.com/arangodb/go-driver 

使用驱动程序,需要始终创建一个Client. 以下示例显示如何在 localhost 上运行的单个服务器创建一个 Client

import ( "fmt" driver "github.com/arangodb/go-driver" "github.com/arangodb/go-driver/http" ) ... conn, err := http.NewConnection(http.ConnectionConfig{ Endpoints: []string{"http://localhost:8529"}, }) if err != nil { // Handle error } client, err := driver.NewClient(driver.ClientConfig{ Connection: conn, }) if err != nil { // Handle error } 

创建Client后,可以在服务器上访问/创建的数据库,访问/创建集合、图形、文档等。

重要类型

使用 Go 驱动程序需要了解的关键类型是:

  • Database 
  • Collection 
  • Graph 
  • EdgeDefinition

连接到 ArangoDB

conn, err := http.NewConnection(http.ConnectionConfig{ Endpoints: []string{"http://localhost:8529"}, TLSConfig: &tls.Config{ /*...*/ }, }) if err != nil { // Handle error } c, err := driver.NewClient(driver.ClientConfig{ Connection: conn, Authentication: driver.BasicAuthentication("user", "password"), }) if err != nil { // Handle error } 

打开数据库

ctx := context.Background() db, err := client.Database(ctx, "myDB") if err != nil { // handle error  }

检查集合是否存在

ctx := context.Background() found, err := db.CollectionExists(ctx, "myCollection") if err != nil { // handle error  } 

创建集合

ctx := context.Background() options := &driver.CreateCollectionOptions{ /* ... */ } col, err := db.CreateCollection(ctx, "myCollection", options) if err != nil { // handle error  }
浏览 7
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报