Constructor
# new Statement(adapter)
Provides a foundation for building and executing database queries
This abstract class implements the query builder pattern for constructing database queries. It supports various query operations like select, from, where, orderBy, groupBy, limit, and offset. It also provides methods for executing queries and handling pagination.
Base class for database query statements
sequenceDiagram
participant Client
participant Statement
participant Adapter
participant Database
Client->>Statement: select()
Client->>Statement: from(Model)
Client->>Statement: where(condition)
Client->>Statement: orderBy([field, direction])
Client->>Statement: limit(value)
Client->>Statement: execute()
Statement->>Statement: build()
Statement->>Adapter: raw(query)
Adapter->>Database: execute query
Database-->>Adapter: return results
Adapter-->>Statement: return processed results
Statement-->>Client: return final results
Parameters:
Name | Type | Description |
---|---|---|
adapter |
Adapter.<any, Q, any, any>
|
The database adapter to use for executing queries |
Example
// Create a statement to query users
const statement = new SQLStatement(adapter);
const users = await statement
.select()
.from(User)
.where(Condition.attribute("status").eq("active"))
.orderBy(["createdAt", "DESC"])
.limit(10)
.execute();
// Use pagination
const paginator = await statement
.select()
.from(User)
.paginate(20); // 20 users per page