-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.js
52 lines (46 loc) · 1.51 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
43
44
45
46
47
48
49
50
51
52
/**
* Demonstrates use of sync-db to create functions, procedures and views in PostgresSql.
*/
const dbConfig = require('../knexfile').connection.connection;
(async () => {
try {
// PostgreSQL connection only works with a connection string.
// Time wasted = 60 minutes
const db = require('knex')({
client: 'pg',
connection: `postgresql://${dbConfig.user}:${dbConfig.password}@${dbConfig.host}:${dbConfig.port}/${dbConfig.database}`
});
const { rows: tables } = await db.raw('SELECT * FROM utils.vw_table_names');
const { rows: users } = await db.raw('SELECT * FROM utils.vw_user_names');
const {
rows: [{ result: sum }]
} = await db.raw('SELECT public.sum(6, 7) AS result;');
const {
rows: [{ result: square }]
} = await db.raw('SELECT public.square(6) AS result;');
const {
rows: [{ result: product }]
} = await db.raw('SELECT utils.product(6, 7) AS result;');
const {
rows: [{ result: date }]
} = await db.raw('SELECT utils.get_date() AS result;');
console.log(
'\nList of table names in the database:\n',
tables.map(({ name }) => name)
);
console.log(
'\nList of user names in the database:\n',
users.map(({ name }) => name)
);
console.log('\nCalculations:\n', {
'Sum of 6 and 7': sum,
'Square of 6': square,
'Product of 6 and 7': product
});
console.log('\nCurrent date time:', date);
process.exit(0);
} catch (e) {
console.log(e);
process.exit(-1);
}
})();