Guest User

Untitled

a guest
Jun 15th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. const sql = require('mssql')
  2.  
  3.  
  4. const config = {
  5. user: '',
  6. password: '',
  7. server: '', // You can use 'localhost\\instance' to connect to named instance
  8. database: '',
  9. domain:'',
  10. options: {
  11. encrypt: false // Use this if you're on Windows Azure
  12. }
  13. }
  14.  
  15.  
  16. module.exports.insertBulk = async (data) => {
  17. try {
  18. const pool = await sql.connect(config);
  19. const table = new sql.Table('BillingService.dbo.StagingIncomingPayments');
  20.  
  21. table.create = false;
  22. // guess Id is auto generated
  23. // table.columns.add('Id', sql.Int, {nullable: false, primary: true});
  24. table.columns.add('TowId', sql.NVarChar(50), {nullable: false});
  25. table.columns.add('WorkType', sql.NVarChar(50), {nullable: false});
  26. table.columns.add('WorkDate', sql.DateTime, {nullable: false});
  27. table.columns.add('ContractorId', sql.NVarChar(50), {nullable: false});
  28. table.columns.add('RegionCode', sql.NVarChar(50), {nullable: false});
  29. table.columns.add('ItemCode', sql.NVarChar(50), {nullable: false});
  30. table.columns.add('Quantity', sql.Int, {nullable: false});
  31. // table.columns.add('TimeStamp', sql.NVarChar(50), {nullable: true});
  32.  
  33.  
  34.  
  35. data.map((record) => {
  36. table.rows.add(record.TowId, record.WorkType, record.WorkDate,
  37. record.ContractorId, record.RegionCode, record.ItemCode, record.Quantity);
  38. });
  39.  
  40. const request = new sql.Request()
  41. await request.bulk(table, (err, result) => {
  42. if (err) {
  43. console.log(err);
  44. }
  45. else {
  46. console.log(result);
  47. pool.close();
  48. }
  49. });
  50.  
  51. } catch (err) {
  52. console.log(err)
  53. }
  54. }
Add Comment
Please, Sign In to add comment