Cloudflare
KV
Use Cloudflare KV namespaces in your Vela application.
The KVModule provides a managed KVService for interacting with Cloudflare KV namespaces.
Setup
Import the module and configure the binding name:
import { Module } from '@velajs/vela';
import { KVModule } from '@velajs/cloudflare';
@Module({
imports: [KVModule.forRoot({ binding: 'MY_KV' })],
})
class AppModule {}Add the KV namespace to wrangler.toml:
[[kv_namespaces]]
binding = "MY_KV"
id = "abc123"Using KVService
Inject KVService into any provider or controller:
import { Controller, Get, Post, Body, Param } from '@velajs/vela';
import { KVService } from '@velajs/cloudflare';
@Controller('cache')
class CacheController {
constructor(private readonly kv: KVService) {}
@Get(':key')
async get(@Param('key') key: string) {
const value = await this.kv.get(key);
return { key, value };
}
@Post(':key')
async set(@Param('key') key: string, @Body() body: { value: string }) {
await this.kv.put(key, body.value);
return { success: true };
}
}Methods
| Method | Description |
|---|---|
get(key, options?) | Read a value by key. Options control the return type (text, json, arrayBuffer, stream). |
getWithMetadata(key, options?) | Read a value and its associated metadata. |
put(key, value, options?) | Write a value. Options support expiration, expirationTtl, and metadata. |
delete(key) | Delete a key. |
list(options?) | List keys. Options support prefix, limit, and cursor for pagination. |
Environment
Access Cloudflare Worker environment bindings directly.
D1
Use Cloudflare D1 databases in your Vela application.
Edit on GitHub
Last updated on