@@ -5,7 +5,7 @@ Getting Started
6In this section, we'll take a look at using Rel8 to work with a small database
6In this section, we'll take a look at using Rel8 to work with a small database
7for Haskell packages. We'll take a look at idiomatic usage of Rel8, mapping
7for Haskell packages. We'll take a look at idiomatic usage of Rel8, mapping
8−tables to Haskell, and then look at writing some simple queries.
8+tables to Haskell, and then look at writing some simple queries.
10Before we get started, we'll be using the following language extensions and
10Before we get started, we'll be using the following language extensions and
11imports throughout this guide::
11imports throughout this guide::
@@ -70,7 +70,7 @@ Following these steps for ``author``, we have::
70 { authorId :: Column f Int64
70 { authorId :: Column f Int64
71 , name :: Column f Text
71 , name :: Column f Text
72 , url :: Column f (Maybe Text)
72 , url :: Column f (Maybe Text)
74 deriving stock (Generic)
74 deriving stock (Generic)
75 deriving anyclass (Rel8able)
75 deriving anyclass (Rel8able)
@@ -81,7 +81,7 @@ in our project, it would be too easy to accidentally mix them up and create
81nonsensical joins. As Haskell programmers, we often solve this problem by
81nonsensical joins. As Haskell programmers, we often solve this problem by
82creating ``newtype`` wrappers, and we can also use this technique with Rel8::
82creating ``newtype`` wrappers, and we can also use this technique with Rel8::
84− newtype AuthorId = AuthorId { toInt64 :: Int64 }
84+ newtype AuthorId = AuthorId { toInt64 :: Int64 }
85 deriving newtype (DBEq, DBType, Eq, Show)
85 deriving newtype (DBEq, DBType, Eq, Show)
87Now we can write our final schema mapping. First, the ``author`` table::
87Now we can write our final schema mapping. First, the ``author`` table::
@@ -90,7 +90,7 @@ Now we can write our final schema mapping. First, the ``author`` table::
90 { authorId :: Column f AuthorId
90 { authorId :: Column f AuthorId
91 , authorName :: Column f Text
91 , authorName :: Column f Text
92 , authorUrl :: Column f (Maybe Text)
92 , authorUrl :: Column f (Maybe Text)
94 deriving stock (Generic)
94 deriving stock (Generic)
95 deriving anyclass (Rel8able)
95 deriving anyclass (Rel8able)
@@ -99,7 +99,7 @@ And similarly, the ``project`` table::
99 data Project f = Project
99 data Project f = Project
100 { projectAuthorId :: Column f AuthorId
100 { projectAuthorId :: Column f AuthorId
101 , projectName :: Column f Text
101 , projectName :: Column f Text
103 deriving stock (Generic)
103 deriving stock (Generic)
104 deriving anyclass (Rel8able
104 deriving anyclass (Rel8able
@@ -107,8 +107,8 @@ To show query results in this documentation, we'll also need ``Show`` instances:
107Unfortunately these definitions look a bit scary, but they are essentially just
107Unfortunately these definitions look a bit scary, but they are essentially just
108``deriving (Show)``::
108``deriving (Show)``::
110− deriving stock instance f ~ Identity => Show (Author f)
110+ deriving stock instance f ~ Result => Show (Author f)
111− deriving stock instance f ~ Identity => Show (Project f)
111+ deriving stock instance f ~ Result => Show (Project f)
113These data types describe the structural mapping of the tables, but we also
113These data types describe the structural mapping of the tables, but we also
114need to specify a ``TableSchema`` for each table. A ``TableSchema`` contains
114need to specify a ``TableSchema`` for each table. A ``TableSchema`` contains
@@ -169,7 +169,7 @@ database heavy applications.
170In Rel8, ``SELECT`` statements are built using the ``Query`` monad. You can
170In Rel8, ``SELECT`` statements are built using the ``Query`` monad. You can
171think of this monad like the ordinary ``[]`` (List) monad - but this isn't
171think of this monad like the ordinary ``[]`` (List) monad - but this isn't
172−required knowledge.
172+required knowledge.
174To start, we'll look at one of the simplest queries possible - a basic ``SELECT
174To start, we'll look at one of the simplest queries possible - a basic ``SELECT
175* FROM`` statement. To select all rows from a table, we use ``each``, and
175* FROM`` statement. To select all rows from a table, we use ``each``, and
@@ -193,17 +193,17 @@ expressions of type ``a``\".
193To execute this ``Query``, we pass it to ``select``::
193To execute this ``Query``, we pass it to ``select``::
195 >>> :t select c (each projectSchema)
195 >>> :t select c (each projectSchema)
196− select c (each projectSchema) :: MonadIO m => m [Project Identity]
196+ select c (each projectSchema) :: MonadIO m => m [Project Result]
198When we ``select`` things containing ``Expr``s, Rel8 builds a new response
198When we ``select`` things containing ``Expr``s, Rel8 builds a new response
199−table with the ``Identity`` interpretation. This means you'll get back plain
199+table with the ``Result`` interpretation. This means you'll get back plain
200Haskell values. Studying ``projectAuthorId`` again, we have::
200Haskell values. Studying ``projectAuthorId`` again, we have::
202− >>> let aProjectIdentity = undefined :: Project Identity
202+ >>> let aProjectResult = undefined :: Project Result
203− >>> :t projectAuthorId aProjectIdentity
203+ >>> :t projectAuthorId aProjectResult
204− projectAuthorId aProjectIdentity :: AuthorId
204+ projectAuthorId aProjectResult :: AuthorId
206−Here ``Column Identity AuthorId`` reduces to just ``AuthorId``, with no
206+Here ``Column Result AuthorId`` reduces to just ``AuthorId``, with no
207wrappping type at all.
207wrappping type at all.
209Putting this all together, we can run our first query::
209Putting this all together, we can run our first query::
@@ -343,8 +343,8 @@ focus on the /type/ of that query::
345 >>> :t select c authorsAndProjects
345 >>> :t select c authorsAndProjects
346− select c authorsAndProjects
346+ select c authorsAndProjects
347− :: MonadIO m => m [(Author Identity, Project Identity)]
347+ :: MonadIO m => m [(Author Result, Project Result)]
350Our query gives us a single list of pairs of authors and projects. However,
350Our query gives us a single list of pairs of authors and projects. However,
@@ -352,7 +352,7 @@ with our domain knowledge of the schema, this isn't a great type - what we'd
352rather have is a list of pairs of authors and /lists/ of projects. That is,
352rather have is a list of pairs of authors and /lists/ of projects. That is,
353what we'd like is::
353what we'd like is::
355− [(Author Identity, [Project Identity])]
355+ [(Author Result, [Project Result])]
357This would be a much better type! Rel8 can produce a query with this type by
357This would be a much better type! Rel8 can produce a query with this type by
358simply wrapping the call to ``projectsForAuthor`` with either ``some`` or
358simply wrapping the call to ``projectsForAuthor`` with either ``some`` or