2 Module : Gargantext.Database.Node.Children
3 Description : Main requests of Node to the database
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
11 {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
12 {-# OPTIONS_GHC -fno-warn-orphans #-}
14 {-# LANGUAGE Arrows #-}
15 {-# LANGUAGE FlexibleInstances #-}
16 {-# LANGUAGE FlexibleContexts #-}
17 {-# LANGUAGE RankNTypes #-}
19 module Gargantext.Database.Node.Children where
23 import Gargantext.Core.Types
24 import Gargantext.Database.Schema.Node
25 import Gargantext.Database.Utils
26 import Gargantext.Database.Schema.NodeNode
27 import Gargantext.Database.Config (nodeTypeId)
28 import Gargantext.Database.Queries.Filter
29 import Gargantext.Database.Node.Contact (HyperdataContact)
30 import Gargantext.Database.Schema.Node (pgNodeId)
31 import Control.Arrow (returnA)
33 getAllDocuments :: ParentId -> Cmd err (TableResult (Node HyperdataDocument))
34 getAllDocuments pId = getAllChildren pId (Proxy :: Proxy HyperdataDocument)
37 getAllContacts :: ParentId -> Cmd err (TableResult (Node HyperdataContact))
38 getAllContacts pId = getAllChildren pId (Proxy :: Proxy HyperdataContact)
41 getAllChildren :: JSONB a
45 -> Cmd err (NodeTableResult a)
46 getAllChildren pId p maybeNodeType = getChildren pId p maybeNodeType Nothing Nothing
48 getChildren :: JSONB a
54 -> Cmd err (NodeTableResult a)
55 getChildren pId _ maybeNodeType maybeOffset maybeLimit = do
57 $ limit' maybeLimit $ offset' maybeOffset
58 $ orderBy (asc _node_id)
61 docCount <- runCountOpaQuery query
63 pure $ TableResult { tr_docs = docs, tr_count = docCount }
66 query = selectChildren pId maybeNodeType
68 selectChildren :: ParentId
71 selectChildren parentId maybeNodeType = proc () -> do
72 row@(Node nId typeName _ parent_id _ _ _) <- queryNodeTable -< ()
73 (NodeNode n1id n2id _ _) <- queryNodeNodeTable -< ()
75 let nodeType = maybe 0 nodeTypeId maybeNodeType
76 restrict -< typeName .== pgInt4 nodeType
78 restrict -< (.||) (parent_id .== (pgNodeId parentId))
79 ( (.&&) (n1id .== pgNodeId parentId)