PrismaP
Prisma2y ago
5 replies
Jonathan

Ensuring Filtering if variable is undefined

I have an application that is a multi-tenant app where we always filter by TenantId , so for example we do queries like this:

 var contacts = await db.contacts.findMany({
        where: {
            tenantId: tenantId,
        }
    });


While obviously I need to check the tenantId but from a general question, when a a variable sometimes is undefined, it completely ignores that part of query. Some of this is just the nature of how JS objects are built.

But I am curious if there is a way to guarantee that the where statement never ignores a variable. I think this is where most query builder alternatives handle by passing variables into a method.

Basically I am curious if there is ways to guarantee that no filter leaks because of a value being undefined.
Was this page helpful?