equalogic / typeorm-relations

A toolkit for working with TypeORM relations objects.
MIT License
7 stars 0 forks source link

Automatic filtering attributes which are relations #396

Open willdochnur opened 4 months ago

willdochnur commented 4 months ago

It would be nice if toFindOptionsRelations would filter everything out what is not a relation.

sgarner commented 4 months ago

Could you share what your use case is for this?

If your RelationMap is properly typed and you are working with relations statically, you should get a compile time error from TypeScript if you reference a relation that doesn't exist.

To detect and filter out non-existent relations at run-time would require this library to validate relations against the entity metadata. It doesn't look at the metadata at all currently, it just works with relation mapping objects in a generic way.

willdochnur commented 4 months ago

I would like to reuse fields which includes simple datatypes and relations:

const fields = {
      id: true,
      name: true,
      customer: {
        id: true,
        name: true
        anotherRelation: {
          id: true,
          name: true
        }
      }
    };
    await ManagementDataSource.getRepository(ProjectEntity).find({
      relations: projectRelationMap.toFindOptionsRelations(fields),
      select: fields
    });

toFindOptionsRelations should filter out all simple data types and take all nested relations.

Result of toFindOptionsRelations should be:

{ customer: { anotherRelation: true } }