Join
This page shows how λORM reads properties of related entities. There is no join method: when a query uses a property through a relation, such as p.category.name, λORM adds the join that the relation defines in the schema. The SQL shown is the one generated for MySQL.
Note
A join needs both entities in the same source. To read related entities that are in another source, use Include; to filter by a property of a relation in another source, see Filter by a field of a relation in another source.
Simple join
Reads the name of each product and the name of its category, through the category relation.
Lambda:
Products.map(p => ({ name: p.name, category: p.category.name }))
SQL Result:
SELECT p.ProductName AS name, c.CategoryName AS category
FROM Products p
INNER JOIN Categories c ON c.CategoryID = p.CategoryID
Join with distinct and sort
Reads the distinct combinations of quantity per unit and category name, sorted by category.
Lambda:
Products
.distinct(p => ({ quantity: p.quantity, category: p.category.name }))
.sort(p => p.category)
SQL Result:
SELECT DISTINCT p.QuantityPerUnit AS quantity, c.CategoryName AS category
FROM Products p
INNER JOIN Categories c ON c.CategoryID = p.CategoryID
ORDER BY category asc
Join with filter
Reads the name and category of the products with a price greater than 10.
Lambda:
Products
.filter(p => p.price > 10)
.map(p => ({ name: p.name, category: p.category.name }))
SQL Result:
SELECT p.ProductName AS name, c.CategoryName AS category
FROM Products p
INNER JOIN Categories c ON c.CategoryID = p.CategoryID
WHERE p.UnitPrice > 10
Join and pagination
Reads the name and category of the products with a price greater than 10, sorted by category, and returns the first page of 10 records.
Lambda:
Products
.filter(p => p.price > 10)
.map(p => ({ name: p.name, category: p.category.name }))
.sort(p => p.category)
.page(1, 10)
SQL Result:
SELECT p.ProductName AS name, c.CategoryName AS category
FROM Products p
INNER JOIN Categories c ON c.CategoryID = p.CategoryID
WHERE p.UnitPrice > 10
ORDER BY category asc
LIMIT 0,10
Multiple joins and grouping
Reads, for the orders of a customer, the order date and the total (sum of quantity × unit price of its details), sorted by total in descending order. The query starts from the order details (Orders.details) and goes through two relations: order, and from the order, customer. Because sum is an aggregate function, λORM also groups by the order date (see Grouping).
Lambda:
Orders.details
.filter(p => p.order.customer.name == customerName)
.map(p => ({ order: p.order.orderDate, total: sum(p.quantity * p.unitPrice) }))
.sort(p => desc(p.total))
SQL Result:
SELECT o1.OrderDate AS `order`, SUM((o.Quantity * o.UnitPrice)) AS total
FROM `Order Details` o
INNER JOIN Orders o1 ON o1.OrderID = o.OrderID
INNER JOIN Customers c ON c.CustomerID = o1.CustomerID
WHERE c.CompanyName = ?
GROUP BY o1.OrderDate
ORDER BY total desc
Code example
import { orm } from '@lambdaorm/lib'
import { Orders } from './domain/model' // model generated by `lambdaorm build`
(async () => {
try {
await orm.init('./lambdaORM.yaml')
const query = (customerName: string) => Orders.details
.filter(p => p.order.customer.name == customerName)
.map(p => ({ order: p.order.orderDate, total: sum(p.quantity * p.unitPrice) }))
.sort(p => desc(p.total))
const result = await orm.execute(query, { customerName: 'Vins et alcools Chevalier' }, { stage: 'MySQL' })
console.log(JSON.stringify(result, null, 2))
} catch (error: any) {
console.error(error.stack)
} finally {
await orm.end()
}
})()