Skip to main content

Real-time Entity Decorators

The @sseNotify decorator provides a declarative way to trigger real-time notifications on any service or store method. It is highly customizable and decouples your business logic from the notification system.

Basic Usage

Apply the decorator to a method. After the method executes successfully, an SSE broadcast is triggered.

[!IMPORTANT] This decorator uses the TC39 Stage 3 syntax and requires TypeScript 5.x+. For legacy environments or older frameworks, use manual notification via SseNotifyRegistry.notify().

import { sseNotify } from 'awesome-node-auth';

class ProductService {
@sseNotify({
topic: 'products',
event: 'product.created',
})
async createProduct(data: any) {
return await db.products.insert(data);
}
}

Advanced Customization

You can resolve topics, events, and payloads dynamically based on the method arguments and the result.

Dynamic Topic & Payload

class UserService {
@sseNotify({
// Resolve topic from the return value (user object)
topic: (args, user) => `user:${user.id}`,

// Custom event name
event: 'profile.updated',

// Transform the payload before sending
payload: (user) => ({
id: user.id,
username: user.username,
updatedAt: user.updatedAt
}),

// Optional tenancy and user context
tenantId: (user) => user.tenantId,
userId: (user) => user.id,
})
async updateProfile(userId: string, data: any) {
return await db.users.update(userId, data);
}
}

How it works

The decorator uses a global SseNotifyRegistry. When you initialize AuthTools with SSE enabled, it automatically registers its SseManager to this registry.

// During app boot
const tools = new AuthTools(bus, { sse: true });
// Now all @sseNotify decorators are active!

Benefits

  1. Cleaner Code: No need to manually call tools.notify() inside every method.
  2. Type-Safe: Use method arguments and result types in your resolver functions.
  3. Flexible: Works on any class method, not just database stores.