Paul Giovanni
Paul Giovanni

Reputation: 27

Prisma and node.js complex module issues

I'm going to try my best to reproduce the issues in this case. I'll probably delete it if I can't get the point across well enough. My development environment includes dockerand node.js

I have 7 models within my prisma schema:

User AuditLog Finance VaultTransaction Empire WarLogEntry Structure

The prisma schema is laid out as such

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

generator client {
  provider = "prisma-client-js"
}

model User {
  id       String     @id @default(uuid())
  username String
  mail     String
  audit    AuditLog[]
  empire   Empire?
}

model AuditLog {
  id      String   @id @default(uuid())
  userId  String   @map("user_id")
  user    User     @relation(fields: [userId], references: [id])
  time    DateTime
  ip      String
  client  String
  i18nKey String   @map("i18n_key")
}

model Finance {
  id           String             @id @default(uuid())
  gold         Float              @default(0)
  vaultGold    Float              @default(0)
  transactions VaultTransaction[]
  empireId     String             @map("empire_id")
  empire       Empire             @relation(fields: [empireId], references: [id])
}

model VaultTransaction {
  id        String  @id @default(uuid())
  financeId String  @map("finance_id")
  finance   Finance @relation(fields: [financeId], references: [id])
  i18nKey   String  @map("i18n_key")
  amount    Float
}

model Empire {
  id             String        @id @default(uuid())
  userId         String        @map("user_id")
  user           User          @relation(fields: [userId], references: [id])
  bannerId       Int           @default(0) @map("banner_id")
  description    String?
  armory         Json
  population     Json
  level          Int           @default(0)
  experience     Int           @default(0)
  attributes     Json
  turnsAvailable Int           @map("turns_available")
  race           String
  class          String
  finance        Finance?
  attacks        WarLogEntry[] @relation("empire_attacks")
  defenses       WarLogEntry[] @relation("empire_defenses")
  structures     Structure[]
}

model WarLogEntry {
  id         String @id @default(uuid())
  battleType String @map("battle_type")
  attackerID String @map("attacker_id")
  attacker   Empire @relation("empire_attacks", fields: [attackerID], references: [id])
  defenderID String @map("defender_id")
  defender   Empire @relation("empire_defenses", fields: [defenderID], references: [id])
  turns   Int
  details Json
}
model Structure {
  id         String @id @default(uuid())
  empireId   String @map("empire_id")
  empire     Empire @relation(fields: [empireId], references: [id])
  i18nKey    String @map("i18n_key")
  category   String
  attributes Json
}

I have type files, resolver files, service files, and module files for each model, but prisma seems to be having some strange errors. I believe they are circular dependency errors, but I fixed the type files to make each complex module @Field optional so that should've fixed it.

My current errors node.js are as such (they change slightly depending on how i edit the files) It appears it is a prisma issue.

[10:14:54 AM] Starting compilation in watch mode...

src/model/empire/empire.service.ts:8:5 - error TS2322: Type '(Empire & { finance: Finance; attacks: WarLogEntry[]; defenses: WarLogEntry[]; structures: Structure[]; })[]' is not assignable to type 'Empire[]'.
  Type 'Empire & { finance: Finance; attacks: WarLogEntry[]; defenses: WarLogEntry[]; structures: Structure[]; }' is not assignable to type 'Empire'.
    The types of 'finance.gold' are incompatible between these types.
      Type 'Decimal' is not assignable to type 'number'.

  8     return prisma.empire.findMany({
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  9       include: {
    ~~~~~~~~~~~~~~~~
... 
 14       },
    ~~~~~~~~
 15     });
    ~~~~~~~

src/model/finance/finance.service.ts:8:5 - error TS2322: Type '(Finance & { transactions: VaultTransaction[]; })[]' is not assignable to type 'Finance[]'.
  Type 'Finance & { transactions: VaultTransaction[]; }' is not assignable to type 'Finance'.
    Types of property 'gold' are incompatible.
      Type 'Decimal' is not assignable to type 'number'.

  8     return prisma.finance.findMany({
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  9       include: {
    ~~~~~~~~~~~~~~~~
... 
 11       },
    ~~~~~~~~
 12     });
    ~~~~~~~

src/model/vault-transaction/vault-transaction.service.ts:8:5 - error TS2322: Type 'import("/home/paul/dukesthrone/DukesThrone/dt-backend/node_modules/.pnpm/@[email protected][email protected]/node_modules/.prisma/client/index").VaultTransaction[]' is not assignable to type 'import("/home/paul/dukesthrone/DukesThrone/dt-backend/src/model/types/vault-transaction.type").VaultTransaction[]'.
  Type 'import("/home/paul/dukesthrone/DukesThrone/dt-backend/node_modules/.pnpm/@[email protected][email protected]/node_modules/.prisma/client/index").VaultTransaction' is not assignable to type 'import("/home/paul/dukesthrone/DukesThrone/dt-backend/src/model/types/vault-transaction.type").VaultTransaction'.
    Types of property 'amount' are incompatible.
      Type 'Decimal' is not assignable to type 'number'.

8     return prisma.vaultTransaction.findMany();
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

[10:14:56 AM] Found 3 errors. Watching for file changes.

Here is the finance.service.ts file for model Finance. The return type includes transactions which is of type model VaultTransactions

@Injectable()
export class FinanceService {
  async getFinances(): Promise<Finance[]> {
    return prisma.finance.findMany({
      include: {
        transactions: true,
      },
    });
  }
}

Update: im not sure if this is relevant, but i think it may be. It's an issue tracker on github for circular dependcies in node.js. https://github.com/MichalLytek/type-graphql/pull/237

Upvotes: 0

Views: 1187

Answers (1)

Ryan
Ryan

Reputation: 6327

Your return type is incorrect as it's not just returning Finance data but also transactions inside Finance.

The type should be:

import { Prisma } from '@prisma/client'

type GetFinances = Prisma.FinanceGetPayload<{
  include: { transactions: true }
}>

@Injectable()
export class FinanceService {
  async getFinances(): Promise<GetFinances[]> {
    return prisma.finance.findMany({
      include: {
        transactions: true,
      },
    });
  }
}

Upvotes: 1

Related Questions