move db setup to its own file
This commit is contained in:
@@ -1,84 +1,6 @@
|
|||||||
import express from 'express'
|
import express from 'express'
|
||||||
// import sqlite3 from 'sqlite3'
|
|
||||||
import { Sequelize, Model, DataTypes } from 'sequelize'
|
|
||||||
import { json } from 'body-parser'
|
import { json } from 'body-parser'
|
||||||
|
import { database, Character, Game, Pick, App } from './db'
|
||||||
const databasePath = './testdb.db'
|
|
||||||
let database
|
|
||||||
|
|
||||||
const sequelize = new Sequelize({
|
|
||||||
dialect: 'sqlite',
|
|
||||||
storage: databasePath
|
|
||||||
})
|
|
||||||
|
|
||||||
const Character = sequelize.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 }
|
|
||||||
)
|
|
||||||
|
|
||||||
const Game = sequelize.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 },
|
|
||||||
payputLoot: { type: DataTypes.INTEGER }
|
|
||||||
},
|
|
||||||
{ timestamps: false }
|
|
||||||
)
|
|
||||||
|
|
||||||
const Pick = sequelize.define(
|
|
||||||
'Picks',
|
|
||||||
{
|
|
||||||
gameId: { type: DataTypes.INTEGER, primaryKey: true },
|
|
||||||
gameTitle: { type: DataTypes.TEXT },
|
|
||||||
characterId: { type: DataTypes.INTEGER, primaryKey: true },
|
|
||||||
characterName: { type: DataTypes.TEXT }
|
|
||||||
},
|
|
||||||
{ timestamps: false }
|
|
||||||
)
|
|
||||||
|
|
||||||
const App = sequelize.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' })
|
|
||||||
|
|
||||||
const app = express()
|
const app = express()
|
||||||
const jsonParser = json()
|
const jsonParser = json()
|
||||||
@@ -166,6 +88,6 @@ app.post('/api/character/:characterId/gameHistory', async (req, res) => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
app.listen(port, async () => {
|
app.listen(port, async () => {
|
||||||
await sequelize.authenticate()
|
await database.authenticate()
|
||||||
return console.log(`Express is listening at http://localhost:${port}`)
|
return console.log(`Express is listening at http://localhost:${port}`)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -42,11 +42,12 @@ import axios from 'axios'
|
|||||||
const route = useRoute()
|
const route = useRoute()
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
|
|
||||||
let count = 10
|
|
||||||
const characters = ref({})
|
const characters = ref({})
|
||||||
const pageCount = ref(1)
|
const pageCount = ref(1)
|
||||||
const page = ref(1)
|
const page = ref(1)
|
||||||
|
|
||||||
|
let count = 10
|
||||||
|
|
||||||
async function loadData() {
|
async function loadData() {
|
||||||
const response = await axios.post('/api/character', {
|
const response = await axios.post('/api/character', {
|
||||||
page: `${page.value-1}`,
|
page: `${page.value-1}`,
|
||||||
|
|||||||
Reference in New Issue
Block a user