@restatedev/sessions 0.1.0 → 0.3.0
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/README.md +54 -8
- package/dist/database.d.ts +41 -2
- package/dist/database.d.ts.map +1 -1
- package/dist/database.js +99 -20
- package/dist/database.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -1
- package/dist/index.js.map +1 -1
- package/dist/internal.d.ts +6 -3
- package/dist/internal.d.ts.map +1 -1
- package/dist/internal.js +5 -2
- package/dist/internal.js.map +1 -1
- package/dist/link.d.ts +10 -0
- package/dist/link.d.ts.map +1 -0
- package/dist/link.js +19 -0
- package/dist/link.js.map +1 -0
- package/dist/object.d.ts +17 -4
- package/dist/object.d.ts.map +1 -1
- package/dist/object.js +61 -46
- package/dist/object.js.map +1 -1
- package/dist/schema.d.ts +7 -6
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +9 -10
- package/dist/schema.js.map +1 -1
- package/dist/scope.d.ts +38 -0
- package/dist/scope.d.ts.map +1 -0
- package/dist/scope.js +55 -0
- package/dist/scope.js.map +1 -0
- package/dist/serve.d.ts +21 -0
- package/dist/serve.d.ts.map +1 -0
- package/dist/serve.js +49 -0
- package/dist/serve.js.map +1 -0
- package/dist/session.d.ts +70 -34
- package/dist/session.d.ts.map +1 -1
- package/dist/session.js +273 -221
- package/dist/session.js.map +1 -1
- package/package.json +4 -4
- package/src/database.ts +111 -22
- package/src/index.ts +4 -1
- package/src/internal.ts +16 -3
- package/src/link.ts +22 -0
- package/src/object.ts +94 -63
- package/src/schema.ts +11 -10
- package/src/scope.ts +78 -0
- package/src/serve.ts +54 -0
- package/src/session.ts +273 -230
package/dist/session.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type * as restate from '@restatedev/restate-sdk';
|
|
2
2
|
import { type Serde } from '@restatedev/restate-sdk-core';
|
|
3
3
|
import { type Row, type Connection, type DatabaseWriter, type SqlParam, type DatabaseReader, type SqlValue, type Store } from './database.ts';
|
|
4
|
+
import { type Schema } from './schema.ts';
|
|
4
5
|
import { type Send } from './calls.ts';
|
|
5
6
|
export type { DatabaseReader, DatabaseWriter, Row, SqlParam, SqlValue };
|
|
6
7
|
/**
|
|
@@ -52,9 +53,12 @@ export interface SqlHost {
|
|
|
52
53
|
run(name: string, action: () => void): PromiseLike<void>;
|
|
53
54
|
genericSend(send: restate.GenericSend<Uint8Array>): unknown;
|
|
54
55
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
56
|
+
/** What a session object gives its attempts: its schema, and a wrapper of its store (fault injection, in tests). */
|
|
57
|
+
export interface ObjectConfig {
|
|
58
|
+
/** The object's own schema, which each of its databases is made by. */
|
|
59
|
+
readonly schema?: Schema;
|
|
60
|
+
wrapStore?: (store: Store) => Store;
|
|
61
|
+
}
|
|
58
62
|
/**
|
|
59
63
|
* `connection`, opened without the SDK (`open`), as a handler's `ctx.db`:
|
|
60
64
|
* for a library's tests, and tools. Its transactions send nothing, and a
|
|
@@ -91,14 +95,19 @@ export declare function database(db: Database, name: string): Database;
|
|
|
91
95
|
/**
|
|
92
96
|
* An attempt's SQL session: opened on its first use, or as an exclusive
|
|
93
97
|
* attempt starts ({@link openNow}), shared by everything the handler does,
|
|
94
|
-
* and saved when the handler ends ({@link end}). With `name`, of the key's
|
|
95
|
-
* ({@link database}), which has neither the object's schema
|
|
98
|
+
* and saved when the handler ends ({@link end}). With `name`, of the key's
|
|
99
|
+
* database `name` ({@link database}), which has neither the object's schema
|
|
100
|
+
* nor an outbox.
|
|
96
101
|
*/
|
|
97
102
|
export declare class Attempt {
|
|
98
103
|
/** `ctx.db`. */
|
|
99
104
|
readonly db: Database;
|
|
100
105
|
private readonly ctx;
|
|
106
|
+
/** Its object's: the key's other databases share its store's wrapper, and have none of its schema. */
|
|
107
|
+
private readonly config;
|
|
101
108
|
private readonly name;
|
|
109
|
+
/** Whether it is the object's own database of an exclusive handler: the one with an outbox. */
|
|
110
|
+
private readonly hasOutbox;
|
|
102
111
|
/** The key's other databases this attempt opened, by name. */
|
|
103
112
|
private readonly databases;
|
|
104
113
|
/** The session, from its first use on. */
|
|
@@ -108,42 +117,75 @@ export declare class Attempt {
|
|
|
108
117
|
/** The schemas this attempt's database has all the steps of. */
|
|
109
118
|
private readonly ensured;
|
|
110
119
|
private ended;
|
|
111
|
-
|
|
112
|
-
private unseenOpen;
|
|
113
|
-
/** Stops closing the session when the attempt ends. */
|
|
114
|
-
private keepOpen;
|
|
115
|
-
/** The invocation's cancellation, once a request of the session failed of it. */
|
|
116
|
-
private cancelled;
|
|
117
|
-
constructor(ctx: SqlHost, name?: string);
|
|
120
|
+
constructor(ctx: SqlHost, config?: ObjectConfig, name?: string);
|
|
118
121
|
/** The key's database `name`, in this attempt: see {@link database}. */
|
|
119
122
|
database(name: string): Database;
|
|
120
123
|
/**
|
|
121
|
-
*
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
+
* Open the session now, as the attempt starts, rather than on its first
|
|
125
|
+
* call: the open makes the sends a crashed attempt committed and left
|
|
126
|
+
* unmade, which must not wait for a handler that uses the database.
|
|
124
127
|
*/
|
|
125
|
-
|
|
128
|
+
openNow(): void;
|
|
126
129
|
/**
|
|
127
|
-
* The handler ended with `outcome`:
|
|
128
|
-
*
|
|
129
|
-
* a storage error ended closes now, rather than when it is collected.
|
|
130
|
+
* The handler ended with `outcome`: the key's other databases end, then
|
|
131
|
+
* this one ({@link endSession}).
|
|
130
132
|
*/
|
|
131
133
|
end<T>(outcome: Outcome<T>): Promise<T>;
|
|
132
134
|
/**
|
|
133
|
-
*
|
|
134
|
-
*
|
|
135
|
-
*
|
|
135
|
+
* `body` as one transaction. In the object's own database of an exclusive
|
|
136
|
+
* handler, its sends commit with it, in the outbox, and are made with its
|
|
137
|
+
* commit, right after its writes and before it waits for anything
|
|
138
|
+
* (`withCommit`): Restate stores an attempt's commands in order, so they
|
|
139
|
+
* are stored only if the commit is, and whatever fails the commit's wait (a
|
|
140
|
+
* cancellation, say) comes after them. A second commit empties the outbox
|
|
141
|
+
* before `transaction` resolves: left until the handler's end, they would
|
|
142
|
+
* be made again by an attempt that resumes a long wait (an approval), past
|
|
143
|
+
* the idempotency retention that drops a repeat.
|
|
136
144
|
*/
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
|
|
145
|
+
private transaction;
|
|
146
|
+
/**
|
|
147
|
+
* Commit with `handle` the outbox as committed, but the sends stored since,
|
|
148
|
+
* and with `outgoing`, which the commit makes. Unchanged, it is not
|
|
149
|
+
* written again.
|
|
150
|
+
*/
|
|
151
|
+
private commitOutbox;
|
|
140
152
|
/**
|
|
141
153
|
* `call` on the session, opening it on the first; its failure as the
|
|
142
|
-
* handler fails with it
|
|
143
|
-
*
|
|
144
|
-
*
|
|
154
|
+
* handler fails with it. The call that fails on a session that a failure
|
|
155
|
+
* ended is its last: the next opens another, which reads what is stored.
|
|
156
|
+
* So the failure of an open made as the attempt started goes to the first
|
|
157
|
+
* call, as it would had that call opened it.
|
|
145
158
|
*/
|
|
146
159
|
private call;
|
|
160
|
+
/** The session, opening one if there is none, or the one there is was retired. */
|
|
161
|
+
private current;
|
|
162
|
+
private open;
|
|
163
|
+
private start;
|
|
164
|
+
/**
|
|
165
|
+
* End the session, then settle as the handler did: an SQL error that
|
|
166
|
+
* escaped it becomes terminal. The handler's error stands: what it
|
|
167
|
+
* committed is stored, and an outbox left full is made by the next open.
|
|
168
|
+
* A failure to end the session fails a handler that succeeded, which a
|
|
169
|
+
* retry then ends (a transaction it left open, say).
|
|
170
|
+
*/
|
|
171
|
+
private endSession;
|
|
172
|
+
/**
|
|
173
|
+
* Close the session as the attempt ends. A view's closes: it read the
|
|
174
|
+
* snapshot it opened on, and one that outlived 16 commits failed its read
|
|
175
|
+
* ("is gone"), which is retried. Another is saved, its outbox emptied. One
|
|
176
|
+
* that a failure ended closes, and the object's own database is opened
|
|
177
|
+
* again and saved: it may hold sends unmade (a crash left them, and this
|
|
178
|
+
* attempt's open failed) or made and not emptied (the commit's wait
|
|
179
|
+
* failed), and no attempt may follow this one (a cancelled invocation's),
|
|
180
|
+
* nor the key's next invocation come within the idempotency retention.
|
|
181
|
+
*/
|
|
182
|
+
private close;
|
|
183
|
+
/** Empty the outbox and save the session, which closes if that fails. */
|
|
184
|
+
private save;
|
|
185
|
+
/** The object's schema, which the key's other databases have none of. */
|
|
186
|
+
private ownSchema;
|
|
187
|
+
/** Empty the database in the transaction `handle` ({@link reset}), and apply the schemas this attempt applied again. */
|
|
188
|
+
private reset;
|
|
147
189
|
/** Apply the steps of `schemas` that the database lacks, each schema once per attempt. */
|
|
148
190
|
private ensureSchemas;
|
|
149
191
|
/** Make the sends of a committed transaction: each with an idempotency key of the transaction (or its own), so that making them again is dropped. */
|
|
@@ -155,12 +197,6 @@ export declare class Attempt {
|
|
|
155
197
|
* commit after them, which stores them first.
|
|
156
198
|
*/
|
|
157
199
|
private flush;
|
|
158
|
-
/** The session, opening it on the first call. */
|
|
159
|
-
private opened;
|
|
160
|
-
private start;
|
|
161
|
-
private endSession;
|
|
162
|
-
/** Empty the outbox and save the session; one a storage error ended only closes. */
|
|
163
|
-
private save;
|
|
164
200
|
}
|
|
165
201
|
/** Run `handler`, then end `attempt`'s SQL session, if it opened one (see {@link Attempt.end}). */
|
|
166
202
|
export declare function withAttempt<T>(handler: () => Promise<T>, attempt: Attempt): Promise<T>;
|
package/dist/session.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../src/session.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,KAAK,OAAO,MAAM,yBAAyB,CAAC;AAExD,OAAO,EAA4B,KAAK,KAAK,EAAE,MAAM,8BAA8B,CAAC;AACpF,OAAO,EAEL,KAAK,GAAG,EAGR,KAAK,UAAU,EACf,KAAK,cAAc,EACnB,KAAK,QAAQ,EACb,KAAK,cAAc,EACnB,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