I am using docker to containerize a Flask API. The Flask API is accessing MongoDB using the "links" keyword in the docker-compose.yml file shown below:
app:
build: .
command: python -u app.py
ports:
- "5000:5000"
volumes:
- .:/app
#Linking db.
links:
- db
db:
image: mongo:latest
hostname: test_mongodb
environment:
- MONGO_INITDB_DATABASE=Exploit_Resources
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=pass
# Data provider.
volumes:
- ./init-db.js:/docker-entrypoint-initdb.d/init-db.js:ro
ports:
- 27017:27017
The link is working fine, and the file given under volumes (init-db.js) is supposed to provide the Mongo container with data. Here is my init-db.js file:
db = db.getSiblingDB("Exploit_Resources");
db.EDB.drop();
db.EDB.insertMany([
// Insert data here.
]);
The data I want to provide MongoDB comes from a remote csv file. I tried using PapaParse to access and feed the data, but I was having some difficulties importing it.
I tried this code inside the insertMany method:
const {StringStream} = require("scramjet");
const request = require("request");
request.get("https://srv.example.com/main.csv") // fetch csv
.pipe(new StringStream()) // pass to stream
.CSVParse() // parse into objects
.consume(object => console.log("Row:", object)) // do whatever you like with the objects
.then(() => console.log("all done"))
I got the code from here. It did not work because a ReferenceError was thrown, saying that 'require' is not defined.
Is there a way to fix this code or another way to get data from a remote csv file and provide it to the MongoDB container from the init-db.js?