Upgraded to express4-tedious DAC

Changed data access layer in nodejs product catalog to express4 tedious.
This commit is contained in:
Jovan Popovic
2018-01-27 14:32:13 +01:00
parent 75fdf72de9
commit d9f2c7125f
5 changed files with 43 additions and 40 deletions
@@ -3,4 +3,5 @@ node_modules/*
bin/*.dll
obj/*
*.sln
*.log
*.log
package-lock.json
@@ -1,10 +1,19 @@
var express = require('express');
var config = require('config');
var express = require('express');
var bodyParser = require('body-parser');
var tediousExpress = require('express4-tedious');
var app = express();
app.use(express.static('wwwroot'));
//app.use(bodyParser.json());
app.use(bodyParser.text({ type: 'application/json' }))
app.use(function (req, res, next) {
req.sql = tediousExpress(req, config.get('connection'));
next();
});
app.use('/api/Product', require('./routes/products'));
// catch 404 and forward to error handler
@@ -13,6 +22,7 @@ app.use(function (req, res, next) {
err.status = 404;
next(err);
});
app.set('port', process.env.PORT || 3000);
var server = app.listen(app.get('port'), function() {
@@ -0,0 +1,8 @@
{
"connection":{
"server" : "<<server name or ip>>",
"userName": "<<user name>>",
"password": "<<password>>",
"options": { "encrypt": true, "database": "<<database name>>" }
}
}
@@ -11,9 +11,11 @@
"email": "jovanpop@microsoft.com"
},
"dependencies": {
"body-parser": "^1.15.2",
"debug": "^2.2.0",
"express": "^4.14.0",
"tedious": "^1.14.0"
"body-parser": "^1.18.2",
"config": "^1.29.2",
"debug": "^2.6.9",
"express": "^4.16.2",
"express4-tedious": "^0.1.0",
"tedious": "^1.15.0"
}
}
@@ -1,59 +1,41 @@
var express = require('express');
var router = express.Router();
var db = require('../db.js');
var TYPES = require('tedious').TYPES;
/* GET products. */
router.get('/', function (req, res) {
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, '[]');
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')")
.into(res, '[]');
});
/* GET single product. */
router.get('/:id', function (req, res) {
var conn = db.createConnection();
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);
request.addParameter('id', TYPES.Int, req.params.id);
db.stream(request, conn, res, '{}');
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")
.param('id', req.params.id, TYPES.Int)
.into(res, '{}');
});
/* POST create product. */
router.post('/', function (req, res) {
var connection = db.createConnection();
var request = db.createRequest("EXEC InsertProductFromJson @json", connection);
request.addParameter('json', TYPES.NVarChar, req.body);
db.executeRequest(request, connection);
res.end();
req.sql("EXEC InsertProductFromJson @json")
.param('json', req.body, TYPES.NVarChar)
.exec(res);
});
/* PUT update product. */
router.put('/:id', function (req, res) {
var connection = db.createConnection();
var request = db.createRequest("EXEC UpdateProductFromJson @id, @json", connection);
request.addParameter('id', TYPES.Int, req.params.id);
request.addParameter('json', TYPES.NVarChar, req.body);
db.executeRequest(request, connection);
res.end();
req.sql("EXEC UpdateProductFromJson @id, @json")
.param('json', req.body, TYPES.NVarChar)
.param('id', req.params.id, TYPES.Int)
.exec(res);
});
/* DELETE delete product. */
router.delete('/:id', function (req, res) {
var connection = db.createConnection();
var request = db.createRequest("DELETE Product WHERE ProductId = @id", connection);
request.addParameter('id', TYPES.Int, req.params.id);
db.executeRequest(request, connection);
res.end();
req.sql("DELETE Product WHERE ProductId = @id")
.param('id', req.params.id, TYPES.Int)
.exec(res);
});
module.exports = router;