Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. const Sequelize = require('sequelize');
  2. const { exec } = require('child_process');
  3.  
  4. const execAsync = function(command) {
  5. return new Promise((resolve, reject) => {
  6. exec(command, (err, stdout, stderr) => {
  7. if (err) {
  8. reject(err);
  9. }
  10.  
  11. resolve(stdout)
  12. });
  13. });
  14. }
  15.  
  16. const sequelize = new Sequelize('sequelize-fiddle', 'root', 'asdqwe123', {
  17. host: 'localhost',
  18. dialect: 'mysql',
  19. operatorsAliases: false,
  20. logging: false,
  21. pool: {
  22. max: 5,
  23. min: 0,
  24. acquire: 30000,
  25. idle: 10000
  26. },
  27. retry: {
  28. match: [
  29. /SequelizeConnectionError/,
  30. /SequelizeConnectionRefusedError/,
  31. /SequelizeHostNotFoundError/,
  32. /SequelizeHostNotReachableError/,
  33. /SequelizeInvalidConnectionError/,
  34. /SequelizeConnectionTimedOutError/,
  35. /SequelizeDatabaseError/
  36. ],
  37. name: 'query',
  38. backoffBase: 100,
  39. backoffExponent: 1.1,
  40. timeout: 60000,
  41. max: Infinity
  42. }
  43. });
  44.  
  45. const matchSchema = {
  46. id: {
  47. type: Sequelize.INTEGER,
  48. autoIncrement: true,
  49. primaryKey: true
  50. },
  51. name: {
  52. type: Sequelize.STRING
  53. }
  54. };
  55.  
  56. // const playerSchema = {
  57. // id: {
  58. // type: Sequelize.INTEGER,
  59. // autoIncrement: true,
  60. // primaryKey: true
  61. // },
  62. // name: {
  63. // type: Sequelize.STRING,
  64. // allowNull: false
  65. // },
  66. // };
  67.  
  68. // const matchPlayerSchema = {
  69. // order: {
  70. // type: Sequelize.INTEGER,
  71. // allowNull: false
  72. // },
  73. // };
  74.  
  75. const Match = sequelize.define('match', matchSchema);
  76. // const Player = sequelize.define('player', playerSchema);
  77. // const MatchPlayer = sequelize.define('match_player', matchPlayerSchema);
  78.  
  79. // Match.belongsToMany(Player, { through: MatchPlayer });
  80. // Player.belongsToMany(Match, { through: MatchPlayer });
  81.  
  82. (async () => {
  83. await Match.sync();
  84. // await Player.sync();
  85. // await MatchPlayer.sync();
  86.  
  87. await Match.destroy({
  88. where: {},
  89. truncate: true
  90. });
  91.  
  92. setTimeout(async () => {
  93. console.log('Stopping mysql service..');
  94. await execAsync('sudo service mysql stop');
  95. console.log('..done!');
  96. }, 5000);
  97.  
  98. setTimeout(async () => {
  99. console.log('Starting mysql service..');
  100. await execAsync('sudo service mysql start');
  101. console.log('..done!');
  102. }, 10000);
  103.  
  104. for (let i = 1; i < 10000; i++) {
  105. console.log('Storing:', i);
  106. const match = new Match();
  107. match.name = String(i);
  108. await match.save();
  109.  
  110. await sleep(100);
  111. }
  112.  
  113. })();
  114.  
  115.  
  116. function sleep(delay) {
  117. return new Promise((resolve) => {
  118. setTimeout(resolve, delay);
  119. });
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement