Select
Performs a query to retrieve data from a table.
Methods
selectDatabase()
→ (database = String) → this
Selects a different database for this query.
Parameters
| Parameter | Type | Description |
|---|---|---|
database | String | Name 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
| Parameter | Type | Description |
|---|---|---|
string | String | WHERE clause with ? placeholders for values. |
values | Array<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
| Parameter | Type | Description |
|---|---|---|
string | String | HAVING clause with ? placeholders.. |
values | Array<any> | Values to replace ?, in order. |
Returns
→ this
order()
→ (column = String, desc = Boolean, aggregation = Enum) → this
Adds sorting to the query.
Parameters
| Parameter | Type | Description |
|---|---|---|
column | String | Column to order by. |
desc | Boolean | Sort in descending order? Defaults to false. |
aggregation | Enum → MIN/MAX/COUNT/SUM/AVG | Optional aggregation function. |
Returns
→ this
count()
→ (doParse = Boolean) → this
Counts the number of rows in the first selected column.
Parameters
| Parameter | Type | Description |
|---|---|---|
doParse | Boolean | If 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
| Parameter | Type | Description |
|---|---|---|
doParse | Boolean | If 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
| Parameter | Type | Description |
|---|---|---|
doParse | Boolean | If true, returns a number. Defaults to false. |
Returns
→ this
group()
→ (...columns = String) → this
Groups rows by the specified columns.
Parameters
| Parameter | Type | Description |
|---|---|---|
...columns | String | Columns to group by. |
Returns
→ this
join()
→ (type = Enum, table = String, onOriginalColumn = String, onJoinedColumn = String, ...columns = String) → this
Performs a SQL JOIN.
Parameters
| Parameter | Type | Description |
|---|---|---|
type | Enum → LEFT/INNER/RIGHT/FULL OUTER | Type of join. |
table | String | Table to join with. |
onOriginalColumn | String | Column from the original table. |
onJoinedColumn | String | Column from the joined table. |
...columns | String | Columns to select. |
Returns
→ this
limit()
→ (number = Number, offset = Number) → this
Limits the number of returned rows and sets an offset.
Parameters
| Parameter | Type | Description |
|---|---|---|
number | Number | Maximum rows to return. |
offset | Number | Offset for starting position. |
Returns
→ this
pagination()
→ (page = Number, itemsPerPage = Number) → this
Applies pagination to the query results.
Parameters
| Parameter | Type | Description |
|---|---|---|
page | Number | The page number (starting from 1). |
itemsPerPage | Number | Number of items per page. |
Returns
→ this
execute()
async → () → Number / Array<Object>
Executes the query.
Returns
→ Number - if (doParse is true)
→ Array<Object> - Query Result