diff --git a/samples/features/json/product-catalog/nodejs-jquery-bootstrap-app/.gitignore b/samples/features/json/product-catalog/nodejs-jquery-bootstrap-app/.gitignore index 2589a3d5..e8f32bd1 100644 --- a/samples/features/json/product-catalog/nodejs-jquery-bootstrap-app/.gitignore +++ b/samples/features/json/product-catalog/nodejs-jquery-bootstrap-app/.gitignore @@ -3,4 +3,5 @@ node_modules/* bin/*.dll obj/* *.sln -*.log \ No newline at end of file +*.log +package-lock.json \ No newline at end of file diff --git a/samples/features/json/product-catalog/nodejs-jquery-bootstrap-app/app.js b/samples/features/json/product-catalog/nodejs-jquery-bootstrap-app/app.js index 2f720268..ce13a599 100644 --- a/samples/features/json/product-catalog/nodejs-jquery-bootstrap-app/app.js +++ b/samples/features/json/product-catalog/nodejs-jquery-bootstrap-app/app.js @@ -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() { diff --git a/samples/features/json/product-catalog/nodejs-jquery-bootstrap-app/config/default.json b/samples/features/json/product-catalog/nodejs-jquery-bootstrap-app/config/default.json new file mode 100644 index 00000000..fd65c54b --- /dev/null +++ b/samples/features/json/product-catalog/nodejs-jquery-bootstrap-app/config/default.json @@ -0,0 +1,8 @@ +{ + "connection":{ + "server" : "<>", + "userName": "<>", + "password": "<>", + "options": { "encrypt": true, "database": "<>" } + } +} \ No newline at end of file diff --git a/samples/features/json/product-catalog/nodejs-jquery-bootstrap-app/package.json b/samples/features/json/product-catalog/nodejs-jquery-bootstrap-app/package.json index 0a63a5a8..91f07104 100644 --- a/samples/features/json/product-catalog/nodejs-jquery-bootstrap-app/package.json +++ b/samples/features/json/product-catalog/nodejs-jquery-bootstrap-app/package.json @@ -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" } } diff --git a/samples/features/json/product-catalog/nodejs-jquery-bootstrap-app/routes/products.js b/samples/features/json/product-catalog/nodejs-jquery-bootstrap-app/routes/products.js index 0a55b309..88eb389e 100644 --- a/samples/features/json/product-catalog/nodejs-jquery-bootstrap-app/routes/products.js +++ b/samples/features/json/product-catalog/nodejs-jquery-bootstrap-app/routes/products.js @@ -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; \ No newline at end of file