• Stars
    star
    22
  • Rank 1,048,934 (Top 21 %)
  • Language
    Go
  • License
    MIT License
  • Created almost 4 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Go database/sql driver for Azure Cosmos DB SQL API

gocosmos

Go Report Card PkgGoDev Actions Status codecov Release Mentioned in Awesome Go

Go driver for Azure Cosmos DB SQL API which can be used with the standard database/sql package. A REST client is also included.

database/sql driver

Summary of supported SQL statements:

Statement Syntax
Create a new database CREATE DATABASE [IF NOT EXISTS] <db-name>
Change database's throughput ALTER DATABASE <db-name> WITH RU/MAXRU=<ru>
Delete an existing database DROP DATABASE [IF EXISTS] <db-name>
List all existing databases LIST DATABASES
Create a new collection CREATE COLLECTION [IF NOT EXISTS] [<db-name>.]<collection-name> <WITH PK=partitionKey>
Change collection's throughput ALTER COLLECTION [<db-name>.]<collection-name> WITH RU/MAXRU=<ru>
Delete an existing collection DROP COLLECTION [IF EXISTS] [<db-name>.]<collection-name>
List all existing collections in a database LIST COLLECTIONS [FROM <db-name>]
Insert a new document into collection INSERT INTO [<db-name>.]<collection-name> ...
Insert or replace a document UPSERT INTO [<db-name>.]<collection-name> ...
Delete an existing document DELETE FROM [<db-name>.]<collection-name> WHERE id=<id-value>
Update an existing document UPDATE [<db-name>.]<collection-name> SET ... WHERE id=<id-value>
Query documents in a collection SELECT [CROSS PARTITION] ... FROM <collection-name> ... [WITH database=<db-name>]

See supported SQL statements for details.

Azure Cosmos DB SQL API currently supports only SELECT statement. gocosmos implements other statements by translating the SQL statement to REST API calls.

Example usage:

package main

import (
	"database/sql"
	_ "github.com/btnguyen2k/gocosmos"
)

func main() {
	driver := "gocosmos"
	dsn := "AccountEndpoint=https://localhost:8081/;AccountKey=<cosmosdb-account-key>"
	db, err := sql.Open(driver, dsn)
	if err != nil {
		panic(err)
	}
	defer db.Close()
	
	_, err = db.Exec("CREATE DATABASE mydb WITH maxru=10000")
	if err != nil {
		panic(err)
	}
	
	// database "mydb" has been created successfuly
}

Data Source Name (DSN) syntax for Cosmos DB

Note: line-break is for readability only!

AccountEndpoint=<cosmosdb-endpoint>
;AccountKey=<cosmosdb-account-key>
[;TimeoutMs=<timeout-in-ms>]
[;Version=<cosmosdb-api-version>]
[;DefaultDb|Db=<db-name>]
[;AutoId=<true/false>]
[;InsecureSkipVerify=<true/false>]
  • AccountEndpoint: (required) endpoint to access Cosmos DB. For example, the endpoint for Azure Cosmos DB Emulator running on local is https://localhost:8081/.
  • AccountKey: (required) account key to authenticate.
  • TimeoutMs: (optional) operation timeout in milliseconds. Default value is 10 seconds if not specified.
  • Version: (optional) version of Cosmos DB to use. Default value is 2020-07-15 if not specified. See: https://learn.microsoft.com/rest/api/cosmos-db/#supported-rest-api-versions.
  • DefaultDb: (optional, available since v0.1.1) specify the default database used in Cosmos DB operations. Alias Db can also be used instead of DefaultDb.
  • AutoId: (optional, available since v0.1.2) see auto id session.
  • InsecureSkipVerify: (optional, available since v0.1.4) if true, disable CA verification for https endpoint (useful to run against test/dev env with local/docker Cosmos DB emulator).

Auto-id

Azure Cosmos DB requires each document has a unique ID that identifies the document. When creating new document, if value for the unique ID field is not supplied gocosmos is able to generate one automatically. This feature is enabled by specifying setting AutoId=true in the Data Source Name (for database/sql driver) or the connection string (for REST client). If not specified, default value is AutoId=true.

This setting is available since v0.1.2.

Known issues

GROUP BY combined with ORDER BY is not supported

Azure Cosmos DB does not support GROUP BY combined with ORDER BY yet. You will receive the following error message:

'ORDER BY' is not supported in presence of GROUP BY.

Cross-partition paging

