-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
266f538
commit 135c888
Showing
27 changed files
with
2,593 additions
and
36 deletions.
There are no files selected for viewing
Empty file.
Empty file.
Empty file.
This file contains 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
Empty file.
Empty file.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,52 @@ | ||
'use strict'; | ||
"use strict"; | ||
|
||
const fs = require('fs'); | ||
const fs = require("fs"); | ||
const fsPromises = fs.promises; | ||
|
||
const mongoose = require('mongoose'); | ||
const conn = require('./lib/connectDB'); | ||
const Anuncio = require('./models/Anuncio'); | ||
const mongoose = require("mongoose"); | ||
const conn = require("./lib/connectDB"); | ||
const Anuncio = require("./models/Anuncio"); | ||
|
||
const file = './data/anuncios.json'; | ||
const data = JSON.parse(fs.readFileSync(file,'utf-8')); | ||
const file = "./data/anuncios.json"; | ||
const data = JSON.parse(fs.readFileSync(file,"utf-8")); | ||
|
||
// conectar | ||
mongoose.connect('mongodb://localhost/nodepop' , { useNewUrlParser: true, useUnifiedTopology: true }); | ||
mongoose.connect("mongodb://127.0.0.1/nodepop" , { useNewUrlParser: true, useUnifiedTopology: true }); | ||
|
||
|
||
async function cleanAgentes() { | ||
try { | ||
const resDel = await Anuncio.deleteMany({}); | ||
console.log('Base de datos borrada!', resDel.ok, resDel.deletedCount); | ||
console.log("Base de datos borrada!", resDel.ok, resDel.deletedCount); | ||
} catch (err) { | ||
console.log('Erro al limpiar Anuncios', err); | ||
console.log("Erro al limpiar Anuncios", err); | ||
|
||
} | ||
} | ||
|
||
async function loadAnuncios() { | ||
try { | ||
await Anuncio.insertMany(data.anuncios); | ||
console.log('Datos de anuncios cargados.!'); | ||
console.log("Datos de anuncios cargados.!"); | ||
} catch (err) { | ||
console.log(`Error al cargar archivo ${file}, >>>> ${err}`); | ||
process.exit(); | ||
} | ||
} | ||
|
||
// gestionar eventos de conexión | ||
conn.on('error', err => { | ||
console.log('Error de conexión', err); | ||
conn.on("error", err => { | ||
console.log("Error de conexión", err); | ||
process.exit(1); | ||
}); | ||
|
||
// Proceso de Inicialización Base de datos una vez conectado a mongodb | ||
conn.once('open', async () => { | ||
console.log('Conectado a MongoDB en ', mongoose.connection.name); | ||
console.log('Limpiando Base de datos..!'); | ||
conn.once("open", async () => { | ||
console.log("Conectado a MongoDB en ", mongoose.connection.name); | ||
console.log("Limpiando Base de datos..!"); | ||
await cleanAgentes(); | ||
console.log('Cargando anuncios.json!'); | ||
console.log("Cargando anuncios.json!"); | ||
await loadAnuncios(); | ||
console.log('Terminado..!'); | ||
console.log("Terminado..!"); | ||
process.exit(); | ||
}); |
This file contains 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
Empty file.
Oops, something went wrong.