Query Builder
The QueryBuilder class provides a fluent interface for constructing SQL queries in a type-safe manner. It supports various SQL operations.
Utility is still in beta, you can report issues on my github.
SELECT
SELECT
The SELECT
operation retrieves data from one or more tables.
Chaining Methods
select(...columns: string[])
: Specifies the columns to select.from(table: string)
: Specifies the table to select from.where(column: string, value: any)
: Adds a WHERE clause.sort(column: string, direction: "ASC" | "DESC")
: Adds an ORDER BY clause.limit(limit: number)
: Adds a LIMIT clause.offset(offset: number)
: Adds an OFFSET clause.join(type: string, table: string, on: string)
: Adds a JOIN clause.distinct()
: Adds a DISTINCT clause.count(column: string)
: Adds a COUNT aggregate function.sum(column: string)
: Adds a SUM aggregate function.avg(column: string)
: Adds an AVG aggregate function.min(column: string)
: Adds a MIN aggregate function.max(column: string)
: Adds a MAX aggregate function.
INSERT
INSERT
The INSERT
operation adds new records to a table.
Chaining Methods
insert(...columns: string[])
: Specifies the columns to insert.into(table: string)
: Specifies the table to insert into.values(...values: any[])
: Specifies the values to insert.
UPDATE
UPDATE
The UPDATE
operation modifies existing records in a table.
Chaining Methods
update(table: string)
: Specifies the table to update.set(column: string, value: any)
: Adds a SET clause.where(column: string, value: any)
: Adds a WHERE clause.
DELETE
DELETE
The DELETE
operation removes records from a table.
Chaining Methods
delete()
: Sets the operation to DELETE.from(table: string)
: Specifies the table to delete from.where(column: string, value: any)
: Adds a WHERE clause.
REPLACE
REPLACE
The REPLACE
operation inserts a new record or replaces an existing one.
Chaining Methods
replace(...columns: string[])
: Specifies the columns to replace.in(table: string)
: Specifies the table to replace into.with(...values: any[])
: Specifies the values to replace.
Executing the query
Executing the query
This can be implemented using any database library. You can see the example below of how you can implement it with my Remote MySQL.
Last updated