Currently, I am attempting to implement a more general method to retrieve data from Prisma. The function in question appears as follows:
import { Prisma, PrismaClient } from '@prisma/client';
import { NextApiRequest, NextApiResponse } from 'next';
const prisma = new PrismaClient();
const getAllOfResource = async (resourceName: Prisma.ModelName) => {
const resource = prisma[resourceName as Prisma.ModelName];
if (!resource.findMany) throw Error('Error: findMany not found, does resource exist?');
return await resource.findMany();
};
const validateResourceTypeExists = (resourceType: unknown): resourceType is Prisma.ModelName => {
return typeof resourceType === 'string' && Object.keys(Prisma.ModelName).includes(resourceType);
};
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const requestedResource = req.query.id;
if (!validateResourceTypeExists(requestedResource)) {
res.status(500).json({
message: 'Please use a valid resource type',
});
} else {
const resource = await getAllOfResource(requestedResource);
return res.status(200).json(resource);
}
}
Nevertheless, an error occurs when invoking the findMany()
on the resource:
This expression is not callable.
Each member of the union type '(<T extends mediaFindManyArgs<DefaultArgs>>(args?: SelectSubset<T, mediaFindManyArgs<DefaultArgs>> | undefined) => PrismaPromise<...>) | (<T extends booksFindManyArgs<...>>(args?: SelectSubset<...> | undefined) => PrismaPromise<...>)' has signatures, but none of those signatures are compatible with each other
I cannot pinpoint the reason for this issue, given that any item within Prisma.ModelName
should possess the findMany
method. Is there perhaps a different type that I overlooked or could there be a fundamental flaw in my approach?
After experimenting with a few options, it seems likely that my lack of comprehension regarding the process is causing this problem.