Skip to content

Commit 98c6328

Browse files
committed
fix: safe query
1 parent 836c844 commit 98c6328

1 file changed

Lines changed: 41 additions & 22 deletions

File tree

src/repository/repository.service.ts

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ type CreateLogs = {
5454
content: any;
5555
};
5656

57+
export function getDatabaseProvider(
58+
databaseUrl?: string,
59+
): 'mysql' | 'postgresql' | 'unknown' {
60+
if (!databaseUrl) return 'unknown';
61+
62+
if (databaseUrl.startsWith('mysql://')) return 'mysql';
63+
if (databaseUrl.startsWith('postgres://') || databaseUrl.startsWith('postgresql://'))
64+
return 'postgresql';
65+
66+
return 'unknown';
67+
}
68+
5769
export class Repository extends PrismaClient {
5870
constructor(private readonly configService: ConfigService) {
5971
super();
@@ -76,41 +88,48 @@ export class Repository extends PrismaClient {
7688
data: Partial<Webhook> & { events?: WebhookEvents },
7789
) {
7890
const find = await this.webhook.findUnique({
79-
where: {
80-
id: webhookId,
81-
},
91+
where: { id: webhookId },
8292
});
93+
8394
if (!find) {
8495
throw new NotFoundException(['Webhook not found', `Webhook id: ${webhookId}`]);
8596
}
97+
8698
try {
87-
for await (const [key, value] of Object.entries(data?.events)) {
88-
if (value === undefined) {
89-
continue;
90-
}
99+
const provider = getDatabaseProvider(process.env.DATABASE_URL);
91100

92-
if (!find?.events) {
93-
break;
94-
}
101+
if (data?.events && find.events) {
102+
for await (const [key, value] of Object.entries(data.events)) {
103+
if (value === undefined) continue;
95104

96-
const k = `ARRAY['${key}']`;
97-
const v = `to_jsonb(${value}::boolean)`;
105+
if (provider === 'mysql') {
106+
const path = `$.${key}`;
107+
const jsonValue = value ? 'true' : 'false';
98108

99-
await this.$queryRaw(
100-
Prisma.sql`UPDATE "Webhook" SET events = jsonb_set(events, ${Prisma.raw(
101-
k,
102-
)}, ${Prisma.raw(v)}) WHERE id = ${webhookId}`,
103-
);
109+
await this.$executeRawUnsafe(
110+
`UPDATE Webhook SET events = JSON_SET(events, ?, CAST(? AS JSON)) WHERE id = ?`,
111+
path,
112+
jsonValue,
113+
webhookId,
114+
);
115+
} else if (provider === 'postgresql') {
116+
await this.$executeRawUnsafe(
117+
`UPDATE "Webhook" SET events = jsonb_set(events, '{${key}}', to_jsonb($1::boolean)) WHERE id = $2`,
118+
value,
119+
webhookId,
120+
);
121+
} else {
122+
throw new Error(`Unsupported database provider: ${provider}`);
123+
}
124+
}
104125
}
105126

106127
const updated = await this.webhook.update({
107-
where: {
108-
id: webhookId,
109-
},
128+
where: { id: webhookId },
110129
data: {
111130
url: data?.url,
112131
enabled: data?.enabled,
113-
events: !find?.events ? data?.events : undefined,
132+
events: !find.events ? data?.events : undefined,
114133
},
115134
select: {
116135
id: true,
@@ -123,7 +142,7 @@ export class Repository extends PrismaClient {
123142

124143
return updated;
125144
} catch (error) {
126-
throw new BadRequestException([error?.message, error?.stack]);
145+
throw new BadRequestException(error?.message, error?.stack);
127146
}
128147
}
129148

0 commit comments

Comments
 (0)