Skip to content

Commit b6ee189

Browse files
author
junerockwell
committed
Added all the templates
1 parent 4ffc1f3 commit b6ee189

File tree

23 files changed

+3468
-0
lines changed

23 files changed

+3468
-0
lines changed

.DS_Store

6 KB
Binary file not shown.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
let config = require('../config/db.js');
2+
let tediousPromise = require('tedious-promises');
3+
let ConnectionPool = require('tedious-connection-pool');
4+
let pool = new ConnectionPool(config.pool, config.connect);
5+
tediousPromise.setConnectionPool(pool);
6+
7+
module.exports = {
8+
AzureExample: callback => {
9+
tediousPromise.sql("SELECT c.CustomerID, c.CompanyName,COUNT(soh.SalesOrderID) AS OrderCount FROM SalesLT.Customer AS c LEFT OUTER JOIN SalesLT.SalesOrderHeader AS soh ON c.CustomerID = soh.CustomerID GROUP BY c.CustomerID, c.CompanyName ORDER BY OrderCount DESC;")
10+
.execute()
11+
.then(results => {
12+
callback(null, results);
13+
})
14+
.fail(err => {
15+
callback(err, []); // make sure that an array is being expected...
16+
});
17+
}
18+
};

API_MSSQL_AZURE/AzureData/UserData.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
let config = require('../config/db.js');
2+
let tediousPromise = require('tedious-promises');
3+
let ConnectionPool = require('tedious-connection-pool');
4+
let pool = new ConnectionPool(config.pool, config.connect);
5+
tediousPromise.setConnectionPool(pool);
6+
let bcrypt = require('bcrypt-nodejs');
7+
let TYPES = require('tedious').TYPES;
8+
9+
let UserData = {
10+
11+
GetUserData: data => {
12+
13+
let qry = "SELECT * FROM users WHERE username = @username";
14+
return tediousPromise.sql(qry)
15+
.parameter('username', TYPES.VarChar, data.username)
16+
.execute()
17+
.then(results => {
18+
if (results.length > 0) {
19+
return {error: null, data: results[0], exists: true};
20+
}
21+
return {error: null, data: {}, exists: false};
22+
})
23+
.fail(err => {
24+
return {error: err, data: {}, exists: false};
25+
});
26+
},
27+
SignUp: data => {
28+
return UserData.GetUserData(data)
29+
.then(user => {
30+
31+
if (user.error) {
32+
return {error: user.error, exists: false, success: false};
33+
}
34+
35+
if (user.exists === true) {
36+
return {error: null, exists: true, success: false};
37+
}
38+
39+
let hasedPassword = generateHash(data.password);
40+
let qry = "INSERT INTO users (username, password) OUTPUT INSERTED.Id VALUES (@username, @password)";
41+
return tediousPromise.sql(qry)
42+
.parameter('username', TYPES.VarChar, data.username)
43+
.parameter('password', TYPES.VarChar, hasedPassword)
44+
.execute()
45+
.then(result => {
46+
return {error: null, data: result};
47+
})
48+
.fail(err => {
49+
return {error: err, data: {}};
50+
});
51+
});
52+
},
53+
LoginAUser: data => {
54+
return UserData.GetUserData(data)
55+
.then(user => {
56+
if (user.error) {
57+
return {error: user.error, success: false, exists: false};
58+
}
59+
60+
if (!user || user.exists === false) {
61+
return {error: null, success: false, exists: false};
62+
}
63+
64+
if (user.data.password && !validPassword(data.password, user.data.password)) {
65+
return {error: null, success: false, exists: true};
66+
}
67+
68+
return {error: null, success: true, exists: true, userId: user.data.id};
69+
});
70+
}
71+
};
72+
73+
function generateHash(password) {
74+
return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
75+
}
76+
77+
function validPassword(userPassword, passwordInDB) {
78+
return bcrypt.compareSync(userPassword, passwordInDB);
79+
}
80+
81+
module.exports = UserData;

API_MSSQL_AZURE/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Node and Azure
2+
3+
### Todo
4+
1. Need to do User table
5+
2. Use bcrypt-nodejs for the password. (Try with MySQL first). Prove the concept?
6+
3. Do Unit Test
7+
8+
### Unit Test
9+
1. Setup Mocha in `test` directory
10+
2. Make sure to put the Mocha config inside `package.json` `script.test`.

API_MSSQL_AZURE/config/db.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
module.exports = {
3+
connect: {
4+
userName: 'azurejune88',
5+
password: 'GottMSDB88!',
6+
server: 'juneazure.database.windows.net',
7+
// If you are on Microsoft Azure, you need this:
8+
options: {
9+
encrypt: true,
10+
database: 'mySampleDatabase',
11+
rowCollectionOnRequestCompletion: true
12+
}
13+
},
14+
pool: { // see tedious-connection-pool documentation
15+
min: 2,
16+
max: 4,
17+
log: true
18+
}
19+
};

0 commit comments

Comments
 (0)