What is persistent-postgresql
masterpersistent-postgresql is a specific database backend for the persistent library. It allows the persistent abstraction layer to communicate with a PostgreSQL database server.repository·master·Indexed 19 days ago
https://github.com/yesodweb/persistentA Haskell datastore library (ORM) providing type-safe database interactions to catch errors at compile-time and reduce serialization boilerplate. It features a backend-agnostic architecture supporting PostgreSQL, MySQL, SQLite, MongoDB, and Redis. The ecosystem includes persistent-qq for raw SQL, esqueleto for complex queries, and persistent-typed-db for multi-database access.
persistent-postgresql is a specific database backend for the persistent library. It allows the persistent abstraction layer to communicate with a PostgreSQL database server.The persistent-qq package provides two primary functions for executing raw SQL queries within the Persistent framework:
sqlQQ: Likely used for querying the database and returning results (selection).executeQQ: Likely used for executing raw SQL commands that do not return a result set (execution/mutation).These functions allow you to bypass the standard Persistent DSL to run arbitrary SQL while remaining integrated with your existing database connections.
persistent-mongoDB backend is currently on hiatus due to complexities involving EmbedEntityDef. A future version of persistent is planned to resolve these issues and make the MongoDB backend easier to use. If you require MongoDB support immediately, the maintainers welcome Pull Requests to improve the current implementation.persistent-template package has been absorbed into the main persistent package as of release 2.12.0.1. For modern projects, use the core persistent library instead of this standalone package.persistent-mysql is a backend implementation for the persistent database library that allows you to use the MySQL database server as your storage engine. It provides the necessary integration to use MySQL with Persistent's type-safe querying and entity management features.The persistent-redis package provides a Redis backend implementation for the Yesod Persistent library.
Supported Data Types: Currently, this backend only supports the following types:
StringBoolDoubleIntegerCompatibility:
This version is compatible with persistent 2.1.
esqueleto library to perform more complex SQL queries (such as joins and advanced filtering) while still leveraging Persistent's backend types.persistent-typed-db library allows for type-safe access to multiple databases that may have different schemas within the same application.Persistent is designed to be adaptable to various datastores, allowing multiple backends to be used simultaneously. It separates the serialization layer from the query layer.
persistent-odbc.PersistStore API for basic operations, rather than the full PersistQuery API).To use Persistent, you need to define your data models using Template Haskell quasi-quotes and then run migrations to set up your schema. Persistent provides type-safe interactions for inserting, reading, and deleting data.
Add the following to your package.yaml:
dependencies:
- base ^>= 4.17
- text ^>= 2
- persistent ^>= 2.14
- persistent-sqlite ^>= 2.13This example demonstrates defining a Person model, running a migration on an in-memory SQLite database, inserting records, querying them, and deleting them.
{-# LANGUAGE EmptyDataDecls #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
import Control.Monad.IO.Class (liftIO)
import Database.Persist
import Database.Persist.Sqlite
import Database.Persist.TH
import Data.Text
share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
Person
name Text
age Int Maybe
deriving Show
|]
main :: IO ()
main = runSqlite ":memory:" $ do
-- setup db schema
runMigration migrateAll
-- write to db
insert $ Person "Jane Doe" Nothing
johnId <- insert $ Person "John Doe" $ Just 35
-- read from db
john1 <- selectList [PersonId ==. johnId] [LimitTo 1]
john2 <- get johnId
liftIO $ print (john1 :: [Entity Person])
liftIO $ print (john2 :: Maybe Person)
-- delete from db
delete johnId
deleteWhere [PersonId ==. johnId]To build the Persistent project from source, clone the repository and use stack:
stack buildIf you only need to build against a specific subset of backends, refer to the development.md file for instructions.
The primary resource for learning how to use Persistent is the official Yesod Persistent book. It provides comprehensive guides on setting up and using the library.
Visit the book at: http://www.yesodweb.com/book/persistent