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
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
22 import Gargantext.Core.Types
23 import Gargantext.Database.Admin.Types.Hyperdata (HyperdataDocument, HyperdataContact)
24 import Gargantext.Database.Prelude
25 import Gargantext.Database.Query.Filter
26 import Gargantext.Database.Query.Table.NodeNode
27 import Gargantext.Database.Schema.Node
32 -- TODO getAllTableDocuments
33 getAllDocuments :: HasDBid NodeType => ParentId -> Cmd err (TableResult (Node HyperdataDocument))
34 getAllDocuments pId = getAllChildren pId (Proxy :: Proxy HyperdataDocument)
37 -- TODO getAllTableContacts
38 getAllContacts :: HasDBid NodeType => ParentId -> Cmd err (TableResult (Node HyperdataContact))
39 getAllContacts pId = getAllChildren pId (Proxy :: Proxy HyperdataContact)
42 getAllChildren :: (JSONB a, HasDBid NodeType)
46 -> Cmd err (NodeTableResult a)
47 getAllChildren pId p maybeNodeType = getChildren pId p maybeNodeType Nothing Nothing
49 getChildren :: (JSONB a, HasDBid NodeType)
55 -> Cmd err (NodeTableResult a)
56 getChildren pId _ maybeNodeType maybeOffset maybeLimit = do
58 $ limit' maybeLimit $ offset' maybeOffset
59 $ orderBy (asc _node_id)
62 docCount <- runCountOpaQuery query
64 pure $ TableResult { tr_docs = docs, tr_count = docCount }
67 query = selectChildren pId maybeNodeType
69 selectChildren :: HasDBid NodeType
73 selectChildren parentId maybeNodeType = proc () -> do
74 row@(Node nId _ typeName _ parent_id _ _ _) <- queryNodeTable -< ()
75 (NodeNode n1id n2id _ _) <- queryNodeNodeTable -< ()
77 let nodeType = maybe 0 toDBid maybeNodeType
78 restrict -< typeName .== pgInt4 nodeType
80 restrict -< (.||) (parent_id .== (pgNodeId parentId))
81 ( (.&&) (n1id .== pgNodeId parentId)