export-data-directory.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. let transformed = await Promise.all(ids.map(async (id) => {
  18. let obj = await api.query.dataDirectory.dataObjectByContentId(id)
  19. if (obj.isNone) { return null }
  20. obj = obj.unwrap()
  21. return [id, {
  22. owner: runtimeSpecVersion <= 15 ? await ownerAccountToMemberId(obj.owner) : obj.owner,
  23. added_at: obj.added_at,
  24. type_id: obj.type_id,
  25. size: obj.size_in_bytes,
  26. liaison: runtimeSpecVersion <= 15 ? new types.u64(0) : obj.liaison,
  27. liaison_judgement: obj.liaison_judgement,
  28. ipfs_content_id: obj.ipfs_content_id }]
  29. }))
  30. console.log(JSON.stringify(transformed))
  31. console.error(`Exported ${transformed.length} objects`)
  32. }
  33. if (typeof module === 'undefined') {
  34. // Pioneer js-toolbox
  35. script({ api, hashing, keyring, types, util })
  36. } else {
  37. // Node
  38. module.exports = script
  39. }