ivangabriele / postgrester

Isomorphic PostgREST API Client for Javascript and Typescript.
Apache License 2.0
50 stars 7 forks source link

Support Prefer: return=representation #163

Closed robertsosinski closed 2 years ago

robertsosinski commented 3 years ago

🚀 Support Prefer: return=representation

When inserting, updating, or deleting records via PostgREST, you can send the Prefer: return=representation header, which will return the object that was written. This is especially helpful when you create new records and need the id that was generated by the database. Currently, the promise returned by post, patch and delete is Promise<void>, but having a return such as Promise<{data: any}> would be great if the representation (or any of the specific columns specified via the query select=id syntax) would be available.

You can see more about the Prefer options here: http://postgrest.org/en/v7.0.0/api.html#insertions-updates

Motivation

Right now if I create a record, I am unable to get database generated columns, such as the id.

Example

(async () => {
  const {data} = await postgrestClient
    .select(["id"])
    .post("/tasks", {"name": "get this working"}, {return: "representation"});

  console.log(data.id);
})();

Pitch

This would make it possible to get return values from PostgREST. Also, when you want to add support for stored procedures via the /rpc path, it would be easy, as you can use the same capabilities to return values from the stored procedure call.

ivangabriele commented 3 years ago

Sorry for the late answer, I will add it today ;)