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

63 lines
1.4 KiB
JavaScript

const config = require('../../config');
const mongoose = require('mongoose');
mongoose.connect(config.database);
const UserModel = require('../../models/user');
const squads = ['590ddfd93f4b67019d0ad4b5', '590e366bfbe1835513601cc5',
'590e367cfbe1835513601cc6', '590f77d33de40440ca826263'];
/**
* 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 to ' + config.database);
});
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();
let createString = (possible, length) =>
{
let text = "";
for( var i=0; i < length; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
};