What is Selda?
Selda is an EDSL — an embedded domain-specific language — for defining, querying and modifying relational databases from Haskell. The same type-safe Selda query can be executed unmodified on either PostgreSQL or SQLite, making Selda ideal for prototyping as well as for taking the step from prototype to real application.
Through its monadic interface, Selda supports writing queries in a linear, natural style. The generated SQL code is guaranteed to be correct, type-safe and executable on all supported backends. With a minimalist approach to dependencies, Selda is lightweight enough to be suitable for inclusion in libraries as well as full applications. All non-essential features are optional, either through configuration flags or through separate add-on packages.
- Type-safe queries, inserts, updates and deletes
- Monadic query language with general inner queries
- Seamless prepared statements
- Upserts, transactions and constraints
- Minimal dependencies
- In-process caching
- Backends for PostgreSQL and SQLite
- ...and much more!
$ cabal update $ cabal install selda-sqlite selda-postgresql
$ cabal update $ cabal install selda-sqlite $ cabal install selda-postgresql
people :: Table (Text :*: Int :*: Maybe Text) people = table "people" $ primary "name" :*: required "age" :*: optional "pet" (name :*: age :*: pet) = selectors people main = withSQLite "people.sqlite" $ do createTable people insert_ people [ "Velvet" :*: 19 :*: Just "orthros" , "Kobayashi" :*: 23 :*: Just "dragon" , "Miyu" :*: 10 :*: Nothing ] adults <- query $ do person <- select people restrict (person ! age .> 18) return (person ! name :*: person ! pet) liftIO $ print adults