VelaVela
Cloudflare

Durable Objects

Use Cloudflare Durable Objects for stateful coordination in your Vela application.

The DurableObjectModule provides a managed DurableObjectService for interacting with Durable Object namespaces.

Setup

Import the module and configure the binding name:

import { Module } from '@velajs/vela';
import { DurableObjectModule } from '@velajs/cloudflare';

@Module({
  imports: [DurableObjectModule.forRoot({ binding: 'MY_DO' })],
})
class AppModule {}

Add the Durable Object binding to wrangler.toml:

[durable_objects]
bindings = [
  { name = "MY_DO", class_name = "MyDurableObject" }
]

[[migrations]]
tag = "v1"
new_classes = ["MyDurableObject"]

Using DurableObjectService

Inject DurableObjectService to obtain stubs and communicate with Durable Object instances:

import { Controller, Get, Param } from '@velajs/vela';
import { DurableObjectService } from '@velajs/cloudflare';

@Controller('rooms')
class RoomsController {
  constructor(private readonly durableObject: DurableObjectService) {}

  @Get(':name')
  async getRoom(@Param('name') name: string) {
    const id = this.durableObject.idFromName(name);
    const stub = this.durableObject.get(id);
    const response = await stub.fetch('https://dummy/state');
    return response.json();
  }
}

Methods

MethodDescription
newUniqueId()Generate a new unique Durable Object ID.
idFromName(name)Derive a deterministic ID from a string name.
idFromString(hexId)Restore an ID from its hex string representation.
get(id)Get a stub for the Durable Object instance associated with the given ID.
jurisdiction(jurisdiction)Restrict the Durable Object to a specific jurisdiction (e.g., "eu").
Edit on GitHub

Last updated on

On this page