-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata-sources.js
More file actions
69 lines (59 loc) · 1.65 KB
/
Copy pathdata-sources.js
File metadata and controls
69 lines (59 loc) · 1.65 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import fs from 'fs-extra';
import sql from 'mssql';
import {
SQL_USER,
SQL_PASSWORD,
SQL_SERVER,
SQL_DATABASE,
SQL_PORT,
SQL_BATCH_SIZE
} from './config/env';
const config = {
user: SQL_USER,
password: SQL_PASSWORD,
server: SQL_SERVER,
database: SQL_DATABASE,
port: SQL_PORT
};
const executeBatchedSqlQuery = async function(query, sqlpool) {
try {
let records = [];
let resultLength = SQL_BATCH_SIZE;
let offset = 0;
let loop = 1;
while(resultLength == SQL_BATCH_SIZE) {
const sql = `${query} OFFSET ${offset} ROWS FETCH NEXT ${SQL_BATCH_SIZE} ROWS ONLY OPTION (RECOMPILE);`;
let result = await sqlpool.request().query(sql);
console.log(`Batch number ${loop} including ${result.rowsAffected} rows`);
records = records.concat(result.recordset);
offset += SQL_BATCH_SIZE;
loop += 1;
resultLength = result.rowsAffected;
}
return records;
} catch (err) {
console.log(`An error has occured: ${err}`);
return [];
}
};
const loadSource = async function(pool, query, file) {
const records = await executeBatchedSqlQuery(query, pool);
console.log(`Writing ${records.length} records to file ${file}`);
const data = JSON.stringify(records);
await fs.writeFile(file, data, { encoding: 'utf8' });
};
const loadSources = async function(tasks) {
console.log(`Loading sources...`);
try {
const pool = await sql.connect(config);
const queryEngine = async function(query, file) {
await loadSource(pool, query, file);
};
for (const task of tasks) {
await task.load(queryEngine);
};
} finally {
sql.close();
}
};
export default loadSources;