Skip to content

Instantly share code, notes, and snippets.

@Aidurber
Last active June 25, 2020 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Aidurber/a8c9b97b994fa4a0e46a9243ba378a79 to your computer and use it in GitHub Desktop.
Save Aidurber/a8c9b97b994fa4a0e46a9243ba378a79 to your computer and use it in GitHub Desktop.
Dynamic paramters Prisma 2 - Ugly. Unable to find clean abstraction in the absence of aggregations
export async function getAllProjectsByStatus(user: UserDto, status: Status) {
return getAllProjects(user, { status: status.toUpperCase() as Status });
}
type ProjectQuery = {
status?: Status;
type?: ProjectType;
};
export async function getAllProjects(
user: UserDto,
queryParams: ProjectQuery = {}
) {
const { status, type } = queryParams;
let query = `select p.*, count(t.id) as tasks from projects as p
left join tasks t on t."projectId" = p.id
where p."organisationId" = $1`;
let params: any[] = [user.organisationId];
if (status) {
params.push(status);
query += ` AND p."status" = $${params.length + 1}`;
}
if (type) {
params.push(type);
query += ` AND p."type" = $${params.length + 1}`;
}
query += ` group by p.id`;
return prisma.queryRaw(query, ...params);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment