-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
129 lines (119 loc) · 3.08 KB
/
index.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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const { MongoClient } = require("mongodb");
// Replace the uri string with your MongoDB deployment's connection string.
let elkClient = null;
let client = null;
async function getMongoClient(uri) {
return new MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
}
function getElasticConnection(url) {
var elasticsearch = require('elasticsearch');
var elkClient = new elasticsearch.Client({
host: url,
log: 'trace'
});
return elkClient;
}
async function watch(db, collectoinToWatch) {
try {
const database = client.db(db);
const collection = database.collection(collectoinToWatch);
return collection.watch({fullDoucment: 'updateLookup'})
} catch (err) {
console.error(err)
}
}
async function watchCollections(collections, mongoUrl) {
let watcher = [];
client = client ? client : await getMongoClient(mongoUrl);
await client.connect();
for await (collection of collections) {
watcher.push({
watcher: await watch(collection.db , collection.name),
query: collection.query,
index: collection.index,
docType: collection.docType,
id: collection.id,
url: '',
func: collection.func
});
}
watcher.forEach(each => {
each && each.watcher ? each.watcher.on('change', next => {
reStructure(next, each.query, each.index, each.docType, each.id, each.func, each.elkUrl);
console.log(next, 'k')
}) : null;
})
}
async function reStructure(document, query = null, index = null, docType, id = null, func = null,elkUrl = '') {
try {
const fullDoucment = document.fullDocument ? document.fullDocument : document.documentKey;
if (!id) {
id = Object.keys(document.documentKey)[0];
}
if (typeof fullDoucment[id] !== 'string') {
fullDoucment[id] = fullDoucment[id].toString();
}
const obj = {
index: index,
type: docType,
id: fullDoucment[id],
body: fullDoucment
};
if (index && elkUrl) {
elkClient = elkClient ? elkClient : getElasticConnection(elkUrl);
try {
const response = await elkClient.update(obj);
console.log(response, '')
} catch (error) {
console.error('elastic indexing error', JSON.stringify(error));
}
}
if (func && func.length > 0) {
func.forEach(each => {
each(obj)
})
}
} catch (error) {
console.error('got error while restructuring document', JSON.stringify(error));
}
}
// or bundled together in an object
module.exports = {
watchCollections
};
watchCollections([{
name: 'changeDetactions',
elkUrl: '',
index: '',
query: null,
docType: null,
id: '_id',
db: 'watcher',
func: [callIt, donot]
},
{
name: 'changes',
elkUrl: '',
index: '',
query: null,
docType: null,
id: '_id',
db: 'watcher',
func: [callIt, donot]
}], 'mongodb://127.0.0.1:27017')
function callIt(doc) {
console.log(doc, 'kk')
}
function donot(doc) {
console.log(doc, 'kk')
}
// three functions
// watch db and collections
// create a watchers array
// listen to each watch
// store it some where
// reformat it
// index it in elastic