@atscript/db-odata 0.0.0-stage → 0.0.2
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 +408 -2
- package/dist/index.cjs +1404 -0
- package/dist/index.d.cts +584 -0
- package/dist/index.d.mts +584 -0
- package/dist/index.mjs +1395 -0
- package/package.json +57 -3
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,584 @@
|
|
|
1
|
+
import { BaseDbAdapter, DbError, DbQuery, DbSpace, FilterExpr, TDbDefaultFn, TDbDeleteResult, TDbFieldMeta, TDbInsertManyResult, TDbInsertResult, TDbSpaceOptions, TDbUpdateResult, TDbVersion, TFieldOps, TSearchIndexInfo } from "@atscript/db";
|
|
2
|
+
import { EntitySetDescription, EntitySetWrite, ODataCallAnswer, ODataCallInit, ODataConnection, ODataOperationRef, ODataOperationVerdict, ODataRunInput, ODataService, ODataSource, ODataSourceOptions, ServiceModel } from "@atscript/odata";
|
|
3
|
+
import { AggregateFn, ComparisonOp } from "@uniqu/core";
|
|
4
|
+
|
|
5
|
+
//#region src/odata-adapter.d.ts
|
|
6
|
+
/** The name of the one search index an entity set with free-text search has. */
|
|
7
|
+
declare const ODATA_SEARCH_INDEX = "DEFAULT";
|
|
8
|
+
type Row = Record<string, unknown>;
|
|
9
|
+
interface ODataAdapterOptions {
|
|
10
|
+
/**
|
|
11
|
+
* What every call to the service is made with: the credentials the server was given, an abort
|
|
12
|
+
* signal, read option overrides, a session. Called once per adapter operation, at the time of
|
|
13
|
+
* the call, so it may read the request context. An error it throws reaches the caller
|
|
14
|
+
* unchanged.
|
|
15
|
+
*/
|
|
16
|
+
init?: () => ODataCallInit | undefined | Promise<ODataCallInit | undefined>;
|
|
17
|
+
/**
|
|
18
|
+
* The saved model of a live space (the model module's): an entity set the live service no
|
|
19
|
+
* longer has is described by it, and every call of its table is refused as gone. Without it,
|
|
20
|
+
* a set the service does not have fails the table. `createODataSpace` gives it.
|
|
21
|
+
*/
|
|
22
|
+
saved?: ServiceModel;
|
|
23
|
+
/**
|
|
24
|
+
* The connection of the space, once it is connected: what {@link ODataAdapter.callOperation}
|
|
25
|
+
* runs on (the operation types, the build's verdicts). `createODataSpace` gives it.
|
|
26
|
+
*/
|
|
27
|
+
connection?: () => ODataConnection | undefined;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* An `@atscript/db` adapter over an entity set of an OData v2 / v4 service, through the
|
|
31
|
+
* `@atscript/odata` engine. One instance serves one table; its entity set is the table's name
|
|
32
|
+
* (`@db.table`, which the generator writes as the entity set's name).
|
|
33
|
+
*
|
|
34
|
+
* Writes go one entity at a time, by key: create, partial update, replace and delete. Writes by a
|
|
35
|
+
* filter and field operators throw {@link ODataNotSupportedError} before any request. On an entity
|
|
36
|
+
* set with optimistic concurrency the table has a version token (the entity's ETag): an update or
|
|
37
|
+
* replace that carries it (`$cas`) is sent with it as `If-Match`, and a change made since the
|
|
38
|
+
* caller's read matches nothing, which moost-db answers as its version mismatch. One that does
|
|
39
|
+
* not carry it, and every delete, is last-write-wins: the adapter reads the current ETag right
|
|
40
|
+
* before the write, which satisfies the service's If-Match requirement but detects no change made
|
|
41
|
+
* since the caller's own read (a change between that read and the write has the write sent again,
|
|
42
|
+
* three attempts at most, then `SERIALIZATION_FAILURE`). A call the service does not serve is a
|
|
43
|
+
* `STORE_FAILED`, `STORE_TIMEOUT` or `STORE_THROTTLED` (see `toDbError`). There
|
|
44
|
+
* are no transactions: a write of several rows that fails midway leaves the rows written before
|
|
45
|
+
* it. The relations of `$with` ride the read as `$expand` (see {@link supportsWithInRead}).
|
|
46
|
+
*/
|
|
47
|
+
declare class ODataAdapter extends BaseDbAdapter {
|
|
48
|
+
/** What the adapter was created with: `createODataSpace` gives the adapters of a space one. */
|
|
49
|
+
readonly options: ODataAdapterOptions;
|
|
50
|
+
private _set?;
|
|
51
|
+
private _leaves?;
|
|
52
|
+
private _members?;
|
|
53
|
+
private _keyFields?;
|
|
54
|
+
private _aggregateFns?;
|
|
55
|
+
private readonly _fields;
|
|
56
|
+
private _service?;
|
|
57
|
+
private readonly provide;
|
|
58
|
+
/** The entity set (the table's name) and the value-help service the adapter was bound to. */
|
|
59
|
+
private _bound?;
|
|
60
|
+
/** What the entity set was described from: the live model, or the saved one for a gone set. */
|
|
61
|
+
private _described?;
|
|
62
|
+
private _gone;
|
|
63
|
+
constructor(
|
|
64
|
+
/**
|
|
65
|
+
* The service of the space (see {@link service}), or a function answering it once it is
|
|
66
|
+
* known: a live space connects after its tables are created.
|
|
67
|
+
*/
|
|
68
|
+
|
|
69
|
+
service: ODataService | (() => ODataService | undefined), /** What the adapter was created with: `createODataSpace` gives the adapters of a space one. */
|
|
70
|
+
|
|
71
|
+
options?: ODataAdapterOptions);
|
|
72
|
+
/**
|
|
73
|
+
* The service the table is read from: the one the adapter was created with, or, for a type of
|
|
74
|
+
* one of its value-help services (`@odata.valueHelpService`), that service's engine
|
|
75
|
+
* (`service.valueHelpService(…)`).
|
|
76
|
+
*
|
|
77
|
+
* @throws Error before the service is known (a live space not connected yet).
|
|
78
|
+
*/
|
|
79
|
+
get service(): ODataService;
|
|
80
|
+
/**
|
|
81
|
+
* Whether the live service no longer has the entity set (a live space only): every call of
|
|
82
|
+
* the table then throws a plain `Error`, a fault of the server's build (moost-db answers it
|
|
83
|
+
* `500`); the OData controller bases of `@atscript/moost-odata` answer such a table `404` first.
|
|
84
|
+
*/
|
|
85
|
+
get gone(): boolean;
|
|
86
|
+
/** Describes the entity set once the service is known; see {@link entitySet}. */
|
|
87
|
+
private describe;
|
|
88
|
+
/**
|
|
89
|
+
* Refuses every call of a table whose entity set the live service no longer has: the server's
|
|
90
|
+
* build no longer fits the service, which no request can avoid (a plain `Error`).
|
|
91
|
+
*/
|
|
92
|
+
private refuseGone;
|
|
93
|
+
/**
|
|
94
|
+
* Why an operation of the build bound to this entity set is refused: its verdict when the live
|
|
95
|
+
* service no longer has it or no longer fits the build's calls of it (see
|
|
96
|
+
* `ODataSpace.refusalOf`); `undefined` when it fits.
|
|
97
|
+
*/
|
|
98
|
+
refusalOf(operation: ODataOperationRef<any, unknown, unknown>): ODataOperationVerdict | undefined;
|
|
99
|
+
/**
|
|
100
|
+
* Calls an operation bound to this entity set (`runOperation` of `@atscript/odata`, the entity
|
|
101
|
+
* set filled in), with the space's credentials and session, as every call of the table: on the
|
|
102
|
+
* collection, or, for an operation called on rows, on `rows` (each with its key and, where the
|
|
103
|
+
* set has a version token, its `__etag`). The handle of an unbound operation (it names no entity
|
|
104
|
+
* set) is called unbound: a db action of the table over it. The parameters are validated against
|
|
105
|
+
* the live operation type (the validator's `ValidatorError`); what the call throws is mapped as
|
|
106
|
+
* the table's calls are (`toDbError`).
|
|
107
|
+
*
|
|
108
|
+
* A call that sends the caller's version token (a call that writes, on a row with its `__etag`;
|
|
109
|
+
* see `sendsRowToken`) that the service refuses with `412` resolves `null`: the entity changed
|
|
110
|
+
* since the caller read it, a version mismatch for the caller to answer (a Moost server answers
|
|
111
|
+
* it as moost-db answers a stale write: `409` with the current token, or `404`). Without the
|
|
112
|
+
* token the ETag is read first, and read again when the entity changed in between
|
|
113
|
+
* (`retryWhileChanged`, then `SERIALIZATION_FAILURE`).
|
|
114
|
+
*
|
|
115
|
+
* A row the service has no entity for (the ETag read before its call finds none, or the call is
|
|
116
|
+
* answered `404` or `410`) is a `DbError` `NOT_FOUND`, which the OData controller bases answer as
|
|
117
|
+
* moost-db answers a missing row (`notFoundError()`, `404`). An operation the live service no
|
|
118
|
+
* longer has never gets here: its verdict refuses it before any call.
|
|
119
|
+
*
|
|
120
|
+
* Several rows go out as one `$batch` by `input.grouping` (see `runOperation`; one call per row
|
|
121
|
+
* on a service created with `batch: false`, as a space's `batch: false` creates it). Each row of
|
|
122
|
+
* an `isolated` run that failed is in `failed`, its error mapped as a call on that row alone: a
|
|
123
|
+
* `DbError` (`toDbError`, `NOT_FOUND` above), or `null` for a `412` on its own `__etag` (its
|
|
124
|
+
* version mismatch, as for one row). A `changeSet` run the service refused throws its one error,
|
|
125
|
+
* mapped so (`SERIALIZATION_FAILURE` for a `412`, `CONFLICT` for a `404`: nothing was applied;
|
|
126
|
+
* the entries of a message that names a row at that row, `[1]`).
|
|
127
|
+
*
|
|
128
|
+
* @throws Error for an entity set the live service no longer has.
|
|
129
|
+
*/
|
|
130
|
+
callOperation(input: Omit<ODataRunInput, "entitySet">): Promise<ODataCallAnswer | null>;
|
|
131
|
+
registerReadable(...args: Parameters<BaseDbAdapter["registerReadable"]>): void;
|
|
132
|
+
/**
|
|
133
|
+
* The entity set this adapter reads, as the query layer describes it: from the live model, or,
|
|
134
|
+
* for a set the live service no longer has, from the saved one.
|
|
135
|
+
*/
|
|
136
|
+
get entitySet(): EntitySetDescription;
|
|
137
|
+
/** A complex type is a nested object; paths arrive as `Address.City`. */
|
|
138
|
+
supportsNestedObjects(): boolean;
|
|
139
|
+
/** Referential integrity is the service's business. */
|
|
140
|
+
supportsNativeForeignKeys(): boolean;
|
|
141
|
+
/**
|
|
142
|
+
* The relations of `$with` are read in the request of the rows, as `$expand`: every row comes
|
|
143
|
+
* back with its related rows under the navigation name. Each `$with` entry goes into the
|
|
144
|
+
* expansion as the table layer hands it over, with its filter (a relation's own and any a
|
|
145
|
+
* controller conjoined into the entry), its `$select` and `$sort` and its nested entries, so a
|
|
146
|
+
* scope a server adds to a relation reaches the service. OData v2 has no options in an
|
|
147
|
+
* expansion other than the selection: there a filter or sort of a relation is refused
|
|
148
|
+
* (`NOT_SUPPORTED`), never dropped. The table layer refuses paging a relation itself.
|
|
149
|
+
*
|
|
150
|
+
* A navigation the service declares no foreign key for (`@db.rel.store`) is read the same way.
|
|
151
|
+
*/
|
|
152
|
+
supportsWithInRead(): boolean;
|
|
153
|
+
/** A relation is read through `$expand`: only to a table of the same service. */
|
|
154
|
+
sharesStoreWith(other: BaseDbAdapter): boolean;
|
|
155
|
+
/**
|
|
156
|
+
* The aggregate functions of the entity set's `$apply` (`aggregateFns` of `describeEntitySet`):
|
|
157
|
+
* none on OData v2 and on a set the service declares no `Aggregation.ApplySupported` for; `sum`,
|
|
158
|
+
* `min`, `max`, `avg` on an SAP service; also `count` and `countDistinct` on any other. The
|
|
159
|
+
* table layer refuses any other function before {@link aggregate} is called.
|
|
160
|
+
*/
|
|
161
|
+
aggregateFns(): ReadonlySet<AggregateFn>;
|
|
162
|
+
/**
|
|
163
|
+
* `store`: a property the service computes (`@db.default.store`, which the generator emits) is
|
|
164
|
+
* left to the service when an insert leaves it out, and a value given for it is dropped by the
|
|
165
|
+
* engine. The key the service assigns comes back in `insertedId`.
|
|
166
|
+
*/
|
|
167
|
+
nativeDefaultFns(): ReadonlySet<TDbDefaultFn>;
|
|
168
|
+
/**
|
|
169
|
+
* The row version is the entity's ETag, which the service moves on every write: the generator
|
|
170
|
+
* emits a version token field on every entity set with optimistic concurrency. Reads fill it;
|
|
171
|
+
* an update or replace with `$cas` sends it as `If-Match`.
|
|
172
|
+
*/
|
|
173
|
+
supportsVersionTokens(): boolean;
|
|
174
|
+
/**
|
|
175
|
+
* Whether the service can compare the field with a value. A property whose type has no literal
|
|
176
|
+
* form (a stream, a geo value, `Edm.Untyped`) answers `false` although the query layer takes a
|
|
177
|
+
* null check on it: this method is about value comparison; {@link filterOps} lists the
|
|
178
|
+
* operators. A primary-key or foreign-key field is always filterable, by `$eq` and `$in`.
|
|
179
|
+
*/
|
|
180
|
+
canFilterField(fd: TDbFieldMeta): boolean;
|
|
181
|
+
/**
|
|
182
|
+
* The operators the service lets a filter use on the field: `allowedFilterOps` of
|
|
183
|
+
* `describeField` (what the query layer translates for the property's type, narrowed by the
|
|
184
|
+
* service's filter restriction). `[]` for a field the service does not let you filter by, so a
|
|
185
|
+
* lone `$exists` is refused there too. The table layer refuses any other operator before a
|
|
186
|
+
* request, and `/meta` serves the list.
|
|
187
|
+
*
|
|
188
|
+
* A primary-key or foreign-key field always keeps `$eq` and `$in`, which the table layer reads
|
|
189
|
+
* by itself (key reads, relation loads) and requires of such a field: a key the service declares
|
|
190
|
+
* non-filterable, or single-value only, still lists them. An equality on the whole key is a key
|
|
191
|
+
* read (`EntitySet(key)`), which needs no filter; any other filter on it the service judges.
|
|
192
|
+
*/
|
|
193
|
+
filterOps(fd: TDbFieldMeta): readonly ComparisonOp[];
|
|
194
|
+
/** The primary-key and foreign-key fields of the table. */
|
|
195
|
+
private get keyFields();
|
|
196
|
+
canSortField(fd: TDbFieldMeta): boolean;
|
|
197
|
+
isSearchable(): boolean;
|
|
198
|
+
getSearchIndexes(): TSearchIndexInfo[];
|
|
199
|
+
private fieldOf;
|
|
200
|
+
/**
|
|
201
|
+
* A filter that is exactly an equality on every key property is read by key
|
|
202
|
+
* (`EntitySet(key)`); anything else is the first row of a collection read. A missing entity is
|
|
203
|
+
* `null`.
|
|
204
|
+
*/
|
|
205
|
+
findOne(query: DbQuery): Promise<Row | null>;
|
|
206
|
+
/**
|
|
207
|
+
* Reads the matching rows; the engine follows the paging links of the service up to `$limit`.
|
|
208
|
+
* A read without `$limit` that the service cuts into more pages than the engine follows fails
|
|
209
|
+
* (`INVALID_QUERY` on `$limit`) instead of answering with a part of the rows.
|
|
210
|
+
*/
|
|
211
|
+
findMany(query: DbQuery): Promise<Row[]>;
|
|
212
|
+
/**
|
|
213
|
+
* `/$count`; a filter that is exactly an equality on every key property is a key read instead,
|
|
214
|
+
* which also works on a set that cannot count or demands a filter on other properties. The
|
|
215
|
+
* table layer asks this way whether the row of a patch with nothing to write exists.
|
|
216
|
+
*/
|
|
217
|
+
count(query: DbQuery): Promise<number>;
|
|
218
|
+
/** One request: the rows with the inline count of the service. */
|
|
219
|
+
findManyWithCount(query: DbQuery): Promise<{
|
|
220
|
+
data: Row[];
|
|
221
|
+
count: number;
|
|
222
|
+
}>;
|
|
223
|
+
search(text: string, query: DbQuery, indexName?: string): Promise<Row[]>;
|
|
224
|
+
searchWithCount(text: string, query: DbQuery, indexName?: string): Promise<{
|
|
225
|
+
data: Row[];
|
|
226
|
+
count: number;
|
|
227
|
+
}>;
|
|
228
|
+
/**
|
|
229
|
+
* A grouped read, as one OData v4 `$apply` request (`planAggregate` of `@atscript/odata`): the
|
|
230
|
+
* filter, the grouping with its aggregates, `$having` as a filter of the groups, then `$sort`,
|
|
231
|
+
* `$skip` and `$limit` over the groups; `$count` asks for the number of groups
|
|
232
|
+
* (`$top=0&$count=true`) and answers `[{ count }]`. Rows come back with the grouped fields
|
|
233
|
+
* (nested under a complex property) and every aggregate under its alias, decoded as reads are
|
|
234
|
+
* (a decimal as a string, a count as a number).
|
|
235
|
+
*
|
|
236
|
+
* Refused with {@link ODataNotSupportedError} before any request: a set that cannot aggregate
|
|
237
|
+
* (OData v2, no `Aggregation.ApplySupported`), and what the service model rules out (an amount
|
|
238
|
+
* whose currency is not grouped, `sum` / `avg` of what is no number, `count` of a field,
|
|
239
|
+
* `$search`). What only the service knows it answers itself.
|
|
240
|
+
*/
|
|
241
|
+
aggregate(query: DbQuery): Promise<Row[]>;
|
|
242
|
+
/**
|
|
243
|
+
* Creates the entity: `POST` to the entity set. Resolves with its key: the one the service
|
|
244
|
+
* reports (from the returned entity, else from the `Location` / `OData-EntityId` header), else
|
|
245
|
+
* the one in `data` when it was sent. A key property the service computes is not sent (the
|
|
246
|
+
* engine drops it), so its value in `data` does not count.
|
|
247
|
+
*/
|
|
248
|
+
insertOne(data: Row): Promise<TDbInsertResult>;
|
|
249
|
+
/**
|
|
250
|
+
* One `POST` per row. Several rows go out as one `$batch`, each row in a changeset of its own
|
|
251
|
+
* (on OData v4 the service is asked to continue past a failed part): it is not atomic, and the
|
|
252
|
+
* rows after a failed one still run. On a service created with `batch: false` (a space's
|
|
253
|
+
* `batch: false`) or without `$batch`, the rows go one after the other, on past a failed one,
|
|
254
|
+
* answered alike. The table
|
|
255
|
+
* layer inserts a single row this way too: one plain `POST`, its failure its own.
|
|
256
|
+
*
|
|
257
|
+
* Several rows answer `{ insertedCount, insertedIds }` in request order when every row was
|
|
258
|
+
* created. When rows failed it throws the first failed row's error (its code), with every
|
|
259
|
+
* failed row's entries at that row (`[1].City`, `[1]`) and, for each row created, an entry
|
|
260
|
+
* `{ path: "[j]", code: "INSERTED", message, params: { insertedId } }`; its message says how many
|
|
261
|
+
* rows were created and which failed (`rowsFailure` of `@atscript/odata`). Every row failed: the
|
|
262
|
+
* first row's message, no `INSERTED` entry. A `$batch` that failed as a whole: that failure,
|
|
263
|
+
* mapped as a single call's (nothing is known to have run).
|
|
264
|
+
*/
|
|
265
|
+
insertMany(data: Row[]): Promise<TDbInsertManyResult>;
|
|
266
|
+
/**
|
|
267
|
+
* Replaces the entity (`PUT`) addressed by `filter`, which has to be an equality on every key
|
|
268
|
+
* property. Properties the service maintains are not sent. An entity the service does not
|
|
269
|
+
* have matches nothing.
|
|
270
|
+
*/
|
|
271
|
+
replaceOne(filter: FilterExpr, data: Row, expectedVersion?: TDbVersion): Promise<TDbUpdateResult>;
|
|
272
|
+
/**
|
|
273
|
+
* Partially updates the entity (`MERGE` on v2, `PATCH` on v4) addressed by `filter`, which has
|
|
274
|
+
* to be an equality on every key property. A complex value in `data` replaces the stored one
|
|
275
|
+
* (its other members are sent as `null`); a dot path is merged into it. Field operators
|
|
276
|
+
* (`$inc`, `$mul`) are refused. An entity the service does not have matches nothing.
|
|
277
|
+
*/
|
|
278
|
+
updateOne(filter: FilterExpr, data: Row, ops?: TFieldOps, expectedVersion?: TDbVersion): Promise<TDbUpdateResult>;
|
|
279
|
+
/**
|
|
280
|
+
* Deletes the entity addressed by `filter`, which has to be an equality on every key property.
|
|
281
|
+
* An entity the service does not have is not deleted (`deletedCount: 0`). Last-write-wins, as
|
|
282
|
+
* an update without a version token: the current ETag is read right before the delete, again
|
|
283
|
+
* when the entity changed in between (`retryWhileChanged`).
|
|
284
|
+
*/
|
|
285
|
+
deleteOne(filter: FilterExpr): Promise<TDbDeleteResult>;
|
|
286
|
+
updateMany(): Promise<TDbUpdateResult>;
|
|
287
|
+
replaceMany(): Promise<TDbUpdateResult>;
|
|
288
|
+
deleteMany(): Promise<TDbDeleteResult>;
|
|
289
|
+
/** The service owns its schema: there is nothing to create. */
|
|
290
|
+
ensureTable(): Promise<void>;
|
|
291
|
+
private refuseByFilter;
|
|
292
|
+
/**
|
|
293
|
+
* The writes the service model allows on the entity set, as the adapter applies them
|
|
294
|
+
* (`allowedWrites` of `@atscript/odata`, the rule the browser client follows too): none on a set
|
|
295
|
+
* that is draft-enabled, can only be reached through a navigation, or holds the parameters of a
|
|
296
|
+
* parameterized view. Every other write is refused with {@link ODataNotSupportedError} before any
|
|
297
|
+
* request.
|
|
298
|
+
*/
|
|
299
|
+
get allowedWrites(): Record<EntitySetWrite, boolean>;
|
|
300
|
+
/**
|
|
301
|
+
* What the service model says about writing to the entity set, whatever the write sends:
|
|
302
|
+
* `writeRefusal` of `@atscript/odata`.
|
|
303
|
+
*/
|
|
304
|
+
private refuseUnless;
|
|
305
|
+
/** The key of a write by key; any other filter is refused (there is no write by query). */
|
|
306
|
+
private keyFor;
|
|
307
|
+
/**
|
|
308
|
+
* Runs a write: refuses what the service model does not allow before anything is asked, then
|
|
309
|
+
* calls `init` once and maps what the write throws (see `toDbError`).
|
|
310
|
+
*/
|
|
311
|
+
private write;
|
|
312
|
+
private create;
|
|
313
|
+
/** The row to create: the service issues the ETag, so a token given with it means nothing. */
|
|
314
|
+
private withoutToken;
|
|
315
|
+
/**
|
|
316
|
+
* The key of a created row: the one the service reports (from the returned entity, else from
|
|
317
|
+
* the `Location` / `OData-EntityId` header), else the one sent.
|
|
318
|
+
*/
|
|
319
|
+
private insertedId;
|
|
320
|
+
/**
|
|
321
|
+
* How a write by key reads the entity (`changeByKey` of `@atscript/odata`): its ETag, when the
|
|
322
|
+
* entity set declares optimistic concurrency (with or without naming the properties), read
|
|
323
|
+
* right before a write without the caller's token to be sent as `If-Match`; when the entity
|
|
324
|
+
* answers without one, `*` is sent (the set wants an If-Match). After a write with nothing to
|
|
325
|
+
* send, its existence is read
|
|
326
|
+
* by the key alone.
|
|
327
|
+
*
|
|
328
|
+
* The pre-read only satisfies the service's If-Match requirement (it would answer `428`
|
|
329
|
+
* otherwise). It is not optimistic concurrency: the ETag is read by the adapter, not carried
|
|
330
|
+
* from the caller's own read, so the write is last-write-wins and overwrites whatever changed
|
|
331
|
+
* since the caller read the entity. A change in the moment between this read and the write (a
|
|
332
|
+
* `412`) has the ETag read again and the write sent again (three attempts); conflict detection
|
|
333
|
+
* is the caller's version token (`$cas`, see {@link change}).
|
|
334
|
+
*/
|
|
335
|
+
private byKey;
|
|
336
|
+
/**
|
|
337
|
+
* An update or replace by key (`changeByKey` of `@atscript/odata`). With `expected` (the
|
|
338
|
+
* caller's version token, from `$cas`) it is sent as `If-Match` and nothing is read first: a
|
|
339
|
+
* `412` (the entity changed) and a `404` match nothing, which moost-db answers as its version
|
|
340
|
+
* mismatch (409, the current token) or a missing row. Without it, the ETag the set needs is
|
|
341
|
+
* read right before the write (last-write-wins), and read again when the entity changed in
|
|
342
|
+
* between (three attempts, then `SERIALIZATION_FAILURE`).
|
|
343
|
+
*/
|
|
344
|
+
private change;
|
|
345
|
+
/**
|
|
346
|
+
* The members of the complex value at `path`, from the table's stored fields
|
|
347
|
+
* (`storedDescriptors`: no navigation property or part of one).
|
|
348
|
+
*/
|
|
349
|
+
private membersOf;
|
|
350
|
+
/** Runs an operation with the call options of the current request, and maps what it throws. */
|
|
351
|
+
private call;
|
|
352
|
+
private get context();
|
|
353
|
+
/**
|
|
354
|
+
* The version token field of the table (`@db.column.version.token`, which the generator emits on
|
|
355
|
+
* an entity set with optimistic concurrency); `undefined` when it has none.
|
|
356
|
+
*/
|
|
357
|
+
private get token();
|
|
358
|
+
/**
|
|
359
|
+
* The rows of a read, each with its ETag as the version token, cut back to the selection, and
|
|
360
|
+
* their related rows to theirs.
|
|
361
|
+
*/
|
|
362
|
+
private rowsOf;
|
|
363
|
+
/** Puts each row's ETag into the version token field, when the table has one. */
|
|
364
|
+
private withTokens;
|
|
365
|
+
/**
|
|
366
|
+
* The paths an exclusion projection is taken from: every stored field (`storedDescriptors`:
|
|
367
|
+
* navigation properties are none) that holds no other field, a collection counting as one
|
|
368
|
+
* field.
|
|
369
|
+
*/
|
|
370
|
+
private get leaves();
|
|
371
|
+
/**
|
|
372
|
+
* The read of a query, with the `$with` entries as expansions: each translated by the adapter
|
|
373
|
+
* of its table (one of the same service, see {@link sharesStoreWith}), nested entries included.
|
|
374
|
+
* Related rows keep their key at every depth, whatever their selection.
|
|
375
|
+
*/
|
|
376
|
+
private shapeOf;
|
|
377
|
+
/** What the table layer hands {@link aggregate}, as the engine's grouped read. */
|
|
378
|
+
private aggregateSpecOf;
|
|
379
|
+
private countSpecOf;
|
|
380
|
+
private searchShapeOf;
|
|
381
|
+
private readWithCount;
|
|
382
|
+
}
|
|
383
|
+
//#endregion
|
|
384
|
+
//#region src/sessions.d.ts
|
|
385
|
+
/** How long an unused session is kept unless `ttl` says otherwise: 30 minutes. */
|
|
386
|
+
declare const DEFAULT_SESSION_TTL: number;
|
|
387
|
+
/** How many sessions a space keeps unless `max` says otherwise. */
|
|
388
|
+
declare const DEFAULT_MAX_SESSIONS = 1000;
|
|
389
|
+
/** The sessions a space keeps with the service. */
|
|
390
|
+
interface ODataSessionOptions {
|
|
391
|
+
/**
|
|
392
|
+
* Milliseconds a session is kept after its last use. Default {@link DEFAULT_SESSION_TTL}. Keep
|
|
393
|
+
* it at or below the idle timeout of the service's own sessions.
|
|
394
|
+
*/
|
|
395
|
+
ttl?: number;
|
|
396
|
+
/**
|
|
397
|
+
* The most sessions kept at once. Default {@link DEFAULT_MAX_SESSIONS}. When one more is
|
|
398
|
+
* needed, the one unused for the longest time is dropped.
|
|
399
|
+
*/
|
|
400
|
+
max?: number;
|
|
401
|
+
}
|
|
402
|
+
//#endregion
|
|
403
|
+
//#region src/space.d.ts
|
|
404
|
+
interface SpaceOptions extends Pick<ODataAdapterOptions, "init">, Pick<TDbSpaceOptions, "logger" | "onClose"> {
|
|
405
|
+
/**
|
|
406
|
+
* The sessions the space keeps with the service, one per credential the calls send (see
|
|
407
|
+
* `sessionKeeper`): `ttl` and `max`, or `false` for none (every call stateless; writes to an SAP
|
|
408
|
+
* service then fail for want of a session).
|
|
409
|
+
*/
|
|
410
|
+
sessions?: ODataSessionOptions | false;
|
|
411
|
+
}
|
|
412
|
+
/** A live space: the service is read from its `$metadata` by `connect()`. */
|
|
413
|
+
type ODataLiveSpaceOptions = SpaceOptions & Omit<ODataSourceOptions, "copies" | "verdicts" | "batch"> & {
|
|
414
|
+
/** See `ODataSourceOptions.copies`. */copies?: ODataSourceOptions["copies"];
|
|
415
|
+
/**
|
|
416
|
+
* `$batch` settings: `maxParts`, how many requests one `$batch` carries at most (default
|
|
417
|
+
* 100). A Moost server never auto-batches: its calls go out as each request makes them, and a
|
|
418
|
+
* `$batch` only where one request holds several independent calls (an operation on several
|
|
419
|
+
* rows, an insert of several rows). `false`: no `$batch` at all, those go one call per row (an
|
|
420
|
+
* operation on several rows as one changeset is refused, `400` `NOT_SUPPORTED`), the space's
|
|
421
|
+
* own `callOperation` too: its services are created with `batch: false`.
|
|
422
|
+
*/
|
|
423
|
+
batch?: false | {
|
|
424
|
+
maxParts?: number;
|
|
425
|
+
};
|
|
426
|
+
};
|
|
427
|
+
/** A space over an engine at hand, read from no `$metadata`: for tests and tools. */
|
|
428
|
+
type ODataFixedSpaceOptions = SpaceOptions & {
|
|
429
|
+
/**
|
|
430
|
+
* The OData service whose entity sets are the tables of the space. Created with `batch: false`,
|
|
431
|
+
* it makes no `$batch` (see `ODataLiveSpaceOptions.batch`).
|
|
432
|
+
*/
|
|
433
|
+
service: ODataService;
|
|
434
|
+
};
|
|
435
|
+
type ODataSpaceOptions = ODataLiveSpaceOptions | ODataFixedSpaceOptions;
|
|
436
|
+
/**
|
|
437
|
+
* A `DbSpace` over an OData service: its entity sets are its tables. A live space reads the
|
|
438
|
+
* service's `$metadata` with {@link connect}, before the application's controllers are created.
|
|
439
|
+
*/
|
|
440
|
+
declare class ODataSpace extends DbSpace {
|
|
441
|
+
/** The live source; absent on a space over an engine at hand. */
|
|
442
|
+
readonly source?: ODataSource;
|
|
443
|
+
private _connection?;
|
|
444
|
+
private readonly defaultInit?;
|
|
445
|
+
private readonly callInit?;
|
|
446
|
+
constructor(adapterFactory: () => ODataAdapter, options: Pick<TDbSpaceOptions, "logger" | "onClose">, from?: {
|
|
447
|
+
source?: ODataSource;
|
|
448
|
+
fixed?: ODataService; /** What `connect()` reads the `$metadata` with: the credentials the space was given. */
|
|
449
|
+
connect?: ODataAdapterOptions["init"]; /** What every call is made with: those credentials, the session, the language. */
|
|
450
|
+
call?: ODataAdapterOptions["init"];
|
|
451
|
+
});
|
|
452
|
+
/** The connection, once {@link connect} succeeded (from the start on a fixed space). */
|
|
453
|
+
get connection(): ODataConnection | undefined;
|
|
454
|
+
/** The engine of the connection. @throws Error before the space is connected. */
|
|
455
|
+
get service(): ODataService;
|
|
456
|
+
/**
|
|
457
|
+
* Reads the live `$metadata` and value-help documents, in the language the models were saved
|
|
458
|
+
* in, and makes the generated types live (`createODataSource` of `@atscript/odata`): call it
|
|
459
|
+
* once before `app.init()`, which creates the controllers and binds them to the tables. The
|
|
460
|
+
* read is made with `init`, else with the space's own `init` (the credentials the server is
|
|
461
|
+
* given). Resolves with the same connection ever after. What the live metadata changed against
|
|
462
|
+
* the saved one is logged once as a warning: entity sets gone or allowing fewer writes,
|
|
463
|
+
* properties added, removed or changed.
|
|
464
|
+
*
|
|
465
|
+
* @throws what reading `$metadata` throws: the `ODataError` of a refused or failed read, the
|
|
466
|
+
* parser's `Error` for an answer that is no `$metadata` document.
|
|
467
|
+
*/
|
|
468
|
+
connect(init?: ODataAdapterOptions["init"]): Promise<ODataConnection>;
|
|
469
|
+
/**
|
|
470
|
+
* Why an operation of the build is refused: its verdict when the live service no longer has it
|
|
471
|
+
* (`gone`) or changed it so that the build's calls no longer fit (`incompatible`); `undefined`
|
|
472
|
+
* when it fits. A live space holds the live service to the build: the verdicts are computed
|
|
473
|
+
* once, on `connect()`, when the live `$metadata` differs from the saved one.
|
|
474
|
+
*/
|
|
475
|
+
refusalOf(operation: ODataOperationRef<any, unknown, unknown>): ODataOperationVerdict | undefined;
|
|
476
|
+
/**
|
|
477
|
+
* Calls an operation of the service (`runOperation` of `@atscript/odata`), with the space's
|
|
478
|
+
* credentials, the session it keeps for them and its language, as the tables' calls are made: an
|
|
479
|
+
* unbound one, or a bound one with `entitySet` (a set's table calls its own with
|
|
480
|
+
* `ODataAdapter.callOperation`). An operation the live service no longer fits is refused first
|
|
481
|
+
* (see {@link refusalOf}); the parameters are validated against the live operation type (the
|
|
482
|
+
* validator's `ValidatorError`). What the call throws, and the error of each row in `failed`, is
|
|
483
|
+
* mapped as the adapter's `callOperation` maps it: a call that sends the caller's version token
|
|
484
|
+
* (a call that writes, on a row with its `__etag`) refused with `412` is that row's version
|
|
485
|
+
* mismatch, for the caller to answer (one row resolves `null`, a row of several is
|
|
486
|
+
* `{ index, error: null }` in `failed`), a row the service has no entity for a `DbError`
|
|
487
|
+
* `NOT_FOUND`, the rest as the tables' calls (`toDbError`).
|
|
488
|
+
*
|
|
489
|
+
* @throws Error before the space is connected.
|
|
490
|
+
*/
|
|
491
|
+
callOperation(input: ODataRunInput): Promise<ODataCallAnswer | null>;
|
|
492
|
+
}
|
|
493
|
+
/**
|
|
494
|
+
* Creates a `DbSpace` over an OData service: `space.getTable(Type)` returns a table that reads the
|
|
495
|
+
* entity set `Type` was generated from (or built from at runtime). A type of one of the service's
|
|
496
|
+
* value-help services (`@odata.valueHelpService`) is read at that service's root, through
|
|
497
|
+
* `service.valueHelpService(…)`, with the space's `init`.
|
|
498
|
+
*
|
|
499
|
+
* The generated types name the service as their `@db.space`: registered under that name with
|
|
500
|
+
* moost-db's `provideDbSpace(space, "sales")`, the space serves model-token controllers
|
|
501
|
+
* (`@TableController(sales.Order, …)`). Never run schema sync over it: the service owns its
|
|
502
|
+
* schema.
|
|
503
|
+
*
|
|
504
|
+
* Live (`baseUrl`, `model`, `types`, …): the tables can be created at import (the controllers'
|
|
505
|
+
* decorators); `connect()` reads the live `$metadata` before `app.init()`. The space holds the
|
|
506
|
+
* live service to the build: an operation of the build the live service no longer fits is
|
|
507
|
+
* refused (`refusalOf`, `callOperation`) and listed in the drift report. An entity set the live
|
|
508
|
+
* service no longer has keeps its table, and every call of it throws a plain `Error` (the
|
|
509
|
+
* server's build no longer fits the service: moost-db answers `500`); the OData controller bases
|
|
510
|
+
* of `@atscript/moost-odata` answer every endpoint of such a table `404` instead.
|
|
511
|
+
*
|
|
512
|
+
* Every call of a table is made with the space's `init` (credentials, a time limit), with the
|
|
513
|
+
* session the space keeps for the credentials it sends, and, when the models name the language
|
|
514
|
+
* they were saved in, with that language as `Accept-Language` (unless `init` names one), so
|
|
515
|
+
* labels and data texts come in one language.
|
|
516
|
+
*/
|
|
517
|
+
declare function createODataSpace(options: ODataSpaceOptions): ODataSpace;
|
|
518
|
+
//#endregion
|
|
519
|
+
//#region src/errors.d.ts
|
|
520
|
+
/**
|
|
521
|
+
* An operation the adapter refuses before any request: one OData cannot express (a write by a
|
|
522
|
+
* filter, a field operator, a grouped read `$apply` has no form for), or one the service model
|
|
523
|
+
* does not allow (a write to an entity set that is not insertable, updatable or deletable, or
|
|
524
|
+
* that is draft-enabled; a grouped read of a set without `$apply`, or of what it does not
|
|
525
|
+
* aggregate).
|
|
526
|
+
*
|
|
527
|
+
* A `DbError` with atscript-db's code `NOT_SUPPORTED` (the adapter cannot perform what was asked),
|
|
528
|
+
* which moost-db answers with `400`: the request can avoid it. Its one entry names no `params`, as
|
|
529
|
+
* moost-db's `NOT_SUPPORTED` does, and is coded `NOT_SUPPORTED`, or as the browser client codes the
|
|
530
|
+
* same refusal (`UNSUPPORTED_FIELD_OPERATOR` for a field operator, `CAS_NOT_SUPPORTED` for a
|
|
531
|
+
* version number); the browser client answers a write the entity set does not allow, and a
|
|
532
|
+
* grouped read it cannot answer, with the same body (`dbFailureOf` of `@atscript/odata`).
|
|
533
|
+
*/
|
|
534
|
+
declare class ODataNotSupportedError extends DbError {
|
|
535
|
+
name: string;
|
|
536
|
+
/** The adapter method that was called (`insertMany`, `updateOne`, `deleteMany`, `aggregate`, …). */
|
|
537
|
+
readonly operation: string;
|
|
538
|
+
/**
|
|
539
|
+
* @param message why it is refused; by default that the adapter does not support `operation`
|
|
540
|
+
* @param cause the engine's refusal; the entry's `path` is the one an `ODataQueryError` names
|
|
541
|
+
* (the field or control it is about), else empty
|
|
542
|
+
* @param entry the entry's `path` and `code` where the browser client names the same refusal
|
|
543
|
+
* so (a field operator at its field, `UNSUPPORTED_FIELD_OPERATOR`; a version number at `$cas`,
|
|
544
|
+
* `CAS_NOT_SUPPORTED`); by default `NOT_SUPPORTED` at the path above
|
|
545
|
+
*/
|
|
546
|
+
constructor(operation: string, entitySet: string, message?: string, cause?: unknown, entry?: {
|
|
547
|
+
path?: string;
|
|
548
|
+
code?: string;
|
|
549
|
+
});
|
|
550
|
+
}
|
|
551
|
+
/** Also true for an `ODataNotSupportedError` created by another copy of this package. */
|
|
552
|
+
declare function isODataNotSupportedError(value: unknown): value is ODataNotSupportedError;
|
|
553
|
+
/**
|
|
554
|
+
* Turns what the OData engine throws into the `DbError` the atscript-db stack expects, with the
|
|
555
|
+
* original as `cause`:
|
|
556
|
+
*
|
|
557
|
+
* - every failure `dbFailureOf` of `@atscript/odata` maps (the one mapping both hosts answer
|
|
558
|
+
* with): `NOT_SUPPORTED` for what OData or the entity set cannot do (a write the set does not
|
|
559
|
+
* allow, a grouped read it cannot answer, what OData v2 cannot express); `INVALID_QUERY` for a
|
|
560
|
+
* read or write the engine refused (moost-db's codes on the entries: `UNKNOWN_FIELD`,
|
|
561
|
+
* `FIELD_NOT_PERMITTED`, …; a refused write keeps the engine's code), a value that cannot be
|
|
562
|
+
* written, a read past `maxPages` (on `$limit`) and a `400` of the service (its messages by
|
|
563
|
+
* field); `CONFLICT` for a `409` of the service; `SERIALIZATION_FAILURE` for a write that met a
|
|
564
|
+
* change between reading the entity's ETag and writing on every attempt; `STORE_THROTTLED`
|
|
565
|
+
* (`params.retryAfter`), `STORE_TIMEOUT` and `STORE_FAILED` for the service's `429`, `504` and
|
|
566
|
+
* other `5xx`
|
|
567
|
+
* - every other failure of the service, which `dbFailureOf` leaves to the host: `STORE_FAILED`
|
|
568
|
+
* (it could not be reached, refused the server's credentials or a write without a session,
|
|
569
|
+
* demanded a precondition its metadata does not declare (`PRECONDITION_REQUIRED` on the entry),
|
|
570
|
+
* answered another status or no OData answer; a `404` is one of them: a key read never gets here
|
|
571
|
+
* with it, the engine answers that with "no entity", and the adapter answers a write by key that
|
|
572
|
+
* meets one with "nothing matched"), `STORE_TIMEOUT` (the call was aborted)
|
|
573
|
+
*
|
|
574
|
+
* moost-db answers the `STORE_*` codes `502`, `504` and `429` (with `Retry-After`) and logs every
|
|
575
|
+
* `5xx` with the cause; their messages are generic, what the service said stays in the cause.
|
|
576
|
+
* Anything else (a `DbError`, an error of the `init` hook, a bug) is returned as it is.
|
|
577
|
+
*
|
|
578
|
+
* @param model the service model, which maps the targets of the service's messages to field paths
|
|
579
|
+
* @param parameters of a failed operation call: the parameters of the operation (`runParameters`
|
|
580
|
+
* of `@atscript/odata`), whose messages are then reported at the parameter they name
|
|
581
|
+
*/
|
|
582
|
+
declare function toDbError(error: unknown, entitySet: string, model?: ServiceModel, parameters?: readonly string[]): unknown;
|
|
583
|
+
//#endregion
|
|
584
|
+
export { DEFAULT_MAX_SESSIONS, DEFAULT_SESSION_TTL, ODATA_SEARCH_INDEX, ODataAdapter, type ODataAdapterOptions, type ODataFixedSpaceOptions, type ODataLiveSpaceOptions, ODataNotSupportedError, type ODataSessionOptions, ODataSpace, type ODataSpaceOptions, createODataSpace, isODataNotSupportedError, toDbError };
|