2 Module : Gargantext.Database.Schema.Node
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 DeriveGeneric #-}
16 {-# LANGUAGE ConstraintKinds #-}
17 {-# LANGUAGE FlexibleContexts #-}
18 {-# LANGUAGE FlexibleInstances #-}
19 {-# LANGUAGE FunctionalDependencies #-}
20 {-# LANGUAGE OverloadedStrings #-}
21 {-# LANGUAGE MultiParamTypeClasses #-}
22 {-# LANGUAGE NoImplicitPrelude #-}
23 {-# LANGUAGE RankNTypes #-}
24 {-# LANGUAGE TemplateHaskell #-}
25 {-# LANGUAGE TypeFamilies #-}
27 module Gargantext.Database.Schema.Node where
29 import Control.Lens.TH (makeLensesWith, abbreviatedFields)
30 import Data.Maybe (Maybe(..))
31 import Data.Profunctor.Product.TH (makeAdaptorAndInstance)
32 import Data.Text (Text)
33 import Database.PostgreSQL.Simple.FromField (FromField, fromField)
34 import Gargantext.Core.Types
35 import Gargantext.Database.Admin.Utils
36 import Gargantext.Viz.Graph (HyperdataGraph(..))
37 import Opaleye hiding (FromField)
38 import Opaleye.Internal.QueryArr (Query)
39 import Prelude hiding (null, id, map, sum)
41 ------------------------------------------------------------------------
42 instance FromField HyperdataAny where
43 fromField = fromField'
45 instance FromField HyperdataCorpus
47 fromField = fromField'
49 instance FromField HyperdataDocument
51 fromField = fromField'
53 instance FromField HyperdataDocumentV3
55 fromField = fromField'
57 instance FromField HyperData
59 fromField = fromField'
61 instance FromField HyperdataListModel
63 fromField = fromField'
65 instance FromField HyperdataGraph
67 fromField = fromField'
69 instance FromField HyperdataPhylo
71 fromField = fromField'
73 instance FromField HyperdataAnnuaire
75 fromField = fromField'
77 instance FromField HyperdataList
79 fromField = fromField'
81 instance FromField (NodeId, Text)
83 fromField = fromField'
84 ------------------------------------------------------------------------
85 instance QueryRunnerColumnDefault PGJsonb HyperdataAny
87 queryRunnerColumnDefault = fieldQueryRunnerColumn
89 instance QueryRunnerColumnDefault PGJsonb HyperdataList
91 queryRunnerColumnDefault = fieldQueryRunnerColumn
93 instance QueryRunnerColumnDefault PGJsonb HyperData
95 queryRunnerColumnDefault = fieldQueryRunnerColumn
98 instance QueryRunnerColumnDefault PGJsonb HyperdataDocument
100 queryRunnerColumnDefault = fieldQueryRunnerColumn
102 instance QueryRunnerColumnDefault PGJsonb HyperdataDocumentV3
104 queryRunnerColumnDefault = fieldQueryRunnerColumn
106 instance QueryRunnerColumnDefault PGJsonb HyperdataCorpus
108 queryRunnerColumnDefault = fieldQueryRunnerColumn
110 instance QueryRunnerColumnDefault PGJsonb HyperdataListModel
112 queryRunnerColumnDefault = fieldQueryRunnerColumn
114 instance QueryRunnerColumnDefault PGJsonb HyperdataGraph
116 queryRunnerColumnDefault = fieldQueryRunnerColumn
118 instance QueryRunnerColumnDefault PGJsonb HyperdataPhylo
120 queryRunnerColumnDefault = fieldQueryRunnerColumn
122 instance QueryRunnerColumnDefault PGJsonb HyperdataAnnuaire
124 queryRunnerColumnDefault = fieldQueryRunnerColumn
126 instance QueryRunnerColumnDefault PGTSVector (Maybe TSVector)
128 queryRunnerColumnDefault = fieldQueryRunnerColumn
130 instance QueryRunnerColumnDefault PGInt4 (Maybe NodeId)
132 queryRunnerColumnDefault = fieldQueryRunnerColumn
134 instance QueryRunnerColumnDefault PGInt4 NodeId
136 queryRunnerColumnDefault = fieldQueryRunnerColumn
138 instance QueryRunnerColumnDefault (Nullable PGInt4) NodeId
140 queryRunnerColumnDefault = fieldQueryRunnerColumn
143 ------------------------------------------------------------------------
144 $(makeAdaptorAndInstance "pNode" ''NodePoly)
145 $(makeLensesWith abbreviatedFields ''NodePoly)
147 $(makeAdaptorAndInstance "pNodeSearch" ''NodePolySearch)
148 $(makeLensesWith abbreviatedFields ''NodePolySearch)
150 type NodeWrite = NodePoly (Maybe (Column PGInt4) )
153 (Maybe (Column PGInt4) )
155 (Maybe (Column PGTimestamptz))
158 type NodeRead = NodePoly (Column PGInt4 )
163 (Column PGTimestamptz )
166 type NodeReadNull = NodePoly (Column (Nullable PGInt4))
167 (Column (Nullable PGInt4))
168 (Column (Nullable PGInt4))
169 (Column (Nullable PGInt4))
170 (Column (Nullable PGText))
171 (Column (Nullable PGTimestamptz))
172 (Column (Nullable PGJsonb))
174 nodeTable :: Table NodeWrite NodeRead
175 nodeTable = Table "nodes" (pNode Node { _node_id = optional "id"
176 , _node_typename = required "typename"
177 , _node_userId = required "user_id"
179 , _node_parentId = optional "parent_id"
180 , _node_name = required "name"
181 , _node_date = optional "date"
183 , _node_hyperdata = required "hyperdata"
187 queryNodeTable :: Query NodeRead
188 queryNodeTable = queryTable nodeTable
190 ------------------------------------------------------------------------
191 -- | Node(Read|Write)Search is slower than Node(Write|Read) use it
192 -- for full text search only
193 type NodeSearchWrite =
195 (Maybe (Column PGInt4) )
198 (Column (Nullable PGInt4) )
200 (Maybe (Column PGTimestamptz))
202 (Maybe (Column PGTSVector) )
204 type NodeSearchRead =
209 (Column (Nullable PGInt4 ))
211 (Column PGTimestamptz )
215 type NodeSearchReadNull =
217 (Column (Nullable PGInt4) )
218 (Column (Nullable PGInt4) )
219 (Column (Nullable PGInt4) )
220 (Column (Nullable PGInt4) )
221 (Column (Nullable PGText) )
222 (Column (Nullable PGTimestamptz))
223 (Column (Nullable PGJsonb) )
224 (Column (Nullable PGTSVector) )
226 nodeTableSearch :: Table NodeSearchWrite NodeSearchRead
227 nodeTableSearch = Table "nodes" (pNodeSearch NodeSearch { _ns_id = optional "id"
228 , _ns_typename = required "typename"
229 , _ns_userId = required "user_id"
231 , _ns_parentId = required "parent_id"
232 , _ns_name = required "name"
233 , _ns_date = optional "date"
235 , _ns_hyperdata = required "hyperdata"
236 , _ns_search = optional "search"