Skip to content

Commit 3bed5c3

Browse files
committed
fix: searchLogs
1 parent 1b5e506 commit 3bed5c3

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

lib/index.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -400,21 +400,21 @@ class ErrsolePostgres extends EventEmitter {
400400
* @returns {Promise<{items: Log[], filters: LogFilter[]}>} - A promise that resolves with an object containing an array of log items and the applied filters.
401401
* @throws {Error} - Throws an error if the operation fails.
402402
*/
403-
async searchLogs (searchTerms, filters = {}) {
403+
async searchLogs (searchTerms = [], filters = {}) {
404404
const DEFAULT_LOGS_LIMIT = 100;
405405
filters.limit = filters.limit || DEFAULT_LOGS_LIMIT;
406406

407407
const whereClauses = [];
408408
const values = [];
409409
let orderBy = 'id DESC';
410410
let shouldReverse = true;
411-
let index = 1;
412411

413-
// Search terms with AND logic
414-
if (searchTerms && searchTerms.length > 0) {
415-
const tsQuery = searchTerms.map(term => `${term}:*`).join(' & '); // AND logic for search terms
416-
whereClauses.push(`message_tsv @@ to_tsquery('english', $${index++})`);
417-
values.push(tsQuery);
412+
if (searchTerms.length > 0) {
413+
const whereConditions = searchTerms.map(
414+
(_, i) => `message_tsv @@ phraseto_tsquery('english', $${values.length + i + 1})`
415+
);
416+
whereClauses.push(`(${whereConditions.join(' AND ')})`);
417+
values.push(...searchTerms);
418418
}
419419

420420
// Apply filters
@@ -438,48 +438,48 @@ class ErrsolePostgres extends EventEmitter {
438438
whereClauses.push(`(${orConditions.join(' OR ')})`);
439439
}
440440
if (filters.lt_id) {
441-
whereClauses.push(`id < $${index++}`);
441+
whereClauses.push(`id < $${values.length + 1}`);
442442
values.push(filters.lt_id);
443443
orderBy = 'id DESC';
444444
shouldReverse = true;
445445
}
446446
if (filters.gt_id) {
447-
whereClauses.push(`id > $${index++}`);
447+
whereClauses.push(`id > $${values.length + 1}`);
448448
values.push(filters.gt_id);
449449
orderBy = 'id ASC';
450450
shouldReverse = false;
451451
}
452452
if (filters.lte_timestamp || filters.gte_timestamp) {
453453
if (filters.lte_timestamp) {
454454
filters.lte_timestamp = new Date(filters.lte_timestamp);
455-
whereClauses.push(`timestamp <= $${index++}`);
455+
whereClauses.push(`timestamp <= $${values.length + 1}`);
456456
values.push(filters.lte_timestamp);
457457
orderBy = 'timestamp DESC, id DESC';
458458
shouldReverse = true;
459459
}
460460
if (filters.gte_timestamp) {
461461
filters.gte_timestamp = new Date(filters.gte_timestamp);
462-
whereClauses.push(`timestamp >= $${index++}`);
462+
whereClauses.push(`timestamp >= $${values.length + 1}`);
463463
values.push(filters.gte_timestamp);
464464
orderBy = 'timestamp ASC, id ASC';
465465
shouldReverse = false;
466466
}
467467
if (filters.lte_timestamp && !filters.gte_timestamp) {
468468
const gteTimestamp = new Date(filters.lte_timestamp.getTime() - 24 * 60 * 60 * 1000);
469-
whereClauses.push(`timestamp >= $${index++}`);
469+
whereClauses.push(`timestamp >= $${values.length + 1}`);
470470
values.push(gteTimestamp);
471471
filters.gte_timestamp = gteTimestamp;
472472
}
473473
if (filters.gte_timestamp && !filters.lte_timestamp) {
474474
const lteTimestamp = new Date(filters.gte_timestamp.getTime() + 24 * 60 * 60 * 1000);
475-
whereClauses.push(`timestamp <= $${index++}`);
475+
whereClauses.push(`timestamp <= $${values.length + 1}`);
476476
values.push(lteTimestamp);
477477
filters.lte_timestamp = lteTimestamp;
478478
}
479479
}
480480

481481
const whereClause = whereClauses.length ? `WHERE ${whereClauses.join(' AND ')}` : '';
482-
const query = `SELECT id, hostname, pid, source, timestamp, level, message,errsole_id FROM ${this.logsTable} ${whereClause} ORDER BY ${orderBy} LIMIT $${index}`;
482+
const query = `SELECT id, hostname, pid, source, timestamp, level, message,errsole_id FROM ${this.logsTable} ${whereClause} ORDER BY ${orderBy} LIMIT $${values.length + 1}`;
483483
values.push(filters.limit);
484484

485485
const { rows } = await this.pool.query(query, values);

0 commit comments

Comments
 (0)