-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get Hasql working for Story data type
Works for storing and retrieving all the story data. Dealing with the composite list of dictionary entries is a bit tricky when doing an insert/update and requires a workaround where the entry is unzipped into arrays and then recombined in the SQL and cast to the dict_entry type. See nikita-volkov/hasql#65 for more information.
- Loading branch information
Showing
4 changed files
with
82 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE DuplicateRecordFields #-} | ||
module HasqlDB where | ||
|
||
import Control.Monad (replicateM) | ||
import Data.Functor.Contravariant | ||
import Data.List (foldl') | ||
import Data.Maybe (fromJust) | ||
import Data.Monoid | ||
import Data.Text (Text) | ||
import Data.Vector (Vector) | ||
import Hasql.Query (Query) | ||
import qualified Hasql.Query as Q | ||
import qualified Hasql.Encoders as E | ||
import qualified Hasql.Decoders as D | ||
import Prelude hiding (id, words) | ||
|
||
import Api.Types | ||
|
||
dvText :: D.Row Text | ||
dvText = D.value D.text | ||
|
||
evText :: E.Params Text | ||
evText = E.value E.text | ||
|
||
selectAllStories :: Query () (Vector Story) | ||
selectAllStories = | ||
Q.statement sql mempty (D.rowsVector decoder) True | ||
where | ||
sql = "SELECT id, title, img_url, level, curriculum, tags, content, words, created_at FROM story" | ||
|
||
dictEntryValue = D.composite (DictEntry <$> D.compositeValue D.text <*> (fromIntegral <$> D.compositeValue D.int2)) | ||
array v = D.value (D.array (D.arrayDimension replicateM (D.arrayValue v))) | ||
|
||
decoder = Story | ||
<$> (Just <$> dvText) | ||
<*> dvText | ||
<*> dvText | ||
<*> (fromIntegral <$> D.value D.int2) | ||
<*> dvText | ||
<*> array D.text | ||
<*> dvText | ||
<*> array dictEntryValue | ||
<*> D.value D.timestamptz | ||
|
||
insertStory :: Query Story () | ||
insertStory = Q.statement sql storyEncoder D.unit True | ||
where | ||
sql = "INSERT INTO story (id, title, img_url, level, curriculum, tags, content, words) \ | ||
\VALUES ($1, $2, $3, $4, $5, $6, $7, (array(select word::dict_entry from unnest ($8, $9) as word)))" | ||
|
||
storyEncoder :: E.Params Story | ||
storyEncoder = | ||
contramap (fromJust . storyId) evText <> | ||
contramap title evText <> | ||
contramap img evText <> | ||
contramap (fromIntegral . storyLevel) (E.value E.int4) <> | ||
contramap curriculum evText <> | ||
contramap tags (array E.text) <> | ||
contramap content evText <> | ||
contramap (map word . words) (array E.text) <> | ||
contramap (map (fromIntegral . index) . words) (array E.int2) | ||
where | ||
array v = E.value (E.array (E.arrayDimension foldl' (E.arrayValue v))) | ||
storyId = id :: Story -> Maybe Text | ||
storyLevel = level :: Story -> Int |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters