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-orphans #-}
13 {-# LANGUAGE Arrows #-}
14 {-# LANGUAGE ConstraintKinds #-}
15 {-# LANGUAGE FunctionalDependencies #-}
16 {-# LANGUAGE TemplateHaskell #-}
17 {-# LANGUAGE TypeFamilies #-}
19 module Gargantext.Database.Schema.Context where
21 import Control.Lens hiding (elements, (&), Context)
22 import Gargantext.Database.Schema.Prelude
23 import Prelude hiding (null, id, map, sum)
26 ------------------------------------------------------------------------
27 -- Main polymorphic Node definition
36 Context { _context_id :: !id
37 , _context_hash_id :: !hash_id
38 , _context_typename :: !typename
40 , _context_user_id :: !user_id
41 , _context_parent_id :: !parent_id
43 , _context_name :: !name
44 , _context_date :: !date
46 , _context_hyperdata :: !hyperdata
47 } deriving (Show, Generic)
49 ------------------------------------------------------------------------
50 -- Automatic instances derivation
51 $(deriveJSON (unPrefix "_context_") ''ContextPoly)
52 $(makeLenses ''ContextPoly)
54 $(makeAdaptorAndInstance "pContext" ''ContextPoly)
55 $(makeLensesWith abbreviatedFields ''ContextPoly)
57 contextTable :: Table ContextWrite ContextRead
58 contextTable = Table "contexts" (pContext Context { _context_id = optionalTableField "id"
59 , _context_hash_id = optionalTableField "hash_id"
60 , _context_typename = requiredTableField "typename"
61 , _context_user_id = requiredTableField "user_id"
63 , _context_parent_id = optionalTableField "parent_id"
64 , _context_name = requiredTableField "name"
65 , _context_date = optionalTableField "date"
67 , _context_hyperdata = requiredTableField "hyperdata"
68 -- ignoring ts_vector field here
72 queryContextTable :: Query ContextRead
73 queryContextTable = selectTable contextTable
74 ------------------------------------------------------------------------
75 type ContextWrite = ContextPoly (Maybe (Field SqlInt4) )
76 (Maybe (Field SqlText) )
79 (Maybe (Field SqlInt4) )
81 (Maybe (Field SqlTimestamptz))
84 type ContextRead = ContextPoly (Field SqlInt4 )
90 (Field SqlTimestamptz )
93 type ContextReadNull = ContextPoly (FieldNullable SqlInt4)
94 (FieldNullable SqlText)
95 (FieldNullable SqlInt4)
96 (FieldNullable SqlInt4)
97 (FieldNullable SqlInt4)
98 (FieldNullable SqlText)
99 (FieldNullable SqlTimestamptz)
100 (FieldNullable SqlJsonb)
101 ------------------------------------------------------------------------
102 -- | Context(Read|Write)Search is slower than Context(Write|Read) use it
103 -- for full text search only
105 type ContextSearchWrite =
107 (Maybe (Field SqlInt4) )
110 (FieldNullable SqlInt4)
112 (Maybe (Field SqlTimestamptz))
114 (Maybe (Field SqlTSVector) )
116 type ContextSearchRead =
121 (FieldNullable SqlInt4 )
123 (Field SqlTimestamptz )
127 type ContextSearchReadNull =
129 (FieldNullable SqlInt4)
130 (FieldNullable SqlInt4)
131 (FieldNullable SqlInt4)
132 (FieldNullable SqlInt4)
133 (FieldNullable SqlText)
134 (FieldNullable SqlTimestamptz)
135 (FieldNullable SqlJsonb)
136 (FieldNullable SqlTSVector)
139 data ContextPolySearch id
147 ContextSearch { _cs_id :: id
148 , _cs_typename :: typename
149 , _cs_user_id :: user_id
150 -- , ContextUniqId :: shaId
151 , _cs_parent_id :: parent_id
155 , _cs_hyperdata :: hyperdata
156 , _cs_search :: search
157 } deriving (Show, Generic)
159 $(makeAdaptorAndInstance "pContextSearch" ''ContextPolySearch)
160 $(makeLensesWith abbreviatedFields ''ContextPolySearch)
161 $(deriveJSON (unPrefix "_cs_") ''ContextPolySearch)
162 $(makeLenses ''ContextPolySearch)
164 contextTableSearch :: Table ContextSearchWrite ContextSearchRead
165 contextTableSearch = Table "contexts" ( pContextSearch
166 ContextSearch { _cs_id = optionalTableField "id"
167 , _cs_typename = requiredTableField "typename"
168 , _cs_user_id = requiredTableField "user_id"
170 , _cs_parent_id = requiredTableField "parent_id"
171 , _cs_name = requiredTableField "name"
172 , _cs_date = optionalTableField "date"
174 , _cs_hyperdata = requiredTableField "hyperdata"
175 , _cs_search = optionalTableField "search"
178 ------------------------------------------------------------------------