Advertisement
Guest User

Untitled

a guest
Sep 12th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. import Fs from 'fs'
  2. import Path from 'path'
  3. import Mime from 'mime'
  4.  
  5.  
  6. import { Connection } from 'tedious'
  7. import { Request } from 'tedious'
  8. const TYPES = require('tedious').TYPES
  9.  
  10.  
  11. module.exports = async (ctx, next) => {
  12.  
  13.  
  14. var config = {
  15. userName: process.env.DB_USER,
  16. password: process.env.DB_PASS,
  17. server: process.env.DB_HOST,
  18.  
  19.  
  20. options: {
  21.  
  22.  
  23. database: process.env.DB_BASE ,
  24. rowCollectionOnRequestCompletion: true
  25. }
  26. }
  27.  
  28.  
  29. const connection = new Connection(config)
  30.  
  31.  
  32. let rowdata = {}
  33. let rowarray = []
  34.  
  35. connection.on('connect', (err) => {
  36. if (err) {
  37. console.log(`error : ${err}`)
  38. } else {
  39. let sql = `select a.nome as name, CONVERT(varchar(7),r.date, 126) as date
  40. from blah a left join blah r
  41. on a.blah = r.blah
  42. where a.blah = @blah `
  43.  
  44.  
  45. let request = new Request(sql, function(err, rowCount) {
  46. if (err) {
  47. console.log(err);
  48. } else {
  49. ctx.statusCode = 200
  50. ctx.body = JSON.stringify(rowdata)
  51. console.log(rowdata.name)
  52. }
  53. })
  54.  
  55.  
  56. request.addParameter('blah ', TYPES.Int, ctx.params.id)
  57.  
  58.  
  59. request.on('row', function(columns) {
  60.  
  61. columns.forEach(function(column) {
  62. rowdata[column.metadata.colName] = column.value
  63. console.log(column.value)
  64. console.log(column.metadata.colName)
  65. })
  66. rowarray.push(rowdata)
  67. })
  68.  
  69. connection.execSql(request);
  70. }
  71. })
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement