feature: .all() to iterate over all objects in a collection
This commit is contained in:
parent
bf2bec0c3d
commit
56715a1400
5 changed files with 147 additions and 5 deletions
47
fsdb.ts
47
fsdb.ts
|
@ -199,6 +199,53 @@ export class FSDB_COLLECTION<T extends Record<string, any>> {
|
|||
return item;
|
||||
}
|
||||
|
||||
/** Iterate through the items. */
|
||||
async all(input_options?: FSDB_SEARCH_OPTIONS): Promise<T[]> {
|
||||
if (Deno.env.get('FSDB_PERF')) performance.mark('fsdb_all_begin');
|
||||
|
||||
const options: FSDB_SEARCH_OPTIONS = {
|
||||
...{
|
||||
limit: 100,
|
||||
offset: 0
|
||||
},
|
||||
...(input_options ?? {})
|
||||
};
|
||||
|
||||
const results: T[] = [];
|
||||
|
||||
const limit = options?.limit ?? 100;
|
||||
const offset = options?.offset ?? 0;
|
||||
let counter = 0;
|
||||
|
||||
// TODO: better way to get a pattern to match files in this collection?
|
||||
for await (
|
||||
const entry of fs.walk(this.config.root, {
|
||||
includeDirs: false,
|
||||
includeSymlinks: false,
|
||||
skip: [/\.fsdb\.collection\.json$/],
|
||||
exts: ['json']
|
||||
})
|
||||
) {
|
||||
if (counter < offset) {
|
||||
++counter;
|
||||
continue;
|
||||
}
|
||||
|
||||
const content = await Deno.readTextFile(entry.path);
|
||||
results.push(JSON.parse(content));
|
||||
++counter;
|
||||
|
||||
if (counter >= (offset + limit)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (Deno.env.get('FSDB_PERF')) performance.mark('fsdb_all_end');
|
||||
if (Deno.env.get('FSDB_PERF')) console.dir(performance.measure('fsdb all items time', 'fsdb_all_begin', 'fsdb_all_end'));
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
/** Use indexes to search for matching items. */
|
||||
async find(criteria: Record<string, any>, input_options?: FSDB_SEARCH_OPTIONS): Promise<T[]> {
|
||||
if (Deno.env.get('FSDB_PERF')) performance.mark('fsdb_find_begin');
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue