Durable Object Namespace
A Durable Object namespace is a set of Durable Objects that are backed by the same Durable Object class. There is only one Durable Object namespace per class. A Durable Object namespace can contain any number of Durable Objects.
The DurableObjectNamespace
interface is used to obtain a reference to new or existing Durable Objects. The interface is accessible from the fetch handler on a Cloudflare Worker via the env
parameter, which is the standard interface when referencing bindings declared in wrangler.toml
.
This interface defines several methods that can be used to create an ID for a Durable Object. Note that creating an ID for a Durable Object does not create the Durable Object. The Durable Object is created lazily after calling DurableObjectNamespace::get
to create a DurableObjectStub
from a DurableObjectId
. This ensures that objects are not constructed until they are actually accessed.
idFromName
creates a unique DurableObjectId
which refers to an individual instance of the Durable Object class. Named Durable Objects are the most common method of referring to Durable Objects.
- A required string to be used to generate a
DurableObjectId
corresponding to the name of a Durable Object.
- A
DurableObjectId
referring to an instance of a Durable Object class.
newUniqueId
creates a randomly generated and unique DurableObjectId
which refers to an individual instance of the Durable Object class. IDs created using newUniqueId
, will need to be stored as a string in order to refer to the same Durable Object again in the future. For example, the ID can be stored in Workers KV, another Durable Object, or in a cookie in the user’s browser.
- An optional object with the key
jurisdiction
and value of a jurisdiction string.
- A
DurableObjectId
referring to an instance of the Durable Object class.
idFromString
creates a DurableObjectId
from a previously generated ID that has been converted to a string. This method throws an exception if the ID is invalid, for example, if the ID was not created from the same DurableObjectNamespace
.
- A required string corresponding to a
DurableObjectId
previously generated either bynewUniqueId
oridFromName
.
- A
DurableObjectId
referring to an instance of a Durable Object class.
get
obtains a DurableObjectStub
from a DurableObjectId
which can be used to invoke methods on a Durable Object.
This method returns the stub immediately, often before a connection has been established to the Durable Object. This allows requests to be sent to the instance right away, without waiting for a network round trip.
- A required
DurableObjectId
- An optional object with the key
locationHint
and value of a locationHint string.
- A
DurableObjectStub
referring to an instance of a Durable Object class.
jurisdiction
creates a subnamespace from a namespace where all Durable Object IDs and references created from that subnamespace will be restricted to the specified jurisdiction.
- A required jurisdiction string.
- A
DurableObjectNamespace
scoped to a particular geographic jurisdiction.