Skip to content

Commit

Permalink
refactor: use Promise.all to await for independent promises (#373)
Browse files Browse the repository at this point in the history
  • Loading branch information
pepakriz committed Mar 6, 2020
1 parent f813568 commit 1732c91
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
6 changes: 4 additions & 2 deletions lib/EntityManager.ts
Expand Up @@ -132,8 +132,10 @@ export class EntityManager<D extends IDatabaseDriver = IDatabaseDriver> {
* where first element is the array of entities and the second is the count.
*/
async findAndCount<T extends AnyEntity<T>>(entityName: EntityName<T>, where: FilterQuery<T>, populate?: string[] | boolean | FindOptions, orderBy?: QueryOrderMap, limit?: number, offset?: number): Promise<[T[], number]> {
const entities = await this.find(entityName, where, populate as string[], orderBy, limit, offset);
const count = await this.count<T>(entityName, where);
const [entities, count] = await Promise.all([
this.find(entityName, where, populate as string[], orderBy, limit, offset),
this.count(entityName, where),
]);

return [entities, count];
}
Expand Down
13 changes: 8 additions & 5 deletions lib/cache/FileCacheAdapter.ts
Expand Up @@ -34,9 +34,12 @@ export class FileCacheAdapter implements CacheAdapter {
* @inheritdoc
*/
async set(name: string, data: any, origin: string): Promise<void> {
const path = await this.path(name);
const [path, hash] = await Promise.all([
this.path(name),
this.getHash(origin),
]);

const opts = this.pretty ? { spaces: 2 } : {};
const hash = await this.getHash(origin);
await writeJSON(path, { data, origin, hash }, opts);
}

Expand All @@ -47,9 +50,9 @@ export class FileCacheAdapter implements CacheAdapter {
const path = await this.path('*');
const files = await globby(path);

for (const file of files) {
await unlink(file);
}
await Promise.all(
files.map((file) => unlink(file)),
);
}

private async path(name: string): Promise<string> {
Expand Down
8 changes: 5 additions & 3 deletions lib/schema/SchemaGenerator.ts
Expand Up @@ -17,10 +17,12 @@ export class SchemaGenerator {
private readonly config: Configuration) { }

async generate(): Promise<string> {
let ret = await this.getDropSchemaSQL(false);
ret += await this.getCreateSchemaSQL(false);
const [dropSchema, createSchema] = await Promise.all([
this.getDropSchemaSQL(false),
this.getCreateSchemaSQL(false),
]);

return this.wrapSchema(ret);
return this.wrapSchema(dropSchema + createSchema);
}

async createSchema(wrap = true): Promise<void> {
Expand Down
7 changes: 5 additions & 2 deletions tests/issues/GH228.test.ts
Expand Up @@ -68,8 +68,11 @@ describe('GH issue 228', () => {
orderBy: { type: 'asc' },
populate: true,
});
expect(mock.mock.calls[0][0]).toMatch('select `e0`.* from `a` as `e0` order by `e0`.`type_id` asc');
expect(mock.mock.calls[1][0]).toMatch('select `e0`.* from `b` as `e0` where `e0`.`id` in (?)');

const queries: string[] = mock.mock.calls.map(c => c[0]).sort();
expect(queries).toHaveLength(3);
expect(queries[0]).toMatch('select `e0`.* from `a` as `e0` order by `e0`.`type_id` asc');
expect(queries[1]).toMatch('select `e0`.* from `b` as `e0` where `e0`.`id` in (?)');
});

});

0 comments on commit 1732c91

Please sign in to comment.