Skip to content

Commit

Permalink
feat: WIP allow spread operators in to-many relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
laurenceisla committed Jul 5, 2024
1 parent c3070bb commit 1a02416
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 11 deletions.
10 changes: 0 additions & 10 deletions src/PostgREST/Plan.hs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,6 @@ readPlan qi@QualifiedIdentifier{..} AppConfig{configDbMaxRows, configDbAggregate
hoistSpreadAggFunctions =<<
addRelSelects =<<
addNullEmbedFilters =<<
validateSpreadEmbeds =<<
addRelatedOrders =<<
addAliases =<<
expandStars ctx =<<
Expand Down Expand Up @@ -890,15 +889,6 @@ resolveLogicTree ctx (Expr b op lts) = CoercibleExpr b op (map (resolveLogicTree
resolveFilter :: ResolverContext -> Filter -> CoercibleFilter
resolveFilter ctx (Filter fld opExpr) = CoercibleFilter{field=resolveQueryInputField ctx fld, opExpr=opExpr}

-- Validates that spread embeds are only done on to-one relationships
validateSpreadEmbeds :: ReadPlanTree -> Either ApiRequestError ReadPlanTree
validateSpreadEmbeds (Node rp@ReadPlan{relToParent=Nothing} forest) = Node rp <$> validateSpreadEmbeds `traverse` forest
validateSpreadEmbeds (Node rp@ReadPlan{relIsSpread,relToParent=Just rel,relName} forest) = do
validRP <- if relIsSpread && not (relIsToOne rel)
then Left $ SpreadNotToOne (qiName $ relTable rel) relName -- TODO using relTable is not entirely right because ReadPlan might have an alias, need to store the parent alias on ReadPlan
else Right rp
Node validRP <$> validateSpreadEmbeds `traverse` forest

-- Find a Node of the Tree and apply a function to it
updateNode :: (a -> ReadPlanTree -> ReadPlanTree) -> (EmbedPath, a) -> Either ApiRequestError ReadPlanTree -> Either ApiRequestError ReadPlanTree
updateNode f ([], a) rr = f a <$> rr
Expand Down
6 changes: 5 additions & 1 deletion src/PostgREST/Query/QueryBuilder.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ import PostgREST.Config.PgVersion (PgVersion, pgVersion130)
import PostgREST.SchemaCache.Identifiers (QualifiedIdentifier (..))
import PostgREST.SchemaCache.Relationship (Cardinality (..),
Junction (..),
Relationship (..))
Relationship (..),
relIsToOne)

Check failure on line 31 in src/PostgREST/Query/QueryBuilder.hs

View workflow job for this annotation

GitHub Actions / Cabal - Linux GHC 9.6.4

The import of ‘relIsToOne’

Check failure on line 31 in src/PostgREST/Query/QueryBuilder.hs

View workflow job for this annotation

GitHub Actions / Cabal - Linux GHC 9.8.2

The import of ‘relIsToOne’

Check failure on line 31 in src/PostgREST/Query/QueryBuilder.hs

View workflow job for this annotation

GitHub Actions / Stack - Linux

The import of ‘relIsToOne’

Check failure on line 31 in src/PostgREST/Query/QueryBuilder.hs

View workflow job for this annotation

GitHub Actions / Stack - MacOS

The import of ‘relIsToOne’

Check failure on line 31 in src/PostgREST/Query/QueryBuilder.hs

View workflow job for this annotation

GitHub Actions / Stack - Windows

The import of `relIsToOne'
import PostgREST.SchemaCache.Routine (RoutineParam (..))

import PostgREST.ApiRequest.Types
Expand All @@ -43,6 +44,9 @@ import Protolude
readPlanToQuery :: ReadPlanTree -> SQL.Snippet
readPlanToQuery node@(Node ReadPlan{select,from=mainQi,fromAlias,where_=logicForest,order, range_=readRange, relToParent, relJoinConds, relSelect} forest) =
"SELECT " <>
-- Needs to validate if: "relIsSpread && (relIsToOne <$> relToParent == Just False)"
-- then use json_agg() in pgFmtSelectItem qi
-- It will not apply for *, check if something can be done here (maybe nest/unnest, use the Schema Cache or disallow completely)
intercalateSnippet ", " ((pgFmtSelectItem qi <$> (if null select && null forest then defSelect else select)) ++ joinsSelects) <> " " <>
fromFrag <> " " <>
intercalateSnippet " " joins <> " " <>
Expand Down

0 comments on commit 1a02416

Please sign in to comment.