Single table
Fetching data from your database via the query builder has a lot of similarities to writing plain SQL.
Basic query
To get data from a single table in the most simple way you can just call for the specified columns from a table using the selectFrom
function chain, followed by the .query()
to execute the network call.
const { data, error } = await db
.selectFrom([
{
table: 'table_name',
columns: ['column_name']
}
])
.query()
Schema
For more complex database structures you may need to specify the schema
of the table you're trying to reference if it's not in the public schema.
NOTE: If the table is in the public schema, or the database does not support schemas then the schema
field is not required.
const { data, error } = await db
.selectFrom([
{
schema: 'custom_schema',
table: 'table_name',
columns: ['column_name'],
}
])
.query()
Specifying columns
To request specific columns, just specify them in an array for each particular table you're accessing.
const { data, error } = await db
.selectFrom([
{
schema: 'custom_schema',
table: 'table_name',
columns: ['id', 'first_name', 'last_name'],
}
])
.query()
Requesting all columns
Similar to SQL, if you want to request all columns from a table you can use the *
character to indicate all columns.
const { data, error } = await db
.selectFrom([
{
schema: 'custom_schema',
table: 'table_name',
columns: ['*'],
}
])
.query()
Last updated