itty-durable
What is it?
This library helps reduce the boilerplate around Durable Objects, allowing you to focus on the business logic of your stateful objects, rather than things like internal routing, building requests, etc.
Now your Durable Object class can look this simple:
ts
import { createDurable } from 'itty-durable'
export class Counter extends createDurable({ autoReturn: true, autoPersist: true }) {
constructor(state, env) {
super(state, env)
this.counter = 0
}
increment() {
this.counter++
}
setValue(newValue: number) {
this.counter = newValue
}
}
While being able to access them from a Worker much like a native class:
ts
import { Router, error, withParams } from 'itty-router'
import { withDurables } from 'itty-durable'
// export the durable class, per spec
export { Counter } from './Counter'
const router = Router()
router
// add upstream middleware
.all('*', withDurables(), withParams)
// get the contents of a DO
.get('/:id', ({ id, Counter }) =>
Counter.get(id).toJSON()
)
// call a method on the DC
.get('/:id/increment', ({ id, Counter }) =>
Counter.get(id).increment()
)
// or pass data to a method
.get('/set/:value', withParams, ({ id, value, Counter }) =>
Counter.get(id).setValue(Number(value))
)