-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe27-queue-consumer.js
More file actions
97 lines (87 loc) · 2.69 KB
/
Copy pathe27-queue-consumer.js
File metadata and controls
97 lines (87 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import Database from '../src/database.class.js';
import Client from '../src/client.class.js';
import { QueueConsumerPlugin } from '../src/plugins/queue-consumer.plugin.js';
// Educational example: simulates SQS queue consumption
async function main() {
// Inicializa database
const client = new Client({
connectionString: 's3db://minio:password@localhost:9000/s3db-test-queue-consumer'
});
const database = new Database({ client });
await database.connect();
// Cria resource
const users = await database.createResource({
name: 'users',
attributes: {
id: 'string|required',
name: 'string|required',
email: 'string|required'
}
});
const plugin = new QueueConsumerPlugin({
enabled: true,
consumers: [
{
driver: 'sqs',
resources: ['users', 'admins'],
config: {
queueUrl: 'https://sqs.us-east-1.amazonaws.com/123456789012/my-queue',
region: 'us-east-1',
credentials: { accessKeyId: '...', secretAccessKey: '...' },
poolingInterval: 1000,
maxMessages: 10,
}
},
{
driver: 'rabbitmq',
resources: 'orders',
config: {
amqpUrl: 'amqp://user:pass@localhost:5672',
queue: 'orders-queue',
prefetch: 10,
reconnectInterval: 2000,
}
}
]
});
// await plugin.setup(database);
// await plugin.start();
// Simulate message reception (in production, would come from SQS)
// Aqui chamamos o handler diretamente para demonstrar
await plugin._handleMessage({
$body: { resource: 'users', action: 'insert', data: { id: 'u1', name: 'Alice', email: 'alice@example.com' } },
$attributes: {},
$raw: {}
}, 'users');
const user = await users.get('u1');
console.log('User inserted via SQS consumer:', user);
await plugin.stop();
await database.disconnect();
}
// --- Example: SQS Consumer ---
import { QueueConsumerPlugin } from '../src/plugins/queue-consumer.plugin.js';
const sqsPlugin = new QueueConsumerPlugin({
driver: 'sqs',
queues: { users: 'https://sqs.us-east-1.amazonaws.com/123456789012/my-queue' },
driverOptions: {
region: 'us-east-1',
credentials: { accessKeyId: '...', secretAccessKey: '...' },
poolingInterval: 1000,
maxMessages: 10,
}
});
// await sqsPlugin.setup(database);
// await sqsPlugin.start();
// --- Example: RabbitMQ Consumer ---
const rabbitPlugin = new QueueConsumerPlugin({
driver: 'rabbitmq',
queues: { users: 'users-queue' },
driverOptions: {
amqpUrl: 'amqp://user:pass@localhost:5672',
prefetch: 10,
reconnectInterval: 2000,
}
});
// await rabbitPlugin.setup(database);
// await rabbitPlugin.start();
main().catch(console.error);