{"id":16939449,"url":"https://github.com/a8m/documentdb","last_synced_at":"2025-03-22T12:32:16.500Z","repository":{"id":28714868,"uuid":"32235721","full_name":"a8m/documentdb","owner":"a8m","description":"Go driver for Microsoft Azure DocumentDB","archived":false,"fork":false,"pushed_at":"2022-04-05T20:52:23.000Z","size":100,"stargazers_count":31,"open_issues_count":6,"forks_count":25,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-03-18T11:11:31.139Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"lightningnetwork/lnd","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/a8m.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-03-14T22:47:21.000Z","updated_at":"2025-02-28T22:39:09.000Z","dependencies_parsed_at":"2022-08-21T16:20:43.602Z","dependency_job_id":null,"html_url":"https://github.com/a8m/documentdb","commit_stats":null,"previous_names":["a8m/documentdb-go"],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/a8m%2Fdocumentdb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/a8m%2Fdocumentdb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/a8m%2Fdocumentdb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/a8m%2Fdocumentdb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/a8m","download_url":"https://codeload.github.com/a8m/documentdb/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244959444,"owners_count":20538625,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-13T21:04:47.088Z","updated_at":"2025-03-22T12:32:15.971Z","avatar_url":"https://github.com/a8m.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"## DocumentDB Go [![Build status][travis-image]][travis-url]\n\n\u003e Go driver for Microsoft Azure DocumentDB\n\n## Table of contents:\n\n* [Get Started](#get-started)\n* [Examples](#examples)\n* [Databases](#databases)\n  * [Get](#readdatabase)\n  * [Query](#querydatabases)\n  * [List](#readdatabases)\n  * [Create](#createdatabase)\n  * [Replace](#replacedatabase)\n  * [Delete](#deletedatabase)\n* [Collections](#collections)\n  * [Get](#readcollection)\n  * [Query](#querycollections)\n  * [List](#readcollection)\n  * [Create](#createcollection)\n  * [Delete](#deletecollection)\n* [Documents](#documents)\n  * [Get](#readdocument)\n  * [Query](#querydocuments)\n  * [List](#readdocuments)\n  * [Create](#createdocument)\n  * [Replace](#replacedocument)\n  * [Delete](#deletedocument)\n* [StoredProcedures](#storedprocedures)\n  * [Get](#readstoredprocedure)\n  * [Query](#querystoredprocedures)\n  * [List](#readstoredprocedures)\n  * [Create](#createstoredprocedure)\n  * [Replace](#replacestoredprocedure)\n  * [Delete](#deletestoredprocedure)\n  * [Execute](#executestoredprocedure)\n* [UserDefinedFunctions](#userdefinedfunctions)\n  * [Get](#readuserdefinedfunction)\n  * [Query](#queryuserdefinedfunctions)\n  * [List](#readuserdefinedfunctions)\n  * [Create](#createuserdefinedfunction)\n  * [Replace](#replaceuserdefinedfunction)\n  * [Delete](#deleteuserdefinedfunction)\n* [Iterator](#iterator)\n  * [DocumentIterator](#documentIterator)\n* [Authentication with Azure AD](#authenticationwithazuread)\n\n### Get Started\n\n#### Installation\n\n```sh\n$ go get github.com/a8m/documentdb\n```\n\n#### Add to your project\n\n```go\nimport (\n\t\"github.com/a8m/documentdb\"\n)\n\nfunc main() {\n\tconfig := documentdb.NewConfig(\u0026documentdb.Key{\n\t\tKey: \"master-key\",\n\t})\n\tclient := documentdb.New(\"connection-url\", config)\n\n\t// Start using DocumentDB\n\tdbs, err := client.ReadDatabases()\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tfmt.Println(dbs)\n}\n```\n\n### Databases\n\n#### ReadDatabase\n\n```go\nfunc main() {\n\t// ...\n\tdb, err := client.ReadDatabase(\"self_link\")\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tfmt.Println(db.Self, db.Id)\n}\n```\n\n#### QueryDatabases\n\n```go\nfunc main() {\n\t// ...\n\tdbs, err := client.QueryDatabases(\"SELECT * FROM ROOT r\")\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tfor _, db := range dbs {\n\t\tfmt.Println(\"DB Name:\", db.Id)\n\t}\n}\n```\n\n#### ReadDatabases\n\n```go\nfunc main() {\n\t// ...\n\tdbs, err := client.ReadDatabases()\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tfor _, db := range dbs {\n\t\tfmt.Println(\"DB Name:\", db.Id)\n\t}\n}\n```\n\n#### CreateDatabase\n\n```go\nfunc main() {\n\t// ...\n\tdb, err := client.CreateDatabase(`{ \"id\": \"test\" }`)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tfmt.Println(db)\n\n\t// or ...\n\tvar db documentdb.Database\n\tdb.Id = \"test\"\n\tdb, err = client.CreateDatabase(\u0026db)\n}\n```\n\n#### ReplaceDatabase\n\n```go\nfunc main() {\n\t// ...\n\tdb, err := client.ReplaceDatabase(\"self_link\", `{ \"id\": \"test\" }`)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tfmt.Println(db)\n\n\t// or ...\n\tvar db documentdb.Database\n\tdb, err = client.ReplaceDatabase(\"self_link\", \u0026db)\n}\n```\n\n#### DeleteDatabase\n\n```go\nfunc main() {\n\t// ...\n\terr := client.DeleteDatabase(\"self_link\")\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n}\n```\n\n### Collections\n\n#### ReadCollection\n\n```go\nfunc main() {\n\t// ...\n\tcoll, err := client.ReadCollection(\"self_link\")\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tfmt.Println(coll.Self, coll.Id)\n}\n```\n\n#### QueryCollections\n\n```go\nfunc main() {\n\t// ...\n\tcolls, err := client.QueryCollections(\"db_self_link\", \"SELECT * FROM ROOT r\")\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tfor _, coll := range colls {\n\t\tfmt.Println(\"Collection Name:\", coll.Id)\n\t}\n}\n```\n\n#### ReadCollections\n\n```go\nfunc main() {\n\t// ...\n\tcolls, err := client.ReadCollections(\"db_self_link\")\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tfor _, coll := range colls {\n\t\tfmt.Println(\"Collection Name:\", coll.Id)\n\t}\n}\n```\n\n#### CreateCollection\n\n```go\nfunc main() {\n\t// ...\n\tcoll, err := client.CreateCollection(\"db_self_link\", `{\"id\": \"my_test\"}`)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tfmt.Println(\"Collection Name:\", coll.Id)\n\n\t// or ...\n\tvar coll documentdb.Collection\n\tcoll.Id = \"test\"\n\tcoll, err = client.CreateCollection(\"db_self_link\", \u0026coll)\n}\n```\n\n#### DeleteCollection\n\n```go\nfunc main() {\n\t// ...\n\terr := client.DeleteCollection(\"self_link\")\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n}\n```\n\n### Documents\n\n#### ReadDocument\n\n```go\ntype Document struct {\n\tdocumentdb.Document\n\t// Your external fields\n\tName    string `json:\"name,omitempty\"`\n\tEmail   string `json:\"email,omitempty\"`\n}\n\nfunc main() {\n\t// ...\n\tvar doc Document\n\terr = client.ReadDocument(\"self_link\", \u0026doc)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tfmt.Println(\"Document Name:\", doc.Name)\n}\n```\n\n#### QueryDocuments\n\n```go\ntype User struct {\n\tdocumentdb.Document\n\t// Your external fields\n\tName    string `json:\"name,omitempty\"`\n\tEmail   string `json:\"email,omitempty\"`\n}\n\nfunc main() {\n\t// ...\n\tvar users []User\n\t_, err = client.QueryDocuments(\n        \"coll_self_link\", \n        documentdb.NewQuery(\"SELECT * FROM ROOT r WHERE r.name=@name\", documentdb.P{\"@name\", \"john\"}),\n        \u0026users,\n    )\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tfor _, user := range users {\n\t\tfmt.Print(\"Name:\", user.Name, \"Email:\", user.Email)\n\t}\n}\n```\n\n#### QueryDocuments with partition key\n\n```go\ntype User struct {\n\tdocumentdb.Document\n\t// Your external fields\n\tName    string `json:\"name,omitempty\"`\n\tEmail   string `json:\"email,omitempty\"`\n}\n\nfunc main() {\n\t// ...\n\tvar users []User\n\t_, err = client.QueryDocuments(\n        \"coll_self_link\", \n\t\tdocumentdb.NewQuery(\n\t\t\t\"SELECT * FROM ROOT r WHERE r.name=@name AND r.company_id = @company_id\", \n\t\t\tdocumentdb.P{\"@name\", \"john\"}, \n\t\t\tdocumentdb.P{\"@company_id\", \"1234\"},\n\t\t),\n\t\t\u0026users,\n\t\tdocumentdb.PartitionKey(\"1234\")\n    )\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tfor _, user := range users {\n\t\tfmt.Print(\"Name:\", user.Name, \"Email:\", user.Email)\n\t}\n}\n```\n\n#### ReadDocuments\n\n```go\ntype User struct {\n\tdocumentdb.Document\n\t// Your external fields\n\tName    string `json:\"name,omitempty\"`\n\tEmail   string `json:\"email,omitempty\"`\n}\n\nfunc main() {\n\t// ...\n\tvar users []User\n\terr = client.ReadDocuments(\"coll_self_link\", \u0026users)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tfor _, user := range users {\n\t\tfmt.Print(\"Name:\", user.Name, \"Email:\", user.Email)\n\t}\n}\n```\n\n#### CreateDocument\n\n```go\ntype User struct {\n\tdocumentdb.Document\n\t// Your external fields\n\tName    string `json:\"name,omitempty\"`\n\tEmail   string `json:\"email,omitempty\"`\n}\n\nfunc main() {\n\t// ...\n\tvar user User\n\t// Note: If the `id` is missing(or empty) in the payload it will generate\n\t// random document id(i.e: uuid4)\n\tuser.Id = \"uuid\"\n\tuser.Name = \"Ariel\"\n\tuser.Email = \"ariel@test.com\"\n\terr := client.CreateDocument(\"coll_self_link\", \u0026doc)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tfmt.Print(\"Name:\", user.Name, \"Email:\", user.Email)\n}\n```\n\n#### ReplaceDocument\n\n```go\ntype User struct {\n\tdocumentdb.Document\n\t// Your external fields\n\tIsAdmin bool   `json:\"isAdmin,omitempty\"`\n}\n\nfunc main() {\n\t// ...\n\tvar user User\n\tuser.Id = \"uuid\"\n\tuser.IsAdmin = false\n\terr := client.ReplaceDocument(\"doc_self_link\", \u0026user)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tfmt.Print(\"Is Admin:\", user.IsAdmin)\n}\n```\n\n#### DeleteDocument\n\n```go\nfunc main() {\n\t// ...\n\terr := client.DeleteDocument(\"doc_self_link\")\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n}\n```\n\n###\n\n#### ExecuteStoredProcedure\n\n```go\nfunc main() {\n\t// ...\n\tvar docs []Document\n\terr := client.ExecuteStoredProcedure(\"sporc_self\", [...]interface{}{p1, p2}, \u0026docs)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\t// ...\n}\n```\n\n### Iterator\n\n#### DocumentIterator\n\n```go\nfunc main() {\n\t// ...\n\tvar docs []Document\n\n\titerator := documentdb.NewIterator(\n\t\tclient, documentdb.NewDocumentIterator(\"coll_self_link\", nil, \u0026docs, documentdb.PartitionKey(\"1\"), documentdb.Limit(1)),\n\t)\n\n\tfor iterator.Next() {\n\t\tif err := iterator.Error(); err != nil {\n\t\t\tlog.Fatal(err)\n\t\t}\n\t\tfmt.Println(len(docs))\n\t}    \n\n\t// ...\n}\n```\n\n### Authentication with Azure AD\n\nYou can authenticate with Cosmos DB using Azure AD and a service principal, including full RBAC support. To configure Cosmos DB to use Azure AD, take a look at the [Cosmos DB documentation](https://docs.microsoft.com/en-us/azure/cosmos-db/how-to-setup-rbac).\n\nTo use this library with a service principal:\n\n```go\nimport (\n\t\"github.com/Azure/go-autorest/autorest/adal\"\n\t\"github.com/a8m/documentdb\"\n)\n\nfunc main() {\n\t// Azure AD application (service principal) client credentials\n\ttenantId := \"tenant-id\"\n\tclientId := \"client-id\"\n\tclientSecret := \"client-secret\"\n\n\t// Azure AD endpoint may be different for sovereign clouds\n\toauthConfig, err := adal.NewOAuthConfig(\"https://login.microsoftonline.com/\", tenantId)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tspt, err := adal.NewServicePrincipalToken(*oauthConfig, clientId, clientSecret, \"https://cosmos.azure.com\") // Always \"https://cosmos.azure.com\"\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\tconfig := documentdb.NewConfigWithServicePrincipal(spt)\n\tclient := documentdb.New(\"connection-url\", config)\n}\n```\n\n### Examples\n\n* [Go DocumentDB Example](https://github.com/a8m/go-documentdb-example) - A users CRUD application using Martini and DocumentDB\n\n### License\n\nDistributed under the MIT license, which is available in the file LICENSE.\n\n[travis-image]: https://img.shields.io/travis/a8m/documentdb.svg?style=flat-square\n[travis-url]: https://travis-ci.org/a8m/documentdb\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fa8m%2Fdocumentdb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fa8m%2Fdocumentdb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fa8m%2Fdocumentdb/lists"}