-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqltest.js
77 lines (69 loc) · 2.53 KB
/
sqltest.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const sql = require('mssql');
/*
const config = {
user: 'username', // better stored in an app setting such as process.env.DB_USER
password: 'password', // better stored in an app setting such as process.env.DB_PASSWORD
server: 'your_server.database.windows.net', // better stored in an app setting such as process.env.DB_SERVER
port: 1433, // optional, defaults to 1433, better stored in an app setting such as process.env.DB_PORT
database: 'AdventureWorksLT', // better stored in an app setting such as process.env.DB_NAME
authentication: {
type: 'default'
},
options: {
encrypt: true
}
}
*/
/*
//Use Azure VM Managed Identity to connect to the SQL database
const config = {
server: process.env["db_server"],
port: process.env["db_port"],
database: process.env["db_database"],
authentication: {
type: 'azure-active-directory-msi-vm'
},
options: {
encrypt: true
}
}
//Use Azure App Service Managed Identity to connect to the SQL database
const config = {
server: process.env["db_server"],
port: process.env["db_port"],
database: process.env["db_database"],
authentication: {
type: 'azure-active-directory-msi-app-service'
},
options: {
encrypt: true
}
}
*/
console.log("Starting...");
connectAndQuery();
async function connectAndQuery() {
try {
var poolConnection = await sql.connect(config);
console.log("Reading rows from the Table...");
var resultSet = await poolConnection.request().query(`SELECT TOP 20 pc.Name as CategoryName,
p.name as ProductName
FROM [SalesLT].[ProductCategory] pc
JOIN [SalesLT].[Product] p ON pc.productcategoryid = p.productcategoryid`);
console.log(`${resultSet.recordset.length} rows returned.`);
// output column headers
var columns = "";
for (var column in resultSet.recordset.columns) {
columns += column + ", ";
}
console.log("%s\t", columns.substring(0, columns.length - 2));
// ouput row contents from default record set
resultSet.recordset.forEach(row => {
console.log("%s\t%s", row.CategoryName, row.ProductName);
});
// close connection only when we're certain application is finished
poolConnection.close();
} catch (err) {
console.error(err.message);
}
}