P
Prisma2mo ago
Werdox

omit with condition

I want to only omit certain fields if a condition in the same thing I'm querying is true example:
const channels = await prisma.channel.findMany({
where: { ownerId: userId },
omit: { name: /* Only omit 'name' if the channel type is 1 */ },
});
const channels = await prisma.channel.findMany({
where: { ownerId: userId },
omit: { name: /* Only omit 'name' if the channel type is 1 */ },
});
Solution:
--- Here is the omitArray function for anyone finding this later ```ts export function omitArray<Data extends object, Keys extends keyof Data>( data: Data[],...
Jump to solution
7 Replies
jonfanz
jonfanz2mo ago
Not currently possible. As a work around, you could put the condition inside your where clause. Query once where channel type is 1 (and omit name). Query another time where channel type is not 1 and don’t omit
Werdox
Werdox2mo ago
Hmm this would be possible but it intentionally orders the list to type 1 and then type 0
jonfanz
jonfanz2mo ago
Hm yes, that’s true. How large are these data sets? Would you be paginating?
Werdox
Werdox2mo ago
Pretty small for now, so no paginating
jonfanz
jonfanz2mo ago
You could then pull everything in and shuffle it. It’s not an ideal solution but it would at least be a proof of concept Otherwise, you could just pull data from your db and then iterate through setting name to undefined where needed Both are not great options, unfortunately
Werdox
Werdox2mo ago
Hmm I need the data in the original order. I do have an omitArray function which removes a property from all indecies of an array I guess I should use that then. Thought there might be a native solution.
Solution
Werdox
Werdox2mo ago
--- Here is the omitArray function for anyone finding this later
export function omitArray<Data extends object, Keys extends keyof Data>(
data: Data[],
keys: Keys[]
): Omit<Data, (typeof keys)[number]>[] {
const result = [];

for (const obj of [...data]) {
const modifiedObj = { ...obj };
for (const key of keys) {
delete modifiedObj[key];
}

result.push(modifiedObj);
}

return result;
}
export function omitArray<Data extends object, Keys extends keyof Data>(
data: Data[],
keys: Keys[]
): Omit<Data, (typeof keys)[number]>[] {
const result = [];

for (const obj of [...data]) {
const modifiedObj = { ...obj };
for (const key of keys) {
delete modifiedObj[key];
}

result.push(modifiedObj);
}

return result;
}
Want results from more Discord servers?
Add your server
More Posts
How to cover this custom SQL with PrismaHi, i currently use this SQL statement to fetch data and make calculations. Is this also possible wilength() of columnHello and good morning. Is there a way to in a query ask for the length of a column to be returned? Unit testing a result extensionHi, folks! Is there any way whatsoever to unit test a result extension. I started out simple - by juHow to populate array in one-to-many relationship (or may be even persist)?Hi. I have the following example models: ``` model User { id String @id @default(auto()Invalid `prisma.account.findUnique()` invocation:I am having issues with the google signin redirecting me to `http://localhost:3000/api/auth/error?erHow can we do arbitrary atomic number operation in prisma?Something like: ```sql SET avg = (avg + newValue) / (count + 1) ```Accelerate migration not happening Vercel Prod but does with Vercel PreviewVercel Preview migration just built and deployed just fine as expected, but Production build does noLocal sqlite db in monorepo with nextJS trying to find table 'main.TABLE' instead of 'TABLE'Hi, I'm running a monorepo with my db separate from my NextJS package, and have already implementedPrisma runtime error using direct db access in parallel with Accelerate on VercelI want to use two paths to Postgres: Prisma Accelerate and direct for a very limited use case (mentiMultiple relation between modelsHello guys, I'm creating a marketplace but I have a problem. I have 2 models: `User` and `Post`. I w