From 96f0cef342b02acd0435887b2d7a404db5e26489 Mon Sep 17 00:00:00 2001 From: Daniel Salazar Date: Sat, 10 Oct 2026 19:02:40 -0400 Subject: [PATCH] refactor(stores): read affected rows off WriteResult Every database client returns affectedRows and anyRowsAffected, so the `affectedRows ?? changes ?? 0` fallbacks in ShareStore, NotificationStore, UserStore and UserBlockStore read those directly. --- src/backend/stores/notification/NotificationStore.js | 12 ++++++------ src/backend/stores/share/ShareStore.js | 10 +++++----- src/backend/stores/user/UserStore.ts | 12 ++---------- src/backend/stores/userBlock/UserBlockStore.ts | 7 +------ 4 files changed, 14 insertions(+), 27 deletions(-) diff --git a/src/backend/stores/notification/NotificationStore.js b/src/backend/stores/notification/NotificationStore.js index 78b85d6a8..b49b23ec8 100644 --- a/src/backend/stores/notification/NotificationStore.js +++ b/src/backend/stores/notification/NotificationStore.js @@ -202,7 +202,7 @@ export class NotificationStore extends PuterStore { 'UPDATE `notification` SET `value` = ?, `shown` = NULL WHERE `uid` = ? AND `user_id` = ? AND `acknowledged` IS NULL', [serialized, uid, userId], ); - return (result?.affectedRows ?? result?.changes ?? 0) > 0; + return result.anyRowsAffected; } async markAcknowledged(uid, userId) { @@ -211,7 +211,7 @@ export class NotificationStore extends PuterStore { 'UPDATE `notification` SET `acknowledged` = ? WHERE `uid` = ? AND `user_id` = ? AND `acknowledged` IS NULL', [now, uid, userId], ); - return (result?.affectedRows ?? result?.changes ?? 0) > 0; + return result.anyRowsAffected; } async markShown(uid, userId) { @@ -220,7 +220,7 @@ export class NotificationStore extends PuterStore { 'UPDATE `notification` SET `shown` = ? WHERE `uid` = ? AND `user_id` = ? AND `shown` IS NULL', [now, uid, userId], ); - return (result?.affectedRows ?? result?.changes ?? 0) > 0; + return result.anyRowsAffected; } /** @@ -240,7 +240,7 @@ export class NotificationStore extends PuterStore { `WHERE \`user_id\` = ? AND \`shown\` IS NULL AND \`uid\` IN (${placeholders})`, [now, userId, ...unique], ); - return result?.affectedRows ?? result?.changes ?? 0; + return result.affectedRows; } async deleteByUid(uid, userId) { @@ -248,7 +248,7 @@ export class NotificationStore extends PuterStore { 'DELETE FROM `notification` WHERE `uid` = ? AND `user_id` = ?', [uid, userId], ); - return (result?.affectedRows ?? result?.changes ?? 0) > 0; + return result.anyRowsAffected; } /** @@ -283,7 +283,7 @@ export class NotificationStore extends PuterStore { }); const result = await this.clients.db.write(statement, [batch]); - return result?.affectedRows ?? result?.changes ?? 0; + return result.affectedRows; } // -- Internals ---------------------------------------------------- diff --git a/src/backend/stores/share/ShareStore.js b/src/backend/stores/share/ShareStore.js index 779fa70d2..9f0084509 100644 --- a/src/backend/stores/share/ShareStore.js +++ b/src/backend/stores/share/ShareStore.js @@ -814,7 +814,7 @@ export class ShareStore extends PuterStore { ? [holderUserId, fsentryId, issuerUserId] : [holderUserId, fsentryId], ); - return (result?.affectedRows ?? result?.changes ?? 0) > 0; + return result.anyRowsAffected; } /** @@ -861,7 +861,7 @@ export class ShareStore extends PuterStore { `DELETE FROM \`share\` WHERE \`fsentry_id\` IN (${placeholders})`, fsentryIds, ); - return result?.affectedRows ?? result?.changes ?? 0; + return result.affectedRows; } /** @@ -925,7 +925,7 @@ export class ShareStore extends PuterStore { `DELETE FROM \`share\` WHERE \`uid\` IN (${placeholders})`, retired.map((row) => row.uid), ); - return result?.affectedRows ?? result?.changes ?? 0; + return result.affectedRows; } async deleteByUid(uid) { @@ -933,7 +933,7 @@ export class ShareStore extends PuterStore { 'DELETE FROM `share` WHERE `uid` = ?', [uid], ); - return (result?.affectedRows ?? result?.changes ?? 0) > 0; + return result.anyRowsAffected; } // -- Anyone with the link ----------------------------------------- @@ -1083,7 +1083,7 @@ export class ShareStore extends PuterStore { 'DELETE FROM `share` WHERE `fsentry_id` = ? AND `anyone` = 1', [fsentryId], ); - const removed = (result?.affectedRows ?? result?.changes ?? 0) > 0; + const removed = result.anyRowsAffected; if (removed) await this.#markLinkRevoked(); return removed; } diff --git a/src/backend/stores/user/UserStore.ts b/src/backend/stores/user/UserStore.ts index 6afd5dbd9..a5e7e24ff 100644 --- a/src/backend/stores/user/UserStore.ts +++ b/src/backend/stores/user/UserStore.ts @@ -711,16 +711,8 @@ export class UserStore extends PuterStore { [...values, userId], ); - if (guard) { - const affected = - (result as { affectedRows?: number; changes?: number }) - ?.affectedRows ?? - (result as { affectedRows?: number; changes?: number }) - ?.changes ?? - 0; - // Nothing was written, so there are no cache keys to retire. - if (affected === 0) return false; - } + // Nothing was written, so there are no cache keys to retire. + if (guard && !result.anyRowsAffected) return false; const fresh = await this.getByProperty('id', userId, { force: true }); diff --git a/src/backend/stores/userBlock/UserBlockStore.ts b/src/backend/stores/userBlock/UserBlockStore.ts index 5a296990c..7003cda2d 100644 --- a/src/backend/stores/userBlock/UserBlockStore.ts +++ b/src/backend/stores/userBlock/UserBlockStore.ts @@ -90,11 +90,6 @@ export class UserBlockStore extends PuterStore { 'DELETE FROM `user_block` WHERE `blocker_user_id` = ? AND `blocked_user_id` = ?', [blockerUserId, blockedUserId], ); - return ( - ((result as { affectedRows?: number; changes?: number }) - ?.affectedRows ?? - (result as { changes?: number })?.changes ?? - 0) > 0 - ); + return result.anyRowsAffected; } }