common.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import { DatabaseManager } from '@joystream/hydra-common'
  2. import { BaseModel } from '@joystream/warthog'
  3. import { WorkingGroup } from '@joystream/types/augment/all'
  4. type EntityClass<T extends BaseModel> = {
  5. new (): T
  6. name: string
  7. }
  8. export async function getById<T extends BaseModel>(
  9. store: DatabaseManager,
  10. entityClass: EntityClass<T>,
  11. id: string,
  12. relations?: Exclude<
  13. keyof T & string,
  14. { [K in keyof T]: T[K] extends BaseModel | undefined ? '' : T[K] extends BaseModel[] | undefined ? '' : K }[keyof T]
  15. >[]
  16. ): Promise<T> {
  17. const result = await store.get(entityClass, { where: { id }, relations })
  18. if (!result) {
  19. throw new Error(`Expected ${entityClass.name} not found by ID: ${id}`)
  20. }
  21. return result
  22. }
  23. export type WorkingGroupModuleName = 'storageWorkingGroup' | 'contentDirectoryWorkingGroup'
  24. export function getWorkingGroupModuleName(group: WorkingGroup): WorkingGroupModuleName {
  25. if (group.isContent) {
  26. return 'contentDirectoryWorkingGroup'
  27. } else if (group.isStorage) {
  28. return 'storageWorkingGroup'
  29. }
  30. throw new Error(`Unsupported working group encountered: ${group.type}`)
  31. }