diff --git a/src/lib/db/client-instance-store.ts b/src/lib/db/client-instance-store.ts index 516259153e..1ec463c7bc 100644 --- a/src/lib/db/client-instance-store.ts +++ b/src/lib/db/client-instance-store.ts @@ -21,17 +21,18 @@ const COLUMNS = [ ]; const TABLE = 'client_instances'; -const mapRow = (row) => ({ +const mapRow = (row): IClientInstance => ({ appName: row.app_name, instanceId: row.instance_id, sdkVersion: row.sdk_version, + sdkType: row.sdk_type, // should we add this? clientIp: row.client_ip, lastSeen: row.last_seen, createdAt: row.created_at, environment: row.environment, }); -const mapToDb = (client) => ({ +const mapToDb = (client: INewClientInstance) => ({ app_name: client.appName, instance_id: client.instanceId, sdk_version: client.sdkVersion || '', diff --git a/src/lib/types/stores/client-instance-store.ts b/src/lib/types/stores/client-instance-store.ts index 36695cd474..eead0fbbc0 100644 --- a/src/lib/types/stores/client-instance-store.ts +++ b/src/lib/types/stores/client-instance-store.ts @@ -11,6 +11,7 @@ export interface INewClientInstance { clientIp?: string; lastSeen?: Date; environment?: string; + sdkType?: 'backend' | 'frontend' | null; } export interface IClientInstanceStore extends Store< diff --git a/src/test/e2e/stores/client-instance-store.e2e.test.ts b/src/test/e2e/stores/client-instance-store.e2e.test.ts new file mode 100644 index 0000000000..3e89a4ef17 --- /dev/null +++ b/src/test/e2e/stores/client-instance-store.e2e.test.ts @@ -0,0 +1,60 @@ +import faker from 'faker'; +import dbInit, { type ITestDb } from '../helpers/database-init.js'; +import getLogger from '../../fixtures/no-logger.js'; +import type { + IClientInstanceStore, + IUnleashStores, +} from '../../../lib/types/index.js'; +import type { INewClientInstance } from '../../../lib/types/stores/client-instance-store.js'; + +let db: ITestDb; +let stores: IUnleashStores; +let clientInstanceStore: IClientInstanceStore; + +beforeAll(async () => { + db = await dbInit('client_application_store_e2e_serial', getLogger); + stores = db.stores; + clientInstanceStore = stores.clientInstanceStore; +}); + +afterAll(async () => { + await db.destroy(); +}); + +test('Upserting an application keeps values not provided intact', async () => { + const clientInstance: INewClientInstance = { + appName: faker.internet.domainName(), + instanceId: faker.datatype.uuid(), + environment: 'development', + sdkVersion: 'unleash-client-node:6.6.0', + sdkType: 'backend', + }; + await clientInstanceStore.insert(clientInstance); + + const initial = await clientInstanceStore.get(clientInstance); + + expect(initial).toMatchObject(clientInstance); + + const update: INewClientInstance = { + appName: clientInstance.appName, + instanceId: clientInstance.instanceId, + environment: clientInstance.environment, + clientIp: '::2', + }; + + await clientInstanceStore.insert(update); + + const updated = await clientInstanceStore.get(clientInstance); + + const expectedAfterUpdate = { + clientIp: '::2', + sdkVersion: 'unleash-client-node:6.6.0', + sdkType: 'backend', + }; + expect(updated).toMatchObject(expectedAfterUpdate); + + await clientInstanceStore.bulkUpsert([clientInstance]); + const doubleUpdated = await clientInstanceStore.get(clientInstance); + + expect(doubleUpdated).toMatchObject(expectedAfterUpdate); +});