Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support TypeORM virtual columns #69

Open
rickyPanzer opened this issue Apr 19, 2024 · 0 comments
Open

Support TypeORM virtual columns #69

rickyPanzer opened this issue Apr 19, 2024 · 0 comments

Comments

@rickyPanzer
Copy link

rickyPanzer commented Apr 19, 2024

It would be nice for TypeORM virtual columns to be supported within AdminJS. I tried, but didn't have much luck

import {
  Column,
  Entity,
  JoinColumn,
  ManyToOne,
  OneToOne,
  RelationId,
  type Relation,
  VirtualColumn,
} from 'typeorm'
import { Quarter } from '../../quarter/entities/quarter.entity.js'
import { TypeormEntityBase } from '../../../infrastructure/database/base-classes/typeormbase.entity.js'
import { Company } from './company.entity.js'

@Entity()
export class CompanyQuarter extends TypeormEntityBase {

  @ManyToOne(() => Company, company => company.quarters, { nullable: false })
  company: Relation<Company>

  @RelationId((companyQuarter: CompanyQuarter) => companyQuarter.company)
  @Column({ nullable: true })
  companyId: number

  @ManyToOne(() => Quarter, quarter => quarter.companyQuarters, {
    nullable: false,
  })
  @JoinColumn()
  quarter: Relation<Quarter>

  @RelationId((companyQuarter: CompanyQuarter) => companyQuarter.quarter)
  @Column({ nullable: true })
  quarterId: number

  // Virtual column to concatenate company name, quarter quarter, and quarter year
  @VirtualColumn({
    query: alias => `
      SELECT CONCAT(c.name, ' ', q.quarter, ' ', q.year)
      FROM company_quarter cq
      JOIN company c ON c.id = cq.companyId
      JOIN quarter q ON q.id = cq.quarterId
      WHERE cq.id = ${alias}.id
    `,
  })
  companyQuarterCompositeName: string
}

admin-dash-resources.provider.ts

  {
    resource: CompanyQuarter,
    options: {
      navigation: {
        name: 'Admin User',
      },
      listProperties: [
        'id',
        'createdAt',
        'companyId',
        'quarterId',
        'companyQuarterCompositeName',
      ],
      filterProperties: [
        'id',
        'createdAt',
        'companyId',
        'quarterId',
        'companyQuarterCompositeName',
      ],
      editProperties: [
        'id',
        'createdAt',
        'companyId',
        'quarterId',
        'companyQuarterCompositeName',
      ],
      showProperties: [
        'id',
        'createdAt',
        'companyId',
        'quarterId',
        'companyQuarterCompositeName',
      ],
    },
  },

produces this error

query failed: SELECT "CompanyQuarter"."id" AS "CompanyQuarter_id", "CompanyQuarter"."createdAt" AS "CompanyQuarter_createdAt", "CompanyQuarter"."updatedAt" AS "CompanyQuarter_updatedAt", "CompanyQuarter"."reserves" AS "CompanyQuarter_reserves", "CompanyQuarter"."investorUpdate" AS "CompanyQuarter_investorUpdate", "CompanyQuarter"."fundId" AS "CompanyQuarter_fundId", "CompanyQuarter"."companyId" AS "CompanyQuarter_companyId", "CompanyQuarter"."quarterId" AS "CompanyQuarter_quarterId" FROM "company_quarter" "CompanyQuarter" ORDER BY "CompanyQuarter"."companyQuarterCompositeName" ASC LIMIT 10
error: error: column CompanyQuarter.companyQuarterCompositeName does not exist

I also just updated all of the packages

    "@adminjs/express": "^6.1.0",
    "@adminjs/nestjs": "^6.1.0",
    "@adminjs/typeorm": "^5.0.1",
    "@nestjs/typeorm": "^10.0.2",
    "typeorm": "^0.3.20",

If anyone knows how to achieve this that would be great! I'm trying to get the name to populate from a virtual column. The error did happen when I sorted on the virtual field. But also the virtual field renders blank in adminjs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant