Provide a way to modify the JSON response through an SQL function (hypermedia, json-ld, json api) #1790
-
Hi, I want to come back over the Json-LD and hypermedia related stuff we already talked about in:
Following @ruslantalpa recommendations on the topic, I attempted to implement JSON-LD over Postgrest and it is indeed possible (not without some drawbacks). First you need to rewrite some paths (using nginx, or Caddy):
Then, you'd need to add custom functions for CREATE OR REPLACE FUNCTION iri(schema.artists) RETURNS text AS
$$
declare iri text;
begin
perform set_config('response.headers',
'[{"Content-Type": "application/ld+json"}]', true);
select '/artists/' || $1.id into iri;
return(iri);
end
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION context(schema.artists) RETURNS text AS $$
SELECT 'https://schema.org';
$$ language sql;
CREATE OR REPLACE FUNCTION type(schema.artists) RETURNS text AS $$
SELECT 'MusicGroup';
$$ language sql; However, this adds lots of functions to maintain if you want to implement it on most of your tables. Especially if, continuing like this I wanted to add the Hydra Core Vocabulary spec. I was thinking of a procedure call, just like you added for create or replace function jsonld(data JSON, table_name regclass) returns JSON as $$
declare
accept text := current_setting('request.header.accept', true);
begin
if accept similar to '%application/json+ld%' then
perform set_config('response.headers',
'[{"Content-Type": "application/ld+json"}]', true);
end if;
-- extend json somehow adding @id, @type etc.
end; $$ language plpgsql; Opinions? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
We are discussing content negotiation and returning custom mimetypes with generic handlers here: #1582 (comment). You might need to read the whole thread and related issues for context. This boils down to allow you to add generic handler for |
Beta Was this translation helpful? Give feedback.
We are discussing content negotiation and returning custom mimetypes with generic handlers here: #1582 (comment). You might need to read the whole thread and related issues for context.
This boils down to allow you to add generic handler for
application/ld+json
as an aggregate function, which can then be used at all endpoints. However, we're not close, yet, to implementing that. Might take a while.