-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
42 lines (38 loc) · 1.3 KB
/
index.js
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
/**
* Demonstrates use of sync-db to create functions, procedures and views in MSSQL.
*/
const knex = require('knex');
const { connections } = require('../connections.sync-db.json');
(async () => {
try {
// Getting knex instance of mssql database with id db1.
const db = knex({
client: 'mssql',
connection: connections.find(({ id }) => id === 'testdb1').connection
});
const tasks = await db.raw('SELECT * FROM utils.vw_tasks');
const users = await db.raw('SELECT * FROM utils.vw_users');
const [{ result: product }] = await db.raw('SELECT utils.product(6, 7) AS result;');
const [{ result: sum }] = await db.raw('SELECT dbo.sum(6, 7) AS result;');
const [{ result: square }] = await db.raw('SELECT dbo.square(6) AS result;');
const [{ result: date }] = await db.raw('EXEC utils.get_date;');
console.log(
'\nList of users:\n',
users.map(({ email }) => email)
);
console.log(
'\nList of completed tasks:\n',
tasks.filter(task => !!task.is_complete)
);
console.log('\nCalculations:\n', {
'Sum of 6 and 7': sum,
'Product of 6 and 7': product,
'Square of 6': square
});
console.log('\nCurrent date time:', date);
process.exit(0);
} catch (err) {
console.log(err);
process.exit(-1);
}
})();