forked from RochMoreau/secure-web-dev-workshop2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import-data.js
83 lines (66 loc) · 2.37 KB
/
import-data.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/mongoose
const mongoose = require('mongoose')
require('dotenv').config()
const filmingLocations = require('./lieux-de-tournage-a-paris.json')
//import mongoose from 'mongoose';
const { Schema } = mongoose;
const Locations = new Schema({
filmType: String, // String is shorthand for {type: String}
filmProducerName: String,
endDate: Date,
filmName: String,
district: String,
geolocation:
{
coordinates: {
type:[Number]
},
type: {
type: String,
enum:["Point",""]
}
},
sourceLocationId: String,
filmDirectorName: String,
address: String,
startDate: Date,
year: String
});
const LocationModel = mongoose.model('Location',Locations)
module.exports = LocationModel;
async function importFilmingLocationInMongo(){
let chunk = []
for (const film of filmingLocations) {
const newFilmModelToInsert = new LocationModel({ filmType: film.fields.type_tournage},
{ filmProducerName: film.fields.nom_producteur },
{ endDate: film.fields.date_fin},
{ filmName: film.fields.nom_tournage},
{ district: film.fields.ardt_lieu},
{ geolocation: film.fields.geo_shape.coordinates },
{ type: film.fields.geo_shape.type },
{ sourceLocationId: film.fields.id_lieu },
{ filmDirectorName: film.fields.nom_realisateur},
{ adress: film.fields.adresse_lieu},
{ startDate: film.fields.date_debut},
{ year: film.fields.annee_tournage });
//On insere des promesses de donnees à envoyer à la bdd
chunk.push(newFilmModelToInsert.save())
//tous les 100 models de films, on envoie à la bdd mongo
if(chunk.length===100){
await Promise.all(chunk)
chunk = []
}
//on envoie les models restants
const results = await Promise.all(chunk)
//on affiche le resultat de la promesse des derniers elements
console.log(results)
}
}
async function main(){
const connected = await mongoose.connect(process.env.MONGO_URI);
console.log(connected);
//On envoie les models sur mongo
importFilmingLocationInMongo()
await mongoose.connection.close();
}
main();