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

57 lines
1.4 KiB
JavaScript
Raw Normal View History

const config = require('../../config/config');
2017-05-10 11:04:06 +02:00
const mongoose = require('mongoose');
mongoose.connect(config.database.uri + config.database.db);
2017-05-10 11:04:06 +02:00
const UserModel = require('../../models/user');
const squads = ['590dd702b899f8050691f09a', '59101b6366d61c085a9f57df',
'591025e266d61c085a9f57e1', '591025cf66d61c085a9f57e0', '590d2cf5bd08480bd4ecc128'];
2017-05-10 11:04:06 +02:00
/**
* 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');
2017-05-10 11:04:06 +02:00
});
const createString = (possible, length) => {
2018-03-12 09:26:44 +01:00
let text = '';
2018-03-12 09:26:44 +01:00
for (let i = 0; i < length; i++) {
2018-03-12 09:59:43 +01:00
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
};
2017-05-10 11:04:06 +02:00
for (let i = 0; i < quantity; i++) {
const squadId = squads[Math.floor(Math.random() * squads.length)];
UserModel.create({
username: createString('abcdefghijklmnopqrstuvwxyz0123456789', 10),
squadId: squadId,
2018-03-12 09:26:44 +01:00
rankLvl: Math.floor(Math.random() * 22),
2018-03-12 12:35:19 +01:00
}, (err, user) => {
2017-05-10 11:04:06 +02:00
if (err) {
console.log(err);
} else {
console.log('User created: ' + user);
}
2018-03-12 09:26:44 +01:00
});
2017-05-10 11:04:06 +02:00
}
// If the Node process ends, close the Mongoose connection
process.on('SIGINT', () => {
2018-03-12 12:35:19 +01:00
mongoose.connection.close(() => {
2017-05-10 11:04:06 +02:00
console.log('Mongoose default connection disconnected through app termination');
process.exit(0);
});
});
// process.exit();