initializeDefaultSchemas.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* eslint-disable import/first */
  2. import 'reflect-metadata'
  3. import { loadConfig } from '../generated/graphql-server/src/config'
  4. loadConfig()
  5. import BN from 'bn.js'
  6. import { nanoid } from 'nanoid'
  7. import { SnakeNamingStrategy } from '@dzlzv/hydra-db-utils'
  8. import { createConnection, ConnectionOptions, getConnection, EntityManager } from 'typeorm'
  9. import { Video } from '../generated/graphql-server/src/modules/video/video.model'
  10. import { Channel } from '../generated/graphql-server/src/modules/channel/channel.model'
  11. import { Block, Network } from '../generated/graphql-server/src/modules/block/block.model'
  12. import { Category } from '../generated/graphql-server/src/modules/category/category.model'
  13. import { VideoMedia } from '../generated/graphql-server/src/modules/video-media/video-media.model'
  14. import { LicenseEntity } from '../generated/graphql-server/src/modules/license-entity/license-entity.model'
  15. import { JoystreamMediaLocation, KnownLicense } from '../generated/graphql-server/src/modules/variants/variants.model'
  16. import { KnownLicenseEntity } from '../generated/graphql-server/src/modules/known-license-entity/known-license-entity.model'
  17. import { VideoMediaEncoding } from '../generated/graphql-server/src/modules/video-media-encoding/video-media-encoding.model'
  18. import { MediaLocationEntity } from '../generated/graphql-server/src/modules/media-location-entity/media-location-entity.model'
  19. import { HttpMediaLocationEntity } from '../generated/graphql-server/src/modules/http-media-location-entity/http-media-location-entity.model'
  20. import { JoystreamMediaLocationEntity } from '../generated/graphql-server/src/modules/joystream-media-location-entity/joystream-media-location-entity.model'
  21. function getConnectionOptions() {
  22. const connectionConfig: ConnectionOptions = {
  23. type: 'postgres',
  24. host: process.env.WARTHOG_DB_HOST,
  25. port: parseInt(process.env.WARTHOG_DB_PORT!),
  26. username: process.env.WARTHOG_DB_USERNAME,
  27. password: process.env.WARTHOG_DB_PASSWORD,
  28. database: process.env.WARTHOG_DB_DATABASE,
  29. entities: [process.env.WARTHOG_DB_ENTITIES!],
  30. namingStrategy: new SnakeNamingStrategy(),
  31. logging: true,
  32. }
  33. return connectionConfig
  34. }
  35. export async function main(): Promise<void> {
  36. console.log(`Initializing...`)
  37. await createConnection(getConnectionOptions())
  38. await getConnection().transaction(async (db: EntityManager) => {
  39. const id = '0'
  40. const createdAt = new Date()
  41. const createdById = '0'
  42. const version = 0
  43. // ///////// Block /////////////////
  44. const happenedIn = new Block({
  45. createdAt,
  46. createdById,
  47. version,
  48. block: 0,
  49. timestamp: new BN(Date.now()),
  50. network: Network.BABYLON,
  51. })
  52. await db.save<Block>(happenedIn)
  53. // ///////// Block /////////////////
  54. const commonProperties = { id, happenedIn, createdAt, createdById, version }
  55. // ///////// HttpMediaLocationEntity /////////////////
  56. const httpMediaLocation = new HttpMediaLocationEntity({
  57. ...commonProperties,
  58. url: '5FyzfM2YtZa75hHYCAo5evNS8bH8P4Kw8EyXqKkC5upVSDBQ',
  59. })
  60. await db.save<HttpMediaLocationEntity>(httpMediaLocation)
  61. // ///////// HttpMediaLocationEntity /////////////////
  62. // ///////// JoystreamMediaLocationEntity /////////////////
  63. const joyMediaLocation = new JoystreamMediaLocationEntity({
  64. ...commonProperties,
  65. dataObjectId: '5FyzfM2YtZa75hHYCAo5evNS8bH8P4Kw8EyXqKkC5upVSDBQ',
  66. })
  67. await db.save<JoystreamMediaLocationEntity>(joyMediaLocation)
  68. // ///////// JoystreamMediaLocationEntity /////////////////
  69. // ///////// KnownLicenseEntity /////////////////
  70. const knownLicense = new KnownLicenseEntity({ ...commonProperties, code: 'NA' })
  71. await db.save<KnownLicenseEntity>(knownLicense)
  72. // ///////// KnownLicenseEntity /////////////////
  73. // ///////// License /////////////////
  74. const k = new KnownLicense()
  75. k.code = knownLicense.code
  76. const license = new LicenseEntity({ ...commonProperties, type: k })
  77. await db.save<LicenseEntity>(license)
  78. // ///////// License /////////////////
  79. // ///////// MediaLocationEntity /////////////////
  80. const mediaLocEntity = new MediaLocationEntity({ ...commonProperties, joystreamMediaLocation: joyMediaLocation })
  81. await db.save<MediaLocationEntity>(mediaLocEntity)
  82. // ///////// MediaLocationEntity /////////////////
  83. // ///////// Channel /////////////////
  84. const channel = new Channel({
  85. ...commonProperties,
  86. handle: `Channel(0) - ${nanoid()}`,
  87. description: `Channel 0`,
  88. isPublic: false,
  89. isCurated: false,
  90. })
  91. await db.save<Channel>(channel)
  92. // ///////// Channel /////////////////
  93. // ///////// Category /////////////////
  94. const category = new Category({ ...commonProperties, name: `Other` })
  95. await db.save<Category>(category)
  96. // ///////// Category /////////////////
  97. // ///////// VideoMediaEncoding /////////////////
  98. const videoMediaEncod = new VideoMediaEncoding({ ...commonProperties, name: 'NA' })
  99. await db.save<VideoMediaEncoding>(videoMediaEncod)
  100. // ///////// VideoMediaEncoding /////////////////
  101. // ///////// VideoMedia /////////////////
  102. const location = new JoystreamMediaLocation()
  103. location.dataObjectId = joyMediaLocation.dataObjectId
  104. const videoMedia = new VideoMedia({
  105. ...commonProperties,
  106. location,
  107. locationEntity: mediaLocEntity,
  108. encoding: videoMediaEncod,
  109. pixelHeight: 0,
  110. pixelWidth: 0,
  111. })
  112. await db.save<VideoMedia>(videoMedia)
  113. // ///////// VideoMedia /////////////////
  114. // ///////// Video /////////////////
  115. const v = new Video({ ...commonProperties })
  116. v.category = category
  117. v.channel = channel
  118. v.media = videoMedia
  119. v.license = license
  120. v.title = `Video(0)`
  121. v.description = `Video(0)`
  122. v.duration = 0
  123. v.thumbnailUrl = 'https://eu-central-1.linodeobjects.com/joystream/1.png'
  124. v.isPublic = false
  125. v.isCurated = false
  126. v.isExplicit = false
  127. v.isFeatured = false
  128. await db.save<Video>(v)
  129. // ///////// Video /////////////////
  130. })
  131. }
  132. main()
  133. .then(() => {
  134. console.log(`Done.`)
  135. process.exit()
  136. })
  137. .catch(console.log)