remove.ts 6.5 KB

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