40 lines
1.2 KiB
TypeScript
40 lines
1.2 KiB
TypeScript
import { ipcMain } from 'electron';
|
|
import { buildCommandHandlers } from '../cqrs/commands';
|
|
import { buildQueryHandlers } from '../cqrs/queries';
|
|
import {
|
|
Command,
|
|
Query,
|
|
CommandTypeKey,
|
|
QueryTypeKey
|
|
} from '../cqrs/types';
|
|
import { getDataSource } from '../db/database';
|
|
|
|
export function setupCqrsHandlers(): void {
|
|
const dataSource = getDataSource();
|
|
|
|
if (!dataSource) {
|
|
throw new Error('DataSource not initialised');
|
|
}
|
|
|
|
const commandHandlerMap = buildCommandHandlers(dataSource) as Record<CommandTypeKey, (command: Command) => unknown>;
|
|
const queryHandlerMap = buildQueryHandlers(dataSource) as Record<QueryTypeKey, (query: Query) => unknown>;
|
|
|
|
ipcMain.handle('cqrs:command', async (_evt, command: Command) => {
|
|
const handler = commandHandlerMap[command.type as CommandTypeKey];
|
|
|
|
if (!handler)
|
|
throw new Error(`No command handler for type: ${command.type}`);
|
|
|
|
return handler(command);
|
|
});
|
|
|
|
ipcMain.handle('cqrs:query', async (_evt, query: Query) => {
|
|
const handler = queryHandlerMap[query.type as QueryTypeKey];
|
|
|
|
if (!handler)
|
|
throw new Error(`No query handler for type: ${query.type}`);
|
|
|
|
return handler(query);
|
|
});
|
|
}
|