remove.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import assert from 'assert'
  2. import { DB } from '../../../generated/indexer'
  3. import { Channel } from '../../../generated/graphql-server/src/modules/channel/channel.model'
  4. import { Category } from '../../../generated/graphql-server/src/modules/category/category.model'
  5. import { KnownLicenseEntity } from '../../../generated/graphql-server/src/modules/known-license-entity/known-license-entity.model'
  6. import { UserDefinedLicenseEntity } from '../../../generated/graphql-server/src/modules/user-defined-license-entity/user-defined-license-entity.model'
  7. import { JoystreamMediaLocationEntity } from '../../../generated/graphql-server/src/modules/joystream-media-location-entity/joystream-media-location-entity.model'
  8. import { HttpMediaLocationEntity } from '../../../generated/graphql-server/src/modules/http-media-location-entity/http-media-location-entity.model'
  9. import { VideoMedia } from '../../../generated/graphql-server/src/modules/video-media/video-media.model'
  10. import { Video } from '../../../generated/graphql-server/src/modules/video/video.model'
  11. import { Language } from '../../../generated/graphql-server/src/modules/language/language.model'
  12. import { VideoMediaEncoding } from '../../../generated/graphql-server/src/modules/video-media-encoding/video-media-encoding.model'
  13. import { LicenseEntity } from '../../../generated/graphql-server/src/modules/license-entity/license-entity.model'
  14. import { MediaLocationEntity } from '../../../generated/graphql-server/src/modules/media-location-entity/media-location-entity.model'
  15. import { FeaturedVideo } from '../../../generated/graphql-server/src/modules/featured-video/featured-video.model'
  16. import { IWhereCond } from '../../types'
  17. function assertKeyViolation(entityName: string, entityId: string) {
  18. assert(false, `Can not remove ${entityName}(${entityId})! There are references to this entity`)
  19. }
  20. async function removeChannel(db: DB, where: IWhereCond): Promise<void> {
  21. const record = await db.get(Channel, where)
  22. if (!record) throw Error(`Channel(${where.where.id}) not found`)
  23. if (record.videos && record.videos.length) assertKeyViolation(`Channel`, record.id)
  24. await db.remove<Channel>(record)
  25. }
  26. async function removeCategory(db: DB, where: IWhereCond): Promise<void> {
  27. const record = await db.get(Category, where)
  28. if (!record) throw Error(`Category(${where.where.id}) not found`)
  29. if (record.videos && record.videos.length) assertKeyViolation(`Category`, record.id)
  30. await db.remove<Category>(record)
  31. }
  32. async function removeVideoMedia(db: DB, where: IWhereCond): Promise<void> {
  33. const record = await db.get(VideoMedia, where)
  34. if (!record) throw Error(`VideoMedia(${where.where.id}) not found`)
  35. if (record.video) assertKeyViolation(`VideoMedia`, record.id)
  36. await db.remove<VideoMedia>(record)
  37. }
  38. async function removeVideo(db: DB, where: IWhereCond): Promise<void> {
  39. const record = await db.get(Video, where)
  40. if (!record) throw Error(`Video(${where.where.id}) not found`)
  41. await db.remove<Video>(record)
  42. }
  43. async function removeLicense(db: DB, where: IWhereCond): Promise<void> {
  44. const record = await db.get(LicenseEntity, where)
  45. if (!record) throw Error(`License(${where.where.id}) not found`)
  46. if (record.videolicense && record.videolicense.length) assertKeyViolation(`License`, record.id)
  47. await db.remove<LicenseEntity>(record)
  48. }
  49. async function removeUserDefinedLicense(db: DB, where: IWhereCond): Promise<void> {
  50. const record = await db.get(UserDefinedLicenseEntity, where)
  51. if (!record) throw Error(`UserDefinedLicense(${where.where.id}) not found`)
  52. await db.remove<UserDefinedLicenseEntity>(record)
  53. }
  54. async function removeKnownLicense(db: DB, where: IWhereCond): Promise<void> {
  55. const record = await db.get(KnownLicenseEntity, where)
  56. if (!record) throw Error(`KnownLicense(${where.where.id}) not found`)
  57. await db.remove<KnownLicenseEntity>(record)
  58. }
  59. async function removeMediaLocation(db: DB, where: IWhereCond): Promise<void> {
  60. const record = await db.get(MediaLocationEntity, where)
  61. if (!record) throw Error(`MediaLocation(${where.where.id}) not found`)
  62. if (record.videoMedia) assertKeyViolation('MediaLocation', record.id)
  63. await db.remove<MediaLocationEntity>(record)
  64. }
  65. async function removeHttpMediaLocation(db: DB, where: IWhereCond): Promise<void> {
  66. const record = await db.get(HttpMediaLocationEntity, where)
  67. if (!record) throw Error(`HttpMediaLocation(${where.where.id}) not found`)
  68. if (record.medialocationentityhttpMediaLocation && record.medialocationentityhttpMediaLocation.length) {
  69. assertKeyViolation('HttpMediaLocation', record.id)
  70. }
  71. await db.remove<HttpMediaLocationEntity>(record)
  72. }
  73. async function removeJoystreamMediaLocation(db: DB, where: IWhereCond): Promise<void> {
  74. const record = await db.get(JoystreamMediaLocationEntity, where)
  75. if (!record) throw Error(`JoystreamMediaLocation(${where.where.id}) not found`)
  76. if (record.medialocationentityjoystreamMediaLocation && record.medialocationentityjoystreamMediaLocation.length) {
  77. assertKeyViolation('JoystreamMediaLocation', record.id)
  78. }
  79. await db.remove<JoystreamMediaLocationEntity>(record)
  80. }
  81. async function removeLanguage(db: DB, where: IWhereCond): Promise<void> {
  82. const record = await db.get(Language, where)
  83. if (!record) throw Error(`Language(${where.where.id}) not found`)
  84. if (record.channellanguage && record.channellanguage.length) assertKeyViolation('Language', record.id)
  85. if (record.videolanguage && record.videolanguage.length) assertKeyViolation('Language', record.id)
  86. await db.remove<Language>(record)
  87. }
  88. async function removeVideoMediaEncoding(db: DB, where: IWhereCond): Promise<void> {
  89. const record = await db.get(VideoMediaEncoding, where)
  90. if (!record) throw Error(`VideoMediaEncoding(${where.where.id}) not found`)
  91. await db.remove<VideoMediaEncoding>(record)
  92. }
  93. async function removeFeaturedVideo(db: DB, where: IWhereCond): Promise<void> {
  94. const record = await db.get(FeaturedVideo, { ...where, relations: ['video'] })
  95. if (!record) throw Error(`FeaturedVideo(${where.where.id}) not found`)
  96. record.video.isFeatured = false
  97. record.video.featured = undefined
  98. await db.save<Video>(record.video)
  99. await db.remove<FeaturedVideo>(record)
  100. }
  101. export {
  102. removeCategory,
  103. removeChannel,
  104. removeVideoMedia,
  105. removeVideo,
  106. removeUserDefinedLicense,
  107. removeKnownLicense,
  108. removeHttpMediaLocation,
  109. removeJoystreamMediaLocation,
  110. removeLanguage,
  111. removeVideoMediaEncoding,
  112. removeMediaLocation,
  113. removeLicense,
  114. removeFeaturedVideo,
  115. }