You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Browse filesBrowse the repository at this point in the historyBrowse files
Ubuntu
committed
feat(plugins): atomic conditional writes for plugin storage (insert / updateIf)
Add two additive, single-statement primitives to the plugin
StorageCollection<T> API:
- insert(id, data): insert-once via INSERT … ON CONFLICT (pk) DO NOTHING.
A same-id collision returns { inserted:false, reason:"exists" }; a
declared unique-index collision on a non-id field is classified as
{ inserted:false, reason:"unique_violation", conflictField? } and any
other DB error is re-thrown.
- updateIf(id, { where, set?, delta? }): predicate-guarded atomic update
via one UPDATE … SET json_set/jsonb_set(…) WHERE <pk> AND <guard>
RETURNING data. The guard reuses the numeric-correct where translation
from the prior fix, and integer deltas are applied in-SQL with
COALESCE(base,0) ± n, so N concurrent guarded decrements cannot
oversell. set and delta are separate args; floats are rejected at
runtime; an empty in:[] guard short-circuits to applied:false.
Declared uniqueIndexes are now materialized as real unique indexes on
plugin install (fail-loud on error) and dropped on uninstall. The new
methods are delegated through the in-process context, the workerd bridge,
and the Cloudflare D1 bridge so all runners behave identically.
Proven no-oversell on real Postgres (and the SQLite/D1 dialect); both
dialects covered by the contract suite.
Adds `insert` (insert-once) and `updateIf` (predicate-guarded atomic update) to plugin storage collections. `insert` creates a document only if its id is free and no declared unique index is violated; `updateIf` applies a wholesale `set` and/or integer `delta` in a single guarded statement, so concurrent guarded decrements (e.g. inventory) can no longer oversell. A collection's declared `uniqueIndexes` are now materialized as real unique indexes when the plugin installs — installation fails loudly if a unique index cannot be created (for instance because existing data already violates it).
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- WhereClause is structurally Record<string, WhereValue>; validated as an object above and re-validated by the repo.
0 commit comments