Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var faker = require('faker');
- var Post = require('./models/blogPost');
- var Comment = require('./models/comments');
- var User = require('./models/user');
- module.exports = {
- postsCount: function() {
- Post.count(function(err, count){
- if(err){
- console.log("error in count")
- } else {
- console.log("COUNT IS", count)
- return count
- }
- })
- },
- makeData: function(){
- if( this.postsCount() < 3 ) {
- console.log("CREATING DATA");
- this.createBlogPosts();
- this.createUsers();
- setTimeout(this.createComments(), 5000);
- }
- console.log("NO NEED FOR NEW DATA");
- },
- createBlogPosts: function(){
- for (var i = 0; i < 25; i++) {
- var post = new Post({
- author: faker.name.findName(),
- title: faker.lorem.words(),
- content: faker.lorem.paragraphs(),
- date: faker.date.recent(),
- });
- post.save();
- };
- },
- createUsers: function() {
- for (var i = 0; i < 40; i++) {
- var user = new User({
- local: {
- email: faker.internet.email(),
- password: faker.internet.password(),
- username: faker.internet.userName(),
- }
- })
- user.save();
- };
- },
- getRandomUsersId: function() {
- User.count().exec(function(err, count){
- var random = Math.floor(Math.random() * count);
- User.findOne().skip(random).exec(
- function (err, result) {
- return result._id
- });
- });
- },
- getRandomPostId: function() {
- Post.count().exec(function(err, count){
- var random = Math.floor(Math.random() * count);
- Post.findOne().skip(random).exec(
- function (err, result) {
- return result._id
- });
- });
- },
- createComments: function() {
- for (var i = 0; i < 125; i++) {
- // These two values are undefined - figure out why and
- // create comments will be complete;
- console.log(this.getRandomPostId(), this.getRandomUsersId());
- var comment = new Comment({
- body: faker.lorem.sentence(),
- date: faker.date.recent(),
- blog: this.getRandomPostId(),
- user: this.getRandomUsersId(),
- });
- comment.save();
- };
- },
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement