Skip to content

Aura example #594

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 2, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions test/examples.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,73 @@ describe('#integration examples', () => {
expect(await consoleLoggedMsg).toEqual(personName)
})

it('driver introduction example', async () => {
const console = consoleOverride
const consoleLoggedMsg = consoleOverridePromise

// tag::driver-introduction-example-imports[]
const neo4j = require('neo4j-driver')
// end::driver-introduction-example-imports[]

// tag::driver-introduction-example-variables[]
/*
const uri = '%%BOLT_URL_PLACEHOLDER%%';
const user = '<Username for Neo4j Aura database>';
const password = '<Password for Neo4j Aura database>';
*/
// end::driver-introduction-example-variables[]

// tag::driver-introduction-example[]
const driver = neo4j.driver(uri, neo4j.auth.basic(user, password))
const session = driver.session()

const person1Name = 'Alice'
const person2Name = 'David'

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.writeTransaction(tx =>
tx.run(writeQuery, { person1Name, person2Name })
)
writeResult.records.forEach(record => {
const person1Node = record.get('p1')
const person2Node = record.get('p2')
console.log(
`Created friendship between: ${person1Node.properties.name}, ${person2Node.properties.name}`
)
})

const readQuery = `MATCH (p:Person)
WHERE p.name = $personName
RETURN p.name AS name`
const readResult = await session.readTransaction(tx =>
tx.run(readQuery, { personName: person1Name })
)
readResult.records.forEach(record => {
console.log(`Found person: ${record.get('name')}`)
})
} catch (error) {
console.error('Something went wrong: ', error)
} finally {
await session.close()
}

// Don't forget to close the driver connection when you're finished with it
await driver.close()
// end::driver-introduction-example[]

expect(await consoleLoggedMsg).toEqual(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this part isn't visible in Aura page?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeh, so it'll only include what's between these lines:

// tag::driver-introduction-example[]

and

// end::driver-introduction-example[]

This is how we pull in the samples on the Aura side:

include::https://raw.githubusercontent.com/mneedham/neo4j-javascript-driver/aura-example/test/examples.test.js[tag="driver-introduction-example-imports", indent=0]
include::https://raw.githubusercontent.com/mneedham/neo4j-javascript-driver/aura-example/test/examples.test.js[tag="driver-introduction-example-variables", indent=0]
include::https://raw.githubusercontent.com/mneedham/neo4j-javascript-driver/aura-example/test/examples.test.js[tag="driver-introduction-example", indent=0]

That's pointing at my branch at the moment, but I'll update it to use the official repo after it's merged.

`Created friendship between: ${person1Name}, ${person2Name}`
)
})

it('read write transaction example', async () => {
const console = consoleOverride
const consoleLoggedMsg = consoleOverridePromise
Expand Down