add rounds and prepare for frontend

This commit is contained in:
unurled 2025-07-05 23:58:07 +02:00
parent a8d502f2ee
commit f723d90e65
9 changed files with 207 additions and 164 deletions

View file

@ -7,6 +7,7 @@ import * as fields from './schema/fields';
import * as matches from './schema/matches';
import * as permissions from './schema/permissions';
import * as roles from './schema/roles';
import * as rounds from './schema/rounds';
import * as schedulingmodes from './schema/schedulingmodes';
import * as sessions from './schema/sessions';
import * as teams from './schema/teams';
@ -24,6 +25,7 @@ export const db = drizzle(client, {
...matches,
...permissions,
...roles,
...rounds,
...schedulingmodes,
...sessions,
...teams,

View file

@ -1,12 +1,12 @@
import * as t from 'drizzle-orm/pg-core';
import { timestamps } from '../util';
import { relations } from 'drizzle-orm';
import { eq, relations } from 'drizzle-orm';
import { users } from './users';
import { schedulingModes } from './schedulingmodes';
import { matches } from './matches';
import { breakperiods } from './breakperiods';
import { fields } from './fields';
import { teams } from './teams';
import { rounds } from './rounds';
import { db } from '..';
export const competitions = t.pgTable('competitions', {
id: t.serial('id').primaryKey(),
@ -14,21 +14,55 @@ export const competitions = t.pgTable('competitions', {
description: t.text('description'),
start_date: t.timestamp({ withTimezone: true, mode: 'date' }).notNull(),
location: t.text('location'),
current_scheduling_mode_id: t.integer('current_scheduling_mode_id').notNull(),
owner: t.text('owner').notNull(),
...timestamps
});
export const competitionsRelations = relations(competitions, ({ one, many }) => ({
scheduling_modes: many(schedulingModes),
owner: one(users, {
fields: [competitions.owner],
references: [users.id]
}),
matches: many(matches),
breakperiods: many(breakperiods),
fields: many(fields),
teams: many(teams)
teams: many(teams),
rounds: many(rounds)
}));
export type Competition = typeof competitions.$inferSelect;
export async function getCompetitionWithAll(id: number) {
return await db.query.competitions.findFirst({
where: eq(competitions.id, id),
with: {
breakperiods: true,
fields: true,
rounds: {
with: {
matches: true,
schedulingMode: true
}
},
teams: true
}
});
}
export async function getCompetitionsWithAll(skip: number = 0, take: number = 10) {
return await db.query.competitions.findMany({
orderBy: (competitions, { desc }) => [desc(competitions.start_date)],
with: {
breakperiods: true,
fields: true,
rounds: {
with: {
matches: true,
schedulingMode: true
}
},
teams: true
},
limit: take,
offset: skip
});
}

View file

@ -1,16 +1,13 @@
import * as t from 'drizzle-orm/pg-core';
import { timestamps } from '../util';
import { competitions } from './competitions';
import { relations } from 'drizzle-orm';
import { fields } from './fields';
import { schedulingModes } from './schedulingmodes';
import { rounds } from './rounds';
export const status = t.pgEnum('match_status', ['pending', 'running', 'finished', 'cancelled']);
export const matches = t.pgTable('match', {
export const matches = t.pgTable('matches', {
id: t.serial('id').primaryKey(),
competition_id: t.integer('competition_id').notNull(),
scheduling_mode_id: t.integer('scheduling_mode_id').notNull(),
field_id: t.integer('field_id').notNull(),
team1: t.text('team1').notNull(),
team2: t.text('team2').notNull(),
@ -20,21 +17,18 @@ export const matches = t.pgTable('match', {
score2: t.integer('score2').notNull(),
status: status('status').notNull(),
match_number: t.integer('match_number').notNull(),
round_id: t.integer('round').notNull(),
...timestamps
});
export const matchRelations = relations(matches, ({ one }) => ({
competition: one(competitions, {
fields: [matches.competition_id],
references: [competitions.id]
}),
field: one(fields, {
fields: [matches.field_id],
references: [fields.id]
}),
scheduling_mode: one(schedulingModes, {
fields: [matches.scheduling_mode_id],
references: [schedulingModes.id]
round: one(rounds, {
fields: [matches.round_id],
references: [rounds.id]
})
}));

View file

@ -0,0 +1,24 @@
import { relations } from 'drizzle-orm';
import * as t from 'drizzle-orm/pg-core';
import { matches } from './matches';
import { competitions } from './competitions';
import { schedulingModes } from './schedulingmodes';
export const rounds = t.pgTable('rounds', {
id: t.serial('id').primaryKey(),
nb_matches: t.integer('nb_matches').notNull(),
competition_id: t.integer('competition_id').notNull(),
scheduling_mode: t.integer('scheduling_mode').notNull()
});
export const roundsRelations = relations(rounds, ({ many, one }) => ({
competition: one(competitions, {
fields: [rounds.competition_id],
references: [competitions.id]
}),
matches: many(matches),
schedulingMode: one(schedulingModes, {
fields: [rounds.scheduling_mode],
references: [schedulingModes.id]
})
}));

View file

@ -1,24 +1,24 @@
import * as t from 'drizzle-orm/pg-core';
import { timestamps } from '../util';
import { competitions } from './competitions';
import { relations } from 'drizzle-orm';
import { rounds } from './rounds';
export const schedulingModes = t.pgTable('scheduling_modes', {
id: t.serial('id').primaryKey(),
competition_id: t.integer('competition_id').notNull(),
name: t.text('name').notNull(),
description: t.text('description'),
algorithm: t.text('algorithm').notNull(),
config: t.jsonb('config').notNull(),
sequence_order: t.integer('sequence_order').notNull(),
status: t.text('status').notNull(),
round_id: t.integer('round_id').notNull(),
...timestamps
});
export const schedulingModesRelations = relations(schedulingModes, ({ one }) => ({
competition: one(competitions, {
fields: [schedulingModes.competition_id],
references: [competitions.id]
round: one(rounds, {
fields: [schedulingModes.round_id],
references: [rounds.id]
})
}));