You cannot pass anything to the DO constructor, it's called by the runtime, not your code.
You cannot pass anything to the DO constructor, it's called by the runtime, not your code.
env to change you make a new deployment, so your DO will restart and the constructor will rerun.
durable-utils library.wrangler config that includes the new class?
env.MY_DURABLE_OBJECT.idFromName("foo"), once inside of a DO instance? At one point I know the answer used to be "no" but I'm wondering if this has changed.To solve this problem, we take advantage of our global network. Every time SQLite commits a transaction, SRS will immediately forward the change log to five "follower" machines across our network. Once at least three of these followers respond that they have received the change, SRS informs the application that the write is confirmed. (As discussed earlier, the write confirmation opens the Durable Object's "output gate", unblocking network communications to the rest of the world.)
When a follower receives a change, it temporarily stores it in a buffer on local disk, and then awaits further instructions. Later on, once SRS has successfully uploaded the change to object storage as part of a batch, it informs each follower that the change has been persisted. At that point, the follower can simply delete the change from its buffer.
[...]
Each of a database's five followers is located in a different physical data center. Cloudflare's network consists of hundreds of data centers around the world, which means it is always easy for us to find four other data centers nearby any Durable Object (in addition to the one it is running in). In order for a confirmed write to be lost, then, at least four different machines in at least three different physical buildings would have to fail simultaneously (three of the five followers, plus the Durable Object's host machine). Of course, anything can happen, but this is exceedingly unlikely.
import { DurableObject as DurableObjectPrimitive } from "cloudflare:workers";
const iife = <T>(fn: () => T): T => fn();
export function DurableObject<TEnv>(accessor: keyof TEnv) {
return class DurableObject extends DurableObjectPrimitive<TEnv> {
/**
* Retrieves a durable object stub for a given id.
*
* @template T - The type of the DurableObject.
* @param {TEnv} env - The env object containing the durable object's namespace.
* @param {DurableObjectId | string} [id] - The id of the DurableObject. If not provided, `newUniqueId()` will be used.
* @returns {DurableObjectStub<T>} The DurableObjectStub for the given id.
*/
static getStub<T extends DurableObjectPrimitive>(
this: { new (...args: any[]): T },
env: TEnv,
id?: DurableObjectId | string
): DurableObjectStub<T> {
const namespace = env[accessor] as DurableObjectNamespace<T>;
const stubId = iife(() => {
if (!id) return namespace.newUniqueId();
if (typeof id === "string") {
// Check if the provided id is a string representation of the
// 256-bit Durable Object ID
if (id.match(/^[0-9a-f]{64}$/)) return namespace.idFromString(id);
else return namespace.idFromName(id);
}
return id;
});
const stub = namespace.get(stubId);
return stub;
}
};
}interface Env {
FOO_OBJECT: DurableObjectNamespace<FooObject>;
BAR_OBJECT: DurableObjectNamespace<BarObject>;
}
export class FooObject extends DurableObject<Env>("FOO_OBJECT") {
getFoo() {
return "foo!";
}
}
export class BarObject extends DurableObject<Env>("BAR_OBJECT") {
getBar() {
return 41;
}
}
// example usage:
const fooStub = FooObject.getStub(env, "123");
const barStub = BarObject.getStub(env, "123");
fooStub.getFoo(); // () -> Promise<string>
barStub.getBar(); // () -> Promise<number>