Skip to content

Commit 0309f87

Browse files
committed
feat: add driver context access in createVars function (#2841)
<!-- Please make sure there is an issue that this PR is correlated to. --> ## Changes <!-- If there are frontend changes, please include screenshots. -->
1 parent 7dff920 commit 0309f87

File tree

4 files changed

+235
-0
lines changed

4 files changed

+235
-0
lines changed

site/public/llms-full.txt

Lines changed: 96 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

site/src/content/docs/actors/ephemeral-variables.mdx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,35 @@ Use `state` when:
118118

119119
- The data must be preserved across actor sleeps, restarts, updates, or crashes
120120
- The information is essential to the actor's core functionality and business logic
121+
122+
## Advanced
123+
124+
### Accessing Driver Context
125+
126+
The `createVars` function receives a second parameter that provides access to driver-specific context. This allows you to access driver-specific functionality.
127+
128+
For example, the Redis driver exposes access to the Redis instance:
129+
130+
```typescript
131+
import { actor, ActorInitContext } from "@rivetkit/actor";
132+
import { DriverContext } from "@rivetkit/redis";
133+
134+
const myActor = actor({
135+
state: { count: 0 },
136+
137+
// The second parameter provides driver-specific context
138+
createVars: (ctx: ActorInitContext, driver: DriverContext) => ({ driver }),
139+
140+
actions: {
141+
accessDriverFeatures: (c) => {
142+
// Access Redis
143+
const redis = c.vars.driver.redis;
144+
const keyPrefix = c.vars.driver.keyPrefix;
145+
// ...etc...
146+
}
147+
}
148+
});
149+
```
150+
151+
Consult the documentation for each driver to learn more about their respective `DriverContext` types.
152+

site/src/content/docs/drivers/redis.mdx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,42 @@ Example using Redis driver with Hono web framework.
129129
Basic Redis driver setup and configuration example.
130130
</Card>
131131
</CardGroup>
132+
133+
## Advanced
134+
135+
### Driver Context
136+
137+
The Redis driver provides access to the underlying Redis connection through the driver context in `createVars`.
138+
139+
```typescript
140+
import { actor, ActorInitContext } from "@rivetkit/actor";
141+
import type { DriverContext } from "@rivetkit/redis";
142+
143+
const myActor = actor({
144+
state: { count: 0 },
145+
146+
// Save the Redis driver context
147+
createVars: (ctx: ActorInitContext, driver: DriverContext) => ({ redis: driver.redis }),
148+
149+
actions: {
150+
// Example: Access Redis directly (not recommended in practice)
151+
getRedisValue: async (c, key: string) => {
152+
// Use the Redis client from driver context
153+
return await c.vars.redis.get(key);
154+
},
155+
}
156+
});
157+
```
158+
159+
The Redis driver context type is exported as `DriverContext` from `@rivetkit/redis`:
160+
161+
```typescript
162+
interface DriverContext {
163+
redis: ioredis.Redis;
164+
keyPrefix: string; // Key prefix that all RivetKit data is stored under
165+
}
166+
```
167+
168+
<Warning>
169+
While you have access to the Redis client, be cautious when directly modifying keys under the `keyPrefix`, as this may interfere with RivetKit's internal operations and potentially break actor functionality.
170+
</Warning>

site/src/content/docs/hosting-providers/cloudflare-workers.mdx

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,71 @@ Example using Cloudflare Workers with Hono web framework.
141141
Basic Cloudflare Workers setup and configuration example.
142142
</Card>
143143
</CardGroup>
144+
145+
## Advanced
146+
147+
### Accessing Environment Bindings
148+
149+
You can access Cloudflare Workers environment bindings directly using the importable `env`:
150+
151+
```typescript
152+
import { env } from "cloudflare:workers";
153+
154+
// Access environment variables and secrets in top-level scope
155+
const API_KEY = env.API_KEY;
156+
const LOG_LEVEL = env.LOG_LEVEL || "info";
157+
158+
// Use bindings in your actor
159+
const myActor = actor({
160+
state: { count: 0 },
161+
162+
actions: {
163+
// Access KV, D1, or other bindings during request handling
164+
getFromKV: async (c, key: string) => {
165+
// Access additional KV namespaces defined in wrangler.json
166+
if (env.MY_CACHE_KV) {
167+
return await env.MY_CACHE_KV.get(key);
168+
}
169+
}
170+
}
171+
});
172+
```
173+
174+
### Driver Context
175+
176+
The Cloudflare Workers driver provides access to the Durable Object state and environment through the driver context in `createVars`.
177+
178+
```typescript
179+
import { actor, ActorInitContext } from "@rivetkit/actor";
180+
import type { DriverContext } from "@rivetkit/cloudflare-workers";
181+
182+
const myActor = actor({
183+
state: { count: 0 },
184+
185+
// Save the Cloudflare driver context
186+
createVars: (ctx: ActorInitContext, driver: DriverContext) => ({
187+
state: driver.state,
188+
}),
189+
190+
actions: {
191+
// Example: Access Durable Object info (not recommended in practice)
192+
kvGet: (c, key: string) => {
193+
const doState = c.vars.state;
194+
return await doState.storage.get(key)
195+
},
196+
}
197+
});
198+
```
199+
200+
The Cloudflare Workers driver context type is exported as `DriverContext` from `@rivetkit/cloudflare-workers`:
201+
202+
```typescript
203+
interface DriverContext {
204+
state: DurableObjectState;
205+
}
206+
```
207+
208+
<Warning>
209+
While you have access to the Durable Object state, be cautious when directly modifying KV storage or alarms, as this may interfere with RivetKit's internal operations and potentially break actor functionality.
210+
</Warning>
211+

0 commit comments

Comments
 (0)