|
5 | 5 | NOTE: THIS IS WORK IN PROGRESS. |
6 | 6 | API and implementation WILL change. |
7 | 7 |
|
| 8 | +This project contains a Go driver for the [ArangoDB database](https://arangodb.com). |
| 9 | + |
| 10 | +## Supported ArangoDB versions |
| 11 | + |
| 12 | +- ArangoDB versions 3.1 and up. |
| 13 | +- Single server setups |
| 14 | +- Server cluster setups. |
| 15 | + |
| 16 | +## Supported Go versions |
| 17 | + |
| 18 | +- Go 1.7 and up. |
| 19 | + |
| 20 | +## Go dependencies |
| 21 | + |
| 22 | +- None |
| 23 | +- Additional error libraries are supported. |
| 24 | + |
| 25 | +## Getting started |
| 26 | + |
| 27 | +Using the driver, you always need to create a `Client`. |
| 28 | +The following example shows how to create a `Client` for a single server |
| 29 | +running on localhost. |
| 30 | + |
| 31 | +``` |
| 32 | +import ( |
| 33 | + "fmt" |
| 34 | +
|
| 35 | + driver "github.com/arangodb/go-driver" |
| 36 | + "github.com/arangodb/go-driver/http" |
| 37 | +) |
| 38 | +
|
| 39 | +... |
| 40 | +
|
| 41 | +conn, err := http.NewConnection(http.ConnectionConfig{ |
| 42 | + Endpoints: []string{"http://localhost:8529"}, |
| 43 | +}) |
| 44 | +if err != nil { |
| 45 | + // Handle error |
| 46 | +} |
| 47 | +c, err := driver.NewClient(driver.ClientConfig{ |
| 48 | + Connection: conn, |
| 49 | +}) |
| 50 | +if err != nil { |
| 51 | + // Handle error |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +Once you have a `Client` you can access/create databases on the server, |
| 56 | +access/create collections, graphs, documents and so on. |
| 57 | + |
| 58 | +The following example shows how to open an existing collection in an existing database |
| 59 | +and create a new document in that collection. |
| 60 | + |
| 61 | +``` |
| 62 | +// Open "examples_books" database |
| 63 | +db, err := c.Database(nil, "examples_books") |
| 64 | +if err != nil { |
| 65 | + // Handle error |
| 66 | +} |
| 67 | +
|
| 68 | +// Open "books" collection |
| 69 | +col, err := db.Collection(nil, "books", nil) |
| 70 | +if err != nil { |
| 71 | + // Handle error |
| 72 | +} |
| 73 | +
|
| 74 | +// Create document |
| 75 | +book := Book{ |
| 76 | + Title: "ArangoDB Cookbook", |
| 77 | + NoPages: 257, |
| 78 | +} |
| 79 | +meta, err := col.CreateDocument(nil, book) |
| 80 | +if err != nil { |
| 81 | + // Handle error |
| 82 | +} |
| 83 | +fmt.Printf("Created document in collection '%s' in database '%s'\n", col.Name(), db.Name()) |
| 84 | +``` |
| 85 | + |
| 86 | +## API design |
| 87 | + |
| 88 | +### Concurrency |
| 89 | + |
| 90 | +All functions of the driver are stricly synchronous. They operate and only return a value (or error) |
| 91 | +when they're done. |
| 92 | + |
| 93 | +If you want to run operations concurrently, use a go routine. All objects in the driver are designed |
| 94 | +to be used from multiple concurrent go routines, except `Cursor`. |
| 95 | + |
| 96 | +All database objects (except `Cursor`) are considered static. After their creation they won't change. |
| 97 | +E.g. after creating a `Collection` instance you can remove the collection, but the (Go) instance |
| 98 | +will still be there. Calling functions on such a removed collection will of course fail. |
| 99 | + |
| 100 | +### Structured error handling & wrapping |
| 101 | + |
| 102 | +All functions of the driver that can fail return an `error` value. If that value is not `nil`, the |
| 103 | +function call is considered to be failed. In that case all other return values are set to their `zero` |
| 104 | +values. |
| 105 | + |
| 106 | +All errors are structured using error checking functions named `Is<SomeErrorCategory>`. |
| 107 | +E.g. `IsNotFound(error)` return true if the given error is of the category "not found". |
| 108 | +There can be multiple internal error codes that all map onto the same category. |
| 109 | + |
| 110 | +All errors returned from any function of the driver (either internal or exposed) wrap errors |
| 111 | +using the `WithStack` function. This can be used to provide detail stack trackes in case of an error. |
| 112 | +All error checking functions use the `Cause` function to get the cause of an error instead of the error wrapper. |
| 113 | + |
| 114 | +Note that `WithStack` and `Cause` are actually variables to you can implement it using your own error |
| 115 | +wrapper library. |
| 116 | + |
| 117 | +If you for example use https://github.com/pkg/errors, you want to initialize to go driver like this: |
| 118 | +``` |
| 119 | +import ( |
| 120 | + driver "github.com/arangodb/go-driver" |
| 121 | + "github.com/pkg/errors" |
| 122 | +) |
| 123 | +
|
| 124 | +func init() { |
| 125 | + driver.WithStack = errors.WithStack |
| 126 | + driver.Cause = errors.Cause |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +### Context aware |
| 131 | + |
| 132 | +All functions of the driver that involve some kind of long running operation or |
| 133 | +support additional options not given as function arguments, have a `context.Context` argument. |
| 134 | +This enables you cancel running requests, pass timeouts/deadlines and pass additional options. |
| 135 | + |
| 136 | +In all methods that take a `context.Context` argument you can pass `nil` as value. |
| 137 | +This is equivalent to passing `context.Background()`. |
| 138 | + |
| 139 | +Many functions support 1 or more optional (and infrequently used) additional options. |
| 140 | +These can be used with a `With<OptionName>` function. |
| 141 | +E.g. to force a create document call to wait until the data is synchronized to disk, |
| 142 | +use a prepared context like this: |
| 143 | +``` |
| 144 | +ctx := driver.WithWaitForSync(parentContext) |
| 145 | +collection.CreateDocument(ctx, yourDocument) |
| 146 | +``` |
| 147 | + |
8 | 148 | # Sample requests |
9 | 149 |
|
10 | 150 | ## Connecting to ArangoDB |
|
0 commit comments