Advertisement
Guest User

Untitled

a guest
Nov 25th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. 'use strict'
  2.  
  3. const reekoh = require('reekoh')
  4. const plugin = new reekoh.plugins.Storage()
  5.  
  6. const async = require('async')
  7. const Connection = require('tedious').Connection
  8. const Request = require('tedious').Request
  9. const TYPES = require('tedious').TYPES
  10.  
  11. const isPlainObject = require('lodash.isplainobject')
  12. const isEmpty = require('lodash.isempty')
  13.  
  14. let _query = null
  15. let _parseFields = null
  16. let _connection = null
  17.  
  18. plugin.on('data', (data) => {
  19. if (!isPlainObject(data)) {
  20. return plugin.logException(new Error(`Invalid data received. Must be a valid JSON Object. Data: ${data}`))
  21. }
  22.  
  23. if (isEmpty(data)) {
  24. return plugin.logException(new Error('Invalid data received. Data must not be empty.'))
  25. }
  26.  
  27. let request = new Request(_query, function (error) {
  28. if (error) {
  29. console.error(error)
  30. return plugin.logException(new Error('Error creating request.'))
  31. }
  32. })
  33.  
  34. async.forEachOf(_parseFields, (field, key, callback) => {
  35. request.addParameter(field.source_field, TYPES[field.data_type], data[field.source_field])
  36. callback()
  37. }, () => {
  38. _connection.on('debug', function (msg) {
  39. if (msg === 'State change: SentClientRequest -> LoggedIn') {
  40. plugin.log(JSON.stringify({
  41. title: 'Data has been sent to Azure Table Storage',
  42. data: data
  43. }))
  44. plugin.emit('processed')
  45. }
  46. })
  47.  
  48. _connection.on('error', function (error) {
  49. console.log('error', error)
  50. plugin.logException(error)
  51. })
  52.  
  53. _connection.execSql(request)
  54. })
  55. })
  56.  
  57. plugin.once('ready', () => {
  58. console.log('ready')
  59. let options = plugin.config
  60.  
  61. let first = true
  62. let columnList = null
  63. let valueList = null
  64.  
  65. try {
  66. _parseFields = JSON.parse(options.fields)
  67. } catch (ex) {
  68. console.log(ex)
  69. plugin.logException(new Error('Error parsing Field Mapping configuration parameter. Must be a valid JSON String.'))
  70.  
  71. setTimeout(() => {
  72. process.exit(1)
  73. }, 2000)
  74.  
  75. return
  76. }
  77.  
  78. async.forEachOf(_parseFields, (field, key, callback) => {
  79. if (field.source_field === undefined || field.source_field === null) {
  80. callback(new Error(`Source field is missing for ${key} in the fields configuration parameter.`))
  81. } else if (!TYPES.hasOwnProperty(field.data_type)) {
  82. callback(new Error(`Invalid Data Type for ${key}, please refer to http://pekim.github.io/tedious/api-datatypes.html for valid values.`))
  83. } else {
  84. if (!first) {
  85. valueList = `${valueList}, @${field.source_field}`
  86. columnList = `${columnList}, ${key}`
  87. } else {
  88. first = false
  89. valueList = `@${field.source_field}`
  90. columnList = key
  91. }
  92.  
  93. callback()
  94. }
  95. }, (error) => {
  96. if (error) {
  97. console.error('Error parsing field mapping configuration for Azure SQL Plugin.', error)
  98. plugin.logException(new Error('Error parsing field mapping configuration for Azure SQL Plugin.'))
  99. return setTimeout(() => {
  100. process.exit(1)
  101. }, 2000)
  102. }
  103.  
  104. _query = `INSERT ${options.table} (${columnList}) VALUES (${valueList})`
  105.  
  106. let config = {
  107. userName: options.user,
  108. password: options.password,
  109. server: options.server,
  110. options: {
  111. port: options.port || 1433,
  112. database: options.database,
  113. encrypt: true
  114. }
  115. }
  116.  
  117. _connection = new Connection(config)
  118.  
  119. _connection.on('connect', function (error) {
  120. if (error) {
  121. console.error('Error connecting to Azure SQL DB Connection.', error)
  122. plugin.logException(error)
  123. } else {
  124. plugin.log('Azure SQL DB Connection initialized.')
  125. plugin.emit('init')
  126. }
  127. })
  128. _connection.on('error', function (error) {
  129. console.error('Error on Azure SQL DB Connection.', error)
  130. plugin.logException(error)
  131. })
  132. _connection.on('end', function () {
  133. plugin.log('Azure SQL connection has ended.')
  134. })
  135. })
  136. })
  137.  
  138. module.exports = plugin
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement