@@ -6,7 +6,7 @@ The `@nextorders/queue` is a TypeScript library designed to simplify working wit
66
77![ RabbitMQ Dashboard] ( https://github.com/user-attachments/assets/b330e911-bc74-404d-9999-c720b04ca9f7 )
88
9- ## Key Features
9+ ## 😨 Key Features
1010
1111- Type-safe operations with message queues
1212- Automatic connection to the RabbitMQ server
@@ -15,97 +15,165 @@ The `@nextorders/queue` is a TypeScript library designed to simplify working wit
1515- Support for various message types
1616- Flexible connection configuration
1717
18- ## Installation
18+ ## 📦 Installation
1919
2020You can install the library via npm:
2121
2222``` bash
2323npm install @nextorders/queue
2424```
2525
26- ## Usage
26+ ## 🚀 Usage
2727
28- Prepare types:
28+ ### 1. Define Event Types
29+
30+ Create type definitions for your events:
2931
3032``` typescript
3133import type { BaseEventMessage } from ' @nextorders/queue'
3234
3335export enum Events {
34- TICKET_MESSAGE_CREATED = ' ticketMessageCreated ' ,
35- OTHER_ACTION = ' otherAction ' ,
36+ UserCreated = ' userCreated ' ,
37+ EmailSent = ' emailSent ' ,
3638}
3739
38- export type EventMessage = BaseEventMessage <Events >
40+ export type EventMessage = UserCreated | EmailSent
41+
42+ export interface UserCreated extends BaseEventMessage {
43+ event: typeof Events .UserCreated
44+ data: {
45+ id: string
46+ name: string
47+ email: string
48+ }
49+ }
3950
40- export interface TicketMessageCreated extends EventMessage {
41- type : typeof Events .TICKET_MESSAGE_CREATED
51+ export interface EmailSent extends BaseEventMessage {
52+ event : typeof Events .EmailSent
4253 data: {
43- ticketId: string
44- ticketOwnerId: string
45- messageId: string
46- userId: string
47- userName: string
48- userSurname: string | undefined
49- userText: string
54+ email: string
5055 }
5156}
5257```
5358
54- Create Entities:
59+ ### 2. Create Entities
60+
61+ Define entities that represent your services:
5562
5663``` typescript
5764import { Entity , Repository } from ' @nextorders/queue'
65+ import { Events } from ' ./types'
66+
67+ export class User extends Entity {
68+ constructor (repository : Repository ) {
69+ super ({
70+ name: ' user' ,
71+ eventsToConsume: [],
72+ repository ,
73+ })
74+ }
75+ }
5876
59- export class Telegram extends Entity {
77+ export class Email extends Entity {
6078 constructor (repository : Repository ) {
6179 super ({
62- name: ' telegram ' ,
63- eventsToConsume: [' ticketMessageCreated ' ],
80+ name: ' email ' ,
81+ eventsToConsume: [Events . UserCreated ],
6482 repository ,
6583 })
6684 }
6785}
6886```
6987
70- Create Repository with Entities:
88+ ### 3. Create Repository
89+
90+ Create a repository that manages your entities:
7191
7292``` typescript
93+ import type { EventMessage } from ' ./types'
7394import { Repository } from ' @nextorders/queue'
74- import { Telegram , Ticket } from ' ./entities'
95+ import { Email , User } from ' ./entities'
7596
7697class QueueRepository extends Repository {
77- telegram: Telegram = new Telegram (this )
78- ticket: Ticket = new Ticket (this )
98+ user: User = new User (this )
99+ email: Email = new Email (this )
100+
101+ // Override publish method with proper typing
102+ publish<T extends EventMessage >(
103+ event : T [' event' ],
104+ data : T [' data' ]
105+ ): Promise <void > {
106+ return super .publish (event , data )
107+ }
79108}
80109
81110export const repository = new QueueRepository ()
82111```
83112
84- On server start (for example in Nuxt server plugin):
113+ ### 4. Connect to RabbitMQ
114+
115+ On service start, connect to your RabbitMQ instance:
85116
86117``` typescript
87- await repository .connect (process .env .QUEUE_URL )
118+ import { repository } from ' ./repository'
119+
120+ await repository .connect (' amqp://guest:guest@localhost:5672' )
121+ ```
122+
123+ ### 5. Publish Events
124+
125+ Create and publish events from your services:
126+
127+ ``` typescript
128+ await repository .publish (Events .UserCreated , {
129+ id: newUser .id ,
130+ name: newUser .name ,
131+ email: newUser .email ,
132+ })
88133```
89134
90- And thats it! Use repository in app:
135+ ### 6. Consume Events
136+
137+ Subscribe to events and handle them:
91138
92139``` typescript
93- // Publisher
94- await repository .publisher .send ({
95- exchange: repository .exchanges .events .exchange ,
96- routingKey: Events .TICKET_MESSAGE_CREATED ,
97- }, body )
98-
99- // Consumer
100- await repository .telegram .consume (async (msg ) => {
101- if (msg .type === ' ticketMessageCreated' ) {
102- return handleTicketMessageCreated (msg ) // ack on finish
140+ import type { UserCreated } from ' ./types'
141+ import { repository } from ' ./repository'
142+ import { Events } from ' ./types'
143+
144+ // Define event handlers
145+ async function handleUserCreated(data : UserCreated [' data' ]): Promise <boolean > {
146+ try {
147+ await sendEmail (data .email )
148+ return true
149+ } catch (error ) {
150+ console .error (' Error handling UserCreated event:' , error )
151+ return false
103152 }
153+ }
154+
155+ async function sendEmail(email : string ): Promise <void > {
156+ console .warn (' Sending email to:' , email )
157+
158+ // Publish EmailSent event
159+ await repository .publish (Events .EmailSent , { email })
160+ }
104161
105- return queue .ignore ()
162+ // Subscribe to events
163+ await repository .consume (repository .email .name , {
164+ [Events .UserCreated ]: handleUserCreated ,
106165})
107166```
108167
109- ## License
168+ ## 💁♂️ Example: Microservices Architecture
169+
170+ Check out the examples/microservices directory for a complete working example with:
171+
172+ - Service 1: User creation service
173+ - Service 2: Email notification service
174+ - Shared repository with entities
175+ - Type-safe event definitions
176+
177+ ## 🤝 License
110178
111179This project is licensed under the MIT License.
0 commit comments