Skip to content

End-to-end typesafe tasks and integration events on postgres made easy.

License

Notifications You must be signed in to change notification settings

ilijaNL/pg-tbus

Repository files navigation

PG-TBus

End-to-end typesafe tasks and integration events on postgres made easy.

codecov

Usage

  1. Define integration events
const account_created_event = defineEvent({
  event_name: 'account_created',
  schema: Type.Object({
    account_id: Type.String({ format: 'uuid' }),
  }),
});
  1. Initialize pg-tbus
const bus = createTBus('my_service', { db: { connectionString: connectionString }, schema: schema });
  1. 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
    },
  })
);
  1. Start the pg-tbus
bus.start();
  1. Emit the event
await bus.publish(account_created_event.from({ account_id: '1234' }));

Tasks

  1. Define task(s)
const send_email_task = defineTask({
  task_name: 'send_email',
  queue: 'email_svc',
  schema: Type.Object({ email: Type.String() }),
});
  1. Register task
bus.registerTask(
  createTaskHandler({
    taskDef: send_email_task,
    handler: async (props) => {
      //send email to props.input.email
    },
  })
);
  1. Create task from somewhere else
await bus.send(send_email_task.from({ email: '[email protected]' }));

For more usage, see tests/bus.ts