export-data-directory.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* global api, hashing, keyring, types, util */
  2. // run this script with:
  3. // yarn script exportDataDirectory
  4. //
  5. // or copy and paste the code into the pioneer javascript toolbox at:
  6. // https://testnet.joystream.org/#/js
  7. const script = async ({ api, hashing, keyring, types, util }) => {
  8. const runtimeSpecVersion = api.runtimeVersion.specVersion
  9. const ownerAccountToMemberId = async (accountId) => {
  10. const memberIds = await api.query.members.memberIdsByRootAccountId(accountId)
  11. return memberIds[0] || null
  12. }
  13. const ids = await api.query.dataDirectory.knownContentIds()
  14. // When a BTreeMap is constructed for injection the node will fail to decode
  15. // it if its not sorted.
  16. ids.sort()
  17. const transformed = await Promise.all(
  18. ids.map(async (id) => {
  19. let obj = await api.query.dataDirectory.dataObjectByContentId(id)
  20. if (obj.isNone) {
  21. return null
  22. }
  23. obj = obj.unwrap()
  24. return [
  25. id,
  26. {
  27. owner: runtimeSpecVersion <= 15 ? await ownerAccountToMemberId(obj.owner) : obj.owner,
  28. added_at: obj.added_at,
  29. type_id: obj.type_id,
  30. size: obj.size_in_bytes,
  31. liaison: runtimeSpecVersion <= 15 ? api.createType('u64', 0) : obj.liaison,
  32. liaison_judgement: obj.liaison_judgement,
  33. ipfs_content_id: obj.ipfs_content_id,
  34. },
  35. ]
  36. })
  37. )
  38. console.log(JSON.stringify(transformed))
  39. console.error(`Exported ${transformed.length} objects`)
  40. }
  41. if (typeof module === 'undefined') {
  42. // Pioneer js-toolbox
  43. script({ api, hashing, keyring, types, util })
  44. } else {
  45. // Node
  46. module.exports = script
  47. }