opt-cc/api/test/content-tool/user-db-item-creator.js

58 lines
1.4 KiB
JavaScript

const config = require('../../config/config');
const mongoose = require('mongoose');
mongoose.connect(config.database.uri + config.database.db);
const UserModel = require('../../models/user');
const squads = ['590dd702b899f8050691f09a', '59101b6366d61c085a9f57df',
'591025e266d61c085a9f57e1', '591025cf66d61c085a9f57e0', '590d2cf5bd08480bd4ecc128'];
/**
* provide number of users to create as argument
*/
const quantity = process.argv[2];
// CONNECTION EVENTS
// When successfully connected
mongoose.connection.on('connected', () => {
console.log('Mongoose default connection open');
});
const createString = (possible, length) => {
let text = "";
for (let i = 0; i < length; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
};
for (let i = 0; i < quantity; i++) {
const squadId = squads[Math.floor(Math.random() * squads.length)];
UserModel.create({
username: createString('abcdefghijklmnopqrstuvwxyz0123456789', 10),
squadId: squadId,
rankLvl: Math.floor(Math.random() * 22)
}, function (err, user) {
if (err) {
console.log(err);
} else {
console.log('User created: ' + user);
}
})
}
// If the Node process ends, close the Mongoose connection
process.on('SIGINT', () => {
mongoose.connection.close(function () {
console.log('Mongoose default connection disconnected through app termination');
process.exit(0);
});
});
// process.exit();