Cross-partition paging can be done with the OFFSET...LIMIT clause. However, the query is not stable without ORDER BY. The returned results may not be consistent from query to query.

Queries that may consume a large amount of memory

These queries may consume a large amount of memory if executed against a large table:

  • OFFSET...LIMIT clause with big offset or limit values.
  • SELECT DISTINCT and SELECT DISTINCT VALUE queries.
  • Queries with GROUP BY clause.

REST client

See the REST.md file for details.

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

Support and Contribution

Disclaimer: I am also a core maintainer of microsoft/gocosmos. Features and bug fixes are synchronized between the two projects.

This project uses GitHub Issues to track bugs and feature requests. Please search the existing issues before filing new issues to avoid duplicates. For new issues, file your bug or feature request as a new Issue.

Please create pull requests in the microsoft/gocosmos repository.

If you find this project useful, please start it.

More Repositories

1

swift-rsautils

RSA Utility Functions for Swift
Swift
116
star
2

swiftutils

Utility library in Swift
Swift
72
star
3

goadmin.g8

Giter8 template to build Admin Control Panel for Go.
JavaScript
72
star
4

govueadmin.g8

AdminCP Giter8 template for GoLang with VueJS-based frontend
Vue
40
star
5

go-module-template

Template to quickly spin up a Go module project
Go
39
star
6

dotnet-webapi-template

Template to quickly spin up .NET Web API projects
C#
27
star
7

docms

Content Management System where its content is built through CI/CD pipeline
CSS
19
star
8

goyai

Yet Another I18n package for Go
Go
18
star
9

exter

An identity gateway that allows application to authenticate users using various identity sources.
Go
17
star
10

consu

Commons utilities and tools in Golang
Go
14
star
11

codeql-container

GitHub CodeQL packaged and precompiled in a container.
Shell
12
star
12

godal

Generic Database Access Layer implementation in Golang.
Go
11
star
13

azp-k8s-agents

Azure Pipelines agents as containers on Kubernetes cluster
Shell
8
star
14

prista

A log collector service
Go
7
star
15

prom

Utility library to manage shared connection in Golang
Go
7
star
16

goapi.g8

Go template for microservices using Echo framework.
Go
7
star
17

olaf

Twitter Snowflake implemented in Go
Go
6
star
18

godynamo

Go database/sql driver for AWS DynamoDB
Go
4
star
19

id-server

Server to generate IDs
Java
4
star
20

action-semrelease

GitHub Action to publish releases using tags, following sematic versioning
JavaScript
4
star
21

queue-server

Queue service that unifies various queue backends into a single API set.
Java
4
star
22

WebTemplates

Free web template collection
CSS
4
star
23

go-giter8-sample.g8

Sample template to demonstrate https://github.com/btnguyen2k/go-giter8
HTML
3
star
24

txapi

Build your own Transformers API server
Python
2
star
25

npm-package-template

Template to quickly spin up a NPM package project
JavaScript
2
star
26

akka-scheduledjob-seed.g8

Java template - Scheduled Jobs using Akka: use "sbt new btnguyen2k/akka-scheduledjob-seed.g8"
Java
2
star
27

henge

NoSQL-style universal data access layer lib for Go (Golang).
Go
1
star
28

btnguyen2k

Capturing my learning adventure as I explore the world of tech and programming.
1
star
29

mpn

My Personal Notes
Dockerfile
1
star
30

js-mdr

Render Markdown to HTML with rich extensions.
JavaScript
1
star
31

oaiaux

OpenAI helper
Go
1
star
32

llm_fetch_data

Python
1
star
33

js-checksum

Javascript module to calculate checksum from any value.
JavaScript
1
star
34

v-ushop

Automatically exported from code.google.com/p/v-ushop
PHP
1
star
35

plurimos

Many-to-one mapping service
Go
1
star
36

gc-stop-of-the-world

How does GC affect Java application?
Shell
1
star
37

docker-scribe-ubuntu

Dockerfile to build a Facebook Scribe server (based on Ubuntu)
Shell
1
star
38

id-jclient

Java client for id-server (https://github.com/btnguyen2k/id-server)
Java
1
star
39

png

Push Notification Gateway (currently support only iOS)
JavaScript
1
star
40

singu

A message queue library implemented in Golang.
Go
1
star
41

microservices-undertow-seed.g8

Java template for microservices using Undertow framework: use "sbt new btnguyen2k/microservices-undertow-seed.g8"
Java
1
star
42

mpps

Memcached Protocol Proxy Server
Java
1
star