-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendtomongo.js
58 lines (53 loc) · 1.75 KB
/
sendtomongo.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
'use strict';
require('dotenv').config()
const fs = require('fs');
const MongoClient = require('mongodb').MongoClient
const logger = require('./logger');
async function deleteFutureCourses(mongoUri, dbName, collectionName) {
const client = await MongoClient.connect(mongoUri);
const dbo = client.db(dbName);
const col = dbo.collection(collectionName);
const query = {start: {$gt: new Date()}}
await col.deleteMany(query);
logger.info('Done deleting');
}
async function upsertCourses(mongoUri, dbName, collectionName, coursesFile) {
const client = await MongoClient.connect(mongoUri);
const dbo = client.db(dbName);
const col = dbo.collection(collectionName);
let data = fs.readFileSync(coursesFile, 'utf8');
const courses = JSON.parse(data);
const ops = courses.map(x => {
return {
updateOne: {
filter: {name:x.name, start: new Date(x.start), studio: x.studio, postcode: x.postcode, timezone: x.timezone},
update: {$set: {
name: x.name,
teacher: x.teacher,
room: x.room,
style: x.style,
start: new Date(x.start),
end: new Date(x.end),
locale: x.locale,
studio: x.studio,
url: x.url,
booking: x.booking,
postcode: x.postcode,
timezone: x.timezone,
}},
upsert:true}
};
});
await col.bulkWrite(ops, {ordered: false})
logger.info('Done writing');
}
async function start() {
await deleteFutureCourses(process.env.MONGO_URI, process.env.DB_NAME, process.env.COLLECTION);
await upsertCourses(process.env.MONGO_URI, process.env.DB_NAME, process.env.COLLECTION, process.argv[2]);
}
if (require.main === module) {
start().then(() => {
logger.info("All done");
process.exit(0);
});
}