Skip to content
This repository has been archived by the owner on Jan 18, 2025. It is now read-only.

Commit

Permalink
Add database Dial example
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestas-poskus committed Dec 27, 2015
1 parent 1e7ae8b commit 5f43949
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ install:
- cd $GOPATH/src/gopkg.in/istreamdata/orientgo.v2
- docker pull dennwc/orientdb:${ORIENT_VERS}
- go get golang.org/x/tools/cmd/vet
- go github.com/istreamdata/orientgo/obinary

# - go get golang.org/x/tools/cmd/cover
# - go get github.com/golang/lint/golint
- go get -t -v ./...
Expand Down
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,56 @@ You are welcome to initiate pull request and suggest a more user-friendly API. W

3) `go test -v ./...`

## Examples

### Dial

```go
// Dial example
func main() {
defaultDbAddr := "127.0.0.1:2424"
testDbName := "test_db_example"
testDbUser := "root"
testDbPass := "root"

client, err := orient.Dial(defaultDbAddr)
if err != nil {
HandleError(err)
}

admin, err := client.Auth(testDbUser, testDbPass)
if err != nil {
HandleError(err)
}

// There are 2 options
// 1. orient.Persistent - represents on-disk database
// 2. orient.Volatile - represents in-memory database
ok, err := admin.DatabaseExists(testDbName, orient.Persistent)
if err != nil {
HandleError(err)
}

// If database does not exist let's create it
if !ok {
// There are 2 options
// 1. orient.GraphDB - graph database
// 2. orient.DocumentDB - document database
err = admin.CreateDatabase(testDbName, orient.GraphDB, orient.Persistent)
if err != nil {
HandleError(err)
}
}

// Open wanted database & operate further
database, err := client.Open(testDbName, orient.GraphDB, testDbUser, testDbPass)
if err != nil {
HandleError(err)
}
defer database.Close()
}
```

## LICENSE

The MIT License
46 changes: 46 additions & 0 deletions dial_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package test

import (
"testing"

_ "gopkg.in/istreamdata/orientgo.v2/obinary"
)

// Dial example
func TestDial(t *testing.T) {
client, err := orient.Dial("127.0.0.1:2424")
if err != nil {
t.Fatal(err)
}

admin, err := client.Auth(dbUser, dbPass)
if err != nil {
t.Fatal(err)
}

// There are 2 options
// 1. orient.Persistent - represents on-disk database
// 2. orient.Volatile - represents in-memory database
ok, err := admin.DatabaseExists(dbName, orient.Persistent)
if err != nil {
t.Fatal(err)
}

// If database does not exist let's create it
if !ok {
// There are 2 options
// 1. orient.GraphDB - graph database
// 2. orient.DocumentDB - document database
err = admin.CreateDatabase(dbName, orient.GraphDB, orient.Persistent)
if err != nil {
t.Fatal(err)
}
}

// Open wanted database & operate further
database, err := client.Open(dbName, orient.GraphDB, dbUser, dbPass)
if err != nil {
t.Fatal(err)
}
defer database.Close()
}

0 comments on commit 5f43949

Please sign in to comment.