Skip to content

Commit 88e646d

Browse files
authored
Aura example (#594)
1 parent 5b1c2eb commit 88e646d

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

test/examples.test.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,73 @@ describe('#integration examples', () => {
405405
expect(await consoleLoggedMsg).toEqual(personName)
406406
})
407407

408+
it('driver introduction example', async () => {
409+
const console = consoleOverride
410+
const consoleLoggedMsg = consoleOverridePromise
411+
412+
// tag::driver-introduction-example-imports[]
413+
const neo4j = require('neo4j-driver')
414+
// end::driver-introduction-example-imports[]
415+
416+
// tag::driver-introduction-example-variables[]
417+
/*
418+
const uri = '%%BOLT_URL_PLACEHOLDER%%';
419+
const user = '<Username for Neo4j Aura database>';
420+
const password = '<Password for Neo4j Aura database>';
421+
*/
422+
// end::driver-introduction-example-variables[]
423+
424+
// tag::driver-introduction-example[]
425+
const driver = neo4j.driver(uri, neo4j.auth.basic(user, password))
426+
const session = driver.session()
427+
428+
const person1Name = 'Alice'
429+
const person2Name = 'David'
430+
431+
try {
432+
// To learn more about the Cypher syntax, see https://neo4j.com/docs/cypher-manual/current/
433+
// The Reference Card is also a good resource for keywords https://neo4j.com/docs/cypher-refcard/current/
434+
const writeQuery = `MERGE (p1:Person { name: $person1Name })
435+
MERGE (p2:Person { name: $person2Name })
436+
MERGE (p1)-[:KNOWS]->(p2)
437+
RETURN p1, p2`
438+
439+
// Write transactions allow the driver to handle retries and transient errors
440+
const writeResult = await session.writeTransaction(tx =>
441+
tx.run(writeQuery, { person1Name, person2Name })
442+
)
443+
writeResult.records.forEach(record => {
444+
const person1Node = record.get('p1')
445+
const person2Node = record.get('p2')
446+
console.log(
447+
`Created friendship between: ${person1Node.properties.name}, ${person2Node.properties.name}`
448+
)
449+
})
450+
451+
const readQuery = `MATCH (p:Person)
452+
WHERE p.name = $personName
453+
RETURN p.name AS name`
454+
const readResult = await session.readTransaction(tx =>
455+
tx.run(readQuery, { personName: person1Name })
456+
)
457+
readResult.records.forEach(record => {
458+
console.log(`Found person: ${record.get('name')}`)
459+
})
460+
} catch (error) {
461+
console.error('Something went wrong: ', error)
462+
} finally {
463+
await session.close()
464+
}
465+
466+
// Don't forget to close the driver connection when you're finished with it
467+
await driver.close()
468+
// end::driver-introduction-example[]
469+
470+
expect(await consoleLoggedMsg).toEqual(
471+
`Created friendship between: ${person1Name}, ${person2Name}`
472+
)
473+
})
474+
408475
it('read write transaction example', async () => {
409476
const console = consoleOverride
410477
const consoleLoggedMsg = consoleOverridePromise

0 commit comments

Comments
 (0)