update.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 {
  15. ICategory,
  16. IChannel,
  17. IHttpMediaLocation,
  18. IJoystreamMediaLocation,
  19. IKnownLicense,
  20. ILanguage,
  21. ILicense,
  22. IMediaLocation,
  23. IReference,
  24. IUserDefinedLicense,
  25. IVideo,
  26. IVideoMedia,
  27. IVideoMediaEncoding,
  28. IWhereCond,
  29. } from '../../types'
  30. function getEntityIdFromReferencedField(ref: IReference, entityIdBeforeTransaction: number): string {
  31. const { entityId, existing } = ref
  32. const id = existing ? entityId : entityIdBeforeTransaction + entityId
  33. return id.toString()
  34. }
  35. async function updateMediaLocationEntityPropertyValues(
  36. db: DB,
  37. where: IWhereCond,
  38. props: IMediaLocation,
  39. entityIdBeforeTransaction: number
  40. ): Promise<void> {
  41. const { httpMediaLocation, joystreamMediaLocation } = props
  42. const record = await db.get(MediaLocation, where)
  43. if (record === undefined) throw Error(`MediaLocation entity not found: ${where.where.id}`)
  44. if (httpMediaLocation) {
  45. const id = getEntityIdFromReferencedField(httpMediaLocation, entityIdBeforeTransaction)
  46. record.httpMediaLocation = await db.get(HttpMediaLocation, { where: { id } })
  47. }
  48. if (joystreamMediaLocation) {
  49. const id = getEntityIdFromReferencedField(joystreamMediaLocation, entityIdBeforeTransaction)
  50. record.joystreamMediaLocation = await db.get(JoystreamMediaLocation, { where: { id } })
  51. }
  52. await db.save<MediaLocation>(record)
  53. }
  54. async function updateLicenseEntityPropertyValues(
  55. db: DB,
  56. where: IWhereCond,
  57. props: ILicense,
  58. entityIdBeforeTransaction: number
  59. ): Promise<void> {
  60. const record = await db.get(License, where)
  61. if (record === undefined) throw Error(`License entity not found: ${where.where.id}`)
  62. const { knownLicense, userDefinedLicense } = props
  63. if (knownLicense) {
  64. const id = getEntityIdFromReferencedField(knownLicense, entityIdBeforeTransaction)
  65. record.knownLicense = await db.get(KnownLicense, { where: { id } })
  66. }
  67. if (userDefinedLicense) {
  68. const id = getEntityIdFromReferencedField(userDefinedLicense, entityIdBeforeTransaction)
  69. record.userdefinedLicense = await db.get(UserDefinedLicense, { where: { id } })
  70. }
  71. await db.save<License>(record)
  72. }
  73. async function updateCategoryEntityPropertyValues(db: DB, where: IWhereCond, props: ICategory): Promise<void> {
  74. const record = await db.get(Category, where)
  75. if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
  76. Object.assign(record, props)
  77. await db.save<Category>(record)
  78. }
  79. async function updateChannelEntityPropertyValues(
  80. db: DB,
  81. where: IWhereCond,
  82. props: IChannel,
  83. entityIdBeforeTransaction: number
  84. ): Promise<void> {
  85. const record = await db.get(Channel, where)
  86. if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
  87. let lang: Language | undefined
  88. if (props.language !== undefined) {
  89. const id = getEntityIdFromReferencedField(props.language, entityIdBeforeTransaction)
  90. lang = await db.get(Language, { where: { id } })
  91. if (lang === undefined) throw Error(`Language entity not found: ${id}`)
  92. props.language = undefined
  93. }
  94. Object.assign(record, props)
  95. record.language = lang || record.language
  96. await db.save<Channel>(record)
  97. }
  98. async function updateVideoMediaEntityPropertyValues(
  99. db: DB,
  100. where: IWhereCond,
  101. props: IVideoMedia,
  102. entityIdBeforeTransaction: number
  103. ): Promise<void> {
  104. const record = await db.get(VideoMedia, where)
  105. if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
  106. let enco: VideoMediaEncoding | undefined
  107. let mediaLoc: MediaLocation | undefined
  108. const { encoding, location } = props
  109. if (encoding) {
  110. const id = getEntityIdFromReferencedField(encoding, entityIdBeforeTransaction)
  111. enco = await db.get(VideoMediaEncoding, { where: { id } })
  112. if (enco === undefined) throw Error(`VideoMediaEncoding entity not found: ${id}`)
  113. props.encoding = undefined
  114. }
  115. if (location) {
  116. const id = getEntityIdFromReferencedField(location, entityIdBeforeTransaction)
  117. mediaLoc = await db.get(MediaLocation, { where: { id } })
  118. if (!mediaLoc) throw Error(`MediaLocation entity not found: ${id}`)
  119. props.location = undefined
  120. }
  121. Object.assign(record, props)
  122. record.encoding = enco || record.encoding
  123. record.location = mediaLoc || record.location
  124. await db.save<VideoMedia>(record)
  125. }
  126. async function updateVideoEntityPropertyValues(
  127. db: DB,
  128. where: IWhereCond,
  129. props: IVideo,
  130. entityIdBeforeTransaction: number
  131. ): Promise<void> {
  132. const record = await db.get<Video>(Video, where)
  133. if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
  134. let chann: Channel | undefined
  135. let cat: Category | undefined
  136. let lang: Language | undefined
  137. let vMedia: VideoMedia | undefined
  138. let lic: License | undefined
  139. const { channel, category, language, media, license } = props
  140. if (channel) {
  141. const id = getEntityIdFromReferencedField(channel, entityIdBeforeTransaction)
  142. chann = await db.get(Channel, { where: { id } })
  143. if (!chann) throw Error(`Channel entity not found: ${id}`)
  144. props.channel = undefined
  145. }
  146. if (category) {
  147. const id = getEntityIdFromReferencedField(category, entityIdBeforeTransaction)
  148. cat = await db.get(Category, { where: { id } })
  149. if (!cat) throw Error(`Category entity not found: ${id}`)
  150. props.category = undefined
  151. }
  152. if (media) {
  153. const id = getEntityIdFromReferencedField(media, entityIdBeforeTransaction)
  154. vMedia = await db.get(VideoMedia, { where: { id } })
  155. if (!vMedia) throw Error(`VideoMedia entity not found: ${id}`)
  156. props.media = undefined
  157. }
  158. if (license) {
  159. const id = getEntityIdFromReferencedField(license, entityIdBeforeTransaction)
  160. lic = await db.get(License, { where: { id } })
  161. if (!lic) throw Error(`License entity not found: ${id}`)
  162. props.license = undefined
  163. }
  164. if (language) {
  165. const id = getEntityIdFromReferencedField(language, entityIdBeforeTransaction)
  166. lang = await db.get(Language, { where: { id } })
  167. if (!lang) throw Error(`Language entity not found: ${id}`)
  168. props.language = undefined
  169. }
  170. Object.assign(record, props)
  171. record.channel = chann || record.channel
  172. record.category = cat || record.category
  173. record.media = vMedia || record.media
  174. record.license = lic || record.license
  175. record.language = lang
  176. await db.save<Video>(record)
  177. }
  178. async function updateUserDefinedLicenseEntityPropertyValues(
  179. db: DB,
  180. where: IWhereCond,
  181. props: IUserDefinedLicense
  182. ): Promise<void> {
  183. const record = await db.get(UserDefinedLicense, where)
  184. if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
  185. Object.assign(record, props)
  186. await db.save<UserDefinedLicense>(record)
  187. }
  188. async function updateKnownLicenseEntityPropertyValues(db: DB, where: IWhereCond, props: IKnownLicense): Promise<void> {
  189. const record = await db.get(KnownLicense, where)
  190. if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
  191. Object.assign(record, props)
  192. await db.save<KnownLicense>(record)
  193. }
  194. async function updateHttpMediaLocationEntityPropertyValues(
  195. db: DB,
  196. where: IWhereCond,
  197. props: IHttpMediaLocation
  198. ): Promise<void> {
  199. const record = await db.get(HttpMediaLocation, where)
  200. if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
  201. Object.assign(record, props)
  202. await db.save<HttpMediaLocation>(record)
  203. }
  204. async function updateJoystreamMediaLocationEntityPropertyValues(
  205. db: DB,
  206. where: IWhereCond,
  207. props: IJoystreamMediaLocation
  208. ): Promise<void> {
  209. const record = await db.get(JoystreamMediaLocation, where)
  210. if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
  211. Object.assign(record, props)
  212. await db.save<JoystreamMediaLocation>(record)
  213. }
  214. async function updateLanguageEntityPropertyValues(db: DB, where: IWhereCond, props: ILanguage): Promise<void> {
  215. const record = await db.get(Language, where)
  216. if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
  217. Object.assign(record, props)
  218. await db.save<Language>(record)
  219. }
  220. async function updateVideoMediaEncodingEntityPropertyValues(
  221. db: DB,
  222. where: IWhereCond,
  223. props: IVideoMediaEncoding
  224. ): Promise<void> {
  225. const record = await db.get(VideoMediaEncoding, where)
  226. if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
  227. Object.assign(record, props)
  228. await db.save<VideoMediaEncoding>(record)
  229. }
  230. export {
  231. updateCategoryEntityPropertyValues,
  232. updateChannelEntityPropertyValues,
  233. updateVideoMediaEntityPropertyValues,
  234. updateVideoEntityPropertyValues,
  235. updateUserDefinedLicenseEntityPropertyValues,
  236. updateHttpMediaLocationEntityPropertyValues,
  237. updateJoystreamMediaLocationEntityPropertyValues,
  238. updateKnownLicenseEntityPropertyValues,
  239. updateLanguageEntityPropertyValues,
  240. updateVideoMediaEncodingEntityPropertyValues,
  241. updateLicenseEntityPropertyValues,
  242. updateMediaLocationEntityPropertyValues,
  243. }