如何循环数据库查询的限制和偏移量,以及如何循环数组和比较其他数据库的数据

时间:2019-04-15 14:54:23

标签: javascript jestjs typeorm

我有两个DB数据集。基本上一个是内部数据库,另一个是客户端数据库,而我对JS和编码是陌生的。如果两个数据集都同步,我想通过运行一些选择查询进行比较。

我已经连接到两个数据库并在两个数据库上运行select查询。目前,我已经硬编码了“偏移量”和“限制”(需要帮助,才能继续进行查询,直到“无数据可显示消息”为止)。完成后,第一个数据库结果包含ID详细信息,需要将ID注入第二个查询并将结果与​​第一个数据库结果进行比较。

数据库连接功能

import {ConnectionOptionsReader, getConnectionManager} from "typeorm";

export async function getDBConnection(dbName: any){
    const createConnectionOptions = await new ConnectionOptionsReader().get(dbName);
    const connection = await getConnectionManager().create(createConnectionOptions).connect();
    return connection;
}

测试用例-

import {getDBConnection} from "../utils/createDBConnection";

test("", async () => {
jest.setTimeout(100000);

const connectionDB = await getDBConnection("default");

<Want to loop Offet and Limit until the query triggers no data to show>
const Query1 = await connectionDB.manager.query("SELECT v.id AS id, t.published AS publisedDate,t.updated AS lastDate, d.description AS summary FROM db.user v INNER JOIN db.descriptions d ON v.id = d.id INNER JOIN db.time_latency t ON t.id = v.id WHERE DATE(v.created) < '2015-04-02' AND v.user_id = '111' limit 1000 offset 1");

Result dataset displays in below format for DB1 - 
[ RowDataPacket {
        id: '111',
        publisedDate: 2003,
        lastModifiedDate: 2003,
        summary: 'Hello' },
      RowDataPacket {
        id: '222',
        publisedDate: 1999,
        lastModifiedDate: 1999,
        summary: 'hi' },
      RowDataPacket {
        id: '333',
        publisedDate: 1998,
        lastModifiedDate: 2012,
        summary: 'bye' },
      RowDataPacket {
        id: '444',
        publisedDate: 1998,
        lastModifiedDate: 1998,
        summary: 'ola' }
]

我需要从上面的数据集中选择每个id并注入下面的Select DB查询(v.id)

const connectionDB2 = await getDBConnection("db2");
const Query2 = await connectionDB2.manager.query("SELECT id, publishedDate, lastModifiedDate, summary FROM apr.V WHERE apr.V.v = "+v.id+");

当我在上面的查询中输入上面的id说444时,结果是-

[ RowDataPacket {
        id: '444',
        publisedDate: 1998,
        lastModifiedDate: 1998,
        summary: 'ola' }
]

获得结果后,我需要进行比较

DB 1                            with DB2 - 
id = 444                      id = 444
publisedDate= 1998               publisedDate= 1998 
lastModifiedDate: 1998          lastModifiedDate: 1998 
summary=ola                    summary=ola

如果在DB1中,summary = ola,在DB2中,summary = OLLA,则它应为此ID打印一条消息,这是错误的。

await connectionDB.close();
await connectionDB2.close();
})

0 个答案:

没有答案