transaction hooks example is very confusing
Closed this issue · 3 comments
tomasonjo commented
import { useReadTransaction } from 'use-neo4j'
const { transaction, commit, rollback } = useTransaction('mydb')
fetchSomeData()
.then(properties => {
// Use `run` to execute a query within the transaction
return run(`CREATE (n:Node) SET n+= $properties`, { properties })
// If all is fine, commit the transaction
.then(() => commit())
})
// If anything goes wrong, you can rollback the transaction
.catch(e => rollback())
you start by importing the readTransaction
and then use a useTransaction
hook, and lastly commit as if you are dealing with a write transaction. Also, I would much rather prefer some hardcoded property example as it's not clear how to actually execute this.
I get a transaction
variable from the hook, but then never use it and should use run
within a transaction that was never defined?
adam-cowley commented
This looks like a copy-and-paste error. This is how you would use useWriteTransaction
:
import { useWriteTransaction } from 'use-neo4j'
const { commit, rollback } = useWriteTransaction('mydb')
fetchSomeData()
.then(properties => {
// Use `run` to execute a query within the transaction
return run(`CREATE (n:Node) SET n+= $properties`, { properties })
// If all is fine, commit the transaction
.then(() => commit())
})
// If anything goes wrong, you can rollback the transaction
.catch(e => rollback())
The transaction object is there if you want to get any metadata from the transaction, I just haven't used it here.
tomasonjo commented
wouldn't you also need the run variable like so ->
const { run, commit, rollback } = useWriteTransaction('mydb')
adam-cowley commented
Yup