Skip to main content

Modifiers

Modifiers are used to change the format of the response, unlike filters, which operate above the row level.

Filters only return rows that match specific conditions without changing the shape of the rows, while modifiers allow changing the format of the response.

select

By default, .insert() does not return the inserted rows. By calling this method, the inserted rows will be returned in the data.

Warning: The .select() method returns the inserted rows only when the table has a single primary key and that primary key is of auto-increment type.

Parameters

ParameterTypeRequiredDescription
columnsstringNoColumns to retrieve, separated by commas

Code Example

// Perform an insert operation in the articles table and return the modified full record
const { data, error } = await db
.from("articles")
.insert({ id: 1, title: "Tencent CloudBase New Feature" })
.select();

order

Sort the query results.

This method can be called multiple times to sort by multiple columns.

The referenced table can be sorted, but it will affect the sorting of the parent table only when !inner is used in the query.

Parameters

ParameterTypeRequiredDescription
columnstringYesColumn to sort
optionsobjectNoNamed parameters

options Parameter Details

ParameterTypeRequiredDescription
ascendingbooleanNoIf true, the results will be sorted in ascending order
nullsFirstbooleanNoIf true, null values will appear first. If false, null values will appear last
referencedTablestringNoSet to sort by the columns of the referenced table

Code Example

// Sort articles by publication time in descending order
const { data, error } = await db
.from("articles")
.select("id, title, published_at")
.order("published_at", { ascending: false });

Sort Referenced Table

// Sort the referenced table categories by name in descending order
const { data, error } = await db
.from("articles")
.select(`
title,
categories (
name
)
`)
.order("name", { referencedTable: "categories", ascending: false });
// Sort by the name column of the referenced table category in ascending order
const { data, error } = await db
.from("articles")
.select(`
title,
category:categories (
name
)
`)
.order("category(name)", { ascending: true });

limit

Limit the number of rows returned.

Parameters

ParameterTypeRequiredDescription
countnumberYesMaximum number of rows to return
optionsobjectNoNamed parameters

options Parameter Details

ParameterTypeRequiredDescription
referencedTablestringNoSet to limit rows of the referenced table instead of the parent table

Code Example

// Limit to returning 5 articles
const { data, error } = await db.from("articles").select("title").limit(5);

Limit Rows of Referenced Table

// Only return 3 categories per article
const { data, error } = await db
.from("articles")
.select(`
title,
categories (
name
)
`)
.limit(3, { referencedTable: "categories" });

range

Limit the range of query results.

Limits query results by starting from offset from to to. Only records within this range will be returned.

This follows the query order. If there is no order clause, the range behavior may be unpredictable.

The from and to values are 0-based and inclusive: range(1, 3) will include the second, third, and fourth rows of the query.

Parameters

ParameterTypeRequiredDescription
fromnumberYesStarting index of the range
tonumberYesEnding index of the range
optionsobjectYesNamed parameters

options Parameter Details

ParameterTypeRequiredDescription
referencedTablestringNoSet to limit rows of the referenced table instead of the parent table

Code Example

// Get records 1-2 from the article list (inclusive)
const { data, error } = await db.from("articles").select("title").range(0, 1);

Range on Referenced Table

// Only return the first 2 categories per article (index 0-1)
const { data, error } = await db
.from("articles")
.select(`
title,
categories (
name
)
`)
.range(0, 1, { referencedTable: "categories" });

abortSignal

Note: This method is only available in Web environments.

Set an AbortSignal for the fetch request.

This feature can be used to set a timeout for requests.

Parameters

ParameterTypeRequiredDescription
signalAbortSignalYesAbortSignal for the fetch request

Code Example

// Manually abort a request using AbortController
const ac = new AbortController();
ac.abort();
const { data, error } = await db
.from("articles")
.select()
.abortSignal(ac.signal);
// Set a 1-second timeout using AbortSignal.timeout
const { data, error } = await db
.from("articles")
.select()
.abortSignal(AbortSignal.timeout(1000));

single

Retrieve a single row of data.

Returns data as a single object instead of an array of objects.

The query result must contain only one row (e.g., by using .limit(1)), otherwise this method will return an error.

Code Example

// Get the title of the first article
const { data, error } = await db
.from("articles")
.select("title")
.limit(1)
.single();

maybeSingle

Retrieve zero or one row of data.

Returns data as a single object instead of an array of objects.

The query result must contain zero or one row (e.g., by using .limit(1)), otherwise this method will return an error.

Code Example

// Find an article by title, which may not exist
const { data, error } = await db
.from("articles")
.select()
.eq("title", "Tencent CloudBase New Feature")
.maybeSingle();

overrideTypes

Partially override or replace the type of a successful response.

Overrides the return type of the data field in the response, useful for type-safe query result transformations.

Parameters

ParameterTypeRequiredDescription
typeTYesType to override
optionsobjectNoOptions object

options Parameter Details

ParameterTypeRequiredDescription
mergebooleanNoIf false, completely replaces the type instead of merging

Code Examples

Fully Override Array Type

// Fully override the response data to a custom array type, merge: false means full replacement instead of merging
const { data } = await db
.from("articles")
.select()
.overrideTypes<Array<MyType>, { merge: false }>();

Fully Override Object Type

// Used with maybeSingle to fully override a single object response to a custom type
const { data } = await db
.from("articles")
.select()
.maybeSingle()
.overrideTypes<MyType, { merge: false }>();

Partially Override Array Type

// Partially override array element types, specifying only the field types that need to change (e.g., status field)
const { data } = await db
.from("articles")
.select()
.overrideTypes<Array<{ status: "A" | "B" }>>();

Partially Override Object Type

// Partially override a single object type, specifying only the field types that need to change (e.g., status field)
const { data } = await db
.from("articles")
.select()
.maybeSingle()
.overrideTypes<{ status: "A" | "B" }>();