6.7 KiB
| sidebar_position |
|---|
| 3 |
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
Adds the 'distinct' keyword to this query.
- With 'distinct' only unique values are returned
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") // Select table "Orders" and column "EmployeeID"
.distinct() // Only get unique EmployeeIDs
.execute();
console.log(empOrders);
/*
[
RowDataPacket { EmployeeID: 5 },
RowDataPacket { EmployeeID: 6 },
RowDataPacket { EmployeeID: 4 },
RowDataPacket { EmployeeID: 3 },
RowDataPacket { EmployeeID: 9 },
RowDataPacket { EmployeeID: 1 },
RowDataPacket { EmployeeID: 8 },
RowDataPacket { EmployeeID: 2 },
RowDataPacket { EmployeeID: 7 }
]
*/
where()
→ (string = String, values = Array<any>) → this
Adds a where-clause to the query
- Values should be set as ? in the string and given in left-to-right order via the 'values'-array to minimize the risk of sql-injection
- If you are using joins, specify the table and column together: table.column
Parameters
| Parameter | Type | Description |
|---|---|---|
string |
String | The where-clause as a string with ? representing each values. |
values |
Array<any> | Array containing values replacing the ? in the string (from left to right) |
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 table 'Orders'
// Filter by orders where CustomerID are our desired ids.
// Note that the values of ? are pushed in order into the value-array
.where("CustomerID = ? OR CustomerID = ?", [90, 34])
.execute();
console.log(filteredOrders);
/*
[
RowDataPacket {
OrderID: 10248,
CustomerID: 90,
EmployeeID: 5,
OrderDate: '1996-07-04',
ShipperID: 3
},
RowDataPacket {
OrderID: 10250,
CustomerID: 34,
EmployeeID: 4,
OrderDate: '1996-07-08',
ShipperID: 2
},
RowDataPacket {
OrderID: 10253,
CustomerID: 34,
EmployeeID: 3,
OrderDate: '1996-07-10',
ShipperID: 2
}
]
*/
having()
→ (string = String, values = Array<any>) → this
Same as where() but allows for aggregation.
- Values should be set as ? in the string and given in left-to-right order via the 'values'-array to minimize the risk of sql-injection
- If you are using joins, specify the table and column together: table.column
Parameters
| Parameter | Type | Description |
|---|---|---|
string |
String | The having-clause with possible aggregation ? representing each values. |
values |
Array<any> | Array containing values replacing the ? in the string (from left to right) |
Returns
→ this
order()
→ (column = String, desc = Boolean, aggregation = Enum) → this
Adds a new sort order.
- Can be used multiple times to order by multiple columns
Parameters
| Parameter | Type | Description |
|---|---|---|
column |
String | Column to order by |
desc |
Boolean | Sort descending? Defaults to false |
aggregation |
Enum → MIN/MAX/COUNT/SUM/AVG |
The aggregation type to use |
Returns
→ this
count()
→ (doParse = Boolean) → this
Counts the number of entries of the first selected column.
Parameters
| Parameter | Type | Description |
|---|---|---|
doParse |
Boolean | If true the query will only return a Number of entries. 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 the query will only return a Number of entries. Defaults to false. |
Returns
→ this
avg()
→ (doParse = Boolean) → this
Averages numerical rows of the first selected column.
Parameters
| Parameter | Type | Description |
|---|---|---|
doParse |
Boolean | If true the query will only return a Number of entries. Defaults to false. |
Returns
→ this
group()
→ (...columns = String) → this
Groups rows that have the same values into summary rows.
Parameters
| Parameter | Type | Description |
|---|---|---|
...columns |
String | The columns to group by |
Returns
→ this
join()
→ (type = Enum, table = String, onOriginalColumn = String, onJoinedColumn = String, ...columns = String) → this
Adds a new join to the query.
Parameters
| Parameter | Type | Description |
|---|---|---|
type |
Enum → LEFT/INNER/RIGHT/FULL OUTER |
The join type |
table |
String | Table to join on |
onOriginalColumn |
String | Column name on the original table to check agains |
onJoinedColumn |
String | Column name of the join table to check against |
...columns |
String | The columns to join. OG-columns must be set! |
Returns
→ this
limit()
→ (number = Number, offset = Number) → this
Limits the query and specifies an offset to start at.
:::warning
offset has no default value and therefore must not be empty!
:::
Parameters
| Parameter | Type | Description |
|---|---|---|
number |
Number | Limits the query by specified rows |
offset |
Number | Offset to start at. |
Returns
→ this
pagination()
→ (page = Number, itemsPerPage = Number) → this
Paginates the query.
Parameters
| Parameter | Type | Description |
|---|---|---|
page |
Number | The page to get (Minimum 1) |
itemsPerPage |
Number | How many items a page should have |
Returns
→ this
execute()
async → () → any
Executes the prepared query.
Returns
→ any - Query result