bootstrap.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { StoreContext } from '@joystream/hydra-common'
  2. import BN from 'bn.js'
  3. import {
  4. Membership,
  5. MembershipEntryMethod,
  6. StorageSystemParameters,
  7. Worker,
  8. WorkerType,
  9. ChannelCategory,
  10. VideoCategory,
  11. } from 'query-node/dist/model'
  12. import { workerEntityId } from './workingGroup'
  13. import {
  14. storageSystemData,
  15. membersData,
  16. workingGroupsData,
  17. videoCategoriesData,
  18. channelCategoriesData,
  19. } from './bootstrap-data'
  20. export async function bootstrapData({ store }: StoreContext): Promise<void> {
  21. // Storage system
  22. await store.save<StorageSystemParameters>(
  23. new StorageSystemParameters({
  24. ...storageSystemData,
  25. storageBucketMaxObjectsCountLimit: new BN(storageSystemData.storageBucketMaxObjectsCountLimit),
  26. storageBucketMaxObjectsSizeLimit: new BN(storageSystemData.storageBucketMaxObjectsSizeLimit),
  27. dataObjectFeePerMb: new BN(storageSystemData.dataObjectFeePerMb),
  28. })
  29. )
  30. // Members
  31. const members = membersData.map(
  32. (m) =>
  33. new Membership({
  34. // main data
  35. id: m.memberId,
  36. rootAccount: m.rootAccount,
  37. controllerAccount: m.controllerAccount,
  38. handle: m.handle,
  39. about: m.about,
  40. avatarUri: m.avatarUri,
  41. createdInBlock: m.registeredAtBlock,
  42. entry: m.registeredAtBlock === 1 ? MembershipEntryMethod.GENESIS : MembershipEntryMethod.PAID,
  43. // fill in auto-generated fields
  44. createdAt: new Date(m.registeredAtTime),
  45. updatedAt: new Date(m.registeredAtTime),
  46. })
  47. )
  48. await Promise.all(members.map((m) => store.save<Membership>(m)))
  49. // Workers
  50. let workers: Worker[] = []
  51. ;(['GATEWAY', 'STORAGE'] as const).map((group) => {
  52. const workersJson = workingGroupsData[group]?.workers || []
  53. workers = workers.concat(
  54. workersJson.map(
  55. (w) =>
  56. new Worker({
  57. id: workerEntityId(WorkerType[group], w.workerId),
  58. workerId: w.workerId,
  59. isActive: true,
  60. type: WorkerType[group],
  61. createdAt: new Date(w.createdAt),
  62. updatedAt: new Date(w.createdAt),
  63. metadata: w.metadata,
  64. })
  65. )
  66. )
  67. })
  68. await Promise.all(workers.map((w) => store.save<Worker>(w)))
  69. const channelCategories = channelCategoriesData.map(
  70. (m) =>
  71. new ChannelCategory({
  72. id: m.id,
  73. name: m.name,
  74. channels: [],
  75. createdInBlock: m.createdInBlock,
  76. createdAt: new Date(m.createdAt),
  77. updatedAt: new Date(m.updatedAt),
  78. })
  79. )
  80. await Promise.all(channelCategories.map((m) => store.save<ChannelCategory>(m)))
  81. const videoCategories = videoCategoriesData.map(
  82. (m) =>
  83. new VideoCategory({
  84. id: m.id,
  85. name: m.name,
  86. videos: [],
  87. createdInBlock: m.createdInBlock,
  88. createdAt: new Date(m.createdAt),
  89. updatedAt: new Date(m.updatedAt),
  90. })
  91. )
  92. await Promise.all(videoCategories.map((m) => store.save<VideoCategory>(m)))
  93. }