Query Builder

The QueryBuilder class provides a fluent interface for constructing SQL queries in a type-safe manner. It supports various SQL operations.

triangle-exclamation
chevron-rightComplete list of methodshashtag
  • select(...columns: string[])

  • selectAll()

  • from(table: string)

  • where(column: string, value: any)

  • insert(...columns: string[])

  • into(table: string)

  • values(...values: any[])

  • update(table: string)

  • set(column: string, value: any)

  • delete()

  • replace(...columns: string[])

  • in(table: string)

  • with(...values: any[])

  • sort(column: string, direction: "ASC" | "DESC" = "ASC")

  • upper(column: string)

  • lower(column: string)

  • concat(column: string, ...values: string[])

  • curdate()

  • now()

  • dateFormat(column: string, format: string)

  • day(column: string)

  • month(column: string)

  • year(column: string)

  • abs(column: string)

  • round(column: string, decimals: number)

  • floor(column: string)

  • ceil(column: string)

  • pow(column: string, exponent: number)

  • sqrt(column: string)

  • groupBy(...columns: string[])

  • having(column: string, value: any)

  • limit(limit: number)

  • offset(offset: number)

  • join(type: string, table: string, on: string)

  • distinct()

  • count(column: string)

  • sum(column: string)

  • avg(column: string)

  • min(column: string)

  • max(column: string)

  • build()

  • destruct()

  • run(callback: (query: string, params: any[]) => Promise<any>)

SELECT

The SELECT operation retrieves data from one or more tables.

const query = new QueryBuilder()
	.select("id", "name")
	.from("users")
	.where("age", 25)
	.sort("name", "DESC")
	.limit(10)
	.offset(5)
	.join("INNER", "orders", "users.id = orders.user_id")
	.distinct()
	.count("orders.id")
	.build();
{
   query: "SELECT DISTINCT id, name, COUNT(orders.id) FROM users INNER JOIN orders ON users.id = orders.user_id WHERE age = ? ORDER BY name DESC LIMIT 10 OFFSET 5",
   params: [25]
}

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

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

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

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

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

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