Skip to content
This repository was archived by the owner on Feb 24, 2024. It is now read-only.

Commit 79f2af0

Browse files
committed
Add wording for schema structure
1 parent 7714bd9 commit 79f2af0

File tree

6 files changed

+28
-12
lines changed

6 files changed

+28
-12
lines changed

Diff for: _static/db.png

-918 Bytes
Loading

Diff for: auth.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ PostgREST uses JWT mainly for authentication and authorization purposes and enco
298298
Schema Isolation
299299
================
300300

301-
A PostgREST instance is configured to expose all the tables, views, and stored procedures of a single schema specified in a server configuration file. This means private data or implementation details can go inside a private schema and be invisible to HTTP clients. You can then expose views and stored procedures which insulate the internal details from the outside world. It keeps you code easier to refactor, and provides a natural way to do API versioning. For an example of wrapping a private table with a public view see the :ref:`public_ui` section below.
301+
You can isolate your api schema from internal implementation details, as explained in :ref:`schema_structure`. For an example of wrapping a private table with a public view see the :ref:`public_ui` section below.
302302

303303
SQL User Management
304304
===================

Diff for: best_practices.rst

+13-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11

2-
Schema structure
3-
----------------
2+
.. _schema_structure:
3+
4+
Schema Structure
5+
================
6+
7+
A PostgREST instance exposes all the tables, views, and stored procedures of a single `PostgreSQL schema <https://www.postgresql.org/docs/12/ddl-schemas.html>`_ (a namespace of database objects). This means private data or implementation details can go inside different private schemas and be invisible to HTTP clients.
8+
9+
It is recommended that you don't expose tables on your API schema. Instead expose views and stored procedures which insulate the internal details from the outside world.
10+
This allows you to change the internals of your schema and maintain backwards compatibility. It also keeps your code easier to refactor, and provides a natural way to do API versioning.
411

512
.. image:: _static/db.png
6-
:align: center
713

814
.. _func_privs:
915

1016
Function privileges
11-
-------------------
17+
===================
1218

1319
By default, when a function is created, the privilege to execute it is not restricted by role. The function access is PUBLIC—executable by all roles(more details at `PostgreSQL Privileges page <https://www.postgresql.org/docs/12/ddl-priv.html>`_). This is not ideal for an API schema. To disable this behavior, you can run the following SQL statement:
1420

@@ -17,7 +23,7 @@ By default, when a function is created, the privilege to execute it is not restr
1723
-- Assuming your schema is named "api"
1824
ALTER DEFAULT PRIVILEGES IN SCHEMA api REVOKE EXECUTE ON FUNCTIONS FROM PUBLIC;
1925
20-
-- Or to stop functions from being executable in the whole database(note the removal of the IN SCHEMA part).
26+
-- Or to stop functions from being PUBLICly executable in the whole database
2127
ALTER DEFAULT PRIVILEGES REVOKE EXECUTE ON FUNCTIONS FROM PUBLIC;
2228
2329
See `PostgreSQL alter default privileges <https://www.postgresql.org/docs/current/static/sql-alterdefaultprivileges.html>`_ for more details.
@@ -40,7 +46,7 @@ By default, a function is executed with the privileges of the user who calls it.
4046
Another option is to define the function with the :code:`SECURITY DEFINER` option. Then only one permission check will take place, the permission to call the function, and the operations in the function will have the authority of the user who owns the function itself. See `PostgreSQL documentation <https://www.postgresql.org/docs/current/static/sql-createfunction.html#SQL-CREATEFUNCTION-SECURITY>`_ for more details.
4147

4248
Views with RLS
43-
--------------
49+
==============
4450

4551
Views are invoked with the privileges of the view owner, much like stored procedures with the ``SECURITY DEFINER`` option. When created by a SUPERUSER role, all `row-level security <https://www.postgresql.org/docs/current/static/ddl-rowsecurity.html>`_ will be bypassed unless a different, non-SUPERUSER owner is specified.
4652

@@ -53,7 +59,7 @@ Views are invoked with the privileges of the view owner, much like stored proced
5359
ALTER VIEW sample_view OWNER TO api_views_owner;
5460
5561
Views with Rules
56-
----------------
62+
================
5763

5864
Insertion on VIEWs with complex `RULEs <https://www.postgresql.org/docs/11/sql-createrule.html>`_ might not work out of the box with PostgREST.
5965
It's recommended that you `use triggers instead of RULEs <https://wiki.postgresql.org/wiki/Don%27t_Do_This#Don.27t_use_rules>`_.

Diff for: diagrams/README.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ The schema structure diagram is done with LaTeX. You can use a GUI like https://
1515
Then use this command to generate the png file.
1616

1717
```bash
18-
pdflatex --shell-escape -halt-on-error -output-directory ../_static db.tex
18+
pdflatex --shell-escape -halt-on-error db.tex
1919

2020
## and move it to the static folder(it's not easy to do it in one go with the pdflatex)
2121
mv db.png ../_static/
@@ -28,3 +28,13 @@ You can install the full latex suite with `nix`:
2828
```
2929
nix-env -iA texlive.combined.scheme-full
3030
```
31+
32+
To tweak the file with a live reload environment use:
33+
34+
```bash
35+
# open the pdf(zathura used as an example)
36+
zathura db.pdf &
37+
38+
# live reload with entr
39+
echo db.tex | entr pdflatex --shell-escape -halt-on-error db.tex
40+
```

Diff for: diagrams/db.png

9.02 KB
Loading

Diff for: diagrams/db.tex

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
\begin{document}
2121

22-
\newcommand\customScale{0.4}
22+
\newcommand\customScale{0.35}
2323

2424
\begin{tikzpicture}[x=0.75pt,y=0.75pt,yscale=-1,xscale=1, scale=\customScale, every node/.style={scale=\customScale}]
2525

@@ -57,11 +57,11 @@
5757
% Text Node
5858
\draw (322,178) node [anchor=north west][inner sep=0.75pt] [color={rgb, 255:red, 255; green, 255; blue, 255 } ,opacity=1 ] [align=left] {\large\textbf{api}};
5959
% Text Node
60-
\draw (202,97) node [anchor=north west][inner sep=0.75pt] [color={rgb, 255:red, 255; green, 255; blue, 255 } ,opacity=1 ] [align=left] {\large\textbf{auth}};
60+
\draw (190,97) node [anchor=north west][inner sep=0.75pt] [color={rgb, 255:red, 255; green, 255; blue, 255 } ,opacity=1 ] [align=left] {\large\textbf{internal}};
6161
% Text Node
6262
\draw (300,99) node [anchor=north west][inner sep=0.75pt] [color={rgb, 255:red, 255; green, 255; blue, 255 } ,opacity=1 ] [align=left] {\large\textbf{private}};
6363
% Text Node
64-
\draw (414,97) node [anchor=north west][inner sep=0.75pt] [color={rgb, 255:red, 255; green, 255; blue, 255 } ,opacity=1 ] [align=left] {\large\textbf{other}};
64+
\draw (417,101) node [anchor=north west][inner sep=0.75pt] [color={rgb, 255:red, 255; green, 255; blue, 255 } ,opacity=1 ] [align=left] {\large\textbf{core}};
6565
% Text Node
6666
\draw (358,306) node [anchor=north west][inner sep=0.75pt] [align=left] {REST};
6767

0 commit comments

Comments
 (0)