fix(db): 会话列表按 updated_at 再按 id 排
兼容层 order() 改为追加,不再只留最后一键。 GET /api/sessions 与套餐列表的两次 order() 都会进 SQL。
This commit is contained in:
@@ -39,6 +39,15 @@ function identifier(value: string): string {
|
||||
return `"${normalized}"`;
|
||||
}
|
||||
|
||||
export function formatOrderClause(
|
||||
ordering: readonly { column: string; ascending: boolean }[],
|
||||
): string {
|
||||
if (ordering.length === 0) return "";
|
||||
return ` order by ${ordering
|
||||
.map((item) => `${identifier(item.column)} ${item.ascending ? "asc" : "desc"}`)
|
||||
.join(", ")}`;
|
||||
}
|
||||
|
||||
export function upsertConflictColumns(options?: { onConflict?: string }): string[] {
|
||||
return (options?.onConflict ?? "")
|
||||
.split(",")
|
||||
@@ -184,8 +193,7 @@ class LocalPostgresQueryBuilder implements PromiseLike<QueryResult> {
|
||||
private selectedColumns: string[] | null = null;
|
||||
private mutation: Mutation | null = null;
|
||||
private readonly filters: Filter[] = [];
|
||||
private ordering: Readonly<{ column: string; ascending: boolean }> | null =
|
||||
null;
|
||||
private ordering: Array<{ column: string; ascending: boolean }> = [];
|
||||
private rowLimit: number | null = null;
|
||||
private abort: AbortSignal | null = null;
|
||||
private cardinality: "many" | "single" | "maybeSingle" = "many";
|
||||
@@ -289,7 +297,7 @@ class LocalPostgresQueryBuilder implements PromiseLike<QueryResult> {
|
||||
|
||||
order(column: string, options: { ascending?: boolean } = {}) {
|
||||
identifier(column);
|
||||
this.ordering = { column, ascending: options.ascending !== false };
|
||||
this.ordering.push({ column, ascending: options.ascending !== false });
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -389,9 +397,7 @@ class LocalPostgresQueryBuilder implements PromiseLike<QueryResult> {
|
||||
.join(", ");
|
||||
sql = `select ${selected} from public.${identifier(this.table)}`;
|
||||
sql += this.filterClause(parameters, types);
|
||||
if (this.ordering) {
|
||||
sql += ` order by ${identifier(this.ordering.column)} ${this.ordering.ascending ? "asc" : "desc"}`;
|
||||
}
|
||||
sql += formatOrderClause(this.ordering);
|
||||
if (this.rowLimit !== null) sql += ` limit ${this.rowLimit}`;
|
||||
} else if (
|
||||
this.mutation.kind === "insert" ||
|
||||
|
||||
Reference in New Issue
Block a user