]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Database/Schema/Node.hs
[Community] Type Design (WIP)
[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 {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
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
29 data NodePoly id
30 typename
31 userId
32 parentId
33 name
34 date
35 hyperdata =
36 Node { _node_id :: !id
37 , _node_typename :: !typename
38
39 , _node_userId :: !userId
40 , _node_parentId :: !parentId
41
42 , _node_name :: !name
43 , _node_date :: !date
44
45 , _node_hyperdata :: !hyperdata
46 } deriving (Show, Generic)
47
48 ------------------------------------------------------------------------
49 -- Automatic instances derivation
50 $(deriveJSON (unPrefix "_node_") ''NodePoly)
51 $(makeLenses ''NodePoly)
52
53 $(makeAdaptorAndInstance "pNode" ''NodePoly)
54 $(makeLensesWith abbreviatedFields ''NodePoly)
55
56 ------------------------------------------------------------------------
57 nodeTable :: Table NodeWrite NodeRead
58 nodeTable = Table "nodes" (pNode Node { _node_id = optional "id"
59 , _node_typename = required "typename"
60 , _node_userId = required "user_id"
61
62 , _node_parentId = optional "parent_id"
63 , _node_name = required "name"
64 , _node_date = optional "date"
65
66 , _node_hyperdata = required "hyperdata"
67 -- ignoring ts_vector field here
68 }
69 )
70
71 queryNodeTable :: Query NodeRead
72 queryNodeTable = queryTable nodeTable
73 ------------------------------------------------------------------------
74 type NodeWrite = NodePoly (Maybe (Column PGInt4) )
75 (Column PGInt4)
76 (Column PGInt4)
77 (Maybe (Column PGInt4) )
78 (Column PGText)
79 (Maybe (Column PGTimestamptz))
80 (Column PGJsonb)
81
82 type NodeRead = NodePoly (Column PGInt4 )
83 (Column PGInt4 )
84 (Column PGInt4 )
85 (Column PGInt4 )
86 (Column PGText )
87 (Column PGTimestamptz )
88 (Column PGJsonb )
89
90 type NodeReadNull = NodePoly (Column (Nullable PGInt4))
91 (Column (Nullable PGInt4))
92 (Column (Nullable PGInt4))
93 (Column (Nullable PGInt4))
94 (Column (Nullable PGText))
95 (Column (Nullable PGTimestamptz))
96 (Column (Nullable PGJsonb))
97 ------------------------------------------------------------------------
98 -- | Node(Read|Write)Search is slower than Node(Write|Read) use it
99 -- for full text search only
100
101 type NodeSearchWrite =
102 NodePolySearch
103 (Maybe (Column PGInt4) )
104 (Column PGInt4 )
105 (Column PGInt4 )
106 (Column (Nullable PGInt4) )
107 (Column PGText )
108 (Maybe (Column PGTimestamptz))
109 (Column PGJsonb )
110 (Maybe (Column PGTSVector) )
111
112 type NodeSearchRead =
113 NodePolySearch
114 (Column PGInt4 )
115 (Column PGInt4 )
116 (Column PGInt4 )
117 (Column (Nullable PGInt4 ))
118 (Column PGText )
119 (Column PGTimestamptz )
120 (Column PGJsonb )
121 (Column PGTSVector )
122
123 type NodeSearchReadNull =
124 NodePolySearch
125 (Column (Nullable PGInt4) )
126 (Column (Nullable PGInt4) )
127 (Column (Nullable PGInt4) )
128 (Column (Nullable PGInt4) )
129 (Column (Nullable PGText) )
130 (Column (Nullable PGTimestamptz))
131 (Column (Nullable PGJsonb) )
132 (Column (Nullable PGTSVector) )
133
134
135 data NodePolySearch id
136 typename
137 userId
138 parentId
139 name
140 date
141 hyperdata
142 search =
143 NodeSearch { _ns_id :: id
144 , _ns_typename :: typename
145 , _ns_userId :: userId
146 -- , nodeUniqId :: shaId
147 , _ns_parentId :: parentId
148 , _ns_name :: name
149 , _ns_date :: date
150
151 , _ns_hyperdata :: hyperdata
152 , _ns_search :: search
153 } deriving (Show, Generic)
154
155 $(makeAdaptorAndInstance "pNodeSearch" ''NodePolySearch)
156 $(makeLensesWith abbreviatedFields ''NodePolySearch)
157 $(deriveJSON (unPrefix "_ns_") ''NodePolySearch)
158 $(makeLenses ''NodePolySearch)
159
160 nodeTableSearch :: Table NodeSearchWrite NodeSearchRead
161 nodeTableSearch = Table "nodes" (pNodeSearch NodeSearch { _ns_id = optional "id"
162 , _ns_typename = required "typename"
163 , _ns_userId = required "user_id"
164
165 , _ns_parentId = required "parent_id"
166 , _ns_name = required "name"
167 , _ns_date = optional "date"
168
169 , _ns_hyperdata = required "hyperdata"
170 , _ns_search = optional "search"
171 }
172 )
173 ------------------------------------------------------------------------