-
Notifications
You must be signed in to change notification settings - Fork 6
/
get-paginated-data-sequelize.ts
55 lines (45 loc) · 1.38 KB
/
get-paginated-data-sequelize.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import sequelize, { QueryOptions } from 'sequelize';
// assuming sequelize is initialized
export async function sleep(timeInMs: number) {
return new Promise(res => setTimeout(res, timeInMs));
}
export const sequelizeSelect = async (sqlQuery: string, queryOptions?: QueryOptions): Promise<any[]> => {
const opts = queryOptions || {};
return await sequelize.query(sqlQuery, {
...opts,
type: QueryTypes.SELECT
});
};
export async function fetchFromDbWithPaginated<T extends Record<string, any>>(opts: {
identifier: string;
limit?: number
lastId?: number
sleepInMs?: number
results?: T[]
getSqlStr: (lastId: number) => string
}): Promise<T[]> {
const { identifier, getSqlStr, limit = 500, lastId = 0, sleepInMs = 0, results = [] } = opts
const sqlStr = `${getSqlStr(lastId)} limit ${limit}`;
const queryResults: T[] = await sequelizeSelect(sqlStr)
if (lastId === 0) {
console.log(new Date(), `fetched first ${limit}...`)
} else {
console.log(new Date(), `last ID: ${lastId}`, `fetched next ${limit}...`)
}
results.push(...queryResults);
if (queryResults.length < limit) {
return results;
}
if (sleepInMs) {
await sleep(sleepInMs)
}
const newLastId = queryResults[queryResults.length - 1][identifier]
return await fetchFromDbWithPaginated<T>({
getSqlStr,
identifier,
lastId: newLastId,
limit,
results,
sleepInMs
})
}