@olivia-db/client 0.1.1 → 0.1.3
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/dist/dsl.d.ts +498 -0
- package/dist/dsl.js +957 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +8 -0
- package/dist/native.d.ts +10 -0
- package/dist/native.js +19 -4
- package/package.json +5 -1
- package/src/dsl.ts +1228 -0
- package/src/index.ts +9 -0
- package/src/native.ts +26 -4
package/dist/dsl.d.ts
ADDED
|
@@ -0,0 +1,498 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Typed models and queries: the runtime of the code `oliviadb codegen --lang ts` generates
|
|
3
|
+
* (`sdk-dsl.md` of the specification). Generated code holds the schema's models, keys, paths,
|
|
4
|
+
* patches and new-node types as types, and tables that describe them; every request is made
|
|
5
|
+
* here, over HTTP (OliviaDbClient) or the native protocol (NativeClient of
|
|
6
|
+
* `@olivia-db/client/native`).
|
|
7
|
+
*
|
|
8
|
+
* const db = new Db(new OliviaDbClient({ baseUrl, apiKey }));
|
|
9
|
+
* const c = await db.get(Customer.key("42"), { tree: true });
|
|
10
|
+
* await db.txn((t) => {
|
|
11
|
+
* t.update(Customer.key("42").account("1001"), { add: { balance: amount.neg() } });
|
|
12
|
+
* t.update(Customer.key("42").account("1002"), { add: { balance: amount } });
|
|
13
|
+
* });
|
|
14
|
+
*
|
|
15
|
+
* A read's answer is decoded into the model from the node form the network API documents
|
|
16
|
+
* (`docs/network-api.md`, "Native aggregates"): over HTTP its JSON, over the native protocol
|
|
17
|
+
* the rows given in that form by the row reader (`Rows.toJson`). A node written with another
|
|
18
|
+
* layout of its type than the one the generated code was made for (an older version of the
|
|
19
|
+
* schema) is read by field number from that layout's fields (the connection's descriptors, or
|
|
20
|
+
* `GET /v1/layouts/{id}` over HTTP), fields it lacks with their declared defaults (`adapt`); a
|
|
21
|
+
* field whose type conflicts is a SchemaMismatchError naming it, never decoded by guess. A field the model requires and the answer
|
|
22
|
+
* lacks or shows masked is a DecodeError naming it, never a default.
|
|
23
|
+
*/
|
|
24
|
+
import { OliviaDbClient, type Json, type U64 } from "./index.ts";
|
|
25
|
+
/** An answer the generated code cannot read as its model (a field missing or of another type). */
|
|
26
|
+
export declare class DecodeError extends Error {
|
|
27
|
+
readonly name = "DecodeError";
|
|
28
|
+
}
|
|
29
|
+
/** A node written with a layout that conflicts with the generated code's: another type under the
|
|
30
|
+
* same field number, or a layout the connection cannot describe. */
|
|
31
|
+
export declare class SchemaMismatchError extends Error {
|
|
32
|
+
readonly name = "SchemaMismatchError";
|
|
33
|
+
}
|
|
34
|
+
/** An exact decimal (`decimal(p,s)`): `unscaled × 10^-scale`. */
|
|
35
|
+
export declare class Decimal {
|
|
36
|
+
readonly unscaled: bigint;
|
|
37
|
+
readonly scale: number;
|
|
38
|
+
constructor(unscaled: bigint, scale: number);
|
|
39
|
+
/** `-12.50`, `3`, `+0.5`; anything else throws DecodeError. */
|
|
40
|
+
static parse(s: string): Decimal;
|
|
41
|
+
private at;
|
|
42
|
+
neg(): Decimal;
|
|
43
|
+
add(o: Decimal): Decimal;
|
|
44
|
+
/** Equal values, whatever their scales (`1.50` equals `1.5`). */
|
|
45
|
+
equals(o: Decimal): boolean;
|
|
46
|
+
toString(): string;
|
|
47
|
+
toJSON(): string;
|
|
48
|
+
}
|
|
49
|
+
/** A calendar date (`date`): days since 1970-01-01. */
|
|
50
|
+
export declare class PlainDate {
|
|
51
|
+
readonly days: number;
|
|
52
|
+
constructor(days: number);
|
|
53
|
+
static of(y: number, m: number, d: number): PlainDate;
|
|
54
|
+
/** `YYYY-MM-DD`. */
|
|
55
|
+
static parse(s: string): PlainDate;
|
|
56
|
+
equals(o: PlainDate): boolean;
|
|
57
|
+
toString(): string;
|
|
58
|
+
toJSON(): string;
|
|
59
|
+
}
|
|
60
|
+
/** An instant in UTC (`timestamp`): microseconds since 1970-01-01T00:00:00Z. */
|
|
61
|
+
export declare class Instant {
|
|
62
|
+
readonly micros: bigint;
|
|
63
|
+
constructor(micros: bigint);
|
|
64
|
+
/** `YYYY-MM-DDTHH:MM:SS[.f{1,6}]Z`, the form the network API shows (it writes six digits). */
|
|
65
|
+
static parse(s: string): Instant;
|
|
66
|
+
equals(o: Instant): boolean;
|
|
67
|
+
toString(): string;
|
|
68
|
+
toJSON(): string;
|
|
69
|
+
}
|
|
70
|
+
/** A reference field (`ref T`): the target's levels below where the reference starts, as the
|
|
71
|
+
* network API shows them (`""`: none). */
|
|
72
|
+
export declare class Ref<T> {
|
|
73
|
+
readonly text: string;
|
|
74
|
+
readonly target?: T;
|
|
75
|
+
constructor(text: string);
|
|
76
|
+
isNone(): boolean;
|
|
77
|
+
toJSON(): string;
|
|
78
|
+
}
|
|
79
|
+
/** A value as the network API's JSON writes it. */
|
|
80
|
+
export declare function toJson(v: unknown): unknown;
|
|
81
|
+
/** A key part's text in a path (before percent-encoding). */
|
|
82
|
+
export declare function keyText(v: unknown): string;
|
|
83
|
+
/** How a field's JSON value becomes the model's value (`undefined`: not of the type). */
|
|
84
|
+
export interface Conv<T> {
|
|
85
|
+
from(v: unknown): T | undefined;
|
|
86
|
+
}
|
|
87
|
+
/** The conversions generated tables name, one per schema type. */
|
|
88
|
+
export declare const V: {
|
|
89
|
+
str: Conv<string>;
|
|
90
|
+
/** `int8..int32`, `uint8..uint16`. */
|
|
91
|
+
int: Conv<number>;
|
|
92
|
+
/** `int64`, `uint32`, `uint64`. */
|
|
93
|
+
big: Conv<bigint>;
|
|
94
|
+
float: Conv<number>;
|
|
95
|
+
bool: Conv<boolean>;
|
|
96
|
+
dec: Conv<Decimal>;
|
|
97
|
+
date: Conv<PlainDate>;
|
|
98
|
+
ts: Conv<Instant>;
|
|
99
|
+
bytes: Conv<Uint8Array>;
|
|
100
|
+
ref<T>(): Conv<Ref<T>>;
|
|
101
|
+
oneOf<T extends string>(members: readonly T[]): Conv<T>;
|
|
102
|
+
};
|
|
103
|
+
/** A field of a model (by the language's name in the table that holds it). */
|
|
104
|
+
export interface FieldDef {
|
|
105
|
+
/** The schema's name. */
|
|
106
|
+
readonly name: string;
|
|
107
|
+
readonly conv: Conv<unknown>;
|
|
108
|
+
/** Every read shows it. */
|
|
109
|
+
readonly required: boolean;
|
|
110
|
+
/** A discriminator: its cases' fields by member. */
|
|
111
|
+
readonly cases?: Readonly<Record<string, Readonly<Record<string, FieldDef>>>>;
|
|
112
|
+
}
|
|
113
|
+
/** A node type as generated code describes it. */
|
|
114
|
+
export interface ModelType<M> {
|
|
115
|
+
/** The type path (`Customer/Account`). */
|
|
116
|
+
readonly type: string;
|
|
117
|
+
/** The layout id (`sha256:...`) of the type in the schema the code was generated from. */
|
|
118
|
+
readonly layout: string;
|
|
119
|
+
/** The model's fields as [number, name, type text]: how a node written with another layout
|
|
120
|
+
* of the type is read (`adapt`). */
|
|
121
|
+
readonly numbered: ReadonlyArray<readonly [number, string, string]>;
|
|
122
|
+
readonly key: Readonly<Record<string, FieldDef>>;
|
|
123
|
+
readonly twin: boolean;
|
|
124
|
+
/** The fields a read shows (masked ones as strings). */
|
|
125
|
+
readonly fields: Readonly<Record<string, FieldDef>>;
|
|
126
|
+
/** Child listings a tree read fills: property -> the child type's name and model. */
|
|
127
|
+
readonly children: Readonly<Record<string, readonly [string, () => ModelType<unknown>]>>;
|
|
128
|
+
/** Every field of the type as declared: what patches and new nodes are written with. */
|
|
129
|
+
readonly decl: Readonly<Record<string, FieldDef>>;
|
|
130
|
+
/** Fields of the model that a write may send back (not masked). */
|
|
131
|
+
readonly writable: readonly string[];
|
|
132
|
+
/** Phantom: the model's type. */
|
|
133
|
+
readonly model?: M;
|
|
134
|
+
}
|
|
135
|
+
/** A writer layout's fields: [number, name, type text] each. */
|
|
136
|
+
export type LayoutFields = ReadonlyArray<readonly [number, string, string]>;
|
|
137
|
+
/** Where the fields of writer layouts come from: `undefined` when not asked for yet (decoding
|
|
138
|
+
* then stops with `NeedLayout`, the caller fetches it and decodes again), `null` when the
|
|
139
|
+
* connection does not know the layout. */
|
|
140
|
+
export interface Layouts {
|
|
141
|
+
fields(id: string): LayoutFields | null | undefined;
|
|
142
|
+
}
|
|
143
|
+
/** Decoding needs the fields of a writer layout not fetched yet. */
|
|
144
|
+
export declare class NeedLayout extends Error {
|
|
145
|
+
readonly id: string;
|
|
146
|
+
constructor(id: string);
|
|
147
|
+
}
|
|
148
|
+
/** The value a field has in a version written before the field existed (`spec/schema.md` 4.5),
|
|
149
|
+
* by its type as descriptors write it, in the network API's JSON form: an empty string, zero,
|
|
150
|
+
* false, the first member of an enum, 1970-01-01 for a date and 1970-01-01T00:00:00Z for a
|
|
151
|
+
* timestamp, bytes of zeros, an empty reference. */
|
|
152
|
+
export declare function defaultOf(ty: string): unknown;
|
|
153
|
+
/** Whether a value written as type `written` reads as type `model` under the same field number:
|
|
154
|
+
* the same type, an enum the model's widens (every member, name and stored value, still there),
|
|
155
|
+
* or a reference (its value is the target's path either way). */
|
|
156
|
+
export declare function compatible(written: string, model: string): boolean;
|
|
157
|
+
/** The node as the model's layout shows it (`spec/sdk-dsl.md` 1): as it is when written with that
|
|
158
|
+
* layout; else its fields read by field number from the writer layout's fields: a field the
|
|
159
|
+
* writer's layout lacks gets the value the server showed for it or its declared default, a field
|
|
160
|
+
* the model lacks is left out, a field under another name is renamed. A field whose written type
|
|
161
|
+
* conflicts with the model's is a SchemaMismatchError naming it and both types. */
|
|
162
|
+
export declare function adapt(mt: ModelType<unknown>, v: Json, layouts?: Layouts): Json;
|
|
163
|
+
/** The model of a node in the network API's node form: checked to be of the type, and read by
|
|
164
|
+
* the layout the generated code knows (a node of another layout through `adapt`). */
|
|
165
|
+
export declare function decode<M>(mt: ModelType<M>, node: Json, layouts?: Layouts): M;
|
|
166
|
+
/** Fields of an object of the language's names as the network API's `fields` (variant cases
|
|
167
|
+
* with their discriminator). */
|
|
168
|
+
export declare function encodeFields(defs: Readonly<Record<string, FieldDef>>, o: Record<string, unknown>, out?: Json): Json;
|
|
169
|
+
/** Percent-encoding of a path level or a query value (`spec/path-keys.md`). */
|
|
170
|
+
export declare function enc(s: string): string;
|
|
171
|
+
/** A path of the network API, built level by level; key parts are percent-encoded and joined
|
|
172
|
+
* by `,`, a twin's ordinal follows `~` (`spec/path-keys.md`). */
|
|
173
|
+
export declare class Path {
|
|
174
|
+
readonly text: string;
|
|
175
|
+
private constructor();
|
|
176
|
+
static root(aggregate: string): Path;
|
|
177
|
+
/** A node's level: its key parts (each a value of the key part's type). */
|
|
178
|
+
key(...parts: unknown[]): Path;
|
|
179
|
+
/** A twin by its ordinal, on the level just given (or alone for a type without a key). */
|
|
180
|
+
ordinal(ord: string): Path;
|
|
181
|
+
/** A child type's level. */
|
|
182
|
+
child(type: string): Path;
|
|
183
|
+
/** A level given as its encoded text. */
|
|
184
|
+
level(encoded: string): Path;
|
|
185
|
+
toString(): string;
|
|
186
|
+
}
|
|
187
|
+
/** How a path names a twin: by its ordinal (stable) or as the n-th with that key now. */
|
|
188
|
+
export type Twin = {
|
|
189
|
+
ord: string;
|
|
190
|
+
} | {
|
|
191
|
+
at: number;
|
|
192
|
+
};
|
|
193
|
+
/** The suffix a twin adds to its level. */
|
|
194
|
+
export declare function twinSuffix(t: Twin): string;
|
|
195
|
+
/** Where an insert puts a new twin among the twins with its key. */
|
|
196
|
+
export type Position = "last" | "first" | {
|
|
197
|
+
before: string;
|
|
198
|
+
} | {
|
|
199
|
+
after: string;
|
|
200
|
+
};
|
|
201
|
+
/** Grants. Generated path classes carry the brand of each operation the profile they were
|
|
202
|
+
* generated for grants (every one without `--profile`): an operation it does not grant does
|
|
203
|
+
* not type-check. */
|
|
204
|
+
export declare const GET: unique symbol;
|
|
205
|
+
export declare const TREE: unique symbol;
|
|
206
|
+
export declare const LIST: unique symbol;
|
|
207
|
+
export declare const INSERT: unique symbol;
|
|
208
|
+
export declare const UPDATE: unique symbol;
|
|
209
|
+
export declare const DELETE: unique symbol;
|
|
210
|
+
export declare const WATCH: unique symbol;
|
|
211
|
+
/** A path to one node of a type. */
|
|
212
|
+
export declare class NodePath<M> {
|
|
213
|
+
readonly path: Path;
|
|
214
|
+
readonly type: ModelType<M>;
|
|
215
|
+
constructor(path: Path, type: ModelType<M>);
|
|
216
|
+
toString(): string;
|
|
217
|
+
}
|
|
218
|
+
/** The nodes of one type under one parent (or an aggregate's roots); `N` is what an insert takes. */
|
|
219
|
+
export declare class CollPath<M, N> {
|
|
220
|
+
readonly path: Path;
|
|
221
|
+
readonly type: ModelType<M>;
|
|
222
|
+
readonly newNode?: N;
|
|
223
|
+
constructor(path: Path, type: ModelType<M>);
|
|
224
|
+
toString(): string;
|
|
225
|
+
}
|
|
226
|
+
export interface CanGet<M> extends NodePath<M> {
|
|
227
|
+
readonly [GET]: true;
|
|
228
|
+
}
|
|
229
|
+
export interface CanTree<M> extends CanGet<M> {
|
|
230
|
+
readonly [TREE]: true;
|
|
231
|
+
}
|
|
232
|
+
/** `P`: the patch type of the node's type under the profile. */
|
|
233
|
+
export interface CanUpdate<M, P> extends NodePath<M> {
|
|
234
|
+
readonly [UPDATE]: P;
|
|
235
|
+
}
|
|
236
|
+
export interface CanDelete<M> extends NodePath<M> {
|
|
237
|
+
readonly [DELETE]: true;
|
|
238
|
+
}
|
|
239
|
+
export interface CanList<M> extends CollPath<M, unknown> {
|
|
240
|
+
readonly [LIST]: true;
|
|
241
|
+
}
|
|
242
|
+
export interface CanInsert<M, N> extends CollPath<M, N> {
|
|
243
|
+
readonly [INSERT]: true;
|
|
244
|
+
}
|
|
245
|
+
/** A path whose changes the profile lets watch; `C` is the union of its changes' nodes
|
|
246
|
+
* (`{ type, node }`: the watched type and the types under it). */
|
|
247
|
+
export interface CanWatch<C> {
|
|
248
|
+
readonly [WATCH]: C;
|
|
249
|
+
readonly path: Path;
|
|
250
|
+
/** The types a change may be of: by type name, the name the change carries and the model. */
|
|
251
|
+
watchTypes(): Readonly<Record<string, readonly [string, ModelType<unknown>]>>;
|
|
252
|
+
}
|
|
253
|
+
/** What a change did. */
|
|
254
|
+
export type Op = "insert" | "replace" | "delete";
|
|
255
|
+
/** One record of the change stream (`docs/network-api.md`, "Change stream"): the node before
|
|
256
|
+
* and after, each as its model. */
|
|
257
|
+
export interface Change<C> {
|
|
258
|
+
/** The idempotency key: the same for the same change on every copy. */
|
|
259
|
+
key: string;
|
|
260
|
+
/** Where to resume after this record. */
|
|
261
|
+
position: string;
|
|
262
|
+
path: string;
|
|
263
|
+
type: string;
|
|
264
|
+
op: Op;
|
|
265
|
+
version: bigint;
|
|
266
|
+
before?: C;
|
|
267
|
+
after?: C;
|
|
268
|
+
}
|
|
269
|
+
/** A batch of the change stream and the position after it. */
|
|
270
|
+
export interface Changes<C> {
|
|
271
|
+
changes: Change<C>[];
|
|
272
|
+
position: string;
|
|
273
|
+
}
|
|
274
|
+
/** A change record in the HTTP JSON form: each side decoded as a node of the record's type,
|
|
275
|
+
* key and layout (the layout of the version shown last). */
|
|
276
|
+
export declare function changeOf<C>(v: Json, types: Readonly<Record<string, readonly [string, ModelType<unknown>]>>, layouts?: Layouts): Change<C>;
|
|
277
|
+
/** What an update changes: fields set, numbers added (the network API's relative update). */
|
|
278
|
+
export interface PatchOf<S, A> {
|
|
279
|
+
set?: S;
|
|
280
|
+
add?: A;
|
|
281
|
+
}
|
|
282
|
+
/** What the typed layer needs of the native protocol client (`NativeClient`). */
|
|
283
|
+
export interface NativeConn {
|
|
284
|
+
get(path: string, params?: Record<string, string>): Promise<RowsLike>;
|
|
285
|
+
lookup(index: string, params?: Record<string, string>): Promise<RowsLike>;
|
|
286
|
+
search(aggregate: string, body: Json): Promise<RowsLike>;
|
|
287
|
+
changes(paths: string[], from: string): AsyncGenerator<{
|
|
288
|
+
toJson(): Json;
|
|
289
|
+
}>;
|
|
290
|
+
/** The fields of a writer layout a descriptor of the connection described. */
|
|
291
|
+
layoutFields?(id: string): LayoutFields | undefined;
|
|
292
|
+
txn(body: Json): Promise<[number, Json]>;
|
|
293
|
+
write(method: string, path: string, body: Json): Promise<[number, Json]>;
|
|
294
|
+
}
|
|
295
|
+
interface NodeLike {
|
|
296
|
+
readonly hit: string | undefined;
|
|
297
|
+
readonly parent: string | undefined;
|
|
298
|
+
readonly score: number | undefined;
|
|
299
|
+
toJson(parent: string, defaults: boolean): Json;
|
|
300
|
+
}
|
|
301
|
+
/** The rows of a native answer (`Rows`). */
|
|
302
|
+
export interface RowsLike {
|
|
303
|
+
toJson(listing: boolean): Json;
|
|
304
|
+
nodes(): NodeLike[];
|
|
305
|
+
entries(): Iterable<{
|
|
306
|
+
node: NodeLike;
|
|
307
|
+
} | {
|
|
308
|
+
path: string;
|
|
309
|
+
hitPath: string;
|
|
310
|
+
}>;
|
|
311
|
+
}
|
|
312
|
+
/** A write's answer. */
|
|
313
|
+
export interface Written {
|
|
314
|
+
version: bigint;
|
|
315
|
+
/** The written node's path, when the answer names it (an insert: a twin's path carries the
|
|
316
|
+
* ordinal the server gave it). */
|
|
317
|
+
path?: string;
|
|
318
|
+
}
|
|
319
|
+
export interface GetOptions {
|
|
320
|
+
/** With the node's subtree (each listing the profile grants). */
|
|
321
|
+
tree?: boolean;
|
|
322
|
+
/** At most this many children of every listing in the subtree. */
|
|
323
|
+
each?: number;
|
|
324
|
+
/** At a version. */
|
|
325
|
+
asOf?: U64;
|
|
326
|
+
}
|
|
327
|
+
export interface ListOptions {
|
|
328
|
+
/** Start after this key (its first parts: a value or an array of values) or a page's `next`
|
|
329
|
+
* given as `{ next }`. */
|
|
330
|
+
after?: unknown;
|
|
331
|
+
to?: unknown;
|
|
332
|
+
prefix?: unknown;
|
|
333
|
+
limit?: number;
|
|
334
|
+
asOf?: U64;
|
|
335
|
+
}
|
|
336
|
+
/** One page of a listing. */
|
|
337
|
+
export interface Page<M> {
|
|
338
|
+
nodes: M[];
|
|
339
|
+
/** Give it as `after: { next }` for the next page; undefined after the last. */
|
|
340
|
+
next: string | undefined;
|
|
341
|
+
version: bigint;
|
|
342
|
+
}
|
|
343
|
+
/** A lookup through a `unique` index: the record's root, or undefined. */
|
|
344
|
+
export declare class Unique<R> {
|
|
345
|
+
readonly kind = "unique";
|
|
346
|
+
readonly index: string;
|
|
347
|
+
readonly key: string;
|
|
348
|
+
readonly root: ModelType<R>;
|
|
349
|
+
constructor(index: string, parts: unknown[], root: ModelType<R>);
|
|
350
|
+
}
|
|
351
|
+
/** A lookup through an index that is not unique: every node found. */
|
|
352
|
+
export declare class Many<R> {
|
|
353
|
+
readonly kind = "many";
|
|
354
|
+
readonly index: string;
|
|
355
|
+
readonly key: string;
|
|
356
|
+
readonly root: ModelType<R>;
|
|
357
|
+
constructor(index: string, parts: unknown[], root: ModelType<R>);
|
|
358
|
+
}
|
|
359
|
+
/** One node found through an index. */
|
|
360
|
+
export interface LookupHit<R> {
|
|
361
|
+
/** The indexed node's path. */
|
|
362
|
+
path: string;
|
|
363
|
+
/** Its record's path (the root's). */
|
|
364
|
+
record: string;
|
|
365
|
+
/** The record's root, when the caller's profile shows it. */
|
|
366
|
+
root: R | undefined;
|
|
367
|
+
}
|
|
368
|
+
/** An aggregate that may be searched; `H` the union of its hits (`{ type, node }`). */
|
|
369
|
+
export declare class Searchable<H> {
|
|
370
|
+
readonly aggregate: string;
|
|
371
|
+
/** Hit types by type name: the name the hit carries and the model. */
|
|
372
|
+
readonly hits: Readonly<Record<string, readonly [string, ModelType<unknown>]>>;
|
|
373
|
+
readonly hit?: H;
|
|
374
|
+
constructor(aggregate: string, hits: Record<string, readonly [string, ModelType<unknown>]>);
|
|
375
|
+
}
|
|
376
|
+
/** A ranked search result. */
|
|
377
|
+
export interface Scored<H> {
|
|
378
|
+
hit: H;
|
|
379
|
+
path: string;
|
|
380
|
+
score: number;
|
|
381
|
+
}
|
|
382
|
+
/** A node of any type (`getUntyped`, `listUntyped`). */
|
|
383
|
+
export interface Node {
|
|
384
|
+
type: string;
|
|
385
|
+
path: string;
|
|
386
|
+
key: Json;
|
|
387
|
+
fields: Json;
|
|
388
|
+
layout: string;
|
|
389
|
+
/** Children by type, in a tree read. */
|
|
390
|
+
children?: Record<string, Node[]>;
|
|
391
|
+
}
|
|
392
|
+
/** How often `txn` runs its function again after a 412. */
|
|
393
|
+
export declare const TXN_RETRIES = 3;
|
|
394
|
+
/** A typed database over HTTP (OliviaDbClient) or the native protocol (NativeClient). */
|
|
395
|
+
export declare class Db {
|
|
396
|
+
private readonly conn;
|
|
397
|
+
constructor(client: OliviaDbClient | NativeConn);
|
|
398
|
+
/** Writer layouts fetched so far, by id (null: the connection does not know it). */
|
|
399
|
+
private readonly layouts;
|
|
400
|
+
private readonly layoutsView;
|
|
401
|
+
/** `f` run with the writer layouts it needs: each one it stops at (`NeedLayout`) is fetched
|
|
402
|
+
* once (`GET /v1/layouts/{id}`, or the connection's descriptors), then `f` runs again. */
|
|
403
|
+
private decoded;
|
|
404
|
+
/** Read a node (`tree`: with its subtree, `asOf`: at a version). */
|
|
405
|
+
get<M>(p: CanTree<M>, o: GetOptions & {
|
|
406
|
+
tree: true;
|
|
407
|
+
}): Promise<M>;
|
|
408
|
+
get<M>(p: CanGet<M>, o?: GetOptions & {
|
|
409
|
+
tree?: false;
|
|
410
|
+
each?: undefined;
|
|
411
|
+
}): Promise<M>;
|
|
412
|
+
/** The model and the version it was read at. */
|
|
413
|
+
getVersioned<M>(p: CanTree<M>, o: GetOptions & {
|
|
414
|
+
tree: true;
|
|
415
|
+
}): Promise<{
|
|
416
|
+
model: M;
|
|
417
|
+
version: bigint;
|
|
418
|
+
}>;
|
|
419
|
+
getVersioned<M>(p: CanGet<M>, o?: GetOptions & {
|
|
420
|
+
tree?: false;
|
|
421
|
+
each?: undefined;
|
|
422
|
+
}): Promise<{
|
|
423
|
+
model: M;
|
|
424
|
+
version: bigint;
|
|
425
|
+
}>;
|
|
426
|
+
private readOne;
|
|
427
|
+
/** A page of a listing. */
|
|
428
|
+
list<M>(c: CanList<M>, o?: ListOptions): Promise<Page<M>>;
|
|
429
|
+
/** The records an index finds: a unique index's root or undefined, another's hits. */
|
|
430
|
+
lookup<R>(k: Unique<R>): Promise<R | undefined>;
|
|
431
|
+
lookup<R>(k: Many<R>): Promise<LookupHit<R>[]>;
|
|
432
|
+
/** Ranked text search over an aggregate. */
|
|
433
|
+
search<H>(s: Searchable<H>, text: string, o?: {
|
|
434
|
+
budget?: number;
|
|
435
|
+
limit?: number;
|
|
436
|
+
}): Promise<Scored<H>[]>;
|
|
437
|
+
insert<M, N>(c: CanInsert<M, N>, n: NoInfer<N>, o?: {
|
|
438
|
+
position?: Position;
|
|
439
|
+
}): Promise<Written>;
|
|
440
|
+
update<M, P extends PatchOf<object, object>>(p: CanUpdate<M, P>, patch: NoInfer<P>): Promise<Written>;
|
|
441
|
+
/** `ifExists`: a missing node deletes nothing instead of answering 404 (in a transaction). */
|
|
442
|
+
delete<M>(p: CanDelete<M>, o?: {
|
|
443
|
+
ifExists?: boolean;
|
|
444
|
+
}): Promise<Written>;
|
|
445
|
+
/**
|
|
446
|
+
* One batch of the change stream under the paths (`GET /v1/changes`, HTTP): the changes after
|
|
447
|
+
* `from` (`start`, `now` (the default) or a record's position) up to `limit`, and the position
|
|
448
|
+
* after them. The native protocol streams changes instead: `watchStream`.
|
|
449
|
+
*/
|
|
450
|
+
watch<C>(paths: CanWatch<C> | CanWatch<C>[], o?: {
|
|
451
|
+
from?: string;
|
|
452
|
+
limit?: number;
|
|
453
|
+
}): Promise<Changes<C>>;
|
|
454
|
+
/** The changes as they come, over the native protocol's change stream, until the iteration
|
|
455
|
+
* stops (the stream is then cancelled). Over HTTP, read batches with `watch`. */
|
|
456
|
+
watchStream<C>(paths: CanWatch<C> | CanWatch<C>[], o?: {
|
|
457
|
+
from?: string;
|
|
458
|
+
}): AsyncGenerator<Change<C>>;
|
|
459
|
+
/** A node of any type, by an untyped path. */
|
|
460
|
+
getUntyped(p: Path, o?: {
|
|
461
|
+
tree?: boolean;
|
|
462
|
+
asOf?: U64;
|
|
463
|
+
}): Promise<Node>;
|
|
464
|
+
/** A page of a listing, by an untyped path. */
|
|
465
|
+
listUntyped(p: Path, o?: ListOptions): Promise<Page<Node>>;
|
|
466
|
+
/**
|
|
467
|
+
* Operations as one transaction, all or none: `f` collects them (reads in it are sent at
|
|
468
|
+
* once), then they are sent as one `POST /v1/txn`. An answer 412 (a precondition of
|
|
469
|
+
* `unchangedSince` or `updateChecked` failed) runs `f` again, at most TXN_RETRIES times.
|
|
470
|
+
*/
|
|
471
|
+
txn(f: (t: Txn) => void | Promise<void>): Promise<Written>;
|
|
472
|
+
}
|
|
473
|
+
/** The operations of one transaction, collected by `Db.txn`'s function. */
|
|
474
|
+
export declare class Txn {
|
|
475
|
+
private readonly db;
|
|
476
|
+
readonly ops: Json[];
|
|
477
|
+
constructor(db: Db);
|
|
478
|
+
/** Read a node now; its version is what `unchangedSince` takes. */
|
|
479
|
+
get<M>(p: CanGet<M>): Promise<{
|
|
480
|
+
model: M;
|
|
481
|
+
version: bigint;
|
|
482
|
+
}>;
|
|
483
|
+
insert<M, N>(c: CanInsert<M, N>, n: NoInfer<N>, o?: {
|
|
484
|
+
position?: Position;
|
|
485
|
+
}): void;
|
|
486
|
+
update<M, P extends PatchOf<object, object>>(p: CanUpdate<M, P>, patch: NoInfer<P>): void;
|
|
487
|
+
delete<M>(p: CanDelete<M>, o?: {
|
|
488
|
+
ifExists?: boolean;
|
|
489
|
+
}): void;
|
|
490
|
+
/** The transaction applies only if the node is as it was at version `v` (412 otherwise). */
|
|
491
|
+
unchangedSince<M>(p: NodePath<M>, v: U64): void;
|
|
492
|
+
/**
|
|
493
|
+
* Read the node, give its model to `f` to change, and write back the fields `f` changed, on
|
|
494
|
+
* the condition that the node is unchanged since the read (a 412 runs the transaction again).
|
|
495
|
+
*/
|
|
496
|
+
updateChecked<M, P>(p: CanGet<M> & CanUpdate<M, P>, f: (m: M) => M | void): Promise<void>;
|
|
497
|
+
}
|
|
498
|
+
export {};
|