Skip to content

Commit 932c0d8

Browse files
committed
add initial version of pagination
1 parent 64066b9 commit 932c0d8

2 files changed

Lines changed: 51 additions & 18 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ build/
1717
package
1818
/package
1919
*.db
20+
*.db*

aiq.js

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,11 @@ async function getConnectionConfig(dbConfigs, nameOrFilePath) {
2424
);
2525

2626
if (!dbConfig) {
27-
// Check if nameOrFilePath is a file directly
28-
console.log(nameOrFilePath);
2927
if (await fileExists(nameOrFilePath)) {
3028
dbConfig = { type: 'sqlite', config: { filename: nameOrFilePath } };
3129
} else {
32-
// Resolve nameOrFilePath relative to the current directory
3330
const relativePath = path.resolve(process.cwd(), nameOrFilePath);
34-
console.log(relativePath);
31+
3532
if (await fileExists(relativePath)) {
3633
dbConfig = { type: 'sqlite', config: { filename: relativePath } };
3734
} else {
@@ -179,14 +176,54 @@ async function handleUserPrompt(queryObj, adapter) {
179176
return true;
180177
}
181178

182-
async function executeWithRetries(adapter, query, client) {
179+
async function displayPagedResult(result) {
180+
const pageSize = 10;
181+
let currentPage = 0;
182+
const totalPages = Math.ceil(result.length / pageSize);
183+
184+
while (true) {
185+
const start = currentPage * pageSize;
186+
const end = start + pageSize;
187+
const pageResult = result.slice(start, end);
188+
189+
console.log(gradient.cristal(`Page ${currentPage + 1} of ${totalPages}:`));
190+
console.log(pageResult);
191+
192+
const { action } = await inquirer.prompt([
193+
{
194+
type: 'list',
195+
name: 'action',
196+
message: 'Navigate:',
197+
choices: ['Next', 'Previous', 'Exit'],
198+
},
199+
]);
200+
201+
if (action === 'Next' && currentPage < totalPages - 1) {
202+
currentPage++;
203+
} else if (action === 'Previous' && currentPage > 0) {
204+
currentPage--;
205+
} else if (action === 'Exit') {
206+
break;
207+
}
208+
}
209+
}
210+
211+
212+
async function executeWithRetries(adapter, query, client, prompt) {
183213
let retries = 2;
184214

185215
while (retries >= 0) {
186216
try {
187217
const result = await executeQuery(adapter, query);
188-
console.log(gradient.cristal("Query Result:"));
189-
console.log(result);
218+
if (result.length > 0) {
219+
if (prompt) {
220+
await displayPagedResult(result);
221+
} else {
222+
console.log(JSON.stringify(result, null, 2));
223+
}
224+
} else {
225+
console.log("No records found.");
226+
}
190227
break;
191228
} catch (error) {
192229
if (retries > 0) {
@@ -201,22 +238,17 @@ async function executeWithRetries(adapter, query, client) {
201238
}
202239
}
203240

204-
async function processQuery(dbConfigs, connectionNameOrFile, command, client) {
205-
const connectionConfig = await getConnectionConfig(
206-
dbConfigs,
207-
connectionNameOrFile,
208-
);
209-
const adapter = getDatabaseAdapter(
210-
connectionConfig.type,
211-
connectionConfig.config,
212-
);
241+
242+
async function processQuery(dbConfigs, connectionNameOrFile, command, client, prompt = true) {
243+
const connectionConfig = await getConnectionConfig(dbConfigs, connectionNameOrFile);
244+
const adapter = getDatabaseAdapter(connectionConfig.type, connectionConfig.config);
213245

214246
const queryObj = await handleQuery(command, client, adapter);
215247

216248
const shouldExecute = await handleUserPrompt(queryObj, adapter);
217249

218250
if (shouldExecute) {
219-
await executeWithRetries(adapter, queryObj.query, client);
251+
await executeWithRetries(adapter, queryObj.query, client, prompt);
220252
} else {
221253
console.log("Query execution aborted by user.");
222254
}
@@ -419,7 +451,7 @@ async function handleExecuteQuery(args, prompt) {
419451
const command = args.slice(1).join(" ");
420452
const dbConfigs = await loadConfig();
421453
const client = await setupClient(command);
422-
await processQuery(dbConfigs, connectionNameOrFile, command, client);
454+
await processQuery(dbConfigs, connectionNameOrFile, command, client, prompt);
423455
if (prompt) {
424456
await promptForCommands(dbConfigs, connectionNameOrFile, client);
425457
}

0 commit comments

Comments
 (0)