Giter Site home page Giter Site logo

Comments (4)

echoNeuro avatar echoNeuro commented on July 29, 2024 1

Some question

from typeorm-polymorphic.

solanamonk avatar solanamonk commented on July 29, 2024 1

I've managed to connect it with the latest version of @nestjs/typeorm (10.0.2) like this:

import { Injectable } from '@nestjs/common';
import { InjectDataSource } from '@nestjs/typeorm';
import { Organization} from './organization.entity';
import { DataSource } from 'typeorm';
import { AbstractPolymorphicRepository } from 'typeorm-polymorphic';
import { OrganizationRepository } from './organization.repository';

@Injectable()
export class OrganizationService {
  private organizationRepository: OrganizationRepository;

  constructor(
    @InjectDataSource()
    private dataSource: DataSource,
  ) {
    this.organizationRepository = AbstractPolymorphicRepository.createRepository(
      dataSource,
      OrganizationRepository,
    )
  }

  async getOrganizations(): Promise<Organization[]> {
    return await this.organizationRepository.find();
  }
}

OrganizationRepository is the standard one from the README:

import { AbstractPolymorphicRepository, PolymorphicRepository } from 'typeorm-polymorphic';
import { Organization } from './organization.entity';

@PolymorphicRepository(Organization)
export class OrganizationRepository extends AbstractPolymorphicRepository<Organization> {}

from typeorm-polymorphic.

karl-dessol avatar karl-dessol commented on July 29, 2024

Hi.

Try something like this:

@Injectable()
export class MediumService {
  constructor(
    @InjectRepository(MediumEntity)
    protected repository: AbstractPolymorphicRepository<MediumEntity>,
  ) {
    this.repository = new class extends AbstractPolymorphicRepository<MediumEntity> {}(
      this.repository.target, this.repository.manager
    )
  }
}

If you don't use the find methods from the repository (and instead use a builder or similar), you'll need to hydrate your response which will fill them with the necessary data, such as:

let response = await this.repository.createQueryBuilder().getMany()

response = await this.repository.hydrateMany(response);

from typeorm-polymorphic.

nizarfadlan avatar nizarfadlan commented on July 29, 2024

Hi.

Try something like this:

@Injectable()
export class MediumService {
  constructor(
    @InjectRepository(MediumEntity)
    protected repository: AbstractPolymorphicRepository<MediumEntity>,
  ) {
    this.repository = new class extends AbstractPolymorphicRepository<MediumEntity> {}(
      this.repository.target, this.repository.manager
    )
  }
}

If you don't use the find methods from the repository (and instead use a builder or similar), you'll need to hydrate your response which will fill them with the necessary data, such as:

let response = await this.repository.createQueryBuilder().getMany()

response = await this.repository.hydrateMany(response);

How to insert data?

from typeorm-polymorphic.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.