|
| 1 | +# Developer Guide: Working with Hasql Implementation |
| 2 | + |
| 3 | +## Core Concepts |
| 4 | + |
| 5 | +### DbAction Monad |
| 6 | +All database operations now use `DbAction m` instead of `ReaderT SqlBackend m`: |
| 7 | + |
| 8 | +```haskell |
| 9 | +-- Old (Persistent) |
| 10 | +insertBlock :: MonadIO m => Block -> ReaderT SqlBackend m BlockId |
| 11 | + |
| 12 | +-- New (Hasql) |
| 13 | +insertBlock :: MonadIO m => Block -> DbAction m BlockId |
| 14 | +``` |
| 15 | + |
| 16 | +### Statement Construction |
| 17 | +Database operations built using Hasql's encoder/decoder pattern: |
| 18 | + |
| 19 | +```haskell |
| 20 | +insertBlockStmt :: HsqlStmt.Statement Block BlockId |
| 21 | +insertBlockStmt = |
| 22 | + insert |
| 23 | + blockEncoder |
| 24 | + (WithResult $ HsqlD.singleRow $ idDecoder BlockId) |
| 25 | +``` |
| 26 | + |
| 27 | +## Module Structure |
| 28 | + |
| 29 | +- `Cardano.Db.Schema.*` - Type-safe schema definitions |
| 30 | +- `Cardano.Db.Statement.*` - Database operations organized by domain |
| 31 | +- `Cardano.Db.Statement.Function.*` - Core statement building utilities |
| 32 | + |
| 33 | +## Key Operations |
| 34 | + |
| 35 | +### Inserts |
| 36 | +```haskell |
| 37 | +-- Simple insert |
| 38 | +insert :: HsqlE.Params a -> ResultType r r -> HsqlS.Statement a r |
| 39 | + |
| 40 | +-- Bulk insert |
| 41 | +insertBulk :: [a] -> DbAction m [r] |
| 42 | + |
| 43 | +-- Conditional insert |
| 44 | +insertIfUnique :: HsqlE.Params a -> ResultType r r -> HsqlS.Statement a r |
| 45 | +``` |
| 46 | + |
| 47 | +### Queries |
| 48 | +```haskell |
| 49 | +-- Count operations |
| 50 | +countAll :: HsqlStmt.Statement () Word64 |
| 51 | +countWhere :: Text -> Text -> HsqlStmt.Statement () Word64 |
| 52 | + |
| 53 | +-- Existence checks |
| 54 | +existsById :: Key a -> DbAction m Bool |
| 55 | +existsWhereByColumn :: Text -> p -> DbAction m Bool |
| 56 | +``` |
| 57 | + |
| 58 | +### Execution Pattern |
| 59 | +```haskell |
| 60 | +runOperation :: MonadIO m => SomeRecord -> DbAction m SomeId |
| 61 | +runOperation record = |
| 62 | + runDbSession (mkDbCallStack "runOperation") $ |
| 63 | + HsqlSes.statement record someStmt |
| 64 | +``` |
| 65 | + |
| 66 | +## Type Safety |
| 67 | + |
| 68 | +### Column Validation |
| 69 | +All column references validated at compile time: |
| 70 | +```haskell |
| 71 | +validateColumn @Block "epoch_no" -- Compile-time check |
| 72 | +``` |
| 73 | + |
| 74 | +### Schema Correspondence |
| 75 | +Each table has corresponding encoder/decoder pairs ensuring type safety. |
| 76 | + |
| 77 | +## Migration Notes |
| 78 | + |
| 79 | +### Database Functions |
| 80 | +- Replace `rawSql` calls with typed statements |
| 81 | +- Use `HsqlStmt.Statement` construction pattern |
| 82 | +- Wrap operations in `runDbSession` with call stack |
| 83 | + |
| 84 | +### Error Handling |
| 85 | +```haskell |
| 86 | +-- Handle Maybe results |
| 87 | +case result of |
| 88 | + Just value -> pure value |
| 89 | + Nothing -> throwError $ DbError callStack errorMsg Nothing |
| 90 | +``` |
| 91 | + |
| 92 | +### Testing |
| 93 | +- Test database roundtrips with property-based testing |
| 94 | +- Use `runDbLovelaceRoundtrip` style functions for validation |
| 95 | +- Test encoders/decoders separately from business logic |
0 commit comments