Skip to content

Commit

Permalink
feat(api): use merge on conflict for last user application connection…
Browse files Browse the repository at this point in the history
…s upsert
  • Loading branch information
P-Jeremy committed Mar 4, 2025
1 parent 895a711 commit 1eee8a1
Showing 1 changed file with 4 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ import { knex } from '../../../../db/knex-database-connection.js';
const TABLE_NAME = 'last-user-application-connections';

async function upsert({ userId, application, lastLoggedAt }) {
const existingConnection = await knex(TABLE_NAME).where({ userId, application }).first();

if (existingConnection) {
return knex(TABLE_NAME).where({ userId, application }).update({ lastLoggedAt });
}

return knex(TABLE_NAME).insert({ userId, application, lastLoggedAt });
return knex(TABLE_NAME)
.insert({ userId, application, lastLoggedAt })
.onConflict(['userId', 'application'])
.merge({ lastLoggedAt });
}

export const lastUserApplicationConnectionsRepository = { upsert };

0 comments on commit 1eee8a1

Please sign in to comment.