No way to get number of objects that share the same string as a property?


#1

I am trying to do something which should be seemingly simple and I can not figure out a way.

If I have a Post type in prisma, which has a field named “color”… and I want to find how many posts have the same “color”, i.e. there is 30 posts, and 5 of them have the “color” named with the string “blue”, how can I get that number 5 for the string “blue” out of the system?

It seems there is no group by, there is an aggregate for count but it only aggregates against the prisma type, and I would have to query for every possible “color”?

Also since this is simple to do in a SQL query… I have also been trying to figure out how to use “executeRaw” to accomplish this… I am using prisma cloud with heroku, prisma cloud created the server. i do not see the executeraw function on my generated prisma client or within my GQL playground. I have tried adding the variable “rawAccess: true” to my heroku server, and redeploying/generating and it doesn’t show up, this is how I added it to my config:

databases:
  default:
    connector: postgres
    migrations: true
    connectionLimit: 2
    rawAccess: true
    uri: ${DATABASE_URL}?ssl=1

This really sucks, any workaround??


#2

I solved this myself. My executeRaw wasn’t working because I wasn’t understanding my prisma cloud config(because prisma cloud is super confusing in my opinion…)

After I got that I was able to do a group by function like this if it helps anyone:

import { prisma } from '../generated/index';

const createQuery = (query) => {
  const returnQuery = `${query} `.replace(/"/g, '\\"').replace(/\n/g, ' ').replace(/\s+/g, ' ');
  return returnQuery;
};

const executeQuery = async (query) => {
  const fixedQuery = createQuery(query);
  const response = await prisma.$graphql(`
  mutation {
    executeRaw(query: "${fixedQuery}")
  }
`);

  return response.executeRaw;
};
    const results = await executeQuery('SELECT count("id") FROM "default$default"."YourTable" group by "yourField"');