Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend the capabilities of PostgREST #280 #295

Merged
merged 38 commits into from Oct 4, 2015
Merged

Extend the capabilities of PostgREST #280 #295

merged 38 commits into from Oct 4, 2015

Conversation

@ruslantalpa
Copy link
Contributor

@ruslantalpa ruslantalpa commented Sep 28, 2015

itsdone

All tests passing (but not 100% ready for release).
I created this pull request to start the conversation so others can take a look at it. This needs fresh pair of eyes.

What currently works (i think) and how it was changed compared to prev version

  • The db structure is read only once at startup
  • / and /table[OPTINS] use only cached info
  • PUT / PATCH also use cached db info
  • Foreign keys for views are also detected
  • As before you can do
?select=*, col1::text, col2->>jp, ...
  • The major change is you can now include info from related tables (parents, children, many to many)
?select=*, child1(*), parent1(id, name), child2(*, subchild(*))
  • To add "filters" for related tables you can do
?child.col=eq.1&child.subchild.col=eq.2

What needs to be done

  • There are no tests for requesting related items (i think someone else should write them)
  • When there is an error in parsing the request or generating the query (no relation) i simply output the text, this needs to be "cast" somehow to be handled by errResponse
App.hs (81)
Left e -> return $ responseLBS status200 [("Content-Type", "text/plain")] $ cs e
  • Probably the code needs a bit of cleanup (formatting, indentation, rename some functions like allcolumns ...)
@@ -1,27 +1,28 @@
{-# LANGUAGE QuasiQuotes, ScopedTypeVariables, OverloadedStrings #-}
{-# LANGUAGE OverloadedStrings #-}
Copy link
Contributor

@diogob diogob Sep 28, 2015

Couldn't we remove this as they are in cabal's default-extensions ?

@diogob
Copy link
Contributor

@diogob diogob commented Sep 28, 2015

Awesome work men!
I can help with tests for the related tables cases if you'd like that.

apiRequest = first formatParserError (parseGetRequest req)
>>= addRelations schema allRelations Nothing
>>= addJoinConditions schema allColumns
where formatParserError = pack.show
Copy link
Member

@begriffs begriffs Sep 30, 2015

Use the general cs function rather than pack since it can figure out what to do.

@begriffs
Copy link
Member

@begriffs begriffs commented Sep 30, 2015

Some of those SQL statements inside PgStructure are works of art. Nice work.

find (\ col -> colSchema col == s && colTable col == t && colName col == c ) allColumns

findTable :: [Table] -> Text -> Text -> Either Text Table
findTable allTables s t = note ("no such table: "<>t) $
Copy link
Member

@begriffs begriffs Sep 30, 2015

I don't see findColumn or findTable used in other parts of the code. Are they here for future convenience?

@ruslantalpa
Copy link
Contributor Author

@ruslantalpa ruslantalpa commented Oct 1, 2015

All suggestions/comments implemented and the latest master merged

)
] (cs $ fromMaybe "[]" body)
else cqs

Copy link
Contributor

@PierreR PierreR Oct 1, 2015

Remove the extra space ?

Copy link
Contributor Author

@ruslantalpa ruslantalpa Oct 1, 2015

I made some changes so i am not sure to what part of the code you are referring to, could you check that part again and see if the spaces are removed?

Copy link
Contributor

@PierreR PierreR Oct 1, 2015

It is here :

https://github.com/ruslantalpa/postgrest/blob/master/src/PostgREST/App.hs#L86

But well it is just a space so don't worry ;-)

@begriffs
Copy link
Member

@begriffs begriffs commented Oct 1, 2015

The child relations bugfix took care of the issue I was seeing locally.

I'm playing with an example database of films, directors, and competitions. This new embedding feature is powerful and fun. I can ask for directors with their films, or even competitions with their films (which goes through a nominations join table). Your branch automatically detects the necessary relations and magically works!

For the error case we should put it into a JSON object that matches the format of the other errors. So rather than

no relation between director and competition

it could be

{
  "hint": "could not find foreign keys between these entities",
  "details": null,
  "code": null,
  "message": "no relation between director and competition"
}

Same for parse errors. Rather than

"failed to parse filter (sdfjsdfjsf)" (line 1, column 1):
unexpected "s"
expecting "not." or operator (eq, gt, ...)

it could be

{
  "hint": "expecting \"not.\" or operator (eq, gt, ...)",
  "details": "unexpected \"s\"",
  "code": null,
  "message": "failed to parse filter (sdfjsdfjsf), (line 1, column 1)"
}

import qualified Data.ByteString.Char8 as BS
import Control.Applicative
import Network.HTTP.Types.Header
import PostgREST.Types ()
Copy link
Member

@begriffs begriffs Oct 1, 2015

Is the types import needed?

Copy link
Contributor Author

@ruslantalpa ruslantalpa Oct 1, 2015

seems so (rangeParse)

Copy link
Member

@begriffs begriffs Oct 1, 2015

Hm, I'm able to compile the project with the import removed.

Ruslan Talpa and others added 4 commits Oct 2, 2015
begriffs added a commit that referenced this issue Oct 4, 2015
Extend the capabilities of PostgREST #280
@begriffs begriffs merged commit 0f1b313 into PostgREST:master Oct 4, 2015
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

4 participants