forked from jschirrmacher/netvis-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabaseReader.js
32 lines (27 loc) · 1.05 KB
/
databaseReader.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
/* eslint-env node */
const fetch = require('node-fetch')
const logger = console
const database = require('./MongoDB')({logger})
async function readData(dbUrl, client) {
const db = await database(dbUrl, client)
const RoomReader = require('./RoomInfoReader')({db, client, logger})
const roomsWithTopics = await RoomReader.getRoomsWithTopics()
// const UserReader = require('./UserInfoReader')({db, client, logger})
// const persons = await UserReader.getConnectedPersons()
const headers = {'content-type': 'application/json'}
await Promise.all(roomsWithTopics.map(async room => {
const body = JSON.stringify(room)
const result = await fetch('http://localhost:3000/rooms', {method: 'post', body, headers})
if (!result.ok) {
throw {errorCode: result.status, message: await result.json()}
}
}))
}
const client = process.argv[2]
if (client) {
readData('mongodb://localhost:27017', client)
.then(() => logger.info('Data loaded'))
.catch(logger.error)
} else {
logger.error(`\nUsage:\n\tnode databaseReader <clientName>\n`)
}