forked from harisarang/typesense-mongodb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
113 lines (104 loc) · 3.36 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
const { MongoClient } = require('mongodb');
const Typesense = require('typesense');
async function listDatabases(client){
databasesList = await client.db().admin().listDatabases();
console.log("Databases:");
databasesList.databases.forEach(db => console.log(` - ${db.name}`));
};
function closeChangeStream(timeInMs = 60000, changeStream) {
return new Promise((resolve) => {
setTimeout(() => {
console.log("Closing the change stream");
changeStream.close();
resolve();
}, timeInMs)
})
};
async function index(next, typesense){
console.log(next);
if(next.operationType == 'delete') {
await typesense.collections('books').documents(next.documentKey._id).delete();
console.log(next.documentKey._id);
} else if(next.operationType == 'update') {
let data = JSON.stringify(next.updateDescription.updatedFields);
await typesense.collections('books').documents(next.documentKey._id).update(data);
console.log(data);
} else {
next.fullDocument.id = next.fullDocument["_id"];
delete next.fullDocument._id;
let data = JSON.stringify(next.fullDocument);
await typesense.collections('books').documents().upsert(data);
console.log(data);
}
}
async function monitorListingsUsingEventEmitter(client, typesense, timeInMs = 60000, pipeline = []){
const collection = client.db("sample").collection("books");
const changeStream = collection.watch(pipeline);
changeStream.on('change', (next) => {
index(next, typesense);
});
await closeChangeStream(timeInMs, changeStream);
}
async function createSchema(schema, typesense) {
const collectionsList = await typesense.collections().retrieve();
var toCreate = collectionsList.find((value, index, array) => {
return value["name"] == schema["name"];
});
if(!toCreate){
await typesense.collections().create(schema);
}
}
async function main() {
const typesense = new Typesense.Client({
'nodes': [{
'host': 'localhost',
'port': '8108',
'protocol': 'http'
}],
'apiKey': 'hari',
'connectionTimeoutSeconds': 2
});
let schema = {
'name': 'books',
'fields': [
{
'name': 'id',
'type': 'string',
'facet': false
},
{
'name': 'name',
'type': 'string',
'facet': false
},
{
'name': 'author',
'type': 'string',
'facet': false
},
{
'name': 'year',
'type': 'int32',
'facet': true
},
],
'default_sorting_field': 'year',
}
createSchema(schema, typesense);
const mongodbOptions = {
useNewUrlParser: true,
useUnifiedTopology: true
}
const uri = 'mongodb+srv://anbalagang:[email protected]/keydemand';
const client = new MongoClient(uri, mongodbOptions);
try {
await client.connect();
await listDatabases(client);
await monitorListingsUsingEventEmitter(client, typesense);
} catch(e) {
console.error(e);
} finally {
await client.close();
}
}
main().catch(console.error);