Guest User

Untitled

a guest
Mar 29th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. const faker = require('faker')
  2. const Sequelize = require('sequelize')
  3. const uuid = require('uuid4')
  4.  
  5. const username = 'postgres'
  6. const password = '******'
  7. const config = {
  8. dialect: 'postgres',
  9. logging: false,
  10. host: 'localhost',
  11. pool: {
  12. idle: 30000,
  13. acquire: 100000,
  14. },
  15. }
  16.  
  17. const sequelizeUser = new Sequelize('******', username, password, config)
  18.  
  19. const addUser = ({
  20. email, hash = 'hash', role = 'admin', name = 'name', tenantIds = '{}', activeTenant = '1',
  21. }) => {
  22. const createdAt = (new Date()).toUTCString()
  23. const updatedAt = (new Date()).toUTCString()
  24. const query = `
  25. INSERT INTO "Users"(
  26. id, email, hash, role, name, "tenantIds", "activeTenant", "createdAt", "updatedAt")
  27. VALUES ('${uuid()}', '${email}', '${hash}', '${role}', '${name}', '${tenantIds}' ,'${activeTenant}', '${createdAt}', '${updatedAt}');
  28. `
  29. return sequelizeUser.query(query)
  30. }
  31.  
  32.  
  33. const generate = async (amount) => {
  34. const BATCH_NUMBER = Math.min(amount/10, 10000)
  35. let i = 0
  36. await sequelizeUser.authenticate()
  37. console.log('cmon')
  38. // console.log('connected?', err)
  39. let promisesBatch = []
  40. const batches = amount / BATCH_NUMBER
  41. for (let nBatch = 0; nBatch < batches; nBatch++) {
  42. promisesBatch = []
  43. for (let nPromise = 0; nPromise < BATCH_NUMBER; nPromise++) {
  44. i++
  45. console.log('i = ', i)
  46. const email = Date.now() + faker.internet.email()
  47. const name = `${Date.now()}-name`
  48. const tenantIds = `{${uuid()}}`
  49. const addUserPromise = addUser({ email, name, tenantIds })
  50. promisesBatch.push(addUserPromise)
  51. }
  52. await Promise.all(promisesBatch)
  53. .then(() => {
  54. console.log('after promise, i = ', i)
  55. })
  56. .catch((e) => {
  57. console.error('error in .all', e)
  58. })
  59. }
  60. process.exit(0)
  61. }
  62.  
  63. generate(300000)
Add Comment
Please, Sign In to add comment