From 396eb4d20930b9a2ba676ffba767da25a8ef261f Mon Sep 17 00:00:00 2001 From: Stefano Ottolenghi Date: Mon, 6 Nov 2023 11:54:01 +0100 Subject: [PATCH] [Javascrip][Go] Example with dotenv for Aura. --- go-manual/modules/ROOT/pages/connect.adoc | 48 +++++++++++++++++++ .../modules/ROOT/pages/connect.adoc | 40 ++++++++++++++++ python-manual/modules/ROOT/pages/connect.adoc | 10 ++-- 3 files changed, 93 insertions(+), 5 deletions(-) diff --git a/go-manual/modules/ROOT/pages/connect.adoc b/go-manual/modules/ROOT/pages/connect.adoc index 7211282e..7bd620a5 100644 --- a/go-manual/modules/ROOT/pages/connect.adoc +++ b/go-manual/modules/ROOT/pages/connect.adoc @@ -12,6 +12,7 @@ package main import ( "context" + "fmt" "github.com/neo4j/neo4j-go-driver/v5/neo4j" ) @@ -32,6 +33,7 @@ func main() { if err != nil { panic(err) } + fmt.Println("Connection established.") } ---- @@ -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 <> 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]. diff --git a/javascript-manual/modules/ROOT/pages/connect.adoc b/javascript-manual/modules/ROOT/pages/connect.adoc index 69ef9833..9866eea9 100644 --- a/javascript-manual/modules/ROOT/pages/connect.adoc +++ b/javascript-manual/modules/ROOT/pages/connect.adoc @@ -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 <> 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.`. +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]. diff --git a/python-manual/modules/ROOT/pages/connect.adoc b/python-manual/modules/ROOT/pages/connect.adoc index ecf87ac2..f9f581eb 100644 --- a/python-manual/modules/ROOT/pages/connect.adoc +++ b/python-manual/modules/ROOT/pages/connect.adoc @@ -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 <> object and providing a URL and an authentication token. -[source,python] +[source, python] ---- from neo4j import GraphDatabase @@ -36,16 +36,16 @@ If you want to alter a `Driver` configuration, you will need to create a new obj When you create an <> 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"))