Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

added new temporary folder: guides2, added array.mdx

Mark Burggraf5c53aa7ac43d
diff --git a/web/docs/guides2/arrays.mdx b/web/docs/guides2/arrays.mdx
new file mode 100644
index 00000000000..f9168738b42
--- /dev/null
+++ b/web/docs/guides2/arrays.mdx
@@ -0,0 +1,212 @@
+---
+id: arrays
+title: "Working With Arrays"
+description: How to use arrays in PostgreSQL and the Supabase API.
+---
+
+import Tabs from '@theme/Tabs';
+import TabItem from '@theme/TabItem';
+
+### How to use arrays in PostgreSQL and the Supabase API.
+
+## Description
+
+PostgreSQL supports flexible [array types](https://www.postgresql.org/docs/12/arrays.html). You can use arrays in your PostgreSQL tables in the Supabase dashboard and in the Javascript API.
+
+## Environment
+
+* Supabase JS Client
+* Supabase (or any PostgreSQL) backend
+* Supabase Dashboard (UI)
+
+
+## Requirements
+* Supabase account or self-hosted Supabase instance
+* Javascript-based client application (Angular, React, Vue, etc.)
+
+
+## Steps
+
+### Create a table with an array column
+
+Create a test table with a text array (an array of strings):
+
+<Tabs
+defaultValue="UI"
+values={[
+ {label: 'UI', value: 'UI'},
+ {label: 'SQL', value: 'SQL'},
+]}>
+
+<TabItem value="UI">
+
+- Table Editor
+- New Table
+- Create a new table named `arraytest` and save the table
+ - New Column
+ - Name the new column `textarray`
+ - Make it type `text`
+ - Check the box labelled `Define as array`
+ - Save the new column
+
+</TabItem>
+
+<TabItem value="SQL">
+
+```sql
+CREATE TABLE arraytest (id integer NOT NULL, textarray text ARRAY);
+```
+
+</TabItem>
+</Tabs>
+
+
+
+
+
+### Insert a record with an array value
+
+
+<Tabs
+defaultValue="UI"
+values={[
+ {label: 'UI', value: 'UI'},
+ {label: 'SQL', value: 'SQL'},
+ {label: 'JS', value: 'JavaScript'},
+]}>
+
+<TabItem value="UI">
+
+- Table Editor
+- Select `arraytest`
+- Insert Row
+ - In the `textarray` field input, paste:
+ - `["Harry", "Larry", "Moe"]`
+ - Save the new row
+
+
+</TabItem>
+
+<TabItem value="SQL">
+
+```sql
+INSERT INTO arraytest (id, textarray) VALUES (1, ARRAY['Harry', 'Larry', 'Moe']);
+```
+
+</TabItem>
+
+<TabItem value="JavaScript">
+To insert a record from the Javascript client:
+
+```js
+const { data, error } = await supabase
+.from('arraytest')
+.insert([
+ { id: 2, textarray: ['one', 'two', 'three', 'four'] }
+]);
+```
+
+</TabItem>
+
+</Tabs>
+
+
+### View the results
+
+
+<Tabs
+defaultValue="UI"
+values={[
+ {label: 'UI', value: 'UI'},
+ {label: 'SQL', value: 'SQL'},
+]}>
+
+<TabItem value="UI">
+
+- Table Editor
+- Select `arraytest`
+
+Your first array data!
+
+| id | textarray |
+| --- | ---------------------------- |
+| 1 | ["Harry","Larry","Moe"] |
+
+</TabItem>
+<TabItem value="SQL">
+
+```sql
+SELECT * FROM arraytest;
+```
+
+Your first array data!
+
+| id | textarray |
+| --- | ---------------------------- |
+| 1 | ["Harry","Larry","Moe"] |
+
+</TabItem>
+</Tabs>
+
+
+### Query Array Data
+
+To query an array, PostgreSQL uses 1-based arrays, so be careful, since you're probably used to 0-based arrays in Javascript.
+
+<Tabs
+defaultValue="UI"
+values={[
+ {label: 'SQL', value: 'SQL'},
+ {label: 'JS', value: 'JavaScript'},
+]}>
+
+<TabItem value="SQL">
+
+To select the first item from the array and get the total length of the array:
+
+```js
+SELECT textarray[1], array_length(textarray, 1) FROM arraytest;
+```
+
+returns:
+
+| textarray | array_length |
+| --------- | ------------ |
+| Harry | 3 |
+
+</TabItem>
+<TabItem value="JavaScript">
+
+This returns the entire array field:
+
+```js
+const { data, error } = await supabase
+.from('arraytest')
+.select('textarray[]');
+console.log(JSON.stringify(data,null,2));
+```
+
+returns:
+
+```js
+[
+ {
+ "textarray": ["Harry", "Larry", "Moe"]
+ }
+]
+```
+
+
+</TabItem>
+
+</Tabs>
+
+## Resources
+
+* [Supabase JS Client](https://github.com/supabase/supabase-js)
+* [Supabase Account - Free Tier OK](https://supabase.io)
+* [PostgreSQL Arrays](https://www.postgresql.org/docs/12/arrays.html)
+
+
+
+