Go driver for Microsoft Azure DocumentDB
This is a WIP project.
I'm doing it on my spare time and hope to stabilize it soon. if you want to contribute, feel free to take some task here
- Get Started
- Examples
- Databases
- Collections
- Documents
- StoredProcedures
- UserDefinedFunctions
- Iterator
- DocumentIterator
$ go get github.com/a8m/documentdb-go
import (
"github.com/a8m/documentdb"
)
func main() {
client := documentdb.New("connection-url", documentdb.Config{"master-key"})
// Start using DocumentDB
dbs, err := client.ReadDatabases()
if err != nil {
log.Fatal(err)
}
fmt.Println(dbs)
}
func main() {
// ...
db, err := client.ReadDatabase("self_link")
if err != nil {
log.Fatal(err)
}
fmt.Println(db.Self, db.Id)
}
func main() {
// ...
dbs, err := client.QueryDatabases("SELECT * FROM ROOT r")
if err != nil {
log.Fatal(err)
}
for _, db := range dbs {
fmt.Println("DB Name:", db.Id)
}
}
func main() {
// ...
dbs, err := client.ReadDatabases()
if err != nil {
log.Fatal(err)
}
for _, db := range dbs {
fmt.Println("DB Name:", db.Id)
}
}
func main() {
// ...
db, err := client.CreateDatabase(`{ "id": "test" }`)
if err != nil {
log.Fatal(err)
}
fmt.Println(db)
// or ...
var db documentdb.Database
db.Id = "test"
db, err = client.CreateDatabase(&db)
}
func main() {
// ...
db, err := client.ReplaceDatabase("self_link", `{ "id": "test" }`)
if err != nil {
log.Fatal(err)
}
fmt.Println(db)
// or ...
var db documentdb.Database
db, err = client.ReplaceDatabase("self_link", &db)
}
func main() {
// ...
err := client.DeleteDatabase("self_link")
if err != nil {
log.Fatal(err)
}
}
func main() {
// ...
coll, err := client.ReadCollection("self_link")
if err != nil {
log.Fatal(err)
}
fmt.Println(coll.Self, coll.Id)
}
func main() {
// ...
colls, err := client.QueryCollections("db_self_link", "SELECT * FROM ROOT r")
if err != nil {
log.Fatal(err)
}
for _, coll := range colls {
fmt.Println("Collection Name:", coll.Id)
}
}
func main() {
// ...
colls, err := client.ReadCollections("db_self_link")
if err != nil {
log.Fatal(err)
}
for _, coll := range colls {
fmt.Println("Collection Name:", coll.Id)
}
}
func main() {
// ...
coll, err := client.CreateCollection("db_self_link", `{"id": "my_test"}`)
if err != nil {
log.Fatal(err)
}
fmt.Println("Collection Name:", coll.Id)
// or ...
var coll documentdb.Collection
coll.Id = "test"
coll, err = client.CreateCollection("db_self_link", &coll)
}
func main() {
// ...
err := client.DeleteCollection("self_link")
if err != nil {
log.Fatal(err)
}
}
type Document struct {
documentdb.Document
// Your external fields
Name string `json:"name,omitempty"`
Email string `json:"email,omitempty"`
}
func main() {
// ...
var doc Document
err = client.ReadDocument("self_link", &doc)
if err != nil {
log.Fatal(err)
}
fmt.Println("Document Name:", doc.Name)
}
type User struct {
documentdb.Document
// Your external fields
Name string `json:"name,omitempty"`
Email string `json:"email,omitempty"`
}
func main() {
// ...
var users []User
_, err = client.QueryDocuments(
"coll_self_link",
documentdb.NewQuery("SELECT * FROM ROOT r WHERE r.name=@name", documentdb.P{"@name", "john"}),
&users,
)
if err != nil {
log.Fatal(err)
}
for _, user := range users {
fmt.Print("Name:", user.Name, "Email:", user.Email)
}
}
type User struct {
documentdb.Document
// Your external fields
Name string `json:"name,omitempty"`
Email string `json:"email,omitempty"`
}
func main() {
// ...
var users []User
_, err = client.QueryDocuments(
"coll_self_link",
documentdb.NewQuery(
"SELECT * FROM ROOT r WHERE r.name=@name AND r.company_id = @company_id",
documentdb.P{"@name", "john"},
documentdb.P{"@company_id", "1234"},
),
&users,
documentdb.PartitionKey("1234")
)
if err != nil {
log.Fatal(err)
}
for _, user := range users {
fmt.Print("Name:", user.Name, "Email:", user.Email)
}
}
type User struct {
documentdb.Document
// Your external fields
Name string `json:"name,omitempty"`
Email string `json:"email,omitempty"`
}
func main() {
// ...
var users []User
err = client.ReadDocuments("coll_self_link", &users)
if err != nil {
log.Fatal(err)
}
for _, user := range users {
fmt.Print("Name:", user.Name, "Email:", user.Email)
}
}
type User struct {
documentdb.Document
// Your external fields
Name string `json:"name,omitempty"`
Email string `json:"email,omitempty"`
}
func main() {
// ...
var user User
// Note: If the `id` is missing(or empty) in the payload it will generate
// random document id(i.e: uuid4)
user.Id = "uuid"
user.Name = "Ariel"
user.Email = "[email protected]"
err := client.CreateDocument("coll_self_link", &doc)
if err != nil {
log.Fatal(err)
}
fmt.Print("Name:", user.Name, "Email:", user.Email)
}
type User struct {
documentdb.Document
// Your external fields
IsAdmin bool `json:"isAdmin,omitempty"`
}
func main() {
// ...
var user User
user.Id = "uuid"
user.IsAdmin = false
err := client.ReplaceDocument("doc_self_link", &user)
if err != nil {
log.Fatal(err)
}
fmt.Print("Is Admin:", user.IsAdmin)
}
func main() {
// ...
err := client.DeleteDocument("doc_self_link")
if err != nil {
log.Fatal(err)
}
}
func main() {
// ...
var docs []Document
err := client.ExecuteStoredProcedure("sporc_self", [...]interface{}{p1, p2}, &docs)
if err != nil {
log.Fatal(err)
}
// ...
}
func main() {
// ...
var docs []Document
iterator := documentdb.NewIterator(
client, documentdb.NewDocumentIterator("coll_self_link", nil, &docs, documentdb.PartitionKey("1"), documentdb.Limit(1)),
)
for iterator.Next() {
if err := iterator.Error(); err != nil {
log.Fatal(err)
}
fmt.Println(len(docs))
}
// ...
}
- Go DocumentDB Example - A users CRUD application using Martini and DocumentDB
MIT