songs.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. const CoreClass = require("../core.js");
  2. const async = require("async");
  3. const mongoose = require("mongoose");
  4. class SongsModule extends CoreClass {
  5. constructor() {
  6. super("songs");
  7. }
  8. initialize() {
  9. return new Promise(async (resolve, reject) => {
  10. this.setStage(1);
  11. this.cache = this.moduleManager.modules["cache"];
  12. this.db = this.moduleManager.modules["db"];
  13. this.io = this.moduleManager.modules["io"];
  14. this.utils = this.moduleManager.modules["utils"];
  15. const songModel = await this.db.runJob("GET_MODEL", {
  16. modelName: "song",
  17. });
  18. const songSchema = await this.cache.runJob("GET_SCHEMA", {
  19. schemaName: "song",
  20. });
  21. async.waterfall(
  22. [
  23. (next) => {
  24. this.setStage(2);
  25. this.cache
  26. .runJob("HGETALL", { table: "songs" })
  27. .then((songs) => next(null, songs))
  28. .catch(next);
  29. },
  30. (songs, next) => {
  31. this.setStage(3);
  32. if (!songs) return next();
  33. let songIds = Object.keys(songs);
  34. async.each(
  35. songIds,
  36. (songId, next) => {
  37. songModel.findOne({ songId }, (err, song) => {
  38. if (err) next(err);
  39. else if (!song)
  40. this.cache
  41. .runJob("HDEL", {
  42. table: "songs",
  43. key: songId,
  44. })
  45. .then(() => next())
  46. .catch(next);
  47. else next();
  48. });
  49. },
  50. next
  51. );
  52. },
  53. (next) => {
  54. this.setStage(4);
  55. songModel.find({}, next);
  56. },
  57. (songs, next) => {
  58. this.setStage(5);
  59. async.each(
  60. songs,
  61. (song, next) => {
  62. this.cache
  63. .runJob("HSET", {
  64. table: "songs",
  65. key: song.songId,
  66. value: songSchema.song(song),
  67. })
  68. .then(() => next())
  69. .catch(next);
  70. },
  71. next
  72. );
  73. },
  74. ],
  75. async (err) => {
  76. if (err) {
  77. err = await this.utils.runJob("GET_ERROR", {
  78. error: err,
  79. });
  80. reject(new Error(err));
  81. } else {
  82. resolve();
  83. }
  84. }
  85. );
  86. });
  87. }
  88. /**
  89. * Gets a song by id from the cache or Mongo, and if it isn't in the cache yet, adds it the cache
  90. *
  91. * @param {String} id - the id of the song we are trying to get
  92. * @param {Function} cb - gets called once we're done initializing
  93. */
  94. GET_SONG(payload) {
  95. //id, cb
  96. return new Promise(async (resolve, reject) => {
  97. const songModel = await this.db.runJob("GET_MODEL", {
  98. modelName: "song",
  99. });
  100. async.waterfall(
  101. [
  102. (next) => {
  103. if (!mongoose.Types.ObjectId.isValid(payload.id))
  104. return next("Id is not a valid ObjectId.");
  105. this.runJob("HGET", { table: "songs", key: payload.id })
  106. .then((song) => next(null, song))
  107. .catch(next);
  108. },
  109. (song, next) => {
  110. if (song) return next(true, song);
  111. songModel.findOne({ _id: payload.id }, next);
  112. },
  113. (song, next) => {
  114. if (song) {
  115. this.cache
  116. .runJob("HSET", {
  117. table: "songs",
  118. key: payload.id,
  119. value: song,
  120. })
  121. .then((song) => next(null, song));
  122. } else next("Song not found.");
  123. },
  124. ],
  125. (err, song) => {
  126. if (err && err !== true) return reject(new Error(err));
  127. resolve({ song });
  128. }
  129. );
  130. });
  131. }
  132. /**
  133. * Gets a song by song id from the cache or Mongo, and if it isn't in the cache yet, adds it the cache
  134. *
  135. * @param {String} songId - the mongo id of the song we are trying to get
  136. * @param {Function} cb - gets called once we're done initializing
  137. */
  138. GET_SONG_FROM_ID(payload) {
  139. //songId, cb
  140. return new Promise(async (resolve, reject) => {
  141. const songModel = await this.db.runJob("GET_MODEL", {
  142. modelName: "song",
  143. });
  144. async.waterfall(
  145. [
  146. (next) => {
  147. songModel.findOne({ songId: payload.songId }, next);
  148. },
  149. ],
  150. (err, song) => {
  151. if (err && err !== true) return reject(new Error(err));
  152. resolve({ song });
  153. }
  154. );
  155. });
  156. }
  157. /**
  158. * Gets a song from id from Mongo and updates the cache with it
  159. *
  160. * @param {String} songId - the id of the song we are trying to update
  161. * @param {Function} cb - gets called when an error occurred or when the operation was successful
  162. */
  163. UPDATE_SONG(payload) {
  164. //songId, cb
  165. return new Promise(async (resolve, reject) => {
  166. const songModel = await this.db.runJob("GET_MODEL", {
  167. modelName: "song",
  168. });
  169. async.waterfall(
  170. [
  171. (next) => {
  172. songModel.findOne({ _id: payload.songId }, next);
  173. },
  174. (song, next) => {
  175. if (!song) {
  176. this.cache.runJob("HDEL", {
  177. table: "songs",
  178. key: payload.songId,
  179. });
  180. return next("Song not found.");
  181. }
  182. this.cache
  183. .runJob("HSET", {
  184. table: "songs",
  185. key: payload.songId,
  186. value: song,
  187. })
  188. .then((song) => next(null, song))
  189. .catch(next);
  190. },
  191. ],
  192. (err, song) => {
  193. if (err && err !== true) return reject(new Error(err));
  194. resolve(song);
  195. }
  196. );
  197. });
  198. }
  199. /**
  200. * Deletes song from id from Mongo and cache
  201. *
  202. * @param {String} songId - the id of the song we are trying to delete
  203. * @param {Function} cb - gets called when an error occurred or when the operation was successful
  204. */
  205. DELETE_SONG(payload) {
  206. //songId, cb
  207. return new Promise(async (resolve, reject) => {
  208. const songModel = await this.db.runJob("GET_MODEL", {
  209. modelName: "song",
  210. });
  211. async.waterfall(
  212. [
  213. (next) => {
  214. songModel.deleteOne({ songId: payload.songId }, next);
  215. },
  216. (next) => {
  217. this.cache
  218. .runJob("HDEL", {
  219. table: "songs",
  220. key: payload.songId,
  221. })
  222. .then(() => next())
  223. .catch(next);
  224. },
  225. ],
  226. (err) => {
  227. if (err && err !== true) return reject(new Error(err));
  228. resolve();
  229. }
  230. );
  231. });
  232. }
  233. }
  234. module.exports = new SongsModule();