forked from juspay/haskell-sequelize
-
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.
- Loading branch information
1 parent
991064c
commit b109d29
Showing
3 changed files
with
53 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
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,42 @@ | ||
{-# LANGUAGE TypeApplications #-} | ||
{-# OPTIONS_GHC -Wno-orphans #-} | ||
|
||
module Sequelize.SQLObject where | ||
|
||
import qualified Data.Aeson as A | ||
import Data.Coerce (coerce) | ||
import qualified Data.Vector as V | ||
import Database.Beam.Backend | ||
import qualified Database.Beam.Backend.SQL.AST as B | ||
import Data.Text | ||
import Data.Aeson | ||
import qualified Data.Text as T | ||
|
||
data SQLObject a = SQLObjectValue Text | SQLObjectList [SQLObject a] | ||
|
||
instance ToJSON (SQLObject a) where | ||
toJSON (SQLObjectValue a) = A.String a | ||
toJSON (SQLObjectList as) = A.Array (V.fromList $ toJSON <$> as) | ||
|
||
class ToSQLObject a where | ||
convertToSQLObject :: a -> SQLObject a | ||
|
||
instance HasSqlValueSyntax B.Value A.Value where | ||
sqlValueSyntax = autoSqlValueSyntax | ||
|
||
instance HasSqlValueSyntax B.Value a => ToSQLObject a where | ||
convertToSQLObject = SQLObjectValue . valueToText . sqlValueSyntax @B.Value | ||
|
||
-- FIXME remove overlapping if possible | ||
instance {-# OVERLAPPING #-} ToSQLObject a => ToSQLObject [a] where | ||
convertToSQLObject v = do | ||
let sqlObjectsList = convertToSQLObject <$> v | ||
SQLObjectList $ coerce @(SQLObject a) @(SQLObject [a]) <$> sqlObjectsList | ||
|
||
instance {-# OVERLAPPING #-} ToSQLObject a => ToSQLObject (Maybe a) where | ||
convertToSQLObject mbA = case mbA of | ||
Just a -> coerce @(SQLObject a) @(SQLObject (Maybe a)) $ convertToSQLObject a | ||
Nothing -> coerce @(SQLObject SqlNull) @(SQLObject (Maybe a)) $ convertToSQLObject SqlNull | ||
|
||
valueToText :: B.Value -> Text | ||
valueToText (B.Value v) = T.pack $ show v |