@atscript/db-odata 0.0.0-stage → 0.0.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/LICENSE +21 -0
- package/README.md +403 -2
- package/dist/index.cjs +1383 -0
- package/dist/index.d.cts +581 -0
- package/dist/index.d.mts +581 -0
- package/dist/index.mjs +1374 -0
- package/package.json +57 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Artem Maltsev <artem@maltsev.nl>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,404 @@
|
|
|
1
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="https://odata.atscript.dev/logo.svg" alt="Atscript" width="120" />
|
|
3
|
+
</p>
|
|
2
4
|
|
|
3
|
-
|
|
5
|
+
<h1 align="center">@atscript/db-odata</h1>
|
|
6
|
+
|
|
7
|
+
<p align="center">
|
|
8
|
+
Atscript DB adapter that uses an OData v2/v4 service as the database.
|
|
9
|
+
</p>
|
|
10
|
+
|
|
11
|
+
<p align="center">
|
|
12
|
+
Part of Atscript OData. Works with SAP S/4HANA®, SAP Gateway and SAP RAP services, and with any standard OData v2/v4 service.
|
|
13
|
+
</p>
|
|
14
|
+
|
|
15
|
+
<p align="center">
|
|
16
|
+
<a href="https://odata.atscript.dev">Documentation</a>
|
|
17
|
+
</p>
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
> Preview release (0.0.x) — APIs may still change between releases. Writes are single-entity; an update or replace detects
|
|
22
|
+
> a conflict when the caller carries the row's version token, and is last-write-wins when it does
|
|
23
|
+
> not (see [Writes](#writes)). The service's operations run through the space and its tables
|
|
24
|
+
> (`callOperation`), which the generated bases of `@atscript/moost-odata` call (see
|
|
25
|
+
> [Operations](https://odata.atscript.dev/guide/operations)).
|
|
26
|
+
|
|
27
|
+
## Installation
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
pnpm add @atscript/db-odata @atscript/odata @atscript/db @atscript/typescript @uniqu/core
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Needs `@atscript/db` 0.1.200 or newer and `@atscript/typescript` 0.2.25 or newer.
|
|
34
|
+
|
|
35
|
+
The models come from the generator of `@atscript/odata`: see the
|
|
36
|
+
[installation guide](https://odata.atscript.dev/guide/installation). Every export is documented in the
|
|
37
|
+
[API reference](https://odata.atscript.dev/api/db-odata).
|
|
38
|
+
|
|
39
|
+
## Usage
|
|
40
|
+
|
|
41
|
+
`createODataSpace` returns a `DbSpace` (an `ODataSpace`) whose tables are the entity sets of one
|
|
42
|
+
OData service. Typed at build time, live at runtime: the table types are the ones `@atscript/odata`
|
|
43
|
+
generates from the saved `$metadata`, and `space.connect()` reads the live `$metadata` (its
|
|
44
|
+
value-help documents included) and makes the generated module's classes the live declarations.
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
import { createODataSpace } from "@atscript/db-odata";
|
|
48
|
+
import { basicAuth } from "@atscript/moost-odata"; // or any function answering { headers }
|
|
49
|
+
import * as sales from "./odata/sales.as";
|
|
50
|
+
import { model } from "./odata/sales.model.gen";
|
|
51
|
+
|
|
52
|
+
const space = createODataSpace({
|
|
53
|
+
baseUrl: "https://my-gateway.example.com/sap/opu/odata/sap/ZDEMO_ORDERS_SRV",
|
|
54
|
+
query: { "sap-client": "100" },
|
|
55
|
+
model,
|
|
56
|
+
types: sales,
|
|
57
|
+
init: basicAuth({ user: process.env.ODATA_USER!, password: process.env.ODATA_PASSWORD! }),
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
await space.connect(); // the live $metadata, read with the space's init
|
|
61
|
+
|
|
62
|
+
const orders = space.getTable(sales.Order);
|
|
63
|
+
|
|
64
|
+
await orders.findMany({
|
|
65
|
+
filter: { CustomerId: "C0001", OrderDate: { $gte: "2024-01-01" } },
|
|
66
|
+
controls: { $sort: { OrderDate: -1 }, $limit: 20, $with: [{ name: "Items" }] },
|
|
67
|
+
});
|
|
68
|
+
await orders.findById("4500000001");
|
|
69
|
+
await orders.findManyWithCount({ filter: {}, controls: { $skip: 20, $limit: 20 } });
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
A table reads and writes the entity set its type names in `@db.table`. The tables work with
|
|
73
|
+
everything that takes an atscript-db table, the REST controllers of `@atscript/moost-db` included.
|
|
74
|
+
The generated types name their service as `@db.space`: register the space under that name with
|
|
75
|
+
moost-db's `provideDbSpace`, and declare a controller per entity set by model token; the base
|
|
76
|
+
classes of `@atscript/moost-odata` add the error mapping and offer only the writes the set allows:
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
import { TableController, provideDbSpace } from "@atscript/moost-db";
|
|
80
|
+
import { ODataTableController } from "@atscript/moost-odata";
|
|
81
|
+
|
|
82
|
+
provideDbSpace(space, "sales"); // the types' @db.space
|
|
83
|
+
|
|
84
|
+
@TableController(sales.Order, "api/sales/OrderSet")
|
|
85
|
+
export class OrderSetController extends ODataTableController<typeof sales.Order> {}
|
|
86
|
+
|
|
87
|
+
await space.connect(); // before app.init()
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
A table of the space (`space.getTable(sales.Order)`) binds as well, and can be created at import.
|
|
91
|
+
Never run schema sync over the space: the service owns its schema. Call `space.connect()` once before
|
|
92
|
+
Moost's `app.init()`, which creates the controllers and reads the tables' structure. `connect()`
|
|
93
|
+
reads with the space's own `init` unless it is given another, and resolves with the same
|
|
94
|
+
connection ever after (`space.connection`, `space.service`). A failed read rejects, and the app
|
|
95
|
+
does not start.
|
|
96
|
+
|
|
97
|
+
- **Drift.** When the live documents differ from the saved ones, the space warns once through its
|
|
98
|
+
`logger` (else `console.warn`): entity sets gone or added, sets allowing fewer writes,
|
|
99
|
+
properties added, removed or changed. `onDrift(drift, message)` replaces the warning.
|
|
100
|
+
- **A set the live service no longer has** keeps its table: every call of it throws a plain
|
|
101
|
+
`Error` before any request, a fault of the server's build (moost-db answers `500`); the
|
|
102
|
+
controller bases of `@atscript/moost-odata` answer it `404` first.
|
|
103
|
+
- **Other options** of the live form: the transport options of `@atscript/odata`, `copies`
|
|
104
|
+
(`annotate X as Y` copies that annotate properties), `name`, `sessions`, `logger`, `onClose`.
|
|
105
|
+
|
|
106
|
+
The fixed form `createODataSpace({ service, init })`, over an engine of `createODataService`, reads
|
|
107
|
+
no `$metadata`: for tests and tools. See [Moost Server](https://odata.atscript.dev/guide/server) and
|
|
108
|
+
[Live metadata](https://odata.atscript.dev/guide/live-metadata).
|
|
109
|
+
|
|
110
|
+
The entity sets of the service's value-help services (OData v4 `Common.ValueListReferences`, as
|
|
111
|
+
SAP RAP serves value helps; their types carry `@odata.valueHelpService`) are tables of the same
|
|
112
|
+
space: `space.getTable(sales.CountryVH)` reads the set at the value-help service's own root,
|
|
113
|
+
through `service.valueHelpService(…)`, with the space's `init`. The space reads their documents
|
|
114
|
+
with the live `$metadata`.
|
|
115
|
+
|
|
116
|
+
### Credentials and sessions
|
|
117
|
+
|
|
118
|
+
The connection to the service is an integration: the server calls it with credentials it was
|
|
119
|
+
given (a technical user), never with those of its own end users. `init` answers them; it is called
|
|
120
|
+
once per table operation, so the reads a write makes (its ETag read, the CSRF token fetch) and the
|
|
121
|
+
write itself carry the same answer. The strategies of `@atscript/moost-odata` (`basicAuth`,
|
|
122
|
+
`bearerAuth`, `staticHeaders`, `callTimeout`, `combineAuth`) build it; any function answering
|
|
123
|
+
`{ headers }` does too.
|
|
124
|
+
|
|
125
|
+
The space keeps the sessions itself, one per credential the calls send (the SHA-256 of a call's
|
|
126
|
+
headers, `Accept-Language` left out): the service's cookies and the CSRF token a write needs. With
|
|
127
|
+
one technical user that is one session; a rotating token starts a new one, and two credentials
|
|
128
|
+
never share a session or a token. Calls without credentials stay stateless.
|
|
129
|
+
|
|
130
|
+
| `sessions` | Meaning |
|
|
131
|
+
| -------------- | ------------------------------------------------------------------------------------------ |
|
|
132
|
+
| not set | `{ ttl: DEFAULT_SESSION_TTL, max: DEFAULT_MAX_SESSIONS }`: 30 minutes after last use, 1000 |
|
|
133
|
+
| `{ ttl, max }` | milliseconds a session is kept after its last use; the most kept (the longest unused goes) |
|
|
134
|
+
| `false` | no session: every call stateless, so writes to an SAP service fail |
|
|
135
|
+
|
|
136
|
+
Keep `ttl` at or below the idle timeout of the service's own sessions. A write the service refuses
|
|
137
|
+
for want of a session (no credentials, or `sessions: false`) fails with a `DbError`
|
|
138
|
+
`STORE_FAILED` whose `cause` has the `hint` `"session-required"` (see [Errors](#errors)).
|
|
139
|
+
|
|
140
|
+
When the models name the language they were saved in (the plugin's `language`), every data call
|
|
141
|
+
also sends `Accept-Language: <language>` unless `init` sets one, so labels and data texts come in
|
|
142
|
+
one language.
|
|
143
|
+
|
|
144
|
+
### What is supported
|
|
145
|
+
|
|
146
|
+
| Table API | OData |
|
|
147
|
+
| ----------------------------------------- | ------------------------------------------------------------------- |
|
|
148
|
+
| `findById`, `findOne` by the whole key | key read, `EntitySet(key)`; a missing entity is `null` |
|
|
149
|
+
| `findMany`, `findOne` | `$filter`, `$select`, `$orderby`, `$skip`, `$top` |
|
|
150
|
+
| `count` | `/$count` |
|
|
151
|
+
| `findManyWithCount` | one request with `$inlinecount=allpages` (v2) or `$count=true` (v4) |
|
|
152
|
+
| `search`, `searchWithCount` | `search` (v2) or `$search` (v4), where the entity set allows it |
|
|
153
|
+
| `$with` | `$expand` in the same request; see [Relations](#relations) |
|
|
154
|
+
| complex types | nested objects; paths are written `Address.City` |
|
|
155
|
+
| `insertOne`, `insertMany` | `POST`, one per row; see [Writes](#writes) |
|
|
156
|
+
| `updateOne`, `bulkUpdate` | `MERGE` (v2) / `PATCH` (v4) of one entity by key |
|
|
157
|
+
| `replaceOne`, `bulkReplace` | `PUT` of one entity by key |
|
|
158
|
+
| `deleteOne` | `DELETE` of one entity by key |
|
|
159
|
+
| `aggregate` | one `$apply` request (OData v4); see [Aggregation](#aggregation) |
|
|
160
|
+
| `updateMany`, `replaceMany`, `deleteMany` | refused: `ODataNotSupportedError` |
|
|
161
|
+
|
|
162
|
+
- Whether a field can be filtered or sorted by comes from the service metadata and is reported
|
|
163
|
+
through `canFilterField` / `canSortField`, so `/meta` shows it and a request that ignores it is
|
|
164
|
+
refused before the service is called. `filterOps` lists the operators the service allows on a
|
|
165
|
+
field (none where it cannot filter, the operators of a filter restriction where it declares one:
|
|
166
|
+
single-value `$eq`, multi-value `$eq $in`, interval `$eq $gt $gte $lt $lte`); any other operator,
|
|
167
|
+
a lone `$exists` included, is refused the same way. A primary-key or foreign-key field always
|
|
168
|
+
takes `$eq` and `$in`, which the table layer reads it by, even where the service declares it
|
|
169
|
+
non-filterable or single-value.
|
|
170
|
+
- The primary key the table layer appends to every `$sort` is left out of `$orderby` where the
|
|
171
|
+
service cannot sort by it.
|
|
172
|
+
A `$sort` by such a key that the caller asks for is refused like any other field the service
|
|
173
|
+
cannot sort by; that includes the primary-key order `@atscript/moost-db` gives `/pages` when
|
|
174
|
+
the request has no `$sort`.
|
|
175
|
+
- A read without `$limit` follows the paging links of the service to the end. When the service
|
|
176
|
+
cuts it into more pages than the engine follows (`maxPages` of the service), the read fails
|
|
177
|
+
instead of returning a part of the rows.
|
|
178
|
+
- A draft-enabled entity set (`Common.DraftRoot` / `Common.DraftNode`) is read and counted for its
|
|
179
|
+
active entities: a filter with no condition on `IsActiveEntity` of its own gets
|
|
180
|
+
`IsActiveEntity eq true`, so the user's drafts do not show up next to them. A read by key reads
|
|
181
|
+
the key it is given.
|
|
182
|
+
|
|
183
|
+
### Relations
|
|
184
|
+
|
|
185
|
+
The adapter reads relations inside the read (`supportsWithInRead()`): the relations of `$with`
|
|
186
|
+
ride the request of the rows as `$expand`, and every row comes back with its related rows under
|
|
187
|
+
the navigation name. There is no second read by the keys of the rows, so a relation to an entity
|
|
188
|
+
set that can only be reached through a navigation loads, and the URL does not grow with the
|
|
189
|
+
number of rows. (A filter an entity set declares mandatory is advisory: reads are sent without
|
|
190
|
+
it, and a service that needs it answers `400`, `INVALID_QUERY` with its message.)
|
|
191
|
+
|
|
192
|
+
```ts
|
|
193
|
+
await orders.findMany({
|
|
194
|
+
filter: { CustomerId: "C0001" },
|
|
195
|
+
controls: {
|
|
196
|
+
$select: ["OrderId", "OrderDate"],
|
|
197
|
+
$with: [{ name: "Items", filter: { Product: "HT-1000" }, controls: { $select: ["ItemNo"] } }],
|
|
198
|
+
},
|
|
199
|
+
});
|
|
200
|
+
// v4: Orders?$filter=CustomerId eq 'C0001'&$select=OrderId,OrderDate
|
|
201
|
+
// &$expand=Items($filter=Product eq 'HT-1000';$select=ItemNo,OrderId)
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
- **Every part of an entry reaches the service**: the relation's filter (the client's and any a
|
|
205
|
+
controller conjoins into the entry, such as a row scope in `validateControls`), its `$select`,
|
|
206
|
+
its `$sort` and its nested `$with`. The related rows are cut back to their selection and their
|
|
207
|
+
key, at every depth, as atscript-db's relation loader keeps the key on other adapters (the
|
|
208
|
+
service always sends it). Nothing in an entry is dropped: what the service cannot express is
|
|
209
|
+
refused.
|
|
210
|
+
- **A filter that can never match** (an empty `$in`, say a controller's scope for a user with no
|
|
211
|
+
values) leaves the relation out of the request: every row has it empty, `[]` or `null`, as
|
|
212
|
+
atscript-db answers it on other adapters. Nothing like `$filter=false` is sent.
|
|
213
|
+
- **OData v2** has no options inside an expansion except the selection (written `Nav/Property` in
|
|
214
|
+
the root `$select`). A filter or a sort of a relation, a scope added by a controller included, is
|
|
215
|
+
refused with a `DbError` `NOT_SUPPORTED` before any request; moost-db answers `400`.
|
|
216
|
+
- **No paging inside `$with`.** The table layer refuses `$skip` and `$limit` in a `$with` entry
|
|
217
|
+
(`NOT_SUPPORTED`).
|
|
218
|
+
- **One service.** A relation is read only to a table of the same service (`sharesStoreWith`).
|
|
219
|
+
- **Navigations without a foreign key** (`@db.rel.store`, which the generator emits where the
|
|
220
|
+
service declares no usable referential constraint) are read the same way. The table layer
|
|
221
|
+
refuses a value in one on every write; only relations with foreign keys (`@db.rel.to` /
|
|
222
|
+
`.from`) give a foreign key to a value help.
|
|
223
|
+
- Related rows carry no version token: the adapter reads no ETag of an expanded entity (read the
|
|
224
|
+
row itself for its token).
|
|
225
|
+
|
|
226
|
+
### Aggregation
|
|
227
|
+
|
|
228
|
+
`table.aggregate()` (moost-db's `/query` with `$groupBy`) is one OData v4 `$apply` request, on an
|
|
229
|
+
entity set the service declares `$apply` for (`Aggregation.ApplySupported` on the set, its type or
|
|
230
|
+
the container, as RAP writes it):
|
|
231
|
+
|
|
232
|
+
```ts
|
|
233
|
+
await lineItems.aggregate({
|
|
234
|
+
filter: { CompanyCode: "1000" },
|
|
235
|
+
controls: {
|
|
236
|
+
$groupBy: ["CompanyCode", "Currency"],
|
|
237
|
+
$select: ["CompanyCode", "Currency", { $fn: "sum", $field: "Amount", $as: "total" }],
|
|
238
|
+
$sort: { total: -1 },
|
|
239
|
+
$limit: 10,
|
|
240
|
+
},
|
|
241
|
+
});
|
|
242
|
+
// LineItems?$apply=filter(CompanyCode eq '1000')/groupby((CompanyCode,Currency),
|
|
243
|
+
// aggregate(Amount with sum as total))&$orderby=total desc&$top=10
|
|
244
|
+
// → [{ CompanyCode: "1000", Currency: "EUR", total: "1500.25" }, …]
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
- **Functions** (`aggregateFns()`, `/meta.aggregateFns`): `sum`, `avg`, `min`, `max` on an SAP
|
|
248
|
+
service (RAP answers `$count` and `countdistinct` with `501`); on any other also `count(*)`
|
|
249
|
+
(`$count as …`) and `countDistinct`. `count` of a field has no `$apply` form.
|
|
250
|
+
- **`$having`** is a `filter()` after the grouping, **`$sort`**, **`$skip`** / **`$limit`** apply
|
|
251
|
+
to the groups, and **`$count`** answers `[{ count }]`, the number of groups
|
|
252
|
+
(`$top=0&$count=true`). `$groupBy: []` is one row over every matching row. A grouping without
|
|
253
|
+
aggregates gives the distinct values of a field (atscript-ui's distinct-values picker).
|
|
254
|
+
- **Rows**: the grouped fields (a member of a complex property nested, as in reads) and every
|
|
255
|
+
aggregate under its alias, decimals as strings. An alias the service could take for a property
|
|
256
|
+
(RAP refuses those, "already used") is sent under another name and comes back under yours.
|
|
257
|
+
- **Refused before any request** (`ODataNotSupportedError` for `aggregate`, `400` over HTTP, the
|
|
258
|
+
entry's `path` at the field or control): an entity set without `$apply` (every set on OData v2);
|
|
259
|
+
`sum` / `avg` of what is no number; a path through a navigation; `$search`. The table layer
|
|
260
|
+
refuses an amount aggregated without its currency (or unit) in `$groupBy`.
|
|
261
|
+
- **Left to the service**: which properties it lets you group by or aggregate, and an amount
|
|
262
|
+
compared in `$having` without its currency (RAP answers `400`, which reaches the caller as
|
|
263
|
+
`INVALID_QUERY` with its message).
|
|
264
|
+
- **Not supported**: calendar buckets, expressions, `first` / `last`, conditional aggregates,
|
|
265
|
+
field lists and two-level aggregates (the table layer refuses them, as the adapter does not
|
|
266
|
+
declare them); OData v2 analytical sets (`sap:semantics="aggregate"`), which aggregate by the
|
|
267
|
+
dimensions and measures of a `$select` with no function to choose.
|
|
268
|
+
|
|
269
|
+
### Writes
|
|
270
|
+
|
|
271
|
+
Every write addresses one entity by its key, as OData writes it:
|
|
272
|
+
|
|
273
|
+
| Adapter method | OData |
|
|
274
|
+
| ----------------------------------------- | ---------------------------------------------------------------------- |
|
|
275
|
+
| `insertOne`, `insertMany` | `POST` to the entity set, one request per row |
|
|
276
|
+
| `updateOne` | `MERGE` (v2) or `PATCH` (v4) to `Set(key)` with the changed properties |
|
|
277
|
+
| `replaceOne` | `PUT` to `Set(key)` with the whole entity |
|
|
278
|
+
| `deleteOne` | `DELETE` of `Set(key)` |
|
|
279
|
+
| `updateMany`, `replaceMany`, `deleteMany` | refused (`ODataNotSupportedError`): there is no write by query |
|
|
280
|
+
|
|
281
|
+
- **By key only.** The table layer addresses a row by its primary key, which arrives as a filter;
|
|
282
|
+
a filter that is not exactly an equality on every key property is refused (a row scope added to
|
|
283
|
+
a delete, for instance). Writes by a filter are refused.
|
|
284
|
+
- **The inserted key** is the one the service reports (from the returned entity, else from the
|
|
285
|
+
`Location` / `OData-EntityId` header), else the one the row carried. A key the service computes
|
|
286
|
+
(`Core.Computed`, v2 `StoreGeneratedPattern="Identity"`, `sap:creatable="false"` with
|
|
287
|
+
`sap:updatable="false"`) is never sent.
|
|
288
|
+
- **What the service maintains is not sent**: computed properties in any write, immutable ones in
|
|
289
|
+
an update or replace, update-only ones in a create. They are left out of the request, so a row
|
|
290
|
+
that was read can be written back as it is. An update made only of such properties sends
|
|
291
|
+
nothing and reports `modifiedCount: 0`.
|
|
292
|
+
- **What the service computes** (`Core.Computed`, `StoreGeneratedPattern`, v2 `sap:creatable` and
|
|
293
|
+
`sap:updatable` false) carries `@db.default.store` in the generated type when it is a scalar:
|
|
294
|
+
the service fills it, so the table layer's insert validation does not demand it and an insert
|
|
295
|
+
leaves it out (the adapter lists `store` in `nativeDefaultFns()`). A value given anyway is not
|
|
296
|
+
sent. A computed key comes back in `insertedId` from the service's answer: no placeholder
|
|
297
|
+
needed. A computed complex value or collection gets no default.
|
|
298
|
+
- **A GUID with a computed default** (`Core.ComputedDefaultValue`, SAP CAP's `cuid` keys)
|
|
299
|
+
carries `@db.default.uuid`: the table layer generates one when the row leaves it out, and it is
|
|
300
|
+
sent, as a key given by the caller is; the service takes it.
|
|
301
|
+
- **Complex values** in an update replace the stored value: the members a value leaves out are sent
|
|
302
|
+
as `null`, because OData merges a complex value into the stored one. A patch with
|
|
303
|
+
`@db.patch.strategy "merge"` is sent as a merge into the stored value.
|
|
304
|
+
- **Collections** are written as a whole (a plain array, or `$replace`): OData has no operator
|
|
305
|
+
for part of a collection. The array operators `$insert`, `$upsert`, `$update` and `$remove` are
|
|
306
|
+
emulated by the table layer, which reads the entity and writes the whole collection back. Nothing
|
|
307
|
+
keeps a concurrent change of the collection from being overwritten in between: last write wins,
|
|
308
|
+
as for every write.
|
|
309
|
+
- **Field operators** (`$inc`, `$mul`) and an integer version (`$cas` with a number) are refused:
|
|
310
|
+
OData has neither. A version token is compared (see [ETags](#etags-the-version-token)).
|
|
311
|
+
- **What the entity set allows.** A set that is not insertable, updatable or deletable (`sap:creatable`,
|
|
312
|
+
`Capabilities.InsertRestrictions`, …) refuses that write, and a set that is draft-enabled, can only
|
|
313
|
+
be reached through a navigation, or holds the parameters of a parameterized view
|
|
314
|
+
refuses every write, with `ODataNotSupportedError` before any request. The rule and its messages
|
|
315
|
+
are `writeRefusal` of `@atscript/odata`, which the browser client follows too; `allowedWrites` says
|
|
316
|
+
the same in advance. The refusal names the adapter method that was called (a single insert
|
|
317
|
+
reaches the adapter as `insertMany`).
|
|
318
|
+
- **A missing entity** matches nothing: `{ matchedCount: 0 }` for an update or replace,
|
|
319
|
+
`{ deletedCount: 0 }` for a delete.
|
|
320
|
+
- **No transactions.** `insertMany` of several rows sends one `$batch`, each row in a changeset of
|
|
321
|
+
its own (row after row with the space's `batch: false`): the rows after a refused one still run.
|
|
322
|
+
When rows are refused, the error is the first refused row's (its code), with the paths of every
|
|
323
|
+
refused row's entries naming it (`[1].City`, `[1]` for a message about the row as a whole) and an
|
|
324
|
+
entry `{ path: "[j]", code: "INSERTED", params: { insertedId } }` for each row created. `bulkUpdate` and `bulkReplace` write row by row as well, but the table layer calls the
|
|
325
|
+
adapter for each row without saying which, so their errors name no row. Nested writes are split
|
|
326
|
+
by the table layer into one write per row and table (OData deep insert is not used).
|
|
327
|
+
|
|
328
|
+
#### ETags: the version token
|
|
329
|
+
|
|
330
|
+
An entity set that declares optimistic concurrency (`Core.OptimisticConcurrency`, also with an
|
|
331
|
+
empty property list except on an SAP service, whose entities carry no ETag then, or v2
|
|
332
|
+
`ConcurrencyMode="Fixed"`) gets a version token from the generator:
|
|
333
|
+
`__etag: string` with `@db.column.version @db.column.version.token`, a field the service does
|
|
334
|
+
not have. Every read fills it with the entity's ETag (`__metadata.etag`, `@odata.etag`, or the
|
|
335
|
+
`ETag` header of a single read); it is never sent to the service, and cannot be filtered or sorted
|
|
336
|
+
by. (A set whose entity type has a property named `__etag` gets no token; the generator reports
|
|
337
|
+
it.)
|
|
338
|
+
|
|
339
|
+
- **With the token, real conflict detection.** An update or replace whose `$cas` carries the
|
|
340
|
+
token (`{ __etag: "<ETag>" }`; moost-db lifts the `__etag` of a request body into it) is sent
|
|
341
|
+
with it as `If-Match`, and nothing is read first. A `412` (someone changed the entity since the
|
|
342
|
+
caller's read) answers `{ matchedCount: 0 }`, which moost-db turns into its `409`
|
|
343
|
+
`VERSION_MISMATCH` with the entity's current token (or `404` when the entity is gone). A write
|
|
344
|
+
with nothing to send compares the token with the entity's.
|
|
345
|
+
- **Without the token, last write wins.** An update or replace without `$cas`, and every delete,
|
|
346
|
+
overwrites whatever changed since the caller read the entity. The service still requires
|
|
347
|
+
`If-Match`, so the adapter reads the entity right before the write and sends that ETag (`*`
|
|
348
|
+
for an entity that comes without one). A `412`
|
|
349
|
+
(a change in the moment between that read and the write) has the ETag read and the write sent
|
|
350
|
+
again, three attempts in all; then the write fails with `SERIALIZATION_FAILURE` (moost-db `503`).
|
|
351
|
+
|
|
352
|
+
Sets without the declaration have no token and are written without `If-Match`. A token in the
|
|
353
|
+
row of an insert is not sent. The table layer refuses a patch that carries `$cas` and writes no
|
|
354
|
+
column (a versioned touch).
|
|
355
|
+
|
|
356
|
+
### Errors
|
|
357
|
+
|
|
358
|
+
| Situation | Error |
|
|
359
|
+
| ------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------- |
|
|
360
|
+
| a read OData cannot express, or the service metadata forbids | `DbError` `INVALID_QUERY` with the path |
|
|
361
|
+
| a filter or sort of a relation on OData v2 | `DbError` `NOT_SUPPORTED` with the relation's path |
|
|
362
|
+
| the service answers `400` | `DbError` `INVALID_QUERY` with its messages, by field (see below) |
|
|
363
|
+
| a read needs more pages than the engine follows | `DbError` `INVALID_QUERY` on `$limit` |
|
|
364
|
+
| a write the entity set does not allow, a draft-enabled set, a write by filter or not by key, field operators | `ODataNotSupportedError`: a `DbError` `NOT_SUPPORTED` |
|
|
365
|
+
| a grouped read the entity set cannot answer (see [Aggregation](#aggregation)) | `ODataNotSupportedError` for `aggregate`: a `DbError` `NOT_SUPPORTED` |
|
|
366
|
+
| a payload the engine refuses (unknown property, bad value) | `DbError` `INVALID_QUERY`, entry `code` of the engine (`UNKNOWN_PROPERTY`, `INVALID_VALUE`, …) |
|
|
367
|
+
| the service answers `412` to a write with the caller's token | not an error: nothing matched (moost-db answers `409` with the current token) |
|
|
368
|
+
| the service answers `412` on each of three attempts (changed since the ETag was read) | `DbError` `SERIALIZATION_FAILURE` (retryable) |
|
|
369
|
+
| the service answers `409` | `DbError` `CONFLICT` with its messages, by field (see below) |
|
|
370
|
+
| a write by key answers `404` | not an error: nothing matched |
|
|
371
|
+
| the service answers `501` (not implemented: a method, function or query option) | `DbError` `NOT_SUPPORTED` with its messages, by field |
|
|
372
|
+
| the service answers another `5xx`, `401`, `403`, `428` or another status, a collection read `404`, no usable answer | `DbError` `STORE_FAILED` |
|
|
373
|
+
| a write without credentials, or with `sessions: false`, fails its CSRF check | `DbError` `STORE_FAILED`, the `cause`'s `hint` `"session-required"` |
|
|
374
|
+
| the service's gateway answers `504`, or the call is aborted or times out | `DbError` `STORE_TIMEOUT` |
|
|
375
|
+
| the service answers `429` | `DbError` `STORE_THROTTLED`, `params.retryAfter` (seconds) when it named a wait |
|
|
376
|
+
| a call of a table whose entity set the live service no longer has | a plain `Error`, before any request |
|
|
377
|
+
|
|
378
|
+
The original error is the `cause`. `@atscript/moost-db` answers a `DbError` `INVALID_QUERY` or
|
|
379
|
+
`NOT_SUPPORTED` with `400`, `CONFLICT` with `409`, `SERIALIZATION_FAILURE` with `503`, and the
|
|
380
|
+
store-failure codes `STORE_FAILED`, `STORE_TIMEOUT`, `STORE_THROTTLED` with `502`, `504` and `429`
|
|
381
|
+
(with `Retry-After`), on any controller, and logs every `5xx` with its cause. A store failure's
|
|
382
|
+
message is generic; what the service said stays in the cause. An `ODataNotSupportedError` is a
|
|
383
|
+
`DbError` with atscript-db's code `NOT_SUPPORTED`, one entry coded `NOT_SUPPORTED` without
|
|
384
|
+
`params`, and the adapter method as `operation`. The `DbError`s come from `dbFailureOf` of
|
|
385
|
+
`@atscript/odata`, the one mapping the browser client answers with too, and from `toDbError` for
|
|
386
|
+
what only a server answers so (its own credentials, connection and session). A `404` on a key read
|
|
387
|
+
is not an error (the entity is `null`); on a collection read it means the service or the entity set
|
|
388
|
+
is not where it was configured.
|
|
389
|
+
|
|
390
|
+
The messages of a `400` or `409` follow `messageEntries` of `@atscript/odata`, the rule the browser
|
|
391
|
+
client follows too, on v2 and v4: the service's leading message is the error's `message`, and an
|
|
392
|
+
entry of `errors` at its field when its target names one, else at `""` with the service's code when
|
|
393
|
+
it has one no detail repeats (`dbFailureOf`); every message with a target is an entry at
|
|
394
|
+
the field path it maps to (with the service's code); a detail without a target is an entry at `""`
|
|
395
|
+
unless it repeats the leading message; a pseudo target like SAP CAP's `/#TRANSIENT#` counts as
|
|
396
|
+
none.
|
|
397
|
+
|
|
398
|
+
## License
|
|
399
|
+
|
|
400
|
+
MIT
|
|
401
|
+
|
|
402
|
+
---
|
|
403
|
+
|
|
404
|
+
SAP, SAP S/4HANA and the other SAP products named here are trademarks or registered trademarks of SAP SE (or an SAP affiliate company) in Germany and other countries. Atscript OData is not affiliated with or endorsed by SAP SE.
|