1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-06 00:07:44 +01:00
unleash.unleash/src/lib/services/setting-service.ts
Gastón Fournier b1ea2c3b88
chore: expose instanceId so it can be used from addons (#5231)
## About the changes
A bit of boy scouting trying to expose the instanceId
2023-10-31 12:38:21 +01:00

82 lines
2.3 KiB
TypeScript

import { IUnleashConfig } from '../types/option';
import { IUnleashStores } from '../types/stores';
import { Logger } from '../logger';
import { ISettingStore } from '../types/stores/settings-store';
import {
SettingCreatedEvent,
SettingDeletedEvent,
SettingUpdatedEvent,
} from '../types/events';
import EventService from './event-service';
export default class SettingService {
private config: IUnleashConfig;
private logger: Logger;
private settingStore: ISettingStore;
private eventService: EventService;
constructor(
{ settingStore }: Pick<IUnleashStores, 'settingStore'>,
config: IUnleashConfig,
eventService: EventService,
) {
this.config = config;
this.logger = config.getLogger('services/setting-service.ts');
this.settingStore = settingStore;
this.eventService = eventService;
}
/**
* @deprecated use getWithDefault instead
*/
async get<T>(id: string, defaultValue?: T): Promise<T | undefined> {
const value = await this.settingStore.get<T>(id);
return value || defaultValue;
}
async getWithDefault<T>(id: string, defaultValue: T): Promise<T> {
const value = await this.settingStore.get<T>(id);
return value || defaultValue;
}
async insert(id: string, value: object, createdBy: string): Promise<void> {
const exists = await this.settingStore.exists(id);
if (exists) {
await this.settingStore.updateRow(id, value);
await this.eventService.storeEvent(
new SettingUpdatedEvent({
createdBy,
data: { id },
}),
);
} else {
await this.settingStore.insert(id, value);
await this.eventService.storeEvent(
new SettingCreatedEvent({
createdBy,
data: { id },
}),
);
}
}
async delete(id: string, createdBy: string): Promise<void> {
await this.settingStore.delete(id);
await this.eventService.storeEvent(
new SettingDeletedEvent({
createdBy,
data: {
id,
},
}),
);
}
async deleteAll(): Promise<void> {
await this.settingStore.deleteAll();
}
}