2 Module : Gargantext.Database.Query.Table.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 #-}
16 module Gargantext.Database.Query.Table.Node.Children
19 import Control.Arrow (returnA)
21 import Gargantext.Core.Types
22 import Gargantext.Database.Admin.Config (nodeTypeId)
23 import Gargantext.Database.Admin.Types.Hyperdata (HyperdataDocument, HyperdataContact)
24 import Gargantext.Database.Admin.Types.Node (pgNodeId)
25 import Gargantext.Database.Prelude
26 import Gargantext.Database.Query.Filter
27 import Gargantext.Database.Query.Table.Node
28 import Gargantext.Database.Query.Table.NodeNode
29 import Gargantext.Database.Schema.Node
34 -- TODO getAllTableDocuments
35 getAllDocuments :: ParentId -> Cmd err (TableResult (Node HyperdataDocument))
36 getAllDocuments pId = getAllChildren pId (Proxy :: Proxy HyperdataDocument)
39 -- TODO getAllTableContacts
40 getAllContacts :: ParentId -> Cmd err (TableResult (Node HyperdataContact))
41 getAllContacts pId = getAllChildren pId (Proxy :: Proxy HyperdataContact)
44 getAllChildren :: JSONB a
48 -> Cmd err (NodeTableResult a)
49 getAllChildren pId p maybeNodeType = getChildren pId p maybeNodeType Nothing Nothing
51 getChildren :: JSONB a
57 -> Cmd err (NodeTableResult a)
58 getChildren pId _ maybeNodeType maybeOffset maybeLimit = do
60 $ limit' maybeLimit $ offset' maybeOffset
61 $ orderBy (asc _node_id)
64 docCount <- runCountOpaQuery query
66 pure $ TableResult { tr_docs = docs, tr_count = docCount }
69 query = selectChildren pId maybeNodeType
71 selectChildren :: ParentId
74 selectChildren parentId maybeNodeType = proc () -> do
75 row@(Node nId typeName _ parent_id _ _ _) <- queryNodeTable -< ()
76 (NodeNode n1id n2id _ _) <- queryNodeNodeTable -< ()
78 let nodeType = maybe 0 nodeTypeId maybeNodeType
79 restrict -< typeName .== pgInt4 nodeType
81 restrict -< (.||) (parent_id .== (pgNodeId parentId))
82 ( (.&&) (n1id .== pgNodeId parentId)