Redis driver: enforce unique ID constraint on create#310
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Enforces a uniqueness constraint for RedisDriver.create() by preventing silent overwrites when a record with the same ID already exists, aligning Redis driver behavior with other drivers and fixing the failing TCK case.
Changes:
- Replace unconditional
SETwith conditionalSET ... NXto avoid overwriting existing records - Throw an error when the key already exists (duplicate ID)
| // Use SET with NX option to prevent overwriting existing records | ||
| const result = await this.client.set(key, JSON.stringify(doc), { NX: true }); | ||
|
|
||
| if (!result) { |
There was a problem hiding this comment.
SET with NX typically returns 'OK' on success and null on failure (duplicate). Using if (!result) works, but it’s clearer/safer to check explicitly for the expected failure value (e.g., result === null) or success value (e.g., result !== 'OK') to avoid any ambiguity if return types change across Redis clients.
| if (!result) { | |
| if (result === null) { |
| const result = await this.client.set(key, JSON.stringify(doc), { NX: true }); | ||
|
|
||
| if (!result) { | ||
| throw new Error(`Record with ID ${id} already exists in ${objectName}`); |
There was a problem hiding this comment.
Throwing a plain Error makes it harder for callers (and cross-driver behavior) to reliably detect a duplicate-ID condition. If the project has a standard duplicate/constraint error type or error code used by other drivers, throw that here (or wrap with a distinct name/code) so consumers can handle duplicates consistently without parsing the message.
Redis driver was silently overwriting records on duplicate IDs instead of throwing. TCK test "should throw error on duplicate ID" failed.
Changes
RedisDriver.create() now uses
SET ... NXto atomically check-and-set:Aligns behavior with other drivers (MongoDB, SQL) which reject duplicate primary keys.
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.