Преглед изворни кода

Use .keys() to retrieve ids in media transport

Leszek Wiesner пре 4 година
родитељ
комит
7a3afd129b

+ 4 - 47
pioneer/packages/joy-media/src/transport.substrate.ts

@@ -24,10 +24,7 @@ import { FeaturedContentType } from './schemas/general/FeaturedContent';
 import { AnyChannelId, AnyClassId, AnyEntityId, asChannelId } from './common/TypeHelpers';
 import { SimpleCache } from '@polkadot/joy-utils/transport/SimpleCache';
 import { ValidationConstraint } from '@polkadot/joy-utils/types/ValidationConstraint';
-
-const FIRST_CHANNEL_ID = 1;
-const FIRST_CLASS_ID = 1;
-const FIRST_ENTITY_ID = 1;
+import { ids } from '@polkadot/joy-utils/transport/base';
 
 /**
  * There are entities that refer to other entities.
@@ -118,22 +115,8 @@ export class SubstrateTransport extends MediaTransport {
   // Channels (Content Working Group module)
   // -----------------------------------------------------------------
 
-  async nextChannelId (): Promise<ChannelId> {
-    return await this.cwgQuery().nextChannelId<ChannelId>();
-  }
-
   async allChannelIds (): Promise<ChannelId[]> {
-    let nextId = (await this.nextChannelId()).toNumber();
-
-    if (nextId < 1) nextId = 1;
-
-    const allIds: ChannelId[] = [];
-
-    for (let id = FIRST_CHANNEL_ID; id < nextId; id++) {
-      allIds.push(this.api.createType('ChannelId', id));
-    }
-
-    return allIds;
+    return ids<ChannelId>(this.cwgQuery().channelById);
   }
 
   async loadChannelsByIds (ids: AnyChannelId[]): Promise<ChannelEntity[]> {
@@ -192,21 +175,8 @@ export class SubstrateTransport extends MediaTransport {
 
   // Classes (Versioned Store module)
   // -----------------------------------------------------------------
-
-  async nextClassId (): Promise<ClassId> {
-    return await this.vsQuery().nextClassId<ClassId>();
-  }
-
   async allClassIds (): Promise<ClassId[]> {
-    const nextId = (await this.nextClassId()).toNumber();
-
-    const allIds: ClassId[] = [];
-
-    for (let id = FIRST_CLASS_ID; id < nextId; id++) {
-      allIds.push(this.api.createType('ClassId', id));
-    }
-
-    return allIds;
+    return ids<ClassId>(this.vsQuery().classById);
   }
 
   async loadClassesByIds (ids: AnyClassId[]): Promise<Class[]> {
@@ -244,21 +214,8 @@ export class SubstrateTransport extends MediaTransport {
 
   // Entities (Versioned Store module)
   // -----------------------------------------------------------------
-
-  async nextEntityId (): Promise<EntityId> {
-    return await this.vsQuery().nextEntityId<EntityId>();
-  }
-
   async allEntityIds (): Promise<EntityId[]> {
-    const nextId = (await this.nextEntityId()).toNumber();
-
-    const allIds: EntityId[] = [];
-
-    for (let id = FIRST_ENTITY_ID; id < nextId; id++) {
-      allIds.push(this.api.createType('EntityId', id));
-    }
-
-    return allIds;
+    return ids<EntityId>(this.vsQuery().entityById);
   }
 
   private async loadEntitiesByIds (ids: AnyEntityId[]): Promise<Entity[]> {

+ 9 - 0
pioneer/packages/joy-utils/src/transport/base.ts

@@ -18,6 +18,14 @@ export async function entriesByIds<IDType extends UInt, ValueType extends Codec>
   return entries.sort((a, b) => a[0].toNumber() - b[0].toNumber());
 }
 
+export async function ids<IDType extends UInt> (
+  apiMethod: QueryableStorageEntry<'promise'>
+): Promise<IDType[]> {
+  const storageKeys = await apiMethod.keys();
+
+  return storageKeys.map((key) => key.args[0] as IDType).sort((a, b) => a.toNumber() - b.toNumber());
+}
+
 export default class BaseTransport {
   protected api: ApiPromise;
   protected cacheApi: APIQueryCache;
@@ -82,4 +90,5 @@ export default class BaseTransport {
   }
 
   protected entriesByIds = entriesByIds
+  protected ids = ids
 }