Skip to content
Oxford Harrison edited this page Nov 17, 2024 · 25 revisions

DOCSAPITable API


Programmatically perform a SELECT query.

See related ➞ SELECT

Syntax

table.select(
    fields?: string | Field[],
    clauses?: SelectClauses | Callback,
): Promise<QueryResult>;
table.select(
    arg?: {
        fields?: string | Field[];
    } & SelectClauses | Callback
): Promise<QueryResult>;
Param Interfaces Description
fields? Field Optional list of fields to select. Defaults to all fields if omitted.
clauses? SelectClauses Optional additional query clauses. Can be Callback—a callback function that recieves the underlying SelectStatement instance for manipulation.
arg? Field
SelectClauses
Optional argument for a single-parameter call pattern. Can be Callback—a callback function that recieves the underlying SelectStatement instance for manipulation.

SelectClauses

interface SelectClauses {
    where?: WhereClause;
    orderBy?: OrderByClause;
    limit?: LimitClause;
    offset?: OffsetClause;
    leftJoin?: LeftJoinClause;
    rightJoin?: RightJoinClause;
    innerJoin?: InnerJoinClause;
    crossJoin?: CrossJoinClause;
    fullJoin?: FullJoinClause;
    joins?: (LeftJoinClause | RightJoinClause | InnerJoinClause | CrossJoinClause | FullJoinClause)[];
    groupBy?: GroupByClause;
    having?: HavingClause;
    window?: WindowClause;
}
Param Interfaces Description
where? WhereClause An optional WHERE clause.
orderBy? OrderByClause An optional ORDER BY clause.
limit? LimitClause An optional LIMIT clause.
offset? OffsetClause An optional OFFSET clause.
leftJoin? LeftJoinClause An optional LEFT JOIN clause.
rightJoin? RightJoinClause An optional RIGHT JOIN clause.
innerJoin? InnerJoinClause An optional INNER JOIN clause.
crossJoin? CrossJoinClause An optional CROSS JOIN clause.
fullJoin? FullJoinClause An optional FULL JOIN clause.
joins? LeftJoinClause
RightJoinClause
InnerJoinClause
CrossJoinClause
FullJoinClause
An optional list of JOIN clauses.
groupBy? GroupByClause An optional GROUP BY clause.
having? HavingClause An optional HAVING clause.
window? WindowClause An optional WINDOW clause.

QueryResult

type QueryResult = Array<object> | object;
Type Interfaces Description
Array<object> - An array of objects—the default query result.
object - A single object—the result of a find-one query.

Usage

Call patterns

Two-parameter call pattern:

// Fields list and query limit passed distinctly
const result = await table.select(['first_name', 'last_name', 'email'], { limit: 4 });

Single-parameter call pattern:

// Query limit only
const result = await table.select('first_name');
// Fields list only
const result = await table.select(['first_name', 'last_name', 'email']);
// Fields list and query limit passed together
const result = await table.select({
    fields: ['first_name', 'last_name', 'email'],
    limit: 4
});

Zero-parameter call pattern:

// A SELECT * query
const result = await table.select();

Find one

Find by ID—with actual ID name automatically figured by Linked QL:

/**
 * SELECT *
 * WHERE automatically_figured_primary_key_name = 4
 */
const result = await table.select(
    { where: 4 }
);
console.log(result); // single result object

Multi-dimensional query

See also ➞ Magic Paths

Select a multi-dimensional record:

// Structured relational query | MANY-TO-ONE
// DESC: Tie-in structure from the users table
const result = await table.select(
    [ 'title', 'content', { expr: {
        rpath: ['author', { fields: ['name', 'email'] }]
    }, as: 'author' } ],
    { where: { eq: [
        { path: ['author', '~>', 'role'] },
        { binding: ['admin'] }
    ] } }
);

See ➞ Magic Paths ➞ Example 2

Clone this wiki locally