Repository navigation
Expand file tree
/
Copy pathrun.js
More file actions
64 lines (57 loc) · 2.11 KB
/
Copy pathrun.js
File metadata and controls
64 lines (57 loc) · 2.11 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
require('dotenv').config();
const consola = require('consola');
const { performance } = require('perf_hooks');
const { Command } = require('commander');
const { db } = require('./database');
const Employee = require('./model');
const numberParser = require('./utils/numberParser');
const program = new Command();
program
.option('-id, --id <id>', 'define root node ID', numberParser)
.option('-l, --levels <levels>', 'define number of levels to fetch', numberParser)
.parse(process.argv);
run(program.id, program.levels);
async function runPWithTimer(label, promise) {
const start = performance.now();
return promise
.then(items => {
const end = performance.now();
consola.success(`Fetched ${items.length} items with ${label} in ${end - start} ms`);
return items;
});
}
async function getTreeById(idOrIds, maxLevel, level = 0) {
if (level === maxLevel) return [];
const rootOrChildren = await Employee.findAll({ where: { superior_id: idOrIds } });
const hasChildren = rootOrChildren.length;
if (!hasChildren) return rootOrChildren;
const childrenNodeIds = rootOrChildren.map(it => it.id);
const descendants = await getTreeById(childrenNodeIds, maxLevel, level + 1);
return [...rootOrChildren, ...descendants];
}
async function getTreeByIdWithCte(id, levels) {
const employee = await Employee.findByPk(id);
return employee.getTree(levels);
}
function run(id, levels) {
const runTests = () => Promise.all([
runPWithTimer('Model.findAll', getTreeById(id, levels)),
runPWithTimer('recursive CTE', getTreeByIdWithCte(id, levels))
]);
return runTests()
.then(() => db.schema.alterTable('employees', table => table.index('superior_id')))
.then(() => {
consola.info('Added index on column superior_id, re-running tests...');
return runTests();
})
.then(() => db.schema.alterTable('employees', table => table.dropIndex('superior_id')))
.then(() => {
consola.info('Dropped index on column superior_id');
consola.info('Finished running tests');
process.exit();
})
.catch(err => {
consola.error(err);
process.exit(1);
});
}