Skip to content

Commit

Permalink
Haskell-Sequelize Open Source
Browse files Browse the repository at this point in the history
  • Loading branch information
hemantmangla committed Jul 11, 2022
0 parents commit 2c9eb73
Show file tree
Hide file tree
Showing 10 changed files with 1,239 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.stack-work/
.idea
*.iml
*~
dist-newstyle
.dir-locals.el
*.local
dist-local-build/
129 changes: 129 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# haskell-sequelize

A port of
[juspay/purescript-sequelize](https://github.com/juspay/purescript-sequelize)
into Haskell, mimicking [Sequelize.js v4](https://sequelize.org/v4).

## Status

Has not been tried in production yet.

Does not provide INSERT or DELETE yet.

## Usage

### Table definition

Define a Beam table:

```haskell
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.

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

### SELECT

Simple SELECT:

```haskell
{-# LANGUAGE OverloadedLabels #-}

import Named

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

A more complex SELECT:

```haskell
{-# 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:

```haskell
{-# LANGUAGE OverloadedLabels #-}

import Named

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

Set all fields:

```haskell
{-# 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:

```bash
$ nix-shell

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

Stack:

```bash
$ 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`
19 changes: 19 additions & 0 deletions euler.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: haskell-sequelize
haskell-name: sequelize
allowed-paths:
- sequelize.cabal
- src
- test

# Disabling tests as they use outdated beam-mysql
disable-tests: true
dependencies:
euler-build:
branch: master
revision: c6af205a7adb1b7abac18f06830f4ed196bfd6e4
beam:
branch: master
revision: 372542ec6c49d18e8c1c4ef9da15ff0b97c07ed8
beam-mysql:
branch: master
revision: 1371202ebc3ec7e9ef3e16d1e99f805596022217
139 changes: 139 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 72 additions & 0 deletions sequelize.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
cabal-version: 2.0

-- This file has been generated from package.yaml by hpack version 0.33.0.
--
-- see: https://github.com/sol/hpack
--
-- hash: 492410794b7d56b74a2a0cc2cb2d4ea37d2c1711251fc612e511aab5adfc35e6

name: sequelize
version: 1.1.1.0
description: A port of <https://github.com/juspay/purescript-sequelize> into Haskell
author: Artyom Kazak
maintainer: [email protected]
copyright: 2020 Juspay
license: BSD3
build-type: Simple
extra-source-files:
README.md

library
exposed-modules:
Sequelize
Sequelize.Encode
other-modules:
Paths_sequelize
hs-source-dirs:
src
default-extensions: AllowAmbiguousTypes RankNTypes ScopedTypeVariables StandaloneDeriving EmptyDataDecls FlexibleContexts FlexibleInstances FunctionalDependencies KindSignatures TypeOperators MultiParamTypeClasses TypeFamilies OverloadedLabels OverloadedStrings DeriveFunctor DeriveGeneric DataKinds DerivingStrategies ConstraintKinds UndecidableInstances InstanceSigs BlockArguments LambdaCase EmptyDataDeriving TypeOperators ViewPatterns KindSignatures
ghc-options: -Wall
build-depends:
aeson
, base >=4.7 && <5
, beam-core ^>=0.9.0.0
, beam-mysql ^>=1.3.0.4
, beam-postgres ^>=0.5.0.0
, beam-sqlite ^>=0.5.0.0
, bytestring
, containers
, generic-lens
, named
, text
, unordered-containers
, vector
default-language: Haskell2010

test-suite sequelize-test
type: exitcode-stdio-1.0
main-is: Test.hs
other-modules:
Paths_sequelize
hs-source-dirs:
test
default-extensions: AllowAmbiguousTypes RankNTypes ScopedTypeVariables StandaloneDeriving EmptyDataDecls FlexibleContexts FlexibleInstances FunctionalDependencies KindSignatures TypeOperators MultiParamTypeClasses TypeFamilies OverloadedLabels OverloadedStrings DeriveFunctor DeriveGeneric DataKinds DerivingStrategies ConstraintKinds UndecidableInstances InstanceSigs BlockArguments LambdaCase EmptyDataDeriving TypeOperators ViewPatterns KindSignatures
ghc-options: -Wall -threaded -rtsopts -with-rtsopts=-N
build-depends:
aeson
, base >=4.7 && <5
, beam-core ^>=0.9.0.0
, beam-mysql ^>=1.3.0.4
, beam-postgres ^>=0.5.0.0
, beam-sqlite ^>=0.5.0.0
, bytestring
, containers
, generic-lens
, named
, sequelize
, tasty
, tasty-hunit
, text
, unordered-containers
, vector
default-language: Haskell2010
Loading

0 comments on commit 2c9eb73

Please sign in to comment.