Skip to content

kv-orm/cf-workers

Repository files navigation

Welcome to @kv-orm/cf-workers 👋

GitHub Actions Checks LGTM Alerts Synk Vulnerabilities Codecov LGTM Code Quality Code Climate Maintainability Version License Types GitHub Last Commit

A Cloudflare Workers KV datastore plugin for kv-orm.

Author

👤 Greg Brimble

🤝 Contributing

Contributions, issues and feature requests are welcome! Feel free to check issues page.

😍 Show your support

Please consider giving this project a ⭐️ if you use it, or if it provides some inspiration!

Usage

Install

npm install --save @kv-orm/core @kv-orm/cf-workers
npm install --save-dev @cloudflare/workers-types

Quick Start

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 {
  // ...
}

Options

The constructor can also take a couple of options:

  • keySeparator defaults to a colon, :
  • optionsGenerator, for a given key and value can return options to pass to the KV Namespace put method: expiration or expirationTtl. 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).

Development

Clone and Install Dependencies

git clone git@github.com:kv-orm/cf-workers.git
npm install

Run tests

npm run lint  # 'npm run lint:fix' will automatically fix most problems
npm test

📝 License

Copyright © 2019 Greg Brimble.
This project is MIT licensed.