@encore.dev/sdk 0.0.0 → 2.0.0-beta.5
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 +362 -0
- package/README.md +122 -2
- package/app_meta.test.ts +134 -0
- package/app_meta.ts +186 -0
- package/config/config.test.ts +381 -0
- package/config/config.ts +178 -0
- package/config/mod.ts +14 -0
- package/config/secrets.ts +147 -0
- package/config/value.ts +243 -0
- package/cron/mod.ts +23 -0
- package/dist/app_meta.d.ts +93 -0
- package/dist/app_meta.js +65 -0
- package/dist/app_meta.js.map +1 -0
- package/dist/config/config.d.ts +105 -0
- package/dist/config/config.js +87 -0
- package/dist/config/config.js.map +1 -0
- package/dist/config/mod.d.ts +4 -0
- package/dist/config/mod.js +3 -0
- package/dist/config/mod.js.map +1 -0
- package/dist/config/secrets.d.ts +92 -0
- package/dist/config/secrets.js +80 -0
- package/dist/config/secrets.js.map +1 -0
- package/dist/config/value.d.ts +51 -0
- package/dist/config/value.js +178 -0
- package/dist/config/value.js.map +1 -0
- package/dist/cron/mod.d.ts +22 -0
- package/dist/cron/mod.js +9 -0
- package/dist/cron/mod.js.map +1 -0
- package/dist/internal/otel/backend.d.ts +50 -0
- package/dist/internal/otel/backend.js +2 -0
- package/dist/internal/otel/backend.js.map +1 -0
- package/dist/internal/otel/context.d.ts +10 -0
- package/dist/internal/otel/context.js +34 -0
- package/dist/internal/otel/context.js.map +1 -0
- package/dist/internal/otel/context_manager.d.ts +22 -0
- package/dist/internal/otel/context_manager.js +120 -0
- package/dist/internal/otel/context_manager.js.map +1 -0
- package/dist/internal/otel/encode.d.ts +112 -0
- package/dist/internal/otel/encode.js +290 -0
- package/dist/internal/otel/encode.js.map +1 -0
- package/dist/internal/otel/index.d.ts +9 -0
- package/dist/internal/otel/index.js +9 -0
- package/dist/internal/otel/index.js.map +1 -0
- package/dist/internal/otel/meter.d.ts +73 -0
- package/dist/internal/otel/meter.js +265 -0
- package/dist/internal/otel/meter.js.map +1 -0
- package/dist/internal/otel/propagator.d.ts +14 -0
- package/dist/internal/otel/propagator.js +61 -0
- package/dist/internal/otel/propagator.js.map +1 -0
- package/dist/internal/otel/provider.d.ts +9 -0
- package/dist/internal/otel/provider.js +19 -0
- package/dist/internal/otel/provider.js.map +1 -0
- package/dist/internal/otel/request_context.d.ts +11 -0
- package/dist/internal/otel/request_context.js +20 -0
- package/dist/internal/otel/request_context.js.map +1 -0
- package/dist/internal/otel/span.d.ts +61 -0
- package/dist/internal/otel/span.js +206 -0
- package/dist/internal/otel/span.js.map +1 -0
- package/dist/internal/otel/tracer.d.ts +15 -0
- package/dist/internal/otel/tracer.js +65 -0
- package/dist/internal/otel/tracer.js.map +1 -0
- package/dist/internal/reqtrack/mod.d.ts +6 -0
- package/dist/internal/reqtrack/mod.js +28 -0
- package/dist/internal/reqtrack/mod.js.map +1 -0
- package/dist/internal/runtime/meter.d.ts +11 -0
- package/dist/internal/runtime/meter.js +22 -0
- package/dist/internal/runtime/meter.js.map +1 -0
- package/dist/internal/runtime/mod.d.ts +12 -0
- package/dist/internal/runtime/mod.js +28 -0
- package/dist/internal/runtime/mod.js.map +1 -0
- package/dist/internal/runtime/napi/napi.cjs +122 -0
- package/dist/internal/runtime/napi/napi.d.cts +601 -0
- package/dist/internal/runtime/shutdown.d.ts +13 -0
- package/dist/internal/runtime/shutdown.js +56 -0
- package/dist/internal/runtime/shutdown.js.map +1 -0
- package/dist/internal/utils/constraints.d.ts +9 -0
- package/dist/internal/utils/constraints.js +2 -0
- package/dist/internal/utils/constraints.js.map +1 -0
- package/dist/log/index.d.ts +39 -0
- package/dist/log/index.js +44 -0
- package/dist/log/index.js.map +1 -0
- package/dist/log/logger.d.ts +123 -0
- package/dist/log/logger.js +203 -0
- package/dist/log/logger.js.map +1 -0
- package/dist/log/runtime.d.ts +19 -0
- package/dist/log/runtime.js +22 -0
- package/dist/log/runtime.js.map +1 -0
- package/dist/log/stderr.d.ts +9 -0
- package/dist/log/stderr.js +25 -0
- package/dist/log/stderr.js.map +1 -0
- package/dist/metrics/mod.d.ts +315 -0
- package/dist/metrics/mod.js +488 -0
- package/dist/metrics/mod.js.map +1 -0
- package/dist/mod.d.ts +6 -0
- package/dist/mod.js +4 -0
- package/dist/mod.js.map +1 -0
- package/dist/otel.d.ts +9 -0
- package/dist/otel.js +13 -0
- package/dist/otel.js.map +1 -0
- package/dist/pubsub/encoding.d.ts +3 -0
- package/dist/pubsub/encoding.js +10 -0
- package/dist/pubsub/encoding.js.map +1 -0
- package/dist/pubsub/mod.d.ts +6 -0
- package/dist/pubsub/mod.js +3 -0
- package/dist/pubsub/mod.js.map +1 -0
- package/dist/pubsub/refs.d.ts +12 -0
- package/dist/pubsub/refs.js +11 -0
- package/dist/pubsub/refs.js.map +1 -0
- package/dist/pubsub/subscription.d.ts +128 -0
- package/dist/pubsub/subscription.js +39 -0
- package/dist/pubsub/subscription.js.map +1 -0
- package/dist/pubsub/topic.d.ts +148 -0
- package/dist/pubsub/topic.js +164 -0
- package/dist/pubsub/topic.js.map +1 -0
- package/dist/req_meta.d.ts +47 -0
- package/dist/req_meta.js +26 -0
- package/dist/req_meta.js.map +1 -0
- package/dist/services/index.d.ts +12 -0
- package/dist/services/index.js +22 -0
- package/dist/services/index.js.map +1 -0
- package/dist/shutdown/mod.d.ts +23 -0
- package/dist/shutdown/mod.js +24 -0
- package/dist/shutdown/mod.js.map +1 -0
- package/dist/storage/objects/bucket.d.ts +216 -0
- package/dist/storage/objects/bucket.js +123 -0
- package/dist/storage/objects/bucket.js.map +1 -0
- package/dist/storage/objects/error.d.ts +14 -0
- package/dist/storage/objects/error.js +81 -0
- package/dist/storage/objects/error.js.map +1 -0
- package/dist/storage/objects/mod.d.ts +4 -0
- package/dist/storage/objects/mod.js +3 -0
- package/dist/storage/objects/mod.js.map +1 -0
- package/dist/storage/objects/refs.d.ts +31 -0
- package/dist/storage/objects/refs.js +20 -0
- package/dist/storage/objects/refs.js.map +1 -0
- package/dist/storage/postgres/database.d.ts +295 -0
- package/dist/storage/postgres/database.js +378 -0
- package/dist/storage/postgres/database.js.map +1 -0
- package/dist/storage/postgres/mod.d.ts +2 -0
- package/dist/storage/postgres/mod.js +2 -0
- package/dist/storage/postgres/mod.js.map +1 -0
- package/dist/storage/redis/commands.d.ts +573 -0
- package/dist/storage/redis/commands.js +637 -0
- package/dist/storage/redis/commands.js.map +1 -0
- package/dist/storage/redis/database.d.ts +302 -0
- package/dist/storage/redis/database.js +336 -0
- package/dist/storage/redis/database.js.map +1 -0
- package/dist/storage/redis/mod.d.ts +88 -0
- package/dist/storage/redis/mod.js +84 -0
- package/dist/storage/redis/mod.js.map +1 -0
- package/dist/storage/redis/native.d.ts +8 -0
- package/dist/storage/redis/native.js +11 -0
- package/dist/storage/redis/native.js.map +1 -0
- package/dist/storage/redis/options.d.ts +89 -0
- package/dist/storage/redis/options.js +53 -0
- package/dist/storage/redis/options.js.map +1 -0
- package/dist/storage/redis/pubsub.d.ts +53 -0
- package/dist/storage/redis/pubsub.js +88 -0
- package/dist/storage/redis/pubsub.js.map +1 -0
- package/dist/storage/redis/reply.d.ts +256 -0
- package/dist/storage/redis/reply.js +572 -0
- package/dist/storage/redis/reply.js.map +1 -0
- package/dist/storage/redis/scan.d.ts +11 -0
- package/dist/storage/redis/scan.js +76 -0
- package/dist/storage/redis/scan.js.map +1 -0
- package/dist/storage/redis/script.d.ts +91 -0
- package/dist/storage/redis/script.js +143 -0
- package/dist/storage/redis/script.js.map +1 -0
- package/dist/storage/redis/target.d.ts +47 -0
- package/dist/storage/redis/target.js +82 -0
- package/dist/storage/redis/target.js.map +1 -0
- package/dist/storage/redis/wire.d.ts +105 -0
- package/dist/storage/redis/wire.js +333 -0
- package/dist/storage/redis/wire.js.map +1 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -0
- package/dist/types/mod.d.ts +42 -0
- package/dist/types/mod.js +65 -0
- package/dist/types/mod.js.map +1 -0
- package/internal/otel/backend.ts +60 -0
- package/internal/otel/context.test.ts +70 -0
- package/internal/otel/context.ts +48 -0
- package/internal/otel/context_manager.test.ts +154 -0
- package/internal/otel/context_manager.ts +141 -0
- package/internal/otel/encode.test.ts +266 -0
- package/internal/otel/encode.ts +408 -0
- package/internal/otel/index.ts +20 -0
- package/internal/otel/meter.test.ts +231 -0
- package/internal/otel/meter.ts +332 -0
- package/internal/otel/propagator.test.ts +105 -0
- package/internal/otel/propagator.ts +68 -0
- package/internal/otel/provider.ts +23 -0
- package/internal/otel/request_context.ts +32 -0
- package/internal/otel/span.ts +252 -0
- package/internal/otel/tracer.test.ts +352 -0
- package/internal/otel/tracer.ts +104 -0
- package/internal/reqtrack/mod.ts +39 -0
- package/internal/runtime/meter.ts +26 -0
- package/internal/runtime/mod.ts +41 -0
- package/internal/runtime/napi/napi.cjs +122 -0
- package/internal/runtime/napi/napi.d.cts +601 -0
- package/internal/runtime/shutdown.test.ts +248 -0
- package/internal/runtime/shutdown.ts +64 -0
- package/internal/utils/constraints.ts +12 -0
- package/log/index.ts +107 -0
- package/log/logger.test.ts +194 -0
- package/log/logger.ts +308 -0
- package/log/runtime.test.ts +65 -0
- package/log/runtime.ts +40 -0
- package/log/stderr.test.ts +23 -0
- package/log/stderr.ts +24 -0
- package/metrics/export.test.ts +337 -0
- package/metrics/mod.test.ts +180 -0
- package/metrics/mod.ts +619 -0
- package/mod.ts +22 -0
- package/otel.test.ts +84 -0
- package/otel.ts +17 -0
- package/package.json +121 -5
- package/pubsub/encoding.ts +8 -0
- package/pubsub/mod.ts +14 -0
- package/pubsub/pubsub.test.ts +186 -0
- package/pubsub/refs.ts +22 -0
- package/pubsub/subscription.ts +180 -0
- package/pubsub/topic.test.ts +121 -0
- package/pubsub/topic.ts +212 -0
- package/req_meta.ts +75 -0
- package/services/index.test.ts +30 -0
- package/services/index.ts +21 -0
- package/shutdown/mod.ts +24 -0
- package/storage/objects/bucket.ts +338 -0
- package/storage/objects/error.ts +94 -0
- package/storage/objects/mod.ts +35 -0
- package/storage/objects/refs.ts +69 -0
- package/storage/postgres/database.integration.test.ts +175 -0
- package/storage/postgres/database.test.ts +241 -0
- package/storage/postgres/database.ts +497 -0
- package/storage/postgres/mod.ts +15 -0
- package/storage/redis/commands.ts +1221 -0
- package/storage/redis/database.ts +466 -0
- package/storage/redis/mod.ts +135 -0
- package/storage/redis/native.ts +11 -0
- package/storage/redis/options.ts +119 -0
- package/storage/redis/pubsub.ts +92 -0
- package/storage/redis/redis.test.ts +706 -0
- package/storage/redis/reply.ts +705 -0
- package/storage/redis/runtime.test.ts +233 -0
- package/storage/redis/scan.ts +125 -0
- package/storage/redis/script.ts +162 -0
- package/storage/redis/target.ts +131 -0
- package/storage/redis/wire.ts +369 -0
- package/types/mod.ts +92 -0
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
import { activeCtxBytes } from '../../internal/otel/index.js';
|
|
2
|
+
import { commandGroups, queueGroups, readCommandGroups, } from './commands.js';
|
|
3
|
+
import { BatchError, Reply } from './reply.js';
|
|
4
|
+
import { Invocation, QueuedInvocation } from './script.js';
|
|
5
|
+
import { ConnTarget, PoolTarget } from './target.js';
|
|
6
|
+
import { encodeList } from './wire.js';
|
|
7
|
+
/** The replies of a batch, or the error some of its commands failed with. */
|
|
8
|
+
function batchReplies(values) {
|
|
9
|
+
const replies = values.map(v => new Reply(v));
|
|
10
|
+
const failed = replies.find(r => r.error);
|
|
11
|
+
if (failed)
|
|
12
|
+
throw new BatchError(replies, failed.error);
|
|
13
|
+
return replies;
|
|
14
|
+
}
|
|
15
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
|
|
16
|
+
export class ReadOnlyDatabaseRef {
|
|
17
|
+
t;
|
|
18
|
+
primary;
|
|
19
|
+
/** @internal */
|
|
20
|
+
constructor(t, groups,
|
|
21
|
+
/**
|
|
22
|
+
* The runtime gives a database one pool for now, on the primary, which
|
|
23
|
+
* every handle shares.
|
|
24
|
+
*/
|
|
25
|
+
primary) {
|
|
26
|
+
this.t = t;
|
|
27
|
+
this.primary = primary;
|
|
28
|
+
Object.assign(this, groups);
|
|
29
|
+
}
|
|
30
|
+
/** A call to `script`, which must be one made by {@link Script.readOnly}. */
|
|
31
|
+
script(script) {
|
|
32
|
+
return new Invocation(this.t, script, true);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
|
|
36
|
+
export class DatabaseRef {
|
|
37
|
+
t;
|
|
38
|
+
groups;
|
|
39
|
+
/** @internal */
|
|
40
|
+
constructor(
|
|
41
|
+
/** @internal */
|
|
42
|
+
t,
|
|
43
|
+
/** @internal */
|
|
44
|
+
groups) {
|
|
45
|
+
this.t = t;
|
|
46
|
+
this.groups = groups;
|
|
47
|
+
Object.assign(this, groups);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* A batch of commands sent in one round trip. Not a transaction: other
|
|
51
|
+
* clients' commands may run between them, and one failing does not stop
|
|
52
|
+
* the rest.
|
|
53
|
+
*
|
|
54
|
+
* ```ts
|
|
55
|
+
* const pipe = SESSIONS.pipeline();
|
|
56
|
+
* pipe.string.get("name:1");
|
|
57
|
+
* pipe.string.get("name:2");
|
|
58
|
+
* const [first, second] = await pipe.exec();
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
pipeline() {
|
|
62
|
+
return new Pipeline(this.t);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* A transaction (`MULTI`/`EXEC`): the commands queued on it run together,
|
|
66
|
+
* with no other client's in between.
|
|
67
|
+
*
|
|
68
|
+
* Nothing is sent until {@link Transaction.exec}, so a transaction is never
|
|
69
|
+
* left open while the code waits on something else.
|
|
70
|
+
*
|
|
71
|
+
* ```ts
|
|
72
|
+
* const tx = LEDGER.transaction();
|
|
73
|
+
* tx.string.incr("hits", 1);
|
|
74
|
+
* tx.hash.hset("user:1", "seen", Date.now());
|
|
75
|
+
* const [hits] = await tx.exec();
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
transaction() {
|
|
79
|
+
return new Transaction(this.t);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Takes a connection out of the pool, for what must run on one connection:
|
|
83
|
+
* watching keys, then a transaction that depends on them. Return it with
|
|
84
|
+
* {@link Connection.close}, or declare it with `await using`.
|
|
85
|
+
*/
|
|
86
|
+
async acquire() {
|
|
87
|
+
const impl = await this.t.database().acquire(activeCtxBytes());
|
|
88
|
+
return new Connection(impl, this.t);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Runs `body` as an optimistic transaction on `keys`: `body` reads what it
|
|
92
|
+
* needs through the connection and queues its changes on the transaction,
|
|
93
|
+
* and the changes are applied only if none of `keys` changed in between. If
|
|
94
|
+
* one did, `body` runs again.
|
|
95
|
+
*
|
|
96
|
+
* ```ts
|
|
97
|
+
* const left = await LEDGER.watch(["balance"], async (conn, tx) => {
|
|
98
|
+
* const balance = (await conn.string.get("balance").number()) ?? 0;
|
|
99
|
+
* tx.string.set("balance", balance - 10);
|
|
100
|
+
* return balance - 10;
|
|
101
|
+
* });
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
async watch(keys, body) {
|
|
105
|
+
// A key changing this many times in a row is not contention that another
|
|
106
|
+
// attempt will get past.
|
|
107
|
+
const attempts = 64;
|
|
108
|
+
const conn = await this.acquire();
|
|
109
|
+
try {
|
|
110
|
+
for (let i = 0; i < attempts; i++) {
|
|
111
|
+
await conn.watch(...keys);
|
|
112
|
+
const tx = conn.transaction();
|
|
113
|
+
const out = await body(conn, tx);
|
|
114
|
+
if ((await tx.exec()) !== null)
|
|
115
|
+
return out;
|
|
116
|
+
}
|
|
117
|
+
throw new Error(`redis: the watched keys kept changing: gave up after ${attempts} attempts`);
|
|
118
|
+
}
|
|
119
|
+
finally {
|
|
120
|
+
await conn.close();
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/** A call to `script`, to give its keys and arguments and then invoke. */
|
|
124
|
+
script(script) {
|
|
125
|
+
return new Invocation(this.t, script, false);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* The connection string to connect to this database with, for a client
|
|
129
|
+
* other than this SDK.
|
|
130
|
+
*
|
|
131
|
+
* It connects past the database's key prefix: what that client stores under
|
|
132
|
+
* a name is not what this SDK reads under it.
|
|
133
|
+
*/
|
|
134
|
+
get connectionString() {
|
|
135
|
+
return this.t.database().connectionUrl();
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* A Redis database, backed by a connection pool built on first use.
|
|
140
|
+
*
|
|
141
|
+
* Declare it at module scope; its commands are in its command groups:
|
|
142
|
+
*
|
|
143
|
+
* ```ts
|
|
144
|
+
* import { Database } from "@encore.dev/sdk/storage/redis";
|
|
145
|
+
*
|
|
146
|
+
* const SESSIONS = new Database("sessions");
|
|
147
|
+
*
|
|
148
|
+
* await SESSIONS.string.incr("visits:1", 1);
|
|
149
|
+
* const name = await SESSIONS.string.get("name:1").text();
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
152
|
+
export class Database extends DatabaseRef {
|
|
153
|
+
readGroups;
|
|
154
|
+
/**
|
|
155
|
+
* Declares a Redis database.
|
|
156
|
+
*
|
|
157
|
+
* Encore reads the name and the configuration from the declaration, so both
|
|
158
|
+
* must be literals, and the database must be declared at module scope.
|
|
159
|
+
*
|
|
160
|
+
* The name must be unique within the Encore application, and be kebab-case.
|
|
161
|
+
* Once deployed, never change it: a new database would be created under the
|
|
162
|
+
* new name.
|
|
163
|
+
*/
|
|
164
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
165
|
+
constructor(name, cfg) {
|
|
166
|
+
const t = new PoolTarget(name);
|
|
167
|
+
super(t, commandGroups(t));
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* References an existing database by name. To declare a new database, use
|
|
171
|
+
* `new Database(...)` instead.
|
|
172
|
+
*
|
|
173
|
+
* The name must be a string literal, so Encore can resolve it statically. A
|
|
174
|
+
* named database takes no configuration: it is configured where it is
|
|
175
|
+
* declared.
|
|
176
|
+
*/
|
|
177
|
+
static named(name) {
|
|
178
|
+
return new Database(name);
|
|
179
|
+
}
|
|
180
|
+
/** A handle to the database that can read and write. */
|
|
181
|
+
write() {
|
|
182
|
+
return new DatabaseRef(this.t, this.groups);
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* A handle to the database that can only read, which may connect to a read
|
|
186
|
+
* replica. To always read from the primary server, use
|
|
187
|
+
* {@link Database.readPrimary}.
|
|
188
|
+
*/
|
|
189
|
+
read() {
|
|
190
|
+
return new ReadOnlyDatabaseRef(this.t, this.readOnlyGroups(), false);
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* A handle to the database that can only read, and connects to the primary
|
|
194
|
+
* server even if there are read replicas available.
|
|
195
|
+
*/
|
|
196
|
+
readPrimary() {
|
|
197
|
+
return new ReadOnlyDatabaseRef(this.t, this.readOnlyGroups(), true);
|
|
198
|
+
}
|
|
199
|
+
readOnlyGroups() {
|
|
200
|
+
return (this.readGroups ??= readCommandGroups(this.t));
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
|
|
204
|
+
export class Pipeline {
|
|
205
|
+
t;
|
|
206
|
+
queued = [];
|
|
207
|
+
/** @internal */
|
|
208
|
+
constructor(t) {
|
|
209
|
+
this.t = t;
|
|
210
|
+
Object.assign(this, queueGroups(this.queued));
|
|
211
|
+
}
|
|
212
|
+
/** How many commands are queued. */
|
|
213
|
+
get length() {
|
|
214
|
+
return this.queued.length;
|
|
215
|
+
}
|
|
216
|
+
/** Queues a call to `script`. Finish it with {@link QueuedInvocation.queue}. */
|
|
217
|
+
script(script) {
|
|
218
|
+
return new QueuedInvocation(this.queued, script);
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Sends the queued commands and returns their replies, in order. If the
|
|
222
|
+
* server rejected any, fails with a {@link BatchError} holding them all.
|
|
223
|
+
* The pipeline is empty afterwards.
|
|
224
|
+
*/
|
|
225
|
+
async exec() {
|
|
226
|
+
const values = await this.t.runAll(this.queued.splice(0), false);
|
|
227
|
+
return batchReplies(values ?? []);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
|
|
231
|
+
export class Transaction {
|
|
232
|
+
t;
|
|
233
|
+
queued = [];
|
|
234
|
+
/** @internal */
|
|
235
|
+
constructor(t) {
|
|
236
|
+
this.t = t;
|
|
237
|
+
Object.assign(this, queueGroups(this.queued));
|
|
238
|
+
}
|
|
239
|
+
/** How many commands are queued. */
|
|
240
|
+
get length() {
|
|
241
|
+
return this.queued.length;
|
|
242
|
+
}
|
|
243
|
+
/** Queues a call to `script`. Finish it with {@link QueuedInvocation.queue}. */
|
|
244
|
+
script(script) {
|
|
245
|
+
return new QueuedInvocation(this.queued, script);
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Runs the queued commands as one transaction and returns their replies, as
|
|
249
|
+
* {@link Pipeline.exec} does. A command the server rejects when it runs does
|
|
250
|
+
* not stop the others: Redis has no rollback.
|
|
251
|
+
*/
|
|
252
|
+
async exec() {
|
|
253
|
+
const values = await this.t.runAll(this.queued.splice(0), true);
|
|
254
|
+
return batchReplies(values ?? []);
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Abandons the transaction. Nothing was sent, so there is nothing to undo:
|
|
258
|
+
* this empties the queue.
|
|
259
|
+
*/
|
|
260
|
+
discard() {
|
|
261
|
+
this.queued.splice(0);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
|
|
265
|
+
export class WatchedTransaction {
|
|
266
|
+
t;
|
|
267
|
+
queued = [];
|
|
268
|
+
/** @internal */
|
|
269
|
+
constructor(t) {
|
|
270
|
+
this.t = t;
|
|
271
|
+
Object.assign(this, queueGroups(this.queued));
|
|
272
|
+
}
|
|
273
|
+
/** How many commands are queued. */
|
|
274
|
+
get length() {
|
|
275
|
+
return this.queued.length;
|
|
276
|
+
}
|
|
277
|
+
/** Queues a call to `script`. Finish it with {@link QueuedInvocation.queue}. */
|
|
278
|
+
script(script) {
|
|
279
|
+
return new QueuedInvocation(this.queued, script);
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Runs the queued commands as one transaction and returns their replies, as
|
|
283
|
+
* {@link Transaction.exec} does, or `null` if a watched key changed, in
|
|
284
|
+
* which case none of them ran. Either way the connection watches nothing
|
|
285
|
+
* afterwards.
|
|
286
|
+
*/
|
|
287
|
+
async exec() {
|
|
288
|
+
const values = await this.t.runAll(this.queued.splice(0), true);
|
|
289
|
+
return values === null ? null : batchReplies(values);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
|
|
293
|
+
export class Connection {
|
|
294
|
+
impl;
|
|
295
|
+
t;
|
|
296
|
+
/** @internal */
|
|
297
|
+
constructor(impl, pool) {
|
|
298
|
+
this.impl = impl;
|
|
299
|
+
this.t = new ConnTarget(impl, pool);
|
|
300
|
+
Object.assign(this, commandGroups(this.t));
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Watches `keys`: a {@link Connection.transaction} then does nothing if any
|
|
304
|
+
* of them has changed by the time it is run.
|
|
305
|
+
*/
|
|
306
|
+
async watch(...keys) {
|
|
307
|
+
await this.impl.watch(encodeList(keys), activeCtxBytes());
|
|
308
|
+
}
|
|
309
|
+
/** Stops watching every key. */
|
|
310
|
+
async unwatch() {
|
|
311
|
+
await this.impl.unwatch(activeCtxBytes());
|
|
312
|
+
}
|
|
313
|
+
/** A transaction on this connection, which depends on the keys it watches. */
|
|
314
|
+
transaction() {
|
|
315
|
+
return new WatchedTransaction(this.t);
|
|
316
|
+
}
|
|
317
|
+
/** A batch of commands sent in one round trip on this connection. */
|
|
318
|
+
pipeline() {
|
|
319
|
+
return new Pipeline(this.t);
|
|
320
|
+
}
|
|
321
|
+
/** A call to `script` on this connection. */
|
|
322
|
+
script(script) {
|
|
323
|
+
return new Invocation(this.t, script, false);
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Returns the connection to its pool. Keys it still watches are unwatched
|
|
327
|
+
* first.
|
|
328
|
+
*/
|
|
329
|
+
async close() {
|
|
330
|
+
await this.impl.release();
|
|
331
|
+
}
|
|
332
|
+
async [Symbol.asyncDispose]() {
|
|
333
|
+
await this.close();
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
//# sourceMappingURL=database.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"database.js","sourceRoot":"","sources":["../../../storage/redis/database.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,cAAc,EAAC,MAAM,2BAA2B,CAAC;AAGzD,OAAO,EACL,aAAa,EACb,WAAW,EACX,iBAAiB,GAIlB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAC,UAAU,EAAE,KAAK,EAAC,MAAM,SAAS,CAAC;AAC1C,OAAO,EAAC,UAAU,EAAE,gBAAgB,EAAS,MAAM,UAAU,CAAC;AAC9D,OAAO,EAAC,UAAU,EAAE,UAAU,EAAc,MAAM,UAAU,CAAC;AAC7D,OAAO,EAAC,UAAU,EAAuB,MAAM,QAAQ,CAAC;AAwCxD,6EAA6E;AAC7E,SAAS,YAAY,CAAC,MAAe;IACnC,MAAM,OAAO,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,IAAI,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;IAC9C,MAAM,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;IAC1C,IAAI,MAAM;QAAE,MAAM,IAAI,UAAU,CAAC,OAAO,EAAE,MAAM,CAAC,KAAM,CAAC,CAAC;IACzD,OAAO,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AAC;IAED,6CAA6C;IAC7C,MAAM,CAAC,MAAc;QACnB,OAAO,IAAI,UAAU,CAAC,IAAI,CAAC,CAAC,EAAE,MAAM,EAAE,KAAK,CAAC,CAAC;IAC/C,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,KAAK;QACT,MAAM,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC;IAC5B,CAAC;IAED,KAAK,CAAC,CAAC,MAAM,CAAC,YAAY,CAAC;QACzB,MAAM,IAAI,CAAC,KAAK,EAAE,CAAC;IACrB,CAAC;CACF"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Redis databases. A database is declared at module scope; its connection pool
|
|
3
|
+
* is built on first use and reused thereafter.
|
|
4
|
+
*
|
|
5
|
+
* ```ts
|
|
6
|
+
* import { Database } from "@encore.dev/sdk/storage/redis";
|
|
7
|
+
*
|
|
8
|
+
* const SESSIONS = new Database("sessions");
|
|
9
|
+
*
|
|
10
|
+
* export async function touch(user: string): Promise<string | null> {
|
|
11
|
+
* await SESSIONS.string.incr(`visits:${user}`, 1);
|
|
12
|
+
* return SESSIONS.string.get(`name:${user}`).text();
|
|
13
|
+
* }
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* A database is a resource of its own: there is no cluster to put it in, and
|
|
17
|
+
* no keyspaces to declare in it. Its keys are whatever the code passes.
|
|
18
|
+
*
|
|
19
|
+
* ## Commands
|
|
20
|
+
*
|
|
21
|
+
* Commands are grouped by the data type they work on, each group a property of
|
|
22
|
+
* the database: `string`, `hash`, `list`, `set`, `sortedSet`, `stream`, `geo`,
|
|
23
|
+
* `bitmap`, `pubsub`, and `generic` for the ones that work on any key. Within a
|
|
24
|
+
* group a command is named after the Redis command it runs — `get`, `hset`,
|
|
25
|
+
* `zadd`, `xadd` — with words of its own for its variants: `setEx`,
|
|
26
|
+
* `zrangeWithscores`.
|
|
27
|
+
*
|
|
28
|
+
* A command whose reply has one shape returns it: `strlen` a number,
|
|
29
|
+
* `sismember` a boolean. One whose reply is the caller's to read returns a
|
|
30
|
+
* {@link ReplyPromise}, read as what it holds:
|
|
31
|
+
*
|
|
32
|
+
* ```ts
|
|
33
|
+
* const name = await SESSIONS.string.get("name:1").text();
|
|
34
|
+
* const visits = await SESSIONS.string.get("visits:1").number();
|
|
35
|
+
* const fields = await SESSIONS.hash.hgetall("user:1").textMap();
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* A missing key reads as `null`.
|
|
39
|
+
*
|
|
40
|
+
* A command is called in the same expression that names its group. That is
|
|
41
|
+
* what lets Encore grant each service exactly the access it uses: the command
|
|
42
|
+
* is visible at the call, so Encore's parser can read it. A group that is
|
|
43
|
+
* stored or passed on instead says nothing about what it will be used for, and
|
|
44
|
+
* is reported as an error — to do that, start from a handle.
|
|
45
|
+
*
|
|
46
|
+
* ## Handles
|
|
47
|
+
*
|
|
48
|
+
* A database cannot be passed around as a value. To hand it to other code,
|
|
49
|
+
* make a handle with the access that code needs — {@link Database.write},
|
|
50
|
+
* {@link Database.read} or {@link Database.readPrimary} — which the service is
|
|
51
|
+
* then granted in full:
|
|
52
|
+
*
|
|
53
|
+
* ```ts
|
|
54
|
+
* // Library code takes a handle, and can only do what the handle allows.
|
|
55
|
+
* function nameOf(db: ReadOnlyDatabaseRef, user: string) {
|
|
56
|
+
* return db.string.get(`name:${user}`).text();
|
|
57
|
+
* }
|
|
58
|
+
*
|
|
59
|
+
* export function greet(user: string) {
|
|
60
|
+
* return nameOf(SESSIONS.read(), user);
|
|
61
|
+
* }
|
|
62
|
+
* ```
|
|
63
|
+
*
|
|
64
|
+
* ## Keys
|
|
65
|
+
*
|
|
66
|
+
* Every key is stored under a prefix of the database's own, which is what
|
|
67
|
+
* keeps databases sharing a server apart. It is applied to every key a command
|
|
68
|
+
* takes and removed from every key a command returns, so code never sees it.
|
|
69
|
+
* Two things follow:
|
|
70
|
+
*
|
|
71
|
+
* - a {@link Script}'s `KEYS` are prefixed, but nothing else in it is: a key
|
|
72
|
+
* it names in its source or its `ARGV` is reached unprefixed, outside the
|
|
73
|
+
* database's isolation, so a script must take its keys through `KEYS`;
|
|
74
|
+
* - {@link DatabaseRef.connectionString} connects past the prefix, so a client
|
|
75
|
+
* using it does not see the keys this SDK writes under the same names.
|
|
76
|
+
*
|
|
77
|
+
* @module
|
|
78
|
+
*/
|
|
79
|
+
export { Database, DatabaseRef, ReadOnlyDatabaseRef, Pipeline, Transaction, WatchedTransaction, Connection, } from './database.js';
|
|
80
|
+
export type { DatabaseConfig, EvictionPolicy } from './database.js';
|
|
81
|
+
export type { BitmapCommands, CommandGroups, Commands, GenericCommands, GeoCommands, HashCommands, ListCommands, PubsubCommands, Queue, QueueGroups, ReadCommandGroups, ReadCommands, ScoreBound, SetCommands, SortedSetCommands, StreamCommands, StringCommands, } from './commands.js';
|
|
82
|
+
export type { Direction, Expiry, RadiusOptions, ScanOptions, SetExpiry, SetOptions, StreamMaxlen, Unit, } from './options.js';
|
|
83
|
+
export { BatchError, Reply, ReplyPromise } from './reply.js';
|
|
84
|
+
export type { FieldValue, GeoPosition, GeoRadiusResult, RedisValue, ScoredMember, Stream, StreamAutoClaim, StreamEntry, StreamInfo, StreamPending, } from './reply.js';
|
|
85
|
+
export { Script, Invocation, QueuedInvocation } from './script.js';
|
|
86
|
+
export { Message, Subscription } from './pubsub.js';
|
|
87
|
+
export { ServerError } from './wire.js';
|
|
88
|
+
export type { Pairs, RedisArg } from './wire.js';
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Redis databases. A database is declared at module scope; its connection pool
|
|
3
|
+
* is built on first use and reused thereafter.
|
|
4
|
+
*
|
|
5
|
+
* ```ts
|
|
6
|
+
* import { Database } from "@encore.dev/sdk/storage/redis";
|
|
7
|
+
*
|
|
8
|
+
* const SESSIONS = new Database("sessions");
|
|
9
|
+
*
|
|
10
|
+
* export async function touch(user: string): Promise<string | null> {
|
|
11
|
+
* await SESSIONS.string.incr(`visits:${user}`, 1);
|
|
12
|
+
* return SESSIONS.string.get(`name:${user}`).text();
|
|
13
|
+
* }
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* A database is a resource of its own: there is no cluster to put it in, and
|
|
17
|
+
* no keyspaces to declare in it. Its keys are whatever the code passes.
|
|
18
|
+
*
|
|
19
|
+
* ## Commands
|
|
20
|
+
*
|
|
21
|
+
* Commands are grouped by the data type they work on, each group a property of
|
|
22
|
+
* the database: `string`, `hash`, `list`, `set`, `sortedSet`, `stream`, `geo`,
|
|
23
|
+
* `bitmap`, `pubsub`, and `generic` for the ones that work on any key. Within a
|
|
24
|
+
* group a command is named after the Redis command it runs — `get`, `hset`,
|
|
25
|
+
* `zadd`, `xadd` — with words of its own for its variants: `setEx`,
|
|
26
|
+
* `zrangeWithscores`.
|
|
27
|
+
*
|
|
28
|
+
* A command whose reply has one shape returns it: `strlen` a number,
|
|
29
|
+
* `sismember` a boolean. One whose reply is the caller's to read returns a
|
|
30
|
+
* {@link ReplyPromise}, read as what it holds:
|
|
31
|
+
*
|
|
32
|
+
* ```ts
|
|
33
|
+
* const name = await SESSIONS.string.get("name:1").text();
|
|
34
|
+
* const visits = await SESSIONS.string.get("visits:1").number();
|
|
35
|
+
* const fields = await SESSIONS.hash.hgetall("user:1").textMap();
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* A missing key reads as `null`.
|
|
39
|
+
*
|
|
40
|
+
* A command is called in the same expression that names its group. That is
|
|
41
|
+
* what lets Encore grant each service exactly the access it uses: the command
|
|
42
|
+
* is visible at the call, so Encore's parser can read it. A group that is
|
|
43
|
+
* stored or passed on instead says nothing about what it will be used for, and
|
|
44
|
+
* is reported as an error — to do that, start from a handle.
|
|
45
|
+
*
|
|
46
|
+
* ## Handles
|
|
47
|
+
*
|
|
48
|
+
* A database cannot be passed around as a value. To hand it to other code,
|
|
49
|
+
* make a handle with the access that code needs — {@link Database.write},
|
|
50
|
+
* {@link Database.read} or {@link Database.readPrimary} — which the service is
|
|
51
|
+
* then granted in full:
|
|
52
|
+
*
|
|
53
|
+
* ```ts
|
|
54
|
+
* // Library code takes a handle, and can only do what the handle allows.
|
|
55
|
+
* function nameOf(db: ReadOnlyDatabaseRef, user: string) {
|
|
56
|
+
* return db.string.get(`name:${user}`).text();
|
|
57
|
+
* }
|
|
58
|
+
*
|
|
59
|
+
* export function greet(user: string) {
|
|
60
|
+
* return nameOf(SESSIONS.read(), user);
|
|
61
|
+
* }
|
|
62
|
+
* ```
|
|
63
|
+
*
|
|
64
|
+
* ## Keys
|
|
65
|
+
*
|
|
66
|
+
* Every key is stored under a prefix of the database's own, which is what
|
|
67
|
+
* keeps databases sharing a server apart. It is applied to every key a command
|
|
68
|
+
* takes and removed from every key a command returns, so code never sees it.
|
|
69
|
+
* Two things follow:
|
|
70
|
+
*
|
|
71
|
+
* - a {@link Script}'s `KEYS` are prefixed, but nothing else in it is: a key
|
|
72
|
+
* it names in its source or its `ARGV` is reached unprefixed, outside the
|
|
73
|
+
* database's isolation, so a script must take its keys through `KEYS`;
|
|
74
|
+
* - {@link DatabaseRef.connectionString} connects past the prefix, so a client
|
|
75
|
+
* using it does not see the keys this SDK writes under the same names.
|
|
76
|
+
*
|
|
77
|
+
* @module
|
|
78
|
+
*/
|
|
79
|
+
export { Database, DatabaseRef, ReadOnlyDatabaseRef, Pipeline, Transaction, WatchedTransaction, Connection, } from './database.js';
|
|
80
|
+
export { BatchError, Reply, ReplyPromise } from './reply.js';
|
|
81
|
+
export { Script, Invocation, QueuedInvocation } from './script.js';
|
|
82
|
+
export { Message, Subscription } from './pubsub.js';
|
|
83
|
+
export { ServerError } from './wire.js';
|
|
84
|
+
//# sourceMappingURL=mod.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mod.js","sourceRoot":"","sources":["../../../storage/redis/mod.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6EG;AAEH,OAAO,EACL,QAAQ,EACR,WAAW,EACX,mBAAmB,EACnB,QAAQ,EACR,WAAW,EACX,kBAAkB,EAClB,UAAU,GACX,MAAM,YAAY,CAAC;AA+BpB,OAAO,EAAC,UAAU,EAAE,KAAK,EAAE,YAAY,EAAC,MAAM,SAAS,CAAC;AAaxD,OAAO,EAAC,MAAM,EAAE,UAAU,EAAE,gBAAgB,EAAC,MAAM,UAAU,CAAC;AAC9D,OAAO,EAAC,OAAO,EAAE,YAAY,EAAC,MAAM,UAAU,CAAC;AAC/C,OAAO,EAAC,WAAW,EAAC,MAAM,QAAQ,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import * as runtime from '../../internal/runtime/mod.js';
|
|
2
|
+
/**
|
|
3
|
+
* The native handle of the database `name`: where the SDK meets the runtime,
|
|
4
|
+
* and what its tests stand in for.
|
|
5
|
+
*
|
|
6
|
+
* @internal
|
|
7
|
+
*/
|
|
8
|
+
export declare function openDatabase(name: string): runtime.RedisDatabase;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import * as runtime from '../../internal/runtime/mod.js';
|
|
2
|
+
/**
|
|
3
|
+
* The native handle of the database `name`: where the SDK meets the runtime,
|
|
4
|
+
* and what its tests stand in for.
|
|
5
|
+
*
|
|
6
|
+
* @internal
|
|
7
|
+
*/
|
|
8
|
+
export function openDatabase(name) {
|
|
9
|
+
return runtime.RT.redisDatabase(name);
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=native.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"native.js","sourceRoot":"","sources":["../../../storage/redis/native.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,OAAO,MAAM,4BAA4B,CAAC;AAEtD;;;;;GAKG;AACH,MAAM,UAAU,YAAY,CAAC,IAAY;IACvC,OAAO,OAAO,CAAC,EAAE,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC;AACxC,CAAC"}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import type { RedisArg } from './wire.js';
|
|
2
|
+
/**
|
|
3
|
+
* When a key `setOptions` sets expires: in seconds (`ex`) or milliseconds
|
|
4
|
+
* (`px`), at a Unix time in seconds (`exAt`) or milliseconds (`pxAt`), or
|
|
5
|
+
* keeping the expiry it has.
|
|
6
|
+
*/
|
|
7
|
+
export type SetExpiry = {
|
|
8
|
+
ex: number;
|
|
9
|
+
} | {
|
|
10
|
+
px: number;
|
|
11
|
+
} | {
|
|
12
|
+
exAt: number;
|
|
13
|
+
} | {
|
|
14
|
+
pxAt: number;
|
|
15
|
+
} | {
|
|
16
|
+
keepTtl: true;
|
|
17
|
+
};
|
|
18
|
+
/** How `setOptions` sets a key. */
|
|
19
|
+
export interface SetOptions {
|
|
20
|
+
/**
|
|
21
|
+
* When to set the key: only if it does not exist (`"nx"`), or only if it
|
|
22
|
+
* does (`"xx"`).
|
|
23
|
+
*/
|
|
24
|
+
condition?: 'nx' | 'xx';
|
|
25
|
+
/** Reply with the key's old value, or nil if it had none. */
|
|
26
|
+
get?: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* When the key expires. Without one it never does, and loses any expiry it
|
|
29
|
+
* had.
|
|
30
|
+
*/
|
|
31
|
+
expiry?: SetExpiry;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* How `getEx` changes a key's expiry: as {@link SetExpiry} says, or removing
|
|
35
|
+
* it.
|
|
36
|
+
*/
|
|
37
|
+
export type Expiry = {
|
|
38
|
+
ex: number;
|
|
39
|
+
} | {
|
|
40
|
+
px: number;
|
|
41
|
+
} | {
|
|
42
|
+
exAt: number;
|
|
43
|
+
} | {
|
|
44
|
+
pxAt: number;
|
|
45
|
+
} | {
|
|
46
|
+
persist: true;
|
|
47
|
+
};
|
|
48
|
+
/** An end of a list: its head, or its tail. */
|
|
49
|
+
export type Direction = 'left' | 'right';
|
|
50
|
+
/** A unit of distance: meters, kilometers, miles or feet. */
|
|
51
|
+
export type Unit = 'm' | 'km' | 'mi' | 'ft';
|
|
52
|
+
/** What a radius search returns of each member it finds, and how many. */
|
|
53
|
+
export interface RadiusOptions {
|
|
54
|
+
/** Return each member's position. */
|
|
55
|
+
withCoord?: boolean;
|
|
56
|
+
/** Return each member's distance from the center. */
|
|
57
|
+
withDist?: boolean;
|
|
58
|
+
/** Return each member's geohash, as an integer. */
|
|
59
|
+
withHash?: boolean;
|
|
60
|
+
/** The most members to return. */
|
|
61
|
+
count?: number;
|
|
62
|
+
/** The nearest first (`"asc"`), or the farthest. In no order without. */
|
|
63
|
+
order?: 'asc' | 'desc';
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* The most entries a stream is trimmed to: exactly `maxlen`, or with `approx`
|
|
67
|
+
* about that many, never fewer, which is cheaper.
|
|
68
|
+
*/
|
|
69
|
+
export interface StreamMaxlen {
|
|
70
|
+
maxlen: number;
|
|
71
|
+
approx?: boolean;
|
|
72
|
+
}
|
|
73
|
+
/** What a scan looks through, and how much at a time. */
|
|
74
|
+
export interface ScanOptions {
|
|
75
|
+
/** Only what matches a glob-style pattern: `user:*`. */
|
|
76
|
+
pattern?: string;
|
|
77
|
+
/** A hint for how many elements to look at per round trip. */
|
|
78
|
+
count?: number;
|
|
79
|
+
}
|
|
80
|
+
/** @internal */
|
|
81
|
+
export declare function expiryArgs(expiry: SetExpiry | Expiry): RedisArg[];
|
|
82
|
+
/** @internal */
|
|
83
|
+
export declare function setArgs(options: SetOptions): RedisArg[];
|
|
84
|
+
/** @internal */
|
|
85
|
+
export declare function directionArg(d: Direction): string;
|
|
86
|
+
/** @internal */
|
|
87
|
+
export declare function radiusArgs(options?: RadiusOptions): RedisArg[];
|
|
88
|
+
/** @internal */
|
|
89
|
+
export declare function maxlenArgs(m: StreamMaxlen): RedisArg[];
|