mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +00:00
## Problem
Three code blocks in the docs guides use the wrong language tag, so they
are highlighted and type-checked as JavaScript or TypeScript instead of
what they are:
- `guides/database/arrays.mdx`: a SQL query is tagged `js`
- `guides/database/import-data.mdx`: the `pgloader config.load` command
is tagged `jsx`
- `guides/ai/hybrid-search.mdx`: a cURL command is tagged `tsx`
The docs style guide says JavaScript and TypeScript blocks get type
hints and are formatted by Prettier, which does not apply to these
blocks.
## Solution
Retag the blocks: `sql` for the query and `bash` for the two shell
commands (`bash` is the most common shell tag in the guides).
For the SQL block I also lowercased the keywords, per the style guide's
"Prefer lowercase for SQL". Prettier's SQL plugin rewrites
`textarray[1]` to `textarray as [1]`, which is not valid Postgres, so
the block is preceded by `{/* prettier-ignore */}`, the same convention
already used in `guides/ai/engineering-for-scale.mdx` and
`guides/api/query-plans.mdx`.
Earlier PRs fixed the same kind of problem: #11870, #19033, #14856,
#11235.
## Review instructions
1. Open the diff: only the language tags change, plus the lowercase SQL
and the `prettier-ignore` comment in `arrays.mdx`.
2. Check the three pages in the preview: the SQL block in the SQL tab of
"Working with arrays", the `pgloader` step in "Import data into
Supabase", and the cURL block in "Hybrid search".
3. Run `pnpm test:prettier`. It passes for these files with the repo's
`prettier.config.mjs`.
## Checklist
- [x] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [ ] If I wrote a new docs topic or edited an existing topic, I used
the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs
[style
guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide).
(Not run: this edit only changes code fence languages, and I applied the
style guide's code block rules by hand.)
Co-authored-by: Santhosh-x23 <185920429+Santhosh-x23@users.noreply.github.com>
282 lines
5.1 KiB
Plaintext
282 lines
5.1 KiB
Plaintext
---
|
|
id: 'arrays'
|
|
title: 'Working With Arrays'
|
|
description: 'How to use arrays in Postgres and the Supabase API.'
|
|
---
|
|
|
|
Postgres supports flexible [array types](https://www.postgresql.org/docs/12/arrays.html). 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):
|
|
|
|
<Tabs
|
|
scrollable
|
|
size="small"
|
|
type="underlined"
|
|
defaultActiveId="dashboard"
|
|
queryGroup="database-method"
|
|
>
|
|
<TabPanel id="dashboard" label="Dashboard">
|
|
|
|
1. Go to the [Table editor](/dashboard/project/_/editor) page in the Dashboard.
|
|
1. Click **New Table** and create a table with the name `arraytest`.
|
|
1. Click **Save**.
|
|
1. Click **New Column** and create a column with the name `textarray`, type `text`, and select **Define as array**.
|
|
1. Click **Save**.
|
|
|
|
</TabPanel>
|
|
<TabPanel id="sql" label="SQL">
|
|
|
|
```sql
|
|
create table arraytest (
|
|
id integer not null,
|
|
textarray text array
|
|
);
|
|
```
|
|
|
|
</TabPanel>
|
|
</Tabs>
|
|
|
|
## Insert a record with an array value
|
|
|
|
<Tabs
|
|
scrollable
|
|
size="small"
|
|
type="underlined"
|
|
defaultActiveId="dashboard"
|
|
queryGroup="database-method"
|
|
>
|
|
<TabPanel id="dashboard" label="Dashboard">
|
|
|
|
1. Go to the [Table editor](/dashboard/project/_/editor) page in the Dashboard.
|
|
1. Select the `arraytest` table.
|
|
1. Click **Insert row** and add `["Harry", "Larry", "Moe"]`.
|
|
1. Click **Save.**
|
|
|
|
</TabPanel>
|
|
<TabPanel id="sql" label="SQL">
|
|
|
|
```sql
|
|
INSERT INTO arraytest (id, textarray) VALUES (1, ARRAY['Harry', 'Larry', 'Moe']);
|
|
```
|
|
|
|
</TabPanel>
|
|
<TabPanel id="js" label="JavaScript">
|
|
|
|
Insert a record from the JavaScript client:
|
|
|
|
```js
|
|
const { data, error } = await supabase
|
|
.from('arraytest')
|
|
.insert([{ id: 2, textarray: ['one', 'two', 'three', 'four'] }])
|
|
```
|
|
|
|
</TabPanel>
|
|
<$Show if="sdk:swift">
|
|
<TabPanel id="swift" label="Swift">
|
|
|
|
Insert a record from the Swift client:
|
|
|
|
```swift
|
|
struct ArrayTest: Encodable {
|
|
let id: Int
|
|
let textarray: [String]
|
|
}
|
|
|
|
try await supabase
|
|
.from("arraytest")
|
|
.insert(
|
|
[
|
|
ArrayTest(
|
|
id: 2,
|
|
textarray: ["one", "two", "three", "four"]
|
|
)
|
|
]
|
|
)
|
|
.execute()
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<$Show if="sdk:python">
|
|
<TabPanel id="python" label="Python">
|
|
|
|
Insert a record from the Python client:
|
|
|
|
```python
|
|
supabase.from_('arraytest').insert(
|
|
[
|
|
{
|
|
id: 2,
|
|
textarray: ["one", "two", "three", "four"]
|
|
}
|
|
]
|
|
)
|
|
.execute()
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<$Show if="sdk:csharp">
|
|
<TabPanel id="csharp" label="C#">
|
|
|
|
Insert a record from the C# client:
|
|
|
|
```c#
|
|
[Table("arraytest")]
|
|
class ArrayTest : BaseModel
|
|
{
|
|
[PrimaryKey("id", false)]
|
|
public int Id { get; set; }
|
|
|
|
[Column("textarray")]
|
|
public List<string> Textarray { get; set; }
|
|
}
|
|
|
|
await supabase
|
|
.From<ArrayTest>()
|
|
.Insert(new ArrayTest { Id = 2, Textarray = new List<string> { "one", "two", "three", "four" } });
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
</Tabs>
|
|
|
|
## View the results
|
|
|
|
<Tabs
|
|
scrollable
|
|
size="small"
|
|
type="underlined"
|
|
defaultActiveId="dashboard"
|
|
queryGroup="database-method"
|
|
>
|
|
<TabPanel id="dashboard" label="Dashboard">
|
|
|
|
1. Go to the [Table editor](/dashboard/project/_/editor) page in the Dashboard.
|
|
1. Select the `arraytest` table.
|
|
|
|
You should see:
|
|
|
|
```
|
|
| id | textarray |
|
|
| --- | ----------------------- |
|
|
| 1 | ["Harry","Larry","Moe"] |
|
|
```
|
|
|
|
</TabPanel>
|
|
<TabPanel id="sql" label="SQL">
|
|
|
|
```sql
|
|
select * from arraytest;
|
|
```
|
|
|
|
You should see:
|
|
|
|
```
|
|
| id | textarray |
|
|
| --- | ----------------------- |
|
|
| 1 | ["Harry","Larry","Moe"] |
|
|
```
|
|
|
|
</TabPanel>
|
|
</Tabs>
|
|
|
|
## Query array data
|
|
|
|
Postgres uses 1-based indexing (e.g., `textarray[1]` is the first item in the array).
|
|
|
|
<Tabs
|
|
scrollable
|
|
size="small"
|
|
type="underlined"
|
|
defaultActiveId="sql"
|
|
queryGroup="language"
|
|
>
|
|
<TabPanel id="sql" label="SQL">
|
|
|
|
To select the first item from the array and get the total length of the array:
|
|
|
|
{/* prettier-ignore */}
|
|
```sql
|
|
select textarray[1], array_length(textarray, 1) from arraytest;
|
|
```
|
|
|
|
returns:
|
|
|
|
```
|
|
| textarray | array_length |
|
|
| --------- | ------------ |
|
|
| Harry | 3 |
|
|
```
|
|
|
|
</TabPanel>
|
|
<TabPanel id="js" label="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:
|
|
|
|
```json
|
|
[
|
|
{
|
|
"textarray": ["Harry", "Larry", "Moe"]
|
|
}
|
|
]
|
|
```
|
|
|
|
</TabPanel>
|
|
<$Show if="sdk:swift">
|
|
<TabPanel id="swift" label="Swift">
|
|
|
|
This returns the entire array field:
|
|
|
|
```swift
|
|
struct Response: Decodable {
|
|
let textarray: [String]
|
|
}
|
|
|
|
let response: [Response] = try await supabase.from("arraytest").select("textarray").execute().value
|
|
dump(response)
|
|
```
|
|
|
|
returns:
|
|
|
|
```
|
|
[
|
|
Response(
|
|
textarray: ["Harry", "Larry", "Moe"],
|
|
)
|
|
]
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<$Show if="sdk:csharp">
|
|
<TabPanel id="csharp" label="C#">
|
|
|
|
This returns the entire array field:
|
|
|
|
```c#
|
|
var result = await supabase
|
|
.From<ArrayTest>()
|
|
.Select(x => new object[] { x.Textarray })
|
|
.Get();
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
</Tabs>
|
|
|
|
## Resources
|
|
|
|
- [Supabase JS Client](https://github.com/supabase/supabase-js)
|
|
- [Supabase - Get started for free](https://supabase.com)
|
|
- [Postgres Arrays](https://www.postgresql.org/docs/15/arrays.html)
|