{"id":15715752,"url":"https://github.com/ticket-bridge/hyper-durable","last_synced_at":"2025-04-14T13:08:57.684Z","repository":{"id":37467058,"uuid":"489120537","full_name":"ticket-bridge/hyper-durable","owner":"ticket-bridge","description":"Simple and useful Durable Object abstraction","archived":false,"fork":false,"pushed_at":"2023-01-22T21:00:18.000Z","size":45906,"stargazers_count":64,"open_issues_count":4,"forks_count":3,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-03-25T00:42:21.318Z","etag":null,"topics":["cloudflare","durable","durable-objects","objects"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ticket-bridge.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-05-05T20:42:56.000Z","updated_at":"2025-01-10T00:13:18.000Z","dependencies_parsed_at":"2023-02-12T18:15:45.963Z","dependency_job_id":null,"html_url":"https://github.com/ticket-bridge/hyper-durable","commit_stats":null,"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ticket-bridge%2Fhyper-durable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ticket-bridge%2Fhyper-durable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ticket-bridge%2Fhyper-durable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ticket-bridge%2Fhyper-durable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ticket-bridge","download_url":"https://codeload.github.com/ticket-bridge/hyper-durable/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248886322,"owners_count":21177643,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cloudflare","durable","durable-objects","objects"],"created_at":"2024-10-03T21:42:45.159Z","updated_at":"2025-04-14T13:08:57.153Z","avatar_url":"https://github.com/ticket-bridge.png","language":"TypeScript","readme":"# HyperDurable\n\nHyperDurable is a base class for Durable Objects to enable natural, object-like access to underlying persistent storage from Durable Object stubs.  HyperDurable completely abstracts the Durable Object fetch API, improving code readability in business logic layers.\n\n- Abstracts away Durable Object fetch API\n- Automatically persists dirty data\n- Built with TypeScript\n- Comprehensive test suite\n\n## Installation\n\nWith Yarn:\n\n```bash\nyarn add @ticketbridge/hyper-durable\n```\n\nWith NPM:\n\n```bash\nnpm i @ticketbridge/hyper-durable\n```\n\n## Usage\n\nWrite your durable object class by extending the `HyperDurable` base class.  In the constructor, pass the `state` and `env` to `HyperDurable` via `super()`.  `HyperDurable` will load all previously persisted data into memory inside its `fetch`, so any properties you set in the constructor after calling `super()` will be overriden by any previously persisted data.\n\nInside your durable object, access properties and methods in memory using `this`.  No need to worry about persistence- dirty data is persisted at the end of every fetch request.\n\n```javascript\n// RubberDuck.js\nimport { HyperDurable } from '@ticketbridge/hyper-durable';\n\nexport class RubberDuck extends HyperDurable {\n  constructor(state, env) {\n    // Pass state and env to HyperDurable\n    super(state, env);\n\n    // Anything set here will be overriden by previously persisted data, if any exists\n    // Therefore, you can safely set default values here\n    this.name = 'New Duck';\n    this.favoriteFoods = [];\n  }\n\n  addFavoriteFood(food) {\n    this.favoriteFoods.push(food);\n  }\n\n  sayHello() {\n    return `Hello world, my name is ${this.name}, and I have ${this.favoriteFoods.length} favorite foods.`;\n  }\n}\n```\n\nIn your worker, first proxy your durable object namespaces with `proxyHyperDurables`.  Obtaining a stub is unchanged: generate your id with your preferred method from the namespace API (i.e., `newUniqueId`, `idFromName`, `idFromString`), then use `get` to construct an object stub.\n\nEvery stub operation must be `await`ed, since they all use the `fetch` API under the hood.  Properties can be read directly from the stub.  Properties can be set with their auto-generated setters (in the format `set` + `PropName`).  Methods can be called directly from the stub.\n\n```javascript\n// worker.js\nimport { proxyHyperDurables } from '@ticketbridge/hyper-durable';\nimport { RubberDuck } from './RubberDuck';\n\n// Export the DO class\nexport { RubberDuck };\n\nexport default {\n  async fetch(request, env) {\n    // Proxy the namespace\n    const { RUBBERDUCK } = proxyHyperDurables(env, {\n      // BINDINGNAME: DurableObjectClass\n      RUBBERDUCK: RubberDuck\n    });\n\n    // Obtain a stub\n    const id = RUBBERDUCK.idFromName('firstDuck');\n    const stub = RUBBERDUCK.get(id);\n\n    // Await properties\n    const name = await stub.name; // 'New Duck'\n\n    // Await setters\n    const newName = await stub.setName('Special Duck'); // 'Special Duck'\n\n    // Await methods\n    await Promise.all([\n      stub.addFavoriteFood('herring'),\n      stub.addFavoriteFood('shrimp')\n    ]);\n    const greeting = await stub.sayHello(); // 'Hello world, my name is Special Duck, and I have 2 favorite foods.'\n  \n    return new Response(greeting);\n  }\n}\n```\n\n## API\n\n### HyperDurable\n\nUse as the base class of your durable object.  Includes some properties and methods by default, which are described below.\n\n#### `env: Env`\n\nThe `env` passed in the constructor.\n\n#### `state: HyperState`\n\nThe `state` passed in the constructor, plus:\n\n##### `state.dirty: Set\u003cstring\u003e`\n\nSet of properties that have been changed in memory but are not yet persisted.\n\n##### `state.persisted: Set\u003cstring\u003e`\n\nSet of properties that are persisted in Durable Object storage.\n\n##### `state.tempKey: string`\n\nUsed to track the key of a deeply-nested property (i.e., when accessing `this.favoriteFoods[0]`, `state.tempKey` is `favoriteFoods`).\n\n#### `storage: DurableObjectStorage`\n\nDurable Object storage, from `state.storage`.\n\n#### `router: Router`\n\nAn [itty-router](https://www.npmjs.com/package/itty-router) to handle incoming fetch requests.\n\n#### `async initialize()`\n\nInitializes loading if loading has not already begun.\n\n#### `async load()`\n\nLoads persisted data into memory.\n\n#### `async persist()`\n\nPersists dirty properties.\n\n#### `async destroy()`\n\nDeletes all data in `storage`, clears `state.dirty` and `state.persisted`, and deletes all properties from memory.\n\n#### `toObject()`\n\nReturns all persisted and dirty data as an object.\n\n#### `async fetch(request: Request): Promise\u003cResponse\u003e`\n\nInitializes the object (if not previously initialized), then passes the request to `router`.  After `router` handles the request, persists any dirty data.\n\n### `proxyHyperDurables(env: Env, doBindings: { [key: string]: DOClass })`\n\nUse to proxy your durable object namespaces.  Accepts two parameters: `env` and `doBindings`.  `env` is the `env` of your worker where namespaces are accessed.  `doBindings` is an object, where the keys are *binding names* and the values are the *durable object classes* associated with those binding.  Returns an object, where the keys are the passed *binding names* and the values are the associated *HyperNamespaceProxy*.\n\n#### HyperNamespaceProxy\n\nUse to generate object IDs and get object stubs, just as in the upstream [DurableObjectNamespace API](https://developers.cloudflare.com/workers/runtime-apis/durable-objects/#accessing-a-durable-object-from-a-worker).\n\n### HyperStub\n\nProduced by `HyperNamespaceProxy.get(id)`.  The `fetch` method can be accessed directly, as in the upstream [API](https://developers.cloudflare.com/workers/runtime-apis/durable-objects/#sending-http-requests).  The stub also allows for simple access to properties and methods.\n\nTo get a property, `await` it:\n\n```javascript\nawait stub.name;\n```\n\nTo set a property, `await` the auto-generated setter (returns the new value):\n\n```javascript\nawait stub.setName('Eendje');\n```\n\nTo invoke a method, `await` it (**NOTE:** If a method has no return value, it will return `null` instead of the usual `undefined`):\n\n```javascript\nawait stub.sayHello();\n```\n\nStub properties and methods will return their value directly in the event of a success.  If the operation fails, a `HyperError` will be thrown with the following structure:\n\n```javascript\n{\n  message: 'Error message',\n  details: 'Error details'\n}\n```\n\n## Special Thanks\n\nThis library was heavily inspired by [itty-durable](https://github.com/kwhitley/itty-durable) from [Kevin Whitley](https://github.com/kwhitley).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fticket-bridge%2Fhyper-durable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fticket-bridge%2Fhyper-durable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fticket-bridge%2Fhyper-durable/lists"}