-
Notifications
You must be signed in to change notification settings - Fork 53
feat: Add key-value storage support for plugins #463
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dylankilkenny
merged 7 commits into
main
from
plat-6929-add-support-for-kv-storage-to-plugins
Sep 19, 2025
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f03a274
feat: Add key-value storage support for plugins
dylankilkenny ad7beff
chore: Add comments
dylankilkenny 9be5dc1
chore: Merge main
dylankilkenny d62bf9d
refactor: Apply review changes
dylankilkenny 5fb2de7
test: Fix
dylankilkenny 2d944b4
chore: Merge main
dylankilkenny d821158
fix: Rename `scan` method to `listKeys`
dylankilkenny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| import { PluginContext } from '../lib/plugin'; | ||
|
|
||
| /** | ||
| * Simple KV storage example | ||
| * | ||
| * Demonstrates: | ||
| * - JSON set/get (with optional TTL) | ||
| * - exists/del | ||
| * - scan pattern listing | ||
| * - clear namespace | ||
| * - withLock for atomic sections | ||
| * | ||
| * Usage (params.action): | ||
| * - 'demo' (default): run a small end-to-end flow | ||
| * - 'set': { key: string, value: any, ttlSec?: number } | ||
| * - 'get': { key: string } | ||
| * - 'exists': { key: string } | ||
| * - 'del': { key: string } | ||
| * - 'scan': { pattern?: string, batch?: number } | ||
| * - 'clear': {} | ||
| * - 'withLock': { key: string, ttlSec?: number, onBusy?: 'throw' | 'skip' } | ||
| */ | ||
| export async function handler({ kv, params }: PluginContext) { | ||
| const action = params?.action ?? 'demo'; | ||
|
|
||
| switch (action) { | ||
| case 'set': { | ||
| const { key, value, ttlSec } = params ?? {}; | ||
| assertString(key, 'key'); | ||
| const ok = await kv.set(key, value, { ttlSec: toInt(ttlSec) }); | ||
| return { ok }; | ||
| } | ||
|
|
||
| case 'get': { | ||
| const { key } = params ?? {}; | ||
| assertString(key, 'key'); | ||
| const value = await kv.get(key); | ||
| return { value }; | ||
| } | ||
|
|
||
| case 'exists': { | ||
| const { key } = params ?? {}; | ||
| assertString(key, 'key'); | ||
| const exists = await kv.exists(key); | ||
| return { exists }; | ||
| } | ||
|
|
||
| case 'del': { | ||
| const { key } = params ?? {}; | ||
| assertString(key, 'key'); | ||
| const deleted = await kv.del(key); | ||
| return { deleted }; | ||
| } | ||
|
|
||
| case 'scan': { | ||
| const { pattern, batch } = params ?? {}; | ||
| const keys = await kv.listKeys(pattern ?? '*', toInt(batch, 500)); | ||
| return { keys }; | ||
| } | ||
|
|
||
| case 'clear': { | ||
| const deleted = await kv.clear(); | ||
| return { deleted }; | ||
| } | ||
|
|
||
| case 'withLock': { | ||
| const { key, ttlSec, onBusy } = params ?? {}; | ||
| assertString(key, 'key'); | ||
| const result = await kv.withLock( | ||
| key, | ||
| async () => { | ||
| // Simulate a small critical section | ||
| const stamp = Date.now(); | ||
| await kv.set(`example:last-lock:${key}`, { stamp }); | ||
| return { ok: true, stamp }; | ||
| }, | ||
| { ttlSec: toInt(ttlSec, 30), onBusy: onBusy === 'skip' ? 'skip' : 'throw' } | ||
| ); | ||
| return { result }; | ||
| } | ||
|
|
||
| case 'demo': | ||
| default: { | ||
| // 1) Write JSON and read it back | ||
| await kv.set('example:greeting', { text: 'hello' }); | ||
| const greeting = await kv.get<{ text: string }>('example:greeting'); | ||
|
|
||
| // 2) Write a TTL value (won't await expiry here) | ||
| await kv.set('example:temp', { expires: true }, { ttlSec: 5 }); | ||
|
|
||
| // 3) Check existence and delete | ||
| const existedBefore = await kv.exists('example:to-delete'); | ||
| await kv.set('example:to-delete', { remove: true }); | ||
| const existedAfterSet = await kv.exists('example:to-delete'); | ||
| const deleted = await kv.del('example:to-delete'); | ||
|
|
||
| // 4) Scan keys under example:* | ||
| const list = await kv.listKeys('example:*'); | ||
|
|
||
| // 5) Use a lock to protect an update | ||
| const lockResult = await kv.withLock( | ||
| 'example:lock', | ||
| async () => { | ||
| const count = (await kv.get<number>('example:counter')) ?? 0; | ||
| const next = count + 1; | ||
| await kv.set('example:counter', next); | ||
| return next; | ||
| }, | ||
| { ttlSec: 10, onBusy: 'throw' } | ||
| ); | ||
|
|
||
| return { | ||
| greeting, | ||
| ttlKeyWritten: true, | ||
| existedBefore, | ||
| existedAfterSet, | ||
| deleted, | ||
| scanned: list, | ||
| lockResult, | ||
| }; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function toInt(v: unknown, def = 0): number { | ||
| const n = typeof v === 'string' ? parseInt(v, 10) : typeof v === 'number' ? Math.floor(v) : def; | ||
| return Number.isFinite(n) && n > 0 ? n : def; | ||
| } | ||
|
|
||
| function assertString(v: any, name: string): asserts v is string { | ||
| if (typeof v !== 'string' || v.length === 0) throw new Error(`${name} must be a non-empty string`); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's rename to listKeys and check if some other doc reference or example needs to be updated.