Last active
June 25, 2020 14:47
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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