Skip to main content

Select

Performs a query to retrieve data from a table.


Methods


selectDatabase()

→ (database = String) → this

Selects a different database for this query.

Parameters

ParameterTypeDescription
databaseStringName of the database to select

Returns

this


distinct()

→ () → this

Ensures that only unique values are returned in the query results.

Returns

this

Example

Scenario: We want to get the EmployeeIDs of the Employees that have open orders in Orders

const empOrders = await instance.select("Orders", "EmployeeID") // Selects "EmployeeID" from "Orders"
.distinct() // Ensures unique EmployeeIDs
.execute();
console.log(empOrders);
/* Output:
[
{ EmployeeID: 5 },
{ EmployeeID: 6 },
{ EmployeeID: 4 },
{ EmployeeID: 3 },
{ EmployeeID: 9 },
{ EmployeeID: 1 },
{ EmployeeID: 8 },
{ EmployeeID: 2 },
{ EmployeeID: 7 }
]
*/

where()

→ (string = String, values = Array<any>) → this

Adds a WHERE clause to filter results.

  • Use ? as placeholders in the condition string to prevent SQL injection.
  • If using joins, specify columns as table.column.

Parameters

ParameterTypeDescription
stringStringWHERE clause with ? placeholders for values.
valuesArray<any>Values to replace ?, in order.

Returns

this

Example

Scenario: We only want to get the Orders from the Customers with the id 90 and 34

const filteredOrders = await instance.select("Orders")  // Select "Orders" table
.where("CustomerID = ? OR CustomerID = ?", [90, 34])
.execute();
console.log(filteredOrders);
/* Output:
[
{ OrderID: 10248, CustomerID: 90, EmployeeID: 5, OrderDate: '1996-07-04', ShipperID: 3 },
{ OrderID: 10250, CustomerID: 34, EmployeeID: 4, OrderDate: '1996-07-08', ShipperID: 2 },
{ OrderID: 10253, CustomerID: 34, EmployeeID: 3, OrderDate: '1996-07-10', ShipperID: 2 }
]
*/

having()

→ (string = String, values = Array<any>) → this

Same as .where(), but allows filtering after aggregation.

Parameters

ParameterTypeDescription
stringStringHAVING clause with ? placeholders..
valuesArray<any>Values to replace ?, in order.

Returns

this


order()

→ (column = String, desc = Boolean, aggregation = Enum) → this

Adds sorting to the query.

Parameters

ParameterTypeDescription
columnStringColumn to order by.
descBooleanSort in descending order? Defaults to false.
aggregationEnumMIN/MAX/COUNT/SUM/AVGOptional aggregation function.

Returns

this


count()

→ (doParse = Boolean) → this

Counts the number of rows in the first selected column.

Parameters

ParameterTypeDescription
doParseBooleanIf true, returns a number instead of an array. Defaults to false.

Returns

this


sum()

→ (doParse = Boolean) → this

Sums numerical rows of the first selected column.

Parameters

ParameterTypeDescription
doParseBooleanIf true, returns a number. Defaults to false.

Returns

this


avg()

→ (doParse = Boolean) → this

Calculates the average value of numerical rows in the first selected column.

Parameters

ParameterTypeDescription
doParseBooleanIf true, returns a number. Defaults to false.

Returns

this


group()

→ (...columns = String) → this

Groups rows by the specified columns.

Parameters

ParameterTypeDescription
...columnsStringColumns to group by.

Returns

this


join()

→ (type = Enum, table = String, onOriginalColumn = String, onJoinedColumn = String, ...columns = String) → this

Performs a SQL JOIN.

Parameters

ParameterTypeDescription
typeEnumLEFT/INNER/RIGHT/FULL OUTERType of join.
tableStringTable to join with.
onOriginalColumnStringColumn from the original table.
onJoinedColumnStringColumn from the joined table.
...columnsStringColumns to select.

Returns

this


limit()

→ (number = Number, offset = Number) → this

Limits the number of returned rows and sets an offset.

Parameters

ParameterTypeDescription
numberNumberMaximum rows to return.
offsetNumberOffset for starting position.

Returns

this


pagination()

→ (page = Number, itemsPerPage = Number) → this

Applies pagination to the query results.

Parameters

ParameterTypeDescription
pageNumberThe page number (starting from 1).
itemsPerPageNumberNumber of items per page.

Returns

this


execute()

async → () → Number / Array<Object>

Executes the query.

Returns

Number - if (doParse is true)
Array<Object> - Query Result