initializeDefaultSchemas.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { DB, SubstrateEvent } from '../../generated/indexer'
  2. import { Channel } from '../../generated/graphql-server/src/modules/channel/channel.model'
  3. import { Video } from '../../generated/graphql-server/src/modules/video/video.model'
  4. import { Category } from '../../generated/graphql-server/src/modules/category/category.model'
  5. import { VideoMedia } from '../../generated/graphql-server/src/modules/video-media/video-media.model'
  6. import { LicenseEntity } from '../../generated/graphql-server/src/modules/license-entity/license-entity.model'
  7. import { VideoMediaEncoding } from '../../generated/graphql-server/src/modules/video-media-encoding/video-media-encoding.model'
  8. import { MediaLocationEntity } from '../../generated/graphql-server/src/modules/media-location-entity/media-location-entity.model'
  9. import { HttpMediaLocationEntity } from '../../generated/graphql-server/src/modules/http-media-location-entity/http-media-location-entity.model'
  10. import { JoystreamMediaLocationEntity } from '../../generated/graphql-server/src/modules/joystream-media-location-entity/joystream-media-location-entity.model'
  11. import { KnownLicenseEntity } from '../../generated/graphql-server/src/modules/known-license-entity/known-license-entity.model'
  12. import { Block, Network } from '../../generated/graphql-server/src/modules/block/block.model'
  13. import BN from 'bn.js'
  14. import {
  15. JoystreamMediaLocation,
  16. KnownLicense,
  17. } from '../../generated/graphql-server/src/modules/variants/variants.model'
  18. import { nanoid } from 'nanoid'
  19. let done = false
  20. // eslint-disable-next-line @typescript-eslint/naming-convention
  21. export async function system_ExtrinsicSuccess(db: DB, event: SubstrateEvent): Promise<void> {
  22. if (done) {
  23. console.log(`Database initialization is done.`)
  24. process.exit()
  25. }
  26. const id = '0'
  27. // ///////// Block /////////////////
  28. const happenedIn = new Block()
  29. happenedIn.block = 0
  30. happenedIn.timestamp = new BN(Date.now())
  31. happenedIn.network = Network.BABYLON
  32. await db.save<Block>(happenedIn)
  33. // ///////// Block /////////////////
  34. const commonProperties = { id, happenedIn }
  35. // ///////// HttpMediaLocationEntity /////////////////
  36. const httpMediaLocation = new HttpMediaLocationEntity({
  37. ...commonProperties,
  38. url: '5FyzfM2YtZa75hHYCAo5evNS8bH8P4Kw8EyXqKkC5upVSDBQ',
  39. })
  40. await db.save<HttpMediaLocationEntity>(httpMediaLocation)
  41. // ///////// HttpMediaLocationEntity /////////////////
  42. // ///////// JoystreamMediaLocationEntity /////////////////
  43. const joyMediaLocation = new JoystreamMediaLocationEntity({
  44. ...commonProperties,
  45. dataObjectId: '5FyzfM2YtZa75hHYCAo5evNS8bH8P4Kw8EyXqKkC5upVSDBQ',
  46. })
  47. await db.save<JoystreamMediaLocationEntity>(joyMediaLocation)
  48. // ///////// JoystreamMediaLocationEntity /////////////////
  49. // ///////// KnownLicenseEntity /////////////////
  50. const knownLicense = new KnownLicenseEntity({ ...commonProperties, code: 'NA' })
  51. await db.save<KnownLicenseEntity>(knownLicense)
  52. // ///////// KnownLicenseEntity /////////////////
  53. // ///////// License /////////////////
  54. const k = new KnownLicense()
  55. k.code = knownLicense.code
  56. const license = new LicenseEntity({ ...commonProperties, type: k })
  57. await db.save<LicenseEntity>(license)
  58. // ///////// License /////////////////
  59. // ///////// MediaLocationEntity /////////////////
  60. const mediaLocEntity = new MediaLocationEntity({ ...commonProperties, joystreamMediaLocation: joyMediaLocation })
  61. await db.save<MediaLocationEntity>(mediaLocEntity)
  62. // ///////// MediaLocationEntity /////////////////
  63. // ///////// Channel /////////////////
  64. const channel = new Channel({
  65. ...commonProperties,
  66. handle: `Channel(0) - ${nanoid()}`,
  67. description: `Channel 0`,
  68. isPublic: false,
  69. isCurated: false,
  70. })
  71. await db.save<Channel>(channel)
  72. // ///////// Channel /////////////////
  73. // ///////// Category /////////////////
  74. const category = new Category({ ...commonProperties, name: `Category(0) ${nanoid()}` })
  75. await db.save<Category>(category)
  76. // ///////// Category /////////////////
  77. // ///////// VideoMediaEncoding /////////////////
  78. const videoMediaEncod = new VideoMediaEncoding({ ...commonProperties, name: 'NA' })
  79. await db.save<VideoMediaEncoding>(videoMediaEncod)
  80. // ///////// VideoMediaEncoding /////////////////
  81. // ///////// VideoMedia /////////////////
  82. const location = new JoystreamMediaLocation()
  83. location.dataObjectId = joyMediaLocation.dataObjectId
  84. const videoMedia = new VideoMedia({
  85. ...commonProperties,
  86. location,
  87. locationEntity: mediaLocEntity,
  88. encoding: videoMediaEncod,
  89. pixelHeight: 0,
  90. pixelWidth: 0,
  91. })
  92. await db.save<VideoMedia>(videoMedia)
  93. // ///////// VideoMedia /////////////////
  94. // ///////// Video /////////////////
  95. const v = new Video({ ...commonProperties })
  96. v.category = category
  97. v.channel = channel
  98. v.media = videoMedia
  99. v.license = license
  100. v.title = `Video(0)`
  101. v.description = `Video(0)`
  102. v.duration = 0
  103. v.thumbnailUrl = 'https://eu-central-1.linodeobjects.com/joystream/1.png'
  104. v.isPublic = false
  105. v.isCurated = false
  106. v.isExplicit = true
  107. v.isFeatured = false
  108. await db.save<Video>(v)
  109. // ///////// Video /////////////////
  110. done = true
  111. }