@vytches/ddd-messaging 0.26.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,61 @@
1
+ import { IOutboxMessage, MessagePriority, MessageStatus } from './outbox-interfaces';
2
+ export declare abstract class IOutboxRepository {
3
+ /**
4
+ * Saves a message to the outbox
5
+ * @param message Message to save
6
+ * @returns ID of the saved message
7
+ */
8
+ abstract saveMessage<T = unknown>(message: IOutboxMessage<T>): Promise<string>;
9
+ /**
10
+ * Saves multiple messages to the outbox
11
+ * @param messages Messages to save
12
+ * @returns IDs of the saved messages
13
+ */
14
+ abstract saveBatch<T = unknown>(messages: IOutboxMessage<T>[]): Promise<string[]>;
15
+ /**
16
+ * Gets unprocessed messages from the outbox
17
+ * @param limit Maximum number of messages to retrieve
18
+ * @param priorityOrder Order of priority for processing (default: [CRITICAL, HIGH, NORMAL, LOW])
19
+ * @returns Unprocessed messages
20
+ */
21
+ abstract getUnprocessedMessages(limit?: number, priorityOrder?: MessagePriority[]): Promise<IOutboxMessage[]>;
22
+ /**
23
+ * Gets specific message by ID
24
+ * @param id Message ID
25
+ * @returns Message or null if not found
26
+ */
27
+ abstract getById(id: string): Promise<IOutboxMessage | null>;
28
+ /**
29
+ * Updates message status
30
+ * @param id Message ID
31
+ * @param status New status
32
+ * @param error Optional error information
33
+ */
34
+ abstract updateStatus(id: string, status: MessageStatus, error?: Error): Promise<void>;
35
+ /**
36
+ * Updates message status for multiple messages
37
+ * @param ids Message IDs
38
+ * @param status New status
39
+ */
40
+ abstract updateStatusBatch(ids: string[], status: MessageStatus): Promise<void>;
41
+ /**
42
+ * Increments attempt counter for a message
43
+ * @param id Message ID
44
+ * @returns Updated attempt count
45
+ */
46
+ abstract incrementAttempt(id: string): Promise<number>;
47
+ /**
48
+ * Deletes messages with specified status older than a specified date
49
+ * @param olderThan Date threshold
50
+ * @param status Status of messages to delete
51
+ * @returns Number of deleted messages
52
+ */
53
+ abstract deleteByStatusAndAge(olderThan: Date, status: MessageStatus): Promise<number>;
54
+ /**
55
+ * Schedules a message for delayed processing
56
+ * @param message Message to schedule
57
+ * @param processAfter When to process the message
58
+ * @returns ID of the saved message
59
+ */
60
+ abstract scheduleMessage<T = unknown>(message: IOutboxMessage<T>, processAfter: Date): Promise<string>;
61
+ }
@@ -0,0 +1,82 @@
1
+ import { IDomainEvent } from '../../../contracts/src/index.ts';
2
+ import { IOutboxMessage, OutboxMessageOptions, MessagePriority } from './outbox-interfaces';
3
+ import { IOutboxRepository } from './outbox-repository.interface';
4
+ export interface OutboxServiceOptions {
5
+ /** Default priority for messages */
6
+ defaultPriority?: MessagePriority;
7
+ /** Default metadata to add to all messages */
8
+ defaultMetadata?: Record<string, unknown>;
9
+ /** Enable automatic cleanup of processed messages */
10
+ autoCleanup?: boolean;
11
+ /** Age threshold for cleanup (in days) */
12
+ cleanupAfterDays?: number;
13
+ /** Enable logging */
14
+ enableLogging?: boolean;
15
+ }
16
+ export declare class OutboxService {
17
+ private readonly repository;
18
+ private readonly options;
19
+ private readonly logger;
20
+ constructor(repository: IOutboxRepository, options?: OutboxServiceOptions);
21
+ /**
22
+ * Saves a message to the outbox
23
+ */
24
+ saveMessage<T = unknown>(messageType: string, payload: T, options?: OutboxMessageOptions): Promise<string>;
25
+ /**
26
+ * Saves multiple messages to the outbox in a batch
27
+ */
28
+ saveMessages<T = unknown>(messages: Array<{
29
+ messageType: string;
30
+ payload: T;
31
+ options?: OutboxMessageOptions;
32
+ }>): Promise<string[]>;
33
+ /**
34
+ * Schedules a message for delayed processing
35
+ */
36
+ scheduleMessage<T = unknown>(messageType: string, payload: T, delayMs: number, options?: OutboxMessageOptions): Promise<string>;
37
+ /**
38
+ * Saves a high priority message to the outbox
39
+ */
40
+ saveHighPriorityMessage<T = unknown>(messageType: string, payload: T, options?: OutboxMessageOptions): Promise<string>;
41
+ /**
42
+ * Saves a critical priority message to the outbox
43
+ */
44
+ saveCriticalMessage<T = unknown>(messageType: string, payload: T, options?: OutboxMessageOptions): Promise<string>;
45
+ /**
46
+ * Converts a domain event to an outbox message and saves it
47
+ */
48
+ saveDomainEvent(event: IDomainEvent, options?: OutboxMessageOptions): Promise<string>;
49
+ /**
50
+ * Gets a message by ID
51
+ */
52
+ getMessage(id: string): Promise<IOutboxMessage | null>;
53
+ /**
54
+ * Gets pending messages (for monitoring purposes)
55
+ */
56
+ getPendingMessages(limit?: number): Promise<IOutboxMessage[]>;
57
+ /**
58
+ * Retries a failed message by resetting it to pending status
59
+ */
60
+ retryMessage(id: string): Promise<void>;
61
+ /**
62
+ * Retries multiple failed messages
63
+ */
64
+ retryMessages(ids: string[]): Promise<void>;
65
+ /**
66
+ * Cleans up old processed messages
67
+ */
68
+ cleanup(olderThanDays?: number): Promise<number>;
69
+ /**
70
+ * Gets outbox statistics
71
+ */
72
+ getStats(): Promise<{
73
+ totalPending: number;
74
+ totalProcessing: number;
75
+ totalProcessed: number;
76
+ totalFailed: number;
77
+ }>;
78
+ /**
79
+ * Logs messages if logging is enabled
80
+ */
81
+ private log;
82
+ }
package/package.json ADDED
@@ -0,0 +1,66 @@
1
+ {
2
+ "name": "@vytches/ddd-messaging",
3
+ "version": "0.26.0",
4
+ "description": "Outbox pattern and reliable message delivery",
5
+ "type": "module",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js",
13
+ "require": "./dist/index.cjs"
14
+ }
15
+ },
16
+ "sideEffects": false,
17
+ "files": [
18
+ "LLMGUIDE.md",
19
+ "README.md",
20
+ "dist"
21
+ ],
22
+ "keywords": [
23
+ "ddd",
24
+ "domain-driven-design",
25
+ "typescript",
26
+ "messaging"
27
+ ],
28
+ "author": "VytchesDDD",
29
+ "license": "MIT",
30
+ "repository": {
31
+ "type": "git",
32
+ "url": "https://github.com/vytches/ddd.git",
33
+ "directory": "packages/messaging"
34
+ },
35
+ "devDependencies": {
36
+ "typescript": "^5.0.0",
37
+ "vite": "^7.3.2",
38
+ "vite-plugin-dts": "^4.0.0",
39
+ "vitest": "^2.0.0",
40
+ "@vytches/ddd-testing": "0.26.0"
41
+ },
42
+ "nx": {
43
+ "tags": [
44
+ "scope:messaging",
45
+ "type:lib",
46
+ "layer:integration"
47
+ ]
48
+ },
49
+ "publishConfig": {
50
+ "registry": "https://registry.npmjs.org",
51
+ "access": "public"
52
+ },
53
+ "dependencies": {
54
+ "@vytches/ddd-events": "0.26.0"
55
+ },
56
+ "scripts": {
57
+ "build": "vite build",
58
+ "dev": "vite build --watch",
59
+ "clean": "rm -rf dist .tsbuildinfo",
60
+ "test": "vitest run",
61
+ "test:watch": "vitest",
62
+ "test:coverage": "vitest run --coverage",
63
+ "lint": "eslint src --ext .ts",
64
+ "type-check": "tsc --noEmit"
65
+ }
66
+ }