-
Notifications
You must be signed in to change notification settings - Fork 0
/
initdb.js
38 lines (32 loc) · 1.07 KB
/
initdb.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
33
34
35
36
37
38
"use strict";
require("dotenv").config();
const mongoose = require("mongoose");
require("./lib/connectMongoose");
const Advert = require("./models/Advert");
const User = require("./models/User");
const initialData = require("./advertsSample.json");
main().catch((err) => console.log("Hubo un error: ", err.message));
async function main() {
await initUsers();
await initAdverts();
mongoose.connection.close();
process.exit(0)
}
async function initUsers() {
const deleted = await User.deleteMany();
console.log(`Se ha eliminado ${deleted.deletedCount} usuarios`);
const users = await User.create([
{ email: "admin@example.com", password: await User.hashPassword("1234") },
]);
console.log(
`La base de datos se ha inicializado con ${users.length} usuario`
);
}
async function initAdverts() {
const { deletedCount } = await Advert.deleteMany();
console.log(`Se han eliminado ${deletedCount} anuncios`);
const result = await Advert.insertMany(initialData.adverts);
console.log(
`La base de datos se ha inicializado con ${result.length} anuncios`
);
}