Created
September 7, 2022 23:46
-
-
Save Phasor/a086953668af2212619c777667da3369 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const Post = require('../models/post'); | |
| const csv = require('fast-csv'); // parses CSV files | |
| const fs = require('fs'); | |
| const path = require('path'); | |
| const mongoose = require('mongoose'); | |
| const dotenv = require('dotenv'); | |
| dotenv.config(); | |
| const main = async () => { | |
| // connect to db | |
| const mongoDB = process.env.MONGODB_URI; | |
| mongoose.connect(mongoDB, { useNewUrlParser: true, useUnifiedTopology: true }); | |
| const db = mongoose.connection; | |
| db.on('error', console.error.bind(console, 'MongoDB connection error:')); | |
| // read csv file | |
| const posts = []; | |
| fs.createReadStream(path.join(__dirname, '/posts.csv')) | |
| .pipe(csv.parse({ headers: true })) | |
| .on('error', error => console.error(error)) | |
| .on('data', function(data) { | |
| data['_id'] = new mongoose.Types.ObjectId(); | |
| data['author'] = new mongoose.Types.ObjectId(data.author); | |
| data['date'] = Date.now(); | |
| data['published'] = Boolean(data.published); | |
| posts.push(data); | |
| }) | |
| .on('end', function(){ | |
| // insert posts into db | |
| Post.insertMany(posts, function(err, documents) { | |
| if (err) { | |
| console.log(err); | |
| } | |
| }); | |
| console.log(`${posts.length} + posts have been successfully uploaded.`); | |
| return; | |
| }); | |
| } | |
| main().catch((error) => { | |
| console.error(error); | |
| process.exit(); | |
| }); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment