typescript - Prisma ORM computed result field in NestJs NX mono repo structure - Stack Overflow

admin2025-04-15  5

In a NX mono repo setup where a NestJs service is being developed, I'm using the Prisma ORM to interact with a MySQL database. This is a simplified Park model:

model Park {
    id          Int          @id @default(autoincrement())
    name        String       @unique
}

I've got a PrismaService that extends the PrismaClient:

import { Injectable, OnModuleInit } from '@nestjs/common';
import { PrismaClient } from '../../generated/client';

@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit {
  async onModuleInit() {
    await this.$connect();
  }
}

And a data access module to interact with the database, where @repo/prisma-client is the module that exports the PrismaService mentioned above:

import { PrismaClientModule } from '@repo/prisma-client';
import { Module } from '@nestjs/common';
import { DataAccessParkService } from './data-access-park.service';

@Module({
  imports: [PrismaClientModule],
  providers: [DataAccessParkService],
  exports: [DataAccessParkService],
})
export class DataAccessParkModule {}

The DataAccessParkService has methods to interact with the data in the database.

This all works fine, I get data when I use the methods in DataAccessParkService.

But I would like to add custom fields to the result of a query as mentioned at Prisma docs. I've tried to use the $extends method in onModuleInit and in the constructor. But I can't get it to work:

// onModuleInit version
import { Injectable, OnModuleInit } from '@nestjs/common';
import { PrismaClient } from '../../generated/client';

@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit {
  async onModuleInit() {
    this.$extends({
      result: {
        park: {
          container: {
            needs: {
              name: true,
            },
            compute(park) {
              return toBucket(`container-${park.name}`);
            },
          },
        },
      },
    });
    await this.$connect();
  }
}

And below is the version where I've tried to set it up in the constructor:

// Constructor version
import { Injectable, OnModuleInit } from '@nestjs/common';
import { PrismaClient } from '../../generated/client';

@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit {
  constructor() {
    super();
    this.$extends({
      result: {
        park: {
          container: {
            needs: {
              name: true,
            },
            compute(park) {
              return `container-${park.name}`;
            },
          },
        },
      },
    });
  }
  
  async onModuleInit() {
    await this.$connect();
  }
}

When I perform a query on another record that includes a park record, I don't get the computed field:

// This is not a direct query on the park service but an model Other with having 1 or more Cities having one or more Parks
async findById(id: number) {
    return this.prismaService.other.findUnique({
      where: {
        id,
      },
      include: {
        city: {
            include: {
                park: true,
            }
        },
      }
  })
}

So when I do a const other = await this.dataAccessParkService.findById(1); and console.log({park}) I would expect the container field to be added to the park object, but I only get the id and name fields of the park, not the computed result:

park: {
    id: 1,
    name: 'TestPark',
}

Any idea how I can get the computed field also?

转载请注明原文地址:http://www.anycun.com/QandA/1744711515a86559.html