Last active
October 14, 2025 15:30
-
-
Save easement/e5679985b744ee8b1b9ff07f9f26f916 to your computer and use it in GitHub Desktop.
reduce N+1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| async deleteTeamAndMembers(id: string): Promise<void> { | |
| return await this.dbUtils.executeInTransaction(async trx => { | |
| // Get team | |
| const team = await trx('teams').where({ id }).first() | |
| if (!team) { | |
| throw new NotFoundError('Team not found') | |
| } | |
| // Get all team member user IDs in ONE query | |
| const memberUserIds = await trx('team_memberships') | |
| .where({ team_id: id }) | |
| .pluck('user_id') | |
| // Get all team manager user IDs in ONE query | |
| const managerUserIds = await trx('team_managements') | |
| .where({ team_id: id }) | |
| .pluck('user_id') | |
| // Combine all user IDs to delete | |
| const allUserIds = [...memberUserIds, ...managerUserIds] | |
| // Delete all team memberships for this team in ONE query | |
| await trx('team_memberships').where({ team_id: id }).del() | |
| // Delete all team managements for this team in ONE query | |
| await trx('team_managements').where({ team_id: id }).del() | |
| // Delete all users (both members and managers) in ONE query | |
| if (allUserIds.length > 0) { | |
| await trx('users').whereIn('id', allUserIds).del() | |
| } | |
| // Delete the team | |
| await trx('teams').where({ id }).del() | |
| }) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment