Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const faker = require('faker')
- const Sequelize = require('sequelize')
- const uuid = require('uuid4')
- const username = 'postgres'
- const password = '******'
- const config = {
- dialect: 'postgres',
- logging: false,
- host: 'localhost',
- pool: {
- idle: 30000,
- acquire: 100000,
- },
- }
- const sequelizeUser = new Sequelize('******', username, password, config)
- const addUser = ({
- email, hash = 'hash', role = 'admin', name = 'name', tenantIds = '{}', activeTenant = '1',
- }) => {
- const createdAt = (new Date()).toUTCString()
- const updatedAt = (new Date()).toUTCString()
- const query = `
- INSERT INTO "Users"(
- id, email, hash, role, name, "tenantIds", "activeTenant", "createdAt", "updatedAt")
- VALUES ('${uuid()}', '${email}', '${hash}', '${role}', '${name}', '${tenantIds}' ,'${activeTenant}', '${createdAt}', '${updatedAt}');
- `
- return sequelizeUser.query(query)
- }
- const generate = async (amount) => {
- const BATCH_NUMBER = Math.min(amount/10, 10000)
- let i = 0
- await sequelizeUser.authenticate()
- console.log('cmon')
- // console.log('connected?', err)
- let promisesBatch = []
- const batches = amount / BATCH_NUMBER
- for (let nBatch = 0; nBatch < batches; nBatch++) {
- promisesBatch = []
- for (let nPromise = 0; nPromise < BATCH_NUMBER; nPromise++) {
- i++
- console.log('i = ', i)
- const email = Date.now() + faker.internet.email()
- const name = `${Date.now()}-name`
- const tenantIds = `{${uuid()}}`
- const addUserPromise = addUser({ email, name, tenantIds })
- promisesBatch.push(addUserPromise)
- }
- await Promise.all(promisesBatch)
- .then(() => {
- console.log('after promise, i = ', i)
- })
- .catch((e) => {
- console.error('error in .all', e)
- })
- }
- process.exit(0)
- }
- generate(300000)
Add Comment
Please, Sign In to add comment