Skip to content

roman-bodavskiy/haskell-sequelize

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

haskell-sequelize

A port of juspay/purescript-sequelize into Haskell, mimicking Sequelize.js v4.

Status

Has not been tried in production yet.

Does not provide INSERT or DELETE yet.

Usage

Table definition

Define a Beam table:

data TestT f = Test
  { email :: B.Columnar f Text,
    enabled :: B.Columnar (B.Nullable f) Bool
  }
  deriving (Generic)

type Test = TestT Identity

instance B.Beamable TestT

instance B.Table TestT where
  data PrimaryKey TestT f = TestId (B.Columnar f Text) deriving (Generic, B.Beamable)
  primaryKey = TestId . email

Define a ModelMeta instance. You can modify table properties here.

instance ModelMeta TestT where
  modelFieldModification = B.tableModification
  modelTableName = "test"

SELECT

Simple SELECT:

{-# LANGUAGE OverloadedLabels #-}

import Named

getDisabled :: SqlSelect MySQL Test
getDisabled =
  sqlSelect
    ! #where_ [Is enabled Null]
    ! defaults

A more complex SELECT:

{-# LANGUAGE OverloadedLabels #-}

import Named

getSpecificPeople :: SqlSelect MySQL Test
getSpecificPeople =
  sqlSelect
    ! #where_
        [ Is enabled (Not Null),
          Or [Is email (Eq "[email protected]"), Is email (Eq "[email protected]")]
        ]
    ! #orderBy [Asc enabled, Desc email]
    ! #offset 8
    ! #limit 10

UPDATE

Set some fields:

{-# LANGUAGE OverloadedLabels #-}

import Named

busted :: SqlUpdate MySQL TestT
busted =
  sqlUpdate
    ! #set [Set enabled Nothing]
    ! #where_ [Is enabled (Not Null)]

Set all fields:

{-# LANGUAGE OverloadedLabels #-}

import Named

disableSomeone :: SqlUpdate MySQL TestT
disableSomeone =
  sqlUpdate'
    ! #save (Test "[email protected]" (Just False))
    ! #where_ [Is email (Eq "[email protected]")]

We support Set and SetDefault.

Developing

Nix:

$ nix-shell

(nix-shell) $ cabal build
(nix-shell) $ cabal test

Stack:

$ stack build
$ stack test

stack test doesn't work, because stack does not able to set lenient flag for cabal. Use cabal tests instead. Be sure the repo has cabal.project.local with packages: ../beam-mysql inside. Then run euler dev and then cabal test -f lenient

Packages

No packages published

Languages

  • Haskell 100.0%