2022-05-22 19:29:02 +02:00
|
|
|
// get the JWT secret etc
|
|
|
|
require("../../src/environment")
|
|
|
|
require("@budibase/backend-core").init()
|
|
|
|
const {
|
|
|
|
getProdAppID,
|
|
|
|
generateGlobalUserID,
|
|
|
|
} = require("@budibase/backend-core/db")
|
|
|
|
const { doInTenant, getGlobalDB } = require("@budibase/backend-core/tenancy")
|
|
|
|
const { internalSaveUser } = require("@budibase/backend-core/utils")
|
|
|
|
const { publicApiUserFix } = require("../../src/utilities/users")
|
|
|
|
const { hash } = require("@budibase/backend-core/utils")
|
2022-05-22 18:14:24 +02:00
|
|
|
|
|
|
|
const USER_LOAD_NUMBER = 10000
|
2022-05-22 19:29:02 +02:00
|
|
|
const BATCH_SIZE = 200
|
2022-05-22 18:14:24 +02:00
|
|
|
const PASSWORD = "test"
|
2022-05-22 19:29:02 +02:00
|
|
|
const TENANT_ID = "default"
|
2022-05-22 18:14:24 +02:00
|
|
|
|
|
|
|
const APP_ID = process.argv[2]
|
|
|
|
|
|
|
|
const words = [
|
|
|
|
"test",
|
|
|
|
"testing",
|
|
|
|
"budi",
|
|
|
|
"mail",
|
|
|
|
"age",
|
|
|
|
"risk",
|
|
|
|
"load",
|
|
|
|
"uno",
|
|
|
|
"arm",
|
|
|
|
"leg",
|
|
|
|
"pen",
|
|
|
|
"glass",
|
|
|
|
"box",
|
|
|
|
"chicken",
|
|
|
|
"bottle",
|
|
|
|
]
|
|
|
|
|
|
|
|
if (!APP_ID) {
|
|
|
|
console.error("Must supply app ID as first CLI option!")
|
|
|
|
process.exit(-1)
|
|
|
|
}
|
|
|
|
|
|
|
|
const WORD_1 = words[Math.floor(Math.random() * words.length)]
|
|
|
|
const WORD_2 = words[Math.floor(Math.random() * words.length)]
|
2022-05-22 19:29:02 +02:00
|
|
|
let HASHED_PASSWORD
|
2022-05-22 18:14:24 +02:00
|
|
|
|
|
|
|
function generateUser(count) {
|
|
|
|
return {
|
2022-05-22 19:29:02 +02:00
|
|
|
_id: generateGlobalUserID(),
|
|
|
|
password: HASHED_PASSWORD,
|
2022-05-22 18:14:24 +02:00
|
|
|
email: `${WORD_1}${count}@${WORD_2}.com`,
|
|
|
|
roles: {
|
|
|
|
[getProdAppID(APP_ID)]: "BASIC",
|
|
|
|
},
|
|
|
|
status: "active",
|
|
|
|
forceResetPassword: false,
|
|
|
|
firstName: "John",
|
|
|
|
lastName: "Smith",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function run() {
|
2022-05-22 19:29:02 +02:00
|
|
|
HASHED_PASSWORD = await hash(PASSWORD)
|
|
|
|
return doInTenant(TENANT_ID, async () => {
|
|
|
|
const db = getGlobalDB()
|
|
|
|
for (let i = 0; i < USER_LOAD_NUMBER; i += BATCH_SIZE) {
|
|
|
|
let userSavePromises = []
|
|
|
|
for (let j = 0; j < BATCH_SIZE; j++) {
|
|
|
|
// like the public API
|
|
|
|
const ctx = publicApiUserFix({
|
|
|
|
request: {
|
|
|
|
body: generateUser(i + j),
|
2022-05-22 18:14:24 +02:00
|
|
|
},
|
|
|
|
})
|
2022-05-22 19:29:02 +02:00
|
|
|
userSavePromises.push(
|
|
|
|
internalSaveUser(ctx.request.body, TENANT_ID, {
|
|
|
|
hashPassword: false,
|
|
|
|
requirePassword: true,
|
|
|
|
bulkCreate: true,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
const users = await Promise.all(userSavePromises)
|
|
|
|
await db.bulkDocs(users)
|
|
|
|
console.log(`${i + BATCH_SIZE} users have been created.`)
|
2022-05-22 18:14:24 +02:00
|
|
|
}
|
2022-05-22 19:29:02 +02:00
|
|
|
})
|
2022-05-22 18:14:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
run()
|
|
|
|
.then(() => {
|
|
|
|
console.log(`Generated ${USER_LOAD_NUMBER} users!`)
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.error("Failed for reason: ", err)
|
|
|
|
process.exit(-1)
|
|
|
|
})
|