Add basic table sorting
All checks were successful
build / build (push) Successful in 1m20s

This commit is contained in:
2025-04-25 07:39:39 +09:30
parent cc5bbb9b6a
commit d225ff9048
2 changed files with 25 additions and 7 deletions

View File

@@ -7,6 +7,11 @@ export interface Column {
type: string;
}
export interface SortColumn {
name: string;
sortType: 'asc' | 'desc';
}
export interface RowsResponse {
rows: any[];
totalRows: bigint;
@@ -98,6 +103,7 @@ export class DuckdbService {
start: number,
numRows: number,
columns: Column[],
sorts: SortColumn[],
filters: unknown[],
aggregations: unknown[],
): Promise<RowsResponse> {
@@ -107,9 +113,12 @@ export class DuckdbService {
`SELECT COUNT(1) totalRows FROM ${sanitisedFileName(file)}`,
);
const { totalRows } = totalRowResponse.get(0)?.toJSON()!;
const response = await conn.query(
`SELECT ${columns.map((column) => `"${column.name}"`).join(', ')} FROM ${sanitisedFileName(file)} LIMIT ${numRows} OFFSET ${start}`,
);
let query = `SELECT ${columns.map((column) => `"${column.name}"`).join(', ')} FROM ${sanitisedFileName(file)} `;
if (sorts.length > 0) {
query += ` ORDER BY ${sorts.map((sort) => `"${sort.name}" ${sort.sortType}`).join(', ')}`;
}
query += ` LIMIT ${numRows} OFFSET ${start}`;
const response = await conn.query(query);
const rows = [];
for (let i = 0; i < response.numRows; i++) {
rows.push(response.get(i)?.toJSON()!);