Browse Source

query node - typings improvements

ondratra 3 years ago
parent
commit
fdc18a1775

+ 3 - 3
query-node/generated/graphql-server/.env

@@ -4,11 +4,11 @@ NODE_ENV=development
 WARTHOG_AUTO_OPEN_PLAYGROUND=false
 WARTHOG_AUTO_GENERATE_FILES=false
 WARTHOG_APP_HOST=localhost
-WARTHOG_APP_PORT=4002
-WARTHOG_DB_DATABASE=query_node_processor
+WARTHOG_APP_PORT=4100
+WARTHOG_DB_DATABASE=query-node
 WARTHOG_DB_HOST=localhost
 WARTHOG_DB_LOGGING=all
-WARTHOG_DB_PASSWORD=postgres
+WARTHOG_DB_PASSWORD=
 WARTHOG_DB_PORT=5432
 WARTHOG_DB_SYNCHRONIZE=true
 WARTHOG_DB_USERNAME=postgres

+ 7 - 7
query-node/mappings/src/content/channel.ts

@@ -42,11 +42,11 @@ export async function content_ChannelCreated(db: DatabaseManager, event: Substra
 
   // create entity
   const channel = new Channel({
-    id: channelId,
+    id: channelId.toString(),
     isCensored: false,
     videos: [],
-    happenedIn: event.blockNumber,
-    ...Object(protobufContent)
+    createdInBlock: event.blockNumber,
+    ...protobufContent
   })
 
   // save entity
@@ -193,7 +193,7 @@ export async function content_ChannelCategoryCreated(
   event: SubstrateEvent
 ) {
   // read event data
-  const {channelCategoryCreationParameters} = new Content.ChannelCategoryCreatedEvent(event).data
+  const {channelCategoryCreationParameters, channelCategoryId} = new Content.ChannelCategoryCreatedEvent(event).data
   const {actor: contentActor} = new Content.CreateChannelCategoryCall(event).args
 
   // read metadata
@@ -208,10 +208,10 @@ export async function content_ChannelCategoryCreated(
 
   // create new channel category
   const channelCategory = new ChannelCategory({
-    id: event.params[0].value.toString(), // ChannelCategoryId
+    id: channelCategoryId.toString(),
     channels: [],
-    happenedIn: event.blockNumber,
-    ...Object(protobufContent)
+    createdInBlock: event.blockNumber,
+    ...protobufContent
   })
 
   // save channel

+ 18 - 10
query-node/mappings/src/content/utils.ts

@@ -92,18 +92,18 @@ export interface IReadProtobufArgumentsWithAssets extends IReadProtobufArguments
 /*
   Reads information from the event and protobuf metadata and constructs changeset that's fit to be used when saving to db.
 */
-export async function readProtobuf(
-  type: ChannelCategory | VideoCategory,
+export async function readProtobuf<T extends ChannelCategory | VideoCategory>(
+  type: T,
   parameters: IReadProtobufArguments,
-): Promise<Partial<typeof type>> {
+): Promise<Partial<T>> {
   // process channel category
   if (type instanceof ChannelCategory) {
-    return ChannelCategoryMetadata.deserializeBinary(parameters.metadata).toObject()
+    return ChannelCategoryMetadata.deserializeBinary(parameters.metadata).toObject() as Partial<T>
   }
 
   // process video category
   if (type instanceof VideoCategory) {
-    return VideoCategoryMetadata.deserializeBinary(parameters.metadata).toObject()
+    return VideoCategoryMetadata.deserializeBinary(parameters.metadata).toObject() as Partial<T>
   }
 
   // this should never happen
@@ -114,15 +114,23 @@ export async function readProtobuf(
   Reads information from the event and protobuf metadata and constructs changeset that's fit to be used when saving to db.
   In addition it handles any assets associated with the metadata.
 */
+
+/*
 export async function readProtobufWithAssets(
   type: Channel | Video,
   parameters: IReadProtobufArgumentsWithAssets,
 ): Promise<Partial<typeof type>> {
+*/
+export async function readProtobufWithAssets<T extends Channel | Video>(
+  type: T,
+  parameters: IReadProtobufArgumentsWithAssets,
+): Promise<Partial<T>> {
+
   // process channel
   if (type instanceof Channel) {
     const meta = ChannelMetadata.deserializeBinary(parameters.metadata)
     const metaAsObject = meta.toObject()
-    const result = metaAsObject as any as Channel
+    const result = metaAsObject as any as Partial<Channel>
 
     // prepare cover photo asset if needed
     if (metaAsObject.coverPhoto !== undefined) {
@@ -155,14 +163,14 @@ export async function readProtobufWithAssets(
       result.language = await prepareLanguage(metaAsObject.language, parameters.db)
     }
 
-    return result
+    return result as Partial<T>
   }
 
   // process video
   if (type instanceof Video) {
     const meta = VideoMetadata.deserializeBinary(parameters.metadata)
     const metaAsObject = meta.toObject()
-    const result = metaAsObject as any as Video
+    const result = metaAsObject as any as Partial<Video>
 
     // prepare video category if needed
     if (metaAsObject.category !== undefined) {
@@ -220,7 +228,7 @@ export async function readProtobufWithAssets(
       handlePublishedBeforeJoystream(result, metaAsObject.publishedBeforeJoystream.date)
     }
 
-    return result
+    return result as Partial<T>
   }
 
   // this should never happen
@@ -252,7 +260,7 @@ export function convertContentActorToOwner(contentActor: ContentActor, channelId
   */
 }
 
-function handlePublishedBeforeJoystream(video: Video, publishedAtString?: string) {
+function handlePublishedBeforeJoystream(video: Partial<Video>, publishedAtString?: string) {
   // published elsewhere before Joystream
   if (publishedAtString) {
     video.publishedBeforeJoystream = new Date(publishedAtString)

+ 14 - 6
query-node/mappings/src/content/video.ts

@@ -18,6 +18,7 @@ import {
 } from './utils'
 
 // primary entities
+import { Channel } from 'query-node/src/modules/channel/channel.model'
 import { Video } from 'query-node/src/modules/video/video.model'
 import { VideoCategory } from 'query-node/src/modules/video-category/video-category.model'
 
@@ -42,7 +43,7 @@ export async function content_VideoCategoryCreated(
   } = new Content.VideoCategoryCreatedEvent(event).data
 
   // read metadata
-  const protobufContent = readProtobuf(
+  const protobufContent = await readProtobuf(
     new VideoCategory(),
     {
       metadata: videoCategoryCreationParameters.meta,
@@ -54,10 +55,9 @@ export async function content_VideoCategoryCreated(
   // create new video category
   const videoCategory = new VideoCategory({
     id: videoCategoryId.toString(), // ChannelId
-    isCensored: false,
     videos: [],
     createdInBlock: event.blockNumber,
-    ...Object(protobufContent)
+    ...protobufContent
   })
 
   // save video category
@@ -159,13 +159,21 @@ export async function content_VideoCreated(
     }
   )
 
+  // load channel
+  const channel = await db.get(Channel, { where: { id: channelId } })
+
+  // ensure channel exists
+  if (!channel) {
+    return inconsistentState('Trying to add video to non-existing channel', channelId)
+  }
+
   // create new video
   const video = new Video({
-    id: videoId,
+    id: videoId.toString(),
     isCensored: false,
-    channel: channelId,
+    channel,
     createdInBlock: event.blockNumber,
-    ...Object(protobufContent)
+    ...protobufContent
   })
 
   // save video