@tinyshed/tinystore 0.1.0-rc.4 → 0.1.0-rc.6
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 +37 -37
- package/bin/tinystore.js +12 -1
- package/dist/backup.d.ts +33 -0
- package/dist/backup.js +49 -0
- package/dist/blobs.d.ts +160 -79
- package/dist/blobs.js +510 -217
- package/dist/channels.d.ts +90 -0
- package/dist/channels.js +291 -0
- package/dist/clock.js +3 -3
- package/dist/config.d.ts +209 -86
- package/dist/config.js +581 -538
- package/dist/connection.d.ts +64 -2
- package/dist/connection.js +201 -16
- package/dist/console.d.ts +27 -3
- package/dist/console.js +165 -12
- package/dist/dotenv.d.ts +7 -0
- package/dist/dotenv.js +79 -0
- package/dist/errors.d.ts +61 -1
- package/dist/errors.js +81 -1
- package/dist/handles.d.ts +44 -2
- package/dist/handles.js +81 -2
- package/dist/index.d.ts +17 -11
- package/dist/index.js +5 -3
- package/dist/instruments.d.ts +177 -0
- package/dist/instruments.js +819 -0
- package/dist/jobs.d.ts +335 -239
- package/dist/jobs.js +716 -536
- package/dist/json.d.ts +2 -0
- package/dist/json.js +122 -0
- package/dist/kv.d.ts +130 -243
- package/dist/kv.js +273 -640
- package/dist/limits.d.ts +80 -0
- package/dist/limits.js +139 -0
- package/dist/locks.d.ts +62 -0
- package/dist/locks.js +237 -0
- package/dist/logger.d.ts +45 -9
- package/dist/logger.js +150 -38
- package/dist/metrics.d.ts +210 -209
- package/dist/metrics.js +319 -556
- package/dist/once.d.ts +19 -18
- package/dist/once.js +74 -63
- package/dist/query.d.ts +181 -0
- package/dist/query.js +788 -0
- package/dist/records.d.ts +241 -150
- package/dist/records.js +507 -291
- package/dist/runtime/addon.d.ts +14 -0
- package/dist/runtime/addon.js +69 -0
- package/dist/runtime/bun.js +5 -7
- package/dist/runtime/node.js +5 -7
- package/dist/runtime/pipe.d.ts +17 -0
- package/dist/runtime/pipe.js +133 -0
- package/dist/runtime/shared.d.ts +6 -0
- package/dist/runtime/shared.js +19 -0
- package/dist/runtime.d.ts +5 -2
- package/dist/search.d.ts +189 -0
- package/dist/search.js +281 -0
- package/dist/session.d.ts +31 -3
- package/dist/session.js +153 -41
- package/dist/sql.d.ts +228 -78
- package/dist/sql.js +745 -240
- package/dist/store.d.ts +244 -24
- package/dist/store.js +270 -67
- package/dist/time.d.ts +5 -1
- package/dist/time.js +22 -2
- package/dist/tx.d.ts +61 -0
- package/dist/tx.js +314 -0
- package/dist/wire/codec.d.ts +23 -7
- package/dist/wire/codec.js +56 -35
- package/dist/wire/frame.d.ts +2 -0
- package/dist/wire/frame.js +31 -14
- package/dist/wire/messages.d.ts +259 -0
- package/dist/wire/messages.js +22 -0
- package/dist/wire/msgpack.d.ts +49 -1
- package/dist/wire/msgpack.js +293 -56
- package/dist/wire/protocol.d.ts +2683 -0
- package/dist/wire/protocol.js +10846 -0
- package/package.json +39 -38
- package/src/backup.ts +81 -0
- package/src/blobs.ts +678 -301
- package/src/channels.ts +377 -0
- package/src/clock.ts +4 -4
- package/src/config.ts +902 -612
- package/src/connection.ts +279 -21
- package/src/console.ts +180 -12
- package/src/dotenv.ts +82 -0
- package/src/errors.ts +105 -0
- package/src/handles.ts +157 -8
- package/src/index.ts +158 -52
- package/src/instruments.ts +1042 -0
- package/src/jobs.ts +1017 -733
- package/src/json.ts +133 -0
- package/src/kv.ts +424 -847
- package/src/limits.ts +202 -0
- package/src/locks.ts +320 -0
- package/src/logger.ts +209 -40
- package/src/metrics.ts +557 -752
- package/src/once.ts +86 -83
- package/src/query.ts +1005 -0
- package/src/records.ts +795 -418
- package/src/runtime/addon.ts +99 -0
- package/src/runtime/bun.ts +5 -7
- package/src/runtime/node.ts +5 -7
- package/src/runtime/pipe.ts +178 -0
- package/src/runtime/shared.ts +20 -0
- package/src/runtime.ts +5 -2
- package/src/search.ts +480 -0
- package/src/session.ts +187 -41
- package/src/sql.ts +993 -306
- package/src/store.ts +473 -78
- package/src/time.ts +48 -3
- package/src/tx.ts +426 -0
- package/src/wire/codec.ts +78 -37
- package/src/wire/frame.ts +44 -14
- package/src/wire/messages.ts +25 -0
- package/src/wire/msgpack.ts +327 -56
- package/src/wire/protocol.ts +11709 -0
- package/dist/limiter.d.ts +0 -45
- package/dist/limiter.js +0 -68
- package/dist/quota.d.ts +0 -44
- package/dist/quota.js +0 -98
- package/src/limiter.ts +0 -99
- package/src/quota.ts +0 -137
package/dist/sql.d.ts
CHANGED
|
@@ -1,121 +1,271 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import {
|
|
1
|
+
import { type Channel, type TxChannel } from './channels.ts';
|
|
2
|
+
import type { Connection, Durability, Link } from './connection.ts';
|
|
3
|
+
import { type Home, type Open, type Via } from './handles.ts';
|
|
4
|
+
import { type Queue, type QueueOptions, type TxQueue, type Worker } from './jobs.ts';
|
|
5
|
+
import { Bucket, type BucketOptions } from './kv.ts';
|
|
6
|
+
import { Table, type TableOptions } from './query.ts';
|
|
7
|
+
import type { StandardSchemaV1 } from './schema.ts';
|
|
8
|
+
import { TableIndex, type TableIndexOptions } from './search.ts';
|
|
9
|
+
import type { Stream } from './session.ts';
|
|
3
10
|
import type { SqlArg, SqlValue } from './wire/codec.ts';
|
|
4
|
-
import { type JobsJob, SqlResults, SqlStatement } from './wire/messages.ts';
|
|
5
11
|
export type { SqlArg, SqlValue };
|
|
6
|
-
/** A row
|
|
12
|
+
/** A row type's columns: an array of each of its fields. */
|
|
13
|
+
export type Columns<T> = {
|
|
14
|
+
[K in keyof T]: T[K][];
|
|
15
|
+
};
|
|
16
|
+
/** A row as SQLite gave it, by its columns' names. */
|
|
7
17
|
export type Row = Record<string, SqlValue>;
|
|
8
18
|
/**
|
|
9
|
-
* A database's migrations: a directory
|
|
10
|
-
*
|
|
19
|
+
* A database's migrations: a directory of .sql files named 0001_notes.sql,
|
|
20
|
+
* 0002_tags.sql, or the files themselves by name, for a program bundled into
|
|
21
|
+
* one file. A relative directory is the working directory's.
|
|
11
22
|
*/
|
|
12
23
|
export type Migrations = string | Record<string, string> | readonly {
|
|
13
24
|
name: string;
|
|
14
|
-
|
|
25
|
+
sql: string;
|
|
15
26
|
}[];
|
|
16
|
-
export interface
|
|
17
|
-
/**
|
|
27
|
+
export interface DatabaseOptions {
|
|
28
|
+
/** applied when the file has not, and checked at every open; without them the file opens as it is */
|
|
18
29
|
migrations?: Migrations;
|
|
30
|
+
/**
|
|
31
|
+
* How far this database's commits go before they return, where the
|
|
32
|
+
* store's own is not this file's: `'os'` for what may be lost to a power
|
|
33
|
+
* loss, beside a store of what may not. Its buckets and queues commit
|
|
34
|
+
* with it. A database open already keeps what it was opened with.
|
|
35
|
+
*/
|
|
36
|
+
durability?: Durability;
|
|
19
37
|
}
|
|
20
|
-
/** What a write changed: the rows, and
|
|
38
|
+
/** What a write changed: the rows, and SQLite's rowid of the row it inserted, 0 when none. */
|
|
21
39
|
export interface Done {
|
|
22
40
|
changes: number;
|
|
23
|
-
|
|
41
|
+
lastInsertRowid: number | bigint;
|
|
24
42
|
}
|
|
25
43
|
/**
|
|
26
|
-
* A statement
|
|
27
|
-
*
|
|
28
|
-
*
|
|
44
|
+
* A statement in any of the ways a call takes it: a template whose values are
|
|
45
|
+
* its values, the text and the values of its ?s, the text and one object of
|
|
46
|
+
* named values, or a piece of SQL from `sql`.
|
|
29
47
|
*
|
|
48
|
+
* db.all`select * from notes where id = ${id}`
|
|
30
49
|
* db.all('select * from notes where id = ?', id)
|
|
31
50
|
* db.all('select * from notes where id = :id', { id })
|
|
32
|
-
* db.all`select * from notes where id = ${id}`
|
|
33
51
|
*/
|
|
34
|
-
export type Statement = [
|
|
35
|
-
/**
|
|
36
|
-
|
|
52
|
+
export type Statement = [text: string | TemplateStringsArray | Sql, ...values: unknown[]];
|
|
53
|
+
/**
|
|
54
|
+
* A piece of SQL and the values of its ?s, which are always values and never
|
|
55
|
+
* SQL. `sql` makes one; a call, a batch and a table's query take it.
|
|
56
|
+
*/
|
|
57
|
+
export declare class Sql {
|
|
58
|
+
readonly text: string;
|
|
59
|
+
readonly values: readonly SqlArg[];
|
|
60
|
+
/** whether it stands beside others in an and or an or without parentheses: one column's comparison */
|
|
61
|
+
readonly bare: boolean;
|
|
62
|
+
/** Pieces come from `sql`. */
|
|
63
|
+
constructor(text: string, values: readonly SqlArg[], bare?: boolean);
|
|
64
|
+
}
|
|
65
|
+
/** Whether text is one group in parentheses, or a bare 0 or 1, which may stand beside others as it is. */
|
|
66
|
+
export declare function grouped(text: string): boolean;
|
|
67
|
+
/** Pieces joined with and or or, each in parentheses unless it may stand as it is. */
|
|
68
|
+
export declare function joinedWith(pieces: readonly Sql[], joiner: string): Sql;
|
|
69
|
+
/**
|
|
70
|
+
* Whether a piece of SQL keeps its meaning beside others in an and or an or:
|
|
71
|
+
* one group, or text with no and or or of its own outside quotes, comments
|
|
72
|
+
* and parentheses, which would bind with its neighbours'.
|
|
73
|
+
*/
|
|
74
|
+
export declare function standsAlone(text: string): boolean;
|
|
75
|
+
/** Something that writes itself as SQL inside another piece: a table's query. */
|
|
76
|
+
export interface Embeddable {
|
|
77
|
+
/** its text and values, wrapped as a subquery is */
|
|
78
|
+
embedded(): Sql;
|
|
79
|
+
}
|
|
80
|
+
/** A condition: equal values in an object, or a piece of SQL. */
|
|
81
|
+
export type Condition = Sql | Record<string, unknown>;
|
|
37
82
|
/**
|
|
38
|
-
* A
|
|
39
|
-
*
|
|
40
|
-
* writes; all, one, scalar and each read, on readers that refuse to write.
|
|
83
|
+
* A piece of SQL: `` sql`total >= ${min}` ``, or `sql('total >= ?', min)`.
|
|
84
|
+
* Its helpers build the pieces a query's conditions are made of.
|
|
41
85
|
*/
|
|
42
|
-
export
|
|
86
|
+
export interface SqlTag {
|
|
87
|
+
(strings: TemplateStringsArray, ...values: unknown[]): Sql;
|
|
88
|
+
(text: string, ...values: unknown[]): Sql;
|
|
89
|
+
/** conditions joined with or, in parentheses; an empty or finds nothing */
|
|
90
|
+
or(...conditions: Condition[]): Sql;
|
|
91
|
+
/** conditions joined with and, in parentheses; an empty and finds everything */
|
|
92
|
+
and(...conditions: Condition[]): Sql;
|
|
93
|
+
/** a list for in (…), as one value SQLite reads with json_each: any length, the same statement */
|
|
94
|
+
list(values: readonly unknown[]): Sql;
|
|
95
|
+
/** a value kept as its JSON */
|
|
96
|
+
json(value: unknown): Sql;
|
|
97
|
+
/** a name, quoted, never SQL: a column a request chooses */
|
|
98
|
+
ident(name: string): Sql;
|
|
99
|
+
/** a JSON list in column `name` holding `value` */
|
|
100
|
+
has(name: string, value: unknown): Sql;
|
|
101
|
+
}
|
|
102
|
+
export declare const sql: SqlTag;
|
|
103
|
+
/** A condition as a piece of SQL: equal values written in the order of their columns' names. */
|
|
104
|
+
export declare function conditionOf(condition: Condition | string): Sql;
|
|
105
|
+
/** A name as SQL quotes it: "u"."created_at", each part on its own. */
|
|
106
|
+
export declare function identOf(name: string): string;
|
|
107
|
+
/**
|
|
108
|
+
* A value as it leaves: SQLite's five, a boolean, kept as 1 or 0, and a Date,
|
|
109
|
+
* kept as its unix milliseconds. Anything that would arrive changed is
|
|
110
|
+
* refused here, before anything is sent.
|
|
111
|
+
*/
|
|
112
|
+
export declare function cellOf(value: unknown, where: string): SqlArg;
|
|
113
|
+
/** What a text's placeholders are, counted outside quotes and comments. */
|
|
114
|
+
export declare function placeholders(text: string): {
|
|
115
|
+
positional: number;
|
|
116
|
+
named: string[];
|
|
117
|
+
numbered: boolean;
|
|
118
|
+
};
|
|
119
|
+
/** A statement as a piece of SQL, in whichever way it was given. */
|
|
120
|
+
export declare function statementOf(given: Statement): Sql;
|
|
121
|
+
/** Opens a database by its name, its migrations applied and checked before it resolves. */
|
|
122
|
+
export declare function openDatabase(link: Link, workers: Set<Worker>, name: string, options?: DatabaseOptions): Promise<Database>;
|
|
123
|
+
/** A query's rows as objects, refusing two columns of one name, which one object cannot hold. */
|
|
124
|
+
export declare function rowsOf<T>(columns: readonly string[], rows: readonly SqlValue[][]): T[];
|
|
125
|
+
/** What one query answered: its columns, and its rows' values. */
|
|
126
|
+
interface Answered {
|
|
127
|
+
columns: string[];
|
|
128
|
+
rows: SqlValue[][];
|
|
129
|
+
}
|
|
130
|
+
type Want = 'all' | 'one' | 'scalar';
|
|
131
|
+
/** What reads and writes a database: the database itself, or a transaction of it. */
|
|
132
|
+
export interface Runner {
|
|
133
|
+
readonly describe: string;
|
|
134
|
+
rows(statement: Sql, want: Want): Promise<Answered>;
|
|
135
|
+
exec(statement: Sql): Promise<Done>;
|
|
136
|
+
}
|
|
137
|
+
/** Whether the caller runs inside the function of a database's transaction. */
|
|
138
|
+
export declare function insideDatabaseTx(): boolean;
|
|
139
|
+
/**
|
|
140
|
+
* An application's database: `await store.database('app', { migrations })`.
|
|
141
|
+
* The verb says what comes back: all the rows, one or none, one value, or
|
|
142
|
+
* what a write changed. A write with `returning` goes through a read's verb
|
|
143
|
+
* and runs on the writer, its rows given once it is durable.
|
|
144
|
+
*/
|
|
145
|
+
export declare class Database implements Runner, Home {
|
|
43
146
|
#private;
|
|
44
147
|
readonly name: string;
|
|
45
|
-
|
|
148
|
+
readonly describe: string;
|
|
149
|
+
/** Databases come from `store.database`. */
|
|
150
|
+
constructor(link: Link, workers: Set<Worker>, name: string, open: Uint8Array);
|
|
46
151
|
handle(connection: Connection): Promise<number>;
|
|
47
|
-
|
|
152
|
+
keep(method: number, bytes: Uint8Array, make: (connection: Connection) => Promise<Uint8Array>): Open;
|
|
153
|
+
via(what: string): Via;
|
|
154
|
+
/**
|
|
155
|
+
* A bucket of values by key kept in this database's file, beside its
|
|
156
|
+
* tables, as `store.bucket` keeps one in kv.db: the same calls, and its
|
|
157
|
+
* writes commit with the rows in `db.tx`, through `tx.with(bucket)`.
|
|
158
|
+
*
|
|
159
|
+
* const sessions = db.bucket<Session>('sessions', { idle: '30d' })
|
|
160
|
+
*/
|
|
161
|
+
bucket<T = unknown>(name: string, options?: BucketOptions): Bucket<T>;
|
|
162
|
+
/**
|
|
163
|
+
* A queue of jobs kept in this database's file, as `store.queue` keeps one
|
|
164
|
+
* in jobs.db: the same calls and workers, and its jobs commit with the
|
|
165
|
+
* rows in `db.tx`, through `tx.with(queue)`. It shares the database's
|
|
166
|
+
* writer, which is the point and its cost.
|
|
167
|
+
*
|
|
168
|
+
* const emails = db.queue<Email>('emails', { attempts: 5 })
|
|
169
|
+
*/
|
|
170
|
+
queue<S extends StandardSchemaV1>(name: string, options: QueueOptions & {
|
|
171
|
+
schema: S;
|
|
172
|
+
}): Queue<StandardSchemaV1.InferOutput<S>>;
|
|
173
|
+
queue<T = unknown>(name: string, options?: QueueOptions): Queue<T>;
|
|
174
|
+
/**
|
|
175
|
+
* An index of a table of this database, kept in its file: its rows found
|
|
176
|
+
* again by their words, vectors or both, each hit's value its row as
|
|
177
|
+
* `all` reads it. The table owns the rows and the index follows them:
|
|
178
|
+
* every change of a row, by any statement, trigger or migration, reaches
|
|
179
|
+
* it in the commit that makes it, and one by a process beside the store
|
|
180
|
+
* within a second.
|
|
181
|
+
*
|
|
182
|
+
* const articles = db.searchIndex<Article>('articles_search', { table: 'articles', fields: { title: 2, body: 1 } })
|
|
183
|
+
*/
|
|
184
|
+
searchIndex<T = Row>(name: string, options: TableIndexOptions): TableIndex<T>;
|
|
185
|
+
/** Drops a table's index this database keeps, its triggers off the table; says whether there was one. */
|
|
186
|
+
dropSearchIndex(name: string): Promise<boolean>;
|
|
187
|
+
/** Every row a statement gives: `db.all<Note>\`select * from notes where author_id = ${id}\`` */
|
|
48
188
|
all<T = Row>(...statement: Statement): Promise<T[]>;
|
|
49
|
-
/** The one row a
|
|
189
|
+
/** The one row a statement gives, or undefined; two are InvalidError, and a write that gave them rolls back. */
|
|
50
190
|
one<T = Row>(...statement: Statement): Promise<T | undefined>;
|
|
51
|
-
/** The one value of
|
|
191
|
+
/** The one value of the one row a statement gives, as count(*) does; null as SQL answers it. */
|
|
52
192
|
scalar<T = SqlValue>(...statement: Statement): Promise<T>;
|
|
53
|
-
/** The columns and rows of a query as SQLite returned them, arrays rather than objects. */
|
|
54
|
-
query(...statement: Statement): Promise<{
|
|
55
|
-
columns: string[];
|
|
56
|
-
rows: SqlValue[][];
|
|
57
|
-
}>;
|
|
58
193
|
/**
|
|
59
|
-
*
|
|
60
|
-
*
|
|
194
|
+
* What `all` gives, by its columns: an array a column where `all` gives
|
|
195
|
+
* an object a row, for rows by the thousand, a chart's points. It comes
|
|
196
|
+
* faster, and a chart takes it as it is.
|
|
197
|
+
*
|
|
198
|
+
* const { ts, cpu } = await db.columns<Sample>`select ts, cpu from samples where host = ${host} order by ts`
|
|
199
|
+
*/
|
|
200
|
+
columns<T = Row>(...statement: Statement): Promise<Columns<T>>;
|
|
201
|
+
/**
|
|
202
|
+
* The rows a statement gives, one at a time as their parts arrive: an
|
|
203
|
+
* export that holds one part, not every row. The server reads them from
|
|
204
|
+
* one snapshot, at most 64 MiB.
|
|
61
205
|
*/
|
|
62
206
|
each<T = Row>(...statement: Statement): AsyncGenerator<T>;
|
|
63
|
-
/** Runs a statement on the writer and says what it changed. */
|
|
64
|
-
exec(...statement: Statement): Promise<Done>;
|
|
65
|
-
/** The rows a write's returning clause answers, run on the writer. */
|
|
66
|
-
execAll<T = Row>(...statement: Statement): Promise<T[]>;
|
|
67
|
-
execOne<T = Row>(...statement: Statement): Promise<T | undefined>;
|
|
68
|
-
execScalar<T = SqlValue>(...statement: Statement): Promise<T>;
|
|
69
207
|
/**
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
* returns comes back answered, an array's promises each:
|
|
208
|
+
* A table of the database, typed where it opens: its rows' inserts and
|
|
209
|
+
* upserts, and queries built without SQL text. It costs nothing until its
|
|
210
|
+
* first call; open each once, in the module that owns it.
|
|
74
211
|
*
|
|
75
|
-
* const
|
|
212
|
+
* const notes = db.table<Note>('notes', { types: { done: 'bool', created_at: 'time' } })
|
|
76
213
|
*/
|
|
77
|
-
|
|
214
|
+
table<S extends StandardSchemaV1>(name: string, options: TableOptions & {
|
|
215
|
+
schema: S;
|
|
216
|
+
}): Table<StandardSchemaV1.InferOutput<S>>;
|
|
217
|
+
table<T = Row>(name: string, options?: TableOptions): Table<T>;
|
|
218
|
+
/** Statements known before they run, written as one: all of them or none, in a shared commit. */
|
|
219
|
+
batch(statements: readonly Sql[]): Promise<Done[]>;
|
|
78
220
|
/**
|
|
79
|
-
* Runs
|
|
80
|
-
*
|
|
221
|
+
* Runs fn in a transaction that holds the writer, for writes that depend on
|
|
222
|
+
* what its reads found: returning commits, a throw rolls back. It runs
|
|
223
|
+
* once, five seconds at most, past which the server rolls it back; inside
|
|
224
|
+
* it, call nothing that waits on the world.
|
|
81
225
|
*
|
|
82
|
-
*
|
|
226
|
+
* await db.tx(async tx => {
|
|
227
|
+
* const left = await tx.scalar<number>`select stock from items where sku = ${sku}`
|
|
228
|
+
* if (left < 1) throw new SoldOut()
|
|
229
|
+
* await tx.exec`update items set stock = stock - 1 where sku = ${sku}`
|
|
230
|
+
* })
|
|
83
231
|
*/
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
statement: Parameters<typeof SqlStatement.encode>[0];
|
|
89
|
-
settle: (result: Result) => void;
|
|
90
|
-
fail: (err: unknown) => void;
|
|
91
|
-
}
|
|
92
|
-
/** A job a batch enqueues after its statements, on a queue that lives in its database. */
|
|
93
|
-
interface Queued {
|
|
94
|
-
/** the queue's jobs.open, whose handle the batch opens on its connection */
|
|
95
|
-
open: Uint8Array;
|
|
96
|
-
job: Parameters<typeof JobsJob.encode>[0];
|
|
97
|
-
settle: () => void;
|
|
98
|
-
fail: (err: unknown) => void;
|
|
232
|
+
tx<T>(fn: (tx: SqlTx) => T | Promise<T>): Promise<T>;
|
|
233
|
+
rows(statement: Sql, want: Want): Promise<Answered>;
|
|
234
|
+
/** Runs a write once it is durable; writes from many callers share a commit, each failing alone. */
|
|
235
|
+
exec(...statement: Statement): Promise<Done>;
|
|
99
236
|
}
|
|
100
|
-
/** The
|
|
101
|
-
|
|
237
|
+
/** The buckets and queues a transaction opened before it began, each with why its open failed, if it did. */
|
|
238
|
+
type Guests = Map<Open, unknown>;
|
|
239
|
+
/**
|
|
240
|
+
* A database's transaction, from `db.tx`: its reads see its writes, and a
|
|
241
|
+
* call that fails leaves it as it was before the call. Its calls go one at a
|
|
242
|
+
* time, in order.
|
|
243
|
+
*/
|
|
244
|
+
export declare class SqlTx implements Runner {
|
|
102
245
|
#private;
|
|
103
|
-
readonly
|
|
104
|
-
/**
|
|
105
|
-
|
|
106
|
-
readonly statements: Batched[];
|
|
107
|
-
readonly jobs: Queued[];
|
|
108
|
-
constructor(read: boolean, database: string);
|
|
109
|
-
close(): void;
|
|
110
|
-
/** Whether the promise is one of this batch's statements', rather than an async function's. */
|
|
111
|
-
made(promise: Promise<unknown>): boolean;
|
|
246
|
+
readonly describe: string;
|
|
247
|
+
/** Transactions come from `db.tx`. */
|
|
248
|
+
constructor(stream: Stream, database: Database, connection: Connection, guests: Guests);
|
|
112
249
|
/**
|
|
113
|
-
*
|
|
114
|
-
*
|
|
250
|
+
* A bucket or a queue opened from this database, inside the transaction:
|
|
251
|
+
* what it writes commits with the rows or not at all, and what it reads
|
|
252
|
+
* sees what the transaction wrote.
|
|
253
|
+
*
|
|
254
|
+
* await db.tx(async tx => {
|
|
255
|
+
* const order = await tx.table<Order>('orders').insert({ user_id, total })
|
|
256
|
+
* await tx.with(emails).add({ order: order.id, to })
|
|
257
|
+
* })
|
|
115
258
|
*/
|
|
116
|
-
|
|
117
|
-
|
|
259
|
+
with<T>(bucket: Bucket<T>): Bucket<T>;
|
|
260
|
+
with<T>(queue: Queue<T>): TxQueue<T>;
|
|
261
|
+
with<T>(channel: Channel<T>): TxChannel<T>;
|
|
118
262
|
all<T = Row>(...statement: Statement): Promise<T[]>;
|
|
119
263
|
one<T = Row>(...statement: Statement): Promise<T | undefined>;
|
|
120
264
|
scalar<T = SqlValue>(...statement: Statement): Promise<T>;
|
|
265
|
+
exec(...statement: Statement): Promise<Done>;
|
|
266
|
+
/** A table inside the transaction: its calls are the transaction's. */
|
|
267
|
+
table<T = Row>(name: string, options?: TableOptions): Table<T>;
|
|
268
|
+
rows(statement: Sql, want: Want): Promise<Answered>;
|
|
269
|
+
/** Sends the last call: commit, or roll back, and waits for the server's word. */
|
|
270
|
+
end(commit: boolean): Promise<void>;
|
|
121
271
|
}
|