get-or-create.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. import { Channel } from '../../generated/graphql-server/src/modules/channel/channel.model'
  2. import { Category } from '../../generated/graphql-server/src/modules/category/category.model'
  3. import { KnownLicense } from '../../generated/graphql-server/src/modules/known-license/known-license.model'
  4. import { UserDefinedLicense } from '../../generated/graphql-server/src/modules/user-defined-license/user-defined-license.model'
  5. import { JoystreamMediaLocation } from '../../generated/graphql-server/src/modules/joystream-media-location/joystream-media-location.model'
  6. import { HttpMediaLocation } from '../../generated/graphql-server/src/modules/http-media-location/http-media-location.model'
  7. import { VideoMedia } from '../../generated/graphql-server/src/modules/video-media/video-media.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 { License } from '../../generated/graphql-server/src/modules/license/license.model'
  11. import { MediaLocation } from '../../generated/graphql-server/src/modules/media-location/media-location.model'
  12. import { decode } from './decode'
  13. import {
  14. CategoryPropertyNamesWithId,
  15. channelPropertyNamesWithId,
  16. httpMediaLocationPropertyNamesWithId,
  17. joystreamMediaLocationPropertyNamesWithId,
  18. knownLicensePropertyNamesWIthId,
  19. languagePropertyNamesWIthId,
  20. licensePropertyNamesWithId,
  21. mediaLocationPropertyNamesWithId,
  22. userDefinedLicensePropertyNamesWithId,
  23. videoMediaEncodingPropertyNamesWithId,
  24. videoPropertyNamesWithId,
  25. } from './content-dir-consts'
  26. import {
  27. ClassEntityMap,
  28. ICategory,
  29. IChannel,
  30. IDBBlockId,
  31. IEntity,
  32. IHttpMediaLocation,
  33. IJoystreamMediaLocation,
  34. IKnownLicense,
  35. ILanguage,
  36. ILicense,
  37. IMediaLocation,
  38. IUserDefinedLicense,
  39. IVideoMedia,
  40. IVideoMediaEncoding,
  41. } from '../types'
  42. import {
  43. createCategory,
  44. createChannel,
  45. createHttpMediaLocation,
  46. createJoystreamMediaLocation,
  47. createKnownLicense,
  48. createLanguage,
  49. createLicense,
  50. createMediaLocation,
  51. createUserDefinedLicense,
  52. createVideoMedia,
  53. createVideoMediaEncoding,
  54. } from './entity-helper'
  55. function generateEntityIdFromIndex(index: number): string {
  56. return `${index + 1}`
  57. }
  58. function findEntity(entityId: number, className: string, classEntityMap: ClassEntityMap): IEntity {
  59. const newlyCreatedEntities = classEntityMap.get(className)
  60. if (newlyCreatedEntities === undefined) throw Error(`Couldn't find '${className}' entities in the classEntityMap`)
  61. const entity = newlyCreatedEntities.find((e) => e.indexOf === entityId)
  62. if (!entity) throw Error(`Unknown ${className} entity id: ${entityId}`)
  63. return entity
  64. }
  65. async function language(
  66. { db, block }: IDBBlockId,
  67. classEntityMap: ClassEntityMap,
  68. entityId: number
  69. ): Promise<Language> {
  70. const entity = findEntity(entityId, 'Language', classEntityMap)
  71. const record = await createLanguage(
  72. { db, block, id: generateEntityIdFromIndex(entityId) },
  73. decode.setEntityPropertyValues<ILanguage>(entity.properties, languagePropertyNamesWIthId)
  74. )
  75. removeInsertedEntity('Language', entityId, classEntityMap)
  76. return record
  77. }
  78. async function videoMediaEncoding(
  79. { db, block }: IDBBlockId,
  80. classEntityMap: ClassEntityMap,
  81. entityId: number
  82. ): Promise<VideoMediaEncoding> {
  83. const entity = findEntity(entityId, 'VideoMediaEncoding', classEntityMap)
  84. const record = await createVideoMediaEncoding(
  85. { db, block, id: generateEntityIdFromIndex(entityId) },
  86. decode.setEntityPropertyValues<IVideoMediaEncoding>(entity.properties, videoMediaEncodingPropertyNamesWithId)
  87. )
  88. removeInsertedEntity('VideoMediaEncoding', entityId, classEntityMap)
  89. return record
  90. }
  91. async function videoMedia(
  92. { db, block }: IDBBlockId,
  93. classEntityMap: ClassEntityMap,
  94. entityId: number
  95. ): Promise<VideoMedia> {
  96. const entity = findEntity(entityId, 'VideoMedia', classEntityMap)
  97. const record = await createVideoMedia(
  98. { db, block, id: generateEntityIdFromIndex(entityId) },
  99. classEntityMap,
  100. decode.setEntityPropertyValues<IVideoMedia>(entity.properties, videoPropertyNamesWithId)
  101. )
  102. removeInsertedEntity('VideoMedia', entityId, classEntityMap)
  103. return record
  104. }
  105. async function knownLicense(
  106. { db, block }: IDBBlockId,
  107. classEntityMap: ClassEntityMap,
  108. entityId: number
  109. ): Promise<KnownLicense> {
  110. const entity = findEntity(entityId, 'KnownLicense', classEntityMap)
  111. const record = await createKnownLicense(
  112. { db, block, id: generateEntityIdFromIndex(entityId) },
  113. decode.setEntityPropertyValues<IKnownLicense>(entity.properties, knownLicensePropertyNamesWIthId)
  114. )
  115. removeInsertedEntity('KnownLicense', entityId, classEntityMap)
  116. return record
  117. }
  118. async function userDefinedLicense(
  119. { db, block }: IDBBlockId,
  120. classEntityMap: ClassEntityMap,
  121. entityId: number
  122. ): Promise<UserDefinedLicense> {
  123. const entity = findEntity(entityId, 'UserDefinedLicense', classEntityMap)
  124. const record = await createUserDefinedLicense(
  125. { db, block, id: generateEntityIdFromIndex(entityId) },
  126. decode.setEntityPropertyValues<IUserDefinedLicense>(entity.properties, userDefinedLicensePropertyNamesWithId)
  127. )
  128. removeInsertedEntity('UserDefinedLicense', entityId, classEntityMap)
  129. return record
  130. }
  131. async function channel({ db, block }: IDBBlockId, classEntityMap: ClassEntityMap, entityId: number): Promise<Channel> {
  132. const entity = findEntity(entityId, 'Channel', classEntityMap)
  133. const record = await createChannel(
  134. { db, block, id: generateEntityIdFromIndex(entityId) },
  135. classEntityMap,
  136. decode.setEntityPropertyValues<IChannel>(entity.properties, channelPropertyNamesWithId)
  137. )
  138. removeInsertedEntity('Channel', entityId, classEntityMap)
  139. return record
  140. }
  141. async function category(
  142. { db, block }: IDBBlockId,
  143. classEntityMap: ClassEntityMap,
  144. entityId: number
  145. ): Promise<Category> {
  146. const entity = findEntity(entityId, 'Category', classEntityMap)
  147. const record = await createCategory(
  148. { db, block, id: generateEntityIdFromIndex(entityId) },
  149. decode.setEntityPropertyValues<ICategory>(entity.properties, CategoryPropertyNamesWithId)
  150. )
  151. removeInsertedEntity('Category', entityId, classEntityMap)
  152. return record
  153. }
  154. async function httpMediaLocation(
  155. { db, block }: IDBBlockId,
  156. classEntityMap: ClassEntityMap,
  157. entityId: number
  158. ): Promise<HttpMediaLocation | undefined> {
  159. const entity = findEntity(entityId, 'HttpMediaLocation', classEntityMap)
  160. const record = await createHttpMediaLocation(
  161. { db, block, id: generateEntityIdFromIndex(entityId) },
  162. decode.setEntityPropertyValues<IHttpMediaLocation>(entity.properties, httpMediaLocationPropertyNamesWithId)
  163. )
  164. removeInsertedEntity('HttpMediaLocation', entityId, classEntityMap)
  165. return record
  166. }
  167. async function joystreamMediaLocation(
  168. { db, block }: IDBBlockId,
  169. classEntityMap: ClassEntityMap,
  170. entityId: number
  171. ): Promise<JoystreamMediaLocation | undefined> {
  172. const entity = findEntity(entityId, 'JoystreamMediaLocation', classEntityMap)
  173. const record = await createJoystreamMediaLocation(
  174. { db, block, id: generateEntityIdFromIndex(entityId) },
  175. decode.setEntityPropertyValues<IJoystreamMediaLocation>(
  176. entity.properties,
  177. joystreamMediaLocationPropertyNamesWithId
  178. )
  179. )
  180. removeInsertedEntity('JoystreamMediaLocation', entityId, classEntityMap)
  181. return record
  182. }
  183. async function license({ db, block }: IDBBlockId, classEntityMap: ClassEntityMap, entityId: number): Promise<License> {
  184. const entity = findEntity(entityId, 'License', classEntityMap)
  185. const record = await createLicense(
  186. { db, block, id: generateEntityIdFromIndex(entityId) },
  187. classEntityMap,
  188. decode.setEntityPropertyValues<ILicense>(entity.properties, licensePropertyNamesWithId)
  189. )
  190. removeInsertedEntity('License', entityId, classEntityMap)
  191. return record
  192. }
  193. async function mediaLocation(
  194. { db, block }: IDBBlockId,
  195. classEntityMap: ClassEntityMap,
  196. entityId: number
  197. ): Promise<MediaLocation> {
  198. const entity = findEntity(entityId, 'MediaLocation', classEntityMap)
  199. const record = await createMediaLocation(
  200. { db, block, id: generateEntityIdFromIndex(entityId) },
  201. classEntityMap,
  202. decode.setEntityPropertyValues<IMediaLocation>(entity.properties, mediaLocationPropertyNamesWithId)
  203. )
  204. removeInsertedEntity('MediaLocation', entityId, classEntityMap)
  205. return record
  206. }
  207. function removeInsertedEntity(key: string, insertedEntityId: number, classEntityMap: ClassEntityMap) {
  208. const newlyCreatedEntities = classEntityMap.get(key)
  209. // Remove the inserted entity from the list
  210. classEntityMap.set(
  211. key,
  212. newlyCreatedEntities!.filter((e) => e.entityId !== insertedEntityId)
  213. )
  214. }
  215. export const getOrCreate = {
  216. language,
  217. videoMediaEncoding,
  218. videoMedia,
  219. knownLicense,
  220. userDefinedLicense,
  221. channel,
  222. category,
  223. joystreamMediaLocation,
  224. httpMediaLocation,
  225. license,
  226. mediaLocation,
  227. }