https://discord.cloudflare.com logo
Join Discord
Powered by
# durable-objects
  • j

    johtso

    03/09/2023, 3:39 PM
    For example will the return value of
    COUNTER.get('myCounter').currentValue()
    be typed as a number if the DO method returns a number?
  • j

    johtso

    03/09/2023, 3:49 PM
    Looks like there's no typescript stuff going on, so you'd have to do something like
    as ReturnType<typeof Counter.currentValue>
  • k

    Kevin W - Itty

    03/09/2023, 4:07 PM
    I would do something like const MyObj = DO.get(‘foo’) as MyDurableClass
  • j

    johtso

    03/09/2023, 4:08 PM
    Oh wow, that's a great idea
  • k

    Kevin W - Itty

    03/09/2023, 4:08 PM
    to just get the full typings at once
  • k

    Kevin W - Itty

    03/09/2023, 4:10 PM
    I’ve had to use that pattern at various places throughout worker code, esp as you pile on more magic… it just reconnects the types that may have been lost or difficult to drill through your app
  • j

    johtso

    03/09/2023, 4:11 PM
    I could probably even do some
    Pick
    magic so it only exposes router methods
  • j

    johtso

    03/09/2023, 4:14 PM
    Although looking at the code it exposes anything that
    isValidMethod
    except for fetch
  • k

    Kevin W - Itty

    03/09/2023, 4:15 PM
    Yeah, I don’t think it would grab the internal itty-durable methods
  • j

    johtso

    03/09/2023, 4:16 PM
    constructor method?
  • k

    Kevin W - Itty

    03/09/2023, 4:17 PM
    I assume the types would refer to your own constructor, if you have one
  • k

    Kevin W - Itty

    03/09/2023, 4:18 PM
    I’m in the middle of a massive overhaul of itty-router, bringing CORS and extras into the main lib, so itty-durable is next for a full TS conversion at least where it matters)
  • j

    johtso

    03/09/2023, 4:19 PM
    Awesome!
  • j

    johtso

    03/09/2023, 4:21 PM
    I do wonder how the trpc approach differs. I do like that you're basically just writing functions that get passed the DO as ctx
  • j

    johtso

    03/09/2023, 4:21 PM
    And that the requests actually get validated using zod
  • k

    Kevin W - Itty

    03/09/2023, 4:22 PM
    Have a link for that?
  • j

    johtso

    03/09/2023, 4:23 PM
    https://github.com/markusahlstrand/trpc-durable-objects
  • k

    Kevin W - Itty

    03/09/2023, 4:23 PM
    Personally I don’t love tRPC much myself, but that’s just cuz it feels boilerplatey to me… I’m sure it’s great for folks that like it!
  • j

    johtso

    03/09/2023, 4:23 PM
    Interesting how it's flipped on its head.. with this approach it creates a DO class for you based on your router
  • j

    johtso

    03/09/2023, 4:24 PM
    Where as itty-durable you define the class but subclass something
  • j

    johtso

    03/09/2023, 4:25 PM
    The example looks rather succinct
  • k

    Kevin W - Itty

    03/09/2023, 4:25 PM
    Haha yeah… see, that’s the part that I didn’t want to have to think about when making DOs. I only want to care about the business logic… in the case of a counter for instance, just the value and the increment. All the rest of the native DO code was noise IMO… the storage bits, fetch, internal routing, even building a request from the Worker.
  • j

    johtso

    03/09/2023, 4:29 PM
    Looks like this takes a similar approach, you just define a tRPC router with your business logic
  • j

    johtso

    03/09/2023, 4:30 PM
    Obviously doesn't have the persistence magic though
  • j

    johtso

    03/09/2023, 4:32 PM
    Obviously the definition isn't as minimal because you write the method input shape using a zod scheme instead of just method arguments, but that gets you type safety
  • j

    johtso

    03/09/2023, 4:33 PM
    I guess it also doesn't have the idea of querying arbitrary properties
  • k

    Kevin W - Itty

    03/09/2023, 4:33 PM
    I think just writing your DO a in full TS and casting the stub as you grab it in the Worker should still get you that… I’ll do some tests to confirm this 🙂
  • j

    johtso

    03/09/2023, 4:34 PM
    I guess the only problem with this is you're still not validating, just asserting
  • k

    Kevin W - Itty

    03/09/2023, 4:34 PM
    I sometimes forget about that feature… I basically always use the methods to interact w the DO now, not awaiting direct props
  • j

    johtso

    03/09/2023, 4:34 PM
    Although I'm not sure when that might cause problems..
1...512513514...567Latest