cache the names of functions returning JWT and avoid extra query on each rpc call

This commit is contained in:
Ruslan Talpa
2016-06-28 12:09:58 +03:00
parent a253ff325d
commit 744bbf7203
3 changed files with 14 additions and 20 deletions
+2 -1
View File
@@ -188,9 +188,10 @@ app dbStructure conf apiRequest =
Just (PayloadJSON (UniformObjects payload))) -> do Just (PayloadJSON (UniformObjects payload))) -> do
let p = V.head payload let p = V.head payload
jwtSecret = configJwtSecret conf jwtSecret = configJwtSecret conf
returnJWT = qiName qi `elem` dbProcsReturningJWT dbStructure
respondToRange $ do respondToRange $ do
row <- H.query () (callProc qi p topLevelRange shouldCount) row <- H.query () (callProc qi p topLevelRange shouldCount)
returnJWT <- H.query qi doesProcReturnJWT --returnJWT <- H.query qi doesProcReturnJWT
let (tableTotal, queryTotal, body) = fromMaybe (Just 0, 0, emptyArray) row let (tableTotal, queryTotal, body) = fromMaybe (Just 0, 0, emptyArray) row
(status, contentRange) = rangeHeader queryTotal tableTotal (status, contentRange) = rangeHeader queryTotal tableTotal
in in
+11 -19
View File
@@ -6,7 +6,6 @@
module PostgREST.DbStructure ( module PostgREST.DbStructure (
getDbStructure getDbStructure
, accessibleTables , accessibleTables
, doesProcReturnJWT
) where ) where
import qualified Hasql.Decoders as HD import qualified Hasql.Decoders as HD
@@ -15,7 +14,6 @@ import qualified Hasql.Query as H
import Control.Applicative import Control.Applicative
import Control.Monad (join, replicateM) import Control.Monad (join, replicateM)
import Data.Functor.Contravariant (contramap)
import Data.List (elemIndex, find, sort, import Data.List (elemIndex, find, sort,
subsequences, transpose) subsequences, transpose)
import Data.Maybe (fromJust, fromMaybe, isJust, import Data.Maybe (fromJust, fromMaybe, isJust,
@@ -37,6 +35,7 @@ getDbStructure schema = do
syns <- H.query () $ allSynonyms cols syns <- H.query () $ allSynonyms cols
rels <- H.query () $ allRelations tabs cols rels <- H.query () $ allRelations tabs cols
keys <- H.query () $ allPrimaryKeys tabs keys <- H.query () $ allPrimaryKeys tabs
retJwt <- H.query schema procsReturningJWT
let rels' = (addManyToManyRelations . raiseRelations schema syns . addParentRelations . addSynonymousRelations syns) rels let rels' = (addManyToManyRelations . raiseRelations schema syns . addParentRelations . addSynonymousRelations syns) rels
cols' = addForeignKeys rels' cols cols' = addForeignKeys rels' cols
@@ -47,13 +46,9 @@ getDbStructure schema = do
, dbColumns = cols' , dbColumns = cols'
, dbRelations = rels' , dbRelations = rels'
, dbPrimaryKeys = keys' , dbPrimaryKeys = keys'
, dbProcsReturningJWT = retJwt
} }
encodeQi :: HE.Params QualifiedIdentifier
encodeQi =
contramap qiSchema (HE.value HE.text) <>
contramap qiName (HE.value HE.text)
decodeTables :: HD.Result [Table] decodeTables :: HD.Result [Table]
decodeTables = decodeTables =
HD.rowsList tblRow HD.rowsList tblRow
@@ -103,19 +98,16 @@ decodeSynonyms cols =
<*> HD.value HD.text <*> HD.value HD.text <*> HD.value HD.text <*> HD.value HD.text
<*> HD.value HD.text <*> HD.value HD.text <*> HD.value HD.text <*> HD.value HD.text
doesProcReturnJWT :: H.Query QualifiedIdentifier Bool procsReturningJWT :: H.Query Schema [Text]
doesProcReturnJWT = procsReturningJWT =
H.statement sql encodeQi (HD.singleRow (HD.value HD.bool)) True H.statement sql (HE.value HE.text) (HD.rowsList (HD.value HD.text)) True
where where
sql = [q| SELECT EXISTS ( sql = [q|
SELECT 1 SELECT p.proname
FROM pg_catalog.pg_namespace n FROM pg_namespace n
JOIN pg_catalog.pg_proc p JOIN pg_proc p
ON pronamespace = n.oid ON pronamespace = n.oid
WHERE nspname = $1 WHERE n.nspname = $1 AND pg_get_function_result(p.oid) like '%jwt_claims'|]
AND proname = $2
AND pg_catalog.pg_get_function_result(p.oid) like '%jwt_claims'
) |]
accessibleTables :: H.Query Schema [Table] accessibleTables :: H.Query Schema [Table]
accessibleTables = accessibleTables =
+1
View File
@@ -13,6 +13,7 @@ data DbStructure = DbStructure {
, dbColumns :: [Column] , dbColumns :: [Column]
, dbRelations :: [Relation] , dbRelations :: [Relation]
, dbPrimaryKeys :: [PrimaryKey] , dbPrimaryKeys :: [PrimaryKey]
, dbProcsReturningJWT :: [Text]
} deriving (Show, Eq) } deriving (Show, Eq)
type Schema = Text type Schema = Text