From 3af6df743bf97bf8c19ac190a39a571200f6d2c8 Mon Sep 17 00:00:00 2001 From: steve-chavez Date: Tue, 21 Apr 2020 14:38:31 -0500 Subject: [PATCH] Reorder calling func with array --- api.rst | 62 +++++++++++++++++++++------------------------------ ecosystem.rst | 4 ++-- 2 files changed, 27 insertions(+), 39 deletions(-) diff --git a/api.rst b/api.rst index 3a0a4da11..350f77123 100644 --- a/api.rst +++ b/api.rst @@ -1179,46 +1179,51 @@ You can also call a function that takes a single parameter of type json by sendi 8 +.. _s_procs_array: + Calling functions with array parameters --------------------------------------- You can call a function that takes an array parameter: -.. code-block:: plpgsql +.. code-block:: postgres - CREATE FUNCTION native_array_func(arr int[]) RETURNS int[] as $$ - SELECT arr; - $$ LANGUAGE SQL; + create function plus_one(arr int[]) returns int[] as $$ + SELECT array_agg(n + 1) FROM unnest($1) AS n; + $$ language sql; .. code-block:: http - POST /rpc/native_array_func HTTP/1.1 + POST /rpc/plus_one HTTP/1.1 + Content-Type: application/json - { "arg": [1,2,3] } + {"arr": [1,2,3,4]} - [1,2,3] +.. code-block:: json + + [2,3,4,5] + +For calling the function with GET, you can pass the array as an `array literal `_, +as in ``{1,2,3,4}``. Note that the curly brackets have to be urlencoded(``{`` is ``%7B`` and ``}`` is ``%7D``). + +.. code-block:: http + + GET /rpc/plus_one?arr=%7B1,2,3,4%7D' HTTP/1.1 + + [2,3,4,5] .. note:: - For versions prior to PostgreSQL 10, to pass a PostgreSQL native array you need to quote it as a string: + For versions prior to PostgreSQL 10, to pass a PostgreSQL native array on a POST payload, you need to quote it and use an array literal: .. code-block:: http - POST /rpc/native_array_func HTTP/1.1 + POST /rpc/plus_one HTTP/1.1 - { "arg": "{1,2,3}" } + { "arr": "{1,2,3,4}" } In these versions we recommend using function parameters of type json to accept arrays from the client. -For calling it with GET, you can pass the array as an `array literal `_; -as in ``{1,2,3}``. Note that the curly brackets have to be urlencoded(``{`` is ``%7B`` and ``}`` is ``%7D``). - -.. code-block:: http - - GET /rpc/native_array_func?arr=%7B1,2,3%7D' HTTP/1.1 - - [1,2,3] - Scalar functions ---------------- @@ -1262,24 +1267,7 @@ It's possible to call a function in a bulk way, analoguosly to :ref:`bulk_insert [ 3, 7 ] -If you have large payloads to process, it's preferrable you instead use a function with an array or json parameter, as this will be more efficient. - -.. code-block:: postgres - - create function plus_one(arr int[]) returns int[] as $$ - SELECT array_agg(n + 1) FROM unnest($1) AS n; - $$ language sql; - -.. code-block:: http - - POST /rpc/plus_one HTTP/1.1 - Content-Type: application/json - - {"arr": [1,2,3,4]} - -.. code-block:: json - - [2,3,4,5] +If you have large payloads to process, it's preferrable you instead use a function with an :ref:`array parameter ` or json parameter, as this will be more efficient. It's also possible to :ref:`Specify Columns ` on functions calls. diff --git a/ecosystem.rst b/ecosystem.rst index feb09410e..0a717478b 100644 --- a/ecosystem.rst +++ b/ecosystem.rst @@ -6,11 +6,11 @@ Community Tutorials * `Building a Contacts List with PostgREST and Vue.js `_ - In this video series, DigitalOcean shows how to build and deploy an Nginx + PostgREST(using a managed PostgreSQL database) + Vue.js webapp in an Ubuntu server droplet. +* `PostgREST + Auth0: Create REST API in mintutes and add social login using Auth0 `_ - A step-by-step tutorial to show how to Dockerize and integrate Auth0 to PostgREST service. + * `PostgREST + PostGIS API tutorial in 5 minutes `_ - In this tutorial, GIS • OPS shows how to perform PostGIS calculations through PostgREST :ref:`s_procs` interface. -* `PostgREST + Auth0: Create REST API in mintutes and add social login using Auth0 `_ - A step-by-step tutorial to show how to Dockerize and integrate Auth0 to PostgREST service. - .. _eco_example_apps: Example Apps