Skip to content

Commit 3d973b7

Browse files
committed
feat: 🎸 implement .scan() in Node.js CRUD
1 parent a148fb8 commit 3d973b7

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

‎src/node-to-crud/NodeCrud.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -182,24 +182,28 @@ export class NodeCrud implements crud.CrudApi {
182182
}
183183
};
184184

185-
public readonly list = async (collection: crud.CrudCollection): Promise<crud.CrudCollectionEntry[]> => {
186-
assertType(collection, 'drop', 'crudfs');
185+
public readonly scan = async function* (collection: crud.CrudCollection): AsyncIterableIterator<crud.CrudCollectionEntry> {
186+
assertType(collection, 'scan', 'crudfs');
187187
const dir = await this.checkDir(collection);
188188
const dirents = (await this.fs.readdir(dir, { withFileTypes: true })) as IDirent[];
189-
const entries: crud.CrudCollectionEntry[] = [];
190189
for await (const entry of dirents) {
191190
if (entry.isFile()) {
192-
entries.push({
191+
yield {
193192
type: 'resource',
194193
id: '' + entry.name,
195-
});
194+
};
196195
} else if (entry.isDirectory()) {
197-
entries.push({
196+
yield {
198197
type: 'collection',
199198
id: '' + entry.name,
200-
});
199+
};
201200
}
202201
}
202+
};
203+
204+
public readonly list = async (collection: crud.CrudCollection): Promise<crud.CrudCollectionEntry[]> => {
205+
const entries: crud.CrudCollectionEntry[] = [];
206+
for await (const entry of this.scan(collection)) entries.push(entry);
203207
return entries;
204208
};
205209

0 commit comments

Comments
 (0)