Created
December 22, 2016 15:23
-
-
Save jackboberg/1bf5644111ca90a30f82a29788518eac to your computer and use it in GitHub Desktop.
connects to a mongo db and randomly populates it with data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* populator | |
* | |
* connects to a db and randomly populates it with data | |
*/ | |
const MONGO_URL = 'mongodb://user:[email protected]:27017/db_name' | |
const mongoose = require('mongoose') | |
const random = require('mongoose-simple-random') | |
mongoose.connect(MONGO_URL); | |
const db = mongoose.connection | |
var EntrySchema = mongoose.Schema({ | |
name: String, | |
createdAt: Date, | |
updatedAt: Date | |
}); | |
EntrySchema.plugin(random); | |
const Entry = mongoose.model('Entry', EntrySchema) | |
const insertEntry = (Model) => { | |
const obj = { name: 'test', createdAt: Date.now() } | |
Model.create(obj, (err, entry) => { | |
if (err) return console.error(err) | |
console.log(`entry inserted: ${entry._id}`) | |
}) | |
} | |
const updateEntry = (Model) => { | |
Model.findOneRandom((err, entry) => { | |
if (err) return console.error(err) | |
entry.updatedAt = Date.now() | |
entry.name = `${entry.name}.updated` | |
entry.save((err) => { | |
if (err) return console.error(err) | |
console.log(`entry updated: ${entry._id}`) | |
}) | |
}) | |
} | |
const getAll = (Model) => { | |
const query = { name: {'$ne': null } } | |
Model.find(query, (err, entries) => { | |
if (err) return console.error(err) | |
console.log(`got all ${entries.length} entries`) | |
}) | |
} | |
db.on('error', (err) => console.error(err)) | |
db.once('open', () => { | |
console.log('Connected correctly to server') | |
setInterval(insertEntry, 1000, Entry) | |
setInterval(updateEntry, 3000, Entry) | |
setInterval(getAll, 30000, Entry) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment