Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add loadManyByIDsNullableAsync loader method #214

Merged
merged 1 commit into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/entity/src/EnforcingEntityLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@ export default class EnforcingEntityLoader<
return mapMap(entityResults, (result) => result.enforceValue());
}

/**
* Enforcing version of entity loader method by the same name.
* @throws EntityNotAuthorizedError when viewer is not authorized to view one or more of the returned entities
*/
async loadManyByIDsNullableAsync(ids: readonly TID[]): Promise<ReadonlyMap<TID, TEntity | null>> {
const entityResults = await this.entityLoader.loadManyByIDsNullableAsync(ids);
return mapMap(entityResults, (result) => result?.enforceValue() ?? null);
}

/**
* Enforcing version of entity loader method by the same name.
* @throws EntityNotAuthorizedError when viewer is not authorized to view one or more of the returned entities
Expand Down
17 changes: 17 additions & 0 deletions packages/entity/src/EntityLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,23 @@
});
}

/**
* Loads many entities for a list of IDs, returning null for any IDs that are non-existent.
* @param ids - IDs of the entities to load
* @returns map from ID to nullable corresponding entity result, where result error can be UnauthorizedError or EntityNotFoundError.
*/
async loadManyByIDsNullableAsync(
ids: readonly TID[]
): Promise<ReadonlyMap<TID, Result<TEntity> | null>> {
const entityResults = (await this.loadManyByFieldEqualingManyAsync(
this.entityConfiguration.idField as TSelectedFields,
ids
)) as ReadonlyMap<TID, readonly Result<TEntity>[]>;
return mapMap(entityResults, (entityResultsForId) => {

Check warning on line 200 in packages/entity/src/EntityLoader.ts

View check run for this annotation

Codecov / codecov/patch

packages/entity/src/EntityLoader.ts#L200

Added line #L200 was not covered by tests
return entityResultsForId[0] ?? null;
});
}

/**
* Loads the first entity matching the selection constructed from the conjunction of specified
* operands, or null if no matching entity exists. Entities loaded using this method are not
Expand Down
43 changes: 43 additions & 0 deletions packages/entity/src/__tests__/EnforcingEntityLoader-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,49 @@ describe(EnforcingEntityLoader, () => {
});
});

describe('loadManyByIDsNullableAsync', () => {
it('throws when result is unsuccessful even when there is a null result', async () => {
const entityLoaderMock = mock<EntityLoader<any, any, any, any, any, any>>(EntityLoader);
const rejection = new Error();
when(entityLoaderMock.loadManyByIDsNullableAsync(anything())).thenResolve(
new Map(
Object.entries({
hello: result(rejection),
world: null,
})
)
);
const entityLoader = instance(entityLoaderMock);
const enforcingEntityLoader = new EnforcingEntityLoader(entityLoader);
await expect(enforcingEntityLoader.loadManyByIDsNullableAsync(anything())).rejects.toThrow(
rejection
);
});

it('returns value when result is successful', async () => {
const entityLoaderMock = mock<EntityLoader<any, any, any, any, any, any>>(EntityLoader);
const resolved = {};
when(entityLoaderMock.loadManyByIDsNullableAsync(anything())).thenResolve(
new Map(
Object.entries({
hello: result(resolved),
world: null,
})
)
);
const entityLoader = instance(entityLoaderMock);
const enforcingEntityLoader = new EnforcingEntityLoader(entityLoader);
await expect(enforcingEntityLoader.loadManyByIDsNullableAsync(anything())).resolves.toEqual(
new Map(
Object.entries({
hello: resolved,
world: null,
})
)
);
});
});

describe('loadFirstByFieldEqualityConjunction', () => {
it('throws when result is unsuccessful', async () => {
const entityLoaderMock = mock<EntityLoader<any, any, any, any, any, any>>(EntityLoader);
Expand Down
6 changes: 5 additions & 1 deletion packages/entity/src/__tests__/EntityLoader-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,11 @@ describe(EntityLoader, () => {
await expect(entityLoader.loadByIDAsync(loadByValue)).rejects.toEqual(error);
await expect(entityLoader.enforcing().loadByIDAsync(loadByValue)).rejects.toEqual(error);
await expect(entityLoader.loadManyByIDsAsync([loadByValue])).rejects.toEqual(error);
await expect(entityLoader.loadManyByIDsAsync([loadByValue])).rejects.toEqual(error);
await expect(entityLoader.enforcing().loadManyByIDsAsync([loadByValue])).rejects.toEqual(error);
await expect(entityLoader.loadManyByIDsNullableAsync([loadByValue])).rejects.toEqual(error);
await expect(
entityLoader.enforcing().loadManyByIDsNullableAsync([loadByValue])
).rejects.toEqual(error);
await expect(
entityLoader.loadManyByFieldEqualingAsync('customIdField', loadByValue)
).rejects.toEqual(error);
Expand Down
Loading