Skip to content

Commit

Permalink
Assert the value is unique as intended
Browse files Browse the repository at this point in the history
  • Loading branch information
Drarig29 committed Jul 2, 2023
1 parent 46f4af2 commit 0fff3e7
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/base/getter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ export class BaseGetter {
*/
private async getUpperBracketFirstRound(stageId: Id): Promise<Round> {
// Considering the database is ordered, this round will always be the first round of the upper bracket.
const firstRound = await this.storage.selectFirst('round', { stage_id: stageId, number: 1 });
const firstRound = await this.storage.selectFirst('round', { stage_id: stageId, number: 1 }, false);
if (!firstRound) throw Error('Round not found.');
return firstRound;
}
Expand All @@ -458,7 +458,7 @@ export class BaseGetter {
* @param groupId ID of the group.
*/
private async getLastRound(groupId: Id): Promise<Round> {
const round = await this.storage.selectLast('round', { group_id: groupId });
const round = await this.storage.selectLast('round', { group_id: groupId }, false);
if (!round) throw Error('Error getting rounds.');
return round;
}
Expand Down
6 changes: 3 additions & 3 deletions src/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,10 @@ export class Get extends BaseGetter {
* @param stage The stage.
*/
private async eliminationSeeding(stage: Stage): Promise<ParticipantSlot[]> {
const round = await this.storage.selectFirst('round', { stage_id: stage.id, number: 1 });
if (!round) throw Error('Error getting the first round.');
const firstRound = await this.storage.selectFirst('round', { stage_id: stage.id, number: 1 }, false);
if (!firstRound) throw Error('Error getting the first round.');

const matches = await this.storage.select('match', { round_id: round.id });
const matches = await this.storage.select('match', { round_id: firstRound.id });
if (!matches) throw Error('Error getting matches.');

return helpers.convertMatchesToSeeding(matches);
Expand Down
10 changes: 8 additions & 2 deletions src/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,25 @@ export class BracketsManager {
this.instrumentStorage();

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
this.storage.selectFirst = async (table, filter) => {
this.storage.selectFirst = async (table, filter, assertUnique = true) => {
const results = await this.storage.select(table, filter);
if (!results || results.length === 0)
return null;

if (assertUnique && results.length > 1)
throw Error(`Selecting ${JSON.stringify(filter)} on table "${table}" must return a unique value.`);

return results[0] ?? null;
};

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
this.storage.selectLast = async (table, filter) => {
this.storage.selectLast = async (table, filter, assertUnique = true) => {
const results = await this.storage.select(table, filter);
if (!results || results.length === 0) return null;

if (assertUnique && results.length > 1)
throw Error(`Selecting ${JSON.stringify(filter)} on table "${table}" must return a unique value.`);

return results[results.length - 1] ?? null;
};

Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,6 @@ export interface CrudInterface {
}

export interface Storage extends CrudInterface {
selectFirst<T extends Table>(table: T, filter: Partial<DataTypes[T]>): Promise<DataTypes[T] | null>
selectLast<T extends Table>(table: T, filter: Partial<DataTypes[T]>): Promise<DataTypes[T] | null>
selectFirst<T extends Table>(table: T, filter: Partial<DataTypes[T]>, assertUnique?: boolean): Promise<DataTypes[T] | null>
selectLast<T extends Table>(table: T, filter: Partial<DataTypes[T]>, assertUnique?: boolean): Promise<DataTypes[T] | null>
}

0 comments on commit 0fff3e7

Please sign in to comment.