Skip to content

Commit

Permalink
feat: 🎸 implement .scan() in Node.js CRUD
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Apr 27, 2024
1 parent a148fb8 commit 3d973b7
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/node-to-crud/NodeCrud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,24 +182,28 @@ export class NodeCrud implements crud.CrudApi {
}
};

public readonly list = async (collection: crud.CrudCollection): Promise<crud.CrudCollectionEntry[]> => {
assertType(collection, 'drop', 'crudfs');
public readonly scan = async function* (collection: crud.CrudCollection): AsyncIterableIterator<crud.CrudCollectionEntry> {
assertType(collection, 'scan', 'crudfs');
const dir = await this.checkDir(collection);
const dirents = (await this.fs.readdir(dir, { withFileTypes: true })) as IDirent[];
const entries: crud.CrudCollectionEntry[] = [];
for await (const entry of dirents) {
if (entry.isFile()) {
entries.push({
yield {
type: 'resource',
id: '' + entry.name,
});
};
} else if (entry.isDirectory()) {
entries.push({
yield {
type: 'collection',
id: '' + entry.name,
});
};
}
}
};

public readonly list = async (collection: crud.CrudCollection): Promise<crud.CrudCollectionEntry[]> => {
const entries: crud.CrudCollectionEntry[] = [];
for await (const entry of this.scan(collection)) entries.push(entry);
return entries;
};

Expand Down

0 comments on commit 3d973b7

Please sign in to comment.