Created
April 6, 2021 09:39
-
-
Save soyuka/57178de88a74de15ead9a6f25a403637 to your computer and use it in GitHub Desktop.
Postgrest JSON-LD support
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Deploy miamp:artists to pg | |
-- requires: appschema | |
BEGIN; | |
SET client_min_messages = 'warning'; | |
CREATE TABLE miamp.artists ( | |
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), | |
name TEXT NOT NULL, | |
-- Musicbrainz link | |
sameAs INET DEFAULT NULL, | |
dateCreated TIMESTAMPTZ NOT NULL DEFAULT now(), | |
dateModified TIMESTAMPTZ NOT NULL DEFAULT now() | |
); | |
CREATE OR REPLACE FUNCTION iri(miamp.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(miamp.artists) RETURNS text AS $$ | |
SELECT 'https://schema.org'; | |
$$ language sql; | |
CREATE OR REPLACE FUNCTION type(miamp.artists) RETURNS text AS $$ | |
SELECT 'MusicGroup'; | |
$$ language sql; | |
COMMIT; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
debug | |
} | |
localhost | |
file_server | |
@resource path_regexp resource ^/api/([a-z_]+)/([a-z0-9-]+)$ | |
@resources path_regexp resources ^/api/([a-z_]+)$ | |
rewrite @resource /api/{re.resource.1}?id=eq.{re.resource.2}&select=*,"%40id":iri,"%40context":context | |
rewrite @resources /api/{re.resources.1}?select=*,"%40id":iri,"%40context":context,"%40type":type | |
handle /api/* { | |
uri strip_prefix /api | |
reverse_proxy postgrest:3000 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment