JavaScript
Installation
Use the following command to install the Neo4j JavaScript Driver using npm
:
npm install neo4j-driver
Use the following command to install the Neo4j JavaScript Driver using yarn
:
yarn add neo4j-driver
Code example
(async() => {
const neo4j = require('neo4j-driver');
const uri = 'neo4j+s://<Bolt url for Neo4j Aura instance>';
const user = '<Username for Neo4j Aura instance>';
const password = '<Password for Neo4j Aura instance>';
// To learn more about the driver: https://neo4j.com/docs/javascript-manual/current/client-applications/#js-driver-driver-object
const driver = neo4j.driver(uri, neo4j.auth.basic(user, password));
try {
const person1Name = 'Alice';
const person2Name = 'David';
await createFriendship(driver, person1Name, person2Name);
await findPerson(driver, person1Name);
await findPerson(driver, person2Name);
} catch (error) {
console.error(`Something went wrong: ${error}`);
} finally {
// Don't forget to close the driver connection when you're finished with it.
await driver.close();
}
async function createFriendship (driver, person1Name, person2Name) {
// To learn more about sessions: https://neo4j.com/docs/javascript-manual/current/session-api/
const session = driver.session({ database: 'neo4j' });
try {
// To learn more about the Cypher syntax, see: https://neo4j.com/docs/cypher-manual/current/
// The Reference Card is also a good resource for keywords: https://neo4j.com/docs/cypher-refcard/current/
const writeQuery = `MERGE (p1:Person { name: $person1Name })
MERGE (p2:Person { name: $person2Name })
MERGE (p1)-[:KNOWS]->(p2)
RETURN p1, p2`;
// Write transactions allow the driver to handle retries and transient errors.
const writeResult = await session.executeWrite(tx =>
tx.run(writeQuery, { person1Name, person2Name })
);
// Check the write results.
writeResult.records.forEach(record => {
const person1Node = record.get('p1');
const person2Node = record.get('p2');
console.info(`Created friendship between: ${person1Node.properties.name}, ${person2Node.properties.name}`);
});
} catch (error) {
console.error(`Something went wrong: ${error}`);
} finally {
// Close down the session if you're not using it anymore.
await session.close();
}
}
async function findPerson(driver, personName) {
const session = driver.session({ database: 'neo4j' });
try {
const readQuery = `MATCH (p:Person)
WHERE p.name = $personName
RETURN p.name AS name`;
const readResult = await session.executeRead(tx =>
tx.run(readQuery, { personName })
);
readResult.records.forEach(record => {
console.log(`Found person: ${record.get('name')}`)
});
} catch (error) {
console.error(`Something went wrong: ${error}`);
} finally {
await session.close();
}
}
})();
Running the example
Follow the steps below to run the example code:
-
Copy and paste the code above into a file named
example.js
. -
Enter the information for the AuraDB instance you want to connect to by replacing:
-
<Bolt url for Neo4j Aura instance>
with the URI. -
<Username for Neo4j Aura instance>
with the username. -
<Password for Neo4j Aura instance>
with the password.
-
-
Use the following command to run the example code:
node example.js
Example walkthrough
The example imports neo4j-driver
to connect to the Neo4j AuraDB instance.
Two functions are then called:
-
createFriendship
creates twoPerson
nodes and aKNOWS
relationship between them using a write transaction. -
findPerson
finds thePerson
nodes using a read transaction.
Developing with Neo4j Aura requires the use of Transaction Functions. Transaction Functions enable automatic recovery from transient network errors and enable load balancing. |
Make sure to log queries and data sent from your application as it is useful when you encounter errors and can help with debugging.
Was this page helpful?