fix(projects): replace \$queryRawUnsafe with Prisma.sql tagged templates in search
- Replace both \$queryRawUnsafe calls in search() with \$queryRaw + Prisma.sql/Prisma.join
- Remove no-op .replace() regex on positional parameters (A-23)
- Change import type { Prisma } to import { Prisma } so Prisma.sql/Prisma.join
are available as runtime values
Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import type { Prisma } from '@prisma/client';
|
import { Prisma } from '@prisma/client';
|
||||||
import { PrismaService } from '@modules/shared';
|
import { PrismaService } from '@modules/shared';
|
||||||
import { ProjectDevelopmentEntity } from '../../domain/entities/project-development.entity';
|
import { ProjectDevelopmentEntity } from '../../domain/entities/project-development.entity';
|
||||||
import type {
|
import type {
|
||||||
@@ -132,58 +132,49 @@ export class PrismaProjectDevelopmentRepository implements IProjectRepository {
|
|||||||
const limit = params.limit ?? 20;
|
const limit = params.limit ?? 20;
|
||||||
const offset = (page - 1) * limit;
|
const offset = (page - 1) * limit;
|
||||||
|
|
||||||
const conditions: string[] = ['1=1'];
|
const clauses: Prisma.Sql[] = [Prisma.sql`1=1`];
|
||||||
const values: unknown[] = [];
|
|
||||||
let paramIndex = 1;
|
|
||||||
|
|
||||||
if (params.status) {
|
if (params.status) {
|
||||||
conditions.push(`status = $${paramIndex++}::"ProjectDevelopmentStatus"`);
|
clauses.push(Prisma.sql`status = ${params.status}::"ProjectDevelopmentStatus"`);
|
||||||
values.push(params.status);
|
|
||||||
}
|
}
|
||||||
if (params.city) {
|
if (params.city) {
|
||||||
conditions.push(`city = $${paramIndex++}`);
|
clauses.push(Prisma.sql`city = ${params.city}`);
|
||||||
values.push(params.city);
|
|
||||||
}
|
}
|
||||||
if (params.district) {
|
if (params.district) {
|
||||||
conditions.push(`district = $${paramIndex++}`);
|
clauses.push(Prisma.sql`district = ${params.district}`);
|
||||||
values.push(params.district);
|
|
||||||
}
|
}
|
||||||
if (params.developer) {
|
if (params.developer) {
|
||||||
conditions.push(`developer ILIKE $${paramIndex++}`);
|
clauses.push(Prisma.sql`developer ILIKE ${'%' + params.developer + '%'}`);
|
||||||
values.push(`%${params.developer}%`);
|
|
||||||
}
|
}
|
||||||
if (params.isVerified !== undefined) {
|
if (params.isVerified !== undefined) {
|
||||||
conditions.push(`"isVerified" = $${paramIndex++}`);
|
clauses.push(Prisma.sql`"isVerified" = ${params.isVerified}`);
|
||||||
values.push(params.isVerified);
|
|
||||||
}
|
}
|
||||||
if (params.ownerId) {
|
if (params.ownerId) {
|
||||||
conditions.push(`"ownerId" = $${paramIndex++}`);
|
clauses.push(Prisma.sql`"ownerId" = ${params.ownerId}`);
|
||||||
values.push(params.ownerId);
|
|
||||||
}
|
}
|
||||||
if (params.query) {
|
if (params.query) {
|
||||||
conditions.push(`(name ILIKE $${paramIndex} OR developer ILIKE $${paramIndex} OR district ILIKE $${paramIndex} OR city ILIKE $${paramIndex})`);
|
const like = `%${params.query}%`;
|
||||||
values.push(`%${params.query}%`);
|
clauses.push(Prisma.sql`(name ILIKE ${like} OR developer ILIKE ${like} OR district ILIKE ${like} OR city ILIKE ${like})`);
|
||||||
paramIndex++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const where = conditions.join(' AND ');
|
const where = Prisma.join(clauses, ' AND ');
|
||||||
|
|
||||||
const countResult = await this.prisma.$queryRawUnsafe<[{ count: bigint }]>(
|
const countResult = await this.prisma.$queryRaw<[{ count: bigint }]>(
|
||||||
`SELECT COUNT(*)::bigint as count FROM "ProjectDevelopment" WHERE ${where}`,
|
Prisma.sql`SELECT COUNT(*)::bigint as count FROM "ProjectDevelopment" WHERE ${where}`,
|
||||||
...values,
|
|
||||||
);
|
);
|
||||||
const total = Number(countResult[0].count);
|
const total = Number(countResult[0].count);
|
||||||
|
|
||||||
const rows = await this.prisma.$queryRawUnsafe<RawProjectDetail[]>(
|
const rows = await this.prisma.$queryRaw<RawProjectDetail[]>(
|
||||||
`SELECT p.*, ST_Y(p.location::geometry) as lat, ST_X(p.location::geometry) as lng,
|
Prisma.sql`
|
||||||
COUNT(pr.id)::int as "propertyCount"
|
SELECT p.*, ST_Y(p.location::geometry) as lat, ST_X(p.location::geometry) as lng,
|
||||||
FROM "ProjectDevelopment" p
|
COUNT(pr.id)::int as "propertyCount"
|
||||||
LEFT JOIN "Property" pr ON pr."projectDevelopmentId" = p.id
|
FROM "ProjectDevelopment" p
|
||||||
WHERE ${where.replace(/\b(\$\d+)/g, (_, m) => m)}
|
LEFT JOIN "Property" pr ON pr."projectDevelopmentId" = p.id
|
||||||
GROUP BY p.id
|
WHERE ${where}
|
||||||
ORDER BY p."createdAt" DESC
|
GROUP BY p.id
|
||||||
LIMIT $${paramIndex++} OFFSET $${paramIndex}`,
|
ORDER BY p."createdAt" DESC
|
||||||
...values, limit, offset,
|
LIMIT ${limit} OFFSET ${offset}
|
||||||
|
`,
|
||||||
);
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
Reference in New Issue
Block a user