lambdaorm
Advanced tools
Comparing version 0.0.33 to 0.0.34
{ | ||
"name": "lambdaorm", | ||
"version": "0.0.33", | ||
"version": "0.0.34", | ||
"description": "ORM", | ||
@@ -5,0 +5,0 @@ "author": "Flavio Lionel Rita <flaviolrita@hotmail.com>", |
103
README.md
@@ -6,4 +6,58 @@ # Lambda ORM | ||
LambdaORM is an ORM based on using the same syntax of lambda expressions in javascript to write the expressions that will be translated into SQL sentences according to the database. | ||
When starting from javascript lambda expressions we can use the IDE's own intellisense to write the sentences. | ||
Example: | ||
```ts | ||
import { orm } from 'lambdaorm' | ||
(async () => { | ||
await orm.init() | ||
const expression = (country:string)=>Products | ||
.filter(p => (p.price > 5 && p.supplier.country == country) || (p.inStock < 3)) | ||
.having(p => max(p.price) > 50) | ||
.map(p => ({ category: p.category.name, largestPrice: max(p.price) })) | ||
.sort(p => desc(p.largestPrice)) | ||
const result = await orm.lambda(expression).execute('mysql') | ||
console.log(JSON.stringify(result, null, 2)) | ||
await orm.end() | ||
})() | ||
``` | ||
where the SQL equivalent of the expression is: | ||
```sql | ||
SELECT c.CategoryName AS `category`, MAX(p.UnitPrice) AS `largestPrice` | ||
FROM Products p | ||
INNER JOIN Suppliers s ON s.SupplierID = p.SupplierID | ||
INNER JOIN Categories c ON c.CategoryID = p.CategoryID | ||
WHERE ((p.UnitPrice > 5 AND s.Country = ?) OR p.UnitsInStock < 3) | ||
GROUP BY c.CategoryName | ||
HAVING MAX(p.UnitPrice) > 50 | ||
ORDER BY `largestPrice` desc | ||
``` | ||
You could also write the expression to a string. | ||
In this case you should use the **orm.expression** method instead of **orm.lambda** | ||
```ts | ||
import { orm } from 'lambdaorm' | ||
(async () => { | ||
await orm.init() | ||
const expression = `Products | ||
.filter(p => (p.price > 5 && p.supplier.country == country) || (p.inStock < 3)) | ||
.having(p => max(p.price) > 50) | ||
.map(p => ({ category: p.category.name, largestPrice: max(p.price) })) | ||
.sort(p => desc(p.largestPrice))` | ||
const result = await orm.expression(expression).execute('mysql') | ||
console.log(JSON.stringify(result, null, 2)) | ||
await orm.end() | ||
})() | ||
``` | ||
## Queries: | ||
@@ -36,23 +90,2 @@ | ||
Example: | ||
``` ts | ||
OrderDetails | ||
.filter(p => p.order.customer.name == customerName) | ||
.map(p => ({ order: p.order.orderDate, total: sum(p.quantity * p.unitPrice) })) | ||
.sort(p => desc(p.total)) | ||
``` | ||
the previous expression is equivalent to the following statement for MySQL. | ||
``` sql | ||
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 | ||
``` | ||
More info: | ||
@@ -64,3 +97,3 @@ | ||
- [delete](https://github.com/FlavioLionelRita/lambdaorm/wiki/Query-Delete) | ||
- [bulkinsert](https://github.com/FlavioLionelRita/lambdaorm/wiki/Query-Bulkinsert) | ||
- [bulkInsert](https://github.com/FlavioLionelRita/lambdaorm/wiki/Query-BulkInsert) | ||
- [include](https://github.com/FlavioLionelRita/lambdaorm/wiki/Query-Include) | ||
@@ -81,10 +114,18 @@ | ||
``` ts | ||
Orders | ||
.filter(p => p.id === id) | ||
.include(p => [p.customer.map(p => ({ name: p.name, address: concat(p.address, ', ', p.city, ' (', p.postalCode, ') ', p.country) })), | ||
import { orm } from 'lambdaorm' | ||
(async () => { | ||
await orm.init() | ||
const expression = (id:number) => Orders | ||
.filter(p => p.id === id) | ||
.include(p => [p.customer.map(p => ({ name: p.name, address: concat(p.address, ', ', p.city, ' (', p.postalCode, ') ', p.country) })), | ||
p.details.include(p => p.product | ||
.include(p => p.category.map(p => p.name)) | ||
.map(p => p.name)) | ||
.map(p => [p.quantity, p.unitPrice])]) | ||
.map(p => p.orderDate) | ||
.map(p => p.name)) | ||
.map(p => [p.quantity, p.unitPrice])]) | ||
.map(p => p.orderDate) | ||
const result = await orm.lambda(expression).execute('mysql') | ||
console.log(JSON.stringify(result, null, 2)) | ||
await orm.end() | ||
})() | ||
``` | ||
@@ -309,6 +350,6 @@ | ||
schema: library | ||
dialect: mysql | ||
dialect: postgres | ||
connection: MY_DB_STRING_CONNECTION | ||
- name: mysql | ||
schema: northwind | ||
- name: otherDb | ||
schema: library | ||
dialect: mysql | ||
@@ -315,0 +356,0 @@ connection: |
752347
573