Joystream / Joystream/hydra

Database eager relations

Open
#372 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

low-prio medium-prio-feature
Dominant language
TypeScript
Stars
50
Forks
43
PR merge metrics
No merged PRs in 30d

Description

When using db.get provided by @dzlzv/hydra-common's DatabaseManager like this:

// input schema
type Video @entity {
  channel: Channel
  license: License
  ...
}

// db.get in mappings
const video = await db.get(Video, { where: { id: videoId.toString() } as FindConditions<Video> })

a flat video record without related objects loaded is returned

Video {
  channelId: '3',
  licenseId: '1',
  ...
}

// instead of what I expected
Video {
    channel: Channel { ... },
    license: License { ... },
}

There is a way to load related object by enumareting them in db.get parameters. That works, but
a) only 1 level deep e.g. I can access video.channel, but can't access video.channel.category.name.
b) brings extra complexity to mappings
c) requires keeping list of relations in mappings
See https://github.com/typeorm/typeorm/blob/51b2a63d/src/find-options/FindOneOptions.ts#L23

const video = await db.get(Video, { where: { id: videoId.toString() } as FindConditions<Video>, relations: ['channel', 'license'] })

From what I found there is a way to automatically load related objects if we expose typeorm's find* functions (preferebly) or QueryBuilder in DatabaseManager.
See https://orkhan.gitbook.io/typeorm/docs/eager-and-lazy-relations

If we introduce find* functions, https://github.com/Joystream/hydra/issues/370 will be solved as well.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the @dzlzv/hydra-common DatabaseManager and its db.get entry point, then compare its options with TypeORM FindOneOptions and the eager and lazy relations guidance. Determine whether exposing find* functions or QueryBuilder can load nested related objects without mapping relation lists, and verify that the approach also addresses issue 370.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
database
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.