src/parser.js
/**
   * Parses the file.
   * @return {Promise} A promise to resolve the file parsing.
   */
Parses the file.
@return {Promise} A promise to resolve the file parsing.
Parses the file . @return { Promise } A promise to resolve the file parsing .
parse() {
    let self = this;
    return new Promise(async(resolve, reject) => {
      let service = new HealthFacilitiesService();
      let stream = fs.createReadStream(self.targetFile);
      let parsingOptions = {
        headers: true,
        delimiter: ',',
        quote: '"',
        objectMode: true,
        trim: true
      };
      let csvStream = csv(parsingOptions).on('data', (data) => {
        let healthFacility = {
          id: data.co_cnes,
          ibge: data.co_ibge,
          type: val(data.ds_tipo_unidade),
          openingHours: val(data.ds_turno_atendimento),
          services: filterServices(data.ds_servico_especializado),
          name: val(data.no_fantasia),
          businessName: val(data.no_razao_social),
          phone: val(data.nu_telefone),
          latitude: data.lat,
          longitude: data.long,
          address: {
            street: val(data.no_logradouro),
            number: val(data.nu_endereco),
            neighborhood: val(data.no_bairro),
            postalCode: val(data.co_cep),
            state: val(data.uf),
            city: val(data.municipio)
          }
        };
        service.addFacility(healthFacility);
      }).on('end', async() => {
        try {
          log.info('CSV parsed. Commiting to dynamodb...');
          await service.commit();
          log.info('Done');
          resolve();
        } catch (err) {
          reject(err);
        }
      });
      await service.createTables();
      log.info('Parsing CSV....');
      stream.pipe(csvStream);
    });
  }
parse ( ) { let self = this ; return new Promise ( async ( resolve , reject ) => { let service = new HealthFacilitiesService ( ) ; let stream = fs . createReadStream ( self . targetFile ) ; let parsingOptions = { headers : true , delimiter : ',' , quote : '"' , objectMode : true , trim : true } ; let csvStream = csv ( parsingOptions ) . on ( 'data' , ( data ) => { let healthFacility = { id : data . co_cnes , ibge : data . co_ibge , type : val ( data . ds_tipo_unidade ) , openingHours : val ( data . ds_turno_atendimento ) , services : filterServices ( data . ds_servico_especializado ) , name : val ( data . no_fantasia ) , businessName : val ( data . no_razao_social ) , phone : val ( data . nu_telefone ) , latitude : data . lat , longitude : data . long , address : { street : val ( data . no_logradouro ) , number : val ( data . nu_endereco ) , neighborhood : val ( data . no_bairro ) , postalCode : val ( data . co_cep ) , state : val ( data . uf ) , city : val ( data . municipio ) } } ; service . addFacility ( healthFacility ) ; } ) . on ( 'end' , async ( ) => { try { log . info ( 'CSV parsed. Commiting to dynamodb...' ) ; await service . commit ( ) ; log . info ( 'Done' ) ; resolve ( ) ; } catch ( err ) { reject ( err ) ; } } ) ; await service . createTables ( ) ; log . info ( 'Parsing CSV....' ) ; stream . pipe ( csvStream ) ; } ) ; }
Parses the file.
Parses the file .
// creates a service instance to store the facilities // Reads the file // CSV parsing options // parses the CSV file // transforms the data structure //creates the dynamodb tables // pipes the stream