remove.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { DB } from '../../../generated/indexer'
  2. import { Channel } from '../../../generated/graphql-server/src/modules/channel/channel.model'
  3. import { Category } from '../../../generated/graphql-server/src/modules/category/category.model'
  4. import { KnownLicense } from '../../../generated/graphql-server/src/modules/known-license/known-license.model'
  5. import { UserDefinedLicense } from '../../../generated/graphql-server/src/modules/user-defined-license/user-defined-license.model'
  6. import { JoystreamMediaLocation } from '../../../generated/graphql-server/src/modules/joystream-media-location/joystream-media-location.model'
  7. import { HttpMediaLocation } from '../../../generated/graphql-server/src/modules/http-media-location/http-media-location.model'
  8. import { VideoMedia } from '../../../generated/graphql-server/src/modules/video-media/video-media.model'
  9. import { Video } from '../../../generated/graphql-server/src/modules/video/video.model'
  10. import { Language } from '../../../generated/graphql-server/src/modules/language/language.model'
  11. import { VideoMediaEncoding } from '../../../generated/graphql-server/src/modules/video-media-encoding/video-media-encoding.model'
  12. import { License } from '../../../generated/graphql-server/src/modules/license/license.model'
  13. import { MediaLocation } from '../../../generated/graphql-server/src/modules/media-location/media-location.model'
  14. import { IWhereCond } from '../../types'
  15. async function removeChannel(db: DB, where: IWhereCond): Promise<void> {
  16. const record = await db.get(Channel, where)
  17. if (record === undefined) throw Error(`Channel not found`)
  18. if (record.videos) record.videos.map(async (v) => await removeVideo(db, { where: { id: v.id } }))
  19. await db.remove<Channel>(record)
  20. }
  21. async function removeCategory(db: DB, where: IWhereCond): Promise<void> {
  22. const record = await db.get(Category, where)
  23. if (record === undefined) throw Error(`Category not found`)
  24. if (record.videos) record.videos.map(async (v) => await removeVideo(db, { where: { id: v.id } }))
  25. await db.remove<Category>(record)
  26. }
  27. async function removeVideoMedia(db: DB, where: IWhereCond): Promise<void> {
  28. const record = await db.get(VideoMedia, where)
  29. if (record === undefined) throw Error(`VideoMedia not found`)
  30. if (record.video) await db.remove<Video>(record.video)
  31. await db.remove<VideoMedia>(record)
  32. }
  33. async function removeVideo(db: DB, where: IWhereCond): Promise<void> {
  34. const record = await db.get(Video, where)
  35. if (record === undefined) throw Error(`Video not found`)
  36. await db.remove<Video>(record)
  37. }
  38. async function removeLicense(db: DB, where: IWhereCond): Promise<void> {
  39. const record = await db.get(License, where)
  40. if (record === undefined) throw Error(`License not found`)
  41. // Remove all the videos under this license
  42. if (record.videolicense) record.videolicense.map(async (v) => await removeVideo(db, { where: { id: v.id } }))
  43. await db.remove<License>(record)
  44. }
  45. async function removeUserDefinedLicense(db: DB, where: IWhereCond): Promise<void> {
  46. const record = await db.get(UserDefinedLicense, where)
  47. if (record === undefined) throw Error(`UserDefinedLicense not found`)
  48. if (record.licenseuserdefinedLicense)
  49. record.licenseuserdefinedLicense.map(async (l) => await removeLicense(db, { where: { id: l.id } }))
  50. await db.remove<UserDefinedLicense>(record)
  51. }
  52. async function removeKnownLicense(db: DB, where: IWhereCond): Promise<void> {
  53. const record = await db.get(KnownLicense, where)
  54. if (record === undefined) throw Error(`KnownLicense not found`)
  55. if (record.licenseknownLicense)
  56. record.licenseknownLicense.map(async (k) => await removeLicense(db, { where: { id: k.id } }))
  57. await db.remove<KnownLicense>(record)
  58. }
  59. async function removeMediaLocation(db: DB, where: IWhereCond): Promise<void> {
  60. const record = await db.get(MediaLocation, where)
  61. if (record === undefined) throw Error(`MediaLocation not found`)
  62. if (record.videoMedia) await removeVideo(db, { where: { id: record.videoMedia.id } })
  63. await db.remove<MediaLocation>(record)
  64. }
  65. async function removeHttpMediaLocation(db: DB, where: IWhereCond): Promise<void> {
  66. const record = await db.get(HttpMediaLocation, where)
  67. if (record === undefined) throw Error(`HttpMediaLocation not found`)
  68. if (record.medialocationhttpMediaLocation)
  69. record.medialocationhttpMediaLocation.map(async (v) => await removeMediaLocation(db, { where: { id: v.id } }))
  70. await db.remove<HttpMediaLocation>(record)
  71. }
  72. async function removeJoystreamMediaLocation(db: DB, where: IWhereCond): Promise<void> {
  73. const record = await db.get(JoystreamMediaLocation, where)
  74. if (record === undefined) throw Error(`JoystreamMediaLocation not found`)
  75. if (record.medialocationjoystreamMediaLocation)
  76. record.medialocationjoystreamMediaLocation.map(async (v) => await removeVideo(db, { where: { id: v.id } }))
  77. await db.remove<JoystreamMediaLocation>(record)
  78. }
  79. async function removeLanguage(db: DB, where: IWhereCond): Promise<void> {
  80. const record = await db.get(Language, where)
  81. if (record === undefined) throw Error(`Language not found`)
  82. if (record.channellanguage) record.channellanguage.map(async (c) => await removeChannel(db, { where: { id: c.id } }))
  83. if (record.videolanguage) record.videolanguage.map(async (v) => await removeVideo(db, { where: { id: v.id } }))
  84. await db.remove<Language>(record)
  85. }
  86. async function removeVideoMediaEncoding(db: DB, where: IWhereCond): Promise<void> {
  87. const record = await db.get(VideoMediaEncoding, where)
  88. if (record === undefined) throw Error(`Language not found`)
  89. await db.remove<VideoMediaEncoding>(record)
  90. }
  91. export {
  92. removeCategory,
  93. removeChannel,
  94. removeVideoMedia,
  95. removeVideo,
  96. removeUserDefinedLicense,
  97. removeKnownLicense,
  98. removeHttpMediaLocation,
  99. removeJoystreamMediaLocation,
  100. removeLanguage,
  101. removeVideoMediaEncoding,
  102. removeMediaLocation,
  103. removeLicense,
  104. }