question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TreeRepository finds don't allow options

See original GitHub issue

Issue type:

[ ] question [ ] bug report [ * ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [ ] postgres [ ] sqlite [ ] sqljs [ ] react-native

TypeORM version:

[ ] latest [ ] @next [*] 0.2.6

TreeRepository has useful methods such as findTrees, findRoots, etc. but these methods do not take find options like find does, limiting their use as you can’t filter or limit (sub)trees.

As an example I would like to find all tree roots which match some predicate (attached to a certain category) as well as only selecting the first N roots, and all of their descendants.

It also doesn’t seem to load relations, and there is no way to pass a relations parameter.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
pdashfordcommented, Apr 21, 2021

You can create your own method, here’s how I did mine

import { TreeRepository } from 'typeorm'

declare module 'typeorm/repository/TreeRepository' {
  interface TreeRepository<Entity> {
    findTreesBy(this: TreeRepository<Entity>, where, value): Promise<Entity[]>
  }
}

TreeRepository.prototype.findTreesBy = async function <Entity>(
  this: TreeRepository<Entity>,
  where,
  value
): Promise<Entity[]> {
  const escapeAlias = (alias: string) => this.manager.connection.driver.escape(alias)
  const escapeColumn = (column: string) => this.manager.connection.driver.escape(column)
  const parentPropertyName = this.manager.connection.namingStrategy.joinColumnName(
    this.metadata.treeParentRelation!.propertyName,
    this.metadata.primaryColumns[0].propertyName
  )

  const roots = await this.createQueryBuilder('treeEntity')
    .where(`${escapeAlias('treeEntity')}.${escapeColumn(parentPropertyName)} IS NULL`)
    .andWhere(`${escapeAlias('treeEntity')}.${escapeColumn(where)} = :value`, { value })
    .getMany()
  return Promise.all(roots.map((root) => this.findDescendantsTree(root)))
}

Then

  const trees = await manager.getTreeRepository(GherkinFeature).findTreesBy('name', 'a1')

Hope that helps

0reactions
jeffersonvelosocommented, Jul 7, 2022
  const trees = await manager.getTreeRepository(GherkinFeature).findTreesBy('name', 'a1')

Open a PR with this feature bro, is very relevant feature.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Class TreeRepository<Entity> - typeorm
Finds entities that match given find options. Also counts all entities that match given conditions, but ignores pagination settings (from and take options)....
Read more >
TypeORM: Relations eager on tree entity - Stack Overflow
I use @Tree decorator with 'closure-table' type which allows me to have tree relation like Parent and Children. I get trees this way:...
Read more >
TypeORM - Amazing ORM for TypeScript and JavaScript (ES7 ...
To use another database, simply change the type in the options to the database type you are using: mysql , mariadb , postgres...
Read more >
TypeORM - Quick Guide - Tutorialspoint
TypeORM provides an option to create multiple database connection as well. ... nullable − Specify whether the database field / column allows null...
Read more >
0.3.7 (2022-06-29) | TypeORM Docs
new where type signature in FindOptions (used in find* methods) now allows to build nested statements with conditional relations, for example:.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found