]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Database/Schema/Node.hs
[istex] scroll API fetch, first draft
[gargantext.git] / src / Gargantext / Database / Schema / Node.hs
1 {-|
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
8 Portability : POSIX
9 -}
10
11
12 {-# OPTIONS_GHC -fno-warn-orphans #-}
13
14 {-# LANGUAGE Arrows #-}
15 {-# LANGUAGE ConstraintKinds #-}
16 {-# LANGUAGE FunctionalDependencies #-}
17 {-# LANGUAGE TemplateHaskell #-}
18 {-# LANGUAGE TypeFamilies #-}
19
20 module Gargantext.Database.Schema.Node where
21
22 import Control.Lens hiding (elements, (&))
23 import Gargantext.Database.Schema.Prelude
24 import Prelude hiding (null, id, map, sum)
25
26 ------------------------------------------------------------------------
27 -- Main polymorphic Node definition
28 data NodePoly id
29 hash_id
30 typename
31 user_id
32 parent_id
33 name
34 date
35 hyperdata =
36 Node { _node_id :: !id
37 , _node_hash_id :: !hash_id
38 , _node_typename :: !typename
39
40 , _node_user_id :: !user_id
41 , _node_parent_id :: !parent_id
42
43 , _node_name :: !name
44 , _node_date :: !date
45
46 , _node_hyperdata :: !hyperdata
47 } deriving (Show, Generic)
48
49 ------------------------------------------------------------------------
50 -- Automatic instances derivation
51 $(deriveJSON (unPrefix "_node_") ''NodePoly)
52 $(makeLenses ''NodePoly)
53
54 $(makeAdaptorAndInstance "pNode" ''NodePoly)
55 $(makeLensesWith abbreviatedFields ''NodePoly)
56
57 nodeTable :: Table NodeWrite NodeRead
58 nodeTable = Table "nodes" (pNode Node { _node_id = optionalTableField "id"
59 , _node_hash_id = optionalTableField "hash_id"
60 , _node_typename = requiredTableField "typename"
61 , _node_user_id = requiredTableField "user_id"
62
63 , _node_parent_id = optionalTableField "parent_id"
64 , _node_name = requiredTableField "name"
65 , _node_date = optionalTableField "date"
66
67 , _node_hyperdata = requiredTableField "hyperdata"
68 -- ignoring ts_vector field here
69 }
70 )
71
72 queryNodeTable :: Query NodeRead
73 queryNodeTable = selectTable nodeTable
74 ------------------------------------------------------------------------
75 type NodeWrite = NodePoly (Maybe (Column PGInt4) )
76 (Maybe (Column PGText) )
77 (Column PGInt4)
78 (Column PGInt4)
79 (Maybe (Column PGInt4) )
80 (Column PGText)
81 (Maybe (Column PGTimestamptz))
82 (Column PGJsonb)
83
84 type NodeRead = NodePoly (Column PGInt4 )
85 (Column PGText )
86 (Column PGInt4 )
87 (Column PGInt4 )
88 (Column PGInt4 )
89 (Column PGText )
90 (Column PGTimestamptz )
91 (Column PGJsonb )
92
93 type NodeReadNull = NodePoly (Column (Nullable PGInt4))
94 (Column (Nullable PGText))
95 (Column (Nullable PGInt4))
96 (Column (Nullable PGInt4))
97 (Column (Nullable PGInt4))
98 (Column (Nullable PGText))
99 (Column (Nullable PGTimestamptz))
100 (Column (Nullable PGJsonb))
101 ------------------------------------------------------------------------
102 -- | Node(Read|Write)Search is slower than Node(Write|Read) use it
103 -- for full text search only
104
105 type NodeSearchWrite =
106 NodePolySearch
107 (Maybe (Column PGInt4) )
108 (Column PGInt4 )
109 (Column PGInt4 )
110 (Column (Nullable PGInt4) )
111 (Column PGText )
112 (Maybe (Column PGTimestamptz))
113 (Column PGJsonb )
114 (Maybe (Column PGTSVector) )
115
116 type NodeSearchRead =
117 NodePolySearch
118 (Column PGInt4 )
119 (Column PGInt4 )
120 (Column PGInt4 )
121 (Column (Nullable PGInt4 ))
122 (Column PGText )
123 (Column PGTimestamptz )
124 (Column PGJsonb )
125 (Column PGTSVector )
126
127 type NodeSearchReadNull =
128 NodePolySearch
129 (Column (Nullable PGInt4) )
130 (Column (Nullable PGInt4) )
131 (Column (Nullable PGInt4) )
132 (Column (Nullable PGInt4) )
133 (Column (Nullable PGText) )
134 (Column (Nullable PGTimestamptz))
135 (Column (Nullable PGJsonb) )
136 (Column (Nullable PGTSVector) )
137
138
139 data NodePolySearch id
140 typename
141 user_id
142 parent_id
143 name
144 date
145 hyperdata
146 search =
147 NodeSearch { _ns_id :: id
148 , _ns_typename :: typename
149 , _ns_user_id :: user_id
150 -- , nodeUniqId :: shaId
151 , _ns_parent_id :: parent_id
152 , _ns_name :: name
153 , _ns_date :: date
154
155 , _ns_hyperdata :: hyperdata
156 , _ns_search :: search
157 } deriving (Show, Generic)
158
159 $(makeAdaptorAndInstance "pNodeSearch" ''NodePolySearch)
160 $(makeLensesWith abbreviatedFields ''NodePolySearch)
161 $(deriveJSON (unPrefix "_ns_") ''NodePolySearch)
162 $(makeLenses ''NodePolySearch)
163
164 nodeTableSearch :: Table NodeSearchWrite NodeSearchRead
165 nodeTableSearch = Table "nodes" ( pNodeSearch
166 NodeSearch { _ns_id = optionalTableField "id"
167 , _ns_typename = requiredTableField "typename"
168 , _ns_user_id = requiredTableField "user_id"
169
170 , _ns_parent_id = requiredTableField "parent_id"
171 , _ns_name = requiredTableField "name"
172 , _ns_date = optionalTableField "date"
173
174 , _ns_hyperdata = requiredTableField "hyperdata"
175 , _ns_search = optionalTableField "search"
176 }
177 )
178 ------------------------------------------------------------------------