zmactep / hasbolt

Haskell driver for Neo4j 3+ (BOLT protocol)
BSD 3-Clause "New" or "Revised" License
82 stars 13 forks source link

Support for transactions #14

Closed gvolpe closed 5 years ago

gvolpe commented 5 years ago

Hi @zmactep , thanks for making this library!

I see that project looks a bit abandoned and I was wondering if you have thought at any point about introducing support for transactions?

I could give it a shot and see if I can implement it if you think it could be merged :)

zmactep commented 5 years ago

Hi @gvolpe!

I thing transaction implementation is a great idea (really, do not understand why I haven't done it before). It would be great if you'll try to implement it. :)

gvolpe commented 5 years ago

Awesome, I'll give it a shot, thanks :)

zmactep commented 5 years ago

The solution at #16 supports this kind of syntax:

query1 :: MonadIO m => BoltActionT m [Record]
query1 = query "CREATE (g:Guitar { brand: 'Gibson' } ) RETURN g"

query2 :: MonadIO m => BoltActionT m ()
query2 = query_ "CREATE (g:Guitar { brand: 'Fender' } ) RETURN g"

query3 :: MonadIO m => BoltActionT m Int
query3 = length <$> query "CREATE (g:Guitar { brand: 'Ibanez' } ) RETURN g"

query4 :: MonadIO m => BoltActionT m Record
query4 = head <$> query "This is going to make it fail, BOOM!"

query5 :: MonadIO m => BoltActionT m [Record]
query5 = query "CREATE (g:Guitar { brand: 'Schecter' } ) RETURN g"

-- We can combine different types of queries or even perform some actions inside
transaction :: MonadIO m => BoltActionT m [Record]
transaction = transact $ do
    records <- query1
    query2
    number <- query3
    forM_ [0..number] $ \_ ->
      query4
    query5

The transaction would be rolled back in case of wrong cypher request or any other failure inside the transact block (e.g. wrong data unpacking).

gvolpe commented 5 years ago

Looks great @zmactep ! It'd be nice if you could include some unit tests. Maybe take the ones from here or if you prefer merge #16 and then I can PR the tests.

zmactep commented 5 years ago

Tests could be found in new commit to #16. They are disabled by default as they require neo4j at localhost with neo4j/test credentials.

gvolpe commented 5 years ago

Fixed by #16