Last active
June 2, 2020 17:17
-
-
Save voutilad/acfedf0f1eb494bb0bdda4cc7f3cd429 to your computer and use it in GitHub Desktop.
Quick and dirty example of the Neo4j 4.0 js driver promise api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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