@lambdaorm/client 0.10.0 → 0.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/README.md +87 -187
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -1,217 +1,117 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @lambdaorm/client
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/@lambdaorm/client)
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
📖 **Documentation:** [lambda-orm.github.io/wiki](https://lambda-orm.github.io/wiki) (English, Deutsch, Español, Português)
|
|
6
7
|
|
|
7
|
-
|
|
8
|
+
Query your databases from any Node.js application **without database drivers, connection strings or database
|
|
9
|
+
credentials**: the queries run on a [λORM Data Gateway](https://lambda-orm.github.io/wiki/data-gateway/), which holds
|
|
10
|
+
the connections and checks what each credential may do. The interface is the library's
|
|
11
|
+
([`@lambdaorm/lib`](https://www.npmjs.com/package/@lambdaorm/lib)) — `orm.execute`, `orm.plan`, `orm.executeScript`,
|
|
12
|
+
… — so moving a service from direct access to the gateway is changing one `init`.
|
|
8
13
|
|
|
9
14
|
```typescript
|
|
10
|
-
import { orm } from 'lambdaorm
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
console.log(await orm.general.ping())
|
|
17
|
-
console.log(await orm.general.health())
|
|
18
|
-
} catch (error: any) {
|
|
19
|
-
console.error(error)
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
execute()
|
|
15
|
+
import { orm } from '@lambdaorm/client'
|
|
16
|
+
|
|
17
|
+
await orm.init('http://gateway:8080/northwind', { headers: { 'X-Api-Key': process.env.KEY! } })
|
|
18
|
+
const top = await orm.execute(
|
|
19
|
+
'Products.filter(p => p.price > minPrice).map(p => ({ name: p.name, category: p.category.name })).page(1, 10)',
|
|
20
|
+
{ minPrice: 50 })
|
|
23
21
|
```
|
|
24
22
|
|
|
25
|
-
|
|
23
|
+
- **REST or WebSocket:** `http://` or `ws://`; over WebSocket, one connection and live notifications.
|
|
24
|
+
- **Typed:** with the model of the schema (`lambdaorm build`), the queries are lambdas checked by TypeScript.
|
|
25
|
+
- **Governed:** an API key or token with permissions; a `read` credential can't write, and every write is audited.
|
|
26
26
|
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
{uptime: 6124.260961852, message: 'OK', time: '2023-11-19T04:25:36.846Z'}
|
|
27
|
+
```sh
|
|
28
|
+
npm install @lambdaorm/client
|
|
30
29
|
```
|
|
31
30
|
|
|
32
|
-
|
|
31
|
+
## Connect
|
|
33
32
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
import { Orders } from './model'
|
|
37
|
-
|
|
38
|
-
async function execute() {
|
|
39
|
-
try {
|
|
40
|
-
orm.init('http://localhost:9291')
|
|
41
|
-
const query = (id: number) =>
|
|
42
|
-
Orders.filter(p => p.id == id).include(p => [
|
|
43
|
-
p.customer.map(c => c.name),
|
|
44
|
-
p.details.include(d =>
|
|
45
|
-
d.product.include(pr =>
|
|
46
|
-
pr.category.map(cat => cat.name)
|
|
47
|
-
).map(pr => pr.name)
|
|
48
|
-
).map(d => [d.quantity, d.unitPrice]),
|
|
49
|
-
])
|
|
50
|
-
const data = { id: 10248 }
|
|
51
|
-
const plan = await orm.plan(query, { stage: 'default' })
|
|
52
|
-
console.log(JSON.stringify(plan, null, 2))
|
|
53
|
-
const result = await orm.execute(query, data, { stage: 'default' })
|
|
54
|
-
console.log(JSON.stringify(result, null, 2))
|
|
55
|
-
} catch (error: any) {
|
|
56
|
-
console.error(error)
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
execute()
|
|
60
|
-
```
|
|
33
|
+
The URL is the route of a schema on the gateway. `http(s)://` uses the REST API; `ws(s)://` the WebSocket API, with
|
|
34
|
+
one persistent connection and notifications.
|
|
61
35
|
|
|
62
|
-
|
|
36
|
+
```ts
|
|
37
|
+
import { orm } from '@lambdaorm/client'
|
|
63
38
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
async function execute() {
|
|
67
|
-
try {
|
|
68
|
-
orm.init('http://localhost:9291')
|
|
69
|
-
const query = `Orders.filter(p => p.id == id)
|
|
70
|
-
.include(p => [p.customer.map(c => c.name),p.details
|
|
71
|
-
.include(d => d.product
|
|
72
|
-
.include(pr => pr.category.map(cat => cat.name))
|
|
73
|
-
.map(pr => pr.name))
|
|
74
|
-
.map(d => [d.quantity, d.unitPrice])])`
|
|
75
|
-
const data = { id: 10248 }
|
|
76
|
-
const plan = await orm.plan(query, { stage: 'default' })
|
|
77
|
-
console.log(JSON.stringify(plan, null, 2))
|
|
78
|
-
const result = await orm.execute(query, data, { stage: 'default' })
|
|
79
|
-
console.log(JSON.stringify(result, null, 2))
|
|
80
|
-
} catch (error: any) {
|
|
81
|
-
console.error(error)
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
execute()
|
|
85
|
-
```
|
|
39
|
+
// an API key (or any other header)
|
|
40
|
+
await orm.init('http://gateway:8080/iot/v1', { headers: { 'X-Api-Key': process.env.IOT_KEY! } })
|
|
86
41
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
```json
|
|
90
|
-
{
|
|
91
|
-
"entity": "Orders",
|
|
92
|
-
"dialect": "MySQL",
|
|
93
|
-
"source": "default",
|
|
94
|
-
"sentence": "SELECT o.OrderID AS id, o.CustomerID AS customerId, o.EmployeeID AS employeeId, o.OrderDate AS orderDate, o.RequiredDate AS requiredDate, o.ShippedDate AS shippedDate, o.ShipVia AS shipViaId, o.Freight AS freight, o.ShipName AS name, o.ShipAddress AS address, o.ShipCity AS city, o.ShipRegion AS region, o.ShipPostalCode AS postalCode, o.ShipCountry AS country, o.CustomerID AS `__customerId`, o.OrderID AS `__id` FROM Orders o WHERE o.OrderID = ? ",
|
|
95
|
-
"children": [
|
|
96
|
-
{
|
|
97
|
-
"entity": "Customers",
|
|
98
|
-
"dialect": "MySQL",
|
|
99
|
-
"source": "default",
|
|
100
|
-
"sentence": "SELECT c.CompanyName AS name, c.CustomerID AS LambdaOrmParentId FROM Customers c WHERE c.CustomerID IN (?) ",
|
|
101
|
-
"children": []
|
|
102
|
-
},
|
|
103
|
-
{
|
|
104
|
-
"entity": "Orders.details",
|
|
105
|
-
"dialect": "MySQL",
|
|
106
|
-
"source": "default",
|
|
107
|
-
"sentence": "SELECT o1.Quantity AS quantity, o1.UnitPrice AS unitPrice, o1.ProductID AS `__productId`, o1.OrderID AS LambdaOrmParentId FROM `Order Details` o1 WHERE o1.OrderID IN (?) ",
|
|
108
|
-
"children": [
|
|
109
|
-
{
|
|
110
|
-
"entity": "Products",
|
|
111
|
-
"dialect": "MySQL",
|
|
112
|
-
"source": "default",
|
|
113
|
-
"sentence": "SELECT p.ProductName AS name, p.CategoryID AS `__categoryId`, p.ProductID AS LambdaOrmParentId FROM Products p WHERE p.ProductID IN (?) ",
|
|
114
|
-
"children": [
|
|
115
|
-
{
|
|
116
|
-
"entity": "Categories",
|
|
117
|
-
"dialect": "MySQL",
|
|
118
|
-
"source": "default",
|
|
119
|
-
"sentence": "SELECT c1.CategoryName AS name, c1.CategoryID AS LambdaOrmParentId FROM Categories c1 WHERE c1.CategoryID IN (?) ",
|
|
120
|
-
"children": []
|
|
121
|
-
}
|
|
122
|
-
]
|
|
123
|
-
}
|
|
124
|
-
]
|
|
125
|
-
}
|
|
126
|
-
]
|
|
127
|
-
}
|
|
42
|
+
// a bearer token: sent as Authorization: Bearer <token>
|
|
43
|
+
await orm.init('http://gateway:8080/iot/v1', { token: process.env.IOT_TOKEN })
|
|
128
44
|
```
|
|
129
45
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
```json
|
|
133
|
-
[
|
|
134
|
-
{
|
|
135
|
-
"id": 10248,
|
|
136
|
-
"customerId": "VINET",
|
|
137
|
-
"employeeId": 5,
|
|
138
|
-
"orderDate": "1996-07-04T00:00:00.000Z",
|
|
139
|
-
"requiredDate": "1996-08-01T00:00:00.000Z",
|
|
140
|
-
"shippedDate": "1996-07-16T00:00:00.000Z",
|
|
141
|
-
"shipViaId": 3,
|
|
142
|
-
"freight": 32.38,
|
|
143
|
-
"name": "Vins et alcools Chevalier",
|
|
144
|
-
"address": "59 rue de l-Abbaye",
|
|
145
|
-
"city": "Reims",
|
|
146
|
-
"region": null,
|
|
147
|
-
"postalCode": "51100",
|
|
148
|
-
"country": "France",
|
|
149
|
-
"customer": {
|
|
150
|
-
"name": "Vins et alcools Chevalier"
|
|
151
|
-
},
|
|
152
|
-
"details": [
|
|
153
|
-
{
|
|
154
|
-
"quantity": 12,
|
|
155
|
-
"unitPrice": 14,
|
|
156
|
-
"product": {
|
|
157
|
-
"name": "Queso Cabrales",
|
|
158
|
-
"category": {
|
|
159
|
-
"name": "Dairy Products"
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
},
|
|
163
|
-
{
|
|
164
|
-
"quantity": 10,
|
|
165
|
-
"unitPrice": 9.8,
|
|
166
|
-
"product": {
|
|
167
|
-
"name": "Singaporean Hokkien Fried Mee",
|
|
168
|
-
"category": {
|
|
169
|
-
"name": "Grains/Cereals"
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
},
|
|
173
|
-
{
|
|
174
|
-
"quantity": 5,
|
|
175
|
-
"unitPrice": 34.8,
|
|
176
|
-
"product": {
|
|
177
|
-
"name": "Mozzarella di Giovanni",
|
|
178
|
-
"category": {
|
|
179
|
-
"name": "Dairy Products"
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
]
|
|
184
|
-
}
|
|
185
|
-
]
|
|
186
|
-
```
|
|
46
|
+
A gateway that serves a single schema at the root is reached at its own URL: `http://gateway:8080`.
|
|
187
47
|
|
|
188
|
-
##
|
|
48
|
+
## Queries
|
|
189
49
|
|
|
190
|
-
|
|
191
|
-
uses it, with the same methods, and receives the gateway's notifications:
|
|
50
|
+
A query is a lambda expression over the domain model, or the same expression as a string:
|
|
192
51
|
|
|
193
52
|
```ts
|
|
194
53
|
import { orm } from '@lambdaorm/client'
|
|
54
|
+
import { Orders } from './northwind/domain/model'
|
|
195
55
|
|
|
196
|
-
await orm.init('
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
56
|
+
await orm.init('http://gateway:8080', { headers: { 'X-Api-Key': process.env.KEY! } })
|
|
57
|
+
|
|
58
|
+
const query = (id: number) => Orders
|
|
59
|
+
.filter(p => p.id == id)
|
|
60
|
+
.include(p => [
|
|
61
|
+
p.customer.map(c => c.name),
|
|
62
|
+
p.details
|
|
63
|
+
.include(d => d.product.include(pr => pr.category.map(c => c.name)).map(pr => pr.name))
|
|
64
|
+
.map(d => [d.quantity, d.unitPrice])
|
|
65
|
+
])
|
|
66
|
+
|
|
67
|
+
const plan = await orm.plan(query) // the sentences per source, without running them
|
|
68
|
+
const result = await orm.execute(query, { id: 10248 }) // the rows
|
|
69
|
+
|
|
70
|
+
// the same query as a string
|
|
71
|
+
const rows = await orm.execute('Products.filter(p => p.price > minPrice).map(p => [p.name, p.price])', { minPrice: 50 })
|
|
202
72
|
```
|
|
203
73
|
|
|
204
|
-
|
|
74
|
+
| Method | What it does | Permission |
|
|
75
|
+
| --- | --- | --- |
|
|
76
|
+
| `orm.execute(query, data?, options?)` | runs a query | `read`, or `write` when it writes |
|
|
77
|
+
| `orm.executeScript(script, data?, options?)` | runs a [script](https://lambda-orm.github.io/wiki/usage/Script/): several queries in one transaction | `script` |
|
|
78
|
+
| `orm.executeQueued(query, topic, data?, chunk?, options?)` | runs a query and sends its rows to a queue (Kafka), in chunks | `queue` |
|
|
79
|
+
| `orm.plan(query, options?)` | the execution plan | `read` |
|
|
80
|
+
| `orm.metadata(query)`, `orm.model(query)`, `orm.parameters(query)`, `orm.constraints(query)` | what the query returns, needs and checks | `read` |
|
|
81
|
+
| `orm.general` | `ping()`, `health()`, `version()`, `metrics()` | — |
|
|
82
|
+
| `orm.schema` | `domain()`, `entities()`, `entity(name)`, `enums()`, `mappings()`, `sources()`, `stages()`, `views()`, … | `read` |
|
|
83
|
+
| `orm.stage` | `exists(stage)`, `export(stage)`, `import(stage, data)` | `read` / `stageImport` |
|
|
84
|
+
|
|
85
|
+
`options.stage` chooses the [stage](https://lambda-orm.github.io/wiki/schema/SchemaExample-MultiplesStages/) of the
|
|
86
|
+
schema; without it, the schema's default.
|
|
205
87
|
|
|
206
|
-
|
|
88
|
+
## WebSocket and notifications
|
|
89
|
+
|
|
90
|
+
With a `ws://` URL every operation goes through one connection, and `orm.on` receives the gateway's
|
|
91
|
+
[notifications](https://lambda-orm.github.io/wiki/data-gateway/WebSocket/): `queue.completed`, `queue.failed` (of the
|
|
92
|
+
`executeQueued` of this connection) and `schema.statusChanged`.
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
await orm.init('ws://gateway:8083/iot/v1', { headers: { 'X-Api-Key': process.env.IOT_KEY! } })
|
|
207
96
|
|
|
208
|
-
|
|
97
|
+
const stop = orm.on('queue.completed', ({ queueId, rows }) => console.log(`${queueId}: ${rows} rows sent`))
|
|
98
|
+
orm.on('schema.statusChanged', ({ status, reason }) => console.log(status, reason ?? ''))
|
|
209
99
|
|
|
210
|
-
|
|
100
|
+
await orm.executeQueued('Products', 'products-topic', {}, 100)
|
|
101
|
+
// …
|
|
102
|
+
stop()
|
|
103
|
+
await orm.end() // closes the connection
|
|
104
|
+
```
|
|
211
105
|
|
|
212
106
|
## Documentation
|
|
213
107
|
|
|
214
|
-
- [
|
|
215
|
-
- [
|
|
216
|
-
- [
|
|
217
|
-
- [
|
|
108
|
+
- [Data Gateway](https://lambda-orm.github.io/wiki/data-gateway/): getting started, configuration, security
|
|
109
|
+
- [Clients](https://lambda-orm.github.io/wiki/data-gateway/Clients/) and the [REST API](https://lambda-orm.github.io/wiki/data-gateway/Rest/)
|
|
110
|
+
- [Query language](https://lambda-orm.github.io/wiki/queries/Query-Language/)
|
|
111
|
+
- [Change log](CHANGELOG.md)
|
|
112
|
+
|
|
113
|
+
> **Note:** `lambdaorm-client-node` is the old, unscoped package, no longer maintained.
|
|
114
|
+
|
|
115
|
+
## License
|
|
116
|
+
|
|
117
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lambdaorm/client",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.1",
|
|
4
4
|
"description": "Client for lambda ORM service",
|
|
5
5
|
"author": "Flavio Lionel Rita <flaviolrita@proton.me>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"test": "npx jest --config ./jest-config.json"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@lambdaorm/base": "^3.3.
|
|
30
|
+
"@lambdaorm/base": "^3.3.2",
|
|
31
31
|
"3xpr": "^5.3.1",
|
|
32
32
|
"h3lp": "^5.0.0",
|
|
33
33
|
"axios": "^1.6.3",
|