-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.sql
53 lines (51 loc) · 1.92 KB
/
main.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
-- Default PostgREST OpenAPI Specification
-- This is the one we should be calling
create or replace function callable_root() returns jsonb as $$
-- Calling this every time is inefficient, but it's the best we can do until PostgREST calls it when it updates the server config
CALL set_server_from_configuration();
SELECT postgrest_openapi_spec(
schemas := string_to_array(current_setting('pgrst.db_schemas', TRUE), ','),
postgrest_version := current_setting('pgrst.version', TRUE),
proa_version := '0.1'::text, -- TODO: needs to be updated; put into config, and have Makefile update
document_version := 'unset'::text
);
$$ language sql volatile;
-- This one returns the OpenAPI JSON; instead of calling it directly, call "callable_root", below
create or replace function postgrest_openapi_spec(
schemas text[],
postgrest_version text default null,
proa_version text default null,
document_version text default null
)
returns jsonb language sql stable as
$$
select oas_openapi_object(
openapi := '3.1.0',
info := oas_info_object(
title := sd.title,
summary := sd.summary,
description := sd.description,
-- The document version
version := coalesce(document_version, 'undefined')
),
xsoftware := jsonb_build_array(
-- The version of the OpenAPI extension
oas_x_software_object(
name := 'OpenAPI',
version := proa_version,
description := 'Automatically/dynamically generate an OpenAPI schema for the API generated by PostgREST'
),
-- The version of PostgREST
oas_x_software_object(
name := 'PostgREST API',
version := postgrest_version,
description := 'Automatically/dynamically turns a PostgreSQL database directly into a RESTful API'
)
),
servers := oas_build_servers(),
paths := oas_build_paths(schemas),
components := oas_build_components(schemas),
security := '[{"JWT": []}]'
)
from postgrest_get_schema_description(schemas[1]) sd;
$$;