adam-cowley / use-neo4j

React Hooks for Neo4j
MIT License
73 stars 21 forks source link

transaction hooks example is very confusing #13

Closed tomasonjo closed 1 year ago

tomasonjo commented 1 year ago
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 1 year ago

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 1 year ago

wouldn't you also need the run variable like so ->

const { run, commit, rollback } = useWriteTransaction('mydb')
adam-cowley commented 1 year ago

Yup