Coder Social home page Coder Social logo

haskell-sql-edsl-demo's Introduction

README

This repo contains the material I used for a HaskellerZ meetup in Zurich.

You can can see information about that here.

You can also see the slides in PDF here.

Before running the code

Make sure you have PostgreSQL installed and run the files in the sql directory.

Composable, type-safe SQL query generation in Haskell

Libraries like Opaleye and Haskell Relational Record make use of Haskell's type system to provide an abstract way of composing SQL queries that are guaranteed to be valid.

Simple database connections

In the material I'm using PostgreSQL.

A basic way of talking to a PostgreSQL database is using postgresql-simple. While really useful, this library makes us write repetitive queries. Furthermore, it's string-based, so when running our queries against the database to get results in some Haskell data type, we're not guaranteed upon compliation time that the software is sound from a type perspective. This is not the "Haskell way" of writing programs.

What does it mean to be composable?

Composability means taking small, well-tested queries, and "mixing" them in some form. From there we can restrict them, join them, aggregate, count, order, and deliver a composable structure that can be reutilized. When we start composing queries, we also touch on the subject of projecting them. Projection means discarding columns from our queries.

Through projection and other means of composition we can achieve arbitrarily complex results while keeping a comprehensible and modular approach. Both Opaleye and Haskell Relational Record ensure meaningful composition with the use of different techniques.

What does it mean to be type-safe?

In Haskell, we normally see declarations such as data Foo a = Foo { _a :: a }. What's on the left side of the = is the type, and on the right are the terms. Type information is useful at compile time, Haskell makes great use of type information to make sure that our programs are safe.

If we can somehow abstract our queries and give them type information, then we could use them throughout our program to make sure that our queries "make sense" from a type perspective. This means that when we compare fields, that those fields are of the same type; when we're joining, our joins are upon compatible fields, etc. Furthermore we want to make sure that when we run queries against our database, that those results have a meaningful representation as Haskell values.

Both Opaleye and Haskell Relational Record exploit the type system via different means to make sure, on compile time, that our queries "make sense".

Author

Carlos D'Agostino [email protected]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.