Skip to content

Commit 396eb4d

Browse files
[Javascrip][Go] Example with dotenv for Aura.
1 parent 13442a2 commit 396eb4d

File tree

3 files changed

+93
-5
lines changed

3 files changed

+93
-5
lines changed

go-manual/modules/ROOT/pages/connect.adoc

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ package main
1212
1313
import (
1414
"context"
15+
"fmt"
1516
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
1617
)
1718
@@ -32,6 +33,7 @@ func main() {
3233
if err != nil {
3334
panic(err)
3435
}
36+
fmt.Println("Connection established.")
3537
}
3638
----
3739

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

5052

53+
== Connect to an Aura instance
54+
55+
When you create an <<Aura>> instance, you may download a text file containing the connection information to the database.
56+
The file has a name of the form `Neo4j-a0a2fa1d-Created-2023-11-06.txt`.
57+
58+
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()`.
59+
This approach requires the package link:https://pkg.go.dev/github.com/joho/godotenv[`godotenv`].
60+
61+
[source, go, role=test-skip]
62+
----
63+
package main
64+
65+
import (
66+
"context"
67+
"os"
68+
"fmt"
69+
"github.com/joho/godotenv"
70+
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
71+
)
72+
73+
func main() {
74+
ctx := context.Background()
75+
err := godotenv.Load("Neo4j-a0a2fa1d-Created-2023-11-06.txt")
76+
if err != nil {
77+
panic(err)
78+
}
79+
dbUri := os.Getenv("NEO4J_URI")
80+
dbUser := os.Getenv("NEO4J_USERNAME")
81+
dbPassword := os.Getenv("NEO4J_PASSWORD")
82+
driver, err := neo4j.NewDriverWithContext(
83+
dbUri,
84+
neo4j.BasicAuth(dbUser, dbPassword, ""))
85+
if err != nil {
86+
panic(err)
87+
}
88+
defer driver.Close(ctx)
89+
90+
err = driver.VerifyConnectivity(ctx)
91+
if err != nil {
92+
panic(err)
93+
}
94+
fmt.Println("Connection established.")
95+
}
96+
----
97+
98+
5199
== Further connection parameters
52100

53101
For more `DriverWithContext` configuration parameters and further connection settings, see xref:connect-advanced.adoc[Advanced connection information].

javascript-manual/modules/ROOT/pages/connect.adoc

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,46 @@ Share them across threads (but not across processes) and use xref:transactions#i
4949
If you want to alter a `Driver` configuration, you will need to create a new object.
5050

5151

52+
== Connect to an Aura instance
53+
54+
When you create an <<Aura>> instance, you may download a text file containing the connection information to the database.
55+
The file has a name of the form `Neo4j-a0a2fa1d-Created-2023-11-06.txt`.
56+
57+
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>`.
58+
This approach requires the package link:https://www.npmjs.com/package/dotenv[`dotenv`].
59+
60+
[source, javascript, role=nocollapse test-skip]
61+
----
62+
(async () => {
63+
var neo4j = require('neo4j-driver')
64+
require('dotenv').config({
65+
path: 'Neo4j-a0a2fa1d-Created-2023-11-06.txt',
66+
debug: true // to raise file/parsing errors
67+
})
68+
69+
const URI = process.env.NEO4J_URI
70+
const USER = process.env.NEO4J_USERNAME
71+
const PASSWORD = process.env.NEO4J_PASSWORD
72+
let driver
73+
74+
try {
75+
driver = neo4j.driver(URI, neo4j.auth.basic(USER, PASSWORD))
76+
const serverInfo = await driver.getServerInfo()
77+
console.log('Connection estabilished')
78+
console.log(serverInfo)
79+
} catch(err) {
80+
console.log(`Connection error\n${err}\nCause: ${err.cause}`)
81+
await driver.close()
82+
return
83+
}
84+
85+
// Use the driver to run queries
86+
87+
await driver.close()
88+
})();
89+
----
90+
91+
5292
== Further connection parameters
5393

5494
For more `Driver` configuration parameters and further connection settings, see xref:connect-advanced.adoc[Advanced connection information].

python-manual/modules/ROOT/pages/connect.adoc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Once you have xref:install.adoc#install-driver[installed the driver] and have xr
66

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

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

39-
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()`.
40-
This requires the package link:https://pypi.org/project/python-dotenv/[`python-dotenv`] to be installed.
39+
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()`.
40+
This approach requires the package link:https://pypi.org/project/python-dotenv/[`python-dotenv`].
4141

42-
[source,python,role=test-skip]
42+
[source, python, role=test-skip]
4343
----
4444
import dotenv
4545
import os
4646
from neo4j import GraphDatabase
4747
48-
dotenv.load_dotenv('Neo4j-a0a2fa1d-Created-2023-11-06.txt')
48+
dotenv.load_dotenv("Neo4j-a0a2fa1d-Created-2023-11-06.txt")
4949
5050
URI = os.getenv("NEO4J_URI")
5151
AUTH = (os.getenv("NEO4J_USERNAME"), os.getenv("NEO4J_PASSWORD"))

0 commit comments

Comments
 (0)