@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/once.d.ts
CHANGED
|
@@ -1,31 +1,32 @@
|
|
|
1
1
|
import type { Link } from './connection.ts';
|
|
2
|
-
import type
|
|
3
|
-
import type { Duration } from './time.ts';
|
|
2
|
+
import { type Duration } from './time.ts';
|
|
4
3
|
import type { Key } from './wire/codec.ts';
|
|
5
4
|
export interface OnceOptions {
|
|
6
5
|
/** how long an answer is kept: a day when absent */
|
|
7
|
-
|
|
6
|
+
keep?: Duration | undefined;
|
|
8
7
|
}
|
|
9
|
-
/** Answers kept once a key: open them with `store.
|
|
10
|
-
export declare class Once<
|
|
8
|
+
/** Answers kept once a key: open them with `store.once(name)`. */
|
|
9
|
+
export declare class Once<T> {
|
|
11
10
|
#private;
|
|
12
11
|
readonly name: string;
|
|
13
|
-
/** Answers come from `store.
|
|
14
|
-
constructor(link: Link, name: string, open: Uint8Array,
|
|
12
|
+
/** Answers come from `store.once` and their `under`. */
|
|
13
|
+
constructor(link: Link, name: string, open: Uint8Array, under: readonly string[]);
|
|
15
14
|
/** The answers of a branch, each key apart from the same key elsewhere. */
|
|
16
|
-
|
|
15
|
+
under(...owners: Key[]): Once<T>;
|
|
17
16
|
/**
|
|
18
|
-
* The answer kept
|
|
19
|
-
* another
|
|
20
|
-
* keeps nothing and is thrown, so the next
|
|
21
|
-
* connection lost after fn returned and before its answer was
|
|
22
|
-
* OutcomeUnknownError.
|
|
17
|
+
* The answer kept for key, or fn's, which is kept. A run of a key that
|
|
18
|
+
* another runs, here or in another process, waits for it and gets its
|
|
19
|
+
* answer; a throw of fn keeps nothing and is thrown, so the next run runs
|
|
20
|
+
* fn again. A connection lost after fn returned and before its answer was
|
|
21
|
+
* kept is OutcomeUnknownError.
|
|
23
22
|
*
|
|
24
23
|
* const receipt = await charges.run(requestId, () => pay.charge(order, requestId))
|
|
25
24
|
*/
|
|
26
|
-
run(key: Key, fn: () =>
|
|
27
|
-
/** The answer kept
|
|
28
|
-
get(key: Key): Promise<
|
|
29
|
-
/** Forgets the answer kept
|
|
30
|
-
delete(key: Key): Promise<
|
|
25
|
+
run(key: Key, fn: () => T | Promise<T>): Promise<T>;
|
|
26
|
+
/** The answer kept for key, undefined when none is. */
|
|
27
|
+
get(key: Key): Promise<T | undefined>;
|
|
28
|
+
/** Forgets the answer kept for key, so that its next run runs fn again. */
|
|
29
|
+
delete(key: Key): Promise<boolean>;
|
|
31
30
|
}
|
|
31
|
+
/** What a once's open sends, checked before anything leaves. */
|
|
32
|
+
export declare function onceOpen(name: string, options?: OnceOptions): Uint8Array;
|
package/dist/once.js
CHANGED
|
@@ -1,84 +1,88 @@
|
|
|
1
|
-
//
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
1
|
+
// A function run once a key, its answer kept: a request sent twice is answered
|
|
2
|
+
// as the first was rather than done twice. The core lets one run of a key go
|
|
3
|
+
// at a time, every client of the store included: it answers the kept answer,
|
|
4
|
+
// or hands the run to this client and keeps what it sends back.
|
|
5
5
|
import { currentSignal } from './cancel.js';
|
|
6
|
-
import { OutcomeUnknownError } from './errors.js';
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
|
|
6
|
+
import { OutcomeUnknownError, ProtocolError } from './errors.js';
|
|
7
|
+
import { checkName, handleOn } from './handles.js';
|
|
8
|
+
import { callOn, jsonValues, keyText, underField } from './kv.js';
|
|
9
|
+
import { ConnectionLostError, watch } from './session.js';
|
|
10
|
+
import { ms } from './time.js';
|
|
11
|
+
import { KvAnswer, KvCall, KvFound, KvOnceOpen, methods } from './wire/protocol.js';
|
|
12
|
+
/** Answers kept once a key: open them with `store.once(name)`. */
|
|
11
13
|
export class Once {
|
|
12
14
|
name;
|
|
13
15
|
#link;
|
|
14
16
|
#open;
|
|
15
17
|
#values;
|
|
16
|
-
#
|
|
17
|
-
/** Answers come from `store.
|
|
18
|
-
constructor(link, name, open,
|
|
18
|
+
#under;
|
|
19
|
+
/** Answers come from `store.once` and their `under`. */
|
|
20
|
+
constructor(link, name, open, under) {
|
|
19
21
|
this.#link = link;
|
|
20
22
|
this.name = name;
|
|
21
23
|
this.#open = open;
|
|
22
|
-
this.#values =
|
|
23
|
-
this.#
|
|
24
|
+
this.#values = jsonValues;
|
|
25
|
+
this.#under = under;
|
|
24
26
|
}
|
|
25
27
|
/** The answers of a branch, each key apart from the same key elsewhere. */
|
|
26
|
-
|
|
27
|
-
return new Once(this.#link, this.name, this.#open, this.#
|
|
28
|
-
...this.#owners,
|
|
29
|
-
...owners.map(ownerText),
|
|
30
|
-
]);
|
|
28
|
+
under(...owners) {
|
|
29
|
+
return new Once(this.#link, this.name, this.#open, [...this.#under, ...owners.map(keyText)]);
|
|
31
30
|
}
|
|
32
31
|
/**
|
|
33
|
-
* The answer kept
|
|
34
|
-
* another
|
|
35
|
-
* keeps nothing and is thrown, so the next
|
|
36
|
-
* connection lost after fn returned and before its answer was
|
|
37
|
-
* OutcomeUnknownError.
|
|
32
|
+
* The answer kept for key, or fn's, which is kept. A run of a key that
|
|
33
|
+
* another runs, here or in another process, waits for it and gets its
|
|
34
|
+
* answer; a throw of fn keeps nothing and is thrown, so the next run runs
|
|
35
|
+
* fn again. A connection lost after fn returned and before its answer was
|
|
36
|
+
* kept is OutcomeUnknownError.
|
|
38
37
|
*
|
|
39
38
|
* const receipt = await charges.run(requestId, () => pay.charge(order, requestId))
|
|
40
39
|
*/
|
|
41
40
|
async run(key, fn) {
|
|
42
|
-
const
|
|
43
|
-
if (
|
|
44
|
-
return
|
|
41
|
+
const handed = await this.#claim(key);
|
|
42
|
+
if (!(handed instanceof Run)) {
|
|
43
|
+
return handed.value;
|
|
45
44
|
}
|
|
46
45
|
let answer;
|
|
47
|
-
let
|
|
46
|
+
let kept;
|
|
48
47
|
try {
|
|
49
48
|
answer = await fn();
|
|
50
|
-
|
|
49
|
+
kept = KvAnswer.encode({ found: true, value: this.#values.encode(answer) });
|
|
51
50
|
}
|
|
52
51
|
catch (err) {
|
|
53
|
-
await end(
|
|
52
|
+
await handed.end(KvAnswer.encode({})).catch(() => { });
|
|
54
53
|
throw err;
|
|
55
54
|
}
|
|
56
|
-
await end(
|
|
55
|
+
await handed.end(kept);
|
|
57
56
|
return answer;
|
|
58
57
|
}
|
|
59
|
-
/** The answer kept
|
|
58
|
+
/** The answer kept for key, undefined when none is. */
|
|
60
59
|
async get(key) {
|
|
61
|
-
const
|
|
62
|
-
|
|
60
|
+
const body = await this.#call('kv.once.get', key, 'read');
|
|
61
|
+
const kept = KvAnswer.decode(body);
|
|
62
|
+
return kept.found === true ? this.#values.decode(kept.value ?? null) : undefined;
|
|
63
63
|
}
|
|
64
|
-
/** Forgets the answer kept
|
|
64
|
+
/** Forgets the answer kept for key, so that its next run runs fn again. */
|
|
65
65
|
async delete(key) {
|
|
66
|
-
await this.#call('kv.delete', key, 'write');
|
|
66
|
+
return KvFound.decode(await this.#call('kv.once.delete', key, 'write')).found === true;
|
|
67
67
|
}
|
|
68
|
-
// asks for key's run: the answer kept
|
|
69
|
-
//
|
|
68
|
+
// asks for key's run: the answer kept for it, or the run the core handed
|
|
69
|
+
// over, which a connection lost before the answer asks for again
|
|
70
70
|
async #claim(key) {
|
|
71
71
|
return this.#link.run('read', async (connection) => {
|
|
72
|
-
const handle = await handleOn(connection, methods['kv.open'], this.#open);
|
|
73
|
-
const
|
|
74
|
-
const stream = await connection.session.open(methods['kv.run'],
|
|
72
|
+
const handle = await handleOn(connection, methods['kv.once.open'], this.#open);
|
|
73
|
+
const call = KvCall.encode({ handle, under: underField(this.#under), key });
|
|
74
|
+
const stream = await connection.session.open(methods['kv.once.run'], call, false);
|
|
75
75
|
const unwatch = watch(currentSignal(), stream);
|
|
76
76
|
try {
|
|
77
77
|
const first = await stream.next();
|
|
78
|
-
|
|
79
|
-
|
|
78
|
+
const offered = KvAnswer.decode(first.body);
|
|
79
|
+
if (!first.end) {
|
|
80
|
+
return new Run(stream);
|
|
80
81
|
}
|
|
81
|
-
|
|
82
|
+
if (offered.found !== true) {
|
|
83
|
+
throw new ProtocolError(`kv once ${this.name}: a run ended without its answer`);
|
|
84
|
+
}
|
|
85
|
+
return { value: this.#values.decode(offered.value ?? null) };
|
|
82
86
|
}
|
|
83
87
|
catch (err) {
|
|
84
88
|
stream.cancel();
|
|
@@ -89,28 +93,35 @@ export class Once {
|
|
|
89
93
|
}
|
|
90
94
|
});
|
|
91
95
|
}
|
|
92
|
-
|
|
93
|
-
return this.#link.
|
|
94
|
-
const handle = await handleOn(connection, methods['kv.open'], this.#open);
|
|
95
|
-
const body = KvCall.encode({ handle, owners: this.#ownersField(), key });
|
|
96
|
-
return KvEntry.decode(await connection.session.call(methods[method], body));
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
#ownersField() {
|
|
100
|
-
return this.#owners.length > 0 ? this.#owners : undefined;
|
|
96
|
+
#call(method, key, idempotence) {
|
|
97
|
+
return callOn(this.#link, 'kv.once.open', this.#open, this.#under, method, { key }, idempotence);
|
|
101
98
|
}
|
|
102
99
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
await stream.next();
|
|
100
|
+
/** A run the core handed to this client, until it sends what to keep. */
|
|
101
|
+
class Run {
|
|
102
|
+
#stream;
|
|
103
|
+
constructor(stream) {
|
|
104
|
+
this.#stream = stream;
|
|
109
105
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
106
|
+
/** Sends the run's answer as the stream's last DATA, and waits for the core's, once it is kept. */
|
|
107
|
+
async end(answer) {
|
|
108
|
+
try {
|
|
109
|
+
await this.#stream.send(answer, true);
|
|
110
|
+
await this.#stream.next();
|
|
111
|
+
}
|
|
112
|
+
catch (err) {
|
|
113
|
+
if (err instanceof ConnectionLostError) {
|
|
114
|
+
throw new OutcomeUnknownError(`the connection was lost before the answer was kept; the next run may run again: ${err.message}`);
|
|
115
|
+
}
|
|
116
|
+
throw err;
|
|
113
117
|
}
|
|
114
|
-
throw err;
|
|
115
118
|
}
|
|
116
119
|
}
|
|
120
|
+
/** What a once's open sends, checked before anything leaves. */
|
|
121
|
+
export function onceOpen(name, options = {}) {
|
|
122
|
+
checkName(name, 'once');
|
|
123
|
+
return KvOnceOpen.encode({
|
|
124
|
+
name,
|
|
125
|
+
keep: options.keep === undefined ? undefined : ms(options.keep),
|
|
126
|
+
});
|
|
127
|
+
}
|
package/dist/query.d.ts
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { type StandardSchemaV1 } from './schema.ts';
|
|
2
|
+
import { type Condition, type Done, type Embeddable, type Row, type Runner, Sql, type SqlArg, type SqlValue } from './sql.ts';
|
|
3
|
+
/** How a column SQLite keeps otherwise reads in JavaScript. */
|
|
4
|
+
export type ColumnType = 'bool' | 'time' | 'json' | 'bigint';
|
|
5
|
+
export interface TableOptions {
|
|
6
|
+
/** the columns SQLite keeps as 0 or 1, unix milliseconds, JSON text or an integer past 2^53 */
|
|
7
|
+
types?: Record<string, ColumnType>;
|
|
8
|
+
/** a Standard Schema, zod's or valibot's, that checks each row read */
|
|
9
|
+
schema?: StandardSchemaV1;
|
|
10
|
+
}
|
|
11
|
+
/** What an upsert does when the row's key is taken. */
|
|
12
|
+
export interface UpsertOptions<T> {
|
|
13
|
+
/** the columns whose value, taken, makes the insert an update: the key */
|
|
14
|
+
conflict: string | readonly string[];
|
|
15
|
+
/** the columns the update sets from the row; none other changes */
|
|
16
|
+
update: readonly (keyof T & string)[];
|
|
17
|
+
/** the condition the row that holds the key must meet, its owner's, or it stays as it is */
|
|
18
|
+
where?: Condition | undefined;
|
|
19
|
+
}
|
|
20
|
+
/** A page of a query's rows, and the cursor that reads the next; none after the last. */
|
|
21
|
+
export interface RowPage<T> {
|
|
22
|
+
rows: T[];
|
|
23
|
+
next?: string;
|
|
24
|
+
}
|
|
25
|
+
type Direction = 'asc' | 'desc';
|
|
26
|
+
/** A condition of a query, and whether it may stand in an and without parentheses. */
|
|
27
|
+
interface Kept {
|
|
28
|
+
piece: Sql;
|
|
29
|
+
bare: boolean;
|
|
30
|
+
}
|
|
31
|
+
interface Parts {
|
|
32
|
+
table: string;
|
|
33
|
+
alias?: string | undefined;
|
|
34
|
+
select?: Sql | undefined;
|
|
35
|
+
joins: Sql[];
|
|
36
|
+
conditions: Kept[];
|
|
37
|
+
groups: string[];
|
|
38
|
+
having: Kept[];
|
|
39
|
+
order: {
|
|
40
|
+
piece: Sql;
|
|
41
|
+
column?: string | undefined;
|
|
42
|
+
direction: Direction;
|
|
43
|
+
}[];
|
|
44
|
+
limit?: number | undefined;
|
|
45
|
+
offset?: number | undefined;
|
|
46
|
+
includes: Included[];
|
|
47
|
+
}
|
|
48
|
+
/** A query whose rows each row of another holds as a list under a name. */
|
|
49
|
+
interface Included {
|
|
50
|
+
name: string;
|
|
51
|
+
parts: Parts;
|
|
52
|
+
/** how the included rows read: their table's types and schema */
|
|
53
|
+
source: Source;
|
|
54
|
+
/** the names of the columns its select gives, in their order */
|
|
55
|
+
columns: string[];
|
|
56
|
+
}
|
|
57
|
+
/** How a table reads and writes: its runner, its name, and how its rows read. */
|
|
58
|
+
interface Source {
|
|
59
|
+
runner: Runner;
|
|
60
|
+
types: Record<string, ColumnType>;
|
|
61
|
+
schema?: StandardSchemaV1 | undefined;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* A query on a table: a value, each call returning a new one, so that the
|
|
65
|
+
* first stays as it was. `all`, `one`, `scalar`, `count`, `each` and `list`
|
|
66
|
+
* read; `update` and `delete` write.
|
|
67
|
+
*
|
|
68
|
+
* const urgent = await orders
|
|
69
|
+
* .where({ status: 'pending', deleted: false })
|
|
70
|
+
* .where(sql.or(sql`amount >= ${1000}`, { priority: 'high' }))
|
|
71
|
+
* .orderBy('created_at', 'desc')
|
|
72
|
+
* .limit(50)
|
|
73
|
+
* .all()
|
|
74
|
+
*/
|
|
75
|
+
export declare class Query<T = Row> implements Embeddable {
|
|
76
|
+
#private;
|
|
77
|
+
/** Queries come from `db.table`. */
|
|
78
|
+
constructor(source: Source, parts: Parts);
|
|
79
|
+
/**
|
|
80
|
+
* Adds a condition with and: equal values, `null` as is null and a list
|
|
81
|
+
* as in (…); a piece of SQL from `sql`; or text and the values of its ?s.
|
|
82
|
+
* `undefined` is InvalidError, never a filter left out.
|
|
83
|
+
*/
|
|
84
|
+
where(condition: Condition | string, ...values: unknown[]): Query<T>;
|
|
85
|
+
/** The columns a row holds, as SQL text, and the shape of the row they make. */
|
|
86
|
+
select<R = Row>(columns: string | Sql): Query<R>;
|
|
87
|
+
/**
|
|
88
|
+
* Gives each row the rows of another query as a list under `name`, from
|
|
89
|
+
* the same statement: the query names the row it belongs to in its
|
|
90
|
+
* `where`, by this table's alias, names its columns with `select`, and has
|
|
91
|
+
* a `limit`, which counts for each row. Every value comes back as it is
|
|
92
|
+
* kept, bytes and integers past 2^53 among them.
|
|
93
|
+
*
|
|
94
|
+
* const latest = db.table<Post>('posts as p').select<{ id: string; title: string }>('p.id, p.title')
|
|
95
|
+
* .where('p.author_id = u.id').orderBy('p.id', 'desc').limit(3)
|
|
96
|
+
* const authors = await db.table<User>('users as u').include('posts', latest).all()
|
|
97
|
+
*/
|
|
98
|
+
include<N extends string, C>(name: N, query: Query<C>): Query<T & Record<N, C[]>>;
|
|
99
|
+
/** Joins a table, `'users as u'`, on a condition as SQL text or a piece of SQL. */
|
|
100
|
+
join(table: string, on: string | Sql): Query<T>;
|
|
101
|
+
/** Joins a table whose columns may be null where no row of it meets the condition. */
|
|
102
|
+
leftJoin(table: string, on: string | Sql): Query<T>;
|
|
103
|
+
/** Groups the rows by columns, quoted as names. */
|
|
104
|
+
groupBy(...columns: string[]): Query<T>;
|
|
105
|
+
/** A condition on the groups, as `where` takes one. */
|
|
106
|
+
having(condition: Condition | string, ...values: unknown[]): Query<T>;
|
|
107
|
+
/**
|
|
108
|
+
* Orders the rows by a column, quoted as a name, so that a sort a request
|
|
109
|
+
* chooses cannot be SQL, or by a piece of SQL: `` orderBy(sql`lower(title)`) ``.
|
|
110
|
+
*/
|
|
111
|
+
orderBy(column: string | Sql, direction?: Direction): Query<T>;
|
|
112
|
+
limit(rows: number): Query<T>;
|
|
113
|
+
offset(rows: number): Query<T>;
|
|
114
|
+
/** The text and the values the query sends, to read or to log. */
|
|
115
|
+
toSQL(): {
|
|
116
|
+
text: string;
|
|
117
|
+
values: SqlArg[];
|
|
118
|
+
};
|
|
119
|
+
/** The query inside another piece of SQL: a subquery, in parentheses. */
|
|
120
|
+
embedded(): Sql;
|
|
121
|
+
all(): Promise<T[]>;
|
|
122
|
+
one(): Promise<T | undefined>;
|
|
123
|
+
scalar<V = SqlValue>(): Promise<V>;
|
|
124
|
+
/** Every row the conditions match, without the order and the limit. */
|
|
125
|
+
count(): Promise<number>;
|
|
126
|
+
/** The rows one at a time, as their parts arrive. */
|
|
127
|
+
each(): AsyncGenerator<T>;
|
|
128
|
+
/**
|
|
129
|
+
* A page after the last row of the one before, by the query's order,
|
|
130
|
+
* which ends in a unique column so that no row is read twice or skipped.
|
|
131
|
+
* `next` reads the next page, and only of this query.
|
|
132
|
+
*
|
|
133
|
+
* const first = await orders.orderBy('created_at', 'desc').orderBy('id', 'desc').list({ limit: 50 })
|
|
134
|
+
* const second = await orders.orderBy('created_at', 'desc').orderBy('id', 'desc').list({ limit: 50, after: first.next })
|
|
135
|
+
*/
|
|
136
|
+
list(options?: {
|
|
137
|
+
limit?: number | undefined;
|
|
138
|
+
after?: string | undefined;
|
|
139
|
+
}): Promise<RowPage<T>>;
|
|
140
|
+
/** SQLite's plan for the query, a line a step. */
|
|
141
|
+
explain(): Promise<string[]>;
|
|
142
|
+
/** Sets columns of the rows the conditions match, and says how many changed. */
|
|
143
|
+
update(values: {
|
|
144
|
+
[K in keyof T]?: T[K] | Sql;
|
|
145
|
+
} & Record<string, unknown>): Promise<Done>;
|
|
146
|
+
/** Deletes the rows the conditions match, and says how many it deleted. */
|
|
147
|
+
delete(): Promise<Done>;
|
|
148
|
+
/** A row as the table's types and schema read it. */
|
|
149
|
+
protected read(row: Row): Promise<T>;
|
|
150
|
+
protected get source(): Source;
|
|
151
|
+
protected get parts(): Parts;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* A table: its queries, and its rows' inserts and upserts.
|
|
155
|
+
*
|
|
156
|
+
* const notes = db.table<Note>('notes', { types: { done: 'bool', tags: 'json', created_at: 'time' } })
|
|
157
|
+
* const note = await notes.insert({ id: uuidv7(), author_id: userId, title: 'Buy milk', done: false, tags: [], created_at: new Date() })
|
|
158
|
+
*/
|
|
159
|
+
export declare class Table<T = Row> extends Query<T> {
|
|
160
|
+
readonly name: string;
|
|
161
|
+
/** Tables come from `db.table` and `tx.table`. */
|
|
162
|
+
constructor(runner: Runner, name: string, options?: TableOptions);
|
|
163
|
+
/** Inserts a row and gives it back as the file keeps it; a list of rows is one write, all of them or none. */
|
|
164
|
+
insert(row: Partial<T>): Promise<T>;
|
|
165
|
+
insert(rows: readonly Partial<T>[]): Promise<T[]>;
|
|
166
|
+
/**
|
|
167
|
+
* Inserts a row, or, when its key is taken, sets the columns `update`
|
|
168
|
+
* names on the row that holds it, if that row meets `where`. It gives
|
|
169
|
+
* back the row as the file keeps it, or undefined when `where` kept it.
|
|
170
|
+
*/
|
|
171
|
+
upsert(row: Partial<T>, options: UpsertOptions<T>): Promise<T | undefined>;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* The names of the columns a select's text gives, in their order: a column's
|
|
175
|
+
* own name, or what follows its `as`. An expression without a name is
|
|
176
|
+
* refused, since its rows could not be given their fields.
|
|
177
|
+
*/
|
|
178
|
+
export declare function namesOf(select: string, describe: string): string[];
|
|
179
|
+
/** The rows an included query gave, read from their literals: `(1,'it''s',NULL),(2,X'00ff',1.5)`. */
|
|
180
|
+
export declare function literalRows(text: string): SqlValue[][];
|
|
181
|
+
export {};
|