-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
68 lines (54 loc) · 1.84 KB
/
main.js
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
const { MeiliSearch } = require('meilisearch')
const { Kafka } = require('kafkajs')
const kafka = new Kafka({
sasl: {
mechanism: 'scram-sha-256',
username: 'USERNAME',
password: 'PASSWORD',
},
ssl: true,
brokers: ['BROKER1']
});
const consumer = kafka.consumer({ groupId: '$GROUP_NAME' });
const client = new MeiliSearch({
host: 'HOST',
apiKey: 'KEY'
});
const run = async() => {
await consumer.connect();
await consumer.subscribe({topic: 'TOPIC'});
await consumer.run({
eachMessage: async({topic, partition, message}) => {
const parsedValue = JSON.parse(message.value);
console.log({
partition,
offset: message.offset,
value: parsedValue,
});
if (parsedValue !== null) {
const operation = parsedValue.payload.op;
const table = parsedValue.payload.source.table;
const before = parsedValue.payload.before;
const after = parsedValue.payload.after;
const index = client.index(table);
// assume that pk column name is always id
switch (operation) {
case 'c':
const res = await index.addDocuments([after]);
console.log(res);
break;
case 'u':
const updRes = await index.addDocuments([after]);
console.log(updRes);
break;
case 'd':
const delRes = await index.deleteDocument(before.id);
console.log(delRes);
break;
default:
}
}
}
});
}
run().catch(console.error);