Browse Source

query node - mappings type safety improvements

ondratra 3 years ago
parent
commit
294b15a163

+ 12 - 5
query-node/mappings/src/content/channel.ts

@@ -1,6 +1,7 @@
 import { SubstrateEvent } from '@dzlzv/hydra-common'
 import { DatabaseManager } from '@dzlzv/hydra-db-utils'
 import ISO6391 from 'iso-639-1';
+import { FindConditions, In } from 'typeorm'
 
 import { AccountId } from "@polkadot/types/interfaces";
 import { Option } from '@polkadot/types/codec';
@@ -76,7 +77,7 @@ export async function content_ChannelUpdated(
   } = new Content.ChannelUpdatedEvent(event).data
 
   // load channel
-  const channel = await db.get(Channel, { where: { id: channelId } })
+  const channel = await db.get(Channel, { where: { id: channelId.toString() } as FindConditions<Channel> })
 
   // ensure channel exists
   if (!channel) {
@@ -132,7 +133,9 @@ export async function content_ChannelAssetsRemoved(
   const {contentId: contentIds} = new Content.ChannelAssetsRemovedEvent(event).data
 
   // load channel
-  const assets = await db.getMany(DataObject, { where: { id: contentIds } })
+  const assets = await db.getMany(DataObject, { where: {
+    id: In(contentIds.toArray().map(item => item.toString()))
+  } as FindConditions<DataObject>})
 
   // delete assets
   for (const asset of assets) {
@@ -152,7 +155,7 @@ export async function content_ChannelCensorshipStatusUpdated(
   const {channelId, bool: isCensored} = new Content.ChannelCensorshipStatusUpdatedEvent(event).data
 
   // load event
-  const channel = await db.get(Channel, { where: { id: channelId } })
+  const channel = await db.get(Channel, { where: { id: channelId.toString() } as FindConditions<Channel> })
 
   // ensure channel exists
   if (!channel) {
@@ -228,7 +231,9 @@ export async function content_ChannelCategoryUpdated(
   } = new Content.ChannelCategoryUpdatedEvent(event).data
 
   // load channel category
-  const channelCategory = await db.get(ChannelCategory, { where: { id: channelCategoryId } })
+  const channelCategory = await db.get(ChannelCategory, { where: {
+    id: channelCategoryId.toString()
+  } as FindConditions<ChannelCategory> })
 
   // ensure channel exists
   if (!channelCategory) {
@@ -269,7 +274,9 @@ export async function content_ChannelCategoryDeleted(
   const {channelCategoryId} = new Content.ChannelCategoryDeletedEvent(event).data
 
   // load channel category
-  const channelCategory = await db.get(ChannelCategory, { where: { id: channelCategoryId } })
+  const channelCategory = await db.get(ChannelCategory, { where: {
+    id: channelCategoryId.toString()
+  } as FindConditions<ChannelCategory> })
 
   // ensure channel category exists
   if (!channelCategory) {

+ 4 - 3
query-node/mappings/src/content/curatorGroup.ts

@@ -1,5 +1,6 @@
 import { SubstrateEvent } from '@dzlzv/hydra-common'
 import { DatabaseManager } from '@dzlzv/hydra-db-utils'
+import { FindConditions } from 'typeorm'
 
 import { CuratorGroup } from 'query-node/src/modules/curator-group/curator-group.model'
 import { Content } from '../../../generated/types'
@@ -43,7 +44,7 @@ export async function content_CuratorGroupStatusSet(
   const {curatorGroupId, bool: isActive} = new Content.CuratorGroupStatusSetEvent(event).data
 
   // load curator group
-  const curatorGroup = await db.get(CuratorGroup, { where: { id: curatorGroupId }})
+  const curatorGroup = await db.get(CuratorGroup, { where: { id: curatorGroupId.toString() } as FindConditions<CuratorGroup>})
 
   // ensure curator group exists
   if (!curatorGroup) {
@@ -71,7 +72,7 @@ export async function content_CuratorAdded(
   const {curatorGroupId, curatorId} = new Content.CuratorAddedEvent(event).data
 
   // load curator group
-  const curatorGroup = await db.get(CuratorGroup, { where: { id: curatorGroupId }})
+  const curatorGroup = await db.get(CuratorGroup, { where: { id: curatorGroupId.toString() } as FindConditions<CuratorGroup>})
 
   // ensure curator group exists
   if (!curatorGroup) {
@@ -99,7 +100,7 @@ export async function content_CuratorRemoved(
   const {curatorGroupId, curatorId} = new Content.CuratorAddedEvent(event).data
 
   // load curator group
-  const curatorGroup = await db.get(CuratorGroup, { where: { id: curatorGroupId }})
+  const curatorGroup = await db.get(CuratorGroup, { where: { id: curatorGroupId.toString() } as FindConditions<CuratorGroup>})
 
   // ensure curator group exists
   if (!curatorGroup) {

+ 3 - 3
query-node/mappings/src/content/utils.ts

@@ -1,4 +1,3 @@
-// TODO: check all `db.get()` and similar calls recieve a proper type argument (aka add `.toString()`, etc. to those calls)
 // TODO: can we rely on db having "foreign keys"? When item is deleted will automaticly be all relations to it unset?
 //       Similarly, will saving item also save all its related items no-yet-saved in db, or do they need to saved individually?
 //       Also, is the assumption that `db.save(MyType, {myProperty: undefined})` unsets value in db correct?
@@ -8,6 +7,7 @@ import { DatabaseManager } from '@dzlzv/hydra-db-utils'
 import ISO6391 from 'iso-639-1';
 import BN from 'bn.js'
 import { u64 } from '@polkadot/types/primitive';
+import { FindConditions } from 'typeorm'
 
 // protobuf definitions
 import {
@@ -402,7 +402,7 @@ async function prepareLanguage(languageIso: string, db: DatabaseManager): Promis
   }
 
   // load language
-  const language = await db.get(Language, { where: { iso: languageIso }})
+  const language = await db.get(Language, { where: { iso: languageIso } as FindConditions<Language> })
 
   // return existing language if any
   if (language) {
@@ -448,7 +448,7 @@ async function prepareVideoMetadata(videoProtobuf: VideoMetadata.AsObject, video
 
 async function prepareVideoCategory(categoryId: number, db: DatabaseManager): Promise<VideoCategory> {
   // load video category
-  const category = await db.get(VideoCategory, { where: { id: categoryId }})
+  const category = await db.get(VideoCategory, { where: { id: categoryId.toString() } as FindConditions<VideoCategory> })
 
   // ensure video category exists
   if (!category) {

+ 11 - 8
query-node/mappings/src/content/video.ts

@@ -1,6 +1,7 @@
 import { SubstrateEvent } from '@dzlzv/hydra-common'
 import { DatabaseManager } from '@dzlzv/hydra-db-utils'
 import BN from 'bn.js'
+import { FindConditions, In } from 'typeorm'
 
 import {
   Content,
@@ -87,7 +88,7 @@ export async function content_VideoCategoryUpdated(
   } = new Content.VideoCategoryUpdatedEvent(event).data
 
   // load video category
-  const videoCategory = await db.get(VideoCategory, { where: { id: videoCategoryId } })
+  const videoCategory = await db.get(VideoCategory, { where: { id: videoCategoryId.toString() } as FindConditions<VideoCategory> })
 
   // ensure video category exists
   if (!videoCategory) {
@@ -128,7 +129,7 @@ export async function content_VideoCategoryDeleted(
   const {videoCategoryId} = new Content.VideoCategoryDeletedEvent(event).data
 
   // load video category
-  const videoCategory = await db.get(VideoCategory, { where: { id: videoCategoryId } })
+  const videoCategory = await db.get(VideoCategory, { where: { id: videoCategoryId.toString() } as FindConditions<VideoCategory> })
 
   // ensure video category exists
   if (!videoCategory) {
@@ -170,7 +171,7 @@ export async function content_VideoCreated(
   )
 
   // load channel
-  const channel = await db.get(Channel, { where: { id: channelId } })
+  const channel = await db.get(Channel, { where: { id: channelId.toString() } as FindConditions<Channel> })
 
   // ensure channel exists
   if (!channel) {
@@ -213,7 +214,7 @@ export async function content_VideoUpdated(
   } = new Content.VideoUpdatedEvent(event).data
 
   // load video
-  const video = await db.get(Video, { where: { id: videoId } })
+  const video = await db.get(Video, { where: { id: videoId.toString() } as FindConditions<Video> })
 
   // ensure video exists
   if (!video) {
@@ -269,7 +270,7 @@ export async function content_VideoDeleted(
   const {videoId} = new Content.VideoDeletedEvent(event).data
 
   // load video
-  const video = await db.get(Video, { where: { id: videoId } })
+  const video = await db.get(Video, { where: { id: videoId.toString() } as FindConditions<Video> })
 
   // ensure video exists
   if (!video) {
@@ -293,7 +294,7 @@ export async function content_VideoCensorshipStatusUpdated(
   const {videoId, bool: isCensored} = new Content.VideoCensorshipStatusUpdatedEvent(event).data
 
   // load video
-  const video = await db.get(Video, { where: { id: videoId } })
+  const video = await db.get(Video, { where: { id: videoId.toString() } as FindConditions<Video> })
 
   // ensure video exists
   if (!video) {
@@ -322,7 +323,7 @@ export async function content_FeaturedVideosSet(
   const {videoId: videoIds} = new Content.FeaturedVideosSetEvent(event).data
 
   // load old featured videos
-  const existingFeaturedVideos = await db.getMany(Video, { where: { isFeatured: true } })
+  const existingFeaturedVideos = await db.getMany(Video, { where: { isFeatured: true } as FindConditions<Video> })
 
   // comparsion utility
   const isSame = (videoIdA: string) => (videoIdB: string) => videoIdA == videoIdB
@@ -358,7 +359,9 @@ export async function content_FeaturedVideosSet(
   }
 
   // read videos previously not-featured videos that are meant to be featured
-  const videosToAdd = await db.getMany(Video, { where: { id: [toAdd] } })
+  const videosToAdd = await db.getMany(Video, { where: {
+    id: In(toAdd.map(item => item.toString()))
+  } as FindConditions<Video> })
 
   if (videosToAdd.length != toAdd.length) {
     return inconsistentState('At least one non-existing video featuring requested', toAdd)

+ 2 - 1
query-node/mappings/src/membership.ts

@@ -3,6 +3,7 @@ import { Bytes } from '@polkadot/types'
 import { MemberId } from '@joystream/types/members'
 import { SubstrateEvent } from '@dzlzv/hydra-common'
 import { DatabaseManager } from '@dzlzv/hydra-db-utils'
+import { FindConditions } from 'typeorm'
 
 import {
   inconsistentState,
@@ -153,7 +154,7 @@ export async function members_MemberSetControllerAccount(db: DatabaseManager, ev
 */
 async function getMemberById(db: DatabaseManager, id: MemberId): Promise<Membership> {
   // load member
-  const member = await db.get(Membership, { where: { id: id.toString() } })
+  const member = await db.get(Membership, { where: { id: id.toString() } as FindConditions<Membership> })
 
   // ensure member exists
   if (!member) {

+ 5 - 2
query-node/mappings/src/storage.ts

@@ -1,5 +1,6 @@
 import { SubstrateEvent } from '@dzlzv/hydra-common'
 import { DatabaseManager } from '@dzlzv/hydra-db-utils'
+import { FindConditions, In } from 'typeorm'
 
 import {
   inconsistentState,
@@ -52,7 +53,9 @@ export async function data_directory_ContentRemoved(db: DatabaseManager, event:
   const {contentId: contentIds} = new DataDirectory.ContentRemovedEvent(event).data
 
   // load assets
-  const dataObjects = await db.getMany(DataObject, { where: { joystreamContentId: contentIds }})
+  const dataObjects = await db.getMany(DataObject, { where: {
+    joystreamContentId: In(contentIds.map(item => item.toString()))
+  } as FindConditions<DataObject> })
 
   // remove assets from database
   for (let item of dataObjects) {
@@ -68,7 +71,7 @@ export async function data_directory_ContentAccepted(db: DatabaseManager, event:
   const {contentId, storageProviderId} = new DataDirectory.ContentAcceptedEvent(event).data
 
   // load asset
-  const dataObject = await db.get(DataObject, { where: { joystreamContentId: contentId }})
+  const dataObject = await db.get(DataObject, { where: { joystreamContentId: contentId.toString() } as FindConditions<DataObject>})
 
   // ensure object exists
   if (!dataObject) {