Type-safe SQL query builder and typed query support for PureScript SQL database bindings.
This package provides:
- Type-level parameter tracking for SQL queries
- Type-safe query builders
- Result set parsing with type classes
- Table schema definitions
- Support for both generic SQL (
?placeholders) and PostgreSQL ($Nplaceholders)
spago install yoga-sql-typesimport Yoga.SQL.Types as SQL
-- Define a typed query with parameters
getUserQuery :: SQL.SQLQuery ( id :: Int )
getUserQuery = SQL.sql $
"SELECT * FROM users WHERE id = " ^ SQL.int @"id"
-- Extract parameters in the correct order
params :: { id :: Int } -> Array SQL.SQLParameter
params = SQL.argsFor getUserQueryimport Yoga.SQL.PostgresTypes as PGSQL
-- PostgreSQL uses $1, $2, etc. for parameters
getUserQuery :: PGSQL.SQLQuery ( id :: Int, name :: String )
getUserQuery = PGSQL.sql $
"SELECT * FROM users WHERE id = " ^ PGSQL.int @"id" ^
" AND name = " ^ PGSQL.str @"name"
-- Parameters are extracted in the correct order
params :: { id :: Int, name :: String } -> Array PGSQL.SQLParameter
params = PGSQL.argsFor getUserQueryimport Yoga.SQL.Types (class FromResultArray, fromResultArray)
type User = { id :: Int, name :: String, email :: String }
-- Parse results from a query that returns (Int, String, String)
parseUser :: Array SQL.SQLResult -> Either String User
parseUser = fromResultArray \id name email -> { id, name, email }import Yoga.SQL.Types as SQL
usersTable :: SQL.Table
( id :: SQL.SQLColumn
, name :: SQL.SQLColumn
, email :: SQL.SQLColumn
)
usersTable = SQL.table (SQL.TableName "users")
{ id: SQL.SQLColumn SQL.IntegerColumn [ SQL.PrimaryKey, SQL.NotNull ]
, name: SQL.SQLColumn SQL.TextColumn [ SQL.NotNull ]
, email: SQL.SQLColumn SQL.TextColumn [ SQL.Unique, SQL.NotNull ]
}
-- Generate CREATE TABLE statement
createStatement :: SQL.CreateTableStatement
createStatement = SQL.createTable usersTableConvert PureScript values to SQL parameters:
class ToSQLParam a where
toSQLParam :: a -> SQLParameterInstances provided for:
Int,String,Number,BooleanArray a,NonEmptyArray aMaybe a- Any
Newtypewrapper
Parse SQL results into PureScript values:
class SQLFromForeign a where
fromSQLResultImpl :: Foreign -> ExceptT (NonEmptyList ForeignError) Identity aParse multiple columns from a result row:
class FromResultArray fn a | fn -> a where
fromResultArray :: fn -> Array SQLResult -> Either String asql- Create a query from a builderarg @type @name- Add a typed parameterint @name- Add an Int parameterstr @name- Add a String parameterbool @name- Add a Boolean parameternum @name- Add a Number parameternonArg- Add a literal SQL string^- Compose builders (infixl 8)
searchQuery :: SQL.SQLQuery ( minAge :: Int, maxAge :: Int, city :: String )
searchQuery = SQL.sql $
"SELECT * FROM users WHERE age >= " ^ SQL.int @"minAge" ^
" AND age <= " ^ SQL.int @"maxAge" ^
" AND city = " ^ SQL.str @"city"- Uses
?for parameter placeholders - Parameters extracted in order they appear
- Compatible with SQLite, MySQL, and other databases
- Uses
$1,$2, etc. for parameter placeholders - Parameters numbered automatically
- Optimised for PostgreSQL's parameter format
This package is used by:
yoga-postgres- PostgreSQL bindingsyoga-scylladb- ScyllaDB/Cassandra bindingsyoga-sqlite- SQLite bindingsyoga-bun-sqlite- Bun SQLite bindingsyoga-node-sqlite- Node SQLite bindings
- yoga-postgres - PostgreSQL client with typed queries
- yoga-scylladb - ScyllaDB client with typed queries
- yoga-sqlite - SQLite client with typed queries
- yoga-json - JSON library used internally
MIT