PrismaP
Prisma12mo ago
5 replies
Acidias

findMany() vs findMany({...})

// src/lib/db/db.ts
import { PrismaClient } from '@prisma/client';

const globalForPrisma = global as unknown as { prisma?: PrismaClient };

export const prisma = globalForPrisma.prisma ?? new PrismaClient();

if (process.env.NODE_ENV !== 'production') {
  globalForPrisma.prisma = prisma;
}

async function listCounties() {
  try {
     console.log("DATABASE_URL:", process.env.DATABASE_URL);

    const counties = await prisma.county.findMany();
    console.log("Counties:", counties);
  } catch (error) {
    console.error("Error listing counties:", error);
  }
}

void listCounties();


//src/lib/db/list.ts
export async function getUKCounties(): Promise<County[]> {
  console.log('Fetching UK counties...');
  try {
    const counties = await prisma.county.findMany();
    console.log('FindMany result:', counties);
    return counties;
  } catch (error) {
    console.error('Error fetching UK counties:', error);
    throw error;
  }
}



In the db.ts its nicely returns the counties as expected.
However in the lists.ts its empty.

I've tried lot of versions and directy query or if i pass {where, selecet..} details to findMany, then it works in the getUKCounties too.
So these works well:
const counties = await prisma.$queryRaw<County[]>`SELECT * FROM "County"`;
const counties = await prisma.county.findMany({
  where: {},
  select: {
    id: true,
    name: true
  }
});


I just started to use prisma, and while I found solution, I would be curious why in db.ts works simpty findMany, but not in the lists.ts
Was this page helpful?