pg-tbus
v1.0.0
Published
End-to-end typesafe tasks and integration events on postgres made easy.
Downloads
108
Maintainers
Readme
PG-TBus
End-to-end typesafe tasks and integration events on postgres made easy.
Usage
- Define integration events
const account_created_event = defineEvent({
event_name: 'account_created',
schema: Type.Object({
account_id: Type.String({ format: 'uuid' }),
}),
});
- Initialize pg-tbus
const bus = createTBus('my_service', { db: { connectionString: connectionString }, schema: schema });
- Register eventHandler
bus.registerHandler(
createEventHandler({
// should be unique for this bus handler
task_name: 'send_email',
eventDef: account_created_event,
handler: async (props) => {
// do something with the data
},
})
);
- Start the pg-tbus
bus.start();
- Emit the event
await bus.publish(account_created_event.from({ account_id: '1234' }));
Tasks
- Define task(s)
const send_email_task = defineTask({
task_name: 'send_email',
queue: 'email_svc',
schema: Type.Object({ email: Type.String() }),
});
- Register task
bus.registerTask(
createTaskHandler({
taskDef: send_email_task,
handler: async (props) => {
//send email to props.input.email
},
})
);
- Create task from somewhere else
await bus.send(send_email_task.from({ email: '[email protected]' }));
For more usage, see tests/bus.ts