Skip to content

Commit 9444989

Browse files
committed
Add executeAll to QueryExecutor and update searchRecords to return multiple matches
1 parent b0c9fd2 commit 9444989

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

src/services/dataio.service.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,15 +200,19 @@ export const searchRecords = (req: IReq<SearchRecordsBody>, res: IRes): IRes =>
200200
}
201201
const db: FileDB = new FileDB(generateFilePath(query.from));
202202
const data: object[] = db.readFile();
203-
const index: number = QueryExecutor.execute(query, data);
204-
if (index === -1) {
203+
const indices: number[] = QueryExecutor.executeAll(query, data);
204+
if (!indices || indices.length === 0) {
205205
console.log('No results found');
206206
return res.json({ records: [] })
207207
}
208-
const dataResult: object = data[index];
209-
convertDateToISO8601(dataResult, query.from);
210208

211-
return res.json({ records: [ResultRehydrator.filterAndRehydrate(query.attributesToSelect, data[index])] });
209+
const records = indices.map((idx: number) => {
210+
const record: object = data[idx];
211+
convertDateToISO8601(record, query.from);
212+
return ResultRehydrator.filterAndRehydrate(query.attributesToSelect, record);
213+
});
214+
215+
return res.json({ records });
212216
} catch (err) {
213217
console.log(`Encountered an error searching data: ${err.message}`);
214218
return res.status(500).json(generateErrorResponse(ErrorCode.INTERNAL_ERROR, err)).send();

src/utils/queryExecutor.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,23 @@ export class QueryExecutor {
3131
return -1;
3232
}
3333

34+
/**
35+
* Executes a query on the given input data and returns all matching indices.
36+
* @param {IQuery} query - The query to execute.
37+
* @param {object[]} inputData - The data to query against.
38+
* @returns {number[]} An array of indices for matching records (empty if none).
39+
*/
40+
public static executeAll(query: IQuery, inputData: object[]): number[] {
41+
const operation: OperationUnion = query.queryFilter.operation;
42+
const results: number[] = [];
43+
for (let index: number = 0; index < inputData.length; index++) {
44+
if (this.evaluateOperation(operation, inputData[index])) {
45+
results.push(index);
46+
}
47+
}
48+
return results;
49+
}
50+
3451
/**
3552
* Evaluates an operation on a given record.
3653
* @param {OperationUnion} operation - The operation to evaluate.

0 commit comments

Comments
 (0)