Advertisement
Guest User

Untitled

a guest
Apr 18th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. var faker = require('faker');
  2. var Post = require('./models/blogPost');
  3. var Comment = require('./models/comments');
  4. var User = require('./models/user');
  5.  
  6.  
  7.  
  8. module.exports = {
  9.  
  10. postsCount: function() {
  11. Post.count(function(err, count){
  12. if(err){
  13. console.log("error in count")
  14. } else {
  15. console.log("COUNT IS", count)
  16. return count
  17. }
  18. })
  19. },
  20.  
  21. makeData: function(){
  22.  
  23. if( this.postsCount() < 3 ) {
  24. console.log("CREATING DATA");
  25. this.createBlogPosts();
  26. this.createUsers();
  27. setTimeout(this.createComments(), 5000);
  28. }
  29. console.log("NO NEED FOR NEW DATA");
  30.  
  31. },
  32.  
  33. createBlogPosts: function(){
  34.  
  35. for (var i = 0; i < 25; i++) {
  36. var post = new Post({
  37. author: faker.name.findName(),
  38. title: faker.lorem.words(),
  39. content: faker.lorem.paragraphs(),
  40. date: faker.date.recent(),
  41. });
  42.  
  43. post.save();
  44. };
  45. },
  46.  
  47. createUsers: function() {
  48.  
  49. for (var i = 0; i < 40; i++) {
  50.  
  51. var user = new User({
  52. local: {
  53. email: faker.internet.email(),
  54. password: faker.internet.password(),
  55. username: faker.internet.userName(),
  56. }
  57. })
  58.  
  59. user.save();
  60. };
  61. },
  62.  
  63. getRandomUsersId: function() {
  64.  
  65. User.count().exec(function(err, count){
  66.  
  67. var random = Math.floor(Math.random() * count);
  68.  
  69. User.findOne().skip(random).exec(
  70. function (err, result) {
  71.  
  72. return result._id
  73.  
  74. });
  75.  
  76. });
  77.  
  78. },
  79.  
  80. getRandomPostId: function() {
  81. Post.count().exec(function(err, count){
  82.  
  83. var random = Math.floor(Math.random() * count);
  84.  
  85. Post.findOne().skip(random).exec(
  86. function (err, result) {
  87.  
  88.  
  89. return result._id
  90.  
  91. });
  92.  
  93. });
  94. },
  95.  
  96. createComments: function() {
  97. for (var i = 0; i < 125; i++) {
  98.  
  99. // These two values are undefined - figure out why and
  100. // create comments will be complete;
  101. console.log(this.getRandomPostId(), this.getRandomUsersId());
  102.  
  103. var comment = new Comment({
  104. body: faker.lorem.sentence(),
  105. date: faker.date.recent(),
  106. blog: this.getRandomPostId(),
  107. user: this.getRandomUsersId(),
  108. });
  109.  
  110. comment.save();
  111.  
  112. };
  113.  
  114. },
  115.  
  116.  
  117. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement