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-types
import { KVNamespace } from "@cloudflare/workers-types";
import { CloudflareWorkersKVDatastore } from "@kv-orm/cf-workers";
import { Entity } from "@kv-orm/core";
declare global {
const myKVNamespaceBinding: KVNamespace;
}
const datastore = new CloudflareWorkersKVDatastore(myKVNamespaceBinding);
@Entity({ datastore })
class MyEntity {
// ...
}
The constructor can also take a couple of options:
keySeparator
defaults to a colon,:
optionsGenerator
, for a givenkey
andvalue
can return options to pass to the KV Namespaceput
method:expiration
orexpirationTtl
. More information about expiring data can be found on the official Cloudflare documentation.
const datastore = new CloudflareWorkersKVDatastore(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 install
npm run lint # 'npm run lint:fix' will automatically fix most problems
npm test
Copyright © 2019 Greg Brimble.
This project is MIT licensed.