Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/twlite/localkv
Asynchronous KV store utility to easily pass data/context in JavaScript applications.
https://github.com/twlite/localkv
api async bun context deno kv local node workers
Last synced: 2 months ago
JSON representation
Asynchronous KV store utility to easily pass data/context in JavaScript applications.
- Host: GitHub
- URL: https://github.com/twlite/localkv
- Owner: twlite
- License: mit
- Created: 2024-02-04T15:52:27.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2024-09-25T19:58:39.000Z (3 months ago)
- Last Synced: 2024-10-12T20:08:07.351Z (3 months ago)
- Topics: api, async, bun, context, deno, kv, local, node, workers
- Language: TypeScript
- Homepage:
- Size: 59.6 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# LocalKV
LocalKV is a simple, type-safe key-value data store to pass data or context between different parts of your application without propagating it through the whole application.
## Installation
```bash
$ npm install localkv
```## Usage
### Example with Express web server
```javascript
import { randomUUID } from 'node:crypto';
import express from 'express';
import { KV } from 'localkv';const app = express();
// create a new instance of KV
const kv = new KV();// middleware to generate a new request id
app.use((req, res, next) => {
// generate new id for each request
const id = randomUUID();console.log(`Request ID Generated: ${id}`);
// ^ "Request ID Generated: e4ce829d-7f67-453a-bc5a-a9db8e4edeef"// create a new context for this request
kv.with(() => {
// set the request-id key to the generated id in this context
kv.set('request-id', id);// execute the next middleware.
next();
});
});app.get('/', (req, res) => {
// get the request-id from the context. Correct data will be automatically available without passing it through the whole application or mutating the request object.
const id = kv.get('request-id');// send the id as response
res.json({ id });
});app.listen(3000, () => {
console.log('Server is running on port 3000');
});
```#### Testing
```bash
$ curl http://localhost:3000
# {"id":"e4ce829d-7f67-453a-bc5a-a9db8e4edeef"}
```