A Cloudflare Workers KV datastore plugin for kv-orm.
👤 Greg Brimble
- Github: @GregBrimble
- Personal Website: https://gregbrimble.com/
Contributions, issues and feature requests are welcome! Feel free to check issues page.
Please consider giving this project a ⭐️ if you use it, or if it provides some inspiration!
npm install --save @kv-orm/core @kv-orm/cf-workers
npm install --save-dev @cloudflare/workers-typesimport{KVNamespace}from"@cloudflare/workers-types";import{CloudflareWorkersKVDatastore}from"@kv-orm/cf-workers";import{Entity}from"@kv-orm/core";declare global {constmyKVNamespaceBinding: KVNamespace;}constdatastore=newCloudflareWorkersKVDatastore(myKVNamespaceBinding);
@Entity({ datastore })classMyEntity{// ...}The constructor can also take a couple of options:
keySeparatordefaults to a colon,:optionsGenerator, for a givenkeyandvaluecan return options to pass to the KV Namespaceputmethod:expirationorexpirationTtl. More information about expiring data can be found on the official Cloudflare documentation.
constdatastore=newCloudflareWorkersKVDatastore(myKVNamespaceBinding,{keySeparator: ":",optionsGenerator: (key,value)=>({expirationTtl: 120,// Or, alternatively, `expiration`}),});In this particular example, all data will expire after 120 seconds.
Note: this expiration only affects data from new read/search operations. Data already in the memory cache will persist until purged (if using within a Worker context, this is until the end of the single request).
git clone git@github.com:kv-orm/cf-workers.git
npm installnpm run lint # 'npm run lint:fix' will automatically fix most problems
npm testCopyright © 2019 Greg Brimble.
This project is MIT licensed.