Working With Arrays
A guide from Supabase
Apps on Plentiful run on Supabase's open-source database, sign-in, file storage and live updates, so most of this guide applies. The differences: your app reaches its database through its own server — not Supabase's data API (supabase.from(…)) — there are no Edge Functions (your app's server does that work), and scheduled work uses Jobs. Where it says "Supabase dashboard", use your app's Dashboard. Ask the builder when in doubt.
Postgres supports flexible array types. These arrays are also supported in the Supabase Dashboard and in the JavaScript API.
Create a table with an array column#
Create a test table with a text array (an array of strings):
- Go to the Table editor page in the Dashboard.
- Click New Table and create a table with the name
arraytest. - Click Save.
- Click New Column and create a column with the name
textarray, typetext, and select Define as array. - Click Save.
Insert a record with an array value#
- Go to the Table editor page in the Dashboard.
- Select the
arraytesttable. - Click Insert row and add
["Harry", "Larry", "Moe"]. - Click Save.
View the results#
- Go to the Table editor page in the Dashboard.
- Select the
arraytesttable.
You should see:
1| id | textarray |2| --- | ----------------------- |3| 1 | ["Harry","Larry","Moe"] |Query array data#
Postgres uses 1-based indexing (e.g., textarray[1] is the first item in the array).
To select the first item from the array and get the total length of the array:
1SELECT textarray[1], array_length(textarray, 1) FROM arraytest;returns:
1| textarray | array_length |2| --------- | ------------ |3| Harry | 3 |