nodejs pgpromise - 帮助使用大量事务

时间:2016-10-22 13:31:36

标签: node.js pg-promise exceljs

我在nodejs app上使用pgpromise。我正在做的事情之一是插入许多记录,超过10000条记录。我这样做的最初方式是使用db.tx,并返回批处理。这种方法的唯一问题是它需要一段时间,而且它会将大量的承诺加载到数组中并占用大量内存。

以下是我目前所做的事情......

var worksheet = workbook.getWorksheet(1);
return db.tx(function (t) {
                    var insertStatements = [];
                    var upsertProcessedData = this.getSql('./sql/upsertProcessedData.sql');
                    for (var i = 2; i <= worksheet._rows.length; i++) {
                        // need to convert strings to dates for postgres insert
                        // console.log('Working row ' + i);
                        worksheet.getRow(i).getCell(8).value = new Date(worksheet.getRow(i).getCell(8).value);

                        // here we create a new array from the worksheet, as we need a 0 index based array.
                        // the worksheet values actually begins at element 1.  We will splice to dump the undefined element at index 0.
                        // This will allow the batch promises to work correctly... otherwise everything will be offset by 1
                        var arrValues = Array.from(worksheet.getRow(i).values);
                        arrValues.splice(0, 1);

                        // these queries are upsert.  Inserts will occur first, however if they error on the constraint, an update will occur instead.
                        insertStatements.push(this.one(upsertProcessedData, arrValues));
                    }
                    console.log('Begin inserts');
                    return t.batch(insertStatements);
                }).then( blah blah blah...)

这种方法有效,一旦获得大型数据工作表,它就会非常慢。

我偶然发现了使用sequence,但我正在努力了解它是如何运作的。根据文档,它声明如此使用它。

function source(index, data, delay) {
    // must create and return a promise object dynamically, 
    // based on the index of the sequence; 
    switch (index) {
        case 0:
            return this.query('select 0');
        case 1:
            return this.query('select 1');
        case 2:
            return this.query('select 2');
    }
    // returning or resolving with undefined ends the sequence; 
    // throwing an error will result in a reject; 
}

db.tx(function (t) {
    // `t` and `this` here are the same; 
    return this.sequence(source);
})

我不明白的是如何允许我的来源&#34;函数访问我的工作表中的数据? &#34;数据&#34;参数来自源函数?似乎我不能将我的工作表obj传递给我的源函数 相反,我想尝试序列方法,但我不确定如何访问包含sql的查询文件,以及如何使用此序列函数迭代我的工作表...

编辑: 因此,在阅读了Vitaly关于序列的文章以及查看github问题之后,我想出了以下内容:

Promise.bind(promiseBindObject)
    .then(function (workbook) {
        return this.workbook.xlsx.readFile(this.fileName);
    }).then(function (workbook) {
        var worksheet = workbook.getWorksheet(1);
        // console.log(worksheet.name);
        var upsertProcessedRqData = db.getSql('./sql/upsertProcessedRqData.sql');
        function source(index, data, delay) {   
            // create and return a promise object dynamically,
            // based on the index passed;
            var arrValues = worksheet.getRow(index).values;
            if (index < worksheet._rows.length) {
                return this.this.one(upsertProcessedRqData, arrValues);
            }
            // returning or resolving with undefined ends the sequence;
            // throwing an error will result in a reject;
        }

        db.tx(function (t) {
            return t.sequence(source);
        ...

然而,问题是在我的&#34;来源&#34;函数,我的所有变量都超出了范围。但是,根据我所看到的情况,我并不确定它们为什么会这样。虽然调试它只是说它们不可用&#39;。

0 个答案:

没有答案