DO + pages

You need a second Worker, also running dev
12 Replies
Avi
Avi2y ago
You mean like multiple wrangler dev calls?
Hello, I’m Allie!
Yup
Avi
Avi2y ago
i see! so my repo directory structure should be like:
package.json
src/
public/
durable_objects/
—— package.json
—— wrangler.toml
—— src/
package.json
src/
public/
durable_objects/
—— package.json
—— wrangler.toml
—— src/
essentially a module (with package.json + wrangler.toml inside a subdirectory of my Pages repo) ah, figured it out!
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Avi
Avi2y ago
yep! i was able to get a worker in the pages deploy to bind to a durable object, both locally and remotely
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Avi
Avi2y ago
haha, that's what i did too, using the discord search function scoped to the #durable-objects channel 🙂 I think this is demonstrable proof that DO and DO+Pages needs better documentation...
zsmooth
zsmooth2y ago
@ar0man do you happen to have any proof of concept code online I can look at? Does my DO really need a worker associated with it? And when Pages uses the DO, does it go through the worker or interact with the DO directly?
Hello, I’m Allie!
Yes, and no, not necessarily Basically, the Worker is only needed to publish the DO class. It doesn't need any of the normal Worker logic. It could be as simple as
export class SomeDOClass {
// ...
}
export class SomeDOClass {
// ...
}
Notice the lack of export default This also means that you can bind directly to the DO from your Functions, and not have to invoke the Worker itself at all
zsmooth
zsmooth2y ago
thanks - i've tried that, I have a src/index.tx that exports just
export class LangChainStore {
export class LangChainStore {
and wrangler.toml like this:
name = "langchain-storage"
compatibility_date = "2023-05-04"
main = "./src/index.ts"

[durable_objects]
bindings = [{name = "LANGCHAIN", class_name = "LangChainStorage"}]

[[migrations]]
tag = "v1"
new_classes = ["LangChainStorage"]
name = "langchain-storage"
compatibility_date = "2023-05-04"
main = "./src/index.ts"

[durable_objects]
bindings = [{name = "LANGCHAIN", class_name = "LangChainStorage"}]

[[migrations]]
tag = "v1"
new_classes = ["LangChainStorage"]
but when I wrangler publish, it says
▲ [WARNING] The entrypoint src/index.ts has exports like an ES Module, but hasn't defined a default export like a module worker normally would. Building the worker using "service-worker" format...

✘ [ERROR] You seem to be trying to use Durable Objects in a Worker written as a service-worker.
▲ [WARNING] The entrypoint src/index.ts has exports like an ES Module, but hasn't defined a default export like a module worker normally would. Building the worker using "service-worker" format...

✘ [ERROR] You seem to be trying to use Durable Objects in a Worker written as a service-worker.
so i'm guessing something wrong with the wrangler.toml?
Hello, I’m Allie!
What if you do
name = "langchain-storage"
compatibility_date = "2023-05-04"
main = "./src/index.ts"
no_bundle = true

[durable_objects]
bindings = [{name = "LANGCHAIN", class_name = "LangChainStorage"}]

[[migrations]]
tag = "v1"
new_classes = ["LangChainStorage"]
name = "langchain-storage"
compatibility_date = "2023-05-04"
main = "./src/index.ts"
no_bundle = true

[durable_objects]
bindings = [{name = "LANGCHAIN", class_name = "LangChainStorage"}]

[[migrations]]
tag = "v1"
new_classes = ["LangChainStorage"]
Oh wait Bundle it first, then change the main Or just add an
export default {}
export default {}
to the file
zsmooth
zsmooth2y ago
that did it! (adding export default {}). thanks. Hmm... but I can't get it to show up in my pages project. I added
--do LANGCHAIN=LangChainStore@langchain-storage
--do LANGCHAIN=LangChainStore@langchain-storage
to my wrangler pages command, and when it starts it says
[wrangler] Your worker has access to the following bindings:
[wrangler] - Durable Objects:
[wrangler] - LANGCHAIN: LangChainStore
(defined in langchain-storage)
[wrangler] - KV Namespaces:
[wrangler] - DBCACHE:
[wrangler] Your worker has access to the following bindings:
[wrangler] - Durable Objects:
[wrangler] - LANGCHAIN: LangChainStore
(defined in langchain-storage)
[wrangler] - KV Namespaces:
[wrangler] - DBCACHE:
but does not show up in context.env in my request handler...
[wrangler] env {
[wrangler] DBCACHE: KVNamespace {},
[wrangler] ASSETS: Fetcher {}
[wrangler] }
[wrangler] env {
[wrangler] DBCACHE: KVNamespace {},
[wrangler] ASSETS: Fetcher {}
[wrangler] }
Oh, never mind. i was running the do with wrangler dev instead of wrangler dev --local
Want results from more Discord servers?
Add your server