Skip to content

Commit

Permalink
Infer types for DbInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
renatodellosso committed Jan 14, 2025
1 parent 8efe6f2 commit 943f121
Show file tree
Hide file tree
Showing 14 changed files with 129 additions and 117 deletions.
8 changes: 4 additions & 4 deletions lib/CompetitionHandling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ export async function AssignScoutersToCompetitionMatches(
teamId: string,
competitionId: string,
) {
const comp = await db.findObjectById<Competition>(
const comp = await db.findObjectById(
CollectionId.Competitions,
new ObjectId(competitionId),
);

const team = await db.findObject<Team>(
const team = await db.findObject(
CollectionId.Teams,
new ObjectId(teamId),
);
Expand Down Expand Up @@ -99,7 +99,7 @@ export async function generateReportsForMatch(
schedule?: ScheduleMatch,
) {
if (typeof match === "string") {
const foundMatch = await db.findObjectById<Match>(
const foundMatch = await db.findObjectById(
CollectionId.Matches,
new ObjectId(match),
);
Expand All @@ -114,7 +114,7 @@ export async function generateReportsForMatch(
match.subjectiveScouter = schedule?.subjectiveScouter;

const existingReportPromises = match.reports.map((r) =>
db.findObjectById<Report>(CollectionId.Reports, new ObjectId(r)),
db.findObjectById(CollectionId.Reports, new ObjectId(r)),
);
const existingReports = await Promise.all(existingReportPromises);

Expand Down
30 changes: 15 additions & 15 deletions lib/MongoDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,30 +101,30 @@ export class MongoDBInterface implements DbInterface {
return Promise.resolve();
}

async findObjectById<Type>(
collection: CollectionId,
id: ObjectId,
): Promise<Type> {
async findObjectById<
TId extends CollectionId,
TObj extends CollectionIdToType<TId>,
>(collection: TId, id: ObjectId): Promise<TObj> {
return (await this?.db
?.collection(collection)
.findOne({ _id: id })) as Type;
.findOne({ _id: id })) as TObj;
}

async findObject<Type>(
collection: CollectionId,
query: object,
): Promise<Type> {
return (await this?.db?.collection(collection).findOne(query)) as Type;
async findObject<
TId extends CollectionId,
TObj extends CollectionIdToType<TId>,
>(collection: TId, query: object): Promise<TObj> {
return (await this?.db?.collection(collection).findOne(query)) as TObj;
}

async findObjects<Type>(
collection: CollectionId,
query: object,
): Promise<Type[]> {
async findObjects<
TId extends CollectionId,
TObj extends CollectionIdToType<TId>,
>(collection: TId, query: object): Promise<TObj[]> {
return (await this?.db
?.collection(collection)
.find(query)
.toArray()) as Type[];
.toArray()) as TObj[];
}

async countObjects(
Expand Down
8 changes: 4 additions & 4 deletions lib/UrlResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,17 @@ export default async function UrlResolver(

try {
const promises = [
db.findObject<Team>(CollectionId.Teams, { slug: teamSlug }),
db.findObject(CollectionId.Teams, { slug: teamSlug }),
seasonSlug
? db.findObject<Season>(CollectionId.Seasons, { slug: seasonSlug })
? db.findObject(CollectionId.Seasons, { slug: seasonSlug })
: null,
competitionSlug
? db.findObject<Competition>(CollectionId.Competitions, {
? db.findObject(CollectionId.Competitions, {
slug: competitionSlug,
})
: null,
reportId
? db.findObject<Report>(CollectionId.Reports, {
? db.findObject(CollectionId.Reports, {
_id: new ObjectId(reportId),
})
: null,
Expand Down
24 changes: 12 additions & 12 deletions lib/api/AccessLevels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ namespace AccessLevels {

const team = await (
await db
).findObjectById<Team>(CollectionId.Teams, new ObjectId(teamId));
).findObjectById(CollectionId.Teams, new ObjectId(teamId));
if (!team) {
return { authorized: false, authData: undefined };
}
Expand All @@ -92,7 +92,7 @@ namespace AccessLevels {

const team = await (
await db
).findObjectById<Team>(CollectionId.Teams, new ObjectId(teamId));
).findObjectById(CollectionId.Teams, new ObjectId(teamId));
if (!team) {
return { authorized: false, authData: undefined };
}
Expand All @@ -116,7 +116,7 @@ namespace AccessLevels {

const comp = await (
await db
).findObjectById<Competition>(
).findObjectById(
CollectionId.Competitions,
new ObjectId(compId),
);
Expand Down Expand Up @@ -148,7 +148,7 @@ namespace AccessLevels {

const season = await (
await db
).findObjectById<Season>(CollectionId.Seasons, new ObjectId(seasonId));
).findObjectById(CollectionId.Seasons, new ObjectId(seasonId));
if (!season) {
return { authorized: false, authData: undefined };
}
Expand Down Expand Up @@ -177,7 +177,7 @@ namespace AccessLevels {

const match = await (
await db
).findObjectById<Match>(CollectionId.Matches, new ObjectId(matchId));
).findObjectById(CollectionId.Matches, new ObjectId(matchId));
if (!match) {
return { authorized: false, authData: undefined };
}
Expand Down Expand Up @@ -211,7 +211,7 @@ namespace AccessLevels {

const report = await (
await db
).findObjectById<Report>(CollectionId.Reports, new ObjectId(reportId));
).findObjectById(CollectionId.Reports, new ObjectId(reportId));
if (!report) {
return { authorized: false, authData: undefined };
}
Expand Down Expand Up @@ -240,7 +240,7 @@ namespace AccessLevels {

const comp = await (
await db
).findObjectById<Competition>(
).findObjectById(
CollectionId.Competitions,
new ObjectId(compId),
);
Expand Down Expand Up @@ -272,7 +272,7 @@ namespace AccessLevels {

const match = await (
await db
).findObjectById<Match>(CollectionId.Matches, new ObjectId(matchId));
).findObjectById(CollectionId.Matches, new ObjectId(matchId));
if (!match) {
return { authorized: false, authData: undefined };
}
Expand Down Expand Up @@ -306,7 +306,7 @@ namespace AccessLevels {

const pitReport = await (
await db
).findObjectById<Pitreport>(
).findObjectById(
CollectionId.PitReports,
new ObjectId(pitReportId),
);
Expand Down Expand Up @@ -343,7 +343,7 @@ namespace AccessLevels {

const report = await (
await db
).findObjectById<Report>(CollectionId.Reports, new ObjectId(reportId));
).findObjectById(CollectionId.Reports, new ObjectId(reportId));
if (!report) {
return { authorized: false, authData: undefined };
}
Expand Down Expand Up @@ -372,7 +372,7 @@ namespace AccessLevels {

const report = await (
await db
).findObjectById<SubjectiveReport>(
).findObjectById(
CollectionId.SubjectiveReports,
new ObjectId(reportId),
);
Expand Down Expand Up @@ -404,7 +404,7 @@ namespace AccessLevels {

const picklist = await (
await db
).findObjectById<CompPicklistGroup>(
).findObjectById(
CollectionId.Picklists,
new ObjectId(picklistId),
);
Expand Down
14 changes: 7 additions & 7 deletions lib/api/ApiUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@ export function ownsTeam(team?: Team | null, user?: User) {
}

export function getCompFromReport(db: DbInterface, report: Report) {
return db.findObject<Competition>(CollectionId.Competitions, {
return db.findObject(CollectionId.Competitions, {
matches: report.match?.toString(),
});
}

export function getCompFromMatch(db: DbInterface, match: Match) {
return db.findObject<Competition>(CollectionId.Competitions, {
return db.findObject(CollectionId.Competitions, {
matches: match._id?.toString(),
});
}

export function getCompFromPitReport(db: DbInterface, report: Pitreport) {
return db.findObject<Competition>(CollectionId.Competitions, {
return db.findObject(CollectionId.Competitions, {
pitReports: report._id?.toString(),
});
}
Expand All @@ -57,7 +57,7 @@ export function getCompFromSubjectiveReport(
report: SubjectiveReport,
) {
return db
.findObject<Match>(CollectionId.Matches, {
.findObject(CollectionId.Matches, {
subjectiveReports: report._id?.toString(),
})
.then((match) => {
Expand All @@ -71,19 +71,19 @@ export function getCompFromPicklist(
db: DbInterface,
picklist: CompPicklistGroup,
) {
return db.findObject<Competition>(CollectionId.Competitions, {
return db.findObject(CollectionId.Competitions, {
picklist: picklist._id?.toString(),
});
}

export function getSeasonFromComp(db: DbInterface, comp: Competition) {
return db.findObject<Season>(CollectionId.Seasons, {
return db.findObject(CollectionId.Seasons, {
competitions: comp?._id?.toString(), // Specifying one value is effectively includes for arrays
});
}

export function getTeamFromSeason(db: DbInterface, season: Season) {
return db.findObject<Team>(CollectionId.Teams, {
return db.findObject(CollectionId.Teams, {
seasons: season._id?.toString(),
});
}
Expand Down
Loading

0 comments on commit 943f121

Please sign in to comment.