Files
RushStatistics/backend/src/db.ts
2024-06-08 12:20:49 -07:00

103 lines
2.9 KiB
TypeScript

import { Sequelize, Model, DataTypes } from 'sequelize'
export type RoleName =
| 'Fixer'
| 'Tech'
| 'Medtech'
| 'Media'
| 'Netrunner'
| 'Solo'
| 'Nomad'
| 'Exec'
| 'Lawman'
| 'Rocker'
export const roleNames: RoleName[] = [
'Fixer',
'Tech',
'Medtech',
'Media',
'Netrunner',
'Solo',
'Nomad',
'Exec',
'Lawman',
'Rocker'
]
const databasePath = './testdb.db'
export const database = new Sequelize({
dialect: 'sqlite',
storage: databasePath
})
export const Character = database.define(
'Characters',
{
id: { type: DataTypes.INTEGER, primaryKey: true },
characterName: { type: DataTypes.TEXT },
playerName: { type: DataTypes.TEXT },
role: { type: DataTypes.TEXT },
creationDate: { type: DataTypes.INTEGER },
status: { type: DataTypes.TEXT }
},
{ timestamps: false }
)
export const Game = database.define(
'Games',
{
id: { type: DataTypes.INTEGER, primaryKey: true },
title: { type: DataTypes.TEXT },
status: { type: DataTypes.TEXT },
fix: { type: DataTypes.BOOLEAN },
postdate: { type: DataTypes.INTEGER },
gamemaster: { type: DataTypes.TEXT },
payoutEB: { type: DataTypes.INTEGER },
payoutIP: { type: DataTypes.INTEGER },
payoutLoot: { type: DataTypes.INTEGER }
},
{ timestamps: false }
)
export const Pick = database.define(
'Picks',
{
gameId: { type: DataTypes.INTEGER, primaryKey: true },
gameTitle: { type: DataTypes.TEXT },
characterId: { type: DataTypes.INTEGER, primaryKey: true },
characterName: { type: DataTypes.TEXT }
},
{ timestamps: false }
)
export const App = database.define(
'Apps',
{
gameId: { type: DataTypes.INTEGER, primaryKey: true },
gameTitle: { type: DataTypes.TEXT },
characterId: { type: DataTypes.INTEGER, primaryKey: true },
characterName: { type: DataTypes.TEXT }
},
{ timestamps: false }
)
// Bind characters to applications and picks
Character.hasMany(App, { foreignKey: 'characterId', as: 'appliedCharacter' })
App.belongsTo(Character, { foreignKey: 'characterId', as: 'appliedCharacter' })
Character.hasMany(Pick, { foreignKey: 'characterId', as: 'pickedCharacter' })
Pick.belongsTo(Character, { foreignKey: 'characterId', as: 'pickedCharacter' })
// Bind games to applications and picks
Game.hasMany(App, { foreignKey: 'gameId', as: 'gameApplications' })
App.belongsTo(Game, { foreignKey: 'gameId', as: 'gameApplications' })
Game.hasMany(Pick, { foreignKey: 'gameId', as: 'gamePicks' })
Pick.belongsTo(Game, { foreignKey: 'gameId', as: 'gamePicks' })
// Bind picked characters to games.
Game.belongsToMany(Character, { through: 'Apps', as: 'characterAppliedForGame' })
Character.belongsToMany(Game, { through: 'Apps', as: 'characterAppliedForGame' })
Game.belongsToMany(Character, { through: 'Picks', as: 'characterPickedForGame' })
Character.belongsToMany(Game, { through: 'Picks', as: 'characterPickedForGame' })