1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-11-01 19:07:38 +01:00
unleash.unleash/src/lib/services/setting-service.ts

76 lines
2.1 KiB
TypeScript
Raw Normal View History

2021-04-22 22:54:08 +02:00
import { IUnleashConfig } from '../types/option';
import { IUnleashStores } from '../types/stores';
import { Logger } from '../logger';
import { ISettingStore } from '../types/stores/settings-store';
import { IEventStore } from '../types/stores/event-store';
import {
SettingCreatedEvent,
SettingDeletedEvent,
SettingUpdatedEvent,
} from '../types/events';
2021-04-22 22:54:08 +02:00
export default class SettingService {
private config: IUnleashConfig;
2021-04-22 22:54:08 +02:00
private logger: Logger;
private settingStore: ISettingStore;
2021-04-22 22:54:08 +02:00
private eventStore: IEventStore;
2021-04-22 22:54:08 +02:00
constructor(
{
settingStore,
eventStore,
}: Pick<IUnleashStores, 'settingStore' | 'eventStore'>,
config: IUnleashConfig,
2021-04-22 22:54:08 +02:00
) {
this.config = config;
this.logger = config.getLogger('services/setting-service.ts');
2021-04-22 22:54:08 +02:00
this.settingStore = settingStore;
this.eventStore = eventStore;
2021-04-22 22:54:08 +02:00
}
async get<T>(id: string, defaultValue?: T): Promise<T> {
const value = await this.settingStore.get(id);
return value || defaultValue;
2021-04-22 22:54:08 +02:00
}
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.eventStore.store(
new SettingUpdatedEvent({
createdBy,
data: { id },
}),
);
} else {
await this.settingStore.insert(id, value);
await this.eventStore.store(
new SettingCreatedEvent({
createdBy,
data: { id },
}),
);
}
2021-04-22 22:54:08 +02:00
}
async delete(id: string, createdBy: string): Promise<void> {
await this.settingStore.delete(id);
await this.eventStore.store(
new SettingDeletedEvent({
createdBy,
data: {
id,
},
}),
);
}
async deleteAll(): Promise<void> {
await this.settingStore.deleteAll();
}
2021-04-22 22:54:08 +02:00
}