Skip to content

Instantly share code, notes, and snippets.

@voutilad

voutilad/sai.js

Last active Jun 2, 2020
Embed
What would you like to do?
Quick and dirty example of the Neo4j 4.0 js driver promise api
const neo4j = require('neo4j-driver')
const queryTemplate = `
MATCH (u:User {id: 'some-user-id'})-[:POSTED_ON_2020_05_10|POSTED_ON_2020_05_17]->(p:Post)
RETURN p ORDER BY p.createdAt;
`
let driver = neo4j.driver(
'neo4j://localhost',
neo4j.auth.basic('neo4j', 'password')
)
let localBuf = [];
const runQuery = async (query) => {
console.log(`running query: ${query}`)
const t0 = Date.now()
let session = driver.session({
database: 'sai',
})
try {
let {records, summary} = await session.run(query)
console.log(`starting result consumption...`)
records.forEach((record) => localBuf.push(record.toObject()))
console.log(`results consumed`)
console.log(`results available after: ${summary.resultAvailableAfter}ms, consumed after ${summary.resultConsumedAfter}ms`)
} catch (e) {
console.error(e)
return
}
session.close()
const delta = Date.now() - t0
console.log(`consumed ${localBuf.length} in ${delta}ms, approximate rate of ${localBuf.length / (delta / 1000)} obj/s`)
}
runQuery(queryTemplate)
.then(() => {
driver.close()
console.log('finished')
})
.catch(e => console.error(e))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment