resolver-mongo.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. const mongoose = require('mongoose');
  2. const Schema = mongoose.Schema,
  3. ObjectId = Schema.ObjectId;
  4. const WorldSchema = new mongoose.Schema({
  5. id: Number,
  6. randomNumber: Number
  7. }, {
  8. collection: 'world'
  9. }),
  10. World = mongoose.model('world', WorldSchema);
  11. const FortuneSchema = new mongoose.Schema({
  12. id: Number,
  13. message: String
  14. }, {
  15. collection: 'fortune'
  16. }),
  17. Fortune = mongoose.model('fortune', FortuneSchema);
  18. // Methods
  19. const randomizeNum = () => {
  20. return Math.floor(Math.random() * 10000) + 1;
  21. };
  22. async function arrayOfRandomWorlds(totalWorldToReturn) {
  23. var totalIterations = sanititizeTotal(totalWorldToReturn);
  24. var arr = [];
  25. return new Promise(async (resolve, reject) => {
  26. for(var i = 0; i < totalIterations; i++) {
  27. arr.push(await World.findOne({ id: randomizeNum() }));
  28. }
  29. if(arr.length == totalIterations) {
  30. resolve(arr);
  31. }
  32. });
  33. };
  34. async function updateRandomWorlds(totalToUpdate) {
  35. const totalIterations = sanititizeTotal(totalToUpdate);
  36. var arr = [];
  37. return new Promise(async (resolve, reject) => {
  38. for(var i = 0; i < totalIterations; i++) {
  39. arr.push(await World.findOneAndUpdate({ id: randomizeNum() }, { randomNumber: randomizeNum() }));
  40. }
  41. if(arr.length == totalIterations) {
  42. resolve(arr);
  43. }
  44. });
  45. };
  46. const sanititizeTotal = (total) => {
  47. var totalIterations;
  48. if (!total) {
  49. totalIterations = 1;
  50. } else if(total < 501 && total > 0) {
  51. totalIterations = total;
  52. } else if (total > 500) {
  53. totalIterations = 500;
  54. } else {
  55. totalIterations = 1;
  56. }
  57. return totalIterations;
  58. };
  59. const sayHello = () => {
  60. var helloWorld = new Object;
  61. helloWorld.message = "Hello, World!";
  62. return JSON.stringify(helloWorld);
  63. };
  64. module.exports = {
  65. Query: {
  66. helloWorld: () => sayHello(),
  67. getAllWorlds: async() => await World.find({}),
  68. singleDatabaseQuery: async() => await World.findOne({ id: randomizeNum() }),
  69. multipleDatabaseQueries: async(parent, args) => await arrayOfRandomWorlds(args.total),
  70. getWorldById: async(parent, args) => await World.findById(args.id),
  71. getAllFortunes: async() => await Fortune.find({}),
  72. getRandomAndUpdate: async(parent, args) => await updateRandomWorlds(args.total)
  73. },
  74. Mutation: {
  75. createWorld: async(parent, args) => {
  76. let randInt = Math.floor(Math.random() * 1000) + 1;
  77. return await World.create({ id: null, randomNumber: randInt });
  78. },
  79. updateWorld: async(parent, args) => {
  80. return await World.update({id: args.id, randomNumber: args.randomNumber});
  81. }
  82. }
  83. }