Skip to content

Commit e1b20f0

Browse files
committed
Enhance article retrieval by adding author ID check in the query. Update persistence layer to include table name in WHERE clause construction for improved SQL query accuracy. Refactor related functions to support this change.
1 parent 191a4ff commit e1b20f0

File tree

3 files changed

+34
-18
lines changed

3 files changed

+34
-18
lines changed

src/app/(dashboard-editor)/dashboard/articles/[uuid]/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const page: React.FC<Props> = async ({ params }) => {
2222
// eq("author_id", sessionUserId)
2323
const [article] = await persistenceRepository.article.findRows({
2424
limit: 1,
25-
where: and(eq("id", _params.uuid)),
25+
where: and(eq("id", _params.uuid), eq("author_id", sessionUserId)),
2626
columns: ["id", "title", "handle"],
2727
joins: [
2828
leftJoin<Article, User>({

src/backend/persistence/persistence-utils.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
DatabaseTableName,
23
IPersistenceLeftJoin,
34
IPersistentOrderBy,
45
SimpleWhere,
@@ -13,7 +14,8 @@ export const sql = String.raw;
1314
* @returns Object containing the WHERE clause and values array
1415
*/
1516
export const buildWhereClause = <T>(
16-
where: WhereCondition<T> | undefined
17+
where: WhereCondition<T> | undefined,
18+
tableName: DatabaseTableName
1719
): { whereClause: string; values: any[] } => {
1820
// If no where clause is provided, return empty
1921
if (!where) {
@@ -23,10 +25,12 @@ export const buildWhereClause = <T>(
2325
const values: any[] = [];
2426

2527
// Process the where condition
26-
const result = processWhereCondition(where, values);
28+
const whereClause = processWhereCondition(where, values, tableName);
29+
30+
console.log({ whereClause, tableName });
2731

2832
return {
29-
whereClause: result,
33+
whereClause,
3034
values,
3135
};
3236
};
@@ -36,13 +40,14 @@ export const buildWhereClause = <T>(
3640
*/
3741
const processWhereCondition = <T>(
3842
where: WhereCondition<T>,
39-
values: any[]
43+
values: any[],
44+
tableName: DatabaseTableName
4045
): string => {
4146
// Handle composite conditions (AND/OR)
4247
if (typeof where === "object") {
4348
if ("AND" in where && Array.isArray(where.AND) && where.AND.length > 0) {
4449
const conditions = where.AND.map((condition) =>
45-
processWhereCondition(condition, values)
50+
processWhereCondition(condition, values, tableName)
4651
).filter(Boolean);
4752

4853
if (conditions.length === 0) return "";
@@ -53,7 +58,7 @@ const processWhereCondition = <T>(
5358

5459
if ("OR" in where && Array.isArray(where.OR) && where.OR.length > 0) {
5560
const conditions = where.OR.map((condition) =>
56-
processWhereCondition(condition, values)
61+
processWhereCondition(condition, values, tableName)
5762
).filter(Boolean);
5863

5964
if (conditions.length === 0) return "";
@@ -64,7 +69,7 @@ const processWhereCondition = <T>(
6469

6570
// Handle simple conditions
6671
if ("key" in where && "operator" in where) {
67-
return processSimpleCondition(where as SimpleWhere<T>, values);
72+
return processSimpleCondition(where as SimpleWhere<T>, values, tableName);
6873
}
6974
}
7075

@@ -76,7 +81,8 @@ const processWhereCondition = <T>(
7681
*/
7782
const processSimpleCondition = <T>(
7883
condition: SimpleWhere<T>,
79-
values: any[]
84+
values: any[],
85+
tableName: DatabaseTableName
8086
): string => {
8187
const { key, operator, value } = condition;
8288

@@ -96,21 +102,21 @@ const processSimpleCondition = <T>(
96102
})
97103
.join(", ");
98104

99-
return `"${key.toString()}" ${operator} (${placeholders})`;
105+
return `"${tableName}"."${key.toString()}" ${operator} (${placeholders})`;
100106
}
101107

102108
// Handle NULL values
103109
if (value === null) {
104110
return operator === "="
105-
? `"${key.toString()}" IS NULL`
111+
? `"${tableName}"."${key.toString()}" IS NULL`
106112
: operator === "<>"
107-
? `"${key.toString()}" IS NOT NULL`
108-
: `"${key.toString()}" IS NULL`;
113+
? `"${tableName}"."${key.toString()}" IS NOT NULL`
114+
: `"${tableName}"."${key.toString()}" IS NULL`;
109115
}
110116

111117
// Standard case with non-null value
112118
values.push(value);
113-
return `"${key.toString()}" ${operator} $${values.length}`;
119+
return `"${tableName}"."${key.toString()}" ${operator} $${values.length}`;
114120
};
115121

116122
/**

src/backend/persistence/persistence.repository.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ export class PersistentRepository<DOMAIN_MODEL_TYPE> {
7070
?.map((col) => `${this.tableName}.${col.toString()}`)
7171
.join(",") ?? `${this.tableName}.*`;
7272

73-
const { whereClause, values } = buildWhereClause(payload.where);
73+
const { whereClause, values } = buildWhereClause(
74+
payload.where,
75+
this.tableName
76+
);
7477
const orderByClause = buildOrderByClause(payload?.orderBy);
7578
const { joinConditionClause, joinSelectClause } = buildJoinClause(
7679
payload.joins
@@ -102,7 +105,10 @@ export class PersistentRepository<DOMAIN_MODEL_TYPE> {
102105
async findRowCount(
103106
payload: IPersistentPaginationPayload<DOMAIN_MODEL_TYPE>
104107
): Promise<number> {
105-
const { whereClause, values } = buildWhereClause(payload.where);
108+
const { whereClause, values } = buildWhereClause(
109+
payload.where,
110+
this.tableName
111+
);
106112

107113
// Construct the SQL query
108114
const query = `
@@ -176,7 +182,8 @@ export class PersistentRepository<DOMAIN_MODEL_TYPE> {
176182
): Promise<DOMAIN_MODEL_TYPE> {
177183
// Build WHERE clause
178184
const { whereClause, values: whereValues } = buildWhereClause(
179-
payload.where
185+
payload.where,
186+
this.tableName
180187
);
181188

182189
// Build SET clause using the where values as starting point
@@ -213,7 +220,10 @@ export class PersistentRepository<DOMAIN_MODEL_TYPE> {
213220
async deleteRows(
214221
payload: IPersistentQueryPayload<DOMAIN_MODEL_TYPE>
215222
): Promise<DOMAIN_MODEL_TYPE[]> {
216-
const { whereClause, values } = buildWhereClause(payload.where);
223+
const { whereClause, values } = buildWhereClause(
224+
payload.where,
225+
this.tableName
226+
);
217227
const columns = toSnakeCase(payload.columns as any) || "*";
218228

219229
const sql = `

0 commit comments

Comments
 (0)