diff --git a/src/__tests__/api/_test-utils/_all.ts b/src/__tests__/api/_test-utils/_all.ts index 28d3d51da..2f1e9bd27 100644 --- a/src/__tests__/api/_test-utils/_all.ts +++ b/src/__tests__/api/_test-utils/_all.ts @@ -1,4 +1,5 @@ import { ConfigDomain } from "backend/lib/config-persistence/types"; +import { KeyValueDomain } from "backend/lib/key-value/types"; import { setupAppConfigTestData } from "./_app-config"; import { setupIntegrationsConstantsTestData } from "./_integrations-constants"; import { setupCredentialsTestData } from "./_credentials"; @@ -13,10 +14,10 @@ import { setupActivatedStorageTestData } from "./_activated-storage"; import { setupTestDatabaseData } from "./_data"; import { portalTestData } from "./portal"; -export const setupAllTestData = async ( - domains: Array -) => { - const allTestData: [ConfigDomain | "data", () => Promise][] = [ +type DomainTypes = ConfigDomain | KeyValueDomain | "data"; + +export const setupAllTestData = async (domains: DomainTypes[]) => { + const allTestData: [DomainTypes, () => Promise][] = [ ["roles", setupRolesTestData], ["app-config", setupAppConfigTestData], ["users", setupUsersTestData], diff --git a/src/backend/lib/key-value/index.ts b/src/backend/lib/key-value/index.ts index dd12c9232..14489dac8 100644 --- a/src/backend/lib/key-value/index.ts +++ b/src/backend/lib/key-value/index.ts @@ -2,32 +2,34 @@ import { AbstractConfigDataPersistenceService, createConfigDomainPersistenceService, } from "../config-persistence"; +import { KeyValueDomain } from "./types"; -interface IStorge { +interface IStorage { data: string; } -export class KeyValueStoreApiService { +export class KeyValueStoreApiService { constructor( - private readonly _keyValueStorePersistenceService: AbstractConfigDataPersistenceService + private readonly key: KeyValueDomain, + private readonly _keyValueStorePersistenceService: AbstractConfigDataPersistenceService ) {} async bootstrap() { await this._keyValueStorePersistenceService.setup(); } - async clearItem(key: string) { - await this._keyValueStorePersistenceService.removeItem(key); + async clearItem() { + await this._keyValueStorePersistenceService.removeItem(this.key); } - async persistItem(key: string, data: unknown) { - await this._keyValueStorePersistenceService.persistItem(key, { + async persistItem(data: T) { + await this._keyValueStorePersistenceService.persistItem(this.key, { data: JSON.stringify(data), }); } - async getItem(key: string): Promise { - const data = await this._keyValueStorePersistenceService.getItem(key); + async getItem(): Promise { + const data = await this._keyValueStorePersistenceService.getItem(this.key); if (!data) { return null; } @@ -36,8 +38,13 @@ export class KeyValueStoreApiService { } const keyValueStorePersistenceService = - createConfigDomainPersistenceService("key-value"); - -export const keyValueStoreApiService = new KeyValueStoreApiService( - keyValueStorePersistenceService -); + createConfigDomainPersistenceService("key-value"); + +export function createKeyValueDomainPersistenceService( + keyValueDomain: KeyValueDomain +) { + return new KeyValueStoreApiService( + keyValueDomain, + keyValueStorePersistenceService + ); +} diff --git a/src/backend/lib/key-value/key-value.spec.ts b/src/backend/lib/key-value/key-value.spec.ts index e4a10a553..fed68037c 100644 --- a/src/backend/lib/key-value/key-value.spec.ts +++ b/src/backend/lib/key-value/key-value.spec.ts @@ -1,26 +1,45 @@ -import { keyValueStoreApiService } from "."; +import { createKeyValueDomainPersistenceService } from "."; +import { KeyValueDomain } from "./types"; + +const keyValueStoreApiService = createKeyValueDomainPersistenceService< + Record +>("test-me" as KeyValueDomain); + +const unTouchedkeyValueStoreApiService = createKeyValueDomainPersistenceService< + Record +>("un-touched" as KeyValueDomain); describe("KeyValueStoreApiService", () => { - it("should return empty for none saved items", async () => { - expect(await keyValueStoreApiService.getItem("non-existent")).toBeNull(); - }); + it("should upsert data", async () => { + await keyValueStoreApiService.persistItem({ bar: "bar" }); + await unTouchedkeyValueStoreApiService.persistItem({ bar: "bar" }); + expect(await keyValueStoreApiService.getItem()).toEqual({ + bar: "bar", + }); - it("should persist items", async () => { - await keyValueStoreApiService.persistItem("persist", { bar: "baz" }); - expect(await keyValueStoreApiService.getItem("persist")).toEqual({ - bar: "baz", + expect(await unTouchedkeyValueStoreApiService.getItem()).toEqual({ + bar: "bar", + }); + + await keyValueStoreApiService.persistItem({ foo: "foo" }); + expect(await keyValueStoreApiService.getItem()).toEqual({ + foo: "foo", + }); + + expect(await unTouchedkeyValueStoreApiService.getItem()).toEqual({ + bar: "bar", }); }); it("should clear items", async () => { - await keyValueStoreApiService.persistItem("clear1", { bar1: "baz1" }); - await keyValueStoreApiService.persistItem("clear2", { bar2: "baz2" }); + await keyValueStoreApiService.persistItem({ bar2: "baz2" }); + + await keyValueStoreApiService.clearItem(); - await keyValueStoreApiService.clearItem("clear1"); + expect(await keyValueStoreApiService.getItem()).toBeNull(); - expect(await keyValueStoreApiService.getItem("clear1")).toBeNull(); - expect(await keyValueStoreApiService.getItem("clear2")).toEqual({ - bar2: "baz2", + expect(await unTouchedkeyValueStoreApiService.getItem()).toEqual({ + bar: "bar", }); }); }); diff --git a/src/backend/lib/key-value/portal/index.ts b/src/backend/lib/key-value/portal/index.ts new file mode 100644 index 000000000..a4f7a39eb --- /dev/null +++ b/src/backend/lib/key-value/portal/index.ts @@ -0,0 +1 @@ +export { type PortalKeyValueDomain } from "./main"; diff --git a/src/backend/lib/key-value/portal/main.ts b/src/backend/lib/key-value/portal/main.ts new file mode 100644 index 000000000..91aa75093 --- /dev/null +++ b/src/backend/lib/key-value/portal/main.ts @@ -0,0 +1 @@ +export type PortalKeyValueDomain = ""; diff --git a/src/backend/lib/key-value/types.ts b/src/backend/lib/key-value/types.ts new file mode 100644 index 000000000..3876774a3 --- /dev/null +++ b/src/backend/lib/key-value/types.ts @@ -0,0 +1,3 @@ +import { PortalKeyValueDomain } from "./portal"; + +export type KeyValueDomain = PortalKeyValueDomain;