Select
This page shows how to read records: all the properties of an entity or only some of them, the first or last record, distinct values and pagination. The SQL shown is the one generated for MySQL.
Every query is based on the schema, from which λORM knows the properties of each entity, its primary key, indexes, relations and constraints. That is why, to read all the properties of an entity, you do not need to list them.
All the properties of an entity
Reads all the properties of all the products.
Lambda:
Products
SQL Result:
SELECT
p.ProductID AS id,
p.ProductName AS name,
p.SupplierID AS supplierId,
p.CategoryID AS categoryId,
p.QuantityPerUnit AS quantity,
p.UnitPrice AS price,
p.UnitsInStock AS inStock,
p.UnitsOnOrder AS onOrder,
p.ReorderLevel AS reorderLevel,
p.Discontinued AS discontinued
FROM Products p
One property
Reads only the name of each product.
Lambda:
Products.map(p => p.name)
SQL Result:
SELECT p.ProductName AS name FROM Products p
First
Reads the first product, ordered by its primary key.
Lambda:
Products.first()
SQL Result:
SELECT
p.ProductID AS id,
p.ProductName AS name,
p.SupplierID AS supplierId,
p.CategoryID AS categoryId,
p.QuantityPerUnit AS quantity,
p.UnitPrice AS price,
p.UnitsInStock AS inStock,
p.UnitsOnOrder AS onOrder,
p.ReorderLevel AS reorderLevel,
p.Discontinued AS discontinued
FROM Products p
ORDER BY p.ProductID asc
LIMIT 0,1
first can also receive the properties to return, as map does: Products.first(p => ({ name: p.name, price: p.price })). In that case the records are ordered by the first property returned.
Last
Reads the last product, ordered by its primary key.
Lambda:
Products.last()
SQL Result:
SELECT
p.ProductID AS id,
p.ProductName AS name,
p.SupplierID AS supplierId,
p.CategoryID AS categoryId,
p.QuantityPerUnit AS quantity,
p.UnitPrice AS price,
p.UnitsInStock AS inStock,
p.UnitsOnOrder AS onOrder,
p.ReorderLevel AS reorderLevel,
p.Discontinued AS discontinued
FROM Products p
ORDER BY p.ProductID desc
LIMIT 0,1
Distinct
Reads the distinct combinations of quantity per unit and category name, sorted by category. The category name comes from the category relation, so λORM adds the join (see Join).
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
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
page(page, records) receives the page number (starting at 1) and the number of records per page. How the page is expressed depends on the dialect: LIMIT on MySQL, OFFSET … FETCH NEXT … ROWS ONLY on Oracle, and so on.
Usage
The same query, with the minimum price as a parameter (minPrice), run in three ways.
Node
With the library @lambdaorm/lib:
import { orm } from '@lambdaorm/lib'
import { Products } from './domain/model' // model generated by `lambdaorm build`
(async () => {
try {
await orm.init('./lambdaORM.yaml')
// products with a price greater than minPrice, with their category, first page of 10
const query = (minPrice: number) => Products
.filter(p => p.price > minPrice)
.map(p => ({ name: p.name, category: p.category.name }))
.sort(p => p.category)
.page(1, 10)
const result = await orm.execute(query, { minPrice: 10 })
console.log(JSON.stringify(result, null, 2))
} catch (error: any) {
console.error(error.stack)
} finally {
// releases the connections
await orm.end()
}
})()
CLI
With the CLI @lambdaorm/cli:
lambdaorm execute -q "Products.filter(p=>p.price>minPrice).map(p=>({name:p.name,category:p.category.name})).sort(p=>p.category).page(1,10)" -d "{\"minPrice\":10}"
Data Gateway
Through the REST API of the Data Gateway:
curl -X POST "http://localhost:8080/execute?format=beautiful" -H "X-Api-Key: $KEY" -H "Content-Type: application/json" \
-d '{"query": "Products.filter(p=>p.price>minPrice).map(p=>({name:p.name,category:p.category.name})).sort(p=>p.category).page(1,10)", "data": {"minPrice": 10}}'