mirror of
https://github.com/kevin-DL/complete-node-bootcamp.git
synced 2026-01-13 03:35:30 +00:00
Initial commit 🚀
This commit is contained in:
52
4-natours/after-section-10/dev-data/data/import-dev-data.js
Normal file
52
4-natours/after-section-10/dev-data/data/import-dev-data.js
Normal file
@@ -0,0 +1,52 @@
|
||||
const fs = require('fs');
|
||||
const mongoose = require('mongoose');
|
||||
const dotenv = require('dotenv');
|
||||
const Tour = require('./../../models/tourModel');
|
||||
|
||||
dotenv.config({ path: './config.env' });
|
||||
|
||||
const DB = process.env.DATABASE.replace(
|
||||
'<PASSWORD>',
|
||||
process.env.DATABASE_PASSWORD
|
||||
);
|
||||
|
||||
mongoose
|
||||
.connect(DB, {
|
||||
useNewUrlParser: true,
|
||||
useCreateIndex: true,
|
||||
useFindAndModify: false
|
||||
})
|
||||
.then(() => console.log('DB connection successful!'));
|
||||
|
||||
// READ JSON FILE
|
||||
const tours = JSON.parse(
|
||||
fs.readFileSync(`${__dirname}/tours-simple.json`, 'utf-8')
|
||||
);
|
||||
|
||||
// IMPORT DATA INTO DB
|
||||
const importData = async () => {
|
||||
try {
|
||||
await Tour.create(tours);
|
||||
console.log('Data successfully loaded!');
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
}
|
||||
process.exit();
|
||||
};
|
||||
|
||||
// DELETE ALL DATA FROM DB
|
||||
const deleteData = async () => {
|
||||
try {
|
||||
await Tour.deleteMany();
|
||||
console.log('Data successfully deleted!');
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
}
|
||||
process.exit();
|
||||
};
|
||||
|
||||
if (process.argv[2] === '--import') {
|
||||
importData();
|
||||
} else if (process.argv[2] === '--delete') {
|
||||
deleteData();
|
||||
}
|
||||
Reference in New Issue
Block a user