Skip to content

Support TypeORM virtual columns #69

@rickyPanzer

Description

@rickyPanzer

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions