Skip to content

Instantly share code, notes, and snippets.

@soyuka
Created April 6, 2021 09:39
Show Gist options
  • Save soyuka/57178de88a74de15ead9a6f25a403637 to your computer and use it in GitHub Desktop.
Save soyuka/57178de88a74de15ead9a6f25a403637 to your computer and use it in GitHub Desktop.
Postgrest JSON-LD support
-- 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;
{
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