Last Updated: November 2026 · Plugin API: 1.1 · Capability: storage
registry.storage is JSON key/value storage scoped to your plugin. Each value lives under your providerType, so other plugins cannot read or overwrite it. Use it for global plugin state (counters, snapshots, daily totals), for per-user state (XP, achievements, cooldowns), and for leaderboards.
Declare "capabilities": ["storage"] in your manifest. Without it, every call throws PluginCapabilityError. See capabilities.md.
API
registry.storage.global.get<T>(key): Promise<T | null>
registry.storage.global.set(key, value): Promise<void>
registry.storage.global.merge(key, partial): Promise<object> // shallow, atomic
registry.storage.global.update<T>(key, fn: (current: T | null) => T | undefined): Promise<T | null> // compare-and-set (API 1.4, additive)
registry.storage.global.delete(key): Promise<boolean>
registry.storage.global.list(prefix?): Promise<{ key, value, updatedAt }[]>
registry.storage.user(userId).get / set / merge / update / delete / list // same signatures
registry.storage.query(key, {
orderBy: 'total', // JSON path inside the value, dot-separated
direction?: 'desc' | 'asc', // default 'desc'
limit?: number, // 1-500, default 50
offset?: number, // 0-100000, default 0
where?: { path: 'weekly.xp', op: 'eq' | 'gt' | 'gte' | 'lt' | 'lte', value: number | string | boolean },
}): Promise<{ userId, value }[]>
Example
export async function register(registry) {
registry.addAction('game:hand:completed', async ({ players }) => {
for (const { playerId } of players) {
const user = registry.storage.user(playerId);
const xp = (await user.get<{ total: number }>('xp')) ?? { total: 0 };
await user.set('xp', { ...xp, total: xp.total + 10, updatedAt: new Date().toISOString() });
}
});
registry.registerRoute('GET', '/leaderboard', async (_req, res) => {
const top = await registry.storage.query<{ total: number }>('xp', { orderBy: 'total', limit: 100 });
res.json({ success: true, data: top.map((row, i) => ({ rank: i + 1, userId: row.userId, xp: row.value.total })) });
});
}
Rules and limits
| Rule | Limit |
|---|---|
| Key format | ^[a-z0-9:_-]{1,128}$. Use : to build namespaces such as stats:2026-11-07 |
| Reserved prefixes | idem: and sys: belong to the engine (wallet idempotency, daily caps, rate limits). Plugins can’t read, write or list them |
| Value | Any JSON value except null or undefined at the top level. Use delete() to remove a key |
| Value size | 64 KB of serialised JSON, checked on set and on the result of merge |
| Not allowed in values | Functions, symbols and BigInt (they don’t serialise), and the NUL character \u0000 (PostgreSQL jsonb can’t store it) |
list() | At most 1000 entries, ordered by key |
user(userId) | userId is 1-128 characters |
Invalid input throws ValidationError (code VALIDATION_ERROR). If the user is deleted, their rows go with them (foreign-key cascade).
merge semantics
merge(key, partial) is one SQL statement (INSERT … ON CONFLICT DO UPDATE SET value = value || partial). Concurrent merges of different fields all land, and the merged object is returned. The merge is shallow: a nested object in partial replaces the stored nested object. If the stored value is not an object, it is replaced by partial. To increment a counter safely, use update (below), or store per-event fields and sum them.
query semantics
queryreads the per-user rows of one key. Global rows are not included.orderByandwhere.pathare JSON paths made of dot-separated segments ([A-Za-z0-9_-], at most 8 deep). They are passed to PostgreSQL as parameters (value #> $path::text[]) and never interpolated into the SQL.- Ordering uses PostgreSQL
jsonbordering: numbers sort numerically and strings lexically. Rows without the path sort last. Ties are broken byuserId. wherecompares only values of the same JSON type.{ op: 'gte', value: '0' }(a string) does not match numeric fields.- Only the five operators above are accepted.
Lifecycle
- Data survives deactivation, upgrades and restarts.
- Deleting the plugin (
DELETE /api/v1/admin/plugins/:id) removes all of itsplugin_dataandplugin_user_datarows in the same transaction as thePluginrow.
Tables
plugin_data (provider_type, key, value jsonb, created_at, updated_at) PK (provider_type, key)
plugin_user_data (provider_type, user_id → users.id ON DELETE CASCADE, key,
value jsonb, created_at, updated_at) PK (provider_type, user_id, key)
INDEX (provider_type, key), INDEX (user_id)
There’s no foreign key to plugins, because plugins loaded from PAYMENT_PLUGINS have no row. deletePlugin removes the rows explicitly instead.
Core and tests
pluginStorageFor(providerType) (exported from src/core/plugins) returns the same API without the capability guard, for core code and integration tests. deletePluginStorage(providerType) removes every row of a plugin. The integration tests in src/core/plugins/__tests__/plugin-storage.integration.test.ts run against a real database when DATABASE_URL is set.
In plugin tests, createTestRegistry() from @poker-engine/plugin-sdk/testing provides an in-memory storage with the same key and size rules (see SDK: Testing).
See Also
- Capabilities — declaring and granting
storage - Wallet — the reserved
idem:andsys:keys - Plugin Development Guide
update (compare-and-set)
update(key, fn) (API 1.4, additive) is a read-modify-write that holds across every server process:
const leaders = await registry.storage.global.update<string[]>('leaders', (current) => {
const list = current ?? [];
return list.includes(userId) ? undefined : [...list, userId]; // undefined: leave it as it is
});
- It runs in one database transaction: a transaction-scoped advisory lock on the plugin, the key (and the user, for
user(userId).update) so two creators of a missing key queue as well, then the row readFOR UPDATE(so a plainsetormergeof an existing row waits too), thenfn, then the write. The locks end with the transaction. fn(current)gets the stored value (nullwhen the key is absent) and returns the next value, orundefinedto keep the current one (nothing is written; a missing key stays missing). The result is the stored value afterwards.- Keep
fnshort and free of side effects: it runs while the lock is held, the transaction may last at most 10 seconds, and on a deadlock or a write conflict the whole update is retried (at most 3 attempts), sofnmay run more than once. Do network calls, payouts and broadcasts outside it, and use whatupdatereturns. - The usual key and value rules apply (reserved prefixes refused, 64 KB, no
nullat the top level). A plugin can only update its own keys. - Use it for leaders, counters, close markers and logs keyed by an event id (skip the entry when the id is already in the list, so a retried event adds nothing).