Giter VIP home page Giter VIP logo

data-serialization-postgresql's Introduction

data-serialization-postgresql

Simply derive your type from @Generic@, @Decoding FromFields@ and @Encoding ToFields@. If you want some field not to present in query, use @OptField@ wrapper.

data Test = Test {
    testInt :: Int, -- ^ This field must be in query
    testOptional :: Maybe Double, -- ^ This field must be in query, but it's nullable
    testString :: OptField String } -- ^ This may not be in query
        deriving (Generic, Show)

instance Serializable (Decoding FromFields) Test
instance Serializable (Encoding ToFields) Test
instance Serializable Fields Test
instance InTable Test where
    table _ = "test"

You can specify names for columns:

instance Serializable Pgser Test where
    ser =
        dat_ (ctor_ (
            stor "id" ser .*.
            stor "opti" ser .*.
            stor "teststr" ser))
        .:.
        giso

Example:

runCreate :: IO ()
runCreate = do
    con <- connect testcon
    execute_ con "drop table test"
    create con (Table :: Table Test)
    return ()

runInsert :: IO ()
runInsert = do
    con <- connect testcon
    insert con (Test 1 Nothing (Has "Hello, world!"))
    insert con (Test 2 (Just 10.0) (Has "Some string"))
    insert con (Test 3 Nothing HasNo)
    -- Test {testInt = 1, testOptional = Nothing, testString = Has "Hello, world!"}
    -- Test {testInt = 2, testOptional = Just 10.0, testString = Has "Some string"}
    -- Test {testInt = 3, testOptional = Nothing, testString = HasNo}
    return ()

runUpdate :: IO ()
runUpdate = do
    con <- connect testcon
    -- @Nothing@ is for null, @HasNo@ is for no update
    update_ con (Test 1 (Just 20.0) HasNo) " where testint = 1"
    update_ con (Test 2 Nothing (Has "New string")) " where testint = 2"
    update_ con (Test 3 (Just 30.0) (HasNo)) " where testint = 3"
    -- Test {testInt = 1, testOptional = Just 20.0, testString = Has "Hello, world!"}
    -- Test {testInt = 2, testOptional = Nothing, testString = Has "New string"}
    -- Test {testInt = 3, testOptional = Just 30.0, testString = HasNo}
    return ()

runSelect :: IO ()
runSelect = do
    con <- connect testcon
    vs <- select_ con "" :: IO [Test]
    mapM_ print vs

data-serialization-postgresql's People

Contributors

mvoidex avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.