Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A getMiniflareDurableObjectStorage that doesn't start the DO #300

Closed
spigaz opened this issue Jun 29, 2022 · 2 comments
Closed

A getMiniflareDurableObjectStorage that doesn't start the DO #300

spigaz opened this issue Jun 29, 2022 · 2 comments
Labels
enhancement New feature or request
Milestone

Comments

@spigaz
Copy link

spigaz commented Jun 29, 2022

When you use getMiniflareDurableObjectStorage() it starts the DO before you can access the storage.

That implies that the DO initialization happens before you are able to setup storage to test its initialization.

So @mrbbot suggested this implementation that works great, but a direct getMiniflareDurableObjectDirectStorage(); makes all the sense for these kind of uses cases.

import { Miniflare } from "miniflare";
import { DurableObjectStorage } from "@miniflare/durable-objects";

const durableObjectsPersist = false;
const mf = new Miniflare({
  modules: true,
  durableObjects: { TEST_OBJECT: "TestObject" },
  durableObjectsPersist,
  script: `
  export class TestObject {
    constructor(state) {
      this.storage = state.storage;
    }

    async fetch(request) {
      return new Response(await this.storage.get("key"));
    }
  }
  `,
});

const objectName = "TEST_OBJECT";
const ns = await mf.getDurableObjectNamespace(objectName);
const id = ns.idFromName("test");
const pluginStorageFactory = mf.getPluginStorage("DurableObjectsPlugin");
const storageKey = `${objectName}:${id.toString()}`;
const innerStorage = await pluginStorageFactory.storage(
  storageKey,
  durableObjectsPersist
);
const storage = new DurableObjectStorage(innerStorage);
await storage.put("key", "value");

const stub = ns.get(id);
const doRes = await stub.fetch("http://localhost:8787");
console.log(await doRes.text()); // "value"

In my use case I ended up using

  // const storage: DurableObjectStorage = await getMiniflareDurableObjectStorage(id);

  // HACK
  const mf = getMiniflare();

  const pluginStorageFactory = mf.getPluginStorage('DurableObjectsPlugin');
  const storageKey = `${'OBJECT_NAME'}:${id.toString()}`;
  const innerStorage = await pluginStorageFactory.storage(storageKey, false);
  const storage = new DurableObjectStorage(innerStorage);

  await storage.put('key', 'value');
@mrbbot mrbbot added the enhancement New feature or request label Jul 2, 2022
@mrbbot mrbbot added this to the 2.6.0 milestone Jul 2, 2022
@mrbbot mrbbot modified the milestones: 2.6.0, 2.7.0 Jul 15, 2022
@mrbbot mrbbot modified the milestones: 2.7.0, 2.8.0 Aug 19, 2022
@mrbbot mrbbot modified the milestones: 2.8.0, 2.9.0 Sep 7, 2022
@mrbbot mrbbot closed this as completed in 6299d1d Sep 11, 2022
@mrbbot
Copy link
Contributor

mrbbot commented Sep 17, 2022

Hey! 👋 Miniflare 2.9.0 has just been released, which implements this change. You can find the full changelog here.

@spigaz
Copy link
Author

spigaz commented Sep 18, 2022

Hey @mrbbot! Thank you!

I was on it... I just removed the hack and its working like a charm!

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants