Skip to content

Commit 8d231e1

Browse files
Merge pull request microsoft#363 from JocaPC/master
Changed data access component in JSON/NodeJS products app
2 parents 96f947b + 302afee commit 8d231e1

File tree

11 files changed

+5733
-131
lines changed

11 files changed

+5733
-131
lines changed

samples/features/json/product-catalog/nodejs-jquery-bootstrap-app/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ node_modules/*
33
bin/*.dll
44
obj/*
55
*.sln
6-
*.log
6+
*.log
7+
package-lock.json

samples/features/json/product-catalog/nodejs-jquery-bootstrap-app/app.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1-
var express = require('express');
1+
var config = require('config');
2+
var express = require('express');
23
var bodyParser = require('body-parser');
4+
var tediousExpress = require('express4-tedious');
35

46
var app = express();
57
app.use(express.static('wwwroot'));
8+
69
//app.use(bodyParser.json());
710
app.use(bodyParser.text({ type: 'application/json' }))
11+
12+
app.use(function (req, res, next) {
13+
req.sql = tediousExpress(req, config.get('connection'));
14+
next();
15+
});
16+
817
app.use('/api/Product', require('./routes/products'));
918

1019
// catch 404 and forward to error handler
@@ -13,6 +22,7 @@ app.use(function (req, res, next) {
1322
err.status = 404;
1423
next(err);
1524
});
25+
1626
app.set('port', process.env.PORT || 3000);
1727

1828
var server = app.listen(app.get('port'), function() {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"connection":{
3+
"server" : "<<server name or ip>>",
4+
"userName": "<<user name>>",
5+
"password": "<<password>>",
6+
"options": { "encrypt": true, "database": "<<database name>>" }
7+
}
8+
}

samples/features/json/product-catalog/nodejs-jquery-bootstrap-app/db.js

Lines changed: 0 additions & 85 deletions
This file was deleted.

samples/features/json/product-catalog/nodejs-jquery-bootstrap-app/package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
"email": "[email protected]"
1212
},
1313
"dependencies": {
14-
"body-parser": "^1.15.2",
15-
"debug": "^2.2.0",
16-
"express": "^4.14.0",
17-
"tedious": "^1.14.0"
14+
"body-parser": "^1.18.2",
15+
"config": "^1.29.2",
16+
"debug": "^2.6.9",
17+
"express": "^4.16.2",
18+
"express4-tedious": "^0.2.0",
19+
"tedious": "^1.15.0"
1820
}
1921
}
Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,41 @@
11
var express = require('express');
22
var router = express.Router();
3-
4-
var db = require('../db.js');
53
var TYPES = require('tedious').TYPES;
64

75
/* GET products. */
86
router.get('/', function (req, res) {
9-
db.stream("select ProductID, Name, Color, Price, Quantity, JSON_VALUE(Data, '$.MadeIn') as MadeIn, JSON_QUERY(Tags) as Tags from Product FOR JSON PATH, ROOT('data')", db.createConnection(), res, '[]');
7+
req.sql("select ProductID, Name, Color, Price, Quantity, JSON_VALUE(Data, '$.MadeIn') as MadeIn, JSON_QUERY(Tags) as Tags from Product FOR JSON PATH, ROOT('data')")
8+
.into(res);
109
});
1110

1211
/* GET single product. */
1312
router.get('/:id', function (req, res) {
14-
15-
var conn = db.createConnection();
16-
17-
var request = db.createRequest("select ProductID, Name, Color, Price, Quantity, JSON_VALUE(Data, '$.MadeIn') as MadeIn, JSON_QUERY(Tags) as Tags from Product where productid = @id for json path, without_array_wrapper", conn);
18-
request.addParameter('id', TYPES.Int, req.params.id);
19-
db.stream(request, conn, res, '{}');
13+
req.sql("select ProductID, Name, Color, Price, Quantity, JSON_VALUE(Data, '$.MadeIn') as MadeIn, JSON_QUERY(Tags) as Tags from Product where productid = @id for json path, without_array_wrapper")
14+
.param('id', req.params.id, TYPES.Int)
15+
.into(res, '{}');
2016
});
2117

2218
/* POST create product. */
2319
router.post('/', function (req, res) {
2420

25-
var connection = db.createConnection();
26-
var request = db.createRequest("EXEC InsertProductFromJson @json", connection);
27-
28-
request.addParameter('json', TYPES.NVarChar, req.body);
29-
30-
db.executeRequest(request, connection);
31-
32-
res.end();
21+
req.sql("EXEC InsertProductFromJson @json")
22+
.param('json', req.body, TYPES.NVarChar)
23+
.exec(res);
3324
});
3425

3526
/* PUT update product. */
3627
router.put('/:id', function (req, res) {
37-
38-
var connection = db.createConnection();
39-
var request = db.createRequest("EXEC UpdateProductFromJson @id, @json", connection);
40-
request.addParameter('id', TYPES.Int, req.params.id);
41-
request.addParameter('json', TYPES.NVarChar, req.body);
42-
43-
db.executeRequest(request, connection);
44-
45-
res.end();
28+
req.sql("EXEC UpdateProductFromJson @id, @json")
29+
.param('json', req.body, TYPES.NVarChar)
30+
.param('id', req.params.id, TYPES.Int)
31+
.exec(res);
4632
});
4733

34+
/* DELETE delete product. */
4835
router.delete('/:id', function (req, res) {
49-
50-
var connection = db.createConnection();
51-
var request = db.createRequest("DELETE Product WHERE ProductId = @id", connection);
52-
request.addParameter('id', TYPES.Int, req.params.id);
53-
54-
db.executeRequest(request, connection);
55-
56-
res.end();
36+
req.sql("DELETE Product WHERE ProductId = @id")
37+
.param('id', req.params.id, TYPES.Int)
38+
.exec(res);
5739
});
5840

5941
module.exports = router;

0 commit comments

Comments
 (0)