import { Result } from "@webiny/feature/api"; import { KeyValueStore as ServiceAbstraction } from "./abstractions.js"; import { GlobalKeyValueStore } from "./abstractions.js"; import type { IKeyValueSetOptions } from "./abstractions.js"; import type { IKeyValueDeleteOptions } from "./abstractions.js"; import type { IKeyValueListParams } from "./abstractions.js"; import { TenantContext } from "~/features/tenancy/TenantContext/index.js"; class KeyValueStoreImpl implements ServiceAbstraction.Interface { constructor( private tenantContext: TenantContext.Interface, private globalStore: GlobalKeyValueStore.Interface ) {} async get(key: string): Promise> { const tenant = this.tenantContext.getTenant(); return this.globalStore.get(key, { scope: tenant.id }); } async set( key: string, value: any, options?: IKeyValueSetOptions ): Promise> { const tenant = this.tenantContext.getTenant(); return this.globalStore.set(key, value, { scope: tenant.id, collection: options?.collection, condition: options?.condition }); } async delete( key: string, options?: IKeyValueDeleteOptions ): Promise> { const tenant = this.tenantContext.getTenant(); return this.globalStore.delete(key, { scope: tenant.id, condition: options?.condition }); } async list( params: IKeyValueListParams ): Promise, ServiceAbstraction.Error>> { const tenant = this.tenantContext.getTenant(); return this.globalStore.list({ ...params, scope: tenant.id }); } } export const KeyValueStore = ServiceAbstraction.createImplementation({ implementation: KeyValueStoreImpl, dependencies: [TenantContext, GlobalKeyValueStore] });