Select

Fetching data from your SQL database happens with a SELECT statement.

Typically in SQL the syntax would look like the below statement which would return all of the rows from the table, table_name and for each row only return the columns named column_1 and column_2 – but nothing else.

SELECT column_1, column_2 FROM table_name

When using the Outerbase query builder you can accomplish the same SQL statement with the following code.

const { data, error } = db
    .selectFrom([
        { table: 'table_name', columns: ['column_1', 'column_2'] }
    ])
    .query()

Specifying a schema

For some databases, tables are nested within a schema which you can in some ways think of as a folder to help categorize your tables intent. By default many database engines have a root schema named public but you do not need to specify public in your SQL statements as it is implied.

However, if you have custom schemas then it is important to specify a schema before the table. In SQL it might look like the statement below.

SELECT * FROM my_schema.table_name

Because the table lives as a child of my_schema we must specify the parent child relationship in the query so the database engine knows where to find that table specifically.

To accomplish this in the query builder you can do so by specifying the schema in addition to the table name and columns.

const { data, error } = db
    .selectFrom([
        { 
            schema: 'my_schema',
            table: 'table_name',
            columns: ['*']
    ])
    .query()

Last updated