-
Notifications
You must be signed in to change notification settings - Fork 3
Redis driver: enforce unique ID constraint on create #310
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -394,7 +394,13 @@ export class RedisDriver implements Driver { | |
| }; | ||
|
|
||
| const key = this.generateRedisKey(objectName, id); | ||
| await this.client.set(key, JSON.stringify(doc)); | ||
|
|
||
| // Use SET with NX option to prevent overwriting existing records | ||
| 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}`); | ||
|
||
| } | ||
|
|
||
| return doc; | ||
| } | ||
|
|
||
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.
SETwithNXtypically returns'OK'on success andnullon failure (duplicate). Usingif (!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.