diff --git a/src/lib/features/unique-connection/unique-connection-store.e2e.test.ts b/src/lib/features/unique-connection/unique-connection-store.e2e.test.ts index 29fca9e691..b189d561d5 100644 --- a/src/lib/features/unique-connection/unique-connection-store.e2e.test.ts +++ b/src/lib/features/unique-connection/unique-connection-store.e2e.test.ts @@ -5,6 +5,7 @@ import type { IUnleashStores, } from '../../../lib/types'; import HyperLogLog from 'hyperloglog-lite'; +import { isAfter } from 'date-fns'; let stores: IUnleashStores; let db: ITestDb; @@ -56,3 +57,23 @@ test('should indicate when no entry', async () => { expect(fetchedHll).toBeNull(); }); + +test('should update updated_at date', async () => { + const hll = HyperLogLog(12); + hll.add(HyperLogLog.hash('connection-1')); + hll.add(HyperLogLog.hash('connection-2')); + + await uniqueConnectionStore.insert({ + id: 'current', + hll: hll.output().buckets, + }); + const firstFetch = await uniqueConnectionStore.get('current'); + + await uniqueConnectionStore.insert({ + id: 'current', + hll: hll.output().buckets, + }); + const secondFetch = await uniqueConnectionStore.get('current'); + + expect(isAfter(secondFetch?.updatedAt!, firstFetch?.updatedAt!)).toBe(true); +}); diff --git a/src/lib/features/unique-connection/unique-connection-store.ts b/src/lib/features/unique-connection/unique-connection-store.ts index aa3eab7c5c..0023703eed 100644 --- a/src/lib/features/unique-connection/unique-connection-store.ts +++ b/src/lib/features/unique-connection/unique-connection-store.ts @@ -1,6 +1,7 @@ import type { Db } from '../../db/db'; import type { IUniqueConnectionStore } from '../../types'; import type { UniqueConnections } from './unique-connection-store-type'; +import type { Knex } from 'knex'; export class UniqueConnectionStore implements IUniqueConnectionStore { private db: Db; @@ -10,8 +11,14 @@ export class UniqueConnectionStore implements IUniqueConnectionStore { } async insert(uniqueConnections: UniqueConnections): Promise { - await this.db('unique_connections') - .insert({ id: uniqueConnections.id, hll: uniqueConnections.hll }) + await this.db }>( + 'unique_connections', + ) + .insert({ + id: uniqueConnections.id, + hll: uniqueConnections.hll, + updated_at: this.db.raw('DEFAULT'), + }) .onConflict('id') .merge(); }