2022-03-16 07:12:15 +03:00
{- # LANGUAGE DeriveAnyClass # -}
--
2022-05-02 08:03:12 +03:00
module Hasura.Backends.DataConnector.API.V0.Table
2022-03-16 07:12:15 +03:00
( TableInfo ( .. ) ,
TableName ( .. ) ,
2022-08-24 00:46:10 +03:00
ForeignKeys ( .. ) ,
ConstraintName ( .. ) ,
Constraint ( .. ) ,
2022-03-16 07:12:15 +03:00
)
where
--------------------------------------------------------------------------------
2022-03-31 07:45:03 +03:00
import Autodocodec
import Autodocodec.OpenAPI ( )
2022-04-01 04:20:23 +03:00
import Control.DeepSeq ( NFData )
2022-08-24 00:46:10 +03:00
import Data.Aeson ( FromJSON , FromJSONKey , ToJSON , ToJSONKey )
2022-04-01 04:20:23 +03:00
import Data.Data ( Data )
2022-08-24 00:46:10 +03:00
import Data.HashMap.Strict ( HashMap )
2022-04-01 04:20:23 +03:00
import Data.Hashable ( Hashable )
2022-08-04 11:34:45 +03:00
import Data.List.NonEmpty ( NonEmpty )
2022-03-31 07:45:03 +03:00
import Data.OpenApi ( ToSchema )
2022-04-01 04:20:23 +03:00
import Data.Text ( Text )
import GHC.Generics ( Generic )
2022-05-02 08:03:12 +03:00
import Hasura.Backends.DataConnector.API.V0.Column qualified as API . V0
2022-04-01 04:20:23 +03:00
import Prelude
2022-03-16 07:12:15 +03:00
--------------------------------------------------------------------------------
2022-08-04 11:34:45 +03:00
newtype TableName = TableName { unTableName :: NonEmpty Text }
2022-03-16 07:12:15 +03:00
deriving stock ( Eq , Ord , Show , Generic , Data )
2022-04-01 04:20:23 +03:00
deriving anyclass ( NFData , Hashable )
2022-03-31 07:45:03 +03:00
deriving ( FromJSON , ToJSON , ToSchema ) via Autodocodec TableName
instance HasCodec TableName where
2022-08-04 11:34:45 +03:00
codec =
named " TableName " $
dimapCodec TableName unTableName codec
<?> " The fully qualified name of a table, where the last item in the array is the table name and any earlier items represent the namespacing of the table name "
2022-03-16 07:12:15 +03:00
--------------------------------------------------------------------------------
-- | Table schema data from the 'SchemaResponse'.
data TableInfo = TableInfo
{ dtiName :: TableName ,
dtiColumns :: [ API . V0 . ColumnInfo ] ,
2022-07-01 15:20:07 +03:00
dtiPrimaryKey :: Maybe [ API . V0 . ColumnName ] ,
2022-08-24 00:46:10 +03:00
dtiDescription :: Maybe Text ,
dtiForeignKeys :: Maybe ForeignKeys
2022-03-16 07:12:15 +03:00
}
deriving stock ( Eq , Ord , Show , Generic , Data )
2022-04-01 04:20:23 +03:00
deriving anyclass ( NFData , Hashable )
2022-03-31 07:45:03 +03:00
deriving ( FromJSON , ToJSON , ToSchema ) via Autodocodec TableInfo
instance HasCodec TableInfo where
codec =
object " TableInfo " $
TableInfo
2022-04-10 07:47:15 +03:00
<$> requiredField " name " " The name of the table " .= dtiName
2022-03-31 07:45:03 +03:00
<*> requiredField " columns " " The columns of the table " .= dtiColumns
<*> optionalFieldOrNull " primary_key " " The primary key of the table " .= dtiPrimaryKey
<*> optionalFieldOrNull " description " " Description of the table " .= dtiDescription
2022-08-24 00:46:10 +03:00
<*> optionalFieldOrNull " foreign_keys " " Foreign Key Constraints " .= dtiForeignKeys
--------------------------------------------------------------------------------
newtype ForeignKeys = ForeignKeys { unConstraints :: HashMap ConstraintName Constraint }
deriving stock ( Eq , Ord , Show , Generic , Data )
2022-08-29 03:20:00 +03:00
deriving newtype ( FromJSON , ToJSON )
2022-08-24 00:46:10 +03:00
deriving anyclass ( NFData , Hashable )
instance HasCodec ForeignKeys where
codec = dimapCodec ForeignKeys unConstraints $ codec @ ( HashMap ConstraintName Constraint )
newtype ConstraintName = ConstraintName { unConstraintName :: Text }
deriving stock ( Eq , Ord , Show , Generic , Data )
deriving newtype ( FromJSON , ToJSON , FromJSONKey , ToJSONKey )
deriving anyclass ( NFData , Hashable )
data Constraint = Constraint
{ cForeignTable :: Text ,
cColumnMapping :: HashMap Text Text
}
deriving stock ( Eq , Ord , Show , Generic , Data )
deriving anyclass ( NFData , Hashable )
2022-08-29 03:20:00 +03:00
deriving ( FromJSON , ToJSON ) via Autodocodec Constraint
2022-08-24 00:46:10 +03:00
instance HasCodec Constraint where
codec =
object " Constraint " $
Constraint
<$> requiredField " foreign_table " " The table referenced by the foreign key in the child table. " .= cForeignTable
<*> requiredField " column_mapping " " The columns on which you want want to define the foreign key. " .= cColumnMapping