added script to reset and seed database (thanks opencode)
This commit is contained in:
@@ -1,14 +1,105 @@
|
||||
import { integer, sqliteTable, text } from 'drizzle-orm/sqlite-core';
|
||||
import { sql, eq } from 'drizzle-orm';
|
||||
import { integer, sqliteTable, text, sqliteView } from 'drizzle-orm/sqlite-core';
|
||||
|
||||
export const houses = sqliteTable('houses', {
|
||||
export const teams = sqliteTable('teams', {
|
||||
id: integer('id').primaryKey({ autoIncrement: true }),
|
||||
name: text('name').notNull(),
|
||||
color: text('color').notNull().default('white'),
|
||||
color: text('color').notNull().default('white')
|
||||
});
|
||||
|
||||
export const divisions = sqliteTable('divisions', {
|
||||
id: integer('id').primaryKey({ autoIncrement: true }),
|
||||
name: text('name').notNull()
|
||||
});
|
||||
|
||||
export const eventAttributions = sqliteTable('eventAttributions', {
|
||||
id: integer('id').primaryKey({ autoIncrement: true }),
|
||||
eventID: integer('eventID')
|
||||
.references(() => events.id)
|
||||
.notNull(),
|
||||
playerID: integer('playerID')
|
||||
.references(() => players.id)
|
||||
.notNull(),
|
||||
placement: integer('placement')
|
||||
.references(() => scoringPresets.placement)
|
||||
.default(0)
|
||||
.notNull()
|
||||
});
|
||||
|
||||
export const scoringPresets = sqliteTable('scoringPresets', {
|
||||
id: integer('id').primaryKey({ autoIncrement: true }),
|
||||
presetID: integer('preset').notNull(),
|
||||
placement: integer('placement').notNull().default(0),
|
||||
points: integer('points').notNull().default(0)
|
||||
});
|
||||
|
||||
export const consests = sqliteTable('contests', {
|
||||
export const events = sqliteTable('events', {
|
||||
id: integer('id').primaryKey({ autoIncrement: true }),
|
||||
event: text('event').notNull()
|
||||
// house:dd make someting idk
|
||||
name: text('name').notNull(),
|
||||
preset: integer('preset')
|
||||
.references(() => scoringPresets.presetID)
|
||||
.notNull(),
|
||||
division: integer('division')
|
||||
.references(() => divisions.id)
|
||||
.notNull(),
|
||||
order: integer('order').notNull(),
|
||||
state: integer('state').default(0).notNull(),
|
||||
timeCompleted: integer('timeCompleted')
|
||||
});
|
||||
|
||||
export const players = sqliteTable('players', {
|
||||
id: integer('id').primaryKey({ autoIncrement: true }),
|
||||
firstName: text('firstName').notNull(),
|
||||
lastName: text('lastName').notNull(),
|
||||
team: integer('team')
|
||||
.references(() => teams.id)
|
||||
.notNull(),
|
||||
division: integer('division')
|
||||
.references(() => divisions.id)
|
||||
.notNull()
|
||||
});
|
||||
|
||||
export const scorers = sqliteTable('scorers', {
|
||||
id: integer('id').primaryKey({ autoIncrement: true }),
|
||||
firstName: text('firstName').notNull(),
|
||||
lastName: text('lastName').notNull(),
|
||||
email: text('email').notNull(),
|
||||
password: text('password').notNull(),
|
||||
displayName: text('displayName').notNull(),
|
||||
role: text('role').notNull().default('scorer')
|
||||
});
|
||||
|
||||
export const ledger = sqliteTable('ledger', {
|
||||
id: integer('id').primaryKey({ autoIncrement: true }),
|
||||
timestamp: integer('timestamp', { mode: 'timestamp' })
|
||||
.notNull()
|
||||
.default(sql`(unixepoch())`),
|
||||
type: text('type').notNull().default('event'),
|
||||
event: integer('event').references(() => events.id),
|
||||
scorer: text('scorer').references(() => scorers.id)
|
||||
});
|
||||
|
||||
export const ledgerScores = sqliteTable('ledgerScores', {
|
||||
id: integer('id').primaryKey({ autoIncrement: true }),
|
||||
ledgerID: integer('ledgerID')
|
||||
.references(() => ledger.id)
|
||||
.notNull(),
|
||||
team: integer('team')
|
||||
.references(() => teams.id)
|
||||
.notNull(),
|
||||
placement: integer('placement').references(() => scoringPresets.placement),
|
||||
points: integer('points').notNull().default(0)
|
||||
});
|
||||
|
||||
export const teamScoresView = sqliteView('teamScoresView').as((qb) => {
|
||||
return qb
|
||||
.select({
|
||||
teamId: teams.id,
|
||||
teamName: teams.name,
|
||||
teamColor: teams.color,
|
||||
totalPoints: sql<number>`sum(${ledgerScores.points})`.mapWith(Number).as('totalPoints')
|
||||
})
|
||||
.from(teams)
|
||||
.leftJoin(ledgerScores, eq(teams.id, ledgerScores.team))
|
||||
.groupBy(teams.id);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user