-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongoDatabase.js
39 lines (35 loc) · 1.34 KB
/
mongoDatabase.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
39
const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://felipemm:[email protected]/test?retryWrites=true&w=majority";
const client = new MongoClient(uri, {
useNewUrlParser: true
});
let produtos = require('./produtos.json');
let clientes = require('./clientes.json');
let vendas = require('./vendas.json');
let execute = () =>
client.connect(async err => {
await client.db('dbvendas').collection('produto').drop();
await client.db('dbvendas').collection('cliente').drop();
await client.db('dbvendas').collection('venda').drop();
await client.db('dbvendas').createCollection('produto');
await client.db('dbvendas').createCollection('cliente');
await client.db('dbvendas').createCollection('venda');
try {
await Promise.all(produtos.map(produto => client.db('dbvendas').collection('produto').insertOne(produto)))
} catch (err) {
console.log(err)
};
try {
await Promise.all(clientes.map(cliente => client.db('dbvendas').collection('cliente').insertOne(cliente)))
} catch (err) {
console.log(err)
};
try {
await Promise.all(vendas.map(venda => client.db('dbvendas').collection('venda').insertOne(venda)))
} catch (err) {
console.log(err)
};
console.log('finished');
return Promise.resolve();
});
execute();