analyse-parrainages/server/tools/import_parrainages.js

39 lines
1.1 KiB
JavaScript

'use strict';
const path = require('path');
const fs = require('fs');
const parse = require('csv-parse');
const transform = require('stream-transform');
const app = require(path.resolve(__dirname, '../server'));
const argv = require('minimist')(process.argv.slice(2));
const promisePipe = require("promisepipe");
const _ = require('lodash');
var input = fs.createReadStream(argv._[0]);
var parser = parse({ columns: true, delimiter: ';' });
var toDatabase = transform(function(record, callback){
app.models.Elu.findOrCreate({
lastname: _.upperCase(record['Nom'].toUpperCase()),
firstname: _.upperCase(record['Prénom'].toUpperCase()),
mandat: record['Mandat'],
circonscription: record['Circonscription'],
departement: record['Département']
}).then(function([elu, created]){
return app.models.Parrainage.create({
eluId: elu.id,
who: record['Candidat-e parrainé-e'],
when: new Date(record['Date de publication'])
})
})
.then(function(){
callback();
})
}, {parallel: 100});
promisePipe(input, parser, toDatabase)
.then(function(){
console.log('Done.');
process.exit();
});