update.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 { KnownLicenseEntity } from '../../../generated/graphql-server/src/modules/known-license-entity/known-license-entity.model'
  5. import { UserDefinedLicenseEntity } from '../../../generated/graphql-server/src/modules/user-defined-license-entity/user-defined-license-entity.model'
  6. import { VideoMedia } from '../../../generated/graphql-server/src/modules/video-media/video-media.model'
  7. import { Video } from '../../../generated/graphql-server/src/modules/video/video.model'
  8. import { Language } from '../../../generated/graphql-server/src/modules/language/language.model'
  9. import { VideoMediaEncoding } from '../../../generated/graphql-server/src/modules/video-media-encoding/video-media-encoding.model'
  10. import { LicenseEntity } from '../../../generated/graphql-server/src/modules/license-entity/license-entity.model'
  11. import { MediaLocationEntity } from '../../../generated/graphql-server/src/modules/media-location-entity/media-location-entity.model'
  12. import { HttpMediaLocationEntity } from '../../../generated/graphql-server/src/modules/http-media-location-entity/http-media-location-entity.model'
  13. import { JoystreamMediaLocationEntity } from '../../../generated/graphql-server/src/modules/joystream-media-location-entity/joystream-media-location-entity.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. import {
  31. HttpMediaLocation,
  32. JoystreamMediaLocation,
  33. KnownLicense,
  34. UserDefinedLicense,
  35. } from '../../../generated/graphql-server/src/modules/variants/variants.model'
  36. function getEntityIdFromReferencedField(ref: IReference, entityIdBeforeTransaction: number): string {
  37. const { entityId, existing } = ref
  38. const id = existing ? entityId : entityIdBeforeTransaction + entityId
  39. return id.toString()
  40. }
  41. async function updateMediaLocationEntityPropertyValues(
  42. db: DB,
  43. where: IWhereCond,
  44. props: IMediaLocation,
  45. entityIdBeforeTransaction: number
  46. ): Promise<void> {
  47. const { httpMediaLocation, joystreamMediaLocation } = props
  48. const record = await db.get(MediaLocationEntity, where)
  49. if (record === undefined) throw Error(`MediaLocation entity not found: ${where.where.id}`)
  50. if (httpMediaLocation) {
  51. const id = getEntityIdFromReferencedField(httpMediaLocation, entityIdBeforeTransaction)
  52. record.httpMediaLocation = await db.get(HttpMediaLocationEntity, { where: { id } })
  53. }
  54. if (joystreamMediaLocation) {
  55. const id = getEntityIdFromReferencedField(joystreamMediaLocation, entityIdBeforeTransaction)
  56. record.joystreamMediaLocation = await db.get(JoystreamMediaLocationEntity, { where: { id } })
  57. }
  58. await db.save<MediaLocationEntity>(record)
  59. }
  60. async function updateLicenseEntityPropertyValues(
  61. db: DB,
  62. where: IWhereCond,
  63. props: ILicense,
  64. entityIdBeforeTransaction: number
  65. ): Promise<void> {
  66. const record = await db.get(LicenseEntity, where)
  67. if (record === undefined) throw Error(`License entity not found: ${where.where.id}`)
  68. const { knownLicense, userDefinedLicense } = props
  69. if (knownLicense) {
  70. const id = getEntityIdFromReferencedField(knownLicense, entityIdBeforeTransaction)
  71. record.knownLicense = await db.get(KnownLicenseEntity, { where: { id } })
  72. }
  73. if (userDefinedLicense) {
  74. const id = getEntityIdFromReferencedField(userDefinedLicense, entityIdBeforeTransaction)
  75. record.userdefinedLicense = await db.get(UserDefinedLicenseEntity, { where: { id } })
  76. }
  77. await db.save<LicenseEntity>(record)
  78. }
  79. async function updateCategoryEntityPropertyValues(db: DB, where: IWhereCond, props: ICategory): Promise<void> {
  80. const record = await db.get(Category, where)
  81. if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
  82. Object.assign(record, props)
  83. await db.save<Category>(record)
  84. }
  85. async function updateChannelEntityPropertyValues(
  86. db: DB,
  87. where: IWhereCond,
  88. props: IChannel,
  89. entityIdBeforeTransaction: number
  90. ): Promise<void> {
  91. const record = await db.get(Channel, where)
  92. if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
  93. let lang: Language | undefined = record.language
  94. if (props.language) {
  95. const id = getEntityIdFromReferencedField(props.language, entityIdBeforeTransaction)
  96. lang = await db.get(Language, { where: { id } })
  97. if (lang === undefined) throw Error(`Language entity not found: ${id}`)
  98. props.language = undefined
  99. }
  100. Object.assign(record, props)
  101. record.language = lang
  102. await db.save<Channel>(record)
  103. }
  104. async function updateVideoMediaEntityPropertyValues(
  105. db: DB,
  106. where: IWhereCond,
  107. props: IVideoMedia,
  108. entityIdBeforeTransaction: number
  109. ): Promise<void> {
  110. const record = await db.get(VideoMedia, where)
  111. if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
  112. let enco: VideoMediaEncoding | undefined
  113. let mediaLoc: HttpMediaLocation | JoystreamMediaLocation = record.location
  114. const { encoding, location } = props
  115. if (encoding) {
  116. const id = getEntityIdFromReferencedField(encoding, entityIdBeforeTransaction)
  117. enco = await db.get(VideoMediaEncoding, { where: { id } })
  118. if (enco === undefined) throw Error(`VideoMediaEncoding entity not found: ${id}`)
  119. props.encoding = undefined
  120. }
  121. if (location) {
  122. const id = getEntityIdFromReferencedField(location, entityIdBeforeTransaction)
  123. const mLoc = await db.get(MediaLocationEntity, { where: { id } })
  124. if (!mLoc) throw Error(`MediaLocation entity not found: ${id}`)
  125. const { httpMediaLocation, joystreamMediaLocation } = mLoc
  126. if (httpMediaLocation) {
  127. mediaLoc = new HttpMediaLocation()
  128. mediaLoc.isTypeOf = typeof HttpMediaLocation
  129. mediaLoc.url = httpMediaLocation.url
  130. mediaLoc.port = httpMediaLocation.port
  131. }
  132. if (joystreamMediaLocation) {
  133. mediaLoc = new JoystreamMediaLocation()
  134. mediaLoc.isTypeOf = typeof JoystreamMediaLocation
  135. mediaLoc.dataObjectId = joystreamMediaLocation.dataObjectId
  136. }
  137. props.location = undefined
  138. }
  139. Object.assign(record, props)
  140. record.encoding = enco || record.encoding
  141. record.location = mediaLoc
  142. await db.save<VideoMedia>(record)
  143. }
  144. async function updateVideoEntityPropertyValues(
  145. db: DB,
  146. where: IWhereCond,
  147. props: IVideo,
  148. entityIdBeforeTransaction: number
  149. ): Promise<void> {
  150. const record = await db.get<Video>(Video, where)
  151. if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
  152. let chann: Channel | undefined
  153. let cat: Category | undefined
  154. let lang: Language | undefined
  155. let vMedia: VideoMedia | undefined
  156. let lic: KnownLicense | UserDefinedLicense = record.license
  157. const { channel, category, language, media, license } = props
  158. if (channel) {
  159. const id = getEntityIdFromReferencedField(channel, entityIdBeforeTransaction)
  160. chann = await db.get(Channel, { where: { id } })
  161. if (!chann) throw Error(`Channel entity not found: ${id}`)
  162. props.channel = undefined
  163. }
  164. if (category) {
  165. const id = getEntityIdFromReferencedField(category, entityIdBeforeTransaction)
  166. cat = await db.get(Category, { where: { id } })
  167. if (!cat) throw Error(`Category entity not found: ${id}`)
  168. props.category = undefined
  169. }
  170. if (media) {
  171. const id = getEntityIdFromReferencedField(media, entityIdBeforeTransaction)
  172. vMedia = await db.get(VideoMedia, { where: { id } })
  173. if (!vMedia) throw Error(`VideoMedia entity not found: ${id}`)
  174. props.media = undefined
  175. }
  176. if (license) {
  177. const id = getEntityIdFromReferencedField(license, entityIdBeforeTransaction)
  178. const licenseEntity = await db.get(LicenseEntity, {
  179. where: { id },
  180. relations: ['knownLicense', 'userdefinedLicense'],
  181. })
  182. if (!licenseEntity) throw Error(`License entity not found: ${id}`)
  183. const { knownLicense, userdefinedLicense } = licenseEntity
  184. if (knownLicense) {
  185. lic = new KnownLicense()
  186. lic.code = knownLicense.code
  187. lic.description = knownLicense.description
  188. lic.isTypeOf = 'KnownLicense'
  189. lic.name = knownLicense.name
  190. lic.url = knownLicense.url
  191. }
  192. if (userdefinedLicense) {
  193. lic = new UserDefinedLicense()
  194. lic.content = userdefinedLicense.content
  195. lic.isTypeOf = 'UserDefinedLicense'
  196. }
  197. props.license = undefined
  198. }
  199. if (language) {
  200. const id = getEntityIdFromReferencedField(language, entityIdBeforeTransaction)
  201. lang = await db.get(Language, { where: { id } })
  202. if (!lang) throw Error(`Language entity not found: ${id}`)
  203. props.language = undefined
  204. }
  205. Object.assign(record, props)
  206. record.channel = chann || record.channel
  207. record.category = cat || record.category
  208. record.media = vMedia || record.media
  209. record.license = lic
  210. record.language = lang
  211. await db.save<Video>(record)
  212. }
  213. async function updateUserDefinedLicenseEntityPropertyValues(
  214. db: DB,
  215. where: IWhereCond,
  216. props: IUserDefinedLicense
  217. ): Promise<void> {
  218. const record = await db.get(UserDefinedLicenseEntity, where)
  219. if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
  220. Object.assign(record, props)
  221. await db.save<UserDefinedLicenseEntity>(record)
  222. }
  223. async function updateKnownLicenseEntityPropertyValues(db: DB, where: IWhereCond, props: IKnownLicense): Promise<void> {
  224. const record = await db.get(KnownLicenseEntity, where)
  225. if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
  226. Object.assign(record, props)
  227. await db.save<KnownLicenseEntity>(record)
  228. }
  229. async function updateHttpMediaLocationEntityPropertyValues(
  230. db: DB,
  231. where: IWhereCond,
  232. props: IHttpMediaLocation
  233. ): Promise<void> {
  234. const record = await db.get(HttpMediaLocationEntity, where)
  235. if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
  236. Object.assign(record, props)
  237. await db.save<HttpMediaLocationEntity>(record)
  238. }
  239. async function updateJoystreamMediaLocationEntityPropertyValues(
  240. db: DB,
  241. where: IWhereCond,
  242. props: IJoystreamMediaLocation
  243. ): Promise<void> {
  244. const record = await db.get(JoystreamMediaLocationEntity, where)
  245. if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
  246. Object.assign(record, props)
  247. await db.save<JoystreamMediaLocationEntity>(record)
  248. }
  249. async function updateLanguageEntityPropertyValues(db: DB, where: IWhereCond, props: ILanguage): Promise<void> {
  250. const record = await db.get(Language, where)
  251. if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
  252. Object.assign(record, props)
  253. await db.save<Language>(record)
  254. }
  255. async function updateVideoMediaEncodingEntityPropertyValues(
  256. db: DB,
  257. where: IWhereCond,
  258. props: IVideoMediaEncoding
  259. ): Promise<void> {
  260. const record = await db.get(VideoMediaEncoding, where)
  261. if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
  262. Object.assign(record, props)
  263. await db.save<VideoMediaEncoding>(record)
  264. }
  265. export {
  266. updateCategoryEntityPropertyValues,
  267. updateChannelEntityPropertyValues,
  268. updateVideoMediaEntityPropertyValues,
  269. updateVideoEntityPropertyValues,
  270. updateUserDefinedLicenseEntityPropertyValues,
  271. updateHttpMediaLocationEntityPropertyValues,
  272. updateJoystreamMediaLocationEntityPropertyValues,
  273. updateKnownLicenseEntityPropertyValues,
  274. updateLanguageEntityPropertyValues,
  275. updateVideoMediaEncodingEntityPropertyValues,
  276. updateLicenseEntityPropertyValues,
  277. updateMediaLocationEntityPropertyValues,
  278. }