@restatedev/sessions 0.0.0 → 0.2.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/LICENSE +21 -0
- package/README.md +480 -2
- package/dist/calls.d.ts +72 -0
- package/dist/calls.d.ts.map +1 -0
- package/dist/calls.js +86 -0
- package/dist/calls.js.map +1 -0
- package/dist/database.d.ts +169 -0
- package/dist/database.d.ts.map +1 -0
- package/dist/database.js +264 -0
- package/dist/database.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +28 -0
- package/dist/index.js.map +1 -0
- package/dist/internal.d.ts +10 -0
- package/dist/internal.d.ts.map +1 -0
- package/dist/internal.js +13 -0
- package/dist/internal.js.map +1 -0
- package/dist/object.d.ts +162 -0
- package/dist/object.d.ts.map +1 -0
- package/dist/object.js +235 -0
- package/dist/object.js.map +1 -0
- package/dist/platform.d.ts +5 -0
- package/dist/platform.d.ts.map +1 -0
- package/dist/platform.js +15 -0
- package/dist/platform.js.map +1 -0
- package/dist/schema.d.ts +35 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +118 -0
- package/dist/schema.js.map +1 -0
- package/dist/selector.d.ts +86 -0
- package/dist/selector.d.ts.map +1 -0
- package/dist/selector.js +196 -0
- package/dist/selector.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 +167 -0
- package/dist/session.d.ts.map +1 -0
- package/dist/session.js +547 -0
- package/dist/session.js.map +1 -0
- package/package.json +40 -5
- package/src/calls.ts +197 -0
- package/src/database.ts +392 -0
- package/src/index.ts +38 -0
- package/src/internal.ts +14 -0
- package/src/object.ts +368 -0
- package/src/platform.ts +15 -0
- package/src/schema.ts +162 -0
- package/src/selector.ts +253 -0
- package/src/serve.ts +54 -0
- package/src/session.ts +663 -0
|
@@ -0,0 +1 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AC;QACL,CAAC,CAAC,CAAC;IACL,CAAC,CAAS,CAAC;AACb,CAAC"}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
2
|
+
/** An SQL value. Integers come back as numbers, or as bigints beyond 2^53. */
|
|
3
|
+
export type SqlValue = null | number | bigint | string | Uint8Array;
|
|
4
|
+
/** An SQL parameter: an `SqlValue`, or a boolean, stored as 0 or 1. */
|
|
5
|
+
export type SqlParam = SqlValue | boolean;
|
|
6
|
+
/** The rows of a statement, with its column names. */
|
|
7
|
+
export interface SqlQueryResult {
|
|
8
|
+
columns: string[];
|
|
9
|
+
rows: SqlValue[][];
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* A failure a retry would repeat: SQL the engine rejected (a syntax error, a
|
|
13
|
+
* constraint, a missing table), which undid just that statement and leaves
|
|
14
|
+
* the session usable; a parameter it cannot bind; or a call the session
|
|
15
|
+
* refused, which may have ended it (a `COMMIT` in a transaction's body, a
|
|
16
|
+
* transaction left open). A storage error, which ends the session, rejects
|
|
17
|
+
* as it is.
|
|
18
|
+
*/
|
|
19
|
+
export declare class SqlError extends Error {
|
|
20
|
+
name: string;
|
|
21
|
+
}
|
|
22
|
+
/** A row as an object, by column name. */
|
|
23
|
+
export type Row = Record<string, SqlValue>;
|
|
24
|
+
/** Reads of a database, or of one transaction of it. */
|
|
25
|
+
export interface DatabaseReader {
|
|
26
|
+
/**
|
|
27
|
+
* The rows of one statement, each an object by column name; parameters
|
|
28
|
+
* bind to a single statement. `T` declares the row's shape, unchecked:
|
|
29
|
+
* integers past 2^53 come back as bigints, and booleans as 0 or 1. Columns
|
|
30
|
+
* are named as SQL names them (an expression needs `AS`), and two of the
|
|
31
|
+
* same name are an {@link SqlError}: alias them, or use {@link values}.
|
|
32
|
+
*/
|
|
33
|
+
query<T = Row>(sql: string, params?: SqlParam[]): Promise<T[]>;
|
|
34
|
+
/** The first row of {@link query}, or `undefined` when there is none. */
|
|
35
|
+
get<T = Row>(sql: string, params?: SqlParam[]): Promise<T | undefined>;
|
|
36
|
+
/** The rows of one statement as arrays, in column order: for adapters, and for columns that share a name. */
|
|
37
|
+
values(sql: string, params?: SqlParam[]): Promise<SqlValue[][]>;
|
|
38
|
+
}
|
|
39
|
+
/** SQL: a database, or one transaction of it. */
|
|
40
|
+
export interface DatabaseWriter extends DatabaseReader {
|
|
41
|
+
/** Run SQL (one or more statements), discarding rows; parameters only with a single statement. */
|
|
42
|
+
execute(sql: string, params?: SqlParam[]): Promise<void>;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* {@link DatabaseWriter} over `run`, which runs one call's SQL and returns
|
|
46
|
+
* its rows with their columns, failing as `failure` makes its errors (as
|
|
47
|
+
* `run` makes the engine's).
|
|
48
|
+
*/
|
|
49
|
+
export declare function sqlOver(run: (sql: string, params?: SqlParam[]) => Promise<SqlQueryResult>, failure?: (error: unknown) => unknown): DatabaseWriter;
|
|
50
|
+
/**
|
|
51
|
+
* A key/value store, the object's state: `set` and `clear` return at once,
|
|
52
|
+
* and `sync` resolves once every write before it is stored. Writes are
|
|
53
|
+
* stored in the order they were made: a crash keeps a prefix of the writes
|
|
54
|
+
* since the last sync and loses the rest. `get` returns the stored value, or
|
|
55
|
+
* this request's own latest write; `undefined` or `null` when absent.
|
|
56
|
+
*/
|
|
57
|
+
export interface Store {
|
|
58
|
+
get(key: string): Promise<Uint8Array | null | undefined>;
|
|
59
|
+
set(key: string, value: Uint8Array): void;
|
|
60
|
+
clear(key: string): void;
|
|
61
|
+
sync(): Promise<void>;
|
|
62
|
+
/**
|
|
63
|
+
* Every key, as `ctx.stateKeys()`: then a commit that checkpoints deletes
|
|
64
|
+
* the packs no checkpoint needs any more. A store without it keeps them.
|
|
65
|
+
*/
|
|
66
|
+
keys?(): Promise<string[]>;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* A session of the addon: one call at a time holds it. A transaction's body
|
|
70
|
+
* runs between `begin` and `commit` (or `rollback`). A call answers at once
|
|
71
|
+
* with its value (or throws) when it needs nothing from the store, such as
|
|
72
|
+
* SQL on cached pages, and with a promise when it waits for the store.
|
|
73
|
+
*/
|
|
74
|
+
interface NativeSession {
|
|
75
|
+
queryColumns(sql: string, params?: SqlParam[]): Answer<SqlQueryResult>;
|
|
76
|
+
begin(): Answer<void>;
|
|
77
|
+
commit(records: Record<string, Buffer>): Answer<void>;
|
|
78
|
+
rollback(): Answer<void>;
|
|
79
|
+
record(name: string): Answer<Buffer>;
|
|
80
|
+
isUsable(): boolean;
|
|
81
|
+
save(): Answer<void>;
|
|
82
|
+
close(): Promise<void>;
|
|
83
|
+
}
|
|
84
|
+
/** A value, or a promise of it: `await` takes either. */
|
|
85
|
+
type Answer<T> = T | Promise<T>;
|
|
86
|
+
/**
|
|
87
|
+
* Open the database of `namespace` over `store`. One process-wide page cache
|
|
88
|
+
* serves every namespace. A `reader` reads beside the namespace's owner (a
|
|
89
|
+
* shared handler), the state as of open, and writes nothing.
|
|
90
|
+
*/
|
|
91
|
+
export declare function open(namespace: string, store: Store, { reader }?: {
|
|
92
|
+
reader?: boolean | undefined;
|
|
93
|
+
}): Promise<Connection>;
|
|
94
|
+
/**
|
|
95
|
+
* SQL on a session of the addon: {@link DatabaseWriter}'s `query` and `execute`
|
|
96
|
+
* by way of its `queryColumns`.
|
|
97
|
+
*/
|
|
98
|
+
declare abstract class Sql implements DatabaseWriter {
|
|
99
|
+
protected readonly native: NativeSession;
|
|
100
|
+
constructor(native: NativeSession);
|
|
101
|
+
abstract queryColumns(sql: string, params?: SqlParam[]): Promise<SqlQueryResult>;
|
|
102
|
+
query<T = Row>(sql: string, params?: SqlParam[]): Promise<T[]>;
|
|
103
|
+
get<T = Row>(sql: string, params?: SqlParam[]): Promise<T | undefined>;
|
|
104
|
+
values(sql: string, params?: SqlParam[]): Promise<SqlValue[][]>;
|
|
105
|
+
execute(sql: string, params?: SqlParam[]): Promise<void>;
|
|
106
|
+
/**
|
|
107
|
+
* `call`'s result, or its {@link failure}: `call` makes the request, as the
|
|
108
|
+
* addon throws as it is called (arguments it cannot convert, and the error
|
|
109
|
+
* of a call that answers at once).
|
|
110
|
+
*/
|
|
111
|
+
protected request<T>(call: () => Answer<T>): Promise<T>;
|
|
112
|
+
/** A request's `error`: an {@link SqlError} if a retry would repeat it. */
|
|
113
|
+
protected failure(error: unknown): unknown;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* A session of a database: calls run one at a time, in call order, and a
|
|
117
|
+
* transaction holds the session until it commits or rolls back. Calls made
|
|
118
|
+
* meanwhile wait, but a call from inside the transaction's body is refused,
|
|
119
|
+
* as it would wait for that body: the body uses its `tx`.
|
|
120
|
+
*/
|
|
121
|
+
export declare class Connection extends Sql {
|
|
122
|
+
/** Settles once the last call queued so far has: the next call runs after it. */
|
|
123
|
+
private queue;
|
|
124
|
+
queryColumns(sql: string, params?: SqlParam[]): Promise<SqlQueryResult>;
|
|
125
|
+
/**
|
|
126
|
+
* Run `body` as one SQL transaction: it commits when `body` resolves, with
|
|
127
|
+
* the records it set, and rolls back when it rejects, with the body's own
|
|
128
|
+
* error, once the calls it made have settled. A statement in it that ends
|
|
129
|
+
* the transaction (`COMMIT`, `ROLLBACK`) ends the session. A failing
|
|
130
|
+
* COMMIT rolls back.
|
|
131
|
+
*/
|
|
132
|
+
transaction<T>(body: (tx: ConnectionTransaction) => Promise<T>): Promise<T>;
|
|
133
|
+
/**
|
|
134
|
+
* The record `name`, as the last commit that set it left it (see
|
|
135
|
+
* {@link ConnectionTransaction.setRecord}); empty if none did.
|
|
136
|
+
*/
|
|
137
|
+
record(name: string): Promise<Uint8Array>;
|
|
138
|
+
/** Whether SQL can still run: an SQL error leaves the session usable, a storage error ends it. */
|
|
139
|
+
isUsable(): boolean;
|
|
140
|
+
/**
|
|
141
|
+
* End the session after the calls before it, leaving its engine to the
|
|
142
|
+
* next one. It rejects if they left a transaction open, or a failure ended
|
|
143
|
+
* the session.
|
|
144
|
+
*/
|
|
145
|
+
save(): Promise<void>;
|
|
146
|
+
/** End the session now, whatever its state: queued calls reject. */
|
|
147
|
+
close(): Promise<void>;
|
|
148
|
+
/** Run `call` once the calls before it have, unless it comes from the body of a transaction it would wait for. */
|
|
149
|
+
private serial;
|
|
150
|
+
/** Roll back, unless a storage error ended the session: the caller rethrows its own error, which says why. */
|
|
151
|
+
private rollback;
|
|
152
|
+
}
|
|
153
|
+
/** SQL in one transaction, while its body runs, and the records it commits with. */
|
|
154
|
+
export interface ConnectionTransaction extends DatabaseWriter {
|
|
155
|
+
/** The rows of one statement, with their column names. */
|
|
156
|
+
queryColumns(sql: string, params?: SqlParam[]): Promise<SqlQueryResult>;
|
|
157
|
+
/**
|
|
158
|
+
* Set the record `name` to `value` with this transaction, in the same
|
|
159
|
+
* write: a whole value kept beside the SQL, in no table, stored exactly
|
|
160
|
+
* when the transaction is, even if it changes no row. It stays until a
|
|
161
|
+
* later transaction sets it again; an empty value reads as never set. A
|
|
162
|
+
* name is at most 255 bytes, and not `db`; a value at most 16 MiB.
|
|
163
|
+
*/
|
|
164
|
+
setRecord(name: string, value: Uint8Array): void;
|
|
165
|
+
/** The record `name` as this transaction would commit it: as it set it, or as committed. */
|
|
166
|
+
record(name: string): Promise<Uint8Array>;
|
|
167
|
+
}
|
|
168
|
+
export {};
|
|
169
|
+
//# sourceMappingURL=database.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"database.d.ts","sourceRoot":"","sources":["../src/database.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAIrC,8EAA8E;AAC9E,MAAM,MAAM,QAAQ,GAAG,IAAI,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,UAAU,CAAC;AAEpE,uEAAuE;AACvE,MAAM,MAAM,QAAQ,GAAG,QAAQ,GAAG,OAAO,CAAC;AAE1C,sDAAsD;AACtD,MAAM,WAAW,cAAc;IAC7B,OAAO,EAAE,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MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAExC;IAED,kGAAkG;IAClG,QAAQ,IAAI,OAAO,CAElB;IAED;;;;OAIG;IACH,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,CAEpB;IAED,oEAAoE;IACpE,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAErB;IAED,kHAAkH;IAClH,OAAO,CAAC,MAAM;IASd,8GAA8G;YAChG,QAAQ;CAKvB;AAED,oFAAoF;AACpF,MAAM,WAAW,qBAAsB,SAAQ,cAAc;IAC3D,0DAA0D;IAC1D,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC,cAAc,CAAC,CAAC;IACxE;;;;;;OAMG;IACH,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,UAAU,GAAG,IAAI,CAAC;IACjD,4FAA4F;IAC5F,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;CAC3C"}
|
package/dist/database.js
ADDED
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
// A session's database, through the addon (`native/`): SQLite-compatible
|
|
2
|
+
// SQL, kept in the object's state. The addon reads and writes the state through a
|
|
3
|
+
// store (`objectStore` in session.ts), in the order it makes its requests
|
|
4
|
+
// (`native/src/node.rs`); this module orders the calls and runs transactions.
|
|
5
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
6
|
+
import { Buffer } from 'node:buffer';
|
|
7
|
+
import { createRequire } from 'node:module';
|
|
8
|
+
import { PLATFORMS, platform } from './platform.js';
|
|
9
|
+
/**
|
|
10
|
+
* A failure a retry would repeat: SQL the engine rejected (a syntax error, a
|
|
11
|
+
* constraint, a missing table), which undid just that statement and leaves
|
|
12
|
+
* the session usable; a parameter it cannot bind; or a call the session
|
|
13
|
+
* refused, which may have ended it (a `COMMIT` in a transaction's body, a
|
|
14
|
+
* transaction left open). A storage error, which ends the session, rejects
|
|
15
|
+
* as it is.
|
|
16
|
+
*/
|
|
17
|
+
export class SqlError extends Error {
|
|
18
|
+
name = 'SqlError';
|
|
19
|
+
}
|
|
20
|
+
/** The rows of `result` as objects by column name; a column name twice is an {@link SqlError}. */
|
|
21
|
+
function objects({ columns, rows }) {
|
|
22
|
+
if (new Set(columns).size !== columns.length) {
|
|
23
|
+
const twice = columns.find((name, i) => columns.indexOf(name) !== i);
|
|
24
|
+
throw new SqlError(`the column ${twice} is named twice: alias one (AS), or read the rows with values()`);
|
|
25
|
+
}
|
|
26
|
+
return rows.map((row) => Object.fromEntries(columns.map((name, i) => [name, row[i]])));
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* {@link DatabaseWriter} over `run`, which runs one call's SQL and returns
|
|
30
|
+
* its rows with their columns, failing as `failure` makes its errors (as
|
|
31
|
+
* `run` makes the engine's).
|
|
32
|
+
*/
|
|
33
|
+
export function sqlOver(run, failure = (error) => error) {
|
|
34
|
+
const rowsOf = async (sql, params) => {
|
|
35
|
+
const result = await run(sql, params);
|
|
36
|
+
try {
|
|
37
|
+
return objects(result);
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
throw failure(error);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
return {
|
|
44
|
+
query: async (sql, params) => (await rowsOf(sql, params)),
|
|
45
|
+
get: async (sql, params) => (await rowsOf(sql, params))[0],
|
|
46
|
+
values: async (sql, params) => (await run(sql, params)).rows,
|
|
47
|
+
execute: async (sql, params) => void (await run(sql, params)),
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
const addon = load();
|
|
51
|
+
/** The addon of this machine's platform, from its package (see platform.ts). */
|
|
52
|
+
function load() {
|
|
53
|
+
const machine = platform();
|
|
54
|
+
const name = `@restatedev/sessions-${machine}`;
|
|
55
|
+
try {
|
|
56
|
+
return createRequire(import.meta.url)(name);
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
if (error.code !== 'MODULE_NOT_FOUND')
|
|
60
|
+
throw error;
|
|
61
|
+
throw new Error(`@restatedev/sessions found no addon for ${machine}, the package ${name}: it ships them for ` +
|
|
62
|
+
`${PLATFORMS.join(', ')}. In its repository, \`pnpm build\` builds this machine's.`, { cause: error });
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Open the database of `namespace` over `store`. One process-wide page cache
|
|
67
|
+
* serves every namespace. A `reader` reads beside the namespace's owner (a
|
|
68
|
+
* shared handler), the state as of open, and writes nothing.
|
|
69
|
+
*/
|
|
70
|
+
export async function open(namespace, store, { reader = false } = {}) {
|
|
71
|
+
return new Connection(await addon.open(namespace, store, reader));
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* SQL on a session of the addon: {@link DatabaseWriter}'s `query` and `execute`
|
|
75
|
+
* by way of its `queryColumns`.
|
|
76
|
+
*/
|
|
77
|
+
class Sql {
|
|
78
|
+
native;
|
|
79
|
+
constructor(native) {
|
|
80
|
+
this.native = native;
|
|
81
|
+
}
|
|
82
|
+
query(sql, params) {
|
|
83
|
+
return sqlOver((s, p) => this.queryColumns(s, p)).query(sql, params);
|
|
84
|
+
}
|
|
85
|
+
get(sql, params) {
|
|
86
|
+
return sqlOver((s, p) => this.queryColumns(s, p)).get(sql, params);
|
|
87
|
+
}
|
|
88
|
+
values(sql, params) {
|
|
89
|
+
return sqlOver((s, p) => this.queryColumns(s, p)).values(sql, params);
|
|
90
|
+
}
|
|
91
|
+
async execute(sql, params) {
|
|
92
|
+
await this.queryColumns(sql, params);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* `call`'s result, or its {@link failure}: `call` makes the request, as the
|
|
96
|
+
* addon throws as it is called (arguments it cannot convert, and the error
|
|
97
|
+
* of a call that answers at once).
|
|
98
|
+
*/
|
|
99
|
+
async request(call) {
|
|
100
|
+
try {
|
|
101
|
+
return await call();
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
throw this.failure(error);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/** A request's `error`: an {@link SqlError} if a retry would repeat it. */
|
|
108
|
+
failure(error) {
|
|
109
|
+
if (error instanceof SqlError)
|
|
110
|
+
return error;
|
|
111
|
+
const repeats = error.code === REFUSED || this.native.isUsable();
|
|
112
|
+
return repeats ? new SqlError(error.message, { cause: error }) : error;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
// The code of the addon's errors that a retry would repeat even though they
|
|
116
|
+
// ended the session (its `Refused`), and of arguments it cannot convert.
|
|
117
|
+
const REFUSED = 'InvalidArg';
|
|
118
|
+
// The transaction whose body is running, in that body's asynchronous context:
|
|
119
|
+
// the innermost, which knows the ones it runs inside.
|
|
120
|
+
const running = new AsyncLocalStorage();
|
|
121
|
+
/**
|
|
122
|
+
* A session of a database: calls run one at a time, in call order, and a
|
|
123
|
+
* transaction holds the session until it commits or rolls back. Calls made
|
|
124
|
+
* meanwhile wait, but a call from inside the transaction's body is refused,
|
|
125
|
+
* as it would wait for that body: the body uses its `tx`.
|
|
126
|
+
*/
|
|
127
|
+
export class Connection extends Sql {
|
|
128
|
+
/** Settles once the last call queued so far has: the next call runs after it. */
|
|
129
|
+
queue = Promise.resolve();
|
|
130
|
+
queryColumns(sql, params) {
|
|
131
|
+
return this.serial(() => this.request(() => this.native.queryColumns(sql, params)));
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Run `body` as one SQL transaction: it commits when `body` resolves, with
|
|
135
|
+
* the records it set, and rolls back when it rejects, with the body's own
|
|
136
|
+
* error, once the calls it made have settled. A statement in it that ends
|
|
137
|
+
* the transaction (`COMMIT`, `ROLLBACK`) ends the session. A failing
|
|
138
|
+
* COMMIT rolls back.
|
|
139
|
+
*/
|
|
140
|
+
transaction(body) {
|
|
141
|
+
return this.serial(async () => {
|
|
142
|
+
await this.request(() => this.native.begin());
|
|
143
|
+
const tx = new OpenTransaction(this, this.native, running.getStore());
|
|
144
|
+
let result;
|
|
145
|
+
try {
|
|
146
|
+
result = await running.run(tx, () => body(tx));
|
|
147
|
+
}
|
|
148
|
+
catch (error) {
|
|
149
|
+
await tx.end();
|
|
150
|
+
await this.rollback();
|
|
151
|
+
throw error;
|
|
152
|
+
}
|
|
153
|
+
const records = await tx.end();
|
|
154
|
+
try {
|
|
155
|
+
await this.native.commit(records);
|
|
156
|
+
}
|
|
157
|
+
catch (error) {
|
|
158
|
+
// A COMMIT that fails (a deferred constraint) leaves the transaction
|
|
159
|
+
// open: without the rollback, the calls after it would run inside it.
|
|
160
|
+
await this.rollback();
|
|
161
|
+
throw this.failure(error);
|
|
162
|
+
}
|
|
163
|
+
return result;
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* The record `name`, as the last commit that set it left it (see
|
|
168
|
+
* {@link ConnectionTransaction.setRecord}); empty if none did.
|
|
169
|
+
*/
|
|
170
|
+
record(name) {
|
|
171
|
+
return this.serial(async () => new Uint8Array(await this.request(() => this.native.record(name))));
|
|
172
|
+
}
|
|
173
|
+
/** Whether SQL can still run: an SQL error leaves the session usable, a storage error ends it. */
|
|
174
|
+
isUsable() {
|
|
175
|
+
return this.native.isUsable();
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* End the session after the calls before it, leaving its engine to the
|
|
179
|
+
* next one. It rejects if they left a transaction open, or a failure ended
|
|
180
|
+
* the session.
|
|
181
|
+
*/
|
|
182
|
+
save() {
|
|
183
|
+
return this.serial(() => this.request(() => this.native.save()));
|
|
184
|
+
}
|
|
185
|
+
/** End the session now, whatever its state: queued calls reject. */
|
|
186
|
+
close() {
|
|
187
|
+
return this.native.close();
|
|
188
|
+
}
|
|
189
|
+
/** Run `call` once the calls before it have, unless it comes from the body of a transaction it would wait for. */
|
|
190
|
+
serial(call) {
|
|
191
|
+
if (running.getStore()?.holds(this)) {
|
|
192
|
+
return Promise.reject(new SqlError('inside a transaction, use the transaction handle its body gets'));
|
|
193
|
+
}
|
|
194
|
+
const result = this.queue.then(call);
|
|
195
|
+
this.queue = result.catch(() => { });
|
|
196
|
+
return result;
|
|
197
|
+
}
|
|
198
|
+
/** Roll back, unless a storage error ended the session: the caller rethrows its own error, which says why. */
|
|
199
|
+
async rollback() {
|
|
200
|
+
try {
|
|
201
|
+
await this.native.rollback();
|
|
202
|
+
}
|
|
203
|
+
catch { }
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* A {@link ConnectionTransaction} of {@link Connection.transaction}, open until its body
|
|
208
|
+
* settles. Its calls run one at a time, in call order, also when the body
|
|
209
|
+
* awaits several together, or none: each call made while it is open runs
|
|
210
|
+
* before its commit or rollback, so none outlives it.
|
|
211
|
+
*/
|
|
212
|
+
class OpenTransaction extends Sql {
|
|
213
|
+
database;
|
|
214
|
+
/** The transaction whose body this one's runs in, if any: of another database, since a database's own are refused. */
|
|
215
|
+
outer;
|
|
216
|
+
/** The records it sets, by name. */
|
|
217
|
+
records = new Map();
|
|
218
|
+
/** Settles once the last call queued so far has: the next call runs after it. */
|
|
219
|
+
queue = Promise.resolve();
|
|
220
|
+
ended = false;
|
|
221
|
+
constructor(database, native, outer) {
|
|
222
|
+
super(native);
|
|
223
|
+
this.database = database;
|
|
224
|
+
this.outer = outer;
|
|
225
|
+
}
|
|
226
|
+
/** Whether this transaction, or one it runs inside, is open and holds `database`. */
|
|
227
|
+
holds(database) {
|
|
228
|
+
return (!this.ended && this.database === database) || (this.outer?.holds(database) ?? false);
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* End it, refusing calls from now on, once the calls made before have
|
|
232
|
+
* settled; then the records it set, as the addon's commit takes them.
|
|
233
|
+
*/
|
|
234
|
+
async end() {
|
|
235
|
+
this.ended = true;
|
|
236
|
+
await this.queue;
|
|
237
|
+
return Object.fromEntries(this.records);
|
|
238
|
+
}
|
|
239
|
+
queryColumns(sql, params) {
|
|
240
|
+
return this.serial(() => this.request(() => this.native.queryColumns(sql, params)));
|
|
241
|
+
}
|
|
242
|
+
setRecord(name, value) {
|
|
243
|
+
this.refuseEnded();
|
|
244
|
+
this.records.set(name, Buffer.from(value));
|
|
245
|
+
}
|
|
246
|
+
record(name) {
|
|
247
|
+
// As set when it is called, in call order with `setRecord`.
|
|
248
|
+
const set = this.records.get(name);
|
|
249
|
+
return this.serial(async () => new Uint8Array(set ?? (await this.request(() => this.native.record(name)))));
|
|
250
|
+
}
|
|
251
|
+
/** Run `call`, made while the transaction is open, once the calls before it have. */
|
|
252
|
+
serial(call) {
|
|
253
|
+
if (this.ended)
|
|
254
|
+
return Promise.reject(new SqlError('the transaction has ended'));
|
|
255
|
+
const result = this.queue.then(call);
|
|
256
|
+
this.queue = result.catch(() => { });
|
|
257
|
+
return result;
|
|
258
|
+
}
|
|
259
|
+
refuseEnded() {
|
|
260
|
+
if (this.ended)
|
|
261
|
+
throw new SqlError('the transaction has ended');
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
//# sourceMappingURL=database.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"database.js","sourceRoot":"","sources":["../src/database.ts"],"names":[],"mappings":"AAAA,yEAAyE;AACzE,kFAAkF;AAClF,0EAA0E;AAC1E,8EAA8E;AAC9E,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AACrD,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAcpD;;;;;;;GAOG;AACH,MAAM,OAAO,QAAS,SAAQ,KAAK;IACxB,IAAI,GAAG,UAAU,CAAC;CAC5B;AA2BD,kGAAkG;AAClG,SAAS,OAAO,CAAC,EAAE,OAAO,EAAE,IAAI,EAAkB;IAChD,IAAI,IAAI,GAAG,CAAC,OAAO,CAAC,CAAC,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 -0,0 +1,7 @@
|
|
|
1
|
+
export * from '@restatedev/restate-sdk';
|
|
2
|
+
export type { Database, DatabaseReader, DatabaseWriter, Row, SqlParam, SqlValue, Transaction } from './session.ts';
|
|
3
|
+
export type { Step } from './schema.ts';
|
|
4
|
+
export { sessionObject, type Cancel, type Entry, type HandlerOptions, type SessionContext, type ViewContext, } from './object.ts';
|
|
5
|
+
export type { CallOptions, Client, Rpc, Send, SendClient, SendOptions, Target } from './calls.ts';
|
|
6
|
+
export { serve } from './serve.ts';
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAwBA,cAAc,yBAAyB,CAAC;AACxC,YAAY,EAAE,QAAQ,EAAE,cAAc,EAAE,cAAc,EAAE,GAAG,EAAE,QAAQ,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AACnH,YAAY,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EACL,aAAa,EACb,KAAK,MAAM,EACX,KAAK,KAAK,EACV,KAAK,cAAc,EACnB,KAAK,cAAc,EACnB,KAAK,WAAW,GACjB,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,WAAW,EAAE,MAAM,EAAE,GAAG,EAAE,IAAI,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAElG,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// Session objects: Restate virtual objects whose state is a SQL database per
|
|
2
|
+
// key. It is the Restate SDK, with `restate.sessionObject` beside its own
|
|
3
|
+
// objects, services and workflows:
|
|
4
|
+
//
|
|
5
|
+
// import * as restate from '@restatedev/sessions';
|
|
6
|
+
//
|
|
7
|
+
// const notes = restate.sessionObject({
|
|
8
|
+
// name: 'notes',
|
|
9
|
+
// schema: ['CREATE TABLE notes (text TEXT NOT NULL)'],
|
|
10
|
+
// handlers: {
|
|
11
|
+
// add: async (ctx, text: string) => {
|
|
12
|
+
// await ctx.db.execute('INSERT INTO notes (text) VALUES (?)', [text]);
|
|
13
|
+
// },
|
|
14
|
+
// },
|
|
15
|
+
// views: {
|
|
16
|
+
// list: async (ctx) => ctx.db.query<{ text: string }>('SELECT text FROM notes'),
|
|
17
|
+
// },
|
|
18
|
+
// });
|
|
19
|
+
//
|
|
20
|
+
// See README.md.
|
|
21
|
+
// The SDK, and what this module adds to it (or declares in its place: its
|
|
22
|
+
// `Client`, `SendClient` and `Target` are a session object's, and `serve`
|
|
23
|
+
// takes the services, with what they need beside them).
|
|
24
|
+
export * from '@restatedev/restate-sdk';
|
|
25
|
+
export { sessionObject, } from './object.js';
|
|
26
|
+
// In place of the SDK's: the services, on PORT, with what they need beside them.
|
|
27
|
+
export { serve } from './serve.js';
|
|
28
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,0EAA0E;AAC1E,mCAAmC;AACnC,EAAE;AACF,qDAAqD;AACrD,EAAE;AACF,0CAA0C;AAC1C,qBAAqB;AACrB,2DAA2D;AAC3D,kBAAkB;AAClB,4CAA4C;AAC5C,+EAA+E;AAC/E,WAAW;AACX,SAAS;AACT,eAAe;AACf,uFAAuF;AACvF,SAAS;AACT,QAAQ;AACR,EAAE;AACF,iBAAiB;AAEjB,0EAA0E;AAC1E,0EAA0E;AAC1E,wDAAwD;AACxD,cAAc,yBAAyB,CAAC;AAGxC,OAAO,EACL,aAAa,GAMd,MAAM,aAAa,CAAC;AAErB,iFAAiF;AACjF,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * as sdk from '@restatedev/restate-sdk';
|
|
2
|
+
export { Schema } from './schema.ts';
|
|
3
|
+
export { asDatabase, lazyDatabase, database, reset, setStoreWrapper, Attempt, withAttempt } from './session.ts';
|
|
4
|
+
export type { SqlHost } from './session.ts';
|
|
5
|
+
export { open, type Connection, type Store, type ConnectionTransaction } from './database.ts';
|
|
6
|
+
export { PromiseSelector } from './selector.ts';
|
|
7
|
+
export { needing, served } from './serve.ts';
|
|
8
|
+
export { cancelCalls, cancelled, contextOf, HEAD_KEY, invoke, type AttemptContext } from './object.ts';
|
|
9
|
+
export type { DoProgressResult, Selectable, UnresolvedFuture } from './selector.ts';
|
|
10
|
+
//# sourceMappingURL=internal.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"internal.d.ts","sourceRoot":"","sources":["../src/internal.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,GAAG,MAAM,yBAAyB,CAAC;AAC/C,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,QAAQ,EAAE,KAAK,EAAE,eAAe,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAChH,YAAY,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,EAAE,IAAI,EAAE,KAAK,UAAU,EAAE,KAAK,KAAK,EAAE,KAAK,qBAAqB,EAAE,MAAM,eAAe,CAAC;AAC9F,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAChD,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAC7C,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,EAAE,KAAK,cAAc,EAAE,MAAM,aAAa,CAAC;AACvG,YAAY,EAAE,gBAAgB,EAAE,UAAU,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC"}
|
package/dist/internal.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// For tests: an attempt's session over a context of their own, the store's
|
|
2
|
+
// fault injection, a database over a store of their own, and the selector on
|
|
3
|
+
// its own. For this repository's libraries: the schemas their tables are in,
|
|
4
|
+
// databases of their own, and the Restate SDK as @restatedev/sessions runs it (its
|
|
5
|
+
// own copy, so that a library's journaled handlers serve beside its objects).
|
|
6
|
+
export * as sdk from '@restatedev/restate-sdk';
|
|
7
|
+
export { Schema } from './schema.js';
|
|
8
|
+
export { asDatabase, lazyDatabase, database, reset, setStoreWrapper, Attempt, withAttempt } from './session.js';
|
|
9
|
+
export { open } from './database.js';
|
|
10
|
+
export { PromiseSelector } from './selector.js';
|
|
11
|
+
export { needing, served } from './serve.js';
|
|
12
|
+
export { cancelCalls, cancelled, contextOf, HEAD_KEY, invoke } from './object.js';
|
|
13
|
+
//# sourceMappingURL=internal.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"internal.js","sourceRoot":"","sources":["../src/internal.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,6EAA6E;AAC7E,6EAA6E;AAC7E,mFAAmF;AACnF,8EAA8E;AAC9E,OAAO,KAAK,GAAG,MAAM,yBAAyB,CAAC;AAC/C,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,QAAQ,EAAE,KAAK,EAAE,eAAe,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAEhH,OAAO,EAAE,IAAI,EAA2D,MAAM,eAAe,CAAC;AAC9F,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAChD,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAC7C,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,EAAuB,MAAM,aAAa,CAAC"}
|