]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Database/Query/Table/Node/UpdateOpaleye.hs
Merge branch 'dev-tree-reload' of ssh://gitlab.iscpif.fr:20022/gargantext/haskell...
[gargantext.git] / src / Gargantext / Database / Query / Table / Node / UpdateOpaleye.hs
1 {-|
2 Module : Gargantext.Database.Node.UpdateOpaleye
3 Description : Update Node in Database (Postgres)
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
8 Portability : POSIX
9
10 -}
11
12 {-# LANGUAGE QuasiQuotes #-}
13
14
15 module Gargantext.Database.Query.Table.Node.UpdateOpaleye
16 where
17
18 import Opaleye
19 import Data.Aeson (encode, ToJSON)
20 import Gargantext.Prelude
21 import Gargantext.Database.Schema.Node
22 import Gargantext.Database.Admin.Types.Node
23 import Gargantext.Database.Prelude (Cmd, mkCmd, JSONB)
24 import Gargantext.Database.Query.Table.Node
25 import Gargantext.Database.Query.Table.Node.Error
26
27 updateHyperdata :: ToJSON a => NodeId -> a -> Cmd err Int64
28 updateHyperdata i h = mkCmd $ \c -> runUpdate_ c (updateHyperdataQuery i h)
29
30 updateHyperdataQuery :: ToJSON a => NodeId -> a -> Update Int64
31 updateHyperdataQuery i h = Update
32 { uTable = nodeTable
33 , uUpdateWith = updateEasy (\ (Node _ni _nh _nt _nu _np _nn _nd _h)
34 -> Node _ni _nh _nt _nu _np _nn _nd h'
35 )
36 , uWhere = (\row -> _node_id row .== pgNodeId i )
37 , uReturning = rCount
38 }
39 where h' = (pgJSONB $ cs $ encode $ h)
40
41 ----------------------------------------------------------------------------------
42 updateNodesWithType :: ( HasNodeError err
43 , JSONB a
44 , ToJSON a
45 ) => NodeType -> proxy a -> (a -> a) -> Cmd err [Int64]
46 updateNodesWithType nt p f = do
47 ns <- getNodesWithType nt p
48 mapM (\n -> updateHyperdata (_node_id n) (f $ _node_hyperdata n)) ns
49
50
51 -- | In case the Hyperdata Types are not compatible
52 updateNodesWithType_ :: ( HasNodeError err
53 , JSONB a
54 , ToJSON a
55 ) => NodeType -> a -> Cmd err [Int64]
56 updateNodesWithType_ nt h = do
57 ns <- getNodesIdWithType nt
58 mapM (\n -> updateHyperdata n h) ns
59
60
61
62