PostgREST / postgrest

REST API for any Postgres database
https://postgrest.org
MIT License
23.42k stars 1.03k forks source link

After the user does a RPC POST Request, how do I make the server Response send a bunch of JSON back to the user - eg, multiple recordsets? #1006

Closed StefanScott closed 6 years ago

StefanScott commented 7 years ago

I'm trying to figure out how to use RPC to send a bunch of JSON to the server, to POST records to multiple tables - and then I want the server Response to send back a bunch of JSON to the client - possibly with some modifications (some records failed to INSERT, some field values altered due to validation errors, etc.).

For example, if the user attempted to POST an Order Line for a Quantity of Product - where the server determined that this Quantity exceeded the amount of Product currently available "in stock" - then the server Response could include a "Failure" flag - or (even better) the server Response could include all the submitted records, with some fields "adjusted" (or some records totally missing) to indicate the specific failures / validation errors.

I don't understand the following:


I've been reading here:

https://postgrest.com/en/v4.3/api.html#stored-procedures

https://github.com/begriffs/postgrest/pull/986

But I'm not sure how to do this. Ideally, I'd just like to be able to send a bunch of JSON back to the user in the body of the Response to the user's RPC POST request. But I'm unsure about some very basic things regarding how to "wire" all this together:


Example

Consider the following scenario involving a "master-detail" layout, where the user is editing one "parent" record, as well as editing one or more sets of "child" records:

So the HTML layout (eg, using Elm) displays the Customer record at the top - plus two "tabs":


You can see a demo of a similar scenario here:

https://marmelab.com/admin-on-rest-demo/#/customers/11

(Look at the Orders and Reviews tabs - each of which displays a list of "child" records.)


So the user can perform the following:

Regarding when to actually POST the user's changes to the server, I guess there are two options:

By the way, I've been looking at GraphQL (and PostgraphQL) as an alternative way of doing this. It looks nice, but I don't really want to add another layer and another language between my client and my database. Plus I don't plan on writing an API to be consumed by lots of different clients - basically I want to write just one SPA app (Elm frontend), using PostgREST.

I'd feel safer writing my own stored procedures in Postgres to handle these sort of master-detail parent-child multiple-record multiple-table inserts / updates - using the proven ACID / transaction guarantees of Postgres directly - rather than learning GraphQL (and trusting GraphQL to provide the same kinds of guarantees).


So my question is:

I would like to get some useful information back from the server in Response to an RPC POST.

For example, consider a slightly different scenario - now involving a "numerical limitation":

Now the Quantity of Product entered by the user on an Order Line could exceed the "Quantity Available In Stock" for that Product.

After the user does the RPC POST, the server could inform the user of this problem by:


So what I want here is for the server to respond with all the records which the user attempted insert / update via the RPC POST - possibly with some changes.

http://www.sqlines.com/postgresql/how-to/return_result_set_from_stored_procedure

https://www.postgresql.org/docs/9.4/static/functions-json.html

I'm just trying to figure out how to "wire this all together".

Summary

Thanks for any help!

ruslantalpa commented 7 years ago

here is a function that receives a single param (json) and outputs it back as json https://github.com/begriffs/postgrest/blob/master/test/fixtures/schema.sql#L340-L344

here it is being called https://github.com/begriffs/postgrest/blob/master/test/Feature/RpcSpec.hs#L290-L304

the function just needs to return json type, whatever it returns will be sent back to the user