hey folks, can creating a DO stub fail with Durable Object overloaded?
hey folks, can creating a DO stub fail with Durable Object overloaded?
durable-utils modulerunAll()) before accessing the storage so that they have the latest schema.CREATE TABLE IF NOT EXISTS vs CREATE TABLE), and are small enough (not data migrations) then you probably don't need to do eager migrations and wake up all your DOs.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?
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>runAll()CREATE TABLE IF NOT EXISTSCREATE TABLE