Skip to content

Commit

Permalink
[Javascrip][Go] Example with dotenv for Aura.
Browse files Browse the repository at this point in the history
  • Loading branch information
stefano-ottolenghi committed Nov 6, 2023
1 parent 13442a2 commit 396eb4d
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 5 deletions.
48 changes: 48 additions & 0 deletions go-manual/modules/ROOT/pages/connect.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package main
import (
"context"
"fmt"
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
)
Expand All @@ -32,6 +33,7 @@ func main() {
if err != nil {
panic(err)
}
fmt.Println("Connection established.")
}
----

Expand All @@ -48,6 +50,52 @@ If you need to query the database with a different user than the one you created
If you want to alter a `DriverWithContext` configuration, you will need to create a new object.


== Connect to an Aura instance

When you create an <<Aura>> instance, you may download a text file containing the connection information to the database.
The file has a name of the form `Neo4j-a0a2fa1d-Created-2023-11-06.txt`.

To connect to such an instance, you may either use the URI, username, and password explicitly in your application, or load the content of the connection file in the environment with `godotenv.Load()` and populate your local variables via `os.Getenv()`.
This approach requires the package link:https://pkg.go.dev/github.com/joho/godotenv[`godotenv`].

[source, go, role=test-skip]
----
package main
import (
"context"
"os"
"fmt"
"github.com/joho/godotenv"
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
)
func main() {
ctx := context.Background()
err := godotenv.Load("Neo4j-a0a2fa1d-Created-2023-11-06.txt")
if err != nil {
panic(err)
}
dbUri := os.Getenv("NEO4J_URI")
dbUser := os.Getenv("NEO4J_USERNAME")
dbPassword := os.Getenv("NEO4J_PASSWORD")
driver, err := neo4j.NewDriverWithContext(
dbUri,
neo4j.BasicAuth(dbUser, dbPassword, ""))
if err != nil {
panic(err)
}
defer driver.Close(ctx)
err = driver.VerifyConnectivity(ctx)
if err != nil {
panic(err)
}
fmt.Println("Connection established.")
}
----


== Further connection parameters

For more `DriverWithContext` configuration parameters and further connection settings, see xref:connect-advanced.adoc[Advanced connection information].
Expand Down
40 changes: 40 additions & 0 deletions javascript-manual/modules/ROOT/pages/connect.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,46 @@ Share them across threads (but not across processes) and use xref:transactions#i
If you want to alter a `Driver` configuration, you will need to create a new object.


== Connect to an Aura instance

When you create an <<Aura>> instance, you may download a text file containing the connection information to the database.
The file has a name of the form `Neo4j-a0a2fa1d-Created-2023-11-06.txt`.

To connect to such an instance, you may either use the URI, username, and password explicitly in your application, or load the content of the connection file in the environment with `dotenv.config()` and populate your local variables via `process.env.<NAME>`.
This approach requires the package link:https://www.npmjs.com/package/dotenv[`dotenv`].

[source, javascript, role=nocollapse test-skip]
----
(async () => {
var neo4j = require('neo4j-driver')
require('dotenv').config({
path: 'Neo4j-a0a2fa1d-Created-2023-11-06.txt',
debug: true // to raise file/parsing errors
})
const URI = process.env.NEO4J_URI
const USER = process.env.NEO4J_USERNAME
const PASSWORD = process.env.NEO4J_PASSWORD
let driver
try {
driver = neo4j.driver(URI, neo4j.auth.basic(USER, PASSWORD))
const serverInfo = await driver.getServerInfo()
console.log('Connection estabilished')
console.log(serverInfo)
} catch(err) {
console.log(`Connection error\n${err}\nCause: ${err.cause}`)
await driver.close()
return
}
// Use the driver to run queries
await driver.close()
})();
----


== Further connection parameters

For more `Driver` configuration parameters and further connection settings, see xref:connect-advanced.adoc[Advanced connection information].
Expand Down
10 changes: 5 additions & 5 deletions python-manual/modules/ROOT/pages/connect.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Once you have xref:install.adoc#install-driver[installed the driver] and have xr

You connect to a database by creating a <<Driver>> object and providing a URL and an authentication token.

[source,python]
[source, python]
----
from neo4j import GraphDatabase
Expand Down Expand Up @@ -36,16 +36,16 @@ If you want to alter a `Driver` configuration, you will need to create a new obj
When you create an <<Aura>> instance, you may download a text file containing the connection information to the database.
The file has a name of the form `Neo4j-a0a2fa1d-Created-2023-11-06.txt`.

To connect to such an instance, you may either use the URI, username, and password explicitly in your application, or load the content of the connection file in the environment with `dotenv.load_dotenv()` and populate your local variables with `os.getenv()`.
This requires the package link:https://pypi.org/project/python-dotenv/[`python-dotenv`] to be installed.
To connect to such an instance, you may either use the URI, username, and password explicitly in your application, or load the content of the connection file in the environment with `dotenv.load_dotenv()` and populate your local variables via `os.getenv()`.
This approach requires the package link:https://pypi.org/project/python-dotenv/[`python-dotenv`].

[source,python,role=test-skip]
[source, python, role=test-skip]
----
import dotenv
import os
from neo4j import GraphDatabase
dotenv.load_dotenv('Neo4j-a0a2fa1d-Created-2023-11-06.txt')
dotenv.load_dotenv("Neo4j-a0a2fa1d-Created-2023-11-06.txt")
URI = os.getenv("NEO4J_URI")
AUTH = (os.getenv("NEO4J_USERNAME"), os.getenv("NEO4J_PASSWORD"))
Expand Down

0 comments on commit 396eb4d

Please sign in to comment.