Browse Source

Merge pull request #1559 from Lezek123/cd-init-helper

Content directory initialization helper
Mokhtar Naamani 4 years ago
parent
commit
4b106c2b15

+ 11 - 1
content-directory-schemas/src/helpers/InputParser.ts

@@ -17,6 +17,7 @@ import { JoyBTreeSet } from '@joystream/types/common'
 import { CreateClass } from '../../types/extrinsics/CreateClass'
 import { EntityBatch } from '../../types/EntityBatch'
 import { getInputs } from './inputs'
+import { SubmittableExtrinsic } from '@polkadot/api/types'
 
 export class InputParser {
   private api: ApiPromise
@@ -33,11 +34,20 @@ export class InputParser {
   private classMapInitialized = false
   private entityIdByUniqueQueryMapInitialized = false
 
+  static createWithInitialInputs(api: ApiPromise): InputParser {
+    return new InputParser(
+      api,
+      getInputs<CreateClass>('classes').map(({ data }) => data),
+      getInputs<AddClassSchema>('schemas').map(({ data }) => data),
+      getInputs<EntityBatch>('entityBatches').map(({ data }) => data)
+    )
+  }
+
   static createWithKnownSchemas(api: ApiPromise, entityBatches?: EntityBatch[]): InputParser {
     return new InputParser(
       api,
       [],
-      getInputs('schemas').map(({ data }) => data),
+      getInputs<AddClassSchema>('schemas').map(({ data }) => data),
       entityBatches
     )
   }

+ 19 - 0
content-directory-schemas/src/helpers/initialize.ts

@@ -0,0 +1,19 @@
+import { ApiPromise } from '@polkadot/api'
+import { InputParser } from './InputParser'
+import { ExtrinsicsHelper } from './extrinsics'
+import { KeyringPair } from '@polkadot/keyring/types'
+
+export default async function initializeContentDir(api: ApiPromise, leadKey: KeyringPair): Promise<void> {
+  const txHelper = new ExtrinsicsHelper(api)
+  const parser = InputParser.createWithInitialInputs(api)
+
+  // Initialize classes first in order to later be able to get classIdByNameMap
+  const createClassTxs = await parser.getCreateClassExntrinsics()
+  await txHelper.sendAndCheck(leadKey, createClassTxs, 'Classes initialization failed!')
+
+  // Initialize schemas and entities
+  const addSchemaTxs = await parser.getAddSchemaExtrinsics()
+  const entitiesTx = api.tx.contentDirectory.transaction({ Lead: null }, await parser.getEntityBatchOperations())
+  await txHelper.sendAndCheck(leadKey, addSchemaTxs, 'Schemas initialization failed!')
+  await txHelper.sendAndCheck(leadKey, [entitiesTx], 'Entities initialization failed!')
+}

+ 1 - 0
content-directory-schemas/src/index.ts

@@ -3,3 +3,4 @@ export { InputParser } from './helpers/InputParser'
 export { getInputs, getInputsLocation } from './helpers/inputs'
 export { isReference, isSingle } from './helpers/propertyType'
 export { getSchemasLocation } from './helpers/schemas'
+export { default as initializeContentDir } from './helpers/initialize'