如何使此打字稿/ JavaScript代码更具“功能性编程风格”

我正在学习打字稿/ javascript,并探索函数式编程。对于经验丰富的javascript程序员来说,这里的某些代码很可能似乎是错误的。所以基本上我想

  1. stream a remote file - I use got
  2. convert each csv to json - I use csvtojson
  3. At this point I can individually write to mongodb but that is inefficient so I collect that in a variable of 5000 json objects and then write to database. - I use mongoose

我不喜欢该全局变量,并且肯定有更好的方法来做我正在做的事情。另外,由于数据并不总是5000的倍数,因此我可能会有一些未写入的行。我也需要解决。谢谢。

import csv from "csvtojson";
import got from "got";
import { CountyDocument, County } from "../models/County";
import logger from "../util/logger";
import {BulkWriteOpResultObject} from "mongodb";

const writetoDataBase = (data: any,): Promise<BulkWriteOpResultObject> => {
    return County.bulkWrite(data);
};

let collect: Array<CountyDocument> = [];

function processIndividualRecord(data: CountyDocument, linenumber: number) {
    let prom;
    if (linenumber !== 0 && linenumber % 5000 === 0) {
        const copyarray = collect.map(x => {
            const upsertDoc = {
                updateOne: {
                    filter: { county: x.county, date: x.date, state: x.state },
                    update: { cases: x.cases, deaths: x.deaths, fips: x.fips },
                    upsert: true
                }
            };
            return upsertDoc;
        });
        prom = writetoDataBase(copyarray).then(value => {
            logger.debug(`writing to DB at ${linenumber}`);
        });
        collect = [];
    } else {
        collect.push(data);
        prom = Promise.resolve();
    }
    return prom;
};
export const saveschedule = () => {
    return csv().fromStream(got.stream(<some url>))
        .subscribe(processIndividualRecord, (err: CSVError) => {
            logger.error(`error processing the rows ${err.message} and the stack is ${err.stack}`);
            process.exit(1);
        }, () => {
            logger.info("DONE");
        });

};
评论