Skip to content

Commit 31bfdac

Browse files
authored
Add example for combine multiple transactions (#742) (#746)
1 parent 7e622d9 commit 31bfdac

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

test/examples.test.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,61 @@ describe('#integration examples', () => {
539539
expect(await consoleLoggedMsg).toEqual('Names: Alice, Bob')
540540
}, 60000)
541541

542+
it('async multiple transactions', async () => {
543+
const console = consoleOverride
544+
const consoleLoggedMsg = consoleOverridePromise
545+
const driver = driverGlobal
546+
const companyName = 'Acme'
547+
const personNames = { nameA: 'Alice', nameB: 'Bob' }
548+
const tmpSession = driver.session()
549+
550+
try {
551+
await tmpSession.run(
552+
'CREATE (a:Person {name: $nameA}), (b:Person {name: $nameB})',
553+
personNames
554+
)
555+
556+
// tag::async-multiple-tx[]
557+
const session = driver.session()
558+
try {
559+
const names = await session.readTransaction(async tx => {
560+
const result = await tx.run('MATCH (a:Person) RETURN a.name AS name')
561+
return result.records.map(record => record.get('name'))
562+
})
563+
564+
const relationshipsCreated = await session.writeTransaction(tx =>
565+
Promise.all(
566+
names.map(name =>
567+
tx
568+
.run(
569+
'MATCH (emp:Person {name: $person_name}) ' +
570+
'MERGE (com:Company {name: $company_name}) ' +
571+
'MERGE (emp)-[:WORKS_FOR]->(com)',
572+
{ person_name: name, company_name: companyName }
573+
)
574+
.then(
575+
result =>
576+
result.summary.counters.updates().relationshipsCreated
577+
)
578+
.then(relationshipsCreated =>
579+
neo4j.int(relationshipsCreated).toInt()
580+
)
581+
)
582+
).then(values => values.reduce((a, b) => a + b))
583+
)
584+
585+
console.log(`Created ${relationshipsCreated} employees relationship`)
586+
} finally {
587+
await session.close()
588+
}
589+
// end::async-multiple-tx[]
590+
} finally {
591+
await tmpSession.close()
592+
}
593+
594+
expect(await consoleLoggedMsg).toEqual('Created 2 employees relationship')
595+
})
596+
542597
it('rx result consume example', async () => {
543598
if (protocolVersion < 4.0) {
544599
return

0 commit comments

Comments
 (0